/
LongListActivityTest.java
153 lines (133 loc) · 5.78 KB
/
LongListActivityTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.testing.espresso.DataAdapterSample;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import android.support.test.espresso.DataInteraction;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.action.ViewActions;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import static android.support.test.espresso.Espresso.onData;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isChecked;
import static android.support.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.is;
/**
* Tests to verify that the behavior of {@link LongListActivity} is correct.
* <p>
* Note that in order to scroll the list you shouldn't use {@link ViewActions#scrollTo()} as
* {@link Espresso#onData(org.hamcrest.Matcher)} handles scrolling.</p>
*
* @see #onRow(String)
*/
@RunWith(AndroidJUnit4.class)
@LargeTest
public class LongListActivityTest {
private static final String TEXT_ITEM_30 = "item: 30";
private static final String TEXT_ITEM_30_SELECTED = "30";
private static final String TEXT_ITEM_60 = "item: 60";
// Match the last item by matching its text.
private static final String LAST_ITEM_ID = "item: 99";
/**
* A JUnit {@link Rule @Rule} to launch your activity under test. This is a replacement
* for {@link ActivityInstrumentationTestCase2}.
* <p>
* Rules are interceptors which are executed for each test method and will run before
* any of your setup code in the {@link Before @Before} method.
* <p>
* {@link ActivityTestRule} will create and launch of the activity for you and also expose
* the activity under test. To get a reference to the activity you can use
* the {@link ActivityTestRule#getActivity()} method.
*/
@Rule
public ActivityTestRule<LongListActivity> mActivityRule = new ActivityTestRule<>(
LongListActivity.class);
/**
* Test that the list is long enough for this sample, the last item shouldn't appear.
*/
@Test
public void lastItem_NotDisplayed() {
// Last item should not exist if the list wasn't scrolled down.
onView(withText(LAST_ITEM_ID)).check(doesNotExist());
}
/**
* Check that the item is created. onData() takes care of scrolling.
*/
@Test
public void list_Scrolls() {
onRow(LAST_ITEM_ID).check(matches(isCompletelyDisplayed()));
}
/**
* Clicks on a row and checks that the activity detected the click.
*/
@Test
public void row_Click() {
// Click on one of the rows.
onRow(TEXT_ITEM_30).onChildView(withId(R.id.rowContentTextView)).perform(click());
// Check that the activity detected the click on the first column.
onView(ViewMatchers.withId(R.id.selection_row_value))
.check(matches(withText(TEXT_ITEM_30_SELECTED)));
}
/**
* Checks that a toggle button is checked after clicking on it.
*/
@Test
public void toggle_Click() {
// Click on a toggle button.
onRow(TEXT_ITEM_30).onChildView(withId(R.id.rowToggleButton)).perform(click());
// Check that the toggle button is checked.
onRow(TEXT_ITEM_30).onChildView(withId(R.id.rowToggleButton)).check(matches(isChecked()));
}
/**
* Make sure that clicking on the toggle button doesn't trigger a click on the row.
*/
@Test
public void toggle_ClickDoesntPropagate() {
// Click on one of the rows.
onRow(TEXT_ITEM_30).onChildView(withId(R.id.rowContentTextView)).perform(click());
// Click on the toggle button, in a different row.
onRow(TEXT_ITEM_60).onChildView(withId(R.id.rowToggleButton)).perform(click());
// Check that the activity didn't detect the click on the first column.
onView(ViewMatchers.withId(R.id.selection_row_value))
.check(matches(withText(TEXT_ITEM_30_SELECTED)));
}
/**
* Uses {@link Espresso#onData(org.hamcrest.Matcher)} to get a reference to a specific row.
* <p>
* Note: A custom matcher can be used to match the content and have more readable code.
* See the Custom Matcher Sample.
* </p>
*
* @param str the content of the field
* @return a {@link DataInteraction} referencing the row
*/
private static DataInteraction onRow(String str) {
return onData(hasEntry(equalTo(LongListActivity.ROW_TEXT), is(str)));
}
}