use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class TypeBasedNotificationsTests method callback_should_trigger_for_copyToRealmOrUpdate.
// UC 0 Uses Realm.copyToRealmOrUpdate.
@Test
@RunTestInLooperThread
public void callback_should_trigger_for_copyToRealmOrUpdate() {
final Realm realm = looperThread.getRealm();
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();
}
});
}
}
});
PrimaryKeyAsLong obj = new PrimaryKeyAsLong();
obj.setId(1);
obj.setName("Foo");
realm.beginTransaction();
final PrimaryKeyAsLong primaryKeyAsLong = realm.copyToRealmOrUpdate(obj);
realm.commitTransaction();
looperThread.keepStrongReference(primaryKeyAsLong);
primaryKeyAsLong.addChangeListener(new RealmChangeListener<PrimaryKeyAsLong>() {
@Override
public void onChange(PrimaryKeyAsLong object) {
assertEquals(1, primaryKeyAsLong.getId());
assertEquals("Bar", primaryKeyAsLong.getName());
assertEquals(1, realm.where(PrimaryKeyAsLong.class).count());
typebasedCommitInvocations.incrementAndGet();
}
});
PrimaryKeyAsLong obj2 = new PrimaryKeyAsLong();
obj2.setId(1);
obj2.setName("Bar");
realm.beginTransaction();
PrimaryKeyAsLong primaryKeyAsLong2 = realm.copyToRealmOrUpdate(obj2);
realm.commitTransaction();
assertEquals(primaryKeyAsLong, primaryKeyAsLong2);
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class TypeBasedNotificationsTests method callback_should_trigger_for_createOrUpdateObjectFromJson.
// UC 0 Uses Realm.createOrUpdateObjectFromJson.
@Test
@RunTestInLooperThread
public void callback_should_trigger_for_createOrUpdateObjectFromJson() {
assumeThat(Build.VERSION.SDK_INT, greaterThanOrEqualTo(Build.VERSION_CODES.HONEYCOMB));
final Realm realm = looperThread.getRealm();
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();
}
});
}
}
});
try {
AllTypesPrimaryKey obj = new AllTypesPrimaryKey();
Date date = new Date(0);
// ID
obj.setColumnLong(1);
obj.setColumnBinary(new byte[] { 1 });
obj.setColumnBoolean(true);
obj.setColumnDate(date);
obj.setColumnDouble(1);
obj.setColumnFloat(1);
obj.setColumnString("1");
realm.beginTransaction();
realm.copyToRealm(obj);
realm.commitTransaction();
InputStream in = TestHelper.loadJsonFromAssets(context, "all_types_primary_key_field_only.json");
realm.beginTransaction();
final AllTypesPrimaryKey objectFromJson = realm.createOrUpdateObjectFromJson(AllTypesPrimaryKey.class, in);
realm.commitTransaction();
in.close();
looperThread.keepStrongReference(objectFromJson);
objectFromJson.addChangeListener(new RealmChangeListener<AllTypesPrimaryKey>() {
@Override
public void onChange(AllTypesPrimaryKey object) {
assertEquals("ObjectFromJson", objectFromJson.getColumnString());
assertEquals(1L, objectFromJson.getColumnLong());
assertEquals(1F, objectFromJson.getColumnFloat(), 0F);
assertEquals(1D, objectFromJson.getColumnDouble(), 0D);
assertEquals(true, objectFromJson.isColumnBoolean());
assertArrayEquals(new byte[] { 1 }, objectFromJson.getColumnBinary());
assertNull(objectFromJson.getColumnRealmObject());
assertEquals(0, objectFromJson.getColumnRealmList().size());
typebasedCommitInvocations.incrementAndGet();
}
});
realm.beginTransaction();
objectFromJson.setColumnString("ObjectFromJson");
realm.commitTransaction();
} catch (IOException e) {
fail(e.getMessage());
}
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class TypeBasedNotificationsTests method non_looper_thread_commit_realmobject_sync.
// ********************************************************************************* //
// UC 3.
// Callback should be invoked when a non Looper thread commits.
// ********************************************************************************* //
// UC 3 for Sync RealmObject.
// 1. Adds listener to RealmObject which is queried synchronized.
// 2. Commits transaction in another non-looper thread.
// 3. Listener on the RealmObject gets triggered.
@Test
@RunTestInLooperThread
public void non_looper_thread_commit_realmobject_sync() {
final Realm realm = looperThread.getRealm();
realm.beginTransaction();
realm.createObject(Dog.class);
realm.commitTransaction();
Dog dog = realm.where(Dog.class).findFirst();
looperThread.keepStrongReference(dog);
dog.addChangeListener(new RealmChangeListener<Dog>() {
@Override
public void onChange(Dog object) {
assertEquals(17, object.getAge());
looperThread.testComplete();
}
});
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 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.getRealm();
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(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_should_trigger_for_copyToRealm.
// UC 0 Uses Realm.copyToRealm.
@Test
@RunTestInLooperThread
public void callback_should_trigger_for_copyToRealm() {
final Realm realm = looperThread.getRealm();
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(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