Search in sources :

Example 36 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.realm;
    realm.beginTransaction();
    AllTypesRealmModel model = realm.createObject(AllTypesRealmModel.class, 0);
    realm.commitTransaction();
    looperThread.keepStrongReference.add(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 37 with RunTestInLooperThread

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

the class NotificationsTest method globalListener_looperThread_triggeredByRemoteCommit.

@Test
@RunTestInLooperThread
public void globalListener_looperThread_triggeredByRemoteCommit() {
    final AtomicInteger success = new AtomicInteger(0);
    Realm realm = looperThread.realm;
    realm.addChangeListener(new RealmChangeListener<Realm>() {

        @Override
        public void onChange(Realm object) {
            assertEquals(1, success.get());
            looperThread.testComplete();
        }
    });
    realm.executeTransactionAsync(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            realm.createObject(AllTypes.class);
        }
    });
    assertEquals(0, success.getAndIncrement());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AllTypes(io.realm.entities.AllTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test)

Example 38 with RunTestInLooperThread

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

the class NotificationsTest method asyncRealmObjectShouldNotBlockBackgroundCommitNotification.

// The presence of async RealmResults blocks any `REALM_CHANGE` notification . We make sure in this test that all
// Realm listeners will be notified regardless of the presence of an async RealmObject. RealmObjects are special
// in the sense that once you got a row accessor to that object, it is automatically up to date as soon as you
// call advance_read().
@Test
@RunTestInLooperThread
public void asyncRealmObjectShouldNotBlockBackgroundCommitNotification() {
    final AtomicInteger numberOfRealmCallbackInvocation = new AtomicInteger(0);
    final CountDownLatch signalClosedRealm = new CountDownLatch(1);
    final Realm realm = looperThread.realm;
    realm.addChangeListener(new RealmChangeListener<Realm>() {

        @Override
        public void onChange(final Realm realm) {
            switch(numberOfRealmCallbackInvocation.incrementAndGet()) {
                case 1:
                    {
                        // First commit.
                        Dog dog = realm.where(Dog.class).findFirstAsync();
                        assertTrue(dog.load());
                        dog.addChangeListener(new RealmChangeListener<Dog>() {

                            @Override
                            public void onChange(Dog dog) {
                            }
                        });
                        new Thread() {

                            @Override
                            public void run() {
                                Realm threadRealm = Realm.getInstance(realm.getConfiguration());
                                threadRealm.beginTransaction();
                                threadRealm.createObject(Dog.class);
                                threadRealm.commitTransaction();
                                threadRealm.close();
                                signalClosedRealm.countDown();
                            }
                        }.start();
                        break;
                    }
                case 2:
                    {
                        // Finishes test.
                        TestHelper.awaitOrFail(signalClosedRealm);
                        looperThread.testComplete();
                        break;
                    }
            }
        }
    });
    looperThread.postRunnable(new Runnable() {

        @Override
        public void run() {
            realm.beginTransaction();
            realm.createObject(Dog.class);
            realm.commitTransaction();
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) Dog(io.realm.entities.Dog) RunInLooperThread(io.realm.rule.RunInLooperThread) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) HandlerThread(android.os.HandlerThread) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test)

Example 39 with RunTestInLooperThread

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

the class NotificationsTest method realmObjectListenerAddedAfterCommit.

// TODO: Fix or delete this test after integration of object notification from Object Store
@Test
@RunTestInLooperThread
@Ignore
public void realmObjectListenerAddedAfterCommit() {
    Realm realm = looperThread.realm;
    realm.beginTransaction();
    AllTypes obj = realm.createObject(AllTypes.class);
    realm.commitTransaction();
    realm.beginTransaction();
    obj.setColumnLong(42);
    realm.commitTransaction();
    obj.addChangeListener(new RealmChangeListener<AllTypes>() {

        @Override
        public void onChange(AllTypes object) {
            looperThread.testComplete();
        }
    });
}
Also used : AllTypes(io.realm.entities.AllTypes) Ignore(org.junit.Ignore) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test)

Example 40 with RunTestInLooperThread

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

the class NotificationsTest method asyncRealmResultsShouldNotBlockBackgroundCommitNotification.

// The presence of async RealmResults block any `REALM_CHANGE` notification causing historically the Realm
// to advance to the latest version. We make sure in this test that all Realm listeners will be notified
// regardless of the presence of an async RealmResults that will delay the `REALM_CHANGE` sometimes.
@Test
@RunTestInLooperThread
public void asyncRealmResultsShouldNotBlockBackgroundCommitNotification() {
    final Realm realm = looperThread.realm;
    final RealmResults<Dog> dogs = realm.where(Dog.class).findAllAsync();
    final AtomicBoolean resultsListenerDone = new AtomicBoolean(false);
    final AtomicBoolean realmListenerDone = new AtomicBoolean(false);
    looperThread.keepStrongReference.add(dogs);
    assertTrue(dogs.load());
    assertEquals(0, dogs.size());
    dogs.addChangeListener(new RealmChangeListener<RealmResults<Dog>>() {

        @Override
        public void onChange(RealmResults<Dog> results) {
            if (dogs.size() == 2) {
                // Results has the latest changes.
                resultsListenerDone.set(true);
                if (realmListenerDone.get()) {
                    looperThread.testComplete();
                }
            }
        }
    });
    realm.addChangeListener(new RealmChangeListener<Realm>() {

        @Override
        public void onChange(Realm element) {
            if (dogs.size() == 1) {
                // Step 2. Creates the second dog.
                realm.executeTransactionAsync(new Realm.Transaction() {

                    @Override
                    public void execute(Realm realm) {
                        realm.createObject(Dog.class);
                    }
                });
            } else if (dogs.size() == 2) {
                // Realm listener can see the latest changes.
                realmListenerDone.set(true);
                if (resultsListenerDone.get()) {
                    looperThread.testComplete();
                }
            }
        }
    });
    // Step 1. Creates the first dog.
    realm.executeTransactionAsync(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            realm.createObject(Dog.class);
        }
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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