|
15 | 15 | */
|
16 | 16 | package com.optimizely.ab.android.test_app;
|
17 | 17 |
|
| 18 | +import android.app.AlarmManager; |
18 | 19 | import android.content.Context;
|
| 20 | +import android.content.Intent; |
19 | 21 | import android.support.test.InstrumentationRegistry;
|
20 | 22 | import android.support.test.espresso.Espresso;
|
21 | 23 | import android.support.test.espresso.idling.CountingIdlingResource;
|
22 | 24 | import android.support.test.filters.LargeTest;
|
23 | 25 | import android.support.test.rule.ActivityTestRule;
|
24 | 26 | import android.support.test.runner.AndroidJUnit4;
|
| 27 | +import android.util.Pair; |
| 28 | + |
| 29 | +import com.optimizely.ab.android.event_handler.EventIntentService; |
| 30 | +import com.optimizely.ab.android.sdk.DataFileService; |
| 31 | +import com.optimizely.ab.android.shared.CountingIdlingResourceManager; |
| 32 | +import com.optimizely.ab.android.shared.ServiceScheduler; |
25 | 33 |
|
26 |
| -import org.junit.After; |
27 |
| -import org.junit.Before; |
28 | 34 | import org.junit.Rule;
|
29 | 35 | import org.junit.Test;
|
| 36 | +import org.junit.rules.ExternalResource; |
| 37 | +import org.junit.rules.RuleChain; |
| 38 | +import org.junit.rules.TestRule; |
30 | 39 | import org.junit.runner.RunWith;
|
| 40 | +import org.slf4j.LoggerFactory; |
| 41 | + |
| 42 | +import java.util.Iterator; |
| 43 | +import java.util.List; |
31 | 44 |
|
32 | 45 | import static android.support.test.espresso.Espresso.onView;
|
| 46 | +import static android.support.test.espresso.action.ViewActions.click; |
33 | 47 | import static android.support.test.espresso.assertion.ViewAssertions.matches;
|
34 | 48 | import static android.support.test.espresso.matcher.ViewMatchers.withId;
|
35 | 49 | import static android.support.test.espresso.matcher.ViewMatchers.withText;
|
| 50 | +import static junit.framework.Assert.assertFalse; |
| 51 | +import static junit.framework.Assert.assertTrue; |
36 | 52 |
|
37 | 53 | @RunWith(AndroidJUnit4.class)
|
38 | 54 | @LargeTest
|
39 | 55 | public class MainActivityEspressoTest {
|
40 | 56 |
|
41 |
| - @Rule |
42 |
| - public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class); |
43 |
| - private Context context; |
| 57 | + private Context context = InstrumentationRegistry.getTargetContext(); |
44 | 58 | private CountingIdlingResource countingIdlingResource;
|
| 59 | + private ServiceScheduler serviceScheduler; |
| 60 | + private Intent dataFileServiceIntent, eventIntentService; |
45 | 61 |
|
46 |
| - @Before |
47 |
| - public void setupStorage() { |
48 |
| - // Clear sticky bucketing |
49 |
| - context = InstrumentationRegistry.getTargetContext(); |
50 |
| - context.deleteFile("optly-user-experiment-record-7664231436.json"); |
51 |
| - context.deleteFile("optly-data-file-7664231436.json"); |
52 |
| - // Set the user's id to the test user that is in the whitelist. |
53 |
| - context.getSharedPreferences("user", Context.MODE_PRIVATE).edit() |
54 |
| - .putString("userId", "test_user") |
55 |
| - .apply(); |
56 |
| - } |
| 62 | + private ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(MainActivity.class); |
| 63 | + @Rule public TestRule chain = RuleChain |
| 64 | + .outerRule(new ExternalResource() { |
| 65 | + @Override |
| 66 | + protected void before() throws Throwable { |
| 67 | + super.before(); |
| 68 | + countingIdlingResource = CountingIdlingResourceManager.getIdlingResource(); |
| 69 | + // To prove that the test fails, omit this call: |
| 70 | + Espresso.registerIdlingResources(countingIdlingResource); |
| 71 | + } |
57 | 72 |
|
| 73 | + @Override |
| 74 | + protected void after() { |
| 75 | + super.after(); |
| 76 | + Espresso.unregisterIdlingResources(countingIdlingResource); |
| 77 | + } |
| 78 | + }) |
| 79 | + .around(new ExternalResource() { |
| 80 | + @Override |
| 81 | + protected void before() throws Throwable { |
| 82 | + super.before(); |
| 83 | + // Set the user's id to the test user that is in the whitelist. |
| 84 | + context.getSharedPreferences("user", Context.MODE_PRIVATE).edit() |
| 85 | + .putString("userId", "test_user") |
| 86 | + .apply(); |
| 87 | + } |
58 | 88 |
|
59 |
| - @Before |
60 |
| - public void registerIdlingResource() { |
61 |
| - countingIdlingResource = activityRule.getActivity().getIdlingResource(); |
62 |
| - // To prove that the test fails, omit this call: |
63 |
| - Espresso.registerIdlingResources(countingIdlingResource); |
64 |
| - } |
| 89 | + @Override |
| 90 | + protected void after() { |
| 91 | + super.after(); |
| 92 | + context.getSharedPreferences("user", Context.MODE_PRIVATE).edit().apply(); |
| 93 | + // Clear sticky bucketing |
| 94 | + context.deleteFile(String.format("optly-user-experiment-record-%s.json", MyApplication.PROJECT_ID)); |
| 95 | + } |
| 96 | + }) |
| 97 | + .around(new ExternalResource() { |
| 98 | + @Override |
| 99 | + protected void before() throws Throwable { |
| 100 | + super.before(); |
| 101 | + |
| 102 | + dataFileServiceIntent = new Intent(context, DataFileService.class); |
| 103 | + dataFileServiceIntent.putExtra(DataFileService.EXTRA_PROJECT_ID, MyApplication.PROJECT_ID); |
| 104 | + |
| 105 | + eventIntentService = new Intent(context, EventIntentService.class); |
| 106 | + eventIntentService.putExtra(DataFileService.EXTRA_PROJECT_ID, MyApplication.PROJECT_ID); |
| 107 | + |
| 108 | + Context applicationContext = context.getApplicationContext(); |
| 109 | + ServiceScheduler.PendingIntentFactory pendingIntentFactory = new ServiceScheduler.PendingIntentFactory(applicationContext); |
| 110 | + AlarmManager alarmManager = (AlarmManager) applicationContext.getSystemService(Context.ALARM_SERVICE); |
| 111 | + serviceScheduler = new ServiceScheduler(alarmManager, pendingIntentFactory, LoggerFactory.getLogger(ServiceScheduler.class)); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + protected void after() { |
| 116 | + super.after(); |
| 117 | + serviceScheduler.unschedule(dataFileServiceIntent); |
| 118 | + assertFalse(serviceScheduler.isScheduled(dataFileServiceIntent)); |
| 119 | + assertFalse(serviceScheduler.isScheduled(eventIntentService)); |
| 120 | + } |
| 121 | + }) |
| 122 | + .around(activityTestRule); |
65 | 123 |
|
66 | 124 | @Test
|
67 |
| - public void experimentActivationForWhitelistUser() { |
| 125 | + public void experimentActivationForWhitelistUser() throws InterruptedException { |
68 | 126 | // Check that the text was changed.
|
69 | 127 | // These tests are pointed at a real project.
|
70 | 128 | // The user 'test_user` is in the whitelist for variation 1 for experiment 0 and experiment 1.
|
71 | 129 | onView(withId(R.id.button_1))
|
72 | 130 | .check(matches(withText(context.getString(R.string.button_1_text_var_1))));
|
73 | 131 |
|
74 |
| - |
75 | 132 | // Espresso will wait for Optimizely to start due to the registered idling resources
|
76 | 133 | onView(withId(R.id.text_view_1))
|
77 | 134 | .check(matches(withText(context.getString(R.string.text_view_1_var_1))));
|
78 |
| - } |
79 | 135 |
|
80 |
| - @After |
81 |
| - public void unregisterIdlingResource() { |
82 |
| - if (countingIdlingResource != null) { |
83 |
| - Espresso.unregisterIdlingResources(countingIdlingResource); |
| 136 | + assertTrue(serviceScheduler.isScheduled(dataFileServiceIntent)); |
| 137 | + |
| 138 | + onView(withId(R.id.button_1)) // withId(R.id.my_view) is a ViewMatcher |
| 139 | + .perform(click()); // click() is a ViewAction |
| 140 | + |
| 141 | + List<Pair<String, String>> events = CountingIdlingResourceManager.getEvents(); |
| 142 | + assertTrue(events.size() == 4); |
| 143 | + Iterator<Pair<String, String>> iterator = events.iterator(); |
| 144 | + while (iterator.hasNext()) { |
| 145 | + Pair<String, String> event = iterator.next(); |
| 146 | + final String url = event.first; |
| 147 | + final String payload = event.second; |
| 148 | + if (url.equals("https://p13nlog.dz.optimizely.com/log/decision") && payload.contains("7676481120") && payload.contains("7661891902") |
| 149 | + || url.equals("https://p13nlog.dz.optimizely.com/log/decision") && payload.contains("7651112186") && payload.contains("7674261140") |
| 150 | + || url.equals("https://p13nlog.dz.optimizely.com/log/event") && payload.contains("experiment_0") |
| 151 | + || url.equals("https://p13nlog.dz.optimizely.com/log/event") && payload.contains("experiment_1")) { |
| 152 | + iterator.remove(); |
| 153 | + } |
84 | 154 | }
|
| 155 | + assertTrue(events.isEmpty()); |
85 | 156 | }
|
86 | 157 | }
|
0 commit comments