2 * Copyright (C) 2015 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
17 package android.widget.espresso;
19 import static android.support.test.espresso.Espresso.onView;
20 import static android.support.test.espresso.assertion.ViewAssertions.matches;
21 import static android.support.test.espresso.matcher.RootMatchers.withDecorView;
22 import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant;
23 import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
24 import static android.support.test.espresso.matcher.ViewMatchers.isRoot;
25 import static android.support.test.espresso.matcher.ViewMatchers.withTagValue;
26 import static android.support.test.espresso.matcher.ViewMatchers.withId;
27 import static org.hamcrest.Matchers.allOf;
28 import static org.hamcrest.Matchers.is;
30 import org.hamcrest.Matcher;
32 import android.support.test.espresso.NoMatchingRootException;
33 import android.support.test.espresso.NoMatchingViewException;
34 import android.support.test.espresso.UiController;
35 import android.support.test.espresso.ViewAction;
36 import android.support.test.espresso.ViewInteraction;
37 import android.support.test.espresso.action.ViewActions;
38 import android.support.test.espresso.matcher.ViewMatchers;
39 import android.view.View;
41 import com.android.internal.widget.FloatingToolbar;
44 * Espresso utility methods for the floating toolbar.
46 public class FloatingToolbarEspressoUtils {
47 private final static Object TAG = FloatingToolbar.FLOATING_TOOLBAR_TAG;
49 private FloatingToolbarEspressoUtils() {}
51 private static ViewInteraction onFloatingToolBar() {
52 return onView(withTagValue(is(TAG)))
53 .inRoot(withDecorView(hasDescendant(withTagValue(is(TAG)))));
57 * Asserts that the floating toolbar is displayed on screen.
59 * @throws AssertionError if the assertion fails
61 public static void assertFloatingToolbarIsDisplayed() {
62 onFloatingToolBar().check(matches(isDisplayed()));
66 * Asserts that the floating toolbar is not displayed on screen.
68 * @throws AssertionError if the assertion fails
70 public static void assertFloatingToolbarIsNotDisplayed() {
72 onFloatingToolBar().check(matches(isDisplayed()));
73 } catch (NoMatchingRootException | NoMatchingViewException | AssertionError e) {
76 throw new AssertionError("Floating toolbar is displayed");
79 private static void toggleOverflow() {
80 final int id = com.android.internal.R.id.overflow;
81 onView(allOf(withId(id), isDisplayed()))
82 .inRoot(withDecorView(hasDescendant(withId(id))))
83 .perform(ViewActions.click());
84 onView(isRoot()).perform(SLEEP);
87 public static void sleepForFloatingToolbarPopup() {
88 onView(isRoot()).perform(SLEEP);
92 * Asserts that the floating toolbar contains the specified item.
94 * @param itemLabel label of the item.
95 * @throws AssertionError if the assertion fails
97 public static void assertFloatingToolbarContainsItem(String itemLabel) {
99 onFloatingToolBar().check(matches(hasDescendant(ViewMatchers.withText(itemLabel))));
100 } catch (AssertionError e) {
103 } catch (NoMatchingViewException | NoMatchingRootException e2) {
104 // No overflow items.
108 onFloatingToolBar().check(matches(hasDescendant(ViewMatchers.withText(itemLabel))));
116 * Asserts that the floating toolbar doesn't contain the specified item.
118 * @param itemLabel label of the item.
119 * @throws AssertionError if the assertion fails
121 public static void assertFloatingToolbarDoesNotContainItem(String itemLabel) {
123 assertFloatingToolbarContainsItem(itemLabel);
124 } catch (AssertionError e) {
127 throw new AssertionError("Floating toolbar contains " + itemLabel);
131 * ViewAction to sleep to wait floating toolbar's animation.
133 private static final ViewAction SLEEP = new ViewAction() {
134 private static final long SLEEP_DURATION = 400;
137 public Matcher<View> getConstraints() {
138 return isDisplayed();
142 public String getDescription() {
143 return "Sleep " + SLEEP_DURATION + " ms.";
147 public void perform(UiController uiController, View view) {
148 uiController.loopMainThreadForAtLeast(SLEEP_DURATION);