Skip to content

Commit e66dd38

Browse files
author
Josh Deffibaugh
committed
Adds assertions to espresso test for user experiment record.
1 parent d605c5b commit e66dd38

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyManager.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import android.support.annotation.Nullable;
3434
import android.support.annotation.RawRes;
3535
import android.support.annotation.RequiresApi;
36+
import android.support.annotation.VisibleForTesting;
3637

3738
import com.optimizely.ab.Optimizely;
3839
import com.optimizely.ab.android.event_handler.OptlyEventHandler;
@@ -66,6 +67,7 @@ public class OptimizelyManager {
6667
@NonNull private final Logger logger;
6768
@Nullable private DataFileServiceConnection dataFileServiceConnection;
6869
@Nullable private OptimizelyStartListener optimizelyStartListener;
70+
@Nullable private UserExperimentRecord userExperimentRecord;
6971

7072
OptimizelyManager(@NonNull String projectId,
7173
@NonNull Long eventHandlerDispatchInterval,
@@ -265,6 +267,7 @@ protected void onPostExecute(UserExperimentRecord userExperimentRecord) {
265267

266268
try {
267269
OptimizelyManager.androidOptimizely = buildOptimizely(context, dataFile, userExperimentRecord);
270+
OptimizelyManager.this.userExperimentRecord = userExperimentRecord;
268271
logger.info("Sending Optimizely instance to listener");
269272

270273
if (optimizelyStartListener != null) {
@@ -295,6 +298,21 @@ private AndroidOptimizely buildOptimizely(@NonNull Context context, @NonNull Str
295298
return new AndroidOptimizely(optimizely, LoggerFactory.getLogger(AndroidOptimizely.class));
296299
}
297300

301+
@VisibleForTesting
302+
public UserExperimentRecord getUserExperimentRecord() {
303+
return userExperimentRecord;
304+
}
305+
306+
private boolean isAndroidVersionSupported() {
307+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
308+
return true;
309+
} else {
310+
logger.warn("Optimizely will not work on this phone. It's Android version {} is less the minimum supported" +
311+
"version {}", Build.VERSION.SDK_INT, Build.VERSION_CODES.ICE_CREAM_SANDWICH);
312+
return false;
313+
}
314+
}
315+
298316
@RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
299317
static class OptlyActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {
300318

@@ -486,14 +504,4 @@ public OptimizelyManager build() {
486504

487505
}
488506
}
489-
490-
private boolean isAndroidVersionSupported() {
491-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
492-
return true;
493-
} else {
494-
logger.warn("Optimizely will not work on this phone. It's Android version {} is less the minimum supported" +
495-
"version {}", Build.VERSION.SDK_INT, Build.VERSION_CODES.ICE_CREAM_SANDWICH);
496-
return false;
497-
}
498-
}
499507
}

test-app/src/androidTest/java/com/optimizely/ab/android/test_app/MainActivityEspressoTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.optimizely.ab.android.sdk.DataFileService;
3131
import com.optimizely.ab.android.shared.CountingIdlingResourceManager;
3232
import com.optimizely.ab.android.shared.ServiceScheduler;
33+
import com.optimizely.ab.bucketing.UserExperimentRecord;
3334

3435
import org.junit.Rule;
3536
import org.junit.Test;
@@ -48,6 +49,7 @@
4849
import static android.support.test.espresso.matcher.ViewMatchers.withId;
4950
import static android.support.test.espresso.matcher.ViewMatchers.withText;
5051
import static junit.framework.Assert.assertFalse;
52+
import static junit.framework.Assert.assertNull;
5153
import static junit.framework.Assert.assertTrue;
5254

5355
@RunWith(AndroidJUnit4.class)
@@ -153,5 +155,10 @@ public void experimentActivationForWhitelistUser() throws InterruptedException {
153155
}
154156
}
155157
assertTrue(events.isEmpty());
158+
MyApplication myApplication = (MyApplication) activityTestRule.getActivity().getApplication();
159+
UserExperimentRecord userExperimentRecord = myApplication.getOptimizelyManager().getUserExperimentRecord();
160+
// Being in the white list should override user experiment record
161+
assertNull(userExperimentRecord.lookup("test_user", "experiment_0"));
162+
assertNull(userExperimentRecord.lookup("test_user", "experiment_1"));
156163
}
157164
}

0 commit comments

Comments
 (0)