/
EspressoWithWindowLayersTest.java
216 lines (187 loc) · 8.36 KB
/
EspressoWithWindowLayersTest.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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.replaceText;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static com.google.common.truth.Truth.assertThat;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import androidx.test.core.app.ActivityScenario;
import androidx.test.espresso.Root;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.LooperMode;
import org.robolectric.integration.axt.R;
/** Test Espresso on Robolectric interoperability for toolbar menus. */
@RunWith(AndroidJUnit4.class)
@LooperMode(LooperMode.Mode.PAUSED)
public class EspressoWithWindowLayersTest {
private static final String TEXT = "Hello World";
/** The touchable popup window gets the event and the button is *not* clicked. */
@Test
public void click_interactivePopupWindow_isNotClicked() {
try (ActivityScenario<EspressoActivity> scenario =
ActivityScenario.launch(EspressoActivity.class)) {
showInteractivePopupAsButtonDropdown(scenario);
onView(withId(R.id.button)).inRoot(new IsBaseApplication()).perform(click());
scenario.onActivity(activity -> assertThat(activity.buttonClicked).isFalse());
}
}
/** The touchable popup window gets the event and the button is *not* clicked. */
@Test
public void click_occludingInteractivePopupWindow_isNotClicked() {
try (ActivityScenario<EspressoActivity> scenario =
ActivityScenario.launch(EspressoActivity.class)) {
showOccludingInteractivePopupAsButtonDropdown(scenario);
onView(withId(R.id.button)).inRoot(new IsBaseApplication()).perform(click());
scenario.onActivity(activity -> assertThat(activity.buttonClicked).isFalse());
}
}
/** The non-touchable popup window does *not* get the event and the button is clicked. */
@Test
public void click_nonInteractivePopupWindow_isClicked() {
try (ActivityScenario<EspressoActivity> scenario =
ActivityScenario.launch(EspressoActivity.class)) {
showNonInteractivePopupAsButtonDropdown(scenario);
onView(withId(R.id.button)).perform(click());
scenario.onActivity(activity -> assertThat(activity.buttonClicked).isTrue());
}
}
/** The focusable popup window gets the event and the text is *not* typed. */
@Test
public void typeText_interactivePopupWindow_textIsNotTyped() {
try (ActivityScenario<EspressoActivity> scenario =
ActivityScenario.launch(EspressoActivity.class)) {
showOccludingInteractivePopupAsButtonDropdown(scenario);
onView(withId(R.id.edit_text)).inRoot(new IsBaseApplication()).perform(typeText(TEXT));
scenario.onActivity(
activity -> {
TextView tv = activity.findViewById(R.id.edit_text);
assertThat(tv.getText().toString()).isNotEqualTo(TEXT);
});
}
}
/** The non-focusable popup window does *not* get the event and the text is typed. */
@Test
public void typeText_nonInteractivePopupWindow_textIsTyped() {
try (ActivityScenario<EspressoActivity> scenario =
ActivityScenario.launch(EspressoActivity.class)) {
showNonInteractivePopupAsButtonDropdown(scenario);
onView(withId(R.id.edit_text)).inRoot(new IsBaseApplication()).perform(typeText(TEXT));
scenario.onActivity(
activity -> {
TextView tv = activity.findViewById(R.id.edit_text);
assertThat(tv.getText().toString()).isEqualTo(TEXT);
});
}
}
/** Replacing text does not depend on events, so the focusable window does not interfere. */
@Test
public void replaceText_interactivePopupWindow_textIsReplaced() {
try (ActivityScenario<EspressoActivity> scenario =
ActivityScenario.launch(EspressoActivity.class)) {
showOccludingInteractivePopupAsButtonDropdown(scenario);
onView(withId(R.id.edit_text)).inRoot(new IsBaseApplication()).perform(replaceText(TEXT));
scenario.onActivity(
activity -> {
TextView tv = activity.findViewById(R.id.edit_text);
assertThat(tv.getText().toString()).isEqualTo(TEXT);
});
}
}
/** Replacing text does not depend on events, so the non-focusable window does not interfere. */
@Test
public void replaceText_nonInteractivePopupWindow_textIsReplaced() {
try (ActivityScenario<EspressoActivity> scenario =
ActivityScenario.launch(EspressoActivity.class)) {
showNonInteractivePopupAsButtonDropdown(scenario);
onView(withId(R.id.edit_text)).perform(replaceText(TEXT));
scenario.onActivity(
activity -> {
TextView tv = activity.findViewById(R.id.edit_text);
assertThat(tv.getText().toString()).isEqualTo(TEXT);
});
}
}
/**
* Shows an occluding touchable and focusable popup window as a drop-down on the button.
*
* <p>The drop-down is shown *over* the button by adjusting the x and y offsets. The position of
* the popup is *not* yet accounted for in the window selection heuristic. If it were, we should
* see different behavior when attempting to click the button underneath.
*/
private static void showOccludingInteractivePopupAsButtonDropdown(
ActivityScenario<EspressoActivity> scenario) {
scenario.onActivity(
activity -> {
View anchor = activity.findViewById(R.id.button);
new PopupWindow(
/* contentView= */ new FrameLayout(activity),
/* width= */ anchor.getWidth() * 2,
/* height= */ anchor.getHeight() * 2,
/* focusable= */ true)
.showAsDropDown(anchor, -anchor.getWidth(), -anchor.getHeight());
});
}
/** Shows a non-occluding touchable and focusable popup window as a drop-down on the button. */
private static void showInteractivePopupAsButtonDropdown(
ActivityScenario<EspressoActivity> scenario) {
scenario.onActivity(
activity -> {
View anchor = activity.findViewById(R.id.button);
new PopupWindow(
/* contentView= */ new FrameLayout(activity),
/* width= */ 10,
/* height= */ 10,
/* focusable= */ true)
.showAsDropDown(anchor);
});
}
/**
* Shows an occluding non-touchable and non-focusable popup window as a drop-down on the button.
*
* <p>The drop-down is shown *over* the button by adjusting the x and y offsets. The position of
* the popup is *not* yet accounted for in the window selection heuristic. If it were, we should
* see different behavior when attempting to click the button underneath.
*/
private static void showNonInteractivePopupAsButtonDropdown(
ActivityScenario<EspressoActivity> scenario) {
scenario.onActivity(
activity -> {
View anchor = activity.findViewById(R.id.button);
PopupWindow popup =
new PopupWindow(
/* contentView= */ new FrameLayout(activity),
/* width= */ anchor.getWidth() * 2,
/* height= */ anchor.getHeight() * 2,
/* focusable= */ false);
popup.setTouchable(false);
popup.showAsDropDown(anchor, -anchor.getWidth(), -anchor.getHeight());
});
}
/**
* Espresso Root matcher for only windows with the base application window type.
*
* <p>This matcher is required as Espresso will dutifully default to finding views in the focused
* window. However, the events are *not* guaranteed to be dispatched in this window. Robolectric
* uses a different heuristic, so forcing this window to be used is good for testing.
*/
static final class IsBaseApplication extends TypeSafeMatcher<Root> {
@Override
public void describeTo(Description description) {
description.appendText("is the base application window");
}
@Override
public boolean matchesSafely(Root root) {
return root.getWindowLayoutParams().get().type
== WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
}
}
}