use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class NotificationsTest method globalListener_looperThread_triggeredByLocalCommit.
@Test
@RunTestInLooperThread
public void globalListener_looperThread_triggeredByLocalCommit() {
final AtomicInteger success = new AtomicInteger(0);
Realm realm = looperThread.realm;
realm.addChangeListener(new RealmChangeListener<Realm>() {
@Override
public void onChange(Realm object) {
assertEquals(0, success.getAndIncrement());
looperThread.testComplete();
}
});
realm.beginTransaction();
realm.createObject(AllTypes.class);
realm.commitTransaction();
assertEquals(1, success.get());
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class RealmObjectTests method removeAllChangeListeners.
/**
* This test is to see if RealmObject.removeChangeListeners() works as it is intended.
*/
@Test
@RunTestInLooperThread
public void removeAllChangeListeners() {
final Realm realm = looperThread.realm;
realm.beginTransaction();
Dog dog = realm.createObject(Dog.class);
dog.setAge(13);
realm.commitTransaction();
dog.addChangeListener(new RealmChangeListener<Dog>() {
@Override
public void onChange(Dog object) {
assertTrue(false);
}
});
dog.removeAllChangeListeners();
realm.beginTransaction();
Dog sameDog = realm.where(Dog.class).equalTo(Dog.FIELD_AGE, 13).findFirst();
sameDog.setName("Jesper");
realm.commitTransaction();
looperThread.testComplete();
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class RealmListTests method removeChangeListener.
@Test
@RunTestInLooperThread
public void removeChangeListener() {
collection = prepareRealmListInLooperThread();
Realm realm = looperThread.realm;
final AtomicInteger listenerCalledCount = new AtomicInteger(0);
RealmChangeListener<RealmList<Dog>> listener1 = new RealmChangeListener<RealmList<Dog>>() {
@Override
public void onChange(RealmList<Dog> element) {
fail();
}
};
OrderedRealmCollectionChangeListener<RealmList<Dog>> listener2 = new OrderedRealmCollectionChangeListener<RealmList<Dog>>() {
@Override
public void onChange(RealmList<Dog> collection, OrderedCollectionChangeSet changes) {
assertEquals(0, listenerCalledCount.getAndIncrement());
}
};
collection.addChangeListener(listener1);
collection.addChangeListener(listener2);
realm.beginTransaction();
collection.get(0).setAge(42);
realm.commitTransaction();
collection.removeChangeListener(listener1);
// This should trigger the listener if there is any.
realm.beginTransaction();
realm.cancelTransaction();
assertEquals(1, listenerCalledCount.get());
looperThread.testComplete();
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class RealmObjectTests method removeChangeListener_throwOnUnmanagedObject.
@Test
@RunTestInLooperThread
public void removeChangeListener_throwOnUnmanagedObject() {
Dog dog = new Dog();
RealmChangeListener listener = new RealmChangeListener<Dog>() {
@Override
public void onChange(Dog object) {
}
};
try {
dog.removeChangeListener(listener);
fail("Failed to remove a listener from null Realm.");
} catch (IllegalArgumentException ignore) {
looperThread.testComplete();
}
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class RealmObjectTests method removeChangeListener_throwOnRemovingNullListenerFromLooperThread.
@Test
@RunTestInLooperThread
public void removeChangeListener_throwOnRemovingNullListenerFromLooperThread() {
final Realm realm = looperThread.realm;
Dog dog = createManagedDogObjectFromRealmInstance(realm);
try {
dog.removeChangeListener(null);
fail("removing null change listener must throw an exception.");
} catch (IllegalArgumentException ignore) {
} finally {
looperThread.testComplete();
}
}
Aggregations