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();
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class DynamicRealmTests method findAllSortedAsync_usingMultipleFields.
@Test
@RunTestInLooperThread
public void findAllSortedAsync_usingMultipleFields() {
final DynamicRealm dynamicRealm = initializeDynamicRealm();
dynamicRealm.setAutoRefresh(false);
dynamicRealm.beginTransaction();
dynamicRealm.delete(AllTypes.CLASS_NAME);
for (int i = 0; i < 5; ) {
DynamicRealmObject allTypes = dynamicRealm.createObject(AllTypes.CLASS_NAME);
allTypes.set(AllTypes.FIELD_LONG, i);
allTypes.set(AllTypes.FIELD_STRING, "data " + i % 3);
allTypes = dynamicRealm.createObject(AllTypes.CLASS_NAME);
allTypes.set(AllTypes.FIELD_LONG, i);
allTypes.set(AllTypes.FIELD_STRING, "data " + (++i % 3));
}
dynamicRealm.commitTransaction();
dynamicRealm.setAutoRefresh(true);
// Sorts first set by using: String[ASC], Long[DESC].
final RealmResults<DynamicRealmObject> realmResults1 = dynamicRealm.where(AllTypes.CLASS_NAME).findAllSortedAsync(new String[] { AllTypes.FIELD_STRING, AllTypes.FIELD_LONG }, new Sort[] { Sort.ASCENDING, Sort.DESCENDING });
// Sorts second set by using: String[DESC], Long[ASC].
final RealmResults<DynamicRealmObject> realmResults2 = dynamicRealm.where(AllTypes.CLASS_NAME).between(AllTypes.FIELD_LONG, 0, 5).findAllSortedAsync(new String[] { AllTypes.FIELD_STRING, AllTypes.FIELD_LONG }, new Sort[] { Sort.DESCENDING, Sort.ASCENDING });
final Runnable signalCallbackDone = new Runnable() {
final AtomicInteger callbacksDone = new AtomicInteger(2);
@Override
public void run() {
if (callbacksDone.decrementAndGet() == 0) {
dynamicRealm.close();
looperThread.testComplete();
}
}
};
realmResults1.addChangeListener(new RealmChangeListener<RealmResults<DynamicRealmObject>>() {
@Override
public void onChange(RealmResults<DynamicRealmObject> object) {
assertEquals("data 0", realmResults1.get(0).get(AllTypes.FIELD_STRING));
assertEquals(3L, realmResults1.get(0).get(AllTypes.FIELD_LONG));
assertEquals("data 0", realmResults1.get(1).get(AllTypes.FIELD_STRING));
assertEquals(2L, realmResults1.get(1).get(AllTypes.FIELD_LONG));
assertEquals("data 0", realmResults1.get(2).get(AllTypes.FIELD_STRING));
assertEquals(0L, realmResults1.get(2).get(AllTypes.FIELD_LONG));
assertEquals("data 1", realmResults1.get(3).get(AllTypes.FIELD_STRING));
assertEquals(4L, realmResults1.get(3).get(AllTypes.FIELD_LONG));
assertEquals("data 1", realmResults1.get(4).get(AllTypes.FIELD_STRING));
assertEquals(3L, realmResults1.get(4).get(AllTypes.FIELD_LONG));
assertEquals("data 1", realmResults1.get(5).get(AllTypes.FIELD_STRING));
assertEquals(1L, realmResults1.get(5).get(AllTypes.FIELD_LONG));
assertEquals("data 1", realmResults1.get(6).get(AllTypes.FIELD_STRING));
assertEquals(0L, realmResults1.get(6).get(AllTypes.FIELD_LONG));
assertEquals("data 2", realmResults1.get(7).get(AllTypes.FIELD_STRING));
assertEquals(4L, realmResults1.get(7).get(AllTypes.FIELD_LONG));
assertEquals("data 2", realmResults1.get(8).get(AllTypes.FIELD_STRING));
assertEquals(2L, realmResults1.get(8).get(AllTypes.FIELD_LONG));
assertEquals("data 2", realmResults1.get(9).get(AllTypes.FIELD_STRING));
assertEquals(1L, realmResults1.get(9).get(AllTypes.FIELD_LONG));
signalCallbackDone.run();
}
});
realmResults2.addChangeListener(new RealmChangeListener<RealmResults<DynamicRealmObject>>() {
@Override
public void onChange(RealmResults<DynamicRealmObject> object) {
assertEquals("data 2", realmResults2.get(0).get(AllTypes.FIELD_STRING));
assertEquals(1L, realmResults2.get(0).get(AllTypes.FIELD_LONG));
assertEquals("data 2", realmResults2.get(1).get(AllTypes.FIELD_STRING));
assertEquals(2L, realmResults2.get(1).get(AllTypes.FIELD_LONG));
assertEquals("data 2", realmResults2.get(2).get(AllTypes.FIELD_STRING));
assertEquals(4L, realmResults2.get(2).get(AllTypes.FIELD_LONG));
assertEquals("data 1", realmResults2.get(3).get(AllTypes.FIELD_STRING));
assertEquals(0L, realmResults2.get(3).get(AllTypes.FIELD_LONG));
assertEquals("data 1", realmResults2.get(4).get(AllTypes.FIELD_STRING));
assertEquals(1L, realmResults2.get(4).get(AllTypes.FIELD_LONG));
assertEquals("data 1", realmResults2.get(5).get(AllTypes.FIELD_STRING));
assertEquals(3L, realmResults2.get(5).get(AllTypes.FIELD_LONG));
assertEquals("data 1", realmResults2.get(6).get(AllTypes.FIELD_STRING));
assertEquals(4L, realmResults2.get(6).get(AllTypes.FIELD_LONG));
assertEquals("data 0", realmResults2.get(7).get(AllTypes.FIELD_STRING));
assertEquals(0L, realmResults2.get(7).get(AllTypes.FIELD_LONG));
assertEquals("data 0", realmResults2.get(8).get(AllTypes.FIELD_STRING));
assertEquals(2L, realmResults2.get(8).get(AllTypes.FIELD_LONG));
assertEquals("data 0", realmResults2.get(9).get(AllTypes.FIELD_STRING));
assertEquals(3L, realmResults2.get(9).get(AllTypes.FIELD_LONG));
signalCallbackDone.run();
}
});
looperThread.keepStrongReference.add(realmResults1);
looperThread.keepStrongReference.add(realmResults2);
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class LinkingObjectsManagedTests method json_jsonUpdateCausesNotification.
// A JSON update should generate a notifcation
@Test
@RunTestInLooperThread
public void json_jsonUpdateCausesNotification() {
final Realm looperThreadRealm = looperThread.realm;
looperThreadRealm.beginTransaction();
AllJavaTypes child = looperThreadRealm.createObject(AllJavaTypes.class, 1);
AllJavaTypes parent = looperThreadRealm.createObject(AllJavaTypes.class, 2);
parent.setFieldObject(child);
looperThreadRealm.commitTransaction();
RealmResults<AllJavaTypes> results = looperThreadRealm.where(AllJavaTypes.class).equalTo("fieldId", 1).findAll();
assertNotNull(results);
assertEquals(results.size(), 1);
child = results.first();
RealmResults<AllJavaTypes> parents = child.getObjectParents();
assertNotNull(parents);
assertEquals(1, parents.size());
final AtomicInteger counter = new AtomicInteger(0);
RealmChangeListener<AllJavaTypes> listener = new RealmChangeListener<AllJavaTypes>() {
@Override
public void onChange(AllJavaTypes object) {
counter.incrementAndGet();
}
};
child.addChangeListener(listener);
looperThreadRealm.beginTransaction();
try {
looperThreadRealm.createOrUpdateAllFromJson(AllJavaTypes.class, "[{ \"fieldId\" : 2, \"fieldObject\" : null }]");
} catch (RealmException e) {
fail("Failed loading JSON" + e);
}
looperThreadRealm.commitTransaction();
verifyPostConditions(looperThreadRealm, new PostConditions() {
public void run(Realm realm) {
RealmResults<AllJavaTypes> results = looperThreadRealm.where(AllJavaTypes.class).equalTo("fieldId", 1).findAll();
assertNotNull(results);
assertEquals(results.size(), 1);
AllJavaTypes child = results.first();
RealmResults<AllJavaTypes> parents = child.getObjectParents();
assertNotNull(parents);
assertEquals(0, parents.size());
assertEquals(1, counter.get());
}
}, child, parent);
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class LinkingObjectsManagedTests method notification_notSentOnUnrelatedChangeModelObject.
// A listener registered on the backlinked object is called
// for an unrelated change on the an object of the same type!!
// This test exists only to document existing (but odd) behavior.
@Test
@RunTestInLooperThread
public void notification_notSentOnUnrelatedChangeModelObject() {
final Realm looperThreadRealm = looperThread.realm;
looperThreadRealm.beginTransaction();
AllJavaTypes child = looperThreadRealm.createObject(AllJavaTypes.class, 10);
AllJavaTypes parent = looperThreadRealm.createObject(AllJavaTypes.class, 1);
looperThreadRealm.commitTransaction();
final AtomicInteger counter = new AtomicInteger(0);
RealmChangeListener<AllJavaTypes> listener = new RealmChangeListener<AllJavaTypes>() {
@Override
public void onChange(AllJavaTypes object) {
counter.incrementAndGet();
}
};
child.addChangeListener(listener);
looperThreadRealm.beginTransaction();
looperThreadRealm.where(AllJavaTypes.class).equalTo("fieldId", 1).findAll().deleteAllFromRealm();
looperThreadRealm.commitTransaction();
verifyPostConditions(looperThreadRealm, new PostConditions() {
public void run(Realm realm) {
assertEquals(1, looperThreadRealm.where(AllJavaTypes.class).findAll().size());
assertEquals(1, counter.get());
}
}, child, parent);
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class RxJavaTests method realmResults_emittedOnUpdate.
@Test
@RunTestInLooperThread
public void realmResults_emittedOnUpdate() {
final AtomicInteger subscriberCalled = new AtomicInteger(0);
Realm realm = looperThread.realm;
realm.beginTransaction();
RealmResults<AllTypes> results = realm.where(AllTypes.class).findAll();
realm.commitTransaction();
subscription = results.asObservable().subscribe(new Action1<RealmResults<AllTypes>>() {
@Override
public void call(RealmResults<AllTypes> allTypes) {
if (subscriberCalled.incrementAndGet() == 2) {
looperThread.testComplete();
}
}
});
realm.beginTransaction();
realm.createObject(AllTypes.class);
realm.commitTransaction();
}
Aggregations