Search in sources :

Example 26 with RunTestInLooperThread

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

the class TypeBasedNotificationsTests method non_looper_thread_commit_realmobject_sync.

// ********************************************************************************* //
// UC 3.
// Callback should be invoked when a non Looper thread commits.
// ********************************************************************************* //
// UC 3 for Sync RealmObject.
// 1. Adds listener to RealmObject which is queried synchronized.
// 2. Commits transaction in another non-looper thread.
// 3. Listener on the RealmObject gets triggered.
@Test
@RunTestInLooperThread
public void non_looper_thread_commit_realmobject_sync() {
    final Realm realm = looperThread.realm;
    realm.addChangeListener(new RealmChangeListener<Realm>() {

        @Override
        public void onChange(Realm object) {
            if (realm.where(Dog.class).count() == 2) {
                looperThread.postRunnable(new Runnable() {

                    @Override
                    public void run() {
                        assertEquals(1, typebasedCommitInvocations.get());
                        looperThread.testComplete();
                    }
                });
            }
        }
    });
    realm.beginTransaction();
    realm.createObject(Dog.class);
    realm.commitTransaction();
    Dog dog = realm.where(Dog.class).findFirst();
    looperThread.keepStrongReference.add(dog);
    dog.addChangeListener(new RealmChangeListener<Dog>() {

        @Override
        public void onChange(Dog object) {
            typebasedCommitInvocations.incrementAndGet();
        }
    });
    Thread thread = new Thread() {

        @Override
        public void run() {
            Realm bgRealm = Realm.getInstance(realm.getConfiguration());
            bgRealm.beginTransaction();
            bgRealm.createObject(Dog.class);
            bgRealm.commitTransaction();
            bgRealm.close();
        }
    };
    thread.start();
    try {
        thread.join();
    } catch (InterruptedException e) {
        fail(e.getMessage());
    }
}
Also used : Dog(io.realm.entities.Dog) RunInLooperThread(io.realm.rule.RunInLooperThread) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 27 with RunTestInLooperThread

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

the class TypeBasedNotificationsTests method callback_with_relevant_commit_realmresults_async.

// UC 1 Async RealmResults.
@Test
@RunTestInLooperThread
public void callback_with_relevant_commit_realmresults_async() {
    final Realm realm = looperThread.realm;
    // Step 1: Creates object.
    realm.beginTransaction();
    final Dog akamaru = realm.createObject(Dog.class);
    akamaru.setName("Akamaru");
    realm.commitTransaction();
    final RealmResults<Dog> dogs = realm.where(Dog.class).findAllAsync();
    looperThread.keepStrongReference.add(dogs);
    dogs.addChangeListener(new RealmChangeListener<RealmResults<Dog>>() {

        @Override
        public void onChange(RealmResults<Dog> object) {
            // Step 4: Responds to relevant change.
            int commits = typebasedCommitInvocations.incrementAndGet();
            switch(commits) {
                case 1:
                    // Async query returns.
                    assertEquals(1, dogs.size());
                    assertEquals("Akamaru", dogs.get(0).getName());
                    // Step 2: Trigger non-related commit. If this triggered the results listener,
                    // assertion will happen there.
                    realm.executeTransactionAsync(new Realm.Transaction() {

                        @Override
                        public void execute(Realm realm) {
                        }
                    });
                    // Step 3: Triggers related commit.
                    realm.executeTransactionAsync(new Realm.Transaction() {

                        @Override
                        public void execute(Realm realm) {
                            realm.where(Dog.class).findFirst().setAge(17);
                        }
                    });
                    break;
                case 2:
                    assertEquals(17, dogs.get(0).getAge());
                    looperThread.testComplete();
                    break;
                default:
                    fail();
            }
        }
    });
}
Also used : Dog(io.realm.entities.Dog) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 28 with RunTestInLooperThread

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

the class TypeBasedNotificationsTests method multiple_callbacks_should_be_invoked_realmresults_sync.

// UC 2 Sync RealmResults.
@Test
@RunTestInLooperThread
public void multiple_callbacks_should_be_invoked_realmresults_sync() {
    final int NUMBER_OF_LISTENERS = 7;
    final Realm realm = looperThread.realm;
    realm.beginTransaction();
    Dog akamaru = realm.createObject(Dog.class);
    realm.commitTransaction();
    RealmResults<Dog> dogs = realm.where(Dog.class).findAll();
    looperThread.keepStrongReference.add(dogs);
    for (int i = 0; i < NUMBER_OF_LISTENERS; i++) {
        dogs.addChangeListener(new RealmChangeListener<RealmResults<Dog>>() {

            @Override
            public void onChange(RealmResults<Dog> results) {
                assertEquals(17, results.first().getAge());
                if (typebasedCommitInvocations.incrementAndGet() == NUMBER_OF_LISTENERS) {
                    looperThread.testComplete();
                }
                assertTrue(typebasedCommitInvocations.get() <= NUMBER_OF_LISTENERS);
            }
        });
    }
    realm.beginTransaction();
    akamaru.setAge(17);
    realm.commitTransaction();
}
Also used : Dog(io.realm.entities.Dog) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 29 with RunTestInLooperThread

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

the class TypeBasedNotificationsTests method callback_should_trigger_for_createOrUpdateObjectFromJson.

//UC 0 Uses Realm.createOrUpdateObjectFromJson.
@Test
@RunTestInLooperThread
public void callback_should_trigger_for_createOrUpdateObjectFromJson() {
    assumeThat(Build.VERSION.SDK_INT, greaterThanOrEqualTo(Build.VERSION_CODES.HONEYCOMB));
    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();
                    }
                });
            }
        }
    });
    try {
        AllTypesPrimaryKey obj = new AllTypesPrimaryKey();
        Date date = new Date(0);
        // ID
        obj.setColumnLong(1);
        obj.setColumnBinary(new byte[] { 1 });
        obj.setColumnBoolean(true);
        obj.setColumnDate(date);
        obj.setColumnDouble(1);
        obj.setColumnFloat(1);
        obj.setColumnString("1");
        realm.beginTransaction();
        realm.copyToRealm(obj);
        realm.commitTransaction();
        InputStream in = TestHelper.loadJsonFromAssets(context, "all_types_primary_key_field_only.json");
        realm.beginTransaction();
        final AllTypesPrimaryKey objectFromJson = realm.createOrUpdateObjectFromJson(AllTypesPrimaryKey.class, in);
        realm.commitTransaction();
        in.close();
        looperThread.keepStrongReference.add(objectFromJson);
        objectFromJson.addChangeListener(new RealmChangeListener<AllTypesPrimaryKey>() {

            @Override
            public void onChange(AllTypesPrimaryKey object) {
                assertEquals("ObjectFromJson", objectFromJson.getColumnString());
                assertEquals(1L, objectFromJson.getColumnLong());
                assertEquals(1F, objectFromJson.getColumnFloat(), 0F);
                assertEquals(1D, objectFromJson.getColumnDouble(), 0D);
                assertEquals(true, objectFromJson.isColumnBoolean());
                assertArrayEquals(new byte[] { 1 }, objectFromJson.getColumnBinary());
                assertNull(objectFromJson.getColumnRealmObject());
                assertEquals(0, objectFromJson.getColumnRealmList().size());
                typebasedCommitInvocations.incrementAndGet();
            }
        });
        realm.beginTransaction();
        objectFromJson.setColumnString("ObjectFromJson");
        realm.commitTransaction();
    } catch (IOException e) {
        fail(e.getMessage());
    }
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) Date(java.util.Date) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 30 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)

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