Search in sources :

Example 86 with RunTestInLooperThread

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();
}
Also used : AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 87 with RunTestInLooperThread

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();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 88 with RunTestInLooperThread

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);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) Dog(io.realm.entities.Dog) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) RunInLooperThread(io.realm.rule.RunInLooperThread) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 89 with RunTestInLooperThread

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();
        }
    });
}
Also used : SyncUser(io.realm.SyncUser) SyncCredentials(io.realm.SyncCredentials) ObjectServerError(io.realm.ObjectServerError) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 90 with RunTestInLooperThread

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);
}
Also used : SyncUser(io.realm.SyncUser) SyncCredentials(io.realm.SyncCredentials) ObjectServerError(io.realm.ObjectServerError) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Aggregations

RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)180 Test (org.junit.Test)180 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)80 AllTypes (io.realm.entities.AllTypes)76 Dog (io.realm.entities.Dog)59 RunInLooperThread (io.realm.rule.RunInLooperThread)30 AllJavaTypes (io.realm.entities.AllJavaTypes)29 AtomicLong (java.util.concurrent.atomic.AtomicLong)23 AndroidJUnit4 (androidx.test.ext.junit.runners.AndroidJUnit4)21 RealmLog (io.realm.log.RealmLog)21 Assert.assertEquals (org.junit.Assert.assertEquals)21 Assert.assertFalse (org.junit.Assert.assertFalse)21 Assert.assertNotNull (org.junit.Assert.assertNotNull)21 Assert.assertTrue (org.junit.Assert.assertTrue)21 Before (org.junit.Before)21 Rule (org.junit.Rule)21 RunWith (org.junit.runner.RunWith)21 Consumer (io.reactivex.functions.Consumer)20 Assert.fail (org.junit.Assert.fail)20 SystemClock (android.os.SystemClock)19