Skip to content

Commit b191874

Browse files
committed
Fix TaskDetailScreenTest
1 parent 9926571 commit b191874

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

todoapp/app/src/androidTestMock/java/com/example/android/architecture/blueprints/todoapp/taskdetail/TaskDetailScreenTest.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.junit.runner.RunWith;
3535

3636
import static android.support.test.espresso.Espresso.onView;
37+
import static android.support.test.espresso.action.ViewActions.click;
3738
import static android.support.test.espresso.assertion.ViewAssertions.matches;
3839
import static android.support.test.espresso.matcher.ViewMatchers.isChecked;
3940
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
@@ -77,34 +78,33 @@ public class TaskDetailScreenTest {
7778
@Rule
7879
public ActivityTestRule<MainActivity> mMainActivityTestRule =
7980
new ActivityTestRule<>(MainActivity.class, true /* Initial touch mode */,
80-
false /* Lazily launch activity */);
81+
false /* Lazily launch activity */);
8182

8283
private void loadActiveTask() {
83-
startActivityWithWithStubbedTask(ACTIVE_TASK);
84+
startWithStubbedTask(ACTIVE_TASK);
8485
}
8586

8687
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());
8891
}
8992

9093
/**
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.
9896
*/
99-
private void startActivityWithWithStubbedTask(Task task) {
97+
private void startWithStubbedTask(Task task) {
10098
// Add a task stub to the fake service api layer.
10199
TasksRepository.destroyInstance();
100+
FakeTasksRemoteDataSource.getInstance().deleteAllTasks();
102101
FakeTasksRemoteDataSource.getInstance().addTasks(task);
103102

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());
108108
}
109109

110110
@Test
@@ -143,5 +143,4 @@ public void orientationChange_menuAndTaskPersist() {
143143
// Check delete menu item is displayed and is unique
144144
onView(withId(R.id.menu_delete)).check(matches(isDisplayed()));
145145
}
146-
147146
}

0 commit comments

Comments
 (0)