OSDN Git Service

f02fe004e5b86bf0ea952cc4d388f7cd5c4b7e52
[android-x86/frameworks-base.git] / core / tests / coretests / src / android / widget / espresso / FloatingToolbarEspressoUtils.java
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
15  */
16
17 package android.widget.espresso;
18
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;
29
30 import org.hamcrest.Matcher;
31
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;
40
41 import com.android.internal.widget.FloatingToolbar;
42
43 /**
44  * Espresso utility methods for the floating toolbar.
45  */
46 public class FloatingToolbarEspressoUtils {
47     private final static Object TAG = FloatingToolbar.FLOATING_TOOLBAR_TAG;
48
49     private FloatingToolbarEspressoUtils() {}
50
51     private static ViewInteraction onFloatingToolBar() {
52         return onView(withTagValue(is(TAG)))
53                 .inRoot(withDecorView(hasDescendant(withTagValue(is(TAG)))));
54     }
55
56     /**
57      * Asserts that the floating toolbar is displayed on screen.
58      *
59      * @throws AssertionError if the assertion fails
60      */
61     public static void assertFloatingToolbarIsDisplayed() {
62         onFloatingToolBar().check(matches(isDisplayed()));
63     }
64
65     /**
66      * Asserts that the floating toolbar is not displayed on screen.
67      *
68      * @throws AssertionError if the assertion fails
69      */
70     public static void assertFloatingToolbarIsNotDisplayed() {
71         try {
72             onFloatingToolBar().check(matches(isDisplayed()));
73         } catch (NoMatchingRootException | NoMatchingViewException | AssertionError e) {
74             return;
75         }
76         throw new AssertionError("Floating toolbar is displayed");
77     }
78
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);
85     }
86
87     public static void sleepForFloatingToolbarPopup() {
88         onView(isRoot()).perform(SLEEP);
89     }
90
91     /**
92      * Asserts that the floating toolbar contains the specified item.
93      *
94      * @param itemLabel label of the item.
95      * @throws AssertionError if the assertion fails
96      */
97     public static void assertFloatingToolbarContainsItem(String itemLabel) {
98         try{
99             onFloatingToolBar().check(matches(hasDescendant(ViewMatchers.withText(itemLabel))));
100         } catch (AssertionError e) {
101             try{
102                 toggleOverflow();
103             } catch (NoMatchingViewException | NoMatchingRootException e2) {
104                 // No overflow items.
105                 throw e;
106             }
107             try{
108                 onFloatingToolBar().check(matches(hasDescendant(ViewMatchers.withText(itemLabel))));
109             } finally {
110                 toggleOverflow();
111             }
112         }
113     }
114
115     /**
116      * Asserts that the floating toolbar doesn't contain the specified item.
117      *
118      * @param itemLabel label of the item.
119      * @throws AssertionError if the assertion fails
120      */
121     public static void assertFloatingToolbarDoesNotContainItem(String itemLabel) {
122         try{
123             assertFloatingToolbarContainsItem(itemLabel);
124         } catch (AssertionError e) {
125             return;
126         }
127         throw new AssertionError("Floating toolbar contains " + itemLabel);
128     }
129
130     /**
131      * ViewAction to sleep to wait floating toolbar's animation.
132      */
133     private static final ViewAction SLEEP = new ViewAction() {
134         private static final long SLEEP_DURATION = 400;
135
136         @Override
137         public Matcher<View> getConstraints() {
138             return isDisplayed();
139         }
140
141         @Override
142         public String getDescription() {
143             return "Sleep " + SLEEP_DURATION + " ms.";
144         }
145
146         @Override
147         public void perform(UiController uiController, View view) {
148             uiController.loopMainThreadForAtLeast(SLEEP_DURATION);
149         }
150     };
151 }