-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathFloatingActionButtonTest.java
More file actions
321 lines (281 loc) · 12.2 KB
/
FloatingActionButtonTest.java
File metadata and controls
321 lines (281 loc) · 12.2 KB
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
/*
* 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.floatingactionbutton;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.isRoot;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static com.google.android.material.testutils.DesignViewActions.setVisibility;
import static com.google.android.material.testutils.FloatingActionButtonActions.hideThenShow;
import static com.google.android.material.testutils.FloatingActionButtonActions.setBackgroundTintColor;
import static com.google.android.material.testutils.FloatingActionButtonActions.setBackgroundTintList;
import static com.google.android.material.testutils.FloatingActionButtonActions.setCompatElevation;
import static com.google.android.material.testutils.FloatingActionButtonActions.setCustomSize;
import static com.google.android.material.testutils.FloatingActionButtonActions.setImageResource;
import static com.google.android.material.testutils.FloatingActionButtonActions.setLayoutGravity;
import static com.google.android.material.testutils.FloatingActionButtonActions.setSize;
import static com.google.android.material.testutils.FloatingActionButtonActions.showThenHide;
import static com.google.android.material.testutils.TestUtilsActions.setClickable;
import static com.google.android.material.testutils.TestUtilsActions.setEnabled;
import static com.google.android.material.testutils.TestUtilsActions.setExpanded;
import static com.google.android.material.testutils.TestUtilsActions.setPressed;
import static com.google.android.material.testutils.TestUtilsActions.setSelected;
import static com.google.android.material.testutils.TestUtilsActions.waitFor;
import static com.google.android.material.testutils.TestUtilsMatchers.hasTranslationZ;
import static com.google.android.material.testutils.TestUtilsMatchers.isExpanded;
import static com.google.android.material.testutils.TestUtilsMatchers.isPressed;
import static com.google.android.material.testutils.TestUtilsMatchers.withFabBackgroundFill;
import static com.google.android.material.testutils.TestUtilsMatchers.withFabContentAreaOnMargins;
import static com.google.android.material.testutils.TestUtilsMatchers.withFabContentHeight;
import static com.google.android.material.testutils.TestUtilsMatchers.withFabCustomSize;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import android.app.Activity;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.View;
import androidx.core.content.ContextCompat;
import androidx.test.espresso.action.GeneralSwipeAction;
import androidx.test.espresso.action.Press;
import androidx.test.espresso.action.Swipe;
import androidx.test.filters.LargeTest;
import androidx.test.filters.MediumTest;
import androidx.test.filters.SmallTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import com.google.android.material.testapp.FloatingActionButtonActivity;
import com.google.android.material.testapp.R;
import com.google.android.material.testutils.ActivityUtils;
import com.google.android.material.testutils.TestUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public class FloatingActionButtonTest {
@Rule
public final ActivityTestRule<FloatingActionButtonActivity> activityTestRule =
new ActivityTestRule<>(FloatingActionButtonActivity.class);
@Test
@SmallTest
public void testDefaultBackgroundTint() {
final int colorAccent =
TestUtils.getThemeAttrColor(activityTestRule.getActivity(), R.attr.colorAccent);
onView(withId(R.id.fab_standard)).check(matches(withFabBackgroundFill(colorAccent)));
}
@Test
@SmallTest
public void testSetTintOnDefaultBackgroundTint() {
onView(withId(R.id.fab_standard))
.perform(setBackgroundTintColor(Color.GREEN))
.check(matches(withFabBackgroundFill(Color.GREEN)));
}
@Test
@SmallTest
public void testDeclaredBackgroundTint() {
onView(withId(R.id.fab_tint)).check(matches(withFabBackgroundFill(Color.MAGENTA)));
}
@Test
@SmallTest
public void testSetTintOnDeclaredBackgroundTint() {
onView(withId(R.id.fab_tint))
.perform(setBackgroundTintColor(Color.GREEN))
.check(matches(withFabBackgroundFill(Color.GREEN)));
}
@Test
@SmallTest
public void testSetStatefulTintAcrossStateChanges() {
final Activity activity = activityTestRule.getActivity();
final ColorStateList tint = ContextCompat.getColorStateList(activity, R.color.fab_tint);
final int normal = ContextCompat.getColor(activity, R.color.sand_default);
final int notSelected = ContextCompat.getColor(activity, R.color.sand_disabled);
// First set the background tint list to the ColorStateList
onView(withId(R.id.fab_standard)).perform(setBackgroundTintList(tint));
// Assert that the background is tinted correctly across state changes
onView(withId(R.id.fab_standard))
.perform(setSelected(true))
.check(matches(withFabBackgroundFill(normal)))
.perform(setSelected(false))
.check(matches(withFabBackgroundFill(notSelected)))
.perform(setSelected(true))
.check(matches(withFabBackgroundFill(normal)));
}
@Test
@SmallTest
public void testDeclaredStatefulTintAcrossStateChanges() {
final Activity activity = activityTestRule.getActivity();
final int normal = ContextCompat.getColor(activity, R.color.sand_default);
final int disabled = ContextCompat.getColor(activity, R.color.sand_disabled);
// Assert that the background is tinted correctly across state changes
onView(withId(R.id.fab_state_tint))
.perform(setSelected(true))
.check(matches(withFabBackgroundFill(normal)))
.perform(setSelected(false))
.check(matches(withFabBackgroundFill(disabled)));
}
@Test
@SmallTest
public void setVectorDrawableSrc() {
onView(withId(R.id.fab_standard)).perform(setImageResource(R.drawable.vector_icon));
}
@Test
@SmallTest
public void testSetMiniSize() {
final int miniSize =
activityTestRule
.getActivity()
.getResources()
.getDimensionPixelSize(R.dimen.fab_mini_height);
onView(withId(R.id.fab_standard))
.perform(setSize(FloatingActionButton.SIZE_MINI))
.check(matches(withFabContentHeight(miniSize)));
}
@Test
@SmallTest
public void testSetSizeToggle() {
final int miniSize =
activityTestRule
.getActivity()
.getResources()
.getDimensionPixelSize(R.dimen.fab_mini_height);
final int normalSize =
activityTestRule
.getActivity()
.getResources()
.getDimensionPixelSize(R.dimen.fab_normal_height);
onView(withId(R.id.fab_standard))
.perform(setSize(FloatingActionButton.SIZE_MINI))
.check(matches(withFabContentHeight(miniSize)));
onView(withId(R.id.fab_standard))
.perform(setSize(FloatingActionButton.SIZE_NORMAL))
.check(matches(withFabContentHeight(normalSize)));
}
@Test
@SmallTest
public void testOffset() {
onView(withId(R.id.fab_standard))
.perform(setLayoutGravity(Gravity.LEFT | Gravity.TOP))
.check(matches(withFabContentAreaOnMargins(Gravity.LEFT | Gravity.TOP)));
onView(withId(R.id.fab_standard))
.perform(setLayoutGravity(Gravity.RIGHT | Gravity.BOTTOM))
.check(matches(withFabContentAreaOnMargins(Gravity.RIGHT | Gravity.BOTTOM)));
}
@Test
@SmallTest
public void testHideShow() {
onView(withId(R.id.fab_standard))
.perform(setVisibility(View.VISIBLE))
.perform(hideThenShow())
.check(matches(isDisplayed()));
}
@Test
@MediumTest
public void testShowHide() {
onView(withId(R.id.fab_standard))
.perform(setVisibility(View.GONE))
.perform(showThenHide())
.check(matches(not(isDisplayed())));
}
@Test
@LargeTest
public void testClickableTouchAndDragOffView() {
onView(withId(R.id.fab_standard))
.perform(setClickable(true))
.perform(
new GeneralSwipeAction(
Swipe.SLOW,
view -> {
// Create coordinators that in the center of the FAB's content area
final FloatingActionButton fab = (FloatingActionButton) view;
final int[] xy = new int[2];
fab.getLocationOnScreen(xy);
final Rect rect = new Rect();
fab.getMeasuredContentRect(rect);
return new float[] {xy[0] + rect.centerX(), xy[1] + rect.centerY()};
},
view -> {
// Create coordinators that in the center horizontally, but well
// below the view vertically (by 50% of the height)
final int[] xy = new int[2];
view.getLocationOnScreen(xy);
return new float[] {
xy[0] + (view.getWidth() / 2f), xy[1] + (view.getHeight() * 1.5f)
};
},
Press.FINGER))
.check(matches(not(isPressed())));
}
@Test
@MediumTest
public void testOnClickListener() {
final View.OnClickListener listener = mock(View.OnClickListener.class);
final View view = activityTestRule.getActivity().findViewById(R.id.fab_standard);
view.setOnClickListener(listener);
// Click on the fab
onView(withId(R.id.fab_standard)).perform(click());
// And verify that the listener was invoked once
verify(listener, times(1)).onClick(view);
}
@Test
@SmallTest
public void testSetCompatElevation() {
onView(withId(R.id.fab_standard)).perform(setEnabled(false)).perform(setCompatElevation(0));
}
@Test
@MediumTest
public void testElevationOnPressed() {
onView(withId(R.id.fab_standard)).perform(setPressed(true));
// It's unclear why the 21+ state list animator is not affected by loopMainThreadUntilIdle().
onView(isRoot()).perform(waitFor(1000));
onView(withId(R.id.fab_standard)).check(matches(hasTranslationZ()));
}
@Test
@MediumTest
public void testInstanceState() throws Throwable {
FloatingActionButtonActivity activity = activityTestRule.getActivity();
onView(withId(R.id.fab_standard)).perform(setExpanded(false));
ActivityUtils.recreateActivity(activityTestRule, activity);
ActivityUtils.waitForExecution(activityTestRule);
onView(withId(R.id.fab_standard)).check(matches(not(isExpanded())));
onView(withId(R.id.fab_standard)).perform(setExpanded(true));
ActivityUtils.recreateActivity(activityTestRule, activity);
ActivityUtils.waitForExecution(activityTestRule);
onView(withId(R.id.fab_standard)).check(matches(isExpanded()));
}
@Test
@SmallTest
public void testSetCustomSize() {
onView(withId(R.id.fab_standard))
.perform(setCustomSize(10))
.check(matches(withFabCustomSize(10)));
onView(withId(R.id.fab_standard))
.perform(setCustomSize(20))
.check(matches(withFabCustomSize(20)));
}
@Test
@SmallTest
public void testA11yClassName() {
FloatingActionButton fab = activityTestRule.getActivity().findViewById(R.id.fab_standard);
assertEquals(
FloatingActionButton.ACCESSIBIILTY_FAB_ROLE, fab.getAccessibilityClassName().toString());
}
}