Skip to content

Commit 9926571

Browse files
committed
Fix StatisticsScreenTest
1 parent 65d8e77 commit 9926571

File tree

1 file changed

+64
-26
lines changed

1 file changed

+64
-26
lines changed

todoapp/app/src/androidTestMock/java/com/example/android/architecture/blueprints/todoapp/statistics/StatisticsScreenTest.java

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,34 @@
1616

1717
package com.example.android.architecture.blueprints.todoapp.statistics;
1818

19-
import android.content.Intent;
2019
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;
2123
import android.support.test.rule.ActivityTestRule;
2224
import android.support.test.runner.AndroidJUnit4;
25+
import android.support.v4.view.GravityCompat;
26+
import android.support.v4.widget.DrawerLayout;
2327
import android.test.suitebuilder.annotation.LargeTest;
28+
import android.view.View;
2429

2530
import com.example.android.architecture.blueprints.todoapp.MainActivity;
2631
import com.example.android.architecture.blueprints.todoapp.R;
2732
import com.example.android.architecture.blueprints.todoapp.data.FakeTasksRemoteDataSource;
2833
import com.example.android.architecture.blueprints.todoapp.data.Task;
2934
import com.example.android.architecture.blueprints.todoapp.data.source.TasksRepository;
3035

31-
import org.junit.Before;
36+
import org.hamcrest.Matcher;
3237
import org.junit.Rule;
3338
import org.junit.Test;
3439
import org.junit.runner.RunWith;
3540

3641
import static android.support.test.espresso.Espresso.onView;
42+
import static android.support.test.espresso.action.ViewActions.click;
3743
import static android.support.test.espresso.assertion.ViewAssertions.matches;
44+
import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
3845
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
46+
import static android.support.test.espresso.matcher.ViewMatchers.withId;
3947
import static android.support.test.espresso.matcher.ViewMatchers.withText;
4048
import static org.hamcrest.Matchers.containsString;
4149

@@ -47,45 +55,75 @@
4755
public class StatisticsScreenTest {
4856

4957
/**
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.
5160
*
5261
* <p>
5362
* Rules are interceptors which are executed for each test method and are important building
5463
* blocks of Junit tests.
5564
*/
5665
@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+
}
5982

6083
/**
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.
6885
*/
69-
@Before
70-
public void intentWithStubbedTaskId() {
86+
public void stubTasks() {
7187
// Given some tasks
7288
TasksRepository.destroyInstance();
7389
FakeTasksRemoteDataSource.getInstance().addTasks(new Task("Title1", "", false));
7490
FakeTasksRemoteDataSource.getInstance().addTasks(new Task("Title2", "", true));
91+
}
7592

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());
79106
}
80107

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+
};
90128
}
91129
}

0 commit comments

Comments
 (0)