use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class RealmObjectTests method addChangeListener_returnedObjectOfCopyToRealmOrUpdate.
// Bug https://github.com/realm/realm-java/issues/2569
@Test
@RunTestInLooperThread
public void addChangeListener_returnedObjectOfCopyToRealmOrUpdate() {
Realm realm = looperThread.getRealm();
realm.beginTransaction();
realm.createObject(AllTypesPrimaryKey.class, 1);
AllTypesPrimaryKey allTypesPrimaryKey = new AllTypesPrimaryKey();
allTypesPrimaryKey.setColumnLong(1);
allTypesPrimaryKey.setColumnFloat(0f);
allTypesPrimaryKey = realm.copyToRealmOrUpdate(allTypesPrimaryKey);
realm.commitTransaction();
looperThread.keepStrongReference(allTypesPrimaryKey);
allTypesPrimaryKey.addChangeListener(new RealmChangeListener<AllTypesPrimaryKey>() {
@Override
public void onChange(AllTypesPrimaryKey element) {
assertEquals(42.0f, element.getColumnFloat(), 0f);
looperThread.testComplete();
}
});
// Change the object to trigger the listener.
realm.beginTransaction();
allTypesPrimaryKey.setColumnFloat(42f);
realm.commitTransaction();
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class OsResultsTests method addListener_triggeredByLocalCommit.
// Local commit will trigger the listener first when beginTransaction gets called then again when transaction
// committed.
@Test
@RunTestInLooperThread
public void addListener_triggeredByLocalCommit() {
final OsSharedRealm sharedRealm = getSharedRealmForLooper();
populateData(sharedRealm);
Table table = getTable(sharedRealm);
final AtomicInteger listenerCounter = new AtomicInteger(0);
final OsResults osResults = OsResults.createFromQuery(sharedRealm, table.where());
looperThread.keepStrongReference(osResults);
osResults.addListener(osResults, new RealmChangeListener<OsResults>() {
@Override
public void onChange(OsResults osResults1) {
switch(listenerCounter.getAndIncrement()) {
case 0:
assertEquals(4, osResults1.size());
break;
case 1:
assertEquals(5, osResults1.size());
sharedRealm.close();
break;
default:
fail();
break;
}
}
});
addRow(sharedRealm);
assertEquals(2, listenerCounter.get());
looperThread.testComplete();
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class OrderedCollectionChangeSetTests method emptyChangeSet_findAllAsync.
// The change set should empty when the async query returns at the first time.
@Test
@RunTestInLooperThread
public void emptyChangeSet_findAllAsync() {
if (type == ObservablesType.REALM_LIST) {
looperThread.testComplete();
return;
}
Realm realm = looperThread.realm;
populateData(realm, 10);
final RealmResults<Dog> results = realm.where(Dog.class).findAllSortedAsync(Dog.FIELD_AGE);
results.addChangeListener(new OrderedRealmCollectionChangeListener<RealmResults<Dog>>() {
@Override
public void onChange(RealmResults<Dog> collection, OrderedCollectionChangeSet changeSet) {
assertSame(collection, results);
assertEquals(9, collection.size());
assertNull(changeSet);
looperThread.testComplete();
}
});
final CountDownLatch bgDeletionLatch = new CountDownLatch(1);
// beginTransaction() will make the async query return immediately. So we have to create an object in another
// thread. Also, the latch has to be counted down after transaction committed so the async query results can
// contain the modification in the background transaction.
new Thread(new Runnable() {
@Override
public void run() {
Realm realm = Realm.getInstance(looperThread.realmConfiguration);
realm.beginTransaction();
realm.where(Dog.class).equalTo(Dog.FIELD_AGE, 0).findFirst().deleteFromRealm();
realm.commitTransaction();
realm.close();
bgDeletionLatch.countDown();
}
}).start();
TestHelper.awaitOrFail(bgDeletionLatch);
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class AuthTests method loginAsync_userNotExist.
@Test
@RunTestInLooperThread
public void loginAsync_userNotExist() {
SyncCredentials credentials = SyncCredentials.usernamePassword("IWantToHackYou", "GeneralPassword", false);
SyncUser.loginAsync(credentials, Constants.AUTH_URL, new SyncUser.Callback() {
@Override
public void onSuccess(SyncUser user) {
fail();
}
@Override
public void onError(ObjectServerError error) {
assertEquals(ErrorCode.INVALID_CREDENTIALS, error.getErrorCode());
looperThread.testComplete();
}
});
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class AuthTests method loginAsync_errorHandlerThrows.
// The error handler throws an exception but it is ignored (but logged). That means, this test should not
// pass and not be stopped by an IllegalArgumentException.
@Test
@RunTestInLooperThread
public void loginAsync_errorHandlerThrows() {
// set log level to info to make sure the IllegalArgumentException
// thrown in the test is visible in Logcat
final int defaultLevel = RealmLog.getLevel();
RealmLog.setLevel(LogLevel.INFO);
SyncCredentials credentials = SyncCredentials.usernamePassword("IWantToHackYou", "GeneralPassword", false);
SyncUser.loginAsync(credentials, Constants.AUTH_URL, new SyncUser.Callback() {
@Override
public void onSuccess(SyncUser user) {
fail();
}
@Override
public void onError(ObjectServerError error) {
assertEquals(ErrorCode.INVALID_CREDENTIALS, error.getErrorCode());
throw new IllegalArgumentException("BOOM");
}
});
looperThread.postRunnableDelayed(new Runnable() {
@Override
public void run() {
RealmLog.setLevel(defaultLevel);
looperThread.testComplete();
}
}, 1000);
}
Aggregations