Search in sources :

Example 51 with RunTestInLooperThread

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

the class RealmChangeListenerTests method returnedRealmModelIsNotNull.

@Test
@RunTestInLooperThread
public void returnedRealmModelIsNotNull() {
    Realm realm = looperThread.getRealm();
    realm.beginTransaction();
    AllTypesRealmModel model = realm.createObject(AllTypesRealmModel.class, 0);
    realm.commitTransaction();
    looperThread.keepStrongReference(model);
    RealmObject.addChangeListener(model, new RealmChangeListener<AllTypesRealmModel>() {

        @Override
        public void onChange(AllTypesRealmModel object) {
            assertEquals("model1", object.columnString);
            looperThread.testComplete();
        }
    });
    realm.beginTransaction();
    model.columnString = "model1";
    realm.commitTransaction();
}
Also used : AllTypesRealmModel(io.realm.entities.pojo.AllTypesRealmModel) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 52 with RunTestInLooperThread

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

the class RxJavaTests method findAllAsync_emittedOnUpdate.

@Test
@RunTestInLooperThread
public void findAllAsync_emittedOnUpdate() {
    final AtomicInteger subscriberCalled = new AtomicInteger(0);
    Realm realm = looperThread.getRealm();
    subscription = realm.where(AllTypes.class).findAllAsync().asFlowable().subscribe(rxResults -> {
        assertTrue(rxResults.isFrozen());
        if (subscriberCalled.incrementAndGet() == 2) {
            disposeSuccessfulTest(realm);
        }
    });
    realm.beginTransaction();
    realm.createObject(AllTypes.class);
    realm.commitTransaction();
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) RunWith(org.junit.runner.RunWith) Dog(io.realm.entities.Dog) SystemClock(android.os.SystemClock) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) Flowable(io.reactivex.Flowable) RunInLooperThread(io.realm.rule.RunInLooperThread) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers(io.reactivex.schedulers.Schedulers) Pair(io.realm.internal.util.Pair) Assert.fail(org.junit.Assert.fail) Before(org.junit.Before) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) RealmLog(io.realm.log.RealmLog) Assert.assertNotNull(org.junit.Assert.assertNotNull) AllTypes(io.realm.entities.AllTypes) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Consumer(io.reactivex.functions.Consumer) CyclicType(io.realm.entities.CyclicType) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) AtomicLong(java.util.concurrent.atomic.AtomicLong) Disposable(io.reactivex.disposables.Disposable) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 53 with RunTestInLooperThread

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

the class RxJavaTests method realmObject_emittedOnSubscribe.

@Test
@RunTestInLooperThread
public void realmObject_emittedOnSubscribe() {
    realm.beginTransaction();
    final AllJavaTypes obj = realm.createObject(AllJavaTypes.class, 42);
    realm.commitTransaction();
    subscription = obj.<AllJavaTypes>asFlowable().subscribe(rxObject -> {
        assertTrue(rxObject.isFrozen());
        // Frozen objects are not equal to their live counter parts.
        assertNotEquals(rxObject, obj);
        assertEquals(rxObject.getFieldId(), obj.getFieldId());
        disposeSuccessfulTest(realm);
    });
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) RunWith(org.junit.runner.RunWith) Dog(io.realm.entities.Dog) SystemClock(android.os.SystemClock) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) Flowable(io.reactivex.Flowable) RunInLooperThread(io.realm.rule.RunInLooperThread) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers(io.reactivex.schedulers.Schedulers) Pair(io.realm.internal.util.Pair) Assert.fail(org.junit.Assert.fail) Before(org.junit.Before) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) RealmLog(io.realm.log.RealmLog) Assert.assertNotNull(org.junit.Assert.assertNotNull) AllTypes(io.realm.entities.AllTypes) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Consumer(io.reactivex.functions.Consumer) CyclicType(io.realm.entities.CyclicType) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) AtomicLong(java.util.concurrent.atomic.AtomicLong) Disposable(io.reactivex.disposables.Disposable) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertEquals(org.junit.Assert.assertEquals) AllJavaTypes(io.realm.entities.AllJavaTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 54 with RunTestInLooperThread

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

the class RxJavaTests method realmList_parentDeletionCompleteFlowable.

@Test
@RunTestInLooperThread
public void realmList_parentDeletionCompleteFlowable() {
    realm.beginTransaction();
    final AllTypes parent = realm.createObject(AllTypes.class);
    final RealmList<Dog> list = parent.getColumnRealmList();
    list.add(new Dog("Fido"));
    realm.commitTransaction();
    looperThread.keepStrongReference(parent);
    // We should only emit valid lists. If the parent of the list is invalidated
    // it should close the stream gracefully resulting in onComplete being called.
    subscription = list.asFlowable().subscribe(change -> {
        assertTrue(change.isValid());
    }, error -> {
        fail(error.toString());
    }, () -> {
        // Deleting the parent will gracefully close the stream
        disposeSuccessfulTest(realm);
    });
    looperThread.postRunnable(() -> {
        realm.beginTransaction();
        parent.deleteFromRealm();
        realm.commitTransaction();
    });
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) RunWith(org.junit.runner.RunWith) Dog(io.realm.entities.Dog) SystemClock(android.os.SystemClock) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) Flowable(io.reactivex.Flowable) RunInLooperThread(io.realm.rule.RunInLooperThread) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers(io.reactivex.schedulers.Schedulers) Pair(io.realm.internal.util.Pair) Assert.fail(org.junit.Assert.fail) Before(org.junit.Before) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) RealmLog(io.realm.log.RealmLog) Assert.assertNotNull(org.junit.Assert.assertNotNull) AllTypes(io.realm.entities.AllTypes) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Consumer(io.reactivex.functions.Consumer) CyclicType(io.realm.entities.CyclicType) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) AtomicLong(java.util.concurrent.atomic.AtomicLong) Disposable(io.reactivex.disposables.Disposable) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertEquals(org.junit.Assert.assertEquals) AllTypes(io.realm.entities.AllTypes) Dog(io.realm.entities.Dog) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 55 with RunTestInLooperThread

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.getRealm();
    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(collection);
    collection.addChangeListener(listener);
    collection.addChangeListener(listener);
    collection.removeChangeListener(listener);
    realm.beginTransaction();
    realm.createObject(AllTypes.class);
    realm.commitTransaction();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AllTypes(io.realm.entities.AllTypes) DictionaryAllTypes(io.realm.entities.DictionaryAllTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

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