Search in sources :

Example 71 with RunTestInLooperThread

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

the class TypeBasedNotificationsTests method callback_should_trigger_for_copyToRealmOrUpdate.

// UC 0 Uses Realm.copyToRealmOrUpdate.
@Test
@RunTestInLooperThread
public void callback_should_trigger_for_copyToRealmOrUpdate() {
    final Realm realm = looperThread.getRealm();
    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();
                    }
                });
            }
        }
    });
    PrimaryKeyAsLong obj = new PrimaryKeyAsLong();
    obj.setId(1);
    obj.setName("Foo");
    realm.beginTransaction();
    final PrimaryKeyAsLong primaryKeyAsLong = realm.copyToRealmOrUpdate(obj);
    realm.commitTransaction();
    looperThread.keepStrongReference(primaryKeyAsLong);
    primaryKeyAsLong.addChangeListener(new RealmChangeListener<PrimaryKeyAsLong>() {

        @Override
        public void onChange(PrimaryKeyAsLong object) {
            assertEquals(1, primaryKeyAsLong.getId());
            assertEquals("Bar", primaryKeyAsLong.getName());
            assertEquals(1, realm.where(PrimaryKeyAsLong.class).count());
            typebasedCommitInvocations.incrementAndGet();
        }
    });
    PrimaryKeyAsLong obj2 = new PrimaryKeyAsLong();
    obj2.setId(1);
    obj2.setName("Bar");
    realm.beginTransaction();
    PrimaryKeyAsLong primaryKeyAsLong2 = realm.copyToRealmOrUpdate(obj2);
    realm.commitTransaction();
    assertEquals(primaryKeyAsLong, primaryKeyAsLong2);
}
Also used : PrimaryKeyAsLong(io.realm.entities.PrimaryKeyAsLong) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 72 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.getRealm();
    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(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 73 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.getRealm();
    realm.beginTransaction();
    realm.createObject(Dog.class);
    realm.commitTransaction();
    Dog dog = realm.where(Dog.class).findFirst();
    looperThread.keepStrongReference(dog);
    dog.addChangeListener(new RealmChangeListener<Dog>() {

        @Override
        public void onChange(Dog object) {
            assertEquals(17, object.getAge());
            looperThread.testComplete();
        }
    });
    realm.executeTransactionAsync(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            realm.where(Dog.class).findFirst().setAge(17);
        }
    });
}
Also used : Dog(io.realm.entities.Dog) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 74 with RunTestInLooperThread

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

the class TypeBasedNotificationsTests method callback_should_trigger_for_createObject.

// ****************************************************************************************** //
// UC 0.
// Callback should be notified if we create a RealmObject without the async mechanism.
// ex: using (createObject, copyOrUpdate, createObjectFromJson etc.)
// ***************************************************************************************** //
// UC 0 Uses Realm.createObject.
@Test
@RunTestInLooperThread
public void callback_should_trigger_for_createObject() {
    final Realm realm = looperThread.getRealm();
    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();
    final Dog dog = realm.createObject(Dog.class);
    realm.commitTransaction();
    looperThread.keepStrongReference(dog);
    dog.addChangeListener(new RealmChangeListener<Dog>() {

        @Override
        public void onChange(Dog object) {
            assertEquals("Akamaru", dog.getName());
            typebasedCommitInvocations.incrementAndGet();
        }
    });
    realm.beginTransaction();
    dog.setName("Akamaru");
    realm.commitTransaction();
}
Also used : Dog(io.realm.entities.Dog) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 75 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.getRealm();
    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(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)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