Search in sources :

Example 66 with RunTestInLooperThread

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

the class TypeBasedNotificationsTests method callback_with_relevant_commit_realmresults_sync.

// UC 1 Sync RealmResults.
@Test
@RunTestInLooperThread
public void callback_with_relevant_commit_realmresults_sync() {
    final Realm realm = looperThread.getRealm();
    // 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).findAll();
    looperThread.keepStrongReference(dogs);
    dogs.addChangeListener(new RealmChangeListener<RealmResults<Dog>>() {

        @Override
        public void onChange(RealmResults<Dog> object) {
            // Step 4: Responds to relevant change.
            typebasedCommitInvocations.incrementAndGet();
            assertEquals(1, dogs.size());
            assertEquals("Akamaru", dogs.get(0).getName());
            assertEquals(17, dogs.get(0).getAge());
            looperThread.testComplete();
        }
    });
    // 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);
        }
    });
}
Also used : Dog(io.realm.entities.Dog) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 67 with RunTestInLooperThread

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

the class TypeBasedNotificationsTests method non_looper_thread_commit_realmobject_async.

// UC 3 Async RealmObject.
// 1. Creates RealmObject async query.
// 2. Waits async returns then change the object.
// 3. Listener on the RealmObject gets triggered again.
@Test
@RunTestInLooperThread
public void non_looper_thread_commit_realmobject_async() {
    final Realm realm = looperThread.getRealm();
    realm.beginTransaction();
    realm.createObject(Dog.class).setAge(1);
    realm.commitTransaction();
    Dog dog = realm.where(Dog.class).findFirstAsync();
    looperThread.keepStrongReference(dog);
    dog.addChangeListener(new RealmChangeListener<Dog>() {

        @Override
        public void onChange(Dog object) {
            switch(typebasedCommitInvocations.incrementAndGet()) {
                case 1:
                    assertEquals(1, object.getAge());
                    realm.executeTransactionAsync(new Realm.Transaction() {

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

Example 68 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.getRealm();
    realm.beginTransaction();
    Dog akamaru = realm.createObject(Dog.class);
    realm.commitTransaction();
    RealmResults<Dog> dogs = realm.where(Dog.class).findAll();
    looperThread.keepStrongReference(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 69 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.getRealm();
    // 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(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 70 with RunTestInLooperThread

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

the class TypeBasedNotificationsTests method multiple_callbacks_should_be_invoked_realmobject_sync.

// ********************************************************************************* //
// UC 2.
// Multiple callbacks should be invoked after a relevant commit.
// ********************************************************************************* //
// UC 2 for Sync RealmObject.
@Test
@RunTestInLooperThread
public void multiple_callbacks_should_be_invoked_realmobject_sync() {
    final int NUMBER_OF_LISTENERS = 7;
    final Realm realm = looperThread.getRealm();
    realm.addChangeListener(new RealmChangeListener<Realm>() {

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

                @Override
                public void run() {
                    assertEquals(NUMBER_OF_LISTENERS, typebasedCommitInvocations.get());
                    looperThread.testComplete();
                }
            });
        }
    });
    realm.beginTransaction();
    Dog akamaru = realm.createObject(Dog.class);
    realm.commitTransaction();
    Dog dog = realm.where(Dog.class).findFirst();
    looperThread.keepStrongReference(dog);
    for (int i = 0; i < NUMBER_OF_LISTENERS; i++) {
        dog.addChangeListener(new RealmChangeListener<Dog>() {

            @Override
            public void onChange(Dog object) {
                typebasedCommitInvocations.incrementAndGet();
            }
        });
    }
    realm.beginTransaction();
    akamaru.setAge(17);
    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