/
EspressoTest.java
142 lines (115 loc) · 5 KB
/
EspressoTest.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
package org.robolectric.integrationtests.axt;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.pressKey;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.action.ViewActions.typeTextIntoFocusedView;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static com.google.common.truth.Truth.assertThat;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.EditText;
import androidx.test.annotation.UiThreadTest;
import androidx.test.espresso.Espresso;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.ActivityTestRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.integration.axt.R;
/** Simple tests to verify espresso APIs can be used on both Robolectric and device. */
@RunWith(AndroidJUnit4.class)
public final class EspressoTest {
@Rule
public ActivityTestRule<EspressoActivity> activityRule =
new ActivityTestRule<>(EspressoActivity.class, false, true);
@Test
public void onIdle_doesnt_block() throws Exception {
Espresso.onIdle();
}
@Test
public void launchActivityAndFindView_ById() throws Exception {
EspressoActivity activity = activityRule.getActivity();
EditText editText = activity.findViewById(R.id.edit_text);
assertThat(editText).isNotNull();
assertThat(editText.isEnabled()).isTrue();
}
/** Perform the equivalent of launchActivityAndFindView_ById except using espresso APIs */
@Test
public void launchActivityAndFindView_espresso() throws Exception {
onView(withId(R.id.edit_text)).check(matches(isCompletelyDisplayed()));
}
/** Perform the 'traditional' mechanism of clicking a button in Robolectric using findViewById */
@Test
@UiThreadTest
public void buttonClick() throws Exception {
EspressoActivity activity = activityRule.getActivity();
Button button = activity.findViewById(R.id.button);
button.performClick();
assertThat(activity.buttonClicked).isTrue();
}
/** Perform the equivalent of click except using espresso APIs */
@Test
public void buttonClick_espresso() throws Exception {
EspressoActivity activity = activityRule.getActivity();
onView(withId(R.id.button)).check(matches(isCompletelyDisplayed()));
onView(withId(R.id.button)).perform(click());
assertThat(activity.buttonClicked).isTrue();
}
/** Perform the 'traditional' mechanism of setting contents of a text view using findViewById */
@Test
@UiThreadTest
public void typeText_findView() throws Exception {
EspressoActivity activity = activityRule.getActivity();
EditText editText = activity.findViewById(R.id.edit_text);
editText.setText("\"new TEXT!#$%&'*+-/=?^_`{|}~@robolectric.org");
assertThat(editText.getText().toString())
.isEqualTo("\"new TEXT!#$%&'*+-/=?^_`{|}~@robolectric.org");
}
/** Perform the equivalent of setText except using espresso APIs */
@Test
public void typeText_espresso() throws Exception {
onView(withId(R.id.edit_text))
.perform(typeText("\"new TEXT!#$%&'*+-/=?^_`{|}~@robolectric.org"));
onView(withId(R.id.edit_text))
.check(matches(withText("\"new TEXT!#$%&'*+-/=?^_`{|}~@robolectric.org")));
}
/** use typeText with a inputType phone */
@Test
public void typeText_phone() throws Exception {
onView(withId(R.id.edit_text_phone)).perform(typeText("411"));
onView(withId(R.id.edit_text_phone)).check(matches(withText("411")));
}
@Test
public void textView() {
onView(withText("Text View"))
.check(
(view, noViewFoundException) -> {
assertThat(view.getWidth()).isGreaterThan(0);
assertThat(view.getHeight()).isGreaterThan(0);
});
onView(withText("Text View")).check(matches(isCompletelyDisplayed()));
}
@Test
public void customActivityLabel() {
onView(withText("Activity Label")).check(matches(isCompletelyDisplayed()));
}
@Test
public void changeText_withCloseSoftKeyboard() {
// Type text and then press the button.
onView(withId(R.id.edit_text)).perform(typeText("anything"), closeSoftKeyboard());
// Check that the text was changed.
onView(withId(R.id.edit_text)).check(matches(withText("anything")));
}
@Test
public void changeText_addNewline() {
onView(withId(R.id.edit_text)).perform(typeText("Some text."));
onView(withId(R.id.edit_text)).perform(pressKey(KeyEvent.KEYCODE_ENTER));
onView(withId(R.id.edit_text)).perform(typeTextIntoFocusedView("Other text."));
onView(withId(R.id.edit_text)).check(matches(withText("Some text.\nOther text.")));
}
}