/
BottomSheetDragHandleTest.java
246 lines (196 loc) · 8.45 KB
/
BottomSheetDragHandleTest.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
/*
* Copyright 2022 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 com.google.android.material.test.R;
import static android.content.Context.ACCESSIBILITY_SERVICE;
import static com.google.common.truth.Truth.assertThat;
import static org.robolectric.Shadows.shadowOf;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.accessibility.AccessibilityManager;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.ViewCompat;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.platform.app.InstrumentationRegistry;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class BottomSheetDragHandleTest {
private AccessibilityManager accessibilityManager;
private TestActivity activity;
private BottomSheetDragHandleView dragHandleView;
@Before
public void setUp() throws Exception {
accessibilityManager =
(AccessibilityManager)
ApplicationProvider.getApplicationContext().getSystemService(ACCESSIBILITY_SERVICE);
activity = Robolectric.buildActivity(TestActivity.class).setup().get();
dragHandleView = new BottomSheetDragHandleView(activity);
}
@Test
public void test_notInteractableWhenDetachedAndAccessibilityDisabled() {
assertImportantForAccessibility(false);
assertThat(dragHandleView.isClickable()).isFalse();
}
@Test
public void test_notInteractableWhenDetachedAndAccessibilityEnabled() {
shadowOf(accessibilityManager).setEnabled(true);
assertImportantForAccessibility(false);
assertThat(dragHandleView.isClickable()).isFalse();
}
@Test
public void test_notInteractableWhenAttachedAndAccessibilityDisabled() {
activity.addViewToBottomSheet(dragHandleView);
assertImportantForAccessibility(true);
assertThat(dragHandleView.isClickable()).isFalse();
}
@Test
public void test_notInteractableWhenNotAttachedToBottomSheetAndAccessibilityEnabled() {
activity.addViewToContainer(dragHandleView);
shadowOf(accessibilityManager).setEnabled(true);
assertImportantForAccessibility(false);
assertThat(dragHandleView.isClickable()).isFalse();
}
@Test
public void test_interactableWhenAttachedAndAccessibilityEnabled() {
activity.addViewToBottomSheet(dragHandleView);
shadowOf(accessibilityManager).setEnabled(true);
assertImportantForAccessibility(true);
assertThat(dragHandleView.isClickable()).isTrue();
}
@Test
public void test_expandCollapsedBottomSheetWhenClicked() {
activity.bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
activity.addViewToBottomSheet(dragHandleView);
shadowOf(accessibilityManager).setEnabled(true);
dragHandleView.performClick();
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
assertThat(activity.bottomSheetBehavior.getState())
.isEqualTo(BottomSheetBehavior.STATE_EXPANDED);
}
@Test
public void test_collapseExpandedBottomSheetWhenClicked() {
activity.bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
activity.addViewToBottomSheet(dragHandleView);
shadowOf(accessibilityManager).setEnabled(true);
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
dragHandleView.performClick();
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
assertThat(activity.bottomSheetBehavior.getState())
.isEqualTo(BottomSheetBehavior.STATE_COLLAPSED);
}
@Test
public void test_collapsedBottomSheetMoveToHalfExpanded_whenClickedAndFitToContentsFalse() {
activity.bottomSheetBehavior.setFitToContents(false);
activity.bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
activity.addViewToBottomSheet(dragHandleView);
shadowOf(accessibilityManager).setEnabled(true);
dragHandleView.performClick();
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
assertThat(activity.bottomSheetBehavior.getState())
.isEqualTo(BottomSheetBehavior.STATE_HALF_EXPANDED);
}
@Test
public void test_expandedBottomSheetMoveToHalfExpanded_whenClickedAndFitToContentsFalse() {
activity.bottomSheetBehavior.setFitToContents(false);
activity.bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
activity.addViewToBottomSheet(dragHandleView);
shadowOf(accessibilityManager).setEnabled(true);
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
dragHandleView.performClick();
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
assertThat(activity.bottomSheetBehavior.getState())
.isEqualTo(BottomSheetBehavior.STATE_HALF_EXPANDED);
}
@Test
public void test_halfExpandedBottomSheetMoveToExpanded_whenPreviouslyCollapsed() {
activity.bottomSheetBehavior.setFitToContents(false);
activity.bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
activity.addViewToBottomSheet(dragHandleView);
shadowOf(accessibilityManager).setEnabled(true);
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
dragHandleView.performClick();
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
dragHandleView.performClick();
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
assertThat(activity.bottomSheetBehavior.getState())
.isEqualTo(BottomSheetBehavior.STATE_EXPANDED);
}
@Test
public void test_halfExpandedBottomSheetMoveToCollapsed_whenPreviouslyExpanded() {
activity.bottomSheetBehavior.setFitToContents(false);
activity.bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
activity.addViewToBottomSheet(dragHandleView);
shadowOf(accessibilityManager).setEnabled(true);
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
dragHandleView.performClick();
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
dragHandleView.performClick();
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
assertThat(activity.bottomSheetBehavior.getState())
.isEqualTo(BottomSheetBehavior.STATE_COLLAPSED);
}
private void assertImportantForAccessibility(boolean important) {
if (important) {
assertThat(ViewCompat.getImportantForAccessibility(dragHandleView))
.isEqualTo(ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
} else {
assertThat(ViewCompat.getImportantForAccessibility(dragHandleView))
.isEqualTo(ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO);
}
}
private static class TestActivity extends AppCompatActivity {
@Nullable
private CoordinatorLayout container;
@Nullable
private FrameLayout bottomSheet;
@NonNull
private final BottomSheetBehavior<View> bottomSheetBehavior = new BottomSheetBehavior<>();
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setTheme(R.style.Theme_Material3_Light_NoActionBar);
container = new CoordinatorLayout(this);
setContentView(
container, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
bottomSheet = new FrameLayout(this);
CoordinatorLayout.LayoutParams layoutParams =
new CoordinatorLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layoutParams.setBehavior(bottomSheetBehavior);
container.addView(bottomSheet, layoutParams);
}
void addViewToContainer(View view) {
if (container == null) {
return;
}
container.addView(view);
}
void addViewToBottomSheet(View view) {
if (bottomSheet == null) {
return;
}
bottomSheet.addView(view);
}
}
}