use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class ObjectChangeSetTests method changeLinkFieldOriginalObjectChanged_notTrigger.
@Test
@RunTestInLooperThread(before = PopulateOneAllTypes.class)
public void changeLinkFieldOriginalObjectChanged_notTrigger() {
Realm realm = looperThread.getRealm();
AllTypes allTypes = realm.where(AllTypes.class).findFirst();
listenerShouldNotBeCalled(allTypes);
realm.beginTransaction();
allTypes.getColumnRealmObject().setAge(42);
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.getRealm();
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 globalListener_looperThread_triggeredByLocalCommit.
@Test
@RunTestInLooperThread
public void globalListener_looperThread_triggeredByLocalCommit() {
final AtomicInteger success = new AtomicInteger(0);
Realm realm = looperThread.getRealm();
realm.addChangeListener(new RealmChangeListener<Realm>() {
@Override
public void onChange(Realm object) {
assertEquals(0, success.getAndIncrement());
looperThread.testComplete();
}
});
realm.beginTransaction();
realm.createObject(AllTypes.class);
realm.commitTransaction();
assertEquals(1, success.get());
}
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.getRealm();
final RealmResults<Dog> dogs = realm.where(Dog.class).findAllAsync();
final AtomicBoolean resultsListenerDone = new AtomicBoolean(false);
final AtomicBoolean realmListenerDone = new AtomicBoolean(false);
looperThread.keepStrongReference(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);
}
});
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class OrderedCollectionChangeSetTests method addChangeListener_bug5507.
// To reproduce https://github.com/realm/realm-java/issues/5507
// 1. Add listener to a collection
// A. change the collection in a background thread
// 2. Remove the listener
// 3. Add another listener
// 4. the listener added in step 3 should be triggered with change set in step A
@Test
@RunTestInLooperThread
public void addChangeListener_bug5507() throws InterruptedException {
// FIXME: See https://github.com/realm/realm-object-store/issues/605
if (type == ObservablesType.REALM_RESULTS) {
looperThread.testComplete();
return;
}
Realm realm = looperThread.getRealm();
populateData(realm, 1);
OrderedRealmCollectionChangeListener<OrderedRealmCollection<Dog>> listener1 = new OrderedRealmCollectionChangeListener<OrderedRealmCollection<Dog>>() {
@Override
public void onChange(OrderedRealmCollection<Dog> dogs, @Nullable OrderedCollectionChangeSet changeSet) {
fail();
}
};
OrderedRealmCollection<Dog> dogs = getTestingCollection(realm);
assertEquals(1, dogs.size());
if (type == ObservablesType.REALM_LIST) {
// noinspection unchecked
((RealmList) dogs).addChangeListener(listener1);
} else {
// noinspection unchecked
((RealmResults) dogs).addChangeListener(listener1);
}
Thread bgThread = new Thread(new Runnable() {
@Override
public void run() {
Realm realm = Realm.getInstance(looperThread.getConfiguration());
realm.beginTransaction();
createObjects(realm, 2);
realm.commitTransaction();
realm.close();
}
});
bgThread.start();
bgThread.join();
if (type == ObservablesType.REALM_LIST) {
// noinspection unchecked
((RealmList) dogs).removeChangeListener(listener1);
} else {
// noinspection unchecked
((RealmResults) dogs).removeChangeListener(listener1);
}
OrderedRealmCollectionChangeListener<OrderedRealmCollection<Dog>> listener2 = new OrderedRealmCollectionChangeListener<OrderedRealmCollection<Dog>>() {
@Override
public void onChange(OrderedRealmCollection<Dog> dogs, @Nullable OrderedCollectionChangeSet changeSet) {
assertEquals(2, dogs.size());
looperThread.testComplete();
}
};
if (type == ObservablesType.REALM_LIST) {
// noinspection unchecked
((RealmList) dogs).addChangeListener(listener2);
} else {
// noinspection unchecked
((RealmResults) dogs).addChangeListener(listener2);
}
}
Aggregations