Search in sources :

Example 46 with RunTestInLooperThread

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

the class ObjectChangeSetTests method changeLinkFieldOriginalObjectChanged_notTrigger.

@Test
@RunTestInLooperThread(before = PopulateOneAllTypes.class)
public void changeLinkFieldOriginalObjectChanged_notTrigger() {
    Realm realm = looperThread.getRealm();
    AllTypes allTypes = realm.where(AllTypes.class).findFirst();
    listenerShouldNotBeCalled(allTypes);
    realm.beginTransaction();
    allTypes.getColumnRealmObject().setAge(42);
    realm.commitTransaction();
}
Also used : AllTypes(io.realm.entities.AllTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 47 with RunTestInLooperThread

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

the class NotificationsTest method globalListener_looperThread_triggeredByRemoteCommit.

@Test
@RunTestInLooperThread
public void globalListener_looperThread_triggeredByRemoteCommit() {
    final AtomicInteger success = new AtomicInteger(0);
    Realm realm = looperThread.getRealm();
    realm.addChangeListener(new RealmChangeListener<Realm>() {

        @Override
        public void onChange(Realm object) {
            assertEquals(1, success.get());
            looperThread.testComplete();
        }
    });
    realm.executeTransactionAsync(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            realm.createObject(AllTypes.class);
        }
    });
    assertEquals(0, success.getAndIncrement());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AllTypes(io.realm.entities.AllTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 48 with RunTestInLooperThread

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

the class NotificationsTest method globalListener_looperThread_triggeredByLocalCommit.

@Test
@RunTestInLooperThread
public void globalListener_looperThread_triggeredByLocalCommit() {
    final AtomicInteger success = new AtomicInteger(0);
    Realm realm = looperThread.getRealm();
    realm.addChangeListener(new RealmChangeListener<Realm>() {

        @Override
        public void onChange(Realm object) {
            assertEquals(0, success.getAndIncrement());
            looperThread.testComplete();
        }
    });
    realm.beginTransaction();
    realm.createObject(AllTypes.class);
    realm.commitTransaction();
    assertEquals(1, success.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 49 with RunTestInLooperThread

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

the class NotificationsTest method asyncRealmResultsShouldNotBlockBackgroundCommitNotification.

// The presence of async RealmResults block any `REALM_CHANGE` notification causing historically the Realm
// to advance to the latest version. We make sure in this test that all Realm listeners will be notified
// regardless of the presence of an async RealmResults that will delay the `REALM_CHANGE` sometimes.
@Test
@RunTestInLooperThread
public void asyncRealmResultsShouldNotBlockBackgroundCommitNotification() {
    final Realm realm = looperThread.getRealm();
    final RealmResults<Dog> dogs = realm.where(Dog.class).findAllAsync();
    final AtomicBoolean resultsListenerDone = new AtomicBoolean(false);
    final AtomicBoolean realmListenerDone = new AtomicBoolean(false);
    looperThread.keepStrongReference(dogs);
    assertTrue(dogs.load());
    assertEquals(0, dogs.size());
    dogs.addChangeListener(new RealmChangeListener<RealmResults<Dog>>() {

        @Override
        public void onChange(RealmResults<Dog> results) {
            if (dogs.size() == 2) {
                // Results has the latest changes.
                resultsListenerDone.set(true);
                if (realmListenerDone.get()) {
                    looperThread.testComplete();
                }
            }
        }
    });
    realm.addChangeListener(new RealmChangeListener<Realm>() {

        @Override
        public void onChange(Realm element) {
            if (dogs.size() == 1) {
                // Step 2. Creates the second dog.
                realm.executeTransactionAsync(new Realm.Transaction() {

                    @Override
                    public void execute(Realm realm) {
                        realm.createObject(Dog.class);
                    }
                });
            } else if (dogs.size() == 2) {
                // Realm listener can see the latest changes.
                realmListenerDone.set(true);
                if (resultsListenerDone.get()) {
                    looperThread.testComplete();
                }
            }
        }
    });
    // Step 1. Creates the first dog.
    realm.executeTransactionAsync(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            realm.createObject(Dog.class);
        }
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Dog(io.realm.entities.Dog) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 50 with RunTestInLooperThread

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

the class OrderedCollectionChangeSetTests method addChangeListener_bug5507.

// To reproduce https://github.com/realm/realm-java/issues/5507
// 1. Add listener to a collection
// A. change the collection in a background thread
// 2. Remove the listener
// 3. Add another listener
// 4. the listener added in step 3 should be triggered with change set in step A
@Test
@RunTestInLooperThread
public void addChangeListener_bug5507() throws InterruptedException {
    // FIXME: See https://github.com/realm/realm-object-store/issues/605
    if (type == ObservablesType.REALM_RESULTS) {
        looperThread.testComplete();
        return;
    }
    Realm realm = looperThread.getRealm();
    populateData(realm, 1);
    OrderedRealmCollectionChangeListener<OrderedRealmCollection<Dog>> listener1 = new OrderedRealmCollectionChangeListener<OrderedRealmCollection<Dog>>() {

        @Override
        public void onChange(OrderedRealmCollection<Dog> dogs, @Nullable OrderedCollectionChangeSet changeSet) {
            fail();
        }
    };
    OrderedRealmCollection<Dog> dogs = getTestingCollection(realm);
    assertEquals(1, dogs.size());
    if (type == ObservablesType.REALM_LIST) {
        // noinspection unchecked
        ((RealmList) dogs).addChangeListener(listener1);
    } else {
        // noinspection unchecked
        ((RealmResults) dogs).addChangeListener(listener1);
    }
    Thread bgThread = new Thread(new Runnable() {

        @Override
        public void run() {
            Realm realm = Realm.getInstance(looperThread.getConfiguration());
            realm.beginTransaction();
            createObjects(realm, 2);
            realm.commitTransaction();
            realm.close();
        }
    });
    bgThread.start();
    bgThread.join();
    if (type == ObservablesType.REALM_LIST) {
        // noinspection unchecked
        ((RealmList) dogs).removeChangeListener(listener1);
    } else {
        // noinspection unchecked
        ((RealmResults) dogs).removeChangeListener(listener1);
    }
    OrderedRealmCollectionChangeListener<OrderedRealmCollection<Dog>> listener2 = new OrderedRealmCollectionChangeListener<OrderedRealmCollection<Dog>>() {

        @Override
        public void onChange(OrderedRealmCollection<Dog> dogs, @Nullable OrderedCollectionChangeSet changeSet) {
            assertEquals(2, dogs.size());
            looperThread.testComplete();
        }
    };
    if (type == ObservablesType.REALM_LIST) {
        // noinspection unchecked
        ((RealmList) dogs).addChangeListener(listener2);
    } else {
        // noinspection unchecked
        ((RealmResults) dogs).addChangeListener(listener2);
    }
}
Also used : RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) RunInLooperThread(io.realm.rule.RunInLooperThread) Dog(io.realm.entities.Dog) Nullable(javax.annotation.Nullable) 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