Skip to content

Commit

Permalink
Unit tests for set email/sms and set EUID within their callback
Browse files Browse the repository at this point in the history
- These tests call setExternalUserId in the onSuccess callback of setEmail or setSMSNumber

- By the time this onSuccess callback is invoked, setting the EUID should update the email player or sms player

- Implicitly tests that the email or sms player is set successfully by the time their onSuccess callback is invoked

- Previously, by the time this callback is invoked, the channel IDs for email or sms has not been set yet, and then calling setExternalUserId does not update those players.
  • Loading branch information
nan-li committed Mar 29, 2022
1 parent 0e4b609 commit 96fde2f
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
import com.onesignal.OSInAppMessageLifecycleHandler;
import com.onesignal.OneSignal;
import com.onesignal.sdktest.R;
import com.onesignal.sdktest.callback.EmailUpdateCallback;
import com.onesignal.sdktest.callback.SMSUpdateCallback;
import com.onesignal.sdktest.constant.Tag;
import com.onesignal.sdktest.constant.Text;
import com.onesignal.sdktest.util.SharedPreferenceUtil;

import org.json.JSONException;
import org.json.JSONObject;

public class MainApplication extends MultiDexApplication {
Expand All @@ -21,27 +24,6 @@ public class MainApplication extends MultiDexApplication {
public void onCreate() {
super.onCreate();

OSInAppMessageLifecycleHandler handler = new OSInAppMessageLifecycleHandler() {
@Override
public void onWillDisplayInAppMessage(OSInAppMessage message) {
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.VERBOSE, "MainApplication onWillDisplayInAppMessage");
}
@Override
public void onDidDisplayInAppMessage(OSInAppMessage message) {
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.VERBOSE, "MainApplication onDidDisplayInAppMessage");
}
@Override
public void onWillDismissInAppMessage(OSInAppMessage message) {
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.VERBOSE, "MainApplication onWillDismissInAppMessage");
}
@Override
public void onDidDismissInAppMessage(OSInAppMessage message) {
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.VERBOSE, "MainApplication onDidDismissInAppMessage");
}
};

OneSignal.setInAppMessageLifecycleHandler(handler);

OneSignal.setLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.NONE);

// OneSignal Initialization
Expand All @@ -54,24 +36,64 @@ public void onDidDismissInAppMessage(OSInAppMessage message) {
OneSignal.setAppId(appId);
OneSignal.initWithContext(this);

OneSignal.setNotificationOpenedHandler(result ->
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.VERBOSE, "OSNotificationOpenedResult result: " + result.toString()));
OneSignal.pauseInAppMessages(true);
OneSignal.setLocationShared(false);

OneSignal.setNotificationWillShowInForegroundHandler(notificationReceivedEvent -> {
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.VERBOSE, "NotificationWillShowInForegroundHandler fired!" +
" with notification event: " + notificationReceivedEvent.toString());
Log.d(Tag.DEBUG, Text.ONESIGNAL_SDK_INIT);

OSNotification notification = notificationReceivedEvent.getNotification();
JSONObject data = notification.getAdditionalData();

notificationReceivedEvent.complete(notification);
// setExternalUserIdInternal("trader_joes", 1);
// setEmailInternal("14@example.com", "euid_14", 2);
OneSignal.setEmail("17@example.com");
setSMSNumberInternal("+11112220002", "euid_17", 3);
}

public void setExternalUserIdInternal(String externalUserId, int order) {
OneSignal.setExternalUserId(externalUserId, new OneSignal.OSExternalUserIdUpdateCompletionHandler() {
@Override
public void onSuccess(JSONObject results) {
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.VERBOSE, order + " 💛💛💛 setExternalUserId set successfully: " + results);
}

@Override
public void onFailure(OneSignal.ExternalIdError error) {
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.VERBOSE, order + " 💛💛💛 setExternalUserId failed with error: " + error);
}
});
}

OneSignal.unsubscribeWhenNotificationsAreDisabled(true);
OneSignal.pauseInAppMessages(true);
OneSignal.setLocationShared(false);
public void setEmailInternal(String email, String externalUserId, int order) {
OneSignal.setEmail(email, new OneSignal.EmailUpdateHandler() {
@Override
public void onSuccess() {
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.VERBOSE, "💛💛💛 setEmail set successfully");
setExternalUserIdInternal(externalUserId, order);
}

Log.d(Tag.DEBUG, Text.ONESIGNAL_SDK_INIT);
@Override
public void onFailure(OneSignal.EmailUpdateError error) {
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.VERBOSE, "💛💛💛 setEmail failed error: " + error);
setExternalUserIdInternal(externalUserId, order);
}
});
}


public void setSMSNumberInternal(String smsNumber, String externalUserId, int order) {
OneSignal.setSMSNumber(smsNumber, new OneSignal.OSSMSUpdateHandler() {
@Override
public void onSuccess(JSONObject result) {
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.VERBOSE, "💛💛💛 setSMSNumber set successfully");
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.VERBOSE, "💛💛💛 " + result);

setExternalUserIdInternal(externalUserId, order);
}

@Override
public void onFailure(OneSignal.OSSMSUpdateError error) {
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.VERBOSE, "💛💛💛 setSMSNumber failed error: " + error);
setExternalUserIdInternal(externalUserId, order);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,11 @@ public void run() {
}

public boolean attemptSignIn(EmailUpdateCallback callback) {
boolean isEmailCached = SharedPreferenceUtil.exists(context, SharedPreferenceUtil.USER_EMAIL_SHARED_PREF);
if (isEmailCached) {
String email = SharedPreferenceUtil.getCachedUserEmail(context);
currentUser.setEmail(email, callback);
}
return isEmailCached;
return false;
}

public boolean attemptExternalUserId() {
boolean isExternalUserIdCached = SharedPreferenceUtil.exists(context, SharedPreferenceUtil.USER_EXTERNAL_USER_ID_SHARED_PREF);
if (isExternalUserIdCached) {
String externalUserId = SharedPreferenceUtil.getCachedUserExternalUserId(context);
currentUser.setExternalUserId(context, externalUserId);
}
return isExternalUserIdCached;
return false;
}

private void attemptEnterApplication() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,26 @@ public void onFailure(OneSignal.EmailUpdateError error) {
}
}

