Skip to content

Commit

Permalink
Merge pull request #655 from OneSignal/observers_consent
Browse files Browse the repository at this point in the history
Remove Privacy Consent Requirement to Add Observers
  • Loading branch information
Nightsd01 authored Oct 26, 2018
2 parents 5bbb8d2 + 5a7e9f9 commit ccbd4e3
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 12 deletions.
12 changes: 0 additions & 12 deletions OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignal.java
Original file line number Diff line number Diff line change
Expand Up @@ -2591,10 +2591,6 @@ public static void removeNotificationReceivedHandler() {
*/
public static void addPermissionObserver(OSPermissionObserver observer) {

//if applicable, check if the user provided privacy consent
if (shouldLogUserPrivacyConsentErrorMessageForMethodName("addPermissionObserver()"))
return;

if (appContext == null) {
Log(LOG_LEVEL.ERROR, "OneSignal.init has not been called. Could not add permission observer");
return;
Expand Down Expand Up @@ -2632,10 +2628,6 @@ public static void removePermissionObserver(OSPermissionObserver observer) {
*/
public static void addSubscriptionObserver(OSSubscriptionObserver observer) {

//if applicable, check if the user provided privacy consent
if (shouldLogUserPrivacyConsentErrorMessageForMethodName("addSubscriptionObserver()"))
return;

if (appContext == null) {
Log(LOG_LEVEL.ERROR, "OneSignal.init has not been called. Could not add subscription observer");
return;
Expand Down Expand Up @@ -2670,10 +2662,6 @@ public static void removeSubscriptionObserver(OSSubscriptionObserver observer) {
*/
public static void addEmailSubscriptionObserver(@NonNull OSEmailSubscriptionObserver observer) {

//if applicable, check if the user provided privacy consent
if (shouldLogUserPrivacyConsentErrorMessageForMethodName("addEmailSubscriptionObserver()"))
return;

if (appContext == null) {
Log(LOG_LEVEL.ERROR, "OneSignal.init has not been called. Could not add email subscription observer");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2352,6 +2352,83 @@ public void shouldReturnCorrectConsentRequiredStatusWhenSetBeforeInit() throws E
assertTrue(OneSignal.userProvidedPrivacyConsent());
}


// Functions to add observers (like addSubscriptionObserver) should continue
// to work even if privacy consent has not been granted.
@Test
public void shouldAddSubscriptionObserverIfConsentNotGranted() throws Exception {
OneSignal.setRequiresUserPrivacyConsent(true);
OneSignalInit();
threadAndTaskWait();

OSSubscriptionObserver subscriptionObserver = new OSSubscriptionObserver() {
@Override
public void onOSSubscriptionChanged(OSSubscriptionStateChanges stateChanges) {
lastSubscriptionStateChanges = stateChanges;
currentSubscription = stateChanges.getTo().getSubscribed();
}
};
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().getSubscribed());
assertFalse(lastSubscriptionStateChanges.getFrom().getSubscribed());
}

@Test
public void shouldAddPermissionObserverIfConsentNotGranted() throws Exception {
OneSignal.setRequiresUserPrivacyConsent(true);
OneSignalInit();
threadAndTaskWait();

OSPermissionObserver permissionObserver = new OSPermissionObserver() {
@Override
public void onOSPermissionChanged(OSPermissionStateChanges stateChanges) {
lastPermissionStateChanges = stateChanges;
currentPermission = stateChanges.getTo().getEnabled();
}
};
OneSignal.addPermissionObserver(permissionObserver);

OneSignal.provideUserConsent(true);
threadAndTaskWait();

// make sure the permission observer was fired
assertFalse(lastPermissionStateChanges.getFrom().getEnabled());
assertTrue(lastPermissionStateChanges.getTo().getEnabled());
}

@Test
public void shouldAddEmailSubscriptionObserverIfConsentNotGranted() throws Exception {
OneSignal.setRequiresUserPrivacyConsent(true);
OneSignalInit();
OSEmailSubscriptionObserver subscriptionObserver = new OSEmailSubscriptionObserver() {
@Override
public void onOSEmailSubscriptionChanged(OSEmailSubscriptionStateChanges stateChanges) {
lastEmailSubscriptionStateChanges = stateChanges;
}
};
OneSignal.addEmailSubscriptionObserver(subscriptionObserver);

OneSignal.provideUserConsent(true);
threadAndTaskWait();

OneSignal.setEmail("[email protected]");
threadAndTaskWait();

// make sure the email subscription observer was fired
assertNull(lastEmailSubscriptionStateChanges.getFrom().getEmailUserId());
assertEquals("b007f967-98cc-11e4-bed1-118f05be4522", lastEmailSubscriptionStateChanges.getTo().getEmailUserId());
assertEquals("[email protected]", lastEmailSubscriptionStateChanges.getTo().getEmailAddress());
assertTrue(lastEmailSubscriptionStateChanges.getTo().getSubscribed());
}

/*
// Can't get test to work from a app flow due to the main thread being locked one way or another in a robolectric env.
// Running ActivityLifecycleListener.focusHandlerThread...advanceToNextPostedRunnable waits on the main thread.
Expand Down

0 comments on commit ccbd4e3

Please sign in to comment.