Search in sources :

Example 16 with RunTestInLooperThread

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

the class RealmNotifierTests method post.

@Test
@RunTestInLooperThread
public void post() {
    RealmNotifier notifier = new AndroidRealmNotifier(null, capabilitiesCanDeliver);
    notifier.post(new Runnable() {

        @Override
        public void run() {
            looperThread.testComplete();
        }
    });
}
Also used : AndroidRealmNotifier(io.realm.internal.android.AndroidRealmNotifier) AndroidRealmNotifier(io.realm.internal.android.AndroidRealmNotifier) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 17 with RunTestInLooperThread

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

the class RealmNotifierTests method addChangeListener_byRemoteChanges.

@Test
@RunTestInLooperThread
public void addChangeListener_byRemoteChanges() {
    // To catch https://github.com/realm/realm-java/pull/4037 CI failure.
    // In this case, object store should not send more than 100 notifications.
    final int TIMES = 100;
    final AtomicInteger commitCounter = new AtomicInteger(0);
    final AtomicInteger listenerCounter = new AtomicInteger(0);
    looperThread.realm.close();
    SharedRealm sharedRealm = getSharedRealm(looperThread.realmConfiguration);
    looperThread.keepStrongReference.add(sharedRealm);
    sharedRealm.realmNotifier.addChangeListener(sharedRealm, new RealmChangeListener<SharedRealm>() {

        @Override
        public void onChange(SharedRealm sharedRealm) {
            int commits = commitCounter.get();
            int listenerCount = listenerCounter.addAndGet(1);
            assertEquals(commits, listenerCount);
            if (commits == TIMES) {
                sharedRealm.close();
                looperThread.testComplete();
            } else {
                makeRemoteChanges(looperThread.realmConfiguration);
                commitCounter.getAndIncrement();
            }
        }
    });
    makeRemoteChanges(looperThread.realmConfiguration);
    commitCounter.getAndIncrement();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 18 with RunTestInLooperThread

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

the class RealmNotifierTests method addChangeListener_byLocalChanges.

// Callback is immediately called when commitTransaction for local changes.
@Test
@RunTestInLooperThread
public void addChangeListener_byLocalChanges() {
    final AtomicBoolean commitReturns = new AtomicBoolean(false);
    SharedRealm sharedRealm = getSharedRealm(looperThread.realmConfiguration);
    sharedRealm.realmNotifier.addChangeListener(sharedRealm, new RealmChangeListener<SharedRealm>() {

        @Override
        public void onChange(SharedRealm sharedRealm) {
            // Transaction has been committed in core, but commitTransaction hasn't returned in java.
            assertFalse(commitReturns.get());
            looperThread.testComplete();
            sharedRealm.close();
        }
    });
    sharedRealm.beginTransaction();
    sharedRealm.commitTransaction();
    commitReturns.set(true);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 19 with RunTestInLooperThread

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

the class TypeBasedNotificationsTests method callback_should_trigger_for_createOrUpdateObjectFromJson_from_JSONObject.

//UC 0 Uses Realm.copyToRealmOrUpdate.
@Test
@RunTestInLooperThread
public void callback_should_trigger_for_createOrUpdateObjectFromJson_from_JSONObject() throws JSONException {
    final Realm realm = looperThread.realm;
    realm.addChangeListener(new RealmChangeListener<Realm>() {

        @Override
        public void onChange(Realm object) {
            looperThread.postRunnable(new Runnable() {

                @Override
                public void run() {
                    assertEquals(1, typebasedCommitInvocations.get());
                    looperThread.testComplete();
                }
            });
        }
    });
    AllTypesPrimaryKey obj = new AllTypesPrimaryKey();
    obj.setColumnLong(1);
    obj.setColumnString("Foo");
    realm.beginTransaction();
    realm.copyToRealm(obj);
    realm.commitTransaction();
    JSONObject json = new JSONObject();
    json.put("columnLong", 1);
    json.put("columnString", "bar");
    realm.beginTransaction();
    final AllTypesPrimaryKey newObj = realm.createOrUpdateObjectFromJson(AllTypesPrimaryKey.class, json);
    realm.commitTransaction();
    looperThread.keepStrongReference.add(newObj);
    newObj.addChangeListener(new RealmChangeListener<AllTypesPrimaryKey>() {

        @Override
        public void onChange(AllTypesPrimaryKey object) {
            assertEquals(1, realm.where(AllTypesPrimaryKey.class).count());
            assertEquals("bar", newObj.getColumnString());
            assertTrue(newObj.getColumnBoxedBoolean());
            typebasedCommitInvocations.incrementAndGet();
        }
    });
    realm.beginTransaction();
    newObj.setColumnBoxedBoolean(Boolean.TRUE);
    realm.commitTransaction();
}
Also used : JSONObject(org.json.JSONObject) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 20 with RunTestInLooperThread

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

the class TypeBasedNotificationsTests method callback_should_trigger_for_copyToRealm.

//UC 0 Uses Realm.copyToRealm.
@Test
@RunTestInLooperThread
public void callback_should_trigger_for_copyToRealm() {
    final Realm realm = looperThread.realm;
    realm.addChangeListener(new RealmChangeListener<Realm>() {

        @Override
        public void onChange(Realm object) {
            if (globalCommitInvocations.incrementAndGet() == 1) {
                looperThread.postRunnable(new Runnable() {

                    @Override
                    public void run() {
                        assertEquals(1, typebasedCommitInvocations.get());
                        looperThread.testComplete();
                    }
                });
            }
        }
    });
    realm.beginTransaction();
    Dog akamaru = new Dog();
    akamaru.setName("Akamaru");
    final Dog dog = realm.copyToRealm(akamaru);
    realm.commitTransaction();
    looperThread.keepStrongReference.add(dog);
    dog.addChangeListener(new RealmChangeListener<Dog>() {

        @Override
        public void onChange(Dog object) {
            assertEquals(8, dog.getAge());
            typebasedCommitInvocations.incrementAndGet();
        }
    });
    realm.beginTransaction();
    dog.setAge(8);
    realm.commitTransaction();
}
Also used : Dog(io.realm.entities.Dog) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) 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