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();
}
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");
}
}
});
}
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();
}
});
}
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();
}
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());
}
Aggregations