Search in sources :

Example 91 with RunTestInLooperThread

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

the class AuthTests method login_withAccessToken.

@Test
@RunTestInLooperThread
public void login_withAccessToken() {
    SyncUser admin = UserFactory.createAdminUser(Constants.AUTH_URL);
    SyncCredentials credentials = SyncCredentials.accessToken(admin.getAccessToken().value(), "custom-admin-user");
    SyncUser.loginAsync(credentials, Constants.AUTH_URL, new SyncUser.Callback() {

        @Override
        public void onSuccess(SyncUser user) {
            final SyncConfiguration config = new SyncConfiguration.Builder(user, Constants.SYNC_SERVER_URL).errorHandler(new SyncSession.ErrorHandler() {

                @Override
                public void onError(SyncSession session, ObjectServerError error) {
                    fail("Session failed: " + error);
                }

                @Override
                public void onClientResetRequired(SyncSession session, ClientResetHandler handler) {
                    fail("Client Reset");
                }
            }).build();
            final Realm realm = Realm.getInstance(config);
            looperThread.testRealms.add(realm);
            // FIXME: Right now we have no Java API for detecting when a session is established
            // So we optimistically assume it has been connected after 1 second.
            looperThread.postRunnableDelayed(new Runnable() {

                @Override
                public void run() {
                    assertTrue(SyncManager.getSession(config).getUser().isValid());
                    looperThread.testComplete();
                }
            }, 1000);
        }

        @Override
        public void onError(ObjectServerError error) {
            fail("Login failed: " + error);
        }
    });
}
Also used : SyncUser(io.realm.SyncUser) ClientResetHandler(io.realm.ClientResetHandler) SyncSession(io.realm.SyncSession) SyncCredentials(io.realm.SyncCredentials) ObjectServerError(io.realm.ObjectServerError) Realm(io.realm.Realm) SyncConfiguration(io.realm.SyncConfiguration) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 92 with RunTestInLooperThread

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

the class ManagementRealmTests method create_acceptOffer.

@Ignore("TODO: Test is currently flaky. See https://github.com/realm/realm-java/pull/4066")
@Test
@RunTestInLooperThread
public void create_acceptOffer() {
    SyncUser user1 = UserFactory.createUser(Constants.AUTH_URL, "user1");
    final SyncUser user2 = UserFactory.createUser(Constants.AUTH_URL, "user2");
    // 1. User1 creates Realm that user2 does not have access
    final String user1RealmUrl = "realm://127.0.0.1:9080/" + user1.getIdentity() + "/permission-offer-test";
    SyncConfiguration config1 = new SyncConfiguration.Builder(user1, user1RealmUrl).errorHandler(new SyncSession.ErrorHandler() {

        @Override
        public void onError(SyncSession session, ObjectServerError error) {
            fail("Realm 1 unexpected error: " + error);
        }

        @Override
        public void onClientResetRequired(SyncSession session, ClientResetHandler handler) {
            fail("Client Reset");
        }
    }).build();
    final Realm realm1 = Realm.getInstance(config1);
    looperThread.testRealms.add(realm1);
    realm1.executeTransactionAsync(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            realm.createObject(Dog.class);
        }
    });
    // 2. Create configuration for User2's Realm.
    final SyncConfiguration config2 = new SyncConfiguration.Builder(user2, user1RealmUrl).build();
    // 3. Create PermissionOffer
    final AtomicReference<String> offerId = new AtomicReference<String>(null);
    final Realm user1ManagementRealm = user1.getManagementRealm();
    looperThread.testRealms.add(user1ManagementRealm);
    user1ManagementRealm.executeTransactionAsync(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            boolean readPermission = true;
            boolean readWritePermission = true;
            boolean managePermission = false;
            Date expiresAt = null;
            PermissionOffer offer = new PermissionOffer(user1RealmUrl, readPermission, readWritePermission, managePermission, expiresAt);
            offerId.set(offer.getId());
            realm.copyToRealm(offer);
        }
    }, new Realm.Transaction.OnSuccess() {

        @Override
        public void onSuccess() {
            // 4. Wait for offer to get an token
            RealmLog.error("OfferID: " + offerId.get());
            RealmResults<PermissionOffer> offers = user1ManagementRealm.where(PermissionOffer.class).equalTo("id", offerId.get()).findAllAsync();
            looperThread.keepStrongReference.add(offers);
            offers.addChangeListener(new RealmChangeListener<RealmResults<PermissionOffer>>() {

                @Override
                public void onChange(RealmResults<PermissionOffer> offers) {
                    final PermissionOffer offer = offers.first(null);
                    if (offer != null && offer.isSuccessful() && offer.getToken() != null) {
                        // 5. User2 uses the token to accept the offer
                        final String offerToken = offer.getToken();
                        final AtomicReference<String> offerResponseId = new AtomicReference<String>();
                        final Realm user2ManagementRealm = user2.getManagementRealm();
                        looperThread.testRealms.add(user2ManagementRealm);
                        user2ManagementRealm.executeTransactionAsync(new Realm.Transaction() {

                            @Override
                            public void execute(Realm realm) {
                                PermissionOfferResponse offerResponse = new PermissionOfferResponse(offerToken);
                                offerResponseId.set(offerResponse.getId());
                                realm.copyToRealm(offerResponse);
                            }
                        }, new Realm.Transaction.OnSuccess() {

                            @Override
                            public void onSuccess() {
                                // 6. Wait for the offer response to be accepted
                                RealmResults<PermissionOfferResponse> responses = user2ManagementRealm.where(PermissionOfferResponse.class).equalTo("id", offerResponseId.get()).findAllAsync();
                                looperThread.keepStrongReference.add(responses);
                                responses.addChangeListener(new RealmChangeListener<RealmResults<PermissionOfferResponse>>() {

                                    @Override
                                    public void onChange(RealmResults<PermissionOfferResponse> responses) {
                                        PermissionOfferResponse response = responses.first(null);
                                        if (response != null && response.isSuccessful() && response.getToken().equals(offerToken)) {
                                            // 7. Response accepted. It should now be possible for user2 to access user1's Realm
                                            Realm realm = Realm.getInstance(config2);
                                            looperThread.testRealms.add(realm);
                                            RealmResults<Dog> dogs = realm.where(Dog.class).findAll();
                                            looperThread.keepStrongReference.add(dogs);
                                            dogs.addChangeListener(new RealmChangeListener<RealmResults<Dog>>() {

                                                @Override
                                                public void onChange(RealmResults<Dog> element) {
                                                    assertEquals(1, element.size());
                                                    looperThread.testComplete();
                                                }
                                            });
                                        }
                                    }
                                });
                            }
                        });
                    }
                }
            });
        }
    });
}
Also used : RealmChangeListener(io.realm.RealmChangeListener) SyncUser(io.realm.SyncUser) PermissionOffer(io.realm.permissions.PermissionOffer) AtomicReference(java.util.concurrent.atomic.AtomicReference) ObjectServerError(io.realm.ObjectServerError) Date(java.util.Date) PermissionOfferResponse(io.realm.permissions.PermissionOfferResponse) ClientResetHandler(io.realm.ClientResetHandler) SyncSession(io.realm.SyncSession) Realm(io.realm.Realm) Dog(io.realm.entities.Dog) SyncConfiguration(io.realm.SyncConfiguration) RealmResults(io.realm.RealmResults) Ignore(org.junit.Ignore) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 93 with RunTestInLooperThread

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

the class RxJavaTests method realm_emittedOnUpdate.

@Test
@RunTestInLooperThread
public void realm_emittedOnUpdate() {
    final AtomicInteger subscriberCalled = new AtomicInteger(0);
    Realm realm = looperThread.realm;
    subscription = realm.asObservable().subscribe(new Action1<Realm>() {

        @Override
        public void call(Realm rxRealm) {
            if (subscriberCalled.incrementAndGet() == 2) {
                looperThread.testComplete();
            }
        }
    });
    realm.beginTransaction();
    realm.createObject(AllTypes.class);
    realm.commitTransaction();
}
Also used : Action1(rx.functions.Action1) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test)

Example 94 with RunTestInLooperThread

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

the class RxJavaTests method findFirstAsync_emittedOnUpdate.

@Test
@RunTestInLooperThread
public void findFirstAsync_emittedOnUpdate() {
    final AtomicInteger subscriberCalled = new AtomicInteger(0);
    Realm realm = looperThread.realm;
    realm.beginTransaction();
    AllTypes obj = realm.createObject(AllTypes.class);
    realm.commitTransaction();
    subscription = realm.where(AllTypes.class).findFirstAsync().<AllTypes>asObservable().subscribe(new Action1<AllTypes>() {

        @Override
        public void call(AllTypes rxObject) {
            if (subscriberCalled.incrementAndGet() == 2) {
                looperThread.testComplete();
            }
        }
    });
    realm.beginTransaction();
    obj.setColumnLong(1);
    realm.commitTransaction();
}
Also used : Action1(rx.functions.Action1) 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 95 with RunTestInLooperThread

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

the class RealmQueryTests method distinctAsync_invalidTypes.

@Test
@RunTestInLooperThread
public void distinctAsync_invalidTypes() {
    populateTestRealm(realm, TEST_DATA_SIZE);
    for (String field : new String[] { AllTypes.FIELD_REALMOBJECT, AllTypes.FIELD_REALMLIST, AllTypes.FIELD_DOUBLE, AllTypes.FIELD_FLOAT }) {
        try {
            realm.where(AllTypes.class).distinctAsync(field);
        } catch (IllegalArgumentException ignored) {
        }
    }
    looperThread.testComplete();
}
Also used : AllTypes(io.realm.entities.AllTypes) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) 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