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.getRealm();
// 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(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);
}
});
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class TypeBasedNotificationsTests method non_looper_thread_commit_realmobject_async.
// UC 3 Async RealmObject.
// 1. Creates RealmObject async query.
// 2. Waits async returns then change the object.
// 3. Listener on the RealmObject gets triggered again.
@Test
@RunTestInLooperThread
public void non_looper_thread_commit_realmobject_async() {
final Realm realm = looperThread.getRealm();
realm.beginTransaction();
realm.createObject(Dog.class).setAge(1);
realm.commitTransaction();
Dog dog = realm.where(Dog.class).findFirstAsync();
looperThread.keepStrongReference(dog);
dog.addChangeListener(new RealmChangeListener<Dog>() {
@Override
public void onChange(Dog object) {
switch(typebasedCommitInvocations.incrementAndGet()) {
case 1:
assertEquals(1, object.getAge());
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.where(Dog.class).findFirst().setAge(17);
}
});
break;
case 2:
assertEquals(17, object.getAge());
looperThread.testComplete();
break;
default:
fail();
}
}
});
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class TypeBasedNotificationsTests method multiple_callbacks_should_be_invoked_realmresults_sync.
// UC 2 Sync RealmResults.
@Test
@RunTestInLooperThread
public void multiple_callbacks_should_be_invoked_realmresults_sync() {
final int NUMBER_OF_LISTENERS = 7;
final Realm realm = looperThread.getRealm();
realm.beginTransaction();
Dog akamaru = realm.createObject(Dog.class);
realm.commitTransaction();
RealmResults<Dog> dogs = realm.where(Dog.class).findAll();
looperThread.keepStrongReference(dogs);
for (int i = 0; i < NUMBER_OF_LISTENERS; i++) {
dogs.addChangeListener(new RealmChangeListener<RealmResults<Dog>>() {
@Override
public void onChange(RealmResults<Dog> results) {
assertEquals(17, results.first().getAge());
if (typebasedCommitInvocations.incrementAndGet() == NUMBER_OF_LISTENERS) {
looperThread.testComplete();
}
assertTrue(typebasedCommitInvocations.get() <= NUMBER_OF_LISTENERS);
}
});
}
realm.beginTransaction();
akamaru.setAge(17);
realm.commitTransaction();
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class TypeBasedNotificationsTests method callback_with_relevant_commit_realmresults_async.
// UC 1 Async RealmResults.
@Test
@RunTestInLooperThread
public void callback_with_relevant_commit_realmresults_async() {
final Realm realm = looperThread.getRealm();
// 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).findAllAsync();
looperThread.keepStrongReference(dogs);
dogs.addChangeListener(new RealmChangeListener<RealmResults<Dog>>() {
@Override
public void onChange(RealmResults<Dog> object) {
// Step 4: Responds to relevant change.
int commits = typebasedCommitInvocations.incrementAndGet();
switch(commits) {
case 1:
// Async query returns.
assertEquals(1, dogs.size());
assertEquals("Akamaru", dogs.get(0).getName());
// 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);
}
});
break;
case 2:
assertEquals(17, dogs.get(0).getAge());
looperThread.testComplete();
break;
default:
fail();
}
}
});
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class TypeBasedNotificationsTests method multiple_callbacks_should_be_invoked_realmobject_sync.
// ********************************************************************************* //
// UC 2.
// Multiple callbacks should be invoked after a relevant commit.
// ********************************************************************************* //
// UC 2 for Sync RealmObject.
@Test
@RunTestInLooperThread
public void multiple_callbacks_should_be_invoked_realmobject_sync() {
final int NUMBER_OF_LISTENERS = 7;
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(NUMBER_OF_LISTENERS, typebasedCommitInvocations.get());
looperThread.testComplete();
}
});
}
});
realm.beginTransaction();
Dog akamaru = realm.createObject(Dog.class);
realm.commitTransaction();
Dog dog = realm.where(Dog.class).findFirst();
looperThread.keepStrongReference(dog);
for (int i = 0; i < NUMBER_OF_LISTENERS; i++) {
dog.addChangeListener(new RealmChangeListener<Dog>() {
@Override
public void onChange(Dog object) {
typebasedCommitInvocations.incrementAndGet();
}
});
}
realm.beginTransaction();
akamaru.setAge(17);
realm.commitTransaction();
}
Aggregations