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.realm;
realm.addChangeListener(new RealmChangeListener<Realm>() {
@Override
public void onChange(Realm object) {
if (realm.where(Dog.class).count() == 2) {
looperThread.postRunnable(new Runnable() {
@Override
public void run() {
assertEquals(1, typebasedCommitInvocations.get());
looperThread.testComplete();
}
});
}
}
});
realm.beginTransaction();
realm.createObject(Dog.class);
realm.commitTransaction();
Dog dog = realm.where(Dog.class).findFirst();
looperThread.keepStrongReference.add(dog);
dog.addChangeListener(new RealmChangeListener<Dog>() {
@Override
public void onChange(Dog object) {
typebasedCommitInvocations.incrementAndGet();
}
});
Thread thread = new Thread() {
@Override
public void run() {
Realm bgRealm = Realm.getInstance(realm.getConfiguration());
bgRealm.beginTransaction();
bgRealm.createObject(Dog.class);
bgRealm.commitTransaction();
bgRealm.close();
}
};
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
fail(e.getMessage());
}
}
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.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).findAllAsync();
looperThread.keepStrongReference.add(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_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.realm;
realm.beginTransaction();
Dog akamaru = realm.createObject(Dog.class);
realm.commitTransaction();
RealmResults<Dog> dogs = realm.where(Dog.class).findAll();
looperThread.keepStrongReference.add(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_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.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();
}
});
}
}
});
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.add(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 RealmNotifierTests method post.
@Test
@RunTestInLooperThread
public void post() {
RealmNotifier notifier = new AndroidRealmNotifier(null, capabilitiesCanDeliver);
notifier.post(new Runnable() {
@Override
public void run() {
looperThread.testComplete();
}
});
}
Aggregations