1 /*
2  * Copyright (C) 2020 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.autofillservice.cts.testcore;
18 
19 import static android.autofillservice.cts.testcore.Timeouts.DATASET_PICKER_NOT_SHOWN_NAPTIME_MS;
20 import static android.autofillservice.cts.testcore.Timeouts.LONG_PRESS_MS;
21 import static android.autofillservice.cts.testcore.Timeouts.UI_TIMEOUT;
22 
23 import android.content.pm.PackageManager;
24 import android.support.test.uiautomator.By;
25 import android.support.test.uiautomator.BySelector;
26 import android.support.test.uiautomator.Direction;
27 import android.support.test.uiautomator.UiObject2;
28 
29 import com.android.compatibility.common.util.RequiredFeatureRule;
30 import com.android.compatibility.common.util.Timeout;
31 import com.android.cts.mockime.MockIme;
32 
33 import org.junit.rules.RuleChain;
34 import org.junit.rules.TestRule;
35 
36 /**
37  * UiBot for the inline suggestion.
38  */
39 public final class InlineUiBot extends UiBot {
40 
41     private static final String TAG = "AutoFillInlineCtsUiBot";
42     public static final String SUGGESTION_STRIP_DESC = "MockIme Inline Suggestion View";
43 
44     private static final BySelector SUGGESTION_STRIP_SELECTOR = By.desc(SUGGESTION_STRIP_DESC);
45 
46     private static final RequiredFeatureRule REQUIRES_IME_RULE = new RequiredFeatureRule(
47             PackageManager.FEATURE_INPUT_METHODS);
48 
InlineUiBot()49     public InlineUiBot() {
50         this(UI_TIMEOUT);
51     }
52 
InlineUiBot(Timeout defaultTimeout)53     public InlineUiBot(Timeout defaultTimeout) {
54         super(defaultTimeout);
55     }
56 
annotateRule(TestRule rule)57     public static RuleChain annotateRule(TestRule rule) {
58         return RuleChain.outerRule(REQUIRES_IME_RULE).around(rule);
59     }
60 
61     @Override
assertNoDatasets()62     public void assertNoDatasets() throws Exception {
63         assertNoDatasetsEver();
64     }
65 
66     @Override
assertNoDatasetsEver()67     public void assertNoDatasetsEver() throws Exception {
68         assertNeverShown("suggestion strip", SUGGESTION_STRIP_SELECTOR,
69                 DATASET_PICKER_NOT_SHOWN_NAPTIME_MS);
70     }
71 
72     /**
73      * Selects the suggestion in the {@link MockIme}'s suggestion strip by the given text.
74      */
selectSuggestion(String name)75     public void selectSuggestion(String name) throws Exception {
76         final UiObject2 strip = findSuggestionStrip(UI_TIMEOUT);
77         final UiObject2 dataset = strip.findObject(By.text(name));
78         if (dataset == null) {
79             throw new AssertionError("no dataset " + name + " in " + getChildrenAsText(strip));
80         }
81         dataset.click();
82     }
83 
84     @Override
selectDataset(String name)85     public void selectDataset(String name) throws Exception {
86         selectSuggestion(name);
87     }
88 
89     @Override
longPressSuggestion(String name)90     public void longPressSuggestion(String name) throws Exception {
91         final UiObject2 strip = findSuggestionStrip(UI_TIMEOUT);
92         final UiObject2 dataset = strip.findObject(By.text(name));
93         if (dataset == null) {
94             throw new AssertionError("no dataset " + name + " in " + getChildrenAsText(strip));
95         }
96         dataset.click(LONG_PRESS_MS);
97     }
98 
99     @Override
assertDatasets(String...names)100     public UiObject2 assertDatasets(String...names) throws Exception {
101         final UiObject2 picker = findSuggestionStrip(UI_TIMEOUT);
102         return assertDatasets(picker, names);
103     }
104 
105     @Override
assertSuggestion(String name)106     public void assertSuggestion(String name) throws Exception {
107         final UiObject2 strip = findSuggestionStrip(UI_TIMEOUT);
108         final UiObject2 dataset = strip.findObject(By.text(name));
109         if (dataset == null) {
110             throw new AssertionError("no dataset " + name + " in " + getChildrenAsText(strip));
111         }
112     }
113 
114     @Override
assertNoSuggestion(String name)115     public void assertNoSuggestion(String name) throws Exception {
116         final UiObject2 strip = findSuggestionStrip(UI_TIMEOUT);
117         final UiObject2 dataset = strip.findObject(By.text(name));
118         if (dataset != null) {
119             throw new AssertionError("has dataset " + name + " in " + getChildrenAsText(strip));
120         }
121     }
122 
123     @Override
scrollSuggestionView(Direction direction, int speed)124     public void scrollSuggestionView(Direction direction, int speed) throws Exception {
125         final UiObject2 strip = findSuggestionStrip(UI_TIMEOUT);
126         strip.fling(direction, speed);
127     }
128 
assertTooltipShowing(String text)129     public void assertTooltipShowing(String text) throws Exception {
130         final UiObject2 strip = waitForObject(By.text(text), UI_TIMEOUT);
131         if (strip == null) {
132             throw new AssertionError("not find inline tooltip by text: " + text);
133         }
134     }
135 
findSuggestionStrip(Timeout timeout)136     private UiObject2 findSuggestionStrip(Timeout timeout) throws Exception {
137         return waitForObject(SUGGESTION_STRIP_SELECTOR, timeout);
138     }
139 }
140