Search in sources :

Example 6 with RunTestInLooperThread

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

the class RealmQueryTests method distinctAsync_indexedLinkedFields.

@Test
@RunTestInLooperThread
public void distinctAsync_indexedLinkedFields() {
    final long numberOfBlocks = 25;
    final long numberOfObjects = 10;
    populateForDistinct(realm, numberOfBlocks, numberOfObjects, false);
    for (String field : AnnotationIndexTypes.INDEX_FIELDS) {
        try {
            realm.where(AnnotationIndexTypes.class).distinctAsync(AnnotationIndexTypes.FIELD_OBJECT + "." + field);
            fail("Unsupported " + field + " linked field");
        } catch (IllegalArgumentException ignored) {
        }
    }
    looperThread.testComplete();
}
Also used : PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) AnnotationIndexTypes(io.realm.entities.AnnotationIndexTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 7 with RunTestInLooperThread

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

the class RealmResultsTests method removeAllChangeListeners.

@Test
@RunTestInLooperThread
public void removeAllChangeListeners() {
    final AtomicInteger listenersTriggered = new AtomicInteger(0);
    final Realm realm = looperThread.realm;
    RealmResults<AllTypes> collection = realm.where(AllTypes.class).findAll();
    RealmChangeListener<RealmResults<AllTypes>> listenerA = new RealmChangeListener<RealmResults<AllTypes>>() {

        @Override
        public void onChange(RealmResults<AllTypes> object) {
            listenersTriggered.incrementAndGet();
        }
    };
    RealmChangeListener<RealmResults<AllTypes>> listenerB = new RealmChangeListener<RealmResults<AllTypes>>() {

        @Override
        public void onChange(RealmResults<AllTypes> object) {
            listenersTriggered.incrementAndGet();
        }
    };
    looperThread.keepStrongReference.add(collection);
    collection.addChangeListener(listenerA);
    collection.addChangeListener(listenerB);
    collection.removeAllChangeListeners();
    realm.beginTransaction();
    realm.createObject(AllTypes.class);
    realm.commitTransaction();
    // The above commit should have put a REALM_CHANGED event on the Looper queue before this runnable.
    looperThread.postRunnable(new Runnable() {

        @Override
        public void run() {
            if (listenersTriggered.get() == 0) {
                looperThread.testComplete();
            } else {
                fail("Listeners wasn't removed");
            }
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AllTypes(io.realm.entities.AllTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test)

Example 8 with RunTestInLooperThread

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

the class RealmResultsTests method addChangeListener_twice.

@Test
@RunTestInLooperThread
public void addChangeListener_twice() {
    final AtomicInteger listenersTriggered = new AtomicInteger(0);
    final Realm realm = looperThread.realm;
    RealmResults<AllTypes> collection = realm.where(AllTypes.class).findAll();
    RealmChangeListener<RealmResults<AllTypes>> listener = new RealmChangeListener<RealmResults<AllTypes>>() {

        @Override
        public void onChange(RealmResults<AllTypes> object) {
            listenersTriggered.incrementAndGet();
        }
    };
    realm.addChangeListener(new RealmChangeListener<Realm>() {

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

                @Override
                public void run() {
                    if (listenersTriggered.get() == 1) {
                        looperThread.testComplete();
                    } else {
                        fail("Only global listener should be triggered");
                    }
                }
            });
        }
    });
    // Adding it twice will be ignored, so removing it will not cause the listener to be triggered.
    looperThread.keepStrongReference.add(collection);
    collection.addChangeListener(listener);
    collection.addChangeListener(listener);
    collection.removeChangeListener(listener);
    realm.beginTransaction();
    realm.createObject(AllTypes.class);
    realm.commitTransaction();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AllTypes(io.realm.entities.AllTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test)

Example 9 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 10 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)

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