Search in sources :

Example 76 with RunTestInLooperThread

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

the class TypeBasedNotificationsTests method callback_should_trigger_for_createOrUpdateObjectFromJson_from_JSONObject.

// UC 0 Uses Realm.copyToRealmOrUpdate.
@Test
@RunTestInLooperThread
public void callback_should_trigger_for_createOrUpdateObjectFromJson_from_JSONObject() throws JSONException {
    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(1, typebasedCommitInvocations.get());
                    looperThread.testComplete();
                }
            });
        }
    });
    AllTypesPrimaryKey obj = new AllTypesPrimaryKey();
    obj.setColumnLong(1);
    obj.setColumnString("Foo");
    realm.beginTransaction();
    realm.copyToRealm(obj);
    realm.commitTransaction();
    JSONObject json = new JSONObject();
    json.put("columnLong", 1);
    json.put("columnString", "bar");
    realm.beginTransaction();
    final AllTypesPrimaryKey newObj = realm.createOrUpdateObjectFromJson(AllTypesPrimaryKey.class, json);
    realm.commitTransaction();
    looperThread.keepStrongReference(newObj);
    newObj.addChangeListener(new RealmChangeListener<AllTypesPrimaryKey>() {

        @Override
        public void onChange(AllTypesPrimaryKey object) {
            assertEquals(1, realm.where(AllTypesPrimaryKey.class).count());
            assertEquals("bar", newObj.getColumnString());
            assertTrue(newObj.getColumnBoxedBoolean());
            typebasedCommitInvocations.incrementAndGet();
        }
    });
    realm.beginTransaction();
    newObj.setColumnBoxedBoolean(Boolean.TRUE);
    realm.commitTransaction();
}
Also used : JSONObject(org.json.JSONObject) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 77 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.getRealm();
    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(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) DictionaryAllTypes(io.realm.entities.DictionaryAllTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 78 with RunTestInLooperThread

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

the class RealmQueryTests method distinct_async_withNullValues.

@Test
@RunTestInLooperThread
public void distinct_async_withNullValues() throws Throwable {
    final AtomicInteger changeListenerCalled = new AtomicInteger(2);
    final Realm realm = looperThread.getRealm();
    final long numberOfBlocks = 3;
    // must be greater than 1
    final long numberOfObjects = 3;
    populateForDistinct(realm, numberOfBlocks, numberOfObjects, true);
    final RealmResults<AnnotationIndexTypes> distinctDate = realm.where(AnnotationIndexTypes.class).distinct(AnnotationIndexTypes.FIELD_INDEX_DATE).findAllAsync();
    final RealmResults<AnnotationIndexTypes> distinctString = realm.where(AnnotationIndexTypes.class).distinct(AnnotationIndexTypes.FIELD_INDEX_STRING).findAllAsync();
    final Runnable endTest = new Runnable() {

        @Override
        public void run() {
            if (changeListenerCalled.decrementAndGet() == 0) {
                looperThread.testComplete();
            }
        }
    };
    looperThread.keepStrongReference(distinctDate);
    looperThread.keepStrongReference(distinctString);
    distinctDate.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            assertEquals(1, distinctDate.size());
            endTest.run();
        }
    });
    distinctString.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            assertEquals(1, distinctString.size());
            endTest.run();
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AnnotationIndexTypes(io.realm.entities.AnnotationIndexTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 79 with RunTestInLooperThread

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

the class RealmQueryTests method distinct_async_invalidTypes.

// Smoke test of async distinct invalid types. Underlying mechanism is the same as for sync test
// (distinct_allFields), so just verifying async mechanism.
@Test
@RunTestInLooperThread
public void distinct_async_invalidTypes() {
    populateTestRealm(realm, TEST_DATA_SIZE);
    RealmObjectSchema schema = realm.getSchema().getSchemaForClass(AllTypes.CLASS_NAME);
    Set<String> fieldNames = schema.getFieldNames();
    for (String fieldName : fieldNames) {
        String field = fieldName;
        RealmFieldType type = schema.getFieldType(fieldName);
        if (!supportDistinct(type)) {
            try {
                realm.where(AllTypes.class).distinct(field).findAllAsync();
            } catch (IllegalArgumentException ignored) {
            }
        }
    }
    looperThread.testComplete();
}
Also used : PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 80 with RunTestInLooperThread

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

the class RealmListTests method removeChangeListener.

@Test
@RunTestInLooperThread
public void removeChangeListener() {
    collection = prepareRealmListInLooperThread();
    Realm realm = looperThread.getRealm();
    final AtomicInteger listenerCalledCount = new AtomicInteger(0);
    RealmChangeListener<RealmList<Dog>> listener1 = new RealmChangeListener<RealmList<Dog>>() {

        @Override
        public void onChange(RealmList<Dog> element) {
            fail();
        }
    };
    OrderedRealmCollectionChangeListener<RealmList<Dog>> listener2 = new OrderedRealmCollectionChangeListener<RealmList<Dog>>() {

        @Override
        public void onChange(RealmList<Dog> collection, OrderedCollectionChangeSet changes) {
            assertEquals(0, listenerCalledCount.getAndIncrement());
            looperThread.testComplete();
        }
    };
    collection.addChangeListener(listener1);
    collection.addChangeListener(listener2);
    collection.removeChangeListener(listener1);
    // This should trigger the listener if there is any.
    realm.beginTransaction();
    collection.get(0).setAge(42);
    realm.commitTransaction();
    assertEquals(1, listenerCalledCount.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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