Search in sources :

Example 41 with RunTestInLooperThread

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test)

Example 42 with RunTestInLooperThread

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();
}
Also used : Dog(io.realm.entities.Dog) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test)

Example 43 with RunTestInLooperThread

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();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Dog(io.realm.entities.Dog) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 44 with RunTestInLooperThread

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();
    }
}
Also used : Dog(io.realm.entities.Dog) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test)

Example 45 with RunTestInLooperThread

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();
    }
}
Also used : Dog(io.realm.entities.Dog) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test)

Aggregations

RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)109 Test (org.junit.Test)109 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)46 UiThreadTest (android.support.test.annotation.UiThreadTest)37 AllTypes (io.realm.entities.AllTypes)35 Dog (io.realm.entities.Dog)30 AllJavaTypes (io.realm.entities.AllJavaTypes)9 Action1 (rx.functions.Action1)9 RunInLooperThread (io.realm.rule.RunInLooperThread)8 AnnotationIndexTypes (io.realm.entities.AnnotationIndexTypes)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 SharedRealm (io.realm.internal.SharedRealm)5 ObjectServerError (io.realm.ObjectServerError)4 SyncUser (io.realm.SyncUser)4 AllTypesPrimaryKey (io.realm.entities.AllTypesPrimaryKey)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 SyncCredentials (io.realm.SyncCredentials)3 Date (java.util.Date)3 ClientResetHandler (io.realm.ClientResetHandler)2