Search in sources :

Example 96 with RunTestInLooperThread

use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.

the class RealmQueryTests method distinctAsync.

@Test
@RunTestInLooperThread
public void distinctAsync() throws Throwable {
    final AtomicInteger changeListenerCalled = new AtomicInteger(4);
    final Realm realm = looperThread.realm;
    final long numberOfBlocks = 25;
    // Must be greater than 1
    final long numberOfObjects = 10;
    populateForDistinct(realm, numberOfBlocks, numberOfObjects, false);
    final RealmResults<AnnotationIndexTypes> distinctBool = realm.where(AnnotationIndexTypes.class).distinctAsync(AnnotationIndexTypes.FIELD_INDEX_BOOL);
    final RealmResults<AnnotationIndexTypes> distinctLong = realm.where(AnnotationIndexTypes.class).distinctAsync(AnnotationIndexTypes.FIELD_INDEX_LONG);
    final RealmResults<AnnotationIndexTypes> distinctDate = realm.where(AnnotationIndexTypes.class).distinctAsync(AnnotationIndexTypes.FIELD_INDEX_DATE);
    final RealmResults<AnnotationIndexTypes> distinctString = realm.where(AnnotationIndexTypes.class).distinctAsync(AnnotationIndexTypes.FIELD_INDEX_STRING);
    assertFalse(distinctBool.isLoaded());
    assertTrue(distinctBool.isValid());
    assertTrue(distinctBool.isEmpty());
    assertFalse(distinctLong.isLoaded());
    assertTrue(distinctLong.isValid());
    assertTrue(distinctLong.isEmpty());
    assertFalse(distinctDate.isLoaded());
    assertTrue(distinctDate.isValid());
    assertTrue(distinctDate.isEmpty());
    assertFalse(distinctString.isLoaded());
    assertTrue(distinctString.isValid());
    assertTrue(distinctString.isEmpty());
    final Runnable endTest = new Runnable() {

        @Override
        public void run() {
            if (changeListenerCalled.decrementAndGet() == 0) {
                looperThread.testComplete();
            }
        }
    };
    looperThread.keepStrongReference.add(distinctBool);
    looperThread.keepStrongReference.add(distinctLong);
    looperThread.keepStrongReference.add(distinctDate);
    looperThread.keepStrongReference.add(distinctString);
    distinctBool.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            assertEquals(2, distinctBool.size());
            endTest.run();
        }
    });
    distinctLong.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            assertEquals(numberOfBlocks, distinctLong.size());
            endTest.run();
        }
    });
    distinctDate.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            assertEquals(numberOfBlocks, distinctDate.size());
            endTest.run();
        }
    });
    distinctString.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            assertEquals(numberOfBlocks, distinctString.size());
            endTest.run();
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AnnotationIndexTypes(io.realm.entities.AnnotationIndexTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 97 with RunTestInLooperThread

use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.

the class LinkingObjectsManagedTests method notification_onCommitModelObject.

// A listener registered on the backlinked object should be called when a commit adds a backlink
@Test
@RunTestInLooperThread
public void notification_onCommitModelObject() {
    final Realm looperThreadRealm = looperThread.realm;
    looperThreadRealm.beginTransaction();
    AllJavaTypes child = looperThreadRealm.createObject(AllJavaTypes.class, 10);
    looperThreadRealm.commitTransaction();
    final AtomicInteger counter = new AtomicInteger(0);
    RealmChangeListener<AllJavaTypes> listener = new RealmChangeListener<AllJavaTypes>() {

        @Override
        public void onChange(AllJavaTypes object) {
            counter.incrementAndGet();
        }
    };
    child.addChangeListener(listener);
    looperThreadRealm.beginTransaction();
    AllJavaTypes parent = looperThreadRealm.createObject(AllJavaTypes.class, 1);
    parent.setFieldObject(child);
    looperThreadRealm.commitTransaction();
    verifyPostConditions(looperThreadRealm, new PostConditions() {

        public void run(Realm realm) {
            assertEquals(2, looperThreadRealm.where(AllJavaTypes.class).findAll().size());
            assertEquals(1, counter.get());
        }
    }, child, parent);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AllJavaTypes(io.realm.entities.AllJavaTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 98 with RunTestInLooperThread

use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.

the class LinkingObjectsManagedTests method notification_onDeleteModelObject.

// A listener registered on the backlinked object should be called when a backlinked object is deleted
@Test
@RunTestInLooperThread
public void notification_onDeleteModelObject() {
    final Realm looperThreadRealm = looperThread.realm;
    looperThreadRealm.beginTransaction();
    AllJavaTypes child = looperThreadRealm.createObject(AllJavaTypes.class, 10);
    AllJavaTypes parent = looperThreadRealm.createObject(AllJavaTypes.class, 1);
    parent.setFieldObject(child);
    looperThreadRealm.commitTransaction();
    final AtomicInteger counter = new AtomicInteger(0);
    RealmChangeListener<AllJavaTypes> listener = new RealmChangeListener<AllJavaTypes>() {

        @Override
        public void onChange(AllJavaTypes object) {
            counter.incrementAndGet();
        }
    };
    child.addChangeListener(listener);
    looperThreadRealm.beginTransaction();
    looperThreadRealm.where(AllJavaTypes.class).equalTo("fieldId", 1).findAll().deleteAllFromRealm();
    looperThreadRealm.commitTransaction();
    verifyPostConditions(looperThreadRealm, new PostConditions() {

        public void run(Realm realm) {
            assertEquals(1, looperThreadRealm.where(AllJavaTypes.class).findAll().size());
            assertEquals(1, counter.get());
        }
    }, child, parent);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AllJavaTypes(io.realm.entities.AllJavaTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 99 with RunTestInLooperThread

use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.

the class RealmResultsTests method distinctAsync_withNullValues.

@Test
@RunTestInLooperThread
public void distinctAsync_withNullValues() throws Throwable {
    final AtomicInteger changeListenerCalled = new AtomicInteger(2);
    final Realm realm = looperThread.realm;
    final long numberOfBlocks = 25;
    // Must be greater than 1
    final long numberOfObjects = 10;
    populateForDistinct(realm, numberOfBlocks, numberOfObjects, true);
    final RealmResults<AnnotationIndexTypes> distinctDate = realm.where(AnnotationIndexTypes.class).findAll().distinctAsync(AnnotationIndexTypes.FIELD_INDEX_DATE);
    final RealmResults<AnnotationIndexTypes> distinctString = realm.where(AnnotationIndexTypes.class).findAll().distinctAsync(AnnotationIndexTypes.FIELD_INDEX_STRING);
    assertFalse(distinctDate.isLoaded());
    assertTrue(distinctDate.isValid());
    assertTrue(distinctDate.isEmpty());
    assertFalse(distinctString.isLoaded());
    assertTrue(distinctString.isValid());
    assertTrue(distinctString.isEmpty());
    final Runnable endTest = new Runnable() {

        @Override
        public void run() {
            if (changeListenerCalled.decrementAndGet() == 0) {
                looperThread.testComplete();
            }
        }
    };
    looperThread.keepStrongReference.add(distinctDate);
    looperThread.keepStrongReference.add(distinctString);
    distinctDate.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            assertEquals("distinctDate", 1, distinctDate.size());
            endTest.run();
        }
    });
    distinctString.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            assertEquals("distinctString", 1, distinctString.size());
            endTest.run();
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AnnotationIndexTypes(io.realm.entities.AnnotationIndexTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test)

Example 100 with RunTestInLooperThread

use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.

the class RealmResultsTests method distinctAsync.

@Test
@RunTestInLooperThread
public void distinctAsync() throws Throwable {
    final AtomicInteger changeListenerCalled = new AtomicInteger(4);
    final Realm realm = looperThread.realm;
    final long numberOfBlocks = 25;
    // Must be greater than 1
    final long numberOfObjects = 10;
    populateForDistinct(realm, numberOfBlocks, numberOfObjects, false);
    final RealmResults<AnnotationIndexTypes> distinctBool = realm.where(AnnotationIndexTypes.class).findAll().distinctAsync(AnnotationIndexTypes.FIELD_INDEX_BOOL);
    final RealmResults<AnnotationIndexTypes> distinctLong = realm.where(AnnotationIndexTypes.class).findAll().distinctAsync(AnnotationIndexTypes.FIELD_INDEX_LONG);
    final RealmResults<AnnotationIndexTypes> distinctDate = realm.where(AnnotationIndexTypes.class).findAll().distinctAsync(AnnotationIndexTypes.FIELD_INDEX_DATE);
    final RealmResults<AnnotationIndexTypes> distinctString = realm.where(AnnotationIndexTypes.class).findAll().distinctAsync(AnnotationIndexTypes.FIELD_INDEX_STRING);
    assertFalse(distinctBool.isLoaded());
    assertTrue(distinctBool.isValid());
    assertTrue(distinctBool.isEmpty());
    assertFalse(distinctLong.isLoaded());
    assertTrue(distinctLong.isValid());
    assertTrue(distinctLong.isEmpty());
    assertFalse(distinctDate.isLoaded());
    assertTrue(distinctDate.isValid());
    assertTrue(distinctDate.isEmpty());
    assertFalse(distinctString.isLoaded());
    assertTrue(distinctString.isValid());
    assertTrue(distinctString.isEmpty());
    final Runnable endTest = new Runnable() {

        @Override
        public void run() {
            if (changeListenerCalled.decrementAndGet() == 0) {
                looperThread.testComplete();
            }
        }
    };
    looperThread.keepStrongReference.add(distinctBool);
    looperThread.keepStrongReference.add(distinctLong);
    looperThread.keepStrongReference.add(distinctDate);
    looperThread.keepStrongReference.add(distinctString);
    distinctBool.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            assertEquals(2, distinctBool.size());
            endTest.run();
        }
    });
    distinctLong.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            assertEquals(numberOfBlocks, distinctLong.size());
            endTest.run();
        }
    });
    distinctDate.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            assertEquals(numberOfBlocks, distinctDate.size());
            endTest.run();
        }
    });
    distinctString.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            assertEquals(numberOfBlocks, distinctString.size());
            endTest.run();
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AnnotationIndexTypes(io.realm.entities.AnnotationIndexTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) UiThreadTest(android.support.test.annotation.UiThreadTest) 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