|
| 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 | +} |
0 commit comments