diff --git a/.circleci/config.yml b/.circleci/config.yml index 788fb47..466f77f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -27,3 +27,9 @@ jobs: destination: reports - store_test_results: path: app/build/test-results + +workflows: + version: 2 + build: + jobs: + - build diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fb3bdd..34d60b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.15.0 (2020-04-28) + +- Add support for event based sampling + ## 2.14.2 (2020-02-28) - Fix bug when thank_you_link_url_settings is not present diff --git a/README.md b/README.md index a6fa372..e07afb0 100644 --- a/README.md +++ b/README.md @@ -28,14 +28,14 @@ If you use Maven, you can include this library as a dependency: com.wootric wootric-sdk-android - 2.14.2 + 2.15.0 ``` ### Using Gradle ```xml -compile 'com.wootric:wootric-sdk-android:2.14.2' +compile 'com.wootric:wootric-sdk-android:2.15.0' ``` ## Initializing Wootric diff --git a/androidsdk/build.gradle b/androidsdk/build.gradle index cf52094..4498898 100644 --- a/androidsdk/build.gradle +++ b/androidsdk/build.gradle @@ -52,7 +52,10 @@ dependencies { testImplementation 'org.robolectric:robolectric:4.1' testImplementation 'org.json:json:20190722' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' - implementation 'androidx.fragment:fragment:1.2.2' + implementation 'androidx.fragment:fragment:1.2.4' + implementation 'androidx.lifecycle:lifecycle-runtime:2.2.0' + implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' + annotationProcessor 'androidx.lifecycle:lifecycle-compiler:2.2.0' } apply from: 'maven_push.gradle' \ No newline at end of file diff --git a/androidsdk/gradle.properties b/androidsdk/gradle.properties index 6e7b265..70d88de 100644 --- a/androidsdk/gradle.properties +++ b/androidsdk/gradle.properties @@ -1,5 +1,5 @@ -VERSION_NAME=2.14.2 -VERSION_CODE=2142 +VERSION_NAME=2.15.0 +VERSION_CODE=2150 GROUP=com.wootric POM_DESCRIPTION=WootricSDK Android diff --git a/androidsdk/src/main/java/com/wootric/androidsdk/SurveyManager.java b/androidsdk/src/main/java/com/wootric/androidsdk/SurveyManager.java index a80be93..9370b93 100644 --- a/androidsdk/src/main/java/com/wootric/androidsdk/SurveyManager.java +++ b/androidsdk/src/main/java/com/wootric/androidsdk/SurveyManager.java @@ -22,13 +22,20 @@ package com.wootric.androidsdk; +import android.annotation.SuppressLint; import android.app.Activity; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; +import android.os.AsyncTask; import android.os.Handler; import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; +import androidx.lifecycle.Lifecycle; +import androidx.lifecycle.LifecycleObserver; +import androidx.lifecycle.OnLifecycleEvent; +import androidx.lifecycle.ProcessLifecycleOwner; + import android.util.Log; import com.wootric.androidsdk.network.WootricApiCallback; @@ -36,64 +43,129 @@ import com.wootric.androidsdk.objects.EndUser; import com.wootric.androidsdk.objects.Settings; import com.wootric.androidsdk.objects.User; +import com.wootric.androidsdk.objects.WootricEvent; import com.wootric.androidsdk.utils.PreferencesUtils; +import com.wootric.androidsdk.utils.Utils; +import com.wootric.androidsdk.views.OnSurveyFinishedListener; import com.wootric.androidsdk.views.support.SurveyFragment; +import java.util.ArrayList; +import java.util.concurrent.ConcurrentLinkedQueue; + /** * Created by maciejwitowski on 9/3/15. */ -public class SurveyManager implements SurveyValidator.OnSurveyValidatedListener, WootricApiCallback { +public class SurveyManager implements SurveyValidator.OnSurveyValidatedListener, WootricApiCallback, LifecycleObserver, OnSurveyFinishedListener { + private OnSurveyFinishedListener onSurveyFinishedListener = this; private Activity activity; private FragmentActivity fragmentActivity; - private WootricSurveyCallback surveyCallback; - private final WootricRemoteClient wootricApiClient; - private final User user; - private final EndUser endUser; - private final SurveyValidator surveyValidator; - private final Settings settings; - private final PreferencesUtils preferencesUtils; + private SurveyValidator surveyValidator; + private ArrayList registeredEvents = new ArrayList<>(); + private ConcurrentLinkedQueue eventQueue = new ConcurrentLinkedQueue(); + private boolean surveyRunning = false; + + private WootricEvent currentEvent; private String accessToken; private String originUrl; private static final String SURVEY_DIALOG_TAG = "survey_dialog_tag"; - SurveyManager(FragmentActivity fragmentActivity, WootricRemoteClient wootricApiClient, User user, EndUser endUser, - Settings settings, PreferencesUtils preferencesUtils, - SurveyValidator surveyValidator, WootricSurveyCallback surveyCallback) { - this.fragmentActivity = fragmentActivity; - this.wootricApiClient = wootricApiClient; - this.user = user; - this.endUser = endUser; - this.surveyValidator = surveyValidator; - this.settings = settings; - this.preferencesUtils = preferencesUtils; - this.surveyCallback = surveyCallback; + static volatile SurveyManager sharedInstance; + + private SurveyManager() { + if (sharedInstance != null){ + throw new RuntimeException("Use getSharedInstance() method to get the single instance of this class."); + } + } + + public static SurveyManager getSharedInstance() { + if (sharedInstance == null){ + synchronized (SurveyManager.class) { + if (sharedInstance == null) sharedInstance = new SurveyManager(); + } + } + return sharedInstance; } - SurveyManager(Activity activity, WootricRemoteClient wootricApiClient, User user, EndUser endUser, - Settings settings, PreferencesUtils preferencesUtils, - SurveyValidator surveyValidator, WootricSurveyCallback surveyCallback) { - this.activity = activity; - this.wootricApiClient = wootricApiClient; - this.user = user; - this.endUser = endUser; - this.surveyValidator = surveyValidator; - this.settings = settings; - this.preferencesUtils = preferencesUtils; - this.surveyCallback = surveyCallback; + synchronized void start(Activity activity, WootricRemoteClient wootricApiClient, User user, EndUser endUser, + Settings settings, PreferencesUtils preferencesUtils, WootricSurveyCallback surveyCallback, SurveyValidator sv) { + ProcessLifecycleOwner.get().getLifecycle().addObserver(this); + Activity activity1 = activity; + WootricRemoteClient wootricApiClient1 = wootricApiClient; + User user1 = new User(user); + EndUser endUser1 = new EndUser(endUser); + Settings settings1 = new Settings(settings); + PreferencesUtils preferencesUtils1 = preferencesUtils; + WootricSurveyCallback surveyCallback1 = surveyCallback; + sv.buildSurveyValidator(user1, endUser1, settings1, wootricApiClient1, preferencesUtils1); + + WootricEvent event = new WootricEvent(activity1, wootricApiClient1, + user1, endUser1, settings1, preferencesUtils1, surveyCallback1, sv); + + preferencesUtils.touchLastSeen(); + + eventQueue.add(event); + + runSurvey(); } - void start() { + synchronized void start(FragmentActivity fragmentActivity, WootricRemoteClient wootricApiClient, User user, EndUser endUser, + Settings settings, PreferencesUtils preferencesUtils, WootricSurveyCallback surveyCallback, SurveyValidator sv) { + ProcessLifecycleOwner.get().getLifecycle().addObserver(this); + + FragmentActivity fragmentActivity1 = fragmentActivity; + WootricRemoteClient wootricApiClient1 = wootricApiClient; + User user1 = new User(user); + EndUser endUser1 = new EndUser(endUser); + Settings settings1 = new Settings(settings); + PreferencesUtils preferencesUtils1 = preferencesUtils; + WootricSurveyCallback surveyCallback1 = surveyCallback; + sv.buildSurveyValidator(user1, endUser1, settings1, wootricApiClient1, preferencesUtils1); + + WootricEvent event = new WootricEvent(fragmentActivity1, wootricApiClient1, + user1, endUser1, settings1, preferencesUtils1, surveyCallback1, sv); + + eventQueue.add(event); + preferencesUtils.touchLastSeen(); - validateSurvey(); + runSurvey(); + } + + private void runSurvey() { + synchronized (this) { + if (!surveyRunning && !eventQueue.isEmpty()) { + surveyRunning = true; + currentEvent = (WootricEvent) eventQueue.poll(); + if (currentEvent != null) { + surveyValidator = currentEvent.getSurveyValidator(); + surveyValidator.setOnSurveyValidatedListener(this); + if (Utils.isBlank(currentEvent.getSettings().getEventName())) { + surveyValidator.validate(); + } else { + if (registeredEvents.isEmpty()) { + surveyValidator.getRegisteredEvents(); + } else { + if (currentEvent.getSettings().isSurveyImmediately() || registeredEvents.contains(currentEvent.getSettings().getEventName())) { + surveyValidator.validate(); + } else { + Log.e(Constants.TAG, "Event name not registered: " + currentEvent.getSettings().getEventName()); + this.surveyRunning = false; + runSurvey(); + } + } + } + } + } + } } @Override public void onSurveyValidated(Settings surveyServerSettings) { - settings.mergeWithSurveyServerSettings(surveyServerSettings); + currentEvent.getSettings().mergeWithSurveyServerSettings(surveyServerSettings); + this.eventQueue.clear(); sendGetAccessTokenRequest(); } @@ -101,12 +173,36 @@ public void onSurveyValidated(Settings surveyServerSettings) { @Override public void onSurveyNotNeeded() { Wootric.notifySurveyFinished(false, false, 0); + this.surveyRunning = false; + runSurvey(); + } + + @Override + public void onRegisteredEvents(ArrayList registeredEvents) { + this.registeredEvents = registeredEvents; + if (currentEvent == null) { + this.surveyRunning = false; + runSurvey(); + } else { + if (!currentEvent.getSettings().getEventName().isEmpty()) { + if (currentEvent.getSettings().isSurveyImmediately() || registeredEvents.contains(currentEvent.getSettings().getEventName())){ + surveyValidator.validate(); + } else { + Log.e(Constants.TAG, "Event name not registered: " + currentEvent.getSettings().getEventName()); + this.surveyRunning = false; + runSurvey(); + } + } else { + surveyValidator.validate(); + } + } } @Override public void onAuthenticateSuccess(String accessToken) { if(accessToken == null) { Wootric.notifySurveyFinished(false, false, 0); + this.surveyRunning = false; return; } @@ -116,16 +212,16 @@ public void onAuthenticateSuccess(String accessToken) { } private void sendOfflineData() { - wootricApiClient.processOfflineData(accessToken); + currentEvent.getWootricApiClient().processOfflineData(accessToken); } @Override public void onGetEndUserIdSuccess(long endUserId) { - endUser.setId(endUserId); + currentEvent.getEndUser().setId(endUserId); - if(this.endUser.hasProperties() || - this.endUser.hasExternalId() || - this.endUser.hasPhoneNumber()) { + if(currentEvent.getEndUser().hasProperties() || + currentEvent.getEndUser().hasExternalId() || + currentEvent.getEndUser().hasPhoneNumber()) { sendUpdateEndUserRequest(); } @@ -139,7 +235,7 @@ public void onEndUserNotFound() { @Override public void onCreateEndUserSuccess(long endUserId) { - this.endUser.setId(endUserId); + currentEvent.getEndUser().setId(endUserId); showSurvey(); } @@ -148,32 +244,28 @@ public void onCreateEndUserSuccess(long endUserId) { public void onApiError(Exception error) { Log.e(Constants.TAG, "API error: " + error); Wootric.notifySurveyFinished(false, false, 0); - } - - private void validateSurvey() { - surveyValidator.setOnSurveyValidatedListener(this); - surveyValidator.validate(); + this.surveyRunning = false; } private void sendGetAccessTokenRequest() { - wootricApiClient.authenticate(user, this); + currentEvent.getWootricApiClient().authenticate(currentEvent.getUser(), this); } private void sendGetEndUserRequest() { - wootricApiClient.getEndUserByEmail(endUser.getEmailOrUnknown(), accessToken, this); + currentEvent.getWootricApiClient().getEndUserByEmail(currentEvent.getEndUser().getEmailOrUnknown(), accessToken, this); } private void sendCreateEndUserRequest() { - wootricApiClient.createEndUser(endUser, accessToken, this); + currentEvent.getWootricApiClient().createEndUser(currentEvent.getEndUser(), accessToken, this); } private void sendUpdateEndUserRequest() { - wootricApiClient.updateEndUser(endUser, accessToken, this); + currentEvent.getWootricApiClient().updateEndUser(currentEvent.getEndUser(), accessToken, this); } void showSurvey() { - if (surveyCallback != null) { - surveyCallback.onSurveyWillShow(); + if (currentEvent.getSurveyCallback() != null) { + currentEvent.getSurveyCallback().onSurveyWillShow(); } new Handler().postDelayed(new Runnable() { @Override @@ -185,20 +277,23 @@ public void run() { Wootric.notifySurveyFinished(false, false, 0); } } - }, settings.getTimeDelayInMillis()); + }, currentEvent.getSettings().getTimeDelayInMillis()); } + @SuppressLint("ResourceType") private void showSurveyFragment() { + this.activity = currentEvent.getActivity(); try { if (fragmentActivity != null) { final FragmentManager fragmentActivityManager = fragmentActivity.getSupportFragmentManager(); - SurveyFragment surveySupportFragment = SurveyFragment.newInstance(endUser, getOriginUrl(), - accessToken, settings, user); + SurveyFragment surveySupportFragment = SurveyFragment.newInstance(currentEvent.getEndUser(), getOriginUrl(), + accessToken, currentEvent.getSettings(), currentEvent.getUser()); + surveySupportFragment.setOnSurveyFinishedListener(onSurveyFinishedListener); final boolean isTablet = fragmentActivity.getResources().getBoolean(R.bool.isTablet); - surveySupportFragment.setSurveyCallback(surveyCallback); + surveySupportFragment.setSurveyCallback(currentEvent.getSurveyCallback()); if(isTablet) { fragmentActivityManager.beginTransaction() .add(android.R.id.content, surveySupportFragment, SURVEY_DIALOG_TAG) @@ -210,13 +305,14 @@ private void showSurveyFragment() { } else { final android.app.FragmentManager fragmentManager = activity.getFragmentManager(); - com.wootric.androidsdk.views.SurveyFragment surveyFragment = com.wootric.androidsdk.views.SurveyFragment.newInstance(endUser, getOriginUrl(), - accessToken, settings, user); + com.wootric.androidsdk.views.SurveyFragment surveyFragment = com.wootric.androidsdk.views.SurveyFragment.newInstance(currentEvent.getEndUser(), getOriginUrl(), + accessToken, currentEvent.getSettings(), currentEvent.getUser()); + surveyFragment.setOnSurveyFinishedListener(onSurveyFinishedListener); final boolean isTablet = activity.getResources().getBoolean(R.bool.isTablet); - surveyFragment.setSurveyCallback(surveyCallback); + surveyFragment.setSurveyCallback(currentEvent.getSurveyCallback()); if(isTablet) { fragmentManager.beginTransaction() .add(android.R.id.content, surveyFragment, SURVEY_DIALOG_TAG) @@ -225,8 +321,8 @@ private void showSurveyFragment() { } else { surveyFragment.show(fragmentManager, SURVEY_DIALOG_TAG); } - if (surveyCallback != null){ - surveyCallback.onSurveyDidShow(); + if (currentEvent.getSurveyCallback() != null){ + currentEvent.getSurveyCallback().onSurveyDidShow(); } } } catch (NullPointerException e) { @@ -267,4 +363,18 @@ private String getOriginUrl() { return originUrl; } + + public WootricEvent getCurrentEvent() { return this.currentEvent; } + + public int eventQueueCount() { return this.eventQueue.size(); } + + @OnLifecycleEvent(Lifecycle.Event.ON_STOP) + public void onBackground() { + registeredEvents.clear(); + } + + @Override + public void onSurveyFinished() { + this.surveyRunning = false; + } } diff --git a/androidsdk/src/main/java/com/wootric/androidsdk/SurveyValidator.java b/androidsdk/src/main/java/com/wootric/androidsdk/SurveyValidator.java index 1e6b226..65fb137 100644 --- a/androidsdk/src/main/java/com/wootric/androidsdk/SurveyValidator.java +++ b/androidsdk/src/main/java/com/wootric/androidsdk/SurveyValidator.java @@ -26,23 +26,29 @@ import com.wootric.androidsdk.network.WootricRemoteClient; import com.wootric.androidsdk.network.responses.EligibilityResponse; +import com.wootric.androidsdk.network.responses.RegisteredEventsResponse; import com.wootric.androidsdk.network.tasks.CheckEligibilityTask; +import com.wootric.androidsdk.network.tasks.GetRegisteredEventsTask; import com.wootric.androidsdk.objects.EndUser; import com.wootric.androidsdk.objects.Settings; import com.wootric.androidsdk.objects.User; import com.wootric.androidsdk.utils.PreferencesUtils; +import java.util.ArrayList; + /** * Created by maciejwitowski on 9/3/15. */ -public class SurveyValidator implements CheckEligibilityTask.Callback { +public class SurveyValidator implements CheckEligibilityTask.Callback, GetRegisteredEventsTask.Callback { private OnSurveyValidatedListener onSurveyValidatedListener; - private final User user; - private final EndUser endUser; - private final Settings settings; - private final WootricRemoteClient wootricRemoteClient; - private final PreferencesUtils preferencesUtils; + private User user; + private EndUser endUser; + private Settings settings; + private WootricRemoteClient wootricRemoteClient; + private PreferencesUtils preferencesUtils; + + public SurveyValidator() {} public SurveyValidator(User user, EndUser endUser, Settings settings, WootricRemoteClient wootricRemoteClient, PreferencesUtils preferencesUtils) { @@ -53,12 +59,20 @@ public SurveyValidator(User user, EndUser endUser, Settings settings, this.preferencesUtils = preferencesUtils; } + public void buildSurveyValidator(User user, EndUser endUser, Settings settings, + WootricRemoteClient wootricRemoteClient, PreferencesUtils preferencesUtils) { + this.user = user; + this.endUser = endUser; + this.settings = settings; + this.wootricRemoteClient = wootricRemoteClient; + this.preferencesUtils = preferencesUtils; + } + public void setOnSurveyValidatedListener(OnSurveyValidatedListener onSurveyValidatedListener) { this.onSurveyValidatedListener = onSurveyValidatedListener; } public void validate() { - Boolean immediate = settings.isSurveyImmediately(); Boolean recently = preferencesUtils.wasRecentlySurveyed(); Boolean firstDelay = firstSurveyDelayPassed(); @@ -69,7 +83,16 @@ public void validate() { Log.d(Constants.TAG, "FIRST SURVEY DELAY PASSED: " + firstDelay); Log.d(Constants.TAG, "LAST SEEN DELAY PASSED: " + lastSeen); - if (immediate || !recently || firstDelay || lastSeen) { + if (immediate) { + Log.d(Constants.TAG, "Needs survey. Will check with server."); + checkEligibility(); + } else if (recently) { + Log.d(Constants.TAG, "Doesn't need survey. Recently surveyed."); + notifyShouldNotShowSurvey(); + } else if (firstDelay) { + Log.d(Constants.TAG, "Needs survey. Will check with server."); + checkEligibility(); + } else if (lastSeen) { Log.d(Constants.TAG, "Needs survey. Will check with server."); checkEligibility(); } else { @@ -80,7 +103,6 @@ public void validate() { @Override public void onEligibilityChecked(EligibilityResponse eligibilityResponse) { - if(eligibilityResponse.isEligible()) { notifyShouldShowSurvey(eligibilityResponse.getSettings()); } else { @@ -88,6 +110,11 @@ public void onEligibilityChecked(EligibilityResponse eligibilityResponse) { } } + @Override + public void onRegisteredEventsList(RegisteredEventsResponse registeredEventsResponse) { + notifyRegisteredEventsRetrieved(registeredEventsResponse.getRegisteredEvents()); + } + private boolean firstSurveyDelayPassed() { return settings.firstSurveyDelayPassed(endUser.getCreatedAt()); } @@ -100,6 +127,8 @@ private void checkEligibility() { wootricRemoteClient.checkEligibility(user, endUser, settings, preferencesUtils, this); } + public void getRegisteredEvents() { wootricRemoteClient.getRegisteredEvents(user, this); } + private void notifyShouldShowSurvey(Settings settings) { if(onSurveyValidatedListener != null) { onSurveyValidatedListener.onSurveyValidated(settings); @@ -112,8 +141,15 @@ private void notifyShouldNotShowSurvey() { } } + private void notifyRegisteredEventsRetrieved(ArrayList registeredEvents){ + if(onSurveyValidatedListener != null) { + onSurveyValidatedListener.onRegisteredEvents(registeredEvents); + } + } + interface OnSurveyValidatedListener { void onSurveyValidated(Settings settings); void onSurveyNotNeeded(); + void onRegisteredEvents(ArrayList registeredEvents); } } diff --git a/androidsdk/src/main/java/com/wootric/androidsdk/Wootric.java b/androidsdk/src/main/java/com/wootric/androidsdk/Wootric.java index 3691600..44eb2cb 100644 --- a/androidsdk/src/main/java/com/wootric/androidsdk/Wootric.java +++ b/androidsdk/src/main/java/com/wootric/androidsdk/Wootric.java @@ -55,7 +55,6 @@ public class Wootric { final User user; final Settings settings; - boolean surveyInProgress; PreferencesUtils preferencesUtils; PermissionsValidator permissionsValidator; @@ -154,6 +153,15 @@ public Wootric get() { public void setSurveyCallback(WootricSurveyCallback surveyCallback){ this.surveyCallback = surveyCallback; } + /** + * Set event. + * + * @param eventName String of the event name. + */ + public void setEventName(String eventName) { + settings.setEventName(eventName); + } + /** * End user email is optional. If not provided, the end user will be considered as "Unknown". * @@ -426,45 +434,58 @@ public void setSurveyTypeScale(int surveyTypeScale) { } /** - * Starts the survey if configuration is correctly set and elibility returns true. + * Starts the survey with an event name if configuration is correctly set and elibility returns true. */ - public void survey() { - if (!permissionsValidator.check() || surveyInProgress) { + public void survey(String eventName) { + this.setEventName(eventName); + if (!permissionsValidator.check()) { return; } - getSurveyManager().start(); - - surveyInProgress = true; - } + OfflineDataHandler offlineDataHandler = new OfflineDataHandler(preferencesUtils); + WootricRemoteClient wootricRemoteClient = new WootricRemoteClient(offlineDataHandler); + SurveyValidator surveyValidator = buildSurveyValidator(); - public void showSurveyInActivity(Activity activity) { - if (!permissionsValidator.check() || surveyInProgress) { - return; + if (weakFragmentActivity != null) { + buildSurveyManager().start(weakFragmentActivity.get(), wootricRemoteClient, + user, endUser, settings, preferencesUtils, surveyCallback, surveyValidator); + } else { + buildSurveyManager().start(weakActivity.get(), wootricRemoteClient, + user, endUser, settings, preferencesUtils, surveyCallback, surveyValidator); } + } - getSurveyManagerForActivity(activity).start(); - - surveyInProgress = true; + /** + * Starts the survey if configuration is correctly set and elibility returns true. + */ + public void survey() { + this.survey(""); } + public void showSurveyInActivity(Activity activity, String eventName) { + this.setEventName(eventName); + if (!permissionsValidator.check()) { + return; + } - private SurveyManager getSurveyManager() { OfflineDataHandler offlineDataHandler = new OfflineDataHandler(preferencesUtils); WootricRemoteClient wootricRemoteClient = new WootricRemoteClient(offlineDataHandler); - - SurveyValidator surveyValidator = buildSurveyValidator(user, endUser, settings, - wootricRemoteClient, preferencesUtils); + SurveyValidator surveyValidator = buildSurveyValidator(); if (weakFragmentActivity != null) { - return buildSurveyManager(weakFragmentActivity.get(), wootricRemoteClient, - user, endUser, settings, preferencesUtils, surveyValidator, surveyCallback); + getSurveyManagerForActivity(activity).start(weakFragmentActivity.get(), wootricRemoteClient, + user, endUser, settings, preferencesUtils, surveyCallback, surveyValidator); } else { - return buildSurveyManager(weakActivity.get(), wootricRemoteClient, - user, endUser, settings, preferencesUtils, surveyValidator, surveyCallback); + getSurveyManagerForActivity(activity).start(weakActivity.get(), wootricRemoteClient, + user, endUser, settings, preferencesUtils, surveyCallback, surveyValidator); } } + + public void showSurveyInActivity(Activity activity) { + this.showSurveyInActivity(activity, ""); + } + private SurveyManager getSurveyManagerForActivity(Activity activity) { if (activity instanceof FragmentActivity) { weakFragmentActivity = new WeakReference<>((FragmentActivity) activity); @@ -477,19 +498,7 @@ private SurveyManager getSurveyManagerForActivity(Activity activity) { preferencesUtils = new PreferencesUtils(weakContext); permissionsValidator = new PermissionsValidator(weakContext); - OfflineDataHandler offlineDataHandler = new OfflineDataHandler(preferencesUtils); - WootricRemoteClient wootricRemoteClient = new WootricRemoteClient(offlineDataHandler); - - SurveyValidator surveyValidator = buildSurveyValidator(user, endUser, settings, - wootricRemoteClient, preferencesUtils); - - if (activity instanceof FragmentActivity) { - return buildSurveyManager(weakFragmentActivity.get(), wootricRemoteClient, - user, endUser, settings, preferencesUtils, surveyValidator, surveyCallback); - } else { - return buildSurveyManager(weakActivity.get(), wootricRemoteClient, - user, endUser, settings, preferencesUtils, surveyValidator, surveyCallback); - } + return buildSurveyManager(); } private Wootric(FragmentActivity fragmentActivity, String clientId, String clientSecret, String accountToken) { @@ -516,22 +525,11 @@ private Wootric(Activity activity, String clientId, String accountToken) { permissionsValidator = new PermissionsValidator(weakContext); } - SurveyValidator buildSurveyValidator(User user, EndUser endUser, Settings settings, - WootricRemoteClient wootricRemoteClient, PreferencesUtils preferencesUtils) { - return new SurveyValidator(user, endUser, settings, wootricRemoteClient, preferencesUtils); - } - - SurveyManager buildSurveyManager(FragmentActivity fragmentActivity, WootricRemoteClient wootricApiClient, User user, - EndUser endUser, Settings settings, PreferencesUtils preferencesUtils, - SurveyValidator surveyValidator, WootricSurveyCallback surveyCallback) { - return new SurveyManager(fragmentActivity, wootricApiClient, user, endUser, - settings, preferencesUtils, surveyValidator, surveyCallback); + SurveyValidator buildSurveyValidator() { + return new SurveyValidator(); } - SurveyManager buildSurveyManager(Activity activity, WootricRemoteClient wootricApiClient, User user, - EndUser endUser, Settings settings, PreferencesUtils preferencesUtils, - SurveyValidator surveyValidator, WootricSurveyCallback surveyCallback) { - return new SurveyManager(activity, wootricApiClient, user, endUser, - settings, preferencesUtils, surveyValidator, surveyCallback); + SurveyManager buildSurveyManager() { + return SurveyManager.getSharedInstance(); } } diff --git a/androidsdk/src/main/java/com/wootric/androidsdk/network/WootricRemoteClient.java b/androidsdk/src/main/java/com/wootric/androidsdk/network/WootricRemoteClient.java index 3095409..ed675f4 100644 --- a/androidsdk/src/main/java/com/wootric/androidsdk/network/WootricRemoteClient.java +++ b/androidsdk/src/main/java/com/wootric/androidsdk/network/WootricRemoteClient.java @@ -29,6 +29,7 @@ import com.wootric.androidsdk.network.tasks.CreateResponseTask; import com.wootric.androidsdk.network.tasks.GetAccessTokenTask; import com.wootric.androidsdk.network.tasks.GetEndUserByEmailTask; +import com.wootric.androidsdk.network.tasks.GetRegisteredEventsTask; import com.wootric.androidsdk.network.tasks.UpdateEndUserTask; import com.wootric.androidsdk.objects.EndUser; import com.wootric.androidsdk.objects.Settings; @@ -39,83 +40,42 @@ * Created by maciejwitowski on 9/11/15. */ public class WootricRemoteClient { - private final OfflineDataHandler offlineDataHandler; public WootricRemoteClient(OfflineDataHandler offlineDataHandler) { this.offlineDataHandler = offlineDataHandler; } - public void - checkEligibility(User user, EndUser endUser, Settings settings, PreferencesUtils preferencesUtils, final CheckEligibilityTask.Callback surveyCallback) { - - new CheckEligibilityTask( - user, - endUser, - settings, - preferencesUtils, - surveyCallback - ).execute(); + public void checkEligibility(User user, EndUser endUser, Settings settings, PreferencesUtils preferencesUtils, final CheckEligibilityTask.Callback surveyCallback) { + new CheckEligibilityTask(user, endUser, settings, preferencesUtils, surveyCallback).execute(); } public void authenticate(User user, final WootricApiCallback wootricApiCallback) { - new GetAccessTokenTask( - user.getClientId(), - user.getClientSecret(), - wootricApiCallback - ).execute(); + new GetAccessTokenTask(user.getClientId(), user.getClientSecret(), wootricApiCallback).execute(); } public void getEndUserByEmail(String email, String accessToken, final WootricApiCallback wootricApiCallback) { - new GetEndUserByEmailTask( - email, - accessToken, - wootricApiCallback - ).execute(); + new GetEndUserByEmailTask(email, accessToken, wootricApiCallback).execute(); } public void createEndUser(EndUser endUser, String accessToken, final WootricApiCallback wootricApiCallback) { - new CreateEndUserTask( - endUser, - accessToken, - wootricApiCallback - ).execute(); + new CreateEndUserTask(endUser, accessToken, wootricApiCallback).execute(); } public void updateEndUser(EndUser endUser, String accessToken, final WootricApiCallback wootricApiCallback) { - new UpdateEndUserTask( - endUser, - accessToken, - wootricApiCallback - ).execute(); + new UpdateEndUserTask(endUser, accessToken, wootricApiCallback).execute(); } public void createDecline(long endUserId, long userId, long accountId, int priority, String accessToken, String originUrl, String uniqueLink) { - new CreateDeclineTask( - endUserId, - userId, - accountId, - priority, - originUrl, - accessToken, - offlineDataHandler, - uniqueLink - ).execute(); + new CreateDeclineTask(endUserId, userId, accountId, priority, originUrl, accessToken, offlineDataHandler, uniqueLink).execute(); } public void createResponse(long endUserId, long userId, long accountId, String accessToken, String originUrl, int score, int priority, String text, String uniqueLink) { - new CreateResponseTask( - endUserId, - userId, - accountId, - originUrl, - score, - priority, - text, - accessToken, - offlineDataHandler, - uniqueLink - ).execute(); + new CreateResponseTask(endUserId, userId, accountId, originUrl, score, priority, text, accessToken, offlineDataHandler, uniqueLink).execute(); + } + + public void getRegisteredEvents(User user, final GetRegisteredEventsTask.Callback surveyCallback) { + new GetRegisteredEventsTask(user, surveyCallback).execute(); } public void processOfflineData(String accessToken) { diff --git a/androidsdk/src/main/java/com/wootric/androidsdk/network/responses/RegisteredEventsResponse.java b/androidsdk/src/main/java/com/wootric/androidsdk/network/responses/RegisteredEventsResponse.java new file mode 100644 index 0000000..9047071 --- /dev/null +++ b/androidsdk/src/main/java/com/wootric/androidsdk/network/responses/RegisteredEventsResponse.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2016 Wootric (https://wootric.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.wootric.androidsdk.network.responses; + +import java.util.ArrayList; + +public class RegisteredEventsResponse { + private final ArrayList registeredEvents; + + public RegisteredEventsResponse(ArrayList registeredEvents) { + this.registeredEvents = registeredEvents; + } + + public ArrayList getRegisteredEvents() { + return registeredEvents; + } +} diff --git a/androidsdk/src/main/java/com/wootric/androidsdk/network/tasks/CheckEligibilityTask.java b/androidsdk/src/main/java/com/wootric/androidsdk/network/tasks/CheckEligibilityTask.java index 99e71ed..334faaa 100644 --- a/androidsdk/src/main/java/com/wootric/androidsdk/network/tasks/CheckEligibilityTask.java +++ b/androidsdk/src/main/java/com/wootric/androidsdk/network/tasks/CheckEligibilityTask.java @@ -30,6 +30,7 @@ import com.wootric.androidsdk.objects.Settings; import com.wootric.androidsdk.objects.User; import com.wootric.androidsdk.utils.PreferencesUtils; +import com.wootric.androidsdk.utils.Utils; import org.json.JSONException; import org.json.JSONObject; @@ -63,6 +64,7 @@ protected void buildParams() { paramsMap.put("account_token", user.getAccountToken()); String email = endUser.getEmail(); + String eventName = settings.getEventName(); // If email is not set, we send an empty string in order to be consistent with web beacon if(email == null) { email = ""; @@ -78,6 +80,10 @@ protected void buildParams() { } } + if (Utils.isNotEmpty(eventName)) { + paramsMap.put("event_name", eventName); + } + addOptionalParam("end_user_created_at", endUser.getCreatedAtOrNull()); addOptionalParam("external_id", endUser.getExternalId()); addOptionalParam("phone_number", endUser.getPhoneNumber()); @@ -102,7 +108,8 @@ protected String requestUrl() { @Override protected void onSuccess(String response) { boolean eligible = false; - Settings settings = null; + String eventName = this.settings.getEventName(); + Settings serverSettings = null; if(response != null) { try { @@ -112,6 +119,10 @@ protected void onSuccess(String response) { endUser.getProperties().put("Wootric sampling rule", jsonObject.getJSONObject("sampling_rule").getString("name")); } + if (Utils.isNotEmpty(eventName)) { + endUser.getProperties().put("custom_event_name", eventName); + } + String code = jsonObject.getJSONObject("details").getString("code"); String description = jsonObject.getJSONObject("details").getString("why"); @@ -120,7 +131,7 @@ protected void onSuccess(String response) { Log.d(Constants.TAG, "Server says the user is eligible for survey"); JSONObject settingsObject = jsonObject.getJSONObject("settings"); - settings = Settings.fromJson(settingsObject); + serverSettings = Settings.fromJson(settingsObject); } else { Log.d(Constants.TAG, "Server says the user is NOT eligible for survey"); @@ -131,7 +142,7 @@ protected void onSuccess(String response) { } } - EligibilityResponse eligibilityResponse = new EligibilityResponse(eligible, settings); + EligibilityResponse eligibilityResponse = new EligibilityResponse(eligible, serverSettings); surveyCallback.onEligibilityChecked(eligibilityResponse); } diff --git a/androidsdk/src/main/java/com/wootric/androidsdk/network/tasks/GetRegisteredEventsTask.java b/androidsdk/src/main/java/com/wootric/androidsdk/network/tasks/GetRegisteredEventsTask.java new file mode 100644 index 0000000..8cbb4cd --- /dev/null +++ b/androidsdk/src/main/java/com/wootric/androidsdk/network/tasks/GetRegisteredEventsTask.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2016 Wootric (https://wootric.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.wootric.androidsdk.network.tasks; + +import android.util.Log; + +import com.wootric.androidsdk.Constants; +import com.wootric.androidsdk.network.responses.RegisteredEventsResponse; +import com.wootric.androidsdk.objects.User; + +import org.json.JSONArray; +import org.json.JSONException; + +import java.util.ArrayList; + +public class GetRegisteredEventsTask extends WootricRemoteRequestTask { + private final User user; + private final GetRegisteredEventsTask.Callback surveyCallback; + + public GetRegisteredEventsTask(User user, GetRegisteredEventsTask.Callback surveyCallback) { + super(REQUEST_TYPE_GET, null, null); + + this.user = user; + this.surveyCallback = surveyCallback; + } + + @Override + protected void buildParams() { + paramsMap.put("account_token", user.getAccountToken()); + + Log.d(Constants.TAG, "parameters: " + paramsMap); + } + + @Override + protected String requestUrl() { + return REGISTERED_EVENTS_URL; + } + + @Override + protected void onSuccess(String response) { + ArrayList registeredEventsList = new ArrayList<>(); + + if (response != null) { + try { + JSONArray jsonArray = new JSONArray(response); + if (jsonArray != null) { + for (int i = 0;i < jsonArray.length(); i++){ + registeredEventsList.add(jsonArray.getString(i)); + } + } + } catch (JSONException e) { + e.printStackTrace(); + } + } + + RegisteredEventsResponse registeredEventsResponse = new RegisteredEventsResponse(registeredEventsList); + surveyCallback.onRegisteredEventsList(registeredEventsResponse); + } + + public interface Callback { + void onRegisteredEventsList(RegisteredEventsResponse registeredEventsResponse); + } +} diff --git a/androidsdk/src/main/java/com/wootric/androidsdk/network/tasks/WootricRemoteRequestTask.java b/androidsdk/src/main/java/com/wootric/androidsdk/network/tasks/WootricRemoteRequestTask.java index 2e8ed07..50ead47 100644 --- a/androidsdk/src/main/java/com/wootric/androidsdk/network/tasks/WootricRemoteRequestTask.java +++ b/androidsdk/src/main/java/com/wootric/androidsdk/network/tasks/WootricRemoteRequestTask.java @@ -42,7 +42,6 @@ * Created by maciejwitowski on 10/13/15. */ public abstract class WootricRemoteRequestTask extends AsyncTask { - private static final String HTTP_AGENT = "Wootric-Mobile-SDK"; static final String REQUEST_TYPE_POST = "POST"; @@ -56,6 +55,7 @@ public abstract class WootricRemoteRequestTask extends AsyncTask(); - if(socialShareJson != null) { + if(socialShareJson.has(SOCIAL_SHARE_QUESTION_KEY) && socialShareJson.has(SOCIAL_SHARE_DECLINE_KEY)) { localizedTexts.socialShare.put(SOCIAL_SHARE_QUESTION_KEY, socialShareJson.optString(SOCIAL_SHARE_QUESTION_KEY)); localizedTexts.socialShare.put(SOCIAL_SHARE_DECLINE_KEY, socialShareJson.optString(SOCIAL_SHARE_DECLINE_KEY)); } diff --git a/androidsdk/src/main/java/com/wootric/androidsdk/objects/Settings.java b/androidsdk/src/main/java/com/wootric/androidsdk/objects/Settings.java index b345201..1d61e89 100644 --- a/androidsdk/src/main/java/com/wootric/androidsdk/objects/Settings.java +++ b/androidsdk/src/main/java/com/wootric/androidsdk/objects/Settings.java @@ -25,7 +25,6 @@ import android.net.Uri; import android.os.Parcel; import android.os.Parcelable; -import android.util.Log; import com.wootric.androidsdk.Constants; import com.wootric.androidsdk.R; @@ -41,7 +40,6 @@ * Created by maciejwitowski on 5/5/15. */ public class Settings implements Parcelable { - private Long firstSurvey = -1L; private int adminPanelTimeDelay = Constants.NOT_SET; private LocalizedTexts localizedTexts; @@ -75,6 +73,7 @@ public class Settings implements Parcelable { private String productName; private String recommendTarget; private String surveyType; + private String eventName; private int surveyColor = Constants.NOT_SET; private int scoreColor = Constants.NOT_SET; @@ -82,6 +81,44 @@ public class Settings implements Parcelable { private int socialSharingColor = Constants.NOT_SET; private int surveyTypeScale = 0; + public Settings(Settings settings) { + this.firstSurvey = settings.firstSurvey; + this.adminPanelTimeDelay = settings.getAdminPanelTimeDelay(); + this.localizedTexts = new LocalizedTexts(settings.getLocalizedTexts()); + this.userID = settings.getUserID(); + this.accountID = settings.getAccountID(); + this.adminPanelCustomMessage = new WootricCustomMessage(settings.getAdminPanelCustomMessage()); + this.localCustomMessage = new WootricCustomMessage(settings.getLocalCustomMessage()); + this.adminPanelSocial = new WootricSocial(settings.adminPanelSocial); + this.localSocial = new WootricSocial(settings.localSocial); + this.localCustomThankYou = new WootricCustomThankYou(settings.localCustomThankYou); + this.adminPanelCustomThankYou = new WootricCustomThankYou(settings.adminPanelCustomThankYou); + this.timeDelay = settings.timeDelay; + this.surveyImmediately = settings.surveyImmediately; + this.showOptOut = settings.showOptOut; + this.skipFollowupScreenForPromoters = settings.skipFollowupScreenForPromoters; + this.skipFeedbackScreen = settings.skipFeedbackScreen; + this.dailyResponseCap = settings.dailyResponseCap; + this.registeredPercent = settings.registeredPercent; + this.visitorPercent = settings.visitorPercent; + this.resurveyThrottle = settings.resurveyThrottle; + this.declineResurveyThrottle = settings.declineResurveyThrottle; + this.languageCode = settings.languageCode; + this.productName = settings.productName; + this.recommendTarget = settings.recommendTarget; + this.surveyType = settings.surveyType; + this.eventName = settings.eventName; + this.surveyColor = settings.surveyColor; + this.scoreColor = settings.scoreColor; + this.thankYouButtonBackgroundColor = settings.thankYouButtonBackgroundColor; + this.socialSharingColor = settings.socialSharingColor; + this.surveyTypeScale = settings.surveyTypeScale; + } + + public Settings() { + this.localSocial = new WootricSocial(); + } + // Sets values from the argument settings only if they are not provided yet public void mergeWithSurveyServerSettings(Settings settings) { if (settings == null) { @@ -522,6 +559,14 @@ public void setLanguageCode(String languageCode) { this.languageCode = languageCode; } + public String getEventName() { + return eventName; + } + + public void setEventName(String eventName) { + this.eventName = eventName; + } + public String getProductName() { return productName; } @@ -655,10 +700,6 @@ public void setSocialSharingColor (int socialSharingColor) { this.socialSharingColor = socialSharingColor; } - public Settings() { - this.localSocial = new WootricSocial(); - } - @Override public int describeContents() { return 0; @@ -719,7 +760,6 @@ private Settings(Parcel in) { public Settings createFromParcel(Parcel source) { return new Settings(source); } - public Settings[] newArray(int size) { return new Settings[size]; } diff --git a/androidsdk/src/main/java/com/wootric/androidsdk/objects/User.java b/androidsdk/src/main/java/com/wootric/androidsdk/objects/User.java index 2d897b8..32f22bd 100644 --- a/androidsdk/src/main/java/com/wootric/androidsdk/objects/User.java +++ b/androidsdk/src/main/java/com/wootric/androidsdk/objects/User.java @@ -29,11 +29,16 @@ * Created by maciejwitowski on 4/21/15. */ public class User implements Parcelable { - private String clientId; private String clientSecret; private String accountToken; + public User(User user) { + this.clientId = user.clientId; + this.clientSecret = user.clientSecret; + this.accountToken = user.accountToken; + } + public User(String clientId, String clientSecret, String accountToken) { this.clientId = clientId; this.clientSecret = clientSecret; @@ -79,7 +84,6 @@ private User(Parcel in) { public User createFromParcel(Parcel source) { return new User(source); } - public User[] newArray(int size) { return new User[size]; } diff --git a/androidsdk/src/main/java/com/wootric/androidsdk/objects/WootricCustomMessage.java b/androidsdk/src/main/java/com/wootric/androidsdk/objects/WootricCustomMessage.java index 626f45e..2b3dde4 100644 --- a/androidsdk/src/main/java/com/wootric/androidsdk/objects/WootricCustomMessage.java +++ b/androidsdk/src/main/java/com/wootric/androidsdk/objects/WootricCustomMessage.java @@ -34,7 +34,6 @@ * Created by maciejwitowski on 4/17/15. */ public class WootricCustomMessage implements Parcelable { - private static final String DETRACTOR_QUESTION_KEY = "detractor_question"; private static final String PASSIVE_QUESTION_KEY = "passive_question"; private static final String PROMOTER_QUESTION_KEY = "promoter_question"; @@ -42,7 +41,6 @@ public class WootricCustomMessage implements Parcelable { private static final String PASSIVE_TEXT_KEY = "passive_text"; private static final String PROMOTER_TEXT_KEY = "promoter_text"; - private String followupQuestion; private HashMap followupQuestionsList; private String placeholderText; @@ -53,6 +51,18 @@ public WootricCustomMessage() { this.placeholderTextsList = new HashMap<>(); } + public WootricCustomMessage(WootricCustomMessage wootricCustomMessage) { + this.followupQuestionsList = new HashMap<>(); + this.placeholderTextsList = new HashMap<>(); + + if (wootricCustomMessage == null) return; + + this.followupQuestion = wootricCustomMessage.followupQuestion; + this.followupQuestionsList = wootricCustomMessage.followupQuestionsList; + this.placeholderText = wootricCustomMessage.placeholderText; + this.placeholderTextsList = wootricCustomMessage.placeholderTextsList; + } + public String getFollowupQuestion() { return followupQuestion; } @@ -174,10 +184,7 @@ private WootricCustomMessage(Parcel in) { } public static final Creator CREATOR = new Creator() { - public WootricCustomMessage createFromParcel(Parcel source) { - return new WootricCustomMessage(source); - } - + public WootricCustomMessage createFromParcel(Parcel source) { return new WootricCustomMessage(source); } public WootricCustomMessage[] newArray(int size) { return new WootricCustomMessage[size]; } diff --git a/androidsdk/src/main/java/com/wootric/androidsdk/objects/WootricCustomThankYou.java b/androidsdk/src/main/java/com/wootric/androidsdk/objects/WootricCustomThankYou.java index ddd748f..0c2d6a8 100644 --- a/androidsdk/src/main/java/com/wootric/androidsdk/objects/WootricCustomThankYou.java +++ b/androidsdk/src/main/java/com/wootric/androidsdk/objects/WootricCustomThankYou.java @@ -35,7 +35,6 @@ * Created by maciejwitowski on 9/23/15. */ public class WootricCustomThankYou implements Parcelable { - private String finalThankYou; private String detractorFinalThankYou; private String passiveFinalThankYou; @@ -71,6 +70,41 @@ public class WootricCustomThankYou implements Parcelable { private boolean uniqueByScore = false; + public WootricCustomThankYou() {} + + public WootricCustomThankYou(WootricCustomThankYou wootricCustomThankYou) { + if (wootricCustomThankYou == null) return; + this.finalThankYou = wootricCustomThankYou.finalThankYou; + this.detractorFinalThankYou = wootricCustomThankYou.detractorFinalThankYou; + this.passiveFinalThankYou = wootricCustomThankYou.passiveFinalThankYou; + this.promoterFinalThankYou = wootricCustomThankYou.promoterFinalThankYou; + this.text = wootricCustomThankYou.text; + this.detractorText = wootricCustomThankYou.detractorText; + this.passiveText = wootricCustomThankYou.passiveText; + this.promoterText = wootricCustomThankYou.promoterText; + this.linkText = wootricCustomThankYou.linkText; + this.detractorLinkText = wootricCustomThankYou.detractorLinkText; + this.passiveLinkText = wootricCustomThankYou.passiveLinkText; + this.promoterLinkText = wootricCustomThankYou.promoterLinkText; + this.linkUri = wootricCustomThankYou.linkUri; + this.detractorLinkUri = wootricCustomThankYou.detractorLinkUri; + this.passiveLinkUri = wootricCustomThankYou.passiveLinkUri; + this.promoterLinkUri = wootricCustomThankYou.promoterLinkUri; + this.emailInUrl = wootricCustomThankYou.emailInUrl; + this.detractorEmailInUrl = wootricCustomThankYou.detractorEmailInUrl; + this.passiveEmailInUrl = wootricCustomThankYou.passiveEmailInUrl; + this.promoterEmailInUrl = wootricCustomThankYou.promoterEmailInUrl; + this.scoreInUrl = wootricCustomThankYou.scoreInUrl; + this.detractorScoreInUrl = wootricCustomThankYou.detractorScoreInUrl; + this.passiveScoreInUrl = wootricCustomThankYou.passiveScoreInUrl; + this.promoterScoreInUrl = wootricCustomThankYou.promoterScoreInUrl; + this.commentInUrl = wootricCustomThankYou.commentInUrl; + this.detractorCommentInUrl = wootricCustomThankYou.detractorCommentInUrl; + this.passiveCommentInUrl = wootricCustomThankYou.passiveCommentInUrl; + this.promoterCommentInUrl = wootricCustomThankYou.promoterCommentInUrl; + this.uniqueByScore = wootricCustomThankYou.uniqueByScore; + } + public String getFinalThankYouForScore(int scoreValue, String surveyType, int surveyTypeScale) { Score score = new Score(scoreValue, surveyType, surveyTypeScale); @@ -307,7 +341,6 @@ public void setPromoterCommentInUrl(boolean promoterCommentInUrl) { this.promoterCommentInUrl = promoterCommentInUrl; } - @Override public int describeContents() { return 0; @@ -341,9 +374,6 @@ public void writeToParcel(Parcel dest, int flags) { dest.writeByte(Utils.getByteValue(this.promoterCommentInUrl)); } - public WootricCustomThankYou() { - } - private WootricCustomThankYou(Parcel in) { this.text = in.readString(); this.detractorText = in.readString(); diff --git a/androidsdk/src/main/java/com/wootric/androidsdk/objects/WootricEvent.java b/androidsdk/src/main/java/com/wootric/androidsdk/objects/WootricEvent.java new file mode 100644 index 0000000..8e8cc36 --- /dev/null +++ b/androidsdk/src/main/java/com/wootric/androidsdk/objects/WootricEvent.java @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2016 Wootric (https://wootric.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.wootric.androidsdk.objects; + +import android.app.Activity; + +import androidx.fragment.app.FragmentActivity; + +import com.wootric.androidsdk.SurveyValidator; +import com.wootric.androidsdk.WootricSurveyCallback; +import com.wootric.androidsdk.network.WootricRemoteClient; +import com.wootric.androidsdk.utils.PreferencesUtils; + +public class WootricEvent { + private Activity activity; + private FragmentActivity fragmentActivity; + private WootricRemoteClient wootricApiClient; + private User user; + private EndUser endUser; + private Settings settings; + private PreferencesUtils preferencesUtils; + private WootricSurveyCallback surveyCallback; + private SurveyValidator surveyValidator; + + public WootricEvent(Activity activity, WootricRemoteClient wootricApiClient, User user, EndUser endUser, + Settings settings, PreferencesUtils preferencesUtils, WootricSurveyCallback surveyCallback, SurveyValidator surveyValidator) { + this.activity = activity; + this.wootricApiClient = wootricApiClient; + this.user = user; + this.endUser = endUser; + this.settings = settings; + this.preferencesUtils = preferencesUtils; + this.surveyCallback = surveyCallback; + this.surveyValidator = surveyValidator; + } + + public WootricEvent(FragmentActivity fragmentActivity, WootricRemoteClient wootricApiClient, User user, EndUser endUser, + Settings settings, PreferencesUtils preferencesUtils, WootricSurveyCallback surveyCallback, SurveyValidator surveyValidator) { + this.fragmentActivity = fragmentActivity; + this.wootricApiClient = wootricApiClient; + this.user = user; + this.endUser = endUser; + this.settings = settings; + this.preferencesUtils = preferencesUtils; + this.surveyCallback = surveyCallback; + this.surveyValidator = surveyValidator; + } + + public WootricRemoteClient getWootricApiClient() { + return this.wootricApiClient; + } + + public User getUser() { + return this.user; + } + + public EndUser getEndUser() { + return this.endUser; + } + + public Settings getSettings() { return this.settings; } + + public PreferencesUtils getPreferencesUtils() { + return this.preferencesUtils; + } + + public Activity getActivity() { return this.activity; } + + public FragmentActivity getFragmentActivity() { return this.fragmentActivity; } + + public WootricSurveyCallback getSurveyCallback() { return this.surveyCallback; } + + public SurveyValidator getSurveyValidator() { return this.surveyValidator; } +} diff --git a/androidsdk/src/main/java/com/wootric/androidsdk/objects/WootricSocial.java b/androidsdk/src/main/java/com/wootric/androidsdk/objects/WootricSocial.java index ba14ed9..c072f49 100644 --- a/androidsdk/src/main/java/com/wootric/androidsdk/objects/WootricSocial.java +++ b/androidsdk/src/main/java/com/wootric/androidsdk/objects/WootricSocial.java @@ -1,6 +1,5 @@ package com.wootric.androidsdk.objects; -import android.net.Uri; import android.os.Parcel; import android.os.Parcelable; @@ -16,6 +15,16 @@ public class WootricSocial implements Parcelable { private Boolean facebookEnabled; private Boolean twitterEnabled; + public WootricSocial() {} + + public WootricSocial(WootricSocial wootricSocial) { + if (wootricSocial == null) return; + this.facebookPageId = wootricSocial.facebookPageId; + this.twitterPage = wootricSocial.twitterPage; + this.facebookEnabled = wootricSocial.facebookEnabled; + this.twitterEnabled = wootricSocial.twitterEnabled; + } + public void setFacebookPageId(String facebookPageId) { this.facebookPageId = facebookPageId; } public String getFacebookPageId() { return this.facebookPageId; } @@ -45,9 +54,6 @@ public void writeToParcel(Parcel dest, int flags) { dest.writeByte(Utils.getByteValue(twitterEnabled)); } - public WootricSocial() { - } - private WootricSocial(Parcel in) { this.facebookPageId = in.readString(); this.twitterPage = in.readString(); @@ -59,7 +65,6 @@ private WootricSocial(Parcel in) { public WootricSocial createFromParcel(Parcel source) { return new WootricSocial(source); } - public WootricSocial[] newArray(int size) { return new WootricSocial[size]; } diff --git a/androidsdk/src/main/java/com/wootric/androidsdk/views/OnSurveyFinishedListener.java b/androidsdk/src/main/java/com/wootric/androidsdk/views/OnSurveyFinishedListener.java new file mode 100644 index 0000000..e9a6619 --- /dev/null +++ b/androidsdk/src/main/java/com/wootric/androidsdk/views/OnSurveyFinishedListener.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2016 Wootric (https://wootric.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.wootric.androidsdk.views; + +public interface OnSurveyFinishedListener { + void onSurveyFinished(); +} diff --git a/androidsdk/src/main/java/com/wootric/androidsdk/views/SurveyFragment.java b/androidsdk/src/main/java/com/wootric/androidsdk/views/SurveyFragment.java index 5f576a3..1cabded 100644 --- a/androidsdk/src/main/java/com/wootric/androidsdk/views/SurveyFragment.java +++ b/androidsdk/src/main/java/com/wootric/androidsdk/views/SurveyFragment.java @@ -31,7 +31,6 @@ import android.net.Uri; import android.os.Bundle; import android.app.DialogFragment; -import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; @@ -75,6 +74,7 @@ public class SurveyFragment extends DialogFragment private SurveyLayout mSurveyLayout; private LinearLayout mFooter; private WootricSurveyCallback mSurveyCallback; + private OnSurveyFinishedListener mOnSurveyFinishedListener; private EndUser mEndUser; private User mUser; @@ -118,6 +118,10 @@ public void setSurveyCallback(WootricSurveyCallback surveyCallback) { mSurveyCallback = surveyCallback; } + public void setOnSurveyFinishedListener(OnSurveyFinishedListener onSurveyFinishedListener) { + mOnSurveyFinishedListener = onSurveyFinishedListener; + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -332,7 +336,7 @@ public void onShouldShowSimpleDialog() { if (activity != null) { mShouldShowSimpleDialog = true; - ThankYouDialogFactory.create(activity, mSettings, mSurveyLayout.getSelectedScore(), mText, mSurveyCallback).show(); + ThankYouDialogFactory.create(activity, mSettings, mSurveyLayout.getSelectedScore(), mText, mSurveyCallback, mOnSurveyFinishedListener).show(); } dismiss(); @@ -356,13 +360,16 @@ public void onDestroy() { isResumedOnConfigurationChange = true; - if (mSurveyCallback != null && !mShouldShowSimpleDialog) { - HashMap hashMap = new HashMap(); - if (mScore != -1) { - hashMap.put("score", mScore); + if (!mShouldShowSimpleDialog) { + mOnSurveyFinishedListener.onSurveyFinished(); + if (mSurveyCallback != null) { + HashMap hashMap = new HashMap(); + if (mScore != -1) { + hashMap.put("score", mScore); + } + hashMap.put("text", mText); + mSurveyCallback.onSurveyDidHide(hashMap); } - hashMap.put("text", mText); - mSurveyCallback.onSurveyDidHide(hashMap); } } @@ -380,7 +387,6 @@ private void notifySurveyFinished() { Wootric.notifySurveyFinished(true, mResponseSent, resurvey_days); } - private void optOut() { String optOutUrl = "https://app.wootric.com/opt_out?token=" + mUser.getAccountToken() + "&metric_type=" + mSettings.getSurveyType() diff --git a/androidsdk/src/main/java/com/wootric/androidsdk/views/phone/ThankYouDialogFactory.java b/androidsdk/src/main/java/com/wootric/androidsdk/views/phone/ThankYouDialogFactory.java index e9ce87a..378b640 100644 --- a/androidsdk/src/main/java/com/wootric/androidsdk/views/phone/ThankYouDialogFactory.java +++ b/androidsdk/src/main/java/com/wootric/androidsdk/views/phone/ThankYouDialogFactory.java @@ -29,6 +29,7 @@ import com.wootric.androidsdk.WootricSurveyCallback; import com.wootric.androidsdk.objects.Settings; +import com.wootric.androidsdk.views.OnSurveyFinishedListener; import java.util.HashMap; @@ -36,8 +37,7 @@ * Created by maciejwitowski on 10/2/15. */ public class ThankYouDialogFactory { - - public static Dialog create(Context context, Settings settings, final int score, final String text, final WootricSurveyCallback surveyCallback) { + public static Dialog create(Context context, Settings settings, final int score, final String text, final WootricSurveyCallback surveyCallback, final OnSurveyFinishedListener onSurveyFinishedListener) { AlertDialog thankYouDialog = new AlertDialog.Builder(context).create(); thankYouDialog.setCancelable(false); final String thankYouText = settings.getFinalThankYou(score); @@ -48,6 +48,8 @@ public static Dialog create(Context context, Settings settings, final int score, @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); + + onSurveyFinishedListener.onSurveyFinished(); if (surveyCallback != null) { HashMap hashMap = new HashMap(); if (score != -1) { diff --git a/androidsdk/src/main/java/com/wootric/androidsdk/views/support/SurveyFragment.java b/androidsdk/src/main/java/com/wootric/androidsdk/views/support/SurveyFragment.java index 71c3f13..5026740 100644 --- a/androidsdk/src/main/java/com/wootric/androidsdk/views/support/SurveyFragment.java +++ b/androidsdk/src/main/java/com/wootric/androidsdk/views/support/SurveyFragment.java @@ -52,6 +52,7 @@ import com.wootric.androidsdk.utils.SHAUtil; import com.wootric.androidsdk.utils.ScreenUtils; import com.wootric.androidsdk.utils.SocialHandler; +import com.wootric.androidsdk.views.OnSurveyFinishedListener; import com.wootric.androidsdk.views.SurveyLayout; import com.wootric.androidsdk.views.SurveyLayoutListener; import com.wootric.androidsdk.views.phone.ThankYouDialogFactory; @@ -63,9 +64,7 @@ /** * Created by maciejwitowski on 9/4/15. */ -public class SurveyFragment extends DialogFragment - implements SurveyLayoutListener { - +public class SurveyFragment extends DialogFragment implements SurveyLayoutListener { private static final String ARG_ORIGIN_URL = "com.wootric.androidsdk.arg.origin_url"; private static final String ARG_END_USER = "com.wootric.androidsdk.arg.end_user"; private static final String ARG_USER = "com.wootric.androidsdk.arg.user"; @@ -76,6 +75,7 @@ public class SurveyFragment extends DialogFragment private SurveyLayout mSurveyLayout; private LinearLayout mFooter; private WootricSurveyCallback mSurveyCallback; + private OnSurveyFinishedListener mOnSurveyFinishedListener; private EndUser mEndUser; private User mUser; @@ -119,6 +119,10 @@ public void setSurveyCallback(WootricSurveyCallback surveyCallback) { mSurveyCallback = surveyCallback; } + public void setOnSurveyFinishedListener(OnSurveyFinishedListener onSurveyFinishedListener) { + mOnSurveyFinishedListener = onSurveyFinishedListener; + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -333,7 +337,7 @@ public void onShouldShowSimpleDialog() { if (activity != null) { mShouldShowSimpleDialog = true; - ThankYouDialogFactory.create(activity, mSettings, mSurveyLayout.getSelectedScore(), mText, mSurveyCallback).show(); + ThankYouDialogFactory.create(activity, mSettings, mSurveyLayout.getSelectedScore(), mText, mSurveyCallback, mOnSurveyFinishedListener).show(); } dismiss(); @@ -356,14 +360,18 @@ public void onDestroy() { isResumedOnConfigurationChange = true; - if (mSurveyCallback != null && !mShouldShowSimpleDialog) { - HashMap hashMap = new HashMap(); - if (mScore != -1) { - hashMap.put("score", mScore); + if (!mShouldShowSimpleDialog) { + mOnSurveyFinishedListener.onSurveyFinished(); + if (mSurveyCallback != null) { + HashMap hashMap = new HashMap(); + if (mScore != -1) { + hashMap.put("score", mScore); + } + hashMap.put("text", mText); + mSurveyCallback.onSurveyDidHide(hashMap); } - hashMap.put("text", mText); - mSurveyCallback.onSurveyDidHide(hashMap); } + } @Override @@ -380,7 +388,6 @@ private void notifySurveyFinished() { Wootric.notifySurveyFinished(true, mResponseSent, resurvey_days); } - private void optOut() { String optOutUrl = "https://app.wootric.com/opt_out?token=" + mUser.getAccountToken() + "&metric_type=" + mSettings.getSurveyType() diff --git a/androidsdk/src/test/java/com/wootric/androidsdk/SurveyManagerTest.java b/androidsdk/src/test/java/com/wootric/androidsdk/SurveyManagerTest.java index 579daca..d01379b 100644 --- a/androidsdk/src/test/java/com/wootric/androidsdk/SurveyManagerTest.java +++ b/androidsdk/src/test/java/com/wootric/androidsdk/SurveyManagerTest.java @@ -1,25 +1,26 @@ package com.wootric.androidsdk; -import androidx.fragment.app.FragmentActivity; - import com.wootric.androidsdk.network.WootricRemoteClient; import com.wootric.androidsdk.objects.EndUser; import com.wootric.androidsdk.objects.Settings; import com.wootric.androidsdk.objects.User; +import com.wootric.androidsdk.objects.WootricEvent; import com.wootric.androidsdk.utils.PreferencesUtils; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; +import java.lang.reflect.Array; +import java.util.ArrayList; import java.util.HashMap; import static com.wootric.androidsdk.TestHelper.TEST_FRAGMENT_ACTIVITY; import static com.wootric.androidsdk.TestHelper.testEndUser; import static com.wootric.androidsdk.TestHelper.testUser; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Matchers.any; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; @@ -40,58 +41,59 @@ public class SurveyManagerTest { @Mock WootricSurveyCallback surveyCallback; + @Before + public void setUp() { + SurveyManager.sharedInstance = null; + } + @Test public void updatesLastSeen() throws Exception { - SurveyManager surveyManager = new SurveyManager(TEST_FRAGMENT_ACTIVITY, - wootricApiClient, testUser(), testEndUser(), new Settings(), preferencesUtils, - surveyValidator, surveyCallback); + SurveyManager surveyManager = SurveyManager.getSharedInstance(); - surveyManager.start(); + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), testEndUser(), new Settings(), preferencesUtils, surveyCallback, surveyValidator); verify(preferencesUtils, times(1)).touchLastSeen(); } @Test public void validatesSurvey() throws Exception { - SurveyManager surveyManager = new SurveyManager(TEST_FRAGMENT_ACTIVITY, - wootricApiClient, testUser(), testEndUser(), new Settings(), preferencesUtils, - surveyValidator, surveyCallback); + SurveyManager surveyManager = SurveyManager.getSharedInstance(); - surveyManager.start(); + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), testEndUser(), new Settings(), preferencesUtils, surveyCallback, surveyValidator); verify(surveyValidator, times(1)).validate(); } @Test public void setsOnSurveyValidatedListener() throws Exception { - SurveyManager surveyManager = new SurveyManager(TEST_FRAGMENT_ACTIVITY, - wootricApiClient, testUser(), testEndUser(), new Settings(), preferencesUtils, - surveyValidator, surveyCallback); + SurveyManager surveyManager = SurveyManager.getSharedInstance(); - surveyManager.start(); + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), testEndUser(), new Settings(), preferencesUtils, surveyCallback, surveyValidator); verify(surveyValidator, times(1)).setOnSurveyValidatedListener(surveyManager); } @Test public void sendsGetAccessTokenRequest() throws Exception { - User user = testUser(); - Settings settings = new Settings(); - SurveyManager surveyManager = new SurveyManager(new FragmentActivity(), - wootricApiClient, user, testEndUser(), settings, preferencesUtils, - surveyValidator, surveyCallback); + SurveyManager surveyManager = SurveyManager.getSharedInstance(); + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), testEndUser(), new Settings(), preferencesUtils, surveyCallback, surveyValidator); surveyManager.onSurveyValidated(new Settings()); - verify(wootricApiClient, times(1)).authenticate(user, surveyManager); + WootricEvent event = surveyManager.getCurrentEvent(); + verify(wootricApiClient, times(1)).authenticate(event.getUser(), surveyManager); } @Test public void sendsGetEndUserRequest() throws Exception { EndUser endUser = new EndUser("nps@example.com"); - SurveyManager surveyManager = new SurveyManager(new FragmentActivity(), - wootricApiClient, testUser(), endUser, new Settings(), preferencesUtils, - surveyValidator, surveyCallback); + SurveyManager surveyManager = SurveyManager.getSharedInstance(); + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), endUser, new Settings(), preferencesUtils, surveyCallback, surveyValidator); final String accessToken = "test123test"; surveyManager.onAuthenticateSuccess(accessToken); @@ -102,16 +104,16 @@ wootricApiClient, testUser(), endUser, new Settings(), preferencesUtils, @Test public void whenEndUserReceived_showsSurvey() throws Exception { long receivedId = 1; - - EndUser endUser = testEndUser(); - SurveyManager surveyManager = spy(new SurveyManager(new FragmentActivity(), - wootricApiClient, testUser(), endUser, new Settings(), preferencesUtils, - surveyValidator, surveyCallback)); + SurveyManager surveyManager = spy(SurveyManager.getSharedInstance()); + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), testEndUser(), new Settings(), preferencesUtils, surveyCallback, surveyValidator); doNothing().when(surveyManager).showSurvey(); surveyManager.onGetEndUserIdSuccess(receivedId); + WootricEvent event = surveyManager.getCurrentEvent(); + EndUser endUser = event.getEndUser(); assertThat(endUser.getId()).isEqualTo(receivedId); verify(surveyManager, times(1)).showSurvey(); } @@ -119,12 +121,9 @@ wootricApiClient, testUser(), endUser, new Settings(), preferencesUtils, @Test public void whenEndUserReceived_sendsRequestToUpdateEndUserProperties() throws Exception { long receivedId = 1; - - EndUser endUser = testEndUser(); - - SurveyManager surveyManager = spy(new SurveyManager(new FragmentActivity(), - wootricApiClient, testUser(), endUser, new Settings(), preferencesUtils, - surveyValidator, surveyCallback)); + SurveyManager surveyManager = spy(SurveyManager.getSharedInstance()); + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), testEndUser(), new Settings(), preferencesUtils, surveyCallback, surveyValidator); final String accessToken = "test123test"; surveyManager.setAccessToken(accessToken); @@ -133,6 +132,8 @@ wootricApiClient, testUser(), endUser, new Settings(), preferencesUtils, surveyManager.onGetEndUserIdSuccess(receivedId); + WootricEvent event = surveyManager.getCurrentEvent(); + EndUser endUser = event.getEndUser(); verify(wootricApiClient, times(0)).updateEndUser(endUser, accessToken, surveyManager); HashMap endUserProperties = new HashMap<>(); @@ -145,12 +146,9 @@ wootricApiClient, testUser(), endUser, new Settings(), preferencesUtils, @Test public void whenEndUserReceived_sendsRequestToUpdateEndUserPhoneNumber() throws Exception { long receivedId = 1; - - EndUser endUser = testEndUser(); - - SurveyManager surveyManager = spy(new SurveyManager(new FragmentActivity(), - wootricApiClient, testUser(), endUser, new Settings(), preferencesUtils, - surveyValidator, surveyCallback)); + SurveyManager surveyManager = spy(SurveyManager.getSharedInstance()); + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), testEndUser(), new Settings(), preferencesUtils, surveyCallback, surveyValidator); final String accessToken = "test123test"; surveyManager.setAccessToken(accessToken); @@ -159,6 +157,8 @@ wootricApiClient, testUser(), endUser, new Settings(), preferencesUtils, surveyManager.onGetEndUserIdSuccess(receivedId); + WootricEvent event = surveyManager.getCurrentEvent(); + EndUser endUser = event.getEndUser(); verify(wootricApiClient, times(0)).updateEndUser(endUser, accessToken, surveyManager); endUser.setPhoneNumber(null); @@ -181,12 +181,9 @@ wootricApiClient, testUser(), endUser, new Settings(), preferencesUtils, @Test public void whenEndUserReceived_sendsRequestToUpdateEndUserExternalId() throws Exception { long receivedId = 1; - - EndUser endUser = testEndUser(); - - SurveyManager surveyManager = spy(new SurveyManager(new FragmentActivity(), - wootricApiClient, testUser(), endUser, new Settings(), preferencesUtils, - surveyValidator, surveyCallback)); + SurveyManager surveyManager = spy(SurveyManager.getSharedInstance()); + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), testEndUser(), new Settings(), preferencesUtils, surveyCallback, surveyValidator); final String accessToken = "test123test"; surveyManager.setAccessToken(accessToken); @@ -195,6 +192,8 @@ wootricApiClient, testUser(), endUser, new Settings(), preferencesUtils, surveyManager.onGetEndUserIdSuccess(receivedId); + WootricEvent event = surveyManager.getCurrentEvent(); + EndUser endUser = event.getEndUser(); verify(wootricApiClient, times(0)).updateEndUser(endUser, accessToken, surveyManager); endUser.setExternalId(null); @@ -217,23 +216,24 @@ wootricApiClient, testUser(), endUser, new Settings(), preferencesUtils, @Test public void whenEndUserNotFound_sendsRequestToCreateEndUser() throws Exception { EndUser endUser = testEndUser(); - SurveyManager surveyManager = new SurveyManager(new FragmentActivity(), - wootricApiClient, testUser(), endUser, new Settings(), preferencesUtils, - surveyValidator, surveyCallback); + SurveyManager surveyManager = SurveyManager.getSharedInstance(); + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), endUser, new Settings(), preferencesUtils, surveyCallback, surveyValidator); final String accessToken = "test123test"; surveyManager.setAccessToken(accessToken); surveyManager.onEndUserNotFound(); - verify(wootricApiClient, times(1)).createEndUser(endUser, accessToken, surveyManager); + WootricEvent event = surveyManager.getCurrentEvent(); + verify(wootricApiClient, times(1)).createEndUser(event.getEndUser(), accessToken, surveyManager); } @Test public void showSurvey() throws Exception { - EndUser endUser = testEndUser(); - SurveyManager surveyManager = spy(new SurveyManager(new FragmentActivity(), - wootricApiClient, testUser(), endUser, new Settings(), preferencesUtils, - surveyValidator, surveyCallback)); + SurveyManager surveyManager = spy(SurveyManager.getSharedInstance()); + + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), testEndUser(), new Settings(), preferencesUtils, surveyCallback, surveyValidator); doNothing().when(surveyManager).showSurvey(); @@ -241,16 +241,15 @@ wootricApiClient, testUser(), endUser, new Settings(), preferencesUtils, receivedEndUser.setId(1); surveyManager.onCreateEndUserSuccess(receivedEndUser.getId()); - assertThat(endUser.getId()).isEqualTo(receivedEndUser.getId()); + assertThat(surveyManager.getCurrentEvent().getEndUser().getId()).isEqualTo(receivedEndUser.getId()); verify(surveyManager, times(1)).showSurvey(); } @Test public void showSurveyWithActivity() throws Exception { - EndUser endUser = testEndUser(); - SurveyManager surveyManager = spy(new SurveyManager(new FragmentActivity(), - wootricApiClient, testUser(), endUser, new Settings(), preferencesUtils, - surveyValidator, surveyCallback)); + SurveyManager surveyManager = spy(SurveyManager.getSharedInstance()); + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), testEndUser(), new Settings(), preferencesUtils, surveyCallback, surveyValidator); doNothing().when(surveyManager).showSurvey(); @@ -258,20 +257,187 @@ wootricApiClient, testUser(), endUser, new Settings(), preferencesUtils, receivedEndUser.setId(1); surveyManager.onCreateEndUserSuccess(receivedEndUser.getId()); - assertThat(endUser.getId()).isEqualTo(receivedEndUser.getId()); + WootricEvent event = surveyManager.getCurrentEvent(); + assertThat(event.getEndUser().getId()).isEqualTo(receivedEndUser.getId()); verify(surveyManager, times(1)).showSurvey(); } @Test public void surveyCallbackWillShow() throws Exception { - EndUser endUser = testEndUser(); - - SurveyManager surveyManager = spy(new SurveyManager(new FragmentActivity(), - wootricApiClient, testUser(), endUser, new Settings(), preferencesUtils, - surveyValidator, surveyCallback)); + SurveyManager surveyManager = spy(SurveyManager.getSharedInstance()); + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), testEndUser(), new Settings(), preferencesUtils, surveyCallback, surveyValidator); surveyManager.showSurvey(); verify(surveyCallback, times(1)).onSurveyWillShow(); } + + @Test + public void whenStartIsCalledMultipleTimes_selectsCorrectEvent() { + SurveyManager surveyManager = spy(SurveyManager.getSharedInstance()); + EndUser endUser1 = new EndUser("testOne@wootric.com"); + EndUser endUser2 = new EndUser("testTwo@wootric.com"); + EndUser endUser3 = new EndUser("testThree@wootric.com"); + + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), endUser1, new Settings(), preferencesUtils, surveyCallback, surveyValidator); + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), endUser2, new Settings(), preferencesUtils, surveyCallback, surveyValidator); + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), endUser3, new Settings(), preferencesUtils, surveyCallback, surveyValidator); + + WootricEvent event = surveyManager.getCurrentEvent(); + assertThat(event.getEndUser().getEmail()).isEqualTo(endUser1.getEmail()); + } + + @Test + public void iteratesThroughQueue() { + SurveyManager surveyManager = spy(SurveyManager.getSharedInstance()); + EndUser endUser1 = new EndUser("testOne@wootric.com"); + EndUser endUser2 = new EndUser("testTwo@wootric.com"); + EndUser endUser3 = new EndUser("testThree@wootric.com"); + + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), endUser1, new Settings(), preferencesUtils, surveyCallback, surveyValidator); + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), endUser2, new Settings(), preferencesUtils, surveyCallback, surveyValidator); + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), endUser3, new Settings(), preferencesUtils, surveyCallback, surveyValidator); + + WootricEvent event = surveyManager.getCurrentEvent(); + assertThat(event.getEndUser().getEmail()).isEqualTo(endUser1.getEmail()); + + surveyManager.onSurveyNotNeeded(); + event = surveyManager.getCurrentEvent(); + assertThat(event.getEndUser().getEmail()).isEqualTo(endUser2.getEmail()); + + surveyManager.onSurveyNotNeeded(); + event = surveyManager.getCurrentEvent(); + assertThat(event.getEndUser().getEmail()).isEqualTo(endUser3.getEmail()); + } + + @Test + public void clearsQueueWhenEndUserSurveyed() { + SurveyManager surveyManager = spy(SurveyManager.getSharedInstance()); + EndUser endUser1 = new EndUser("testOne@wootric.com"); + EndUser endUser2 = new EndUser("testTwo@wootric.com"); + EndUser endUser3 = new EndUser("testThree@wootric.com"); + + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), endUser1, new Settings(), preferencesUtils, surveyCallback, surveyValidator); + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), endUser2, new Settings(), preferencesUtils, surveyCallback, surveyValidator); + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), endUser3, new Settings(), preferencesUtils, surveyCallback, surveyValidator); + + WootricEvent event = surveyManager.getCurrentEvent(); + assertThat(event.getEndUser().getEmail()).isEqualTo(endUser1.getEmail()); + + surveyManager.onSurveyValidated(new Settings()); + event = surveyManager.getCurrentEvent(); + assertThat(event.getEndUser().getEmail()).isEqualTo(endUser1.getEmail()); + assertThat(surveyManager.eventQueueCount()).isEqualTo(0); + } + + @Test + public void whenEventNamePresent_validateIfEventInRegisteredEvents() { + SurveyManager surveyManager = spy(SurveyManager.getSharedInstance()); + ArrayList registeredEvents = new ArrayList<>(); + registeredEvents.add("On Purchase"); + registeredEvents.add("On Exit"); + + EndUser endUser = new EndUser("testOne@wootric.com"); + Settings settings = new Settings(); + settings.setEventName("On Exit"); + + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), endUser, settings, preferencesUtils, surveyCallback, surveyValidator); + surveyManager.onRegisteredEvents(registeredEvents); + + verify(surveyValidator, times(1)).validate(); + } + + @Test + public void whenEventNamePresent_doesntValidateIfEventNotInRegisteredEvents() { + SurveyManager surveyManager = spy(SurveyManager.getSharedInstance()); + ArrayList registeredEvents = new ArrayList<>(); + registeredEvents.add("On Purchase"); + registeredEvents.add("On Exit"); + surveyManager.onRegisteredEvents(registeredEvents); + + EndUser endUser = new EndUser("testOne@wootric.com"); + Settings settings = new Settings(); + settings.setEventName("On Click"); + + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), endUser, settings, preferencesUtils, surveyCallback, surveyValidator); + + verify(surveyValidator, times(0)).validate(); + } + + @Test + public void whenEventNamePresent_callsValidateForCorrectEvent() { + SurveyManager surveyManager = spy(SurveyManager.getSharedInstance()); + ArrayList registeredEvents = new ArrayList<>(); + registeredEvents.add("On Purchase"); + registeredEvents.add("On Exit"); + surveyManager.onRegisteredEvents(registeredEvents); + + EndUser endUser = new EndUser("testOne@wootric.com"); + Settings settings = new Settings(); + settings.setEventName("On Click"); + + EndUser endUser2 = new EndUser("testTwo@wootric.com"); + Settings settings2 = new Settings(); + settings2.setEventName("On Exit"); + + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), endUser, settings, preferencesUtils, surveyCallback, surveyValidator); + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), endUser2, settings2, preferencesUtils, surveyCallback, surveyValidator); + + WootricEvent event = surveyManager.getCurrentEvent(); + verify(surveyValidator, times(1)).validate(); + assertThat(event.getEndUser().getEmail()).isEqualTo(endUser2.getEmail()); + } + + @Test + public void whenEventNamePresent_callsRegisteredEventsIfListIsEmpty() { + SurveyManager surveyManager = spy(SurveyManager.getSharedInstance()); + + Settings settings = new Settings(); + settings.setEventName("On Click"); + + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), testEndUser(), settings, preferencesUtils, surveyCallback, surveyValidator); + + verify(surveyValidator, times(1)).getRegisteredEvents(); + } + + @Test + public void whenEventNamePresent_doesntCallRegisteredEventsIfListIsNotEmpty() { + SurveyManager surveyManager = spy(SurveyManager.getSharedInstance()); + ArrayList registeredEvents = new ArrayList<>(); + registeredEvents.add("On Purchase"); + registeredEvents.add("On Exit"); + surveyManager.onRegisteredEvents(registeredEvents); + + Settings settings = new Settings(); + settings.setEventName("On Click"); + + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), testEndUser(), settings, preferencesUtils, surveyCallback, surveyValidator); + + verify(surveyValidator, times(0)).getRegisteredEvents(); + } + @Test + public void whenEventNameNotPresent_doesntCallRegisteredEvents() { + SurveyManager surveyManager = spy(SurveyManager.getSharedInstance()); + + surveyManager.start(TEST_FRAGMENT_ACTIVITY, + wootricApiClient, testUser(), testEndUser(), new Settings(), preferencesUtils, surveyCallback, surveyValidator); + + verify(surveyValidator, times(0)).getRegisteredEvents(); + } } diff --git a/androidsdk/src/test/java/com/wootric/androidsdk/WootricTest.java b/androidsdk/src/test/java/com/wootric/androidsdk/WootricTest.java index c9f3903..0798345 100644 --- a/androidsdk/src/test/java/com/wootric/androidsdk/WootricTest.java +++ b/androidsdk/src/test/java/com/wootric/androidsdk/WootricTest.java @@ -30,7 +30,7 @@ @RunWith(MockitoJUnitRunner.class) public class WootricTest { - @Mock SurveyManager mockSurveyManager; + @Mock SurveyManager mockSurveyManager = SurveyManager.getSharedInstance(); @Mock SurveyValidator mockSurveyValidator; @Mock PreferencesUtils mockPreferencesUtils; @Mock PermissionsValidator mockPermissionsValidator; @@ -231,22 +231,10 @@ public void setsTweeterPage() { Wootric wootric = spy(Wootric.init(TEST_FRAGMENT_ACTIVITY, CLIENT_ID, CLIENT_SECRET, ACCOUNT_TOKEN)); Wootric wootric_1 = spy(Wootric.init(TEST_FRAGMENT_ACTIVITY, CLIENT_ID, ACCOUNT_TOKEN)); - doReturn(mockSurveyValidator).when(wootric).buildSurveyValidator(eq(wootric.user), - eq(wootric.endUser), eq(wootric.settings), any(WootricRemoteClient.class), any(PreferencesUtils.class)); - - doReturn(mockSurveyValidator).when(wootric_1).buildSurveyValidator(eq(wootric_1.user), - eq(wootric_1.endUser), eq(wootric_1.settings), any(WootricRemoteClient.class), any(PreferencesUtils.class)); - - - doReturn(mockSurveyManager).when(wootric).buildSurveyManager(eq(wootric.weakFragmentActivity.get()), - any(WootricRemoteClient.class), eq(wootric.user), - eq(wootric.endUser), eq(wootric.settings), - any(PreferencesUtils.class), eq(mockSurveyValidator), any(WootricSurveyCallback.class)); - - doReturn(mockSurveyManager).when(wootric_1).buildSurveyManager(eq(wootric_1.weakFragmentActivity.get()), - any(WootricRemoteClient.class), eq(wootric_1.user), - eq(wootric_1.endUser), eq(wootric_1.settings), - any(PreferencesUtils.class), eq(mockSurveyValidator), any(WootricSurveyCallback.class)); + doReturn(mockSurveyValidator).when(wootric).buildSurveyValidator(); + doReturn(mockSurveyValidator).when(wootric_1).buildSurveyValidator(); + doReturn(mockSurveyManager).when(wootric).buildSurveyManager(); + doReturn(mockSurveyManager).when(wootric_1).buildSurveyManager(); wootric.permissionsValidator = mockPermissionsValidator; wootric_1.permissionsValidator = mockPermissionsValidator; @@ -255,9 +243,10 @@ public void setsTweeterPage() { wootric.survey(); wootric_1.survey(); - verify(mockSurveyManager, times(2)).start(); - assertThat(wootric.surveyInProgress).isTrue(); - assertThat(wootric_1.surveyInProgress).isTrue(); + verify(mockSurveyManager, times(2)).start(eq(wootric.weakFragmentActivity.get()), + any(WootricRemoteClient.class), eq(wootric.user), + eq(wootric.endUser), eq(wootric.settings), + any(PreferencesUtils.class), any(WootricSurveyCallback.class), eq(mockSurveyValidator)); } @Test public void showSurveyInActivity_startsSurvey() throws Exception { @@ -265,22 +254,10 @@ public void setsTweeterPage() { Wootric wootric = spy(Wootric.init(TEST_FRAGMENT_ACTIVITY, CLIENT_ID, CLIENT_SECRET, ACCOUNT_TOKEN)); Wootric wootric_1 = spy(Wootric.init(TEST_FRAGMENT_ACTIVITY, CLIENT_ID, ACCOUNT_TOKEN)); - doReturn(mockSurveyValidator).when(wootric).buildSurveyValidator(eq(wootric.user), - eq(wootric.endUser), eq(wootric.settings), any(WootricRemoteClient.class), any(PreferencesUtils.class)); - - doReturn(mockSurveyValidator).when(wootric_1).buildSurveyValidator(eq(wootric_1.user), - eq(wootric_1.endUser), eq(wootric_1.settings), any(WootricRemoteClient.class), any(PreferencesUtils.class)); - - - doReturn(mockSurveyManager).when(wootric).buildSurveyManager(eq(wootric.weakFragmentActivity.get()), - any(WootricRemoteClient.class), eq(wootric.user), - eq(wootric.endUser), eq(wootric.settings), - any(PreferencesUtils.class), eq(mockSurveyValidator), any(WootricSurveyCallback.class)); - - doReturn(mockSurveyManager).when(wootric_1).buildSurveyManager(eq(wootric_1.weakFragmentActivity.get()), - any(WootricRemoteClient.class), eq(wootric_1.user), - eq(wootric_1.endUser), eq(wootric_1.settings), - any(PreferencesUtils.class), eq(mockSurveyValidator), any(WootricSurveyCallback.class)); + doReturn(mockSurveyValidator).when(wootric).buildSurveyValidator(); + doReturn(mockSurveyValidator).when(wootric_1).buildSurveyValidator(); + doReturn(mockSurveyManager).when(wootric).buildSurveyManager(); + doReturn(mockSurveyManager).when(wootric_1).buildSurveyManager(); wootric.permissionsValidator = mockPermissionsValidator; wootric_1.permissionsValidator = mockPermissionsValidator; @@ -289,45 +266,10 @@ public void setsTweeterPage() { wootric.showSurveyInActivity(TEST_FRAGMENT_ACTIVITY); wootric_1.showSurveyInActivity(TEST_FRAGMENT_ACTIVITY); - verify(mockSurveyManager, times(2)).start(); - assertThat(wootric.surveyInProgress).isTrue(); - assertThat(wootric_1.surveyInProgress).isTrue(); - } - - @Test - public void doesNotStartSurvey_whenSurveyInProgress() { - Wootric.singleton = null; - Wootric wootric = spy(Wootric.init(TEST_FRAGMENT_ACTIVITY, CLIENT_ID, CLIENT_SECRET, ACCOUNT_TOKEN)); - Wootric wootric_1 = spy(Wootric.init(TEST_FRAGMENT_ACTIVITY, CLIENT_ID, ACCOUNT_TOKEN)); - - doReturn(mockSurveyValidator).when(wootric).buildSurveyValidator(eq(wootric.user), - eq(wootric.endUser), eq(wootric.settings), any(WootricRemoteClient.class), any(PreferencesUtils.class)); - - doReturn(mockSurveyValidator).when(wootric_1).buildSurveyValidator(eq(wootric_1.user), - eq(wootric_1.endUser), eq(wootric_1.settings), any(WootricRemoteClient.class), any(PreferencesUtils.class)); - - - doReturn(mockSurveyManager).when(wootric).buildSurveyManager(eq(wootric.weakFragmentActivity.get()), + verify(mockSurveyManager, times(2)).start(eq(wootric.weakFragmentActivity.get()), any(WootricRemoteClient.class), eq(wootric.user), eq(wootric.endUser), eq(wootric.settings), - any(PreferencesUtils.class), eq(mockSurveyValidator), any(WootricSurveyCallback.class)); - - doReturn(mockSurveyManager).when(wootric_1).buildSurveyManager(eq(wootric_1.weakFragmentActivity.get()), - any(WootricRemoteClient.class), eq(wootric_1.user), - eq(wootric_1.endUser), eq(wootric_1.settings), - any(PreferencesUtils.class), eq(mockSurveyValidator), any(WootricSurveyCallback.class)); - - wootric.permissionsValidator = mockPermissionsValidator; - wootric_1.permissionsValidator = mockPermissionsValidator; - doReturn(true).when(wootric.permissionsValidator).check(); - doReturn(true).when(wootric_1.permissionsValidator).check(); - wootric.surveyInProgress = true; - wootric_1.surveyInProgress = true; - - wootric.survey(); - wootric_1.survey(); - - verify(mockSurveyManager, times(0)).start(); + any(PreferencesUtils.class), any(WootricSurveyCallback.class), eq(mockSurveyValidator)); } @Test @@ -336,34 +278,27 @@ public void doesNotStartSurvey_whenPermissionsValidatorChecksReturnsFalse() { Wootric wootric = spy(Wootric.init(TEST_FRAGMENT_ACTIVITY, CLIENT_ID, CLIENT_SECRET, ACCOUNT_TOKEN)); Wootric wootric_1 = spy(Wootric.init(TEST_FRAGMENT_ACTIVITY, CLIENT_ID, ACCOUNT_TOKEN)); - doReturn(mockSurveyValidator).when(wootric).buildSurveyValidator(eq(wootric.user), - eq(wootric.endUser), eq(wootric.settings), any(WootricRemoteClient.class), any(PreferencesUtils.class)); + doReturn(mockSurveyValidator).when(wootric).buildSurveyValidator(); - doReturn(mockSurveyValidator).when(wootric_1).buildSurveyValidator(eq(wootric_1.user), - eq(wootric_1.endUser), eq(wootric_1.settings), any(WootricRemoteClient.class), any(PreferencesUtils.class)); + doReturn(mockSurveyValidator).when(wootric_1).buildSurveyValidator(); - doReturn(mockSurveyManager).when(wootric).buildSurveyManager(eq(wootric.weakFragmentActivity.get()), - any(WootricRemoteClient.class), eq(wootric.user), - eq(wootric.endUser), eq(wootric.settings), - any(PreferencesUtils.class), eq(mockSurveyValidator), any(WootricSurveyCallback.class)); + doReturn(mockSurveyManager).when(wootric).buildSurveyManager(); - doReturn(mockSurveyManager).when(wootric_1).buildSurveyManager(eq(wootric_1.weakFragmentActivity.get()), - any(WootricRemoteClient.class), eq(wootric_1.user), - eq(wootric_1.endUser), eq(wootric_1.settings), - any(PreferencesUtils.class), eq(mockSurveyValidator), any(WootricSurveyCallback.class)); + doReturn(mockSurveyManager).when(wootric_1).buildSurveyManager(); wootric.permissionsValidator = mockPermissionsValidator; wootric_1.permissionsValidator = mockPermissionsValidator; doReturn(false).when(wootric.permissionsValidator).check(); doReturn(false).when(wootric_1.permissionsValidator).check(); - wootric.surveyInProgress = false; - wootric_1.surveyInProgress = false; wootric.survey(); wootric_1.survey(); - verify(mockSurveyManager, times(0)).start(); + verify(mockSurveyManager, times(0)).start(eq(wootric.weakFragmentActivity.get()), + any(WootricRemoteClient.class), eq(wootric.user), + eq(wootric.endUser), eq(wootric.settings), + any(PreferencesUtils.class), any(WootricSurveyCallback.class), eq(mockSurveyValidator)); } @Test diff --git a/androidsdk/src/test/java/com/wootric/androidsdk/network/tasks/CheckEligibilityTaskTest.java b/androidsdk/src/test/java/com/wootric/androidsdk/network/tasks/CheckEligibilityTaskTest.java index 3690ed3..041c1fa 100644 --- a/androidsdk/src/test/java/com/wootric/androidsdk/network/tasks/CheckEligibilityTaskTest.java +++ b/androidsdk/src/test/java/com/wootric/androidsdk/network/tasks/CheckEligibilityTaskTest.java @@ -1,5 +1,7 @@ package com.wootric.androidsdk.network.tasks; +import android.util.Log; + import com.wootric.androidsdk.SurveyValidator; import com.wootric.androidsdk.network.WootricRemoteClient; import com.wootric.androidsdk.objects.EndUser; @@ -15,6 +17,8 @@ import static com.wootric.androidsdk.TestHelper.testUser; import static com.wootric.androidsdk.TestHelper.testPreferenceUtils; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @RunWith(RobolectricTestRunner.class) @@ -38,4 +42,20 @@ public void testGet_RequestWithProperties() throws Exception { assertTrue(asyncTask.paramsMap.containsKey("properties[pricing_plan]")); } + + @Test + public void testGet_RequestWithEventName() throws Exception { + EndUser endUser = new EndUser(); + Settings settings = new Settings(); + settings.setEventName("test event"); + SurveyValidator surveyValidator = new SurveyValidator(testUser(), endUser, settings, + wootricRemoteClient, testPreferenceUtils()); + + CheckEligibilityTask asyncTask = new CheckEligibilityTask(testUser(), endUser, settings, testPreferenceUtils(), surveyValidator); + asyncTask.execute(); + Robolectric.flushBackgroundThreadScheduler(); + + assertTrue(asyncTask.paramsMap.containsKey("event_name")); + assertThat(asyncTask.paramsMap.get("event_name")).isEqualTo("test event"); + } } diff --git a/build.gradle b/build.gradle index 155fc0f..988ce1f 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ buildscript { google() } dependencies { - classpath 'com.android.tools.build:gradle:3.6.0' + classpath 'com.android.tools.build:gradle:3.6.3' classpath 'com.github.dcendents:android-maven-plugin:1.2' // NOTE: Do not place your application dependencies here; they belong