use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class RealmChangeListenerTests method returnedRealmModelIsNotNull.
@Test
@RunTestInLooperThread
public void returnedRealmModelIsNotNull() {
Realm realm = looperThread.realm;
realm.beginTransaction();
AllTypesRealmModel model = realm.createObject(AllTypesRealmModel.class, 0);
realm.commitTransaction();
looperThread.keepStrongReference.add(model);
RealmObject.addChangeListener(model, new RealmChangeListener<AllTypesRealmModel>() {
@Override
public void onChange(AllTypesRealmModel object) {
assertEquals("model1", object.columnString);
looperThread.testComplete();
}
});
realm.beginTransaction();
model.columnString = "model1";
realm.commitTransaction();
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class NotificationsTest method globalListener_looperThread_triggeredByRemoteCommit.
@Test
@RunTestInLooperThread
public void globalListener_looperThread_triggeredByRemoteCommit() {
final AtomicInteger success = new AtomicInteger(0);
Realm realm = looperThread.realm;
realm.addChangeListener(new RealmChangeListener<Realm>() {
@Override
public void onChange(Realm object) {
assertEquals(1, success.get());
looperThread.testComplete();
}
});
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.createObject(AllTypes.class);
}
});
assertEquals(0, success.getAndIncrement());
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class NotificationsTest method asyncRealmObjectShouldNotBlockBackgroundCommitNotification.
// The presence of async RealmResults blocks any `REALM_CHANGE` notification . We make sure in this test that all
// Realm listeners will be notified regardless of the presence of an async RealmObject. RealmObjects are special
// in the sense that once you got a row accessor to that object, it is automatically up to date as soon as you
// call advance_read().
@Test
@RunTestInLooperThread
public void asyncRealmObjectShouldNotBlockBackgroundCommitNotification() {
final AtomicInteger numberOfRealmCallbackInvocation = new AtomicInteger(0);
final CountDownLatch signalClosedRealm = new CountDownLatch(1);
final Realm realm = looperThread.realm;
realm.addChangeListener(new RealmChangeListener<Realm>() {
@Override
public void onChange(final Realm realm) {
switch(numberOfRealmCallbackInvocation.incrementAndGet()) {
case 1:
{
// First commit.
Dog dog = realm.where(Dog.class).findFirstAsync();
assertTrue(dog.load());
dog.addChangeListener(new RealmChangeListener<Dog>() {
@Override
public void onChange(Dog dog) {
}
});
new Thread() {
@Override
public void run() {
Realm threadRealm = Realm.getInstance(realm.getConfiguration());
threadRealm.beginTransaction();
threadRealm.createObject(Dog.class);
threadRealm.commitTransaction();
threadRealm.close();
signalClosedRealm.countDown();
}
}.start();
break;
}
case 2:
{
// Finishes test.
TestHelper.awaitOrFail(signalClosedRealm);
looperThread.testComplete();
break;
}
}
}
});
looperThread.postRunnable(new Runnable() {
@Override
public void run() {
realm.beginTransaction();
realm.createObject(Dog.class);
realm.commitTransaction();
}
});
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class NotificationsTest method realmObjectListenerAddedAfterCommit.
// TODO: Fix or delete this test after integration of object notification from Object Store
@Test
@RunTestInLooperThread
@Ignore
public void realmObjectListenerAddedAfterCommit() {
Realm realm = looperThread.realm;
realm.beginTransaction();
AllTypes obj = realm.createObject(AllTypes.class);
realm.commitTransaction();
realm.beginTransaction();
obj.setColumnLong(42);
realm.commitTransaction();
obj.addChangeListener(new RealmChangeListener<AllTypes>() {
@Override
public void onChange(AllTypes object) {
looperThread.testComplete();
}
});
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class NotificationsTest method asyncRealmResultsShouldNotBlockBackgroundCommitNotification.
// The presence of async RealmResults block any `REALM_CHANGE` notification causing historically the Realm
// to advance to the latest version. We make sure in this test that all Realm listeners will be notified
// regardless of the presence of an async RealmResults that will delay the `REALM_CHANGE` sometimes.
@Test
@RunTestInLooperThread
public void asyncRealmResultsShouldNotBlockBackgroundCommitNotification() {
final Realm realm = looperThread.realm;
final RealmResults<Dog> dogs = realm.where(Dog.class).findAllAsync();
final AtomicBoolean resultsListenerDone = new AtomicBoolean(false);
final AtomicBoolean realmListenerDone = new AtomicBoolean(false);
looperThread.keepStrongReference.add(dogs);
assertTrue(dogs.load());
assertEquals(0, dogs.size());
dogs.addChangeListener(new RealmChangeListener<RealmResults<Dog>>() {
@Override
public void onChange(RealmResults<Dog> results) {
if (dogs.size() == 2) {
// Results has the latest changes.
resultsListenerDone.set(true);
if (realmListenerDone.get()) {
looperThread.testComplete();
}
}
}
});
realm.addChangeListener(new RealmChangeListener<Realm>() {
@Override
public void onChange(Realm element) {
if (dogs.size() == 1) {
// Step 2. Creates the second dog.
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.createObject(Dog.class);
}
});
} else if (dogs.size() == 2) {
// Realm listener can see the latest changes.
realmListenerDone.set(true);
if (resultsListenerDone.get()) {
looperThread.testComplete();
}
}
}
});
// Step 1. Creates the first dog.
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.createObject(Dog.class);
}
});
}
Aggregations