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 RealmObjectTests method addChangeListener_returnedObjectOfCopyToRealmOrUpdate.
// Bug https://github.com/realm/realm-java/issues/2569
@Test
@RunTestInLooperThread
public void addChangeListener_returnedObjectOfCopyToRealmOrUpdate() {
Realm realm = looperThread.realm;
realm.beginTransaction();
realm.createObject(AllTypesPrimaryKey.class, 1);
AllTypesPrimaryKey allTypesPrimaryKey = new AllTypesPrimaryKey();
allTypesPrimaryKey.setColumnLong(1);
allTypesPrimaryKey.setColumnFloat(0f);
allTypesPrimaryKey = realm.copyToRealmOrUpdate(allTypesPrimaryKey);
realm.commitTransaction();
looperThread.keepStrongReference.add(allTypesPrimaryKey);
allTypesPrimaryKey.addChangeListener(new RealmChangeListener<AllTypesPrimaryKey>() {
@Override
public void onChange(AllTypesPrimaryKey element) {
assertEquals(42.0f, element.getColumnFloat(), 0f);
looperThread.testComplete();
}
});
// Change the object to trigger the listener.
realm.beginTransaction();
allTypesPrimaryKey.setColumnFloat(42f);
realm.commitTransaction();
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class TypeBasedNotificationsTests method callback_should_trigger_for_createObject.
// ****************************************************************************************** //
// UC 0.
// Callback should be notified if we create a RealmObject without the async mechanism.
// ex: using (createObject, copyOrUpdate, createObjectFromJson etc.)
// ***************************************************************************************** //
//UC 0 Uses Realm.createObject.
@Test
@RunTestInLooperThread
public void callback_should_trigger_for_createObject() {
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();
final Dog dog = realm.createObject(Dog.class);
realm.commitTransaction();
looperThread.keepStrongReference.add(dog);
dog.addChangeListener(new RealmChangeListener<Dog>() {
@Override
public void onChange(Dog object) {
assertEquals("Akamaru", dog.getName());
typebasedCommitInvocations.incrementAndGet();
}
});
realm.beginTransaction();
dog.setName("Akamaru");
realm.commitTransaction();
}
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.realm;
// 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.add(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 COMPLETED_ASYNC_REALM_OBJECT then commits transaction in another non-looper thread.
// 3. Listener on the RealmObject gets triggered again.
@Test
@RunTestInLooperThread
public void non_looper_thread_commit_realmobject_async() {
final Realm realm = looperThread.realm;
realm.addChangeListener(new RealmChangeListener<Realm>() {
@Override
public void onChange(Realm object) {
// Checks if the 2nd transaction is committed.
if (realm.where(Dog.class).count() == 2) {
looperThread.postRunnable(new Runnable() {
@Override
public void run() {
assertEquals(2, typebasedCommitInvocations.get());
looperThread.testComplete();
}
});
}
}
});
realm.beginTransaction();
realm.createObject(Dog.class);
realm.commitTransaction();
final Thread thread = new Thread() {
@Override
public void run() {
if (typebasedCommitInvocations.get() != 1) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
fail(e.getMessage());
}
}
Realm bgRealm = Realm.getInstance(realm.getConfiguration());
bgRealm.beginTransaction();
bgRealm.createObject(Dog.class);
bgRealm.commitTransaction();
bgRealm.close();
}
};
Dog dog = realm.where(Dog.class).findFirstAsync();
looperThread.keepStrongReference.add(dog);
dog.addChangeListener(new RealmChangeListener<Dog>() {
@Override
public void onChange(Dog object) {
typebasedCommitInvocations.incrementAndGet();
if (typebasedCommitInvocations.get() == 1) {
try {
thread.join();
} catch (InterruptedException e) {
fail(e.getMessage());
}
}
}
});
thread.start();
}
Aggregations