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