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 * Creates a {@link ViewInteraction} for the floating bar menu item with the given matcher.
59 * @param matcher The matcher for the menu item.
61 public static ViewInteraction onFloatingToolBarItem(Matcher<View> matcher) {
62 return onView(matcher)
63 .inRoot(withDecorView(hasDescendant(withTagValue(is(TAG)))));
67 * Asserts that the floating toolbar is displayed on screen.
69 * @throws AssertionError if the assertion fails
71 public static void assertFloatingToolbarIsDisplayed() {
72 onFloatingToolBar().check(matches(isDisplayed()));
76 * Asserts that the floating toolbar is not displayed on screen.
78 * @throws AssertionError if the assertion fails
80 public static void assertFloatingToolbarIsNotDisplayed() {
82 onFloatingToolBar().check(matches(isDisplayed()));
83 } catch (NoMatchingRootException | NoMatchingViewException | AssertionError e) {
86 throw new AssertionError("Floating toolbar is displayed");
89 private static void toggleOverflow() {
90 final int id = com.android.internal.R.id.overflow;
91 onView(allOf(withId(id), isDisplayed()))
92 .inRoot(withDecorView(hasDescendant(withId(id))))
93 .perform(ViewActions.click());
94 onView(isRoot()).perform(SLEEP);
97 public static void sleepForFloatingToolbarPopup() {
98 onView(isRoot()).perform(SLEEP);
102 * Asserts that the floating toolbar contains the specified item.
104 * @param itemLabel label of the item.
105 * @throws AssertionError if the assertion fails
107 public static void assertFloatingToolbarContainsItem(String itemLabel) {
109 onFloatingToolBar().check(matches(hasDescendant(ViewMatchers.withText(itemLabel))));
110 } catch (AssertionError e) {
113 } catch (NoMatchingViewException | NoMatchingRootException e2) {
114 // No overflow items.
118 onFloatingToolBar().check(matches(hasDescendant(ViewMatchers.withText(itemLabel))));
126 * Asserts that the floating toolbar doesn't contain the specified item.
128 * @param itemLabel label of the item.
129 * @throws AssertionError if the assertion fails
131 public static void assertFloatingToolbarDoesNotContainItem(String itemLabel) {
133 assertFloatingToolbarContainsItem(itemLabel);
134 } catch (AssertionError e) {
137 throw new AssertionError("Floating toolbar contains " + itemLabel);
141 * ViewAction to sleep to wait floating toolbar's animation.
143 private static final ViewAction SLEEP = new ViewAction() {
144 private static final long SLEEP_DURATION = 400;
147 public Matcher<View> getConstraints() {
148 return isDisplayed();
152 public String getDescription() {
153 return "Sleep " + SLEEP_DURATION + " ms.";
157 public void perform(UiController uiController, View view) {
158 uiController.loopMainThreadForAtLeast(SLEEP_DURATION);