/
BottomSheetDialogTest.java
324 lines (295 loc) · 12.1 KB
/
BottomSheetDialogTest.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* Copyright (C) 2016 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.google.android.material.bottomsheet;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import android.content.Context;
import android.content.DialogInterface;
import android.os.SystemClock;
import androidx.appcompat.widget.AppCompatTextView;
import android.view.View;
import android.widget.FrameLayout;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.test.espresso.Espresso;
import androidx.test.espresso.UiController;
import androidx.test.espresso.ViewAction;
import androidx.test.espresso.assertion.ViewAssertions;
import androidx.test.espresso.matcher.ViewMatchers;
import androidx.test.filters.MediumTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import com.google.android.material.testapp.BottomSheetDialogActivity;
import com.google.android.material.testapp.R;
import java.util.concurrent.atomic.AtomicBoolean;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@MediumTest
@RunWith(AndroidJUnit4.class)
public class BottomSheetDialogTest {
@Rule
public final ActivityTestRule<BottomSheetDialogActivity> activityTestRule =
new ActivityTestRule<>(BottomSheetDialogActivity.class);
private BottomSheetDialog dialog;
@After
public void tearDown() {
if (dialog != null && dialog.isShowing()) {
// Close the dialog
Espresso.pressBack();
}
}
@Test
public void testBasicDialogSetup() throws Throwable {
showDialog();
// Confirms that the dialog is shown
assertThat(dialog.isShowing(), is(true));
FrameLayout bottomSheet = dialog.findViewById(R.id.design_bottom_sheet);
assertThat(bottomSheet, is(notNullValue()));
BottomSheetBehavior<FrameLayout> behavior = BottomSheetBehavior.from(bottomSheet);
assertThat(behavior.isHideable(), is(true));
assertThat(behavior, is(notNullValue()));
// Modal bottom sheets have auto peek height by default.
assertThat(behavior.getPeekHeight(), is(BottomSheetBehavior.PEEK_HEIGHT_AUTO));
// Click outside the bottom sheet
onView(ViewMatchers.withId(R.id.touch_outside)).perform(click());
// Confirm that the dialog is no longer shown
assertThat(dialog.isShowing(), is(false));
}
@Test
public void testTouchInside() throws Throwable {
showDialog();
// Confirms that the dialog is shown
assertThat(dialog.isShowing(), is(true));
FrameLayout bottomSheet = dialog.findViewById(R.id.design_bottom_sheet);
// The bottom sheet is not clickable
assertNotNull(bottomSheet);
assertThat(bottomSheet.isClickable(), is(false));
// Click on the bottom sheet
onView(ViewMatchers.withId(R.id.design_bottom_sheet)).perform(click());
// Confirm that touch didn't fall through as outside touch
assertThat(dialog.isShowing(), is(true));
}
@Test
public void testClickContent() throws Throwable {
final View.OnClickListener mockListener = mock(View.OnClickListener.class);
showDialog();
// Confirms that the dialog is shown
assertThat(dialog.isShowing(), is(true));
FrameLayout bottomSheet = dialog.findViewById(R.id.design_bottom_sheet);
// Set up an OnClickListener to the content of the bottom sheet
assertNotNull(bottomSheet);
View child = bottomSheet.getChildAt(0);
child.setOnClickListener(mockListener);
// Click on the bottom sheet; since the whole sheet is occupied with its only child, this
// clicks the child
onView(ViewMatchers.withParent(ViewMatchers.withId(R.id.design_bottom_sheet)))
.perform(click());
verify(mockListener, times(1)).onClick(any(View.class));
}
@Test
public void testShortDialog() throws Throwable {
showDialog();
// This ensures that the views are laid out before assertions below
onView(ViewMatchers.withId(R.id.design_bottom_sheet))
.perform(setTallPeekHeight())
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
FrameLayout bottomSheet = dialog.findViewById(R.id.design_bottom_sheet);
CoordinatorLayout coordinator = (CoordinatorLayout) bottomSheet.getParent();
BottomSheetBehavior<FrameLayout> behavior = BottomSheetBehavior.from(bottomSheet);
assertThat(bottomSheet, is(notNullValue()));
assertThat(coordinator, is(notNullValue()));
assertThat(behavior, is(notNullValue()));
// This bottom sheet is shorter than the peek height
assertThat(bottomSheet.getHeight(), is(lessThan(behavior.getPeekHeight())));
// Confirm that the bottom sheet is bottom-aligned
assertThat(bottomSheet.getTop(), is(coordinator.getHeight() - bottomSheet.getHeight()));
}
@Test
public void testNonCancelableDialog() throws Throwable {
showDialog();
dialog.setCancelable(false);
// Click outside the bottom sheet
onView(ViewMatchers.withId(R.id.touch_outside)).perform(click());
FrameLayout bottomSheet = dialog.findViewById(R.id.design_bottom_sheet);
BottomSheetBehavior<FrameLayout> behavior = BottomSheetBehavior.from(bottomSheet);
assertThat(behavior.isHideable(), is(false));
assertThat(dialog.isShowing(), is(true));
activityTestRule.runOnUiThread(
() -> {
dialog.cancel();
assertThat(dialog.isShowing(), is(false));
});
}
@Test
public void testHideBottomSheet() throws Throwable {
final AtomicBoolean canceled = new AtomicBoolean(false);
showDialog();
dialog.setOnCancelListener(dialogInterface -> canceled.set(true));
onView(ViewMatchers.withId(R.id.design_bottom_sheet))
.perform(setState(BottomSheetBehavior.STATE_HIDDEN));
// The dialog should be canceled
long start = System.currentTimeMillis();
while (!canceled.get()) {
SystemClock.sleep(31);
if (System.currentTimeMillis() - start > 3000) {
fail("Timed out while waiting for the dialog to be canceled.");
}
}
}
@Test
@MediumTest
public void testHideThenShow() throws Throwable {
// Hide the bottom sheet and wait for the dialog to be canceled.
final DialogInterface.OnCancelListener onCancelListener =
mock(DialogInterface.OnCancelListener.class);
showDialog();
onView(ViewMatchers.withId(R.id.design_bottom_sheet))
.perform(setShortPeekHeight())
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
dialog.setOnCancelListener(onCancelListener);
onView(ViewMatchers.withId(R.id.design_bottom_sheet))
.perform(setState(BottomSheetBehavior.STATE_HIDDEN));
verify(onCancelListener, timeout(3000)).onCancel(any(DialogInterface.class));
// Reshow the same dialog instance and wait for the bottom sheet to be collapsed.
final BottomSheetBehavior.BottomSheetCallback callback =
mock(BottomSheetBehavior.BottomSheetCallback.class);
BottomSheetBehavior.from(dialog.findViewById(R.id.design_bottom_sheet))
.addBottomSheetCallback(callback);
// Show the same dialog again.
activityTestRule.runOnUiThread(() -> dialog.show());
verify(callback, timeout(3000))
.onStateChanged(any(View.class), eq(BottomSheetBehavior.STATE_SETTLING));
verify(callback, timeout(3000))
.onStateChanged(any(View.class), eq(BottomSheetBehavior.STATE_COLLAPSED));
}
@Test
@MediumTest
public void testHideThenShowNotCollapsible() throws Throwable {
// Hide the bottom sheet and wait for the dialog to be canceled.
final DialogInterface.OnCancelListener onCancelListener =
mock(DialogInterface.OnCancelListener.class);
showDialog();
onView(ViewMatchers.withId(R.id.design_bottom_sheet))
.perform(setTallPeekHeight())
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
dialog.setOnCancelListener(onCancelListener);
onView(ViewMatchers.withId(R.id.design_bottom_sheet))
.perform(setState(BottomSheetBehavior.STATE_HIDDEN));
verify(onCancelListener, timeout(3000)).onCancel(any(DialogInterface.class));
// Reshow the same dialog instance and wait for the bottom sheet to be collapsed.
final BottomSheetBehavior.BottomSheetCallback callback =
mock(BottomSheetBehavior.BottomSheetCallback.class);
BottomSheetBehavior.from(dialog.findViewById(R.id.design_bottom_sheet))
.addBottomSheetCallback(callback);
// Show the same dialog again.
activityTestRule.runOnUiThread(() -> dialog.show());
verify(callback, timeout(3000))
.onStateChanged(any(View.class), eq(BottomSheetBehavior.STATE_SETTLING));
verify(callback, timeout(3000))
.onStateChanged(any(View.class), eq(BottomSheetBehavior.STATE_EXPANDED));
}
private void showDialog() throws Throwable {
activityTestRule.runOnUiThread(
() -> {
Context context = activityTestRule.getActivity();
dialog = new BottomSheetDialog(context);
AppCompatTextView text = new AppCompatTextView(context);
StringBuilder builder = new StringBuilder();
builder.append("It is fine today. ");
text.setText(builder);
dialog.setContentView(text);
dialog.show();
});
}
private static ViewAction setTallPeekHeight() {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return ViewMatchers.isDisplayed();
}
@Override
public String getDescription() {
return "set tall peek height";
}
@Override
public void perform(UiController uiController, View view) {
BottomSheetBehavior<?> behavior = BottomSheetBehavior.from(view);
behavior.setPeekHeight(view.getHeight() + 100);
}
};
}
private static ViewAction setShortPeekHeight() {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return ViewMatchers.isDisplayed();
}
@Override
public String getDescription() {
return "set tall peek height";
}
@Override
public void perform(UiController uiController, View view) {
BottomSheetBehavior<?> behavior = BottomSheetBehavior.from(view);
behavior.setPeekHeight(view.getHeight() / 2);
}
};
}
private static ViewAction setState(@BottomSheetBehavior.State final int state) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isBottomSheet();
}
@Override
public String getDescription() {
return "set state to " + state;
}
@Override
public void perform(UiController uiController, View view) {
BottomSheetBehavior.from(view).setState(state);
}
};
}
private static Matcher<View> isBottomSheet() {
return new TypeSafeMatcher<View>() {
@Override
protected boolean matchesSafely(View view) {
return BottomSheetBehavior.from(view) != null;
}
@Override
public void describeTo(Description description) {
description.appendText("is a bottom sheet");
}
};
}
}