Skip to content

Commit 38a4966

Browse files
author
Josh Deffibaugh
committed
Sets up espresso tests for MainActivity
1 parent 339ec36 commit 38a4966

File tree

4 files changed

+122
-4
lines changed

4 files changed

+122
-4
lines changed

test-app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ dependencies {
3636
// compile 'com.optimizely.ab:android-sdk:0.1.2-SNAPSHOT'
3737
compile 'com.android.support:appcompat-v7:24.2.1'
3838
compile 'com.android.support:design:24.2.1'
39+
compile "com.android.support.test.espresso:espresso-idling-resource:$espresso_ver"
40+
3941

4042
testCompile "junit:junit:$junit_ver"
4143
testCompile "org.mockito:mockito-core:$mockito_ver"
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2016, Optimizely
3+
* <p/>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p/>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p/>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.optimizely.ab.android.test_app;
17+
18+
import android.content.Context;
19+
import android.support.test.InstrumentationRegistry;
20+
import android.support.test.espresso.Espresso;
21+
import android.support.test.espresso.idling.CountingIdlingResource;
22+
import android.support.test.filters.LargeTest;
23+
import android.support.test.rule.ActivityTestRule;
24+
import android.support.test.runner.AndroidJUnit4;
25+
26+
import org.junit.After;
27+
import org.junit.Before;
28+
import org.junit.Rule;
29+
import org.junit.Test;
30+
import org.junit.runner.RunWith;
31+
32+
import static android.support.test.espresso.Espresso.onView;
33+
import static android.support.test.espresso.assertion.ViewAssertions.matches;
34+
import static android.support.test.espresso.matcher.ViewMatchers.withId;
35+
import static android.support.test.espresso.matcher.ViewMatchers.withText;
36+
37+
@RunWith(AndroidJUnit4.class)
38+
@LargeTest
39+
public class MainActivityEspressoTest {
40+
41+
@Rule
42+
public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class);
43+
private Context context;
44+
private CountingIdlingResource countingIdlingResource;
45+
46+
@Before
47+
public void setupStorage() {
48+
// Clear sticky bucketing
49+
context = InstrumentationRegistry.getTargetContext();
50+
context.deleteFile("optly-user-experiment-record-7664231436.json");
51+
// Set the user's id to the test user that is in the whitelist.
52+
context.getSharedPreferences("user", Context.MODE_PRIVATE).edit()
53+
.putString("userId", "test_user")
54+
.apply();
55+
}
56+
57+
58+
@Before
59+
public void registerIdlingResource() {
60+
countingIdlingResource = activityRule.getActivity().getIdlingResource();
61+
// To prove that the test fails, omit this call:
62+
Espresso.registerIdlingResources(countingIdlingResource);
63+
}
64+
65+
@Test
66+
public void experimentActivationForWhitelistUser() {
67+
// Check that the text was changed.
68+
// These tests are pointed at a real project.
69+
// The user 'test_user` is in the whitelist for variation 1 for experiment 0 and experiment 1.
70+
onView(withId(R.id.button_1))
71+
.check(matches(withText(context.getString(R.string.button_1_text_var_1))));
72+
73+
74+
// Espresso will wait for Optimizely to start due to the registered idling resources
75+
onView(withId(R.id.text_view_1))
76+
.check(matches(withText(context.getString(R.string.text_view_1_var_1))));
77+
}
78+
79+
@After
80+
public void unregisterIdlingResource() {
81+
if (countingIdlingResource != null) {
82+
Espresso.unregisterIdlingResources(countingIdlingResource);
83+
}
84+
}
85+
}

test-app/src/main/java/com/optimizely/ab/android/test_app/MainActivity.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* Copyright 2016, Optimizely
33
* <p/>
44
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,6 +17,11 @@
1717

1818
import android.content.Intent;
1919
import android.os.Bundle;
20+
import android.support.annotation.NonNull;
21+
import android.support.annotation.Nullable;
22+
import android.support.annotation.VisibleForTesting;
23+
import android.support.test.espresso.IdlingResource;
24+
import android.support.test.espresso.idling.CountingIdlingResource;
2025
import android.support.v7.app.AppCompatActivity;
2126
import android.view.View;
2227
import android.widget.Button;
@@ -32,6 +37,10 @@ public class MainActivity extends AppCompatActivity {
3237
private OptimizelyManager optimizelyManager;
3338
private MyApplication myApplication;
3439

40+
41+
// The Idling Resource which will be null in production.
42+
@Nullable private CountingIdlingResource countingIdlingResource;
43+
3544
@Override
3645
protected void onCreate(Bundle savedInstanceState) {
3746
super.onCreate(savedInstanceState);
@@ -74,6 +83,9 @@ public void onClick(View v) {
7483
protected void onStart() {
7584
super.onStart();
7685

86+
if (countingIdlingResource != null) {
87+
countingIdlingResource.increment();
88+
}
7789
optimizelyManager.start(this, new OptimizelyStartListener() {
7890
@Override
7991
public void onStart(AndroidOptimizely optimizely) {
@@ -89,7 +101,22 @@ public void onStart(AndroidOptimizely optimizely) {
89101
} else {
90102
textView1.setText(R.string.text_view_1_default);
91103
}
104+
if (countingIdlingResource != null) {
105+
countingIdlingResource.decrement();
106+
}
92107
}
93108
});
94109
}
110+
111+
/**
112+
* Only called from test, creates and returns a new {@link CountingIdlingResource}.
113+
*/
114+
@VisibleForTesting
115+
@NonNull
116+
public CountingIdlingResource getIdlingResource() {
117+
if (countingIdlingResource == null) {
118+
countingIdlingResource = new CountingIdlingResource("onOptimizelyStart", true);
119+
}
120+
return countingIdlingResource;
121+
}
95122
}

test-app/src/main/res/raw/data_file.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
"key": "variation_2"
2828
}
2929
],
30-
"forcedVariations": {},
30+
"forcedVariations": {
31+
"test_user": "variation_1"
32+
},
3133
"id": "7651112186"
3234
},
3335
{
@@ -55,7 +57,9 @@
5557
"key": "variation_2"
5658
}
5759
],
58-
"forcedVariations": {},
60+
"forcedVariations": {
61+
"test_user": "variation_1"
62+
},
5963
"id": "7676481120"
6064
}
6165
],
@@ -92,5 +96,5 @@
9296
"key": "experiment_1"
9397
}
9498
],
95-
"revision": "10"
99+
"revision": "12"
96100
}

0 commit comments

Comments
 (0)