Search in sources :

Example 21 with RunTestInLooperThread

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

the class RealmObjectTests method changeListener_triggeredWhenObjectIsdeleted.

@Test
@RunTestInLooperThread
public void changeListener_triggeredWhenObjectIsdeleted() {
    final Realm realm = looperThread.realm;
    realm.beginTransaction();
    AllTypes obj = realm.createObject(AllTypes.class);
    realm.commitTransaction();
    obj.addChangeListener(new RealmChangeListener<AllTypes>() {

        @Override
        public void onChange(AllTypes obj) {
            assertFalse(obj.isValid());
            looperThread.testComplete();
        }
    });
    realm.beginTransaction();
    obj.deleteFromRealm();
    realm.commitTransaction();
}
Also used : AllTypes(io.realm.entities.AllTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test)

Example 22 with RunTestInLooperThread

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

the class RealmObjectTests method addChangeListener_returnedObjectOfCopyToRealmOrUpdate.

// Bug https://github.com/realm/realm-java/issues/2569
@Test
@RunTestInLooperThread
public void addChangeListener_returnedObjectOfCopyToRealmOrUpdate() {
    Realm realm = looperThread.realm;
    realm.beginTransaction();
    realm.createObject(AllTypesPrimaryKey.class, 1);
    AllTypesPrimaryKey allTypesPrimaryKey = new AllTypesPrimaryKey();
    allTypesPrimaryKey.setColumnLong(1);
    allTypesPrimaryKey.setColumnFloat(0f);
    allTypesPrimaryKey = realm.copyToRealmOrUpdate(allTypesPrimaryKey);
    realm.commitTransaction();
    looperThread.keepStrongReference.add(allTypesPrimaryKey);
    allTypesPrimaryKey.addChangeListener(new RealmChangeListener<AllTypesPrimaryKey>() {

        @Override
        public void onChange(AllTypesPrimaryKey element) {
            assertEquals(42.0f, element.getColumnFloat(), 0f);
            looperThread.testComplete();
        }
    });
    // Change the object to trigger the listener.
    realm.beginTransaction();
    allTypesPrimaryKey.setColumnFloat(42f);
    realm.commitTransaction();
}
Also used : AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test)

Example 23 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.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();
    final Dog dog = realm.createObject(Dog.class);
    realm.commitTransaction();
    looperThread.keepStrongReference.add(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 24 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.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).findAll();
    looperThread.keepStrongReference.add(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 25 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 COMPLETED_ASYNC_REALM_OBJECT then commits transaction in another non-looper thread.
// 3. Listener on the RealmObject gets triggered again.
@Test
@RunTestInLooperThread
public void non_looper_thread_commit_realmobject_async() {
    final Realm realm = looperThread.realm;
    realm.addChangeListener(new RealmChangeListener<Realm>() {

        @Override
        public void onChange(Realm object) {
            // Checks if the 2nd transaction is committed.
            if (realm.where(Dog.class).count() == 2) {
                looperThread.postRunnable(new Runnable() {

                    @Override
                    public void run() {
                        assertEquals(2, typebasedCommitInvocations.get());
                        looperThread.testComplete();
                    }
                });
            }
        }
    });
    realm.beginTransaction();
    realm.createObject(Dog.class);
    realm.commitTransaction();
    final Thread thread = new Thread() {

        @Override
        public void run() {
            if (typebasedCommitInvocations.get() != 1) {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    fail(e.getMessage());
                }
            }
            Realm bgRealm = Realm.getInstance(realm.getConfiguration());
            bgRealm.beginTransaction();
            bgRealm.createObject(Dog.class);
            bgRealm.commitTransaction();
            bgRealm.close();
        }
    };
    Dog dog = realm.where(Dog.class).findFirstAsync();
    looperThread.keepStrongReference.add(dog);
    dog.addChangeListener(new RealmChangeListener<Dog>() {

        @Override
        public void onChange(Dog object) {
            typebasedCommitInvocations.incrementAndGet();
            if (typebasedCommitInvocations.get() == 1) {
                try {
                    thread.join();
                } catch (InterruptedException e) {
                    fail(e.getMessage());
                }
            }
        }
    });
    thread.start();
}
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)

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