|
16 | 16 |
|
17 | 17 | package com.example.android.architecture.blueprints.todoapp.statistics; |
18 | 18 |
|
19 | | -import android.content.Intent; |
20 | 19 | import android.support.test.InstrumentationRegistry; |
| 20 | +import android.support.test.espresso.UiController; |
| 21 | +import android.support.test.espresso.ViewAction; |
| 22 | +import android.support.test.espresso.intent.rule.IntentsTestRule; |
21 | 23 | import android.support.test.rule.ActivityTestRule; |
22 | 24 | import android.support.test.runner.AndroidJUnit4; |
| 25 | +import android.support.v4.view.GravityCompat; |
| 26 | +import android.support.v4.widget.DrawerLayout; |
23 | 27 | import android.test.suitebuilder.annotation.LargeTest; |
| 28 | +import android.view.View; |
24 | 29 |
|
25 | 30 | import com.example.android.architecture.blueprints.todoapp.MainActivity; |
26 | 31 | import com.example.android.architecture.blueprints.todoapp.R; |
27 | 32 | import com.example.android.architecture.blueprints.todoapp.data.FakeTasksRemoteDataSource; |
28 | 33 | import com.example.android.architecture.blueprints.todoapp.data.Task; |
29 | 34 | import com.example.android.architecture.blueprints.todoapp.data.source.TasksRepository; |
30 | 35 |
|
31 | | -import org.junit.Before; |
| 36 | +import org.hamcrest.Matcher; |
32 | 37 | import org.junit.Rule; |
33 | 38 | import org.junit.Test; |
34 | 39 | import org.junit.runner.RunWith; |
35 | 40 |
|
36 | 41 | import static android.support.test.espresso.Espresso.onView; |
| 42 | +import static android.support.test.espresso.action.ViewActions.click; |
37 | 43 | import static android.support.test.espresso.assertion.ViewAssertions.matches; |
| 44 | +import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom; |
38 | 45 | import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; |
| 46 | +import static android.support.test.espresso.matcher.ViewMatchers.withId; |
39 | 47 | import static android.support.test.espresso.matcher.ViewMatchers.withText; |
40 | 48 | import static org.hamcrest.Matchers.containsString; |
41 | 49 |
|
|
47 | 55 | public class StatisticsScreenTest { |
48 | 56 |
|
49 | 57 | /** |
50 | | - * {@link ActivityTestRule} is a JUnit {@link Rule @Rule} to launch your activity under test. |
| 58 | + * {@link IntentsTestRule} is an {@link ActivityTestRule} which inits and releases Espresso |
| 59 | + * Intents before and after each test run. |
51 | 60 | * |
52 | 61 | * <p> |
53 | 62 | * Rules are interceptors which are executed for each test method and are important building |
54 | 63 | * blocks of Junit tests. |
55 | 64 | */ |
56 | 65 | @Rule |
57 | | - public ActivityTestRule<MainActivity> mStatisticsActivityTestRule = |
58 | | - new ActivityTestRule<>(MainActivity.class, true, false); |
| 66 | + public IntentsTestRule<MainActivity> mAddTaskIntentsTestRule = |
| 67 | + new IntentsTestRule<>(MainActivity.class); |
| 68 | + |
| 69 | + @Test |
| 70 | + public void Tasks_ShowsNonEmptyMessage() throws Exception { |
| 71 | + stubTasks(); |
| 72 | + navigateStatisticsScreen(); |
| 73 | + |
| 74 | + // Check that the active and completed tasks text is displayed |
| 75 | + String expectedActiveTaskText = InstrumentationRegistry.getTargetContext() |
| 76 | + .getString(R.string.statistics_active_tasks); |
| 77 | + onView(withText(containsString(expectedActiveTaskText))).check(matches(isDisplayed())); |
| 78 | + String expectedCompletedTaskText = InstrumentationRegistry.getTargetContext() |
| 79 | + .getString(R.string.statistics_completed_tasks); |
| 80 | + onView(withText(containsString(expectedCompletedTaskText))).check(matches(isDisplayed())); |
| 81 | + } |
59 | 82 |
|
60 | 83 | /** |
61 | | - * Setup your test fixture with a fake task id. The {@link MainActivity} is started with |
62 | | - * a particular task id, which is then loaded from the service API. |
63 | | - * |
64 | | - * <p> |
65 | | - * Note that this test runs hermetically and is fully isolated using a fake implementation of |
66 | | - * the service API. This is a great way to make your tests more reliable and faster at the same |
67 | | - * time, since they are isolated from any outside dependencies. |
| 84 | + * Setup the test fixture with a some fake tasks. |
68 | 85 | */ |
69 | | - @Before |
70 | | - public void intentWithStubbedTaskId() { |
| 86 | + public void stubTasks() { |
71 | 87 | // Given some tasks |
72 | 88 | TasksRepository.destroyInstance(); |
73 | 89 | FakeTasksRemoteDataSource.getInstance().addTasks(new Task("Title1", "", false)); |
74 | 90 | FakeTasksRemoteDataSource.getInstance().addTasks(new Task("Title2", "", true)); |
| 91 | + } |
75 | 92 |
|
76 | | - // Lazily start the Activity from the ActivityTestRule |
77 | | - Intent startIntent = new Intent(); |
78 | | - mStatisticsActivityTestRule.launchActivity(startIntent); |
| 93 | + /** |
| 94 | + * Since this is a single Activity version of the app, we cannot launch straight to the |
| 95 | + * Statistics screen (we can't just launch an Activity). |
| 96 | + * We need to manually navigate there from the first screen (the Tasks screen). |
| 97 | + */ |
| 98 | + public void navigateStatisticsScreen() { |
| 99 | + // Open the navigation drawer |
| 100 | + onView(withId(R.id.drawer_layout)).perform(actionOpenDrawer()); |
| 101 | + // Click the Statistics menu item. Note: if the test is failing here due to Espresso not |
| 102 | + // waiting for the navigation drawer animation to complete, turn off animations on your |
| 103 | + // test device as outlined in the Espresso setup guide: |
| 104 | + // https://google.github.io/android-testing-support-library/docs/espresso/setup/index.html#setup-your-test-environment |
| 105 | + onView(withText(R.string.statistics_title)).perform(click()); |
79 | 106 | } |
80 | 107 |
|
81 | | - @Test |
82 | | - public void Tasks_ShowsNonEmptyMessage() throws Exception { |
83 | | - // Check that the active and completed tasks text is displayed |
84 | | - String expectedActiveTaskText = InstrumentationRegistry.getTargetContext() |
85 | | - .getString(R.string.statistics_active_tasks); |
86 | | - onView(withText(containsString(expectedActiveTaskText))).check(matches(isDisplayed())); |
87 | | - String expectedCompletedTaskText = InstrumentationRegistry.getTargetContext() |
88 | | - .getString(R.string.statistics_completed_tasks); |
89 | | - onView(withText(containsString(expectedCompletedTaskText))).check(matches(isDisplayed())); |
| 108 | + /** |
| 109 | + * A custom Espresso action used to open the navigation drawer. |
| 110 | + */ |
| 111 | + private static ViewAction actionOpenDrawer() { |
| 112 | + return new ViewAction() { |
| 113 | + @Override |
| 114 | + public Matcher<View> getConstraints() { |
| 115 | + return isAssignableFrom(DrawerLayout.class); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public String getDescription() { |
| 120 | + return "open drawer"; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void perform(UiController uiController, View view) { |
| 125 | + ((DrawerLayout) view).openDrawer(GravityCompat.START); |
| 126 | + } |
| 127 | + }; |
90 | 128 | } |
91 | 129 | } |
0 commit comments