|
34 | 34 | import org.junit.runner.RunWith; |
35 | 35 |
|
36 | 36 | import static android.support.test.espresso.Espresso.onView; |
| 37 | +import static android.support.test.espresso.action.ViewActions.click; |
37 | 38 | import static android.support.test.espresso.assertion.ViewAssertions.matches; |
38 | 39 | import static android.support.test.espresso.matcher.ViewMatchers.isChecked; |
39 | 40 | import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; |
@@ -77,34 +78,33 @@ public class TaskDetailScreenTest { |
77 | 78 | @Rule |
78 | 79 | public ActivityTestRule<MainActivity> mMainActivityTestRule = |
79 | 80 | new ActivityTestRule<>(MainActivity.class, true /* Initial touch mode */, |
80 | | - false /* Lazily launch activity */); |
| 81 | + false /* Lazily launch activity */); |
81 | 82 |
|
82 | 83 | private void loadActiveTask() { |
83 | | - startActivityWithWithStubbedTask(ACTIVE_TASK); |
| 84 | + startWithStubbedTask(ACTIVE_TASK); |
84 | 85 | } |
85 | 86 |
|
86 | 87 | private void loadCompletedTask() { |
87 | | - startActivityWithWithStubbedTask(COMPLETED_TASK); |
| 88 | + startWithStubbedTask(COMPLETED_TASK); |
| 89 | + // click the task to navigate to it |
| 90 | + onView(withText(COMPLETED_TASK.getTitle())).perform(click()); |
88 | 91 | } |
89 | 92 |
|
90 | 93 | /** |
91 | | - * Setup your test fixture with a fake task id. The {@link MainActivity} is started with |
92 | | - * a particular task id, which is then loaded from the service API. |
93 | | - * |
94 | | - * <p> |
95 | | - * Note that this test runs hermetically and is fully isolated using a fake implementation of |
96 | | - * the service API. This is a great way to make your tests more reliable and faster at the same |
97 | | - * time, since they are isolated from any outside dependencies. |
| 94 | + * Setup your test fixture with a fake task already initialised. |
| 95 | + * The {@link MainActivity} is started after the fake service API has been configured. |
98 | 96 | */ |
99 | | - private void startActivityWithWithStubbedTask(Task task) { |
| 97 | + private void startWithStubbedTask(Task task) { |
100 | 98 | // Add a task stub to the fake service api layer. |
101 | 99 | TasksRepository.destroyInstance(); |
| 100 | + FakeTasksRemoteDataSource.getInstance().deleteAllTasks(); |
102 | 101 | FakeTasksRemoteDataSource.getInstance().addTasks(task); |
103 | 102 |
|
104 | | - // Lazily start the Activity from the ActivityTestRule this time to inject the start Intent |
105 | | - Intent startIntent = new Intent(); |
106 | | -// startIntent.putExtra(MainActivity.EXTRA_TASK_ID, task.getId()); |
107 | | - mMainActivityTestRule.launchActivity(startIntent); |
| 103 | + // Lazily start the Activity from the ActivityTestRule |
| 104 | + mMainActivityTestRule.launchActivity(new Intent()); |
| 105 | + |
| 106 | + // click the task in the Tasks screen to navigate to it |
| 107 | + onView(withText(task.getTitle())).perform(click()); |
108 | 108 | } |
109 | 109 |
|
110 | 110 | @Test |
@@ -143,5 +143,4 @@ public void orientationChange_menuAndTaskPersist() { |
143 | 143 | // Check delete menu item is displayed and is unique |
144 | 144 | onView(withId(R.id.menu_delete)).check(matches(isDisplayed())); |
145 | 145 | } |
146 | | - |
147 | 146 | } |
0 commit comments