/**
* @see #shouldSetExternalUserId_inOnSuccessOfEmailUpdate
*/
private class TestEmailUpdateHandler_onSuccess_callSetExternalUserId implements OneSignal.EmailUpdateHandler {
boolean emailFiredSuccess = false;
OneSignal.EmailUpdateError emailFiredFailure = null;
String testExternalId = "test_ext_id";

@Override
public void onSuccess() {
emailFiredSuccess = true;
OneSignal.setExternalUserId(testExternalId, getExternalUserIdUpdateCompletionHandler());
}

@Override
public void onFailure(OneSignal.EmailUpdateError error) {
emailFiredFailure = error;
}
}

private class TestSMSUpdateHandler implements OneSignal.OSSMSUpdateHandler {
JSONObject smsResult = null;
OneSignal.OSSMSUpdateError smsFiredFailure = null;
Expand All @@ -559,6 +579,26 @@ public void onFailure(OneSignal.OSSMSUpdateError error) {
}
}

/**
* @see #shouldSetExternalUserId_inOnSuccessOfSMSUpdate
*/
private class TestSMSUpdateHandler_onSuccess_callSetExternalUserId implements OneSignal.OSSMSUpdateHandler {
JSONObject smsResult = null;
OneSignal.OSSMSUpdateError smsFiredFailure = null;
String testExternalId = "test_ext_id";

@Override
public void onSuccess(JSONObject result) {
smsResult = result;
OneSignal.setExternalUserId(testExternalId, getExternalUserIdUpdateCompletionHandler());
}

@Override
public void onFailure(OneSignal.OSSMSUpdateError error) {
smsFiredFailure = error;
}
}

@Test
public void shouldFireOnSuccessOfEmailUpdate() throws Exception {
OneSignalInit();
Expand Down Expand Up @@ -2019,6 +2059,77 @@ public void sendExternalUserId_withFailure_withCompletionHandler() throws Except
assertEquals(expectedExternalUserIdResponse.toString(), lastExternalUserIdResponse.toString());
}

/**
* This test calls setExternalUserId in the onSuccess callback of setEmail.
* By the time this callback is invoked, setting EUID should update the email player.
*/
@Test
public void shouldSetExternalUserId_inOnSuccessOfEmailUpdate() throws Exception {
// 1. Init OneSignal
OneSignalInit();

// 2. Create an EmailUpdateHandler whose onSuccess callback calls setExternalUserId
TestEmailUpdateHandler_onSuccess_callSetExternalUserId testEmailUpdateHandler = new
TestEmailUpdateHandler_onSuccess_callSetExternalUserId();

// 3. Attempt to set email using this email update handler
OneSignal.setEmail(ONESIGNAL_EMAIL_ADDRESS, testEmailUpdateHandler);
threadAndTaskWait();

// 4. Check that the email callback is successful
assertTrue(testEmailUpdateHandler.emailFiredSuccess);
assertNull(testEmailUpdateHandler.emailFiredFailure);

// 5. Make sure lastExternalUserIdResponse has push and email with success : true
// The order of the JSONObject is email first then push
JSONObject expectedExternalUserIdResponse = new JSONObject(
"{" +
" \"email\" : {" +
" \"success\" : true" +
" }" + ", " +
" \"push\" : {" +
" \"success\" : true" +
" }" +
"}"
);
assertEquals(expectedExternalUserIdResponse.toString(), lastExternalUserIdResponse.toString());
}

/**
* This test calls setExternalUserId in the onSuccess callback of setSMSNumber.
* By the time this callback is invoked, setting EUID should update the SMS player.
*/
@Test
public void shouldSetExternalUserId_inOnSuccessOfSMSUpdate() throws Exception {
// 1. Init OneSignal
OneSignalInit();

// 2. Create an SMSUpdateHandler whose onSuccess callback calls setExternalUserId
TestSMSUpdateHandler_onSuccess_callSetExternalUserId testSMSUpdateHandler = new
TestSMSUpdateHandler_onSuccess_callSetExternalUserId();

// 3. Attempt to set SMS using this SMS update handler
OneSignal.setSMSNumber(ONESIGNAL_SMS_NUMBER, testSMSUpdateHandler);
threadAndTaskWait();

// 4. Check that the sms callback is invoked
assertNotNull(testSMSUpdateHandler.smsResult);

// 5. Make sure lastExternalUserIdResponse has push and sms with success : true
// The order of the JSONObject is sms first then push
JSONObject expectedExternalUserIdResponse = new JSONObject(
"{" +
" \"sms\" : {" +
" \"success\" : true" +
" }" + ", " +
" \"push\" : {" +
" \"success\" : true" +
" }" +
"}"
);
assertEquals(expectedExternalUserIdResponse.toString(), lastExternalUserIdResponse.toString());
}

@Test
public void sendExternalUserId_forPushAndEmail_withFailure_withCompletionHandler() throws Exception {
String testEmail = "test@onesignal.com";
Expand Down

0 comments on commit 96fde2f

Please sign in to comment.