use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class RealmNotifierTests method post.
@Test
@RunTestInLooperThread
public void post() {
RealmNotifier notifier = new AndroidRealmNotifier(null, capabilitiesCanDeliver);
notifier.post(new Runnable() {
@Override
public void run() {
looperThread.testComplete();
}
});
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class RealmNotifierTests method addChangeListener_byRemoteChanges.
@Test
@RunTestInLooperThread
public void addChangeListener_byRemoteChanges() {
// To catch https://github.com/realm/realm-java/pull/4037 CI failure.
// In this case, object store should not send more than 100 notifications.
final int TIMES = 100;
final AtomicInteger commitCounter = new AtomicInteger(0);
final AtomicInteger listenerCounter = new AtomicInteger(0);
looperThread.realm.close();
SharedRealm sharedRealm = getSharedRealm(looperThread.realmConfiguration);
looperThread.keepStrongReference.add(sharedRealm);
sharedRealm.realmNotifier.addChangeListener(sharedRealm, new RealmChangeListener<SharedRealm>() {
@Override
public void onChange(SharedRealm sharedRealm) {
int commits = commitCounter.get();
int listenerCount = listenerCounter.addAndGet(1);
assertEquals(commits, listenerCount);
if (commits == TIMES) {
sharedRealm.close();
looperThread.testComplete();
} else {
makeRemoteChanges(looperThread.realmConfiguration);
commitCounter.getAndIncrement();
}
}
});
makeRemoteChanges(looperThread.realmConfiguration);
commitCounter.getAndIncrement();
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class RealmNotifierTests method addChangeListener_byLocalChanges.
// Callback is immediately called when commitTransaction for local changes.
@Test
@RunTestInLooperThread
public void addChangeListener_byLocalChanges() {
final AtomicBoolean commitReturns = new AtomicBoolean(false);
SharedRealm sharedRealm = getSharedRealm(looperThread.realmConfiguration);
sharedRealm.realmNotifier.addChangeListener(sharedRealm, new RealmChangeListener<SharedRealm>() {
@Override
public void onChange(SharedRealm sharedRealm) {
// Transaction has been committed in core, but commitTransaction hasn't returned in java.
assertFalse(commitReturns.get());
looperThread.testComplete();
sharedRealm.close();
}
});
sharedRealm.beginTransaction();
sharedRealm.commitTransaction();
commitReturns.set(true);
}
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.realm;
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.add(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 TypeBasedNotificationsTests method callback_should_trigger_for_copyToRealm.
//UC 0 Uses Realm.copyToRealm.
@Test
@RunTestInLooperThread
public void callback_should_trigger_for_copyToRealm() {
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();
Dog akamaru = new Dog();
akamaru.setName("Akamaru");
final Dog dog = realm.copyToRealm(akamaru);
realm.commitTransaction();
looperThread.keepStrongReference.add(dog);
dog.addChangeListener(new RealmChangeListener<Dog>() {
@Override
public void onChange(Dog object) {
assertEquals(8, dog.getAge());
typebasedCommitInvocations.incrementAndGet();
}
});
realm.beginTransaction();
dog.setAge(8);
realm.commitTransaction();
}
Aggregations