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.getRealm();
realm.beginTransaction();
AllTypesRealmModel model = realm.createObject(AllTypesRealmModel.class, 0);
realm.commitTransaction();
looperThread.keepStrongReference(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 RxJavaTests method findAllAsync_emittedOnUpdate.
@Test
@RunTestInLooperThread
public void findAllAsync_emittedOnUpdate() {
final AtomicInteger subscriberCalled = new AtomicInteger(0);
Realm realm = looperThread.getRealm();
subscription = realm.where(AllTypes.class).findAllAsync().asFlowable().subscribe(rxResults -> {
assertTrue(rxResults.isFrozen());
if (subscriberCalled.incrementAndGet() == 2) {
disposeSuccessfulTest(realm);
}
});
realm.beginTransaction();
realm.createObject(AllTypes.class);
realm.commitTransaction();
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class RxJavaTests method realmObject_emittedOnSubscribe.
@Test
@RunTestInLooperThread
public void realmObject_emittedOnSubscribe() {
realm.beginTransaction();
final AllJavaTypes obj = realm.createObject(AllJavaTypes.class, 42);
realm.commitTransaction();
subscription = obj.<AllJavaTypes>asFlowable().subscribe(rxObject -> {
assertTrue(rxObject.isFrozen());
// Frozen objects are not equal to their live counter parts.
assertNotEquals(rxObject, obj);
assertEquals(rxObject.getFieldId(), obj.getFieldId());
disposeSuccessfulTest(realm);
});
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class RxJavaTests method realmList_parentDeletionCompleteFlowable.
@Test
@RunTestInLooperThread
public void realmList_parentDeletionCompleteFlowable() {
realm.beginTransaction();
final AllTypes parent = realm.createObject(AllTypes.class);
final RealmList<Dog> list = parent.getColumnRealmList();
list.add(new Dog("Fido"));
realm.commitTransaction();
looperThread.keepStrongReference(parent);
// We should only emit valid lists. If the parent of the list is invalidated
// it should close the stream gracefully resulting in onComplete being called.
subscription = list.asFlowable().subscribe(change -> {
assertTrue(change.isValid());
}, error -> {
fail(error.toString());
}, () -> {
// Deleting the parent will gracefully close the stream
disposeSuccessfulTest(realm);
});
looperThread.postRunnable(() -> {
realm.beginTransaction();
parent.deleteFromRealm();
realm.commitTransaction();
});
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class RealmResultsTests method addChangeListener_twice.
@Test
@RunTestInLooperThread
public void addChangeListener_twice() {
final AtomicInteger listenersTriggered = new AtomicInteger(0);
final Realm realm = looperThread.getRealm();
RealmResults<AllTypes> collection = realm.where(AllTypes.class).findAll();
RealmChangeListener<RealmResults<AllTypes>> listener = new RealmChangeListener<RealmResults<AllTypes>>() {
@Override
public void onChange(RealmResults<AllTypes> object) {
listenersTriggered.incrementAndGet();
}
};
realm.addChangeListener(new RealmChangeListener<Realm>() {
@Override
public void onChange(Realm object) {
listenersTriggered.incrementAndGet();
looperThread.postRunnable(new Runnable() {
@Override
public void run() {
if (listenersTriggered.get() == 1) {
looperThread.testComplete();
} else {
fail("Only global listener should be triggered");
}
}
});
}
});
// Adding it twice will be ignored, so removing it will not cause the listener to be triggered.
looperThread.keepStrongReference(collection);
collection.addChangeListener(listener);
collection.addChangeListener(listener);
collection.removeChangeListener(listener);
realm.beginTransaction();
realm.createObject(AllTypes.class);
realm.commitTransaction();
}
Aggregations