use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class RealmQueryTests method distinctAsync_indexedLinkedFields.
@Test
@RunTestInLooperThread
public void distinctAsync_indexedLinkedFields() {
final long numberOfBlocks = 25;
final long numberOfObjects = 10;
populateForDistinct(realm, numberOfBlocks, numberOfObjects, false);
for (String field : AnnotationIndexTypes.INDEX_FIELDS) {
try {
realm.where(AnnotationIndexTypes.class).distinctAsync(AnnotationIndexTypes.FIELD_OBJECT + "." + field);
fail("Unsupported " + field + " linked field");
} catch (IllegalArgumentException ignored) {
}
}
looperThread.testComplete();
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class RealmResultsTests method removeAllChangeListeners.
@Test
@RunTestInLooperThread
public void removeAllChangeListeners() {
final AtomicInteger listenersTriggered = new AtomicInteger(0);
final Realm realm = looperThread.realm;
RealmResults<AllTypes> collection = realm.where(AllTypes.class).findAll();
RealmChangeListener<RealmResults<AllTypes>> listenerA = new RealmChangeListener<RealmResults<AllTypes>>() {
@Override
public void onChange(RealmResults<AllTypes> object) {
listenersTriggered.incrementAndGet();
}
};
RealmChangeListener<RealmResults<AllTypes>> listenerB = new RealmChangeListener<RealmResults<AllTypes>>() {
@Override
public void onChange(RealmResults<AllTypes> object) {
listenersTriggered.incrementAndGet();
}
};
looperThread.keepStrongReference.add(collection);
collection.addChangeListener(listenerA);
collection.addChangeListener(listenerB);
collection.removeAllChangeListeners();
realm.beginTransaction();
realm.createObject(AllTypes.class);
realm.commitTransaction();
// The above commit should have put a REALM_CHANGED event on the Looper queue before this runnable.
looperThread.postRunnable(new Runnable() {
@Override
public void run() {
if (listenersTriggered.get() == 0) {
looperThread.testComplete();
} else {
fail("Listeners wasn't removed");
}
}
});
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class RealmResultsTests method addChangeListener_twice.
@Test
@RunTestInLooperThread
public void addChangeListener_twice() {
final AtomicInteger listenersTriggered = new AtomicInteger(0);
final Realm realm = looperThread.realm;
RealmResults<AllTypes> collection = realm.where(AllTypes.class).findAll();
RealmChangeListener<RealmResults<AllTypes>> listener = new RealmChangeListener<RealmResults<AllTypes>>() {
@Override
public void onChange(RealmResults<AllTypes> object) {
listenersTriggered.incrementAndGet();
}
};
realm.addChangeListener(new RealmChangeListener<Realm>() {
@Override
public void onChange(Realm object) {
listenersTriggered.incrementAndGet();
looperThread.postRunnable(new Runnable() {
@Override
public void run() {
if (listenersTriggered.get() == 1) {
looperThread.testComplete();
} else {
fail("Only global listener should be triggered");
}
}
});
}
});
// Adding it twice will be ignored, so removing it will not cause the listener to be triggered.
looperThread.keepStrongReference.add(collection);
collection.addChangeListener(listener);
collection.addChangeListener(listener);
collection.removeChangeListener(listener);
realm.beginTransaction();
realm.createObject(AllTypes.class);
realm.commitTransaction();
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class TypeBasedNotificationsTests method callback_should_trigger_for_createObject.
// ****************************************************************************************** //
// UC 0.
// Callback should be notified if we create a RealmObject without the async mechanism.
// ex: using (createObject, copyOrUpdate, createObjectFromJson etc.)
// ***************************************************************************************** //
//UC 0 Uses Realm.createObject.
@Test
@RunTestInLooperThread
public void callback_should_trigger_for_createObject() {
final Realm realm = looperThread.realm;
realm.addChangeListener(new RealmChangeListener<Realm>() {
@Override
public void onChange(Realm object) {
if (globalCommitInvocations.incrementAndGet() == 1) {
looperThread.postRunnable(new Runnable() {
@Override
public void run() {
assertEquals(1, typebasedCommitInvocations.get());
looperThread.testComplete();
}
});
}
}
});
realm.beginTransaction();
final Dog dog = realm.createObject(Dog.class);
realm.commitTransaction();
looperThread.keepStrongReference.add(dog);
dog.addChangeListener(new RealmChangeListener<Dog>() {
@Override
public void onChange(Dog object) {
assertEquals("Akamaru", dog.getName());
typebasedCommitInvocations.incrementAndGet();
}
});
realm.beginTransaction();
dog.setName("Akamaru");
realm.commitTransaction();
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class TypeBasedNotificationsTests method callback_with_relevant_commit_realmresults_sync.
// UC 1 Sync RealmResults.
@Test
@RunTestInLooperThread
public void callback_with_relevant_commit_realmresults_sync() {
final Realm realm = looperThread.realm;
// Step 1: Creates object.
realm.beginTransaction();
final Dog akamaru = realm.createObject(Dog.class);
akamaru.setName("Akamaru");
realm.commitTransaction();
final RealmResults<Dog> dogs = realm.where(Dog.class).findAll();
looperThread.keepStrongReference.add(dogs);
dogs.addChangeListener(new RealmChangeListener<RealmResults<Dog>>() {
@Override
public void onChange(RealmResults<Dog> object) {
// Step 4: Responds to relevant change.
typebasedCommitInvocations.incrementAndGet();
assertEquals(1, dogs.size());
assertEquals("Akamaru", dogs.get(0).getName());
assertEquals(17, dogs.get(0).getAge());
looperThread.testComplete();
}
});
// Step 2: Trigger non-related commit. If this triggered the results listener, assertion will happen there.
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
}
});
// Step 3: Triggers related commit.
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.where(Dog.class).findFirst().setAge(17);
}
});
}
Aggregations