Search in sources :

Example 1 with OSSubscriptionStateChanges

use of com.onesignal.OSSubscriptionStateChanges in project OneSignal-Android-SDK by OneSignal.

the class MainOneSignalClassRunner method shouldFireSubscriptionObserverWhenUserDisablesNotifications.

@Test
public void shouldFireSubscriptionObserverWhenUserDisablesNotifications() throws Exception {
    ShadowOneSignalRestClient.setRemoteParamsGetHtmlResponse(new JSONObject().put("unsubscribe_on_notifications_disabled", false));
    OneSignalInit();
    threadAndTaskWait();
    OSSubscriptionObserver subscriptionObserver = new OSSubscriptionObserver() {

        @Override
        public void onOSSubscriptionChanged(OSSubscriptionStateChanges stateChanges) {
            lastSubscriptionStateChanges = stateChanges;
            currentSubscription = stateChanges.getTo().isSubscribed();
        }
    };
    OneSignal.addSubscriptionObserver(subscriptionObserver);
    lastSubscriptionStateChanges = null;
    // Make sure garbage collection doesn't nuke any observers.
    Runtime.getRuntime().gc();
    pauseActivity(blankActivityController);
    ShadowNotificationManagerCompat.enabled = false;
    blankActivityController.resume();
    threadAndTaskWait();
    assertTrue(lastSubscriptionStateChanges.getFrom().isSubscribed());
    assertFalse(lastSubscriptionStateChanges.getTo().isSubscribed());
    // Test to make sure object was correct at the time of firing.
    assertFalse(currentSubscription);
    // unsubscribeWhenNotificationsAreDisabled is not set so don't send notification_types.
    assertFalse(ShadowOneSignalRestClient.lastPost.has("notification_types"));
}
Also used : OSSubscriptionStateChanges(com.onesignal.OSSubscriptionStateChanges) JSONObject(org.json.JSONObject) OSSubscriptionObserver(com.onesignal.OSSubscriptionObserver) Test(org.junit.Test)

Example 2 with OSSubscriptionStateChanges

use of com.onesignal.OSSubscriptionStateChanges in project OneSignal-Android-SDK by OneSignal.

the class MainOneSignalClassRunner method shouldFireSubscriptionObserverWhenChangesHappen.

@Test
public void shouldFireSubscriptionObserverWhenChangesHappen() throws Exception {
    OneSignalInit();
    OSSubscriptionObserver permissionObserver = new OSSubscriptionObserver() {

        @Override
        public void onOSSubscriptionChanged(OSSubscriptionStateChanges stateChanges) {
            lastSubscriptionStateChanges = stateChanges;
            currentSubscription = stateChanges.getTo().isSubscribed();
        }
    };
    OneSignal.addSubscriptionObserver(permissionObserver);
    threadAndTaskWait();
    assertFalse(lastSubscriptionStateChanges.getFrom().isSubscribed());
    assertTrue(lastSubscriptionStateChanges.getTo().isSubscribed());
    // Test to make sure object was correct at the time of firing.
    assertTrue(currentSubscription);
    assertFalse(lastSubscriptionStateChanges.getTo().isPushDisabled());
    assertEquals(ShadowPushRegistratorFCM.regId, lastSubscriptionStateChanges.getTo().getPushToken());
    assertEquals(ShadowOneSignalRestClient.pushUserId, lastSubscriptionStateChanges.getTo().getUserId());
}
Also used : OSSubscriptionStateChanges(com.onesignal.OSSubscriptionStateChanges) OSSubscriptionObserver(com.onesignal.OSSubscriptionObserver) Test(org.junit.Test)

Example 3 with OSSubscriptionStateChanges

use of com.onesignal.OSSubscriptionStateChanges in project OneSignal-Android-SDK by OneSignal.

the class MainOneSignalClassRunner method shouldFireSubscriptionObserverOnFirstAdd.

@Test
public void shouldFireSubscriptionObserverOnFirstAdd() throws Exception {
    OneSignalInit();
    threadAndTaskWait();
    OSSubscriptionObserver permissionObserver = new OSSubscriptionObserver() {

        @Override
        public void onOSSubscriptionChanged(OSSubscriptionStateChanges stateChanges) {
            lastSubscriptionStateChanges = stateChanges;
            currentSubscription = stateChanges.getTo().isSubscribed();
        }
    };
    OneSignal.addSubscriptionObserver(permissionObserver);
    assertFalse(lastSubscriptionStateChanges.getFrom().isSubscribed());
    assertTrue(lastSubscriptionStateChanges.getTo().isSubscribed());
    // Test to make sure object was correct at the time of firing.
    assertTrue(currentSubscription);
}
Also used : OSSubscriptionStateChanges(com.onesignal.OSSubscriptionStateChanges) OSSubscriptionObserver(com.onesignal.OSSubscriptionObserver) Test(org.junit.Test)

Example 4 with OSSubscriptionStateChanges

use of com.onesignal.OSSubscriptionStateChanges in project OneSignal-Android-SDK by OneSignal.

the class MainOneSignalClassRunner method shouldNotFireSubscriptionObserverWhenChangesHappenIfRemoved.

@Test
public void shouldNotFireSubscriptionObserverWhenChangesHappenIfRemoved() throws Exception {
    OneSignalInit();
    OSSubscriptionObserver permissionObserver = new OSSubscriptionObserver() {

        @Override
        public void onOSSubscriptionChanged(OSSubscriptionStateChanges stateChanges) {
            lastSubscriptionStateChanges = stateChanges;
            currentSubscription = stateChanges.getTo().isSubscribed();
        }
    };
    OneSignal.addSubscriptionObserver(permissionObserver);
    lastSubscriptionStateChanges = null;
    OneSignal.removeSubscriptionObserver(permissionObserver);
    threadAndTaskWait();
    assertFalse(currentSubscription);
    assertNull(lastSubscriptionStateChanges);
}
Also used : OSSubscriptionStateChanges(com.onesignal.OSSubscriptionStateChanges) OSSubscriptionObserver(com.onesignal.OSSubscriptionObserver) Test(org.junit.Test)

Aggregations

OSSubscriptionObserver (com.onesignal.OSSubscriptionObserver)4 OSSubscriptionStateChanges (com.onesignal.OSSubscriptionStateChanges)4 Test (org.junit.Test)4 JSONObject (org.json.JSONObject)1