Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Privacy Consent Requirement to Add Observers #655

Merged
merged 2 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("josh@onesignal.com");
threadAndTaskWait();

// make sure the email subscription observer was fired
assertNull(lastEmailSubscriptionStateChanges.getFrom().getEmailUserId());
assertEquals("b007f967-98cc-11e4-bed1-118f05be4522", lastEmailSubscriptionStateChanges.getTo().getEmailUserId());
assertEquals("josh@onesignal.com", 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