use of com.onesignal.OSSubscriptionObserver 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"));
}
use of com.onesignal.OSSubscriptionObserver 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());
}
use of com.onesignal.OSSubscriptionObserver in project OneSignal-Android-SDK by OneSignal.
the class MainOneSignalClassRunner method shouldAddSubscriptionObserverIfConsentNotGranted.
// Functions to add observers (like addSubscriptionObserver) should continue
// to work even if privacy consent has not been granted.
@Test
public void shouldAddSubscriptionObserverIfConsentNotGranted() throws Exception {
ShadowOneSignalRestClient.setRemoteParamsRequirePrivacyConsent(true);
OneSignalInit();
threadAndTaskWait();
OSSubscriptionObserver subscriptionObserver = stateChanges -> {
lastSubscriptionStateChanges = stateChanges;
currentSubscription = stateChanges.getTo().isSubscribed();
};
OneSignal.addSubscriptionObserver(subscriptionObserver);
lastSubscriptionStateChanges = null;
// Make sure garbage collection doesn't nuke any observers.
Runtime.getRuntime().gc();
OneSignal.provideUserConsent(true);
threadAndTaskWait();
// make sure the subscription observer was fired
assertTrue(lastSubscriptionStateChanges.getTo().isSubscribed());
assertFalse(lastSubscriptionStateChanges.getFrom().isSubscribed());
}
use of com.onesignal.OSSubscriptionObserver 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);
}
use of com.onesignal.OSSubscriptionObserver 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);
}
Aggregations