Search in sources :

Example 16 with AllTypes

use of io.realm.entities.AllTypes in project realm-java by realm.

the class RealmResultsTests method removeAllChangeListeners.

@Test
@RunTestInLooperThread
public void removeAllChangeListeners() {
    final AtomicInteger listenersTriggered = new AtomicInteger(0);
    final Realm realm = looperThread.realm;
    RealmResults<AllTypes> collection = realm.where(AllTypes.class).findAll();
    RealmChangeListener<RealmResults<AllTypes>> listenerA = new RealmChangeListener<RealmResults<AllTypes>>() {

        @Override
        public void onChange(RealmResults<AllTypes> object) {
            listenersTriggered.incrementAndGet();
        }
    };
    RealmChangeListener<RealmResults<AllTypes>> listenerB = new RealmChangeListener<RealmResults<AllTypes>>() {

        @Override
        public void onChange(RealmResults<AllTypes> object) {
            listenersTriggered.incrementAndGet();
        }
    };
    looperThread.keepStrongReference.add(collection);
    collection.addChangeListener(listenerA);
    collection.addChangeListener(listenerB);
    collection.removeAllChangeListeners();
    realm.beginTransaction();
    realm.createObject(AllTypes.class);
    realm.commitTransaction();
    // The above commit should have put a REALM_CHANGED event on the Looper queue before this runnable.
    looperThread.postRunnable(new Runnable() {

        @Override
        public void run() {
            if (listenersTriggered.get() == 0) {
                looperThread.testComplete();
            } else {
                fail("Listeners wasn't removed");
            }
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AllTypes(io.realm.entities.AllTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test)

Example 17 with AllTypes

use of io.realm.entities.AllTypes in project realm-java by realm.

the class RealmResultsTests method size_returns_Integer_MAX_VALUE_for_huge_results.

@Test
public void size_returns_Integer_MAX_VALUE_for_huge_results() {
    final Collection collection = Mockito.mock(Collection.class);
    final RealmResults<AllTypes> targetResult = TestHelper.newRealmResults(realm, collection, AllTypes.class);
    Mockito.when(collection.isLoaded()).thenReturn(true);
    Mockito.when(collection.size()).thenReturn(((long) Integer.MAX_VALUE) - 1);
    assertEquals(Integer.MAX_VALUE - 1, targetResult.size());
    Mockito.when(collection.size()).thenReturn(((long) Integer.MAX_VALUE));
    assertEquals(Integer.MAX_VALUE, targetResult.size());
    Mockito.when(collection.size()).thenReturn(((long) Integer.MAX_VALUE) + 1);
    assertEquals(Integer.MAX_VALUE, targetResult.size());
}
Also used : Collection(io.realm.internal.Collection) AllTypes(io.realm.entities.AllTypes) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test)

Example 18 with AllTypes

use of io.realm.entities.AllTypes 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.realm;
    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.add(collection);
    collection.addChangeListener(listener);
    collection.addChangeListener(listener);
    collection.removeChangeListener(listener);
    realm.beginTransaction();
    realm.createObject(AllTypes.class);
    realm.commitTransaction();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AllTypes(io.realm.entities.AllTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test)

Example 19 with AllTypes

use of io.realm.entities.AllTypes in project realm-java by realm.

the class RealmQueryTests method greaterThanOrEqualTo_date.

@Test
public void greaterThanOrEqualTo_date() {
    final int TEST_OBJECTS_COUNT = 200;
    populateTestRealm(realm, TEST_OBJECTS_COUNT);
    RealmResults<AllTypes> resultList;
    resultList = realm.where(AllTypes.class).greaterThanOrEqualTo(AllTypes.FIELD_DATE, new Date(Long.MIN_VALUE)).findAll();
    assertEquals(TEST_OBJECTS_COUNT, resultList.size());
    resultList = realm.where(AllTypes.class).greaterThanOrEqualTo(AllTypes.FIELD_DATE, new Date(DECADE_MILLIS * -80)).findAll();
    assertEquals(180, resultList.size());
    resultList = realm.where(AllTypes.class).greaterThanOrEqualTo(AllTypes.FIELD_DATE, new Date(0)).findAll();
    assertEquals(TEST_OBJECTS_COUNT / 2, resultList.size());
    resultList = realm.where(AllTypes.class).greaterThanOrEqualTo(AllTypes.FIELD_DATE, new Date(DECADE_MILLIS * 80)).findAll();
    assertEquals(20, resultList.size());
    resultList = realm.where(AllTypes.class).greaterThanOrEqualTo(AllTypes.FIELD_DATE, new Date(Long.MAX_VALUE)).findAll();
    assertEquals(0, resultList.size());
}
Also used : AllTypes(io.realm.entities.AllTypes) Date(java.util.Date) Test(org.junit.Test)

Example 20 with AllTypes

use of io.realm.entities.AllTypes in project realm-java by realm.

the class RealmQueryTests method greaterThan_date.

@Test
public void greaterThan_date() {
    final int TEST_OBJECTS_COUNT = 200;
    populateTestRealm(realm, TEST_OBJECTS_COUNT);
    RealmResults<AllTypes> resultList;
    resultList = realm.where(AllTypes.class).greaterThan(AllTypes.FIELD_DATE, new Date(Long.MIN_VALUE)).findAll();
    assertEquals(TEST_OBJECTS_COUNT, resultList.size());
    resultList = realm.where(AllTypes.class).greaterThan(AllTypes.FIELD_DATE, new Date(DECADE_MILLIS * -80)).findAll();
    assertEquals(179, resultList.size());
    resultList = realm.where(AllTypes.class).greaterThan(AllTypes.FIELD_DATE, new Date(0)).findAll();
    assertEquals(TEST_OBJECTS_COUNT / 2 - 1, resultList.size());
    resultList = realm.where(AllTypes.class).greaterThan(AllTypes.FIELD_DATE, new Date(DECADE_MILLIS * 80)).findAll();
    assertEquals(19, resultList.size());
    resultList = realm.where(AllTypes.class).greaterThan(AllTypes.FIELD_DATE, new Date(Long.MAX_VALUE)).findAll();
    assertEquals(0, resultList.size());
}
Also used : AllTypes(io.realm.entities.AllTypes) Date(java.util.Date) Test(org.junit.Test)

Aggregations

AllTypes (io.realm.entities.AllTypes)134 Test (org.junit.Test)122 UiThreadTest (android.support.test.annotation.UiThreadTest)40 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)33 Date (java.util.Date)27 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 Action1 (rx.functions.Action1)12 JSONObject (org.json.JSONObject)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 Dog (io.realm.entities.Dog)8 InputStream (java.io.InputStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 NonLatinFieldNames (io.realm.entities.NonLatinFieldNames)5 SharedRealm (io.realm.internal.SharedRealm)5 RealmException (io.realm.exceptions.RealmException)4 RunInLooperThread (io.realm.rule.RunInLooperThread)3 IOException (java.io.IOException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ExecutionException (java.util.concurrent.ExecutionException)3 JSONException (org.json.JSONException)3