Search in sources :

Example 81 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) {
        }
    };
    RealmObjectChangeListener objectChangeListener = new RealmObjectChangeListener<Dog>() {

        @Override
        public void onChange(Dog object, ObjectChangeSet changeSet) {
        }
    };
    try {
        dog.removeChangeListener(listener);
        fail("Failed to remove a listener from null Realm.");
    } catch (IllegalArgumentException ignore) {
    }
    try {
        dog.removeChangeListener(objectChangeListener);
        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) Test(org.junit.Test)

Example 82 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.getRealm();
    Dog dog = createManagedDogObjectFromRealmInstance(realm);
    try {
        dog.removeChangeListener((RealmChangeListener) null);
        fail("removing null change listener must throw an exception.");
    } catch (IllegalArgumentException ignore) {
    }
    try {
        dog.removeChangeListener((RealmObjectChangeListener) null);
        fail("removing null change listener must throw an exception.");
    } catch (IllegalArgumentException ignore) {
    }
    looperThread.testComplete();
}
Also used : Dog(io.realm.entities.Dog) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 83 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.getRealm();
    realm.beginTransaction();
    Dog dog = realm.createObject(Dog.class);
    dog.setAge(13);
    realm.commitTransaction();
    dog.addChangeListener(new RealmChangeListener<Dog>() {

        @Override
        public void onChange(Dog object) {
            fail();
        }
    });
    dog.addChangeListener(new RealmObjectChangeListener<Dog>() {

        @Override
        public void onChange(Dog object, ObjectChangeSet changeSet) {
            fail();
        }
    });
    dog.removeAllChangeListeners();
    realm.beginTransaction();
    Dog sameDog = realm.where(Dog.class).equalTo(Dog.FIELD_AGE, 13).findFirst();
    sameDog.setName("Jesper");
    realm.commitTransaction();
    // Try to trigger the listeners.
    realm.sharedRealm.refresh();
    looperThread.testComplete();
}
Also used : Dog(io.realm.entities.Dog) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 84 with RunTestInLooperThread

use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.

the class RealmObjectTests method removeAllChangeListeners_thenAdd.

@Test
@RunTestInLooperThread
public void removeAllChangeListeners_thenAdd() {
    final Realm realm = looperThread.getRealm();
    realm.beginTransaction();
    Dog dog = realm.createObject(Dog.class);
    dog.setAge(13);
    realm.commitTransaction();
    dog.addChangeListener(new RealmChangeListener<Dog>() {

        @Override
        public void onChange(Dog object) {
            fail();
        }
    });
    dog.removeAllChangeListeners();
    dog.addChangeListener(new RealmChangeListener<Dog>() {

        @Override
        public void onChange(Dog dog) {
            assertEquals(14, dog.getAge());
            looperThread.testComplete();
        }
    });
    realm.beginTransaction();
    dog.setAge(14);
    realm.commitTransaction();
}
Also used : Dog(io.realm.entities.Dog) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 85 with RunTestInLooperThread

use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.

the class RealmObjectTests method changeListener_triggeredWhenObjectIsDeleted.

@Test
@RunTestInLooperThread
public void changeListener_triggeredWhenObjectIsDeleted() {
    final Realm realm = looperThread.getRealm();
    realm.beginTransaction();
    AllTypes obj = realm.createObject(AllTypes.class);
    realm.commitTransaction();
    obj.addChangeListener(new RealmChangeListener<AllTypes>() {

        @Override
        public void onChange(AllTypes obj) {
            assertFalse(obj.isValid());
            looperThread.testComplete();
        }
    });
    realm.beginTransaction();
    obj.deleteFromRealm();
    realm.commitTransaction();
}
Also used : AllTypes(io.realm.entities.AllTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Aggregations

RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)180 Test (org.junit.Test)180 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)80 AllTypes (io.realm.entities.AllTypes)76 Dog (io.realm.entities.Dog)59 RunInLooperThread (io.realm.rule.RunInLooperThread)30 AllJavaTypes (io.realm.entities.AllJavaTypes)29 AtomicLong (java.util.concurrent.atomic.AtomicLong)23 AndroidJUnit4 (androidx.test.ext.junit.runners.AndroidJUnit4)21 RealmLog (io.realm.log.RealmLog)21 Assert.assertEquals (org.junit.Assert.assertEquals)21 Assert.assertFalse (org.junit.Assert.assertFalse)21 Assert.assertNotNull (org.junit.Assert.assertNotNull)21 Assert.assertTrue (org.junit.Assert.assertTrue)21 Before (org.junit.Before)21 Rule (org.junit.Rule)21 RunWith (org.junit.runner.RunWith)21 Consumer (io.reactivex.functions.Consumer)20 Assert.fail (org.junit.Assert.fail)20 SystemClock (android.os.SystemClock)19