1 /*
2  * Copyright (C) 2023 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.platform.helpers;
18 
19 import android.platform.spectatio.exceptions.MissingUiElementException;
20 import android.platform.spectatio.utils.SpectatioUiUtil;
21 
22 import androidx.test.uiautomator.BySelector;
23 import androidx.test.uiautomator.UiObject2;
24 
25 /** Utility file for scroll functions */
26 public class ScrollUtility {
27 
28     private static ScrollUtility sScrollUtilityInstance;
29     private SpectatioUiUtil mSpectatioUiUtil;
30 
ScrollUtility(SpectatioUiUtil spectatioUiUtil)31     private ScrollUtility(SpectatioUiUtil spectatioUiUtil) {
32         mSpectatioUiUtil = spectatioUiUtil;
33     }
34 
35     public enum ScrollActions {
36         USE_BUTTON,
37         USE_GESTURE;
38     }
39 
40     public enum ScrollDirection {
41         VERTICAL,
42         HORIZONTAL;
43     }
44 
45     /** Get ScrollUtility Instance */
getInstance(SpectatioUiUtil spectatioUiUtil)46     public static ScrollUtility getInstance(SpectatioUiUtil spectatioUiUtil) {
47         if (sScrollUtilityInstance == null) {
48             sScrollUtilityInstance = new ScrollUtility(spectatioUiUtil);
49         }
50         return sScrollUtilityInstance;
51     }
52 
53     /** Sets the scrolls margins and wait time after the scroll */
setScrollValues(Integer scrollMargin, Integer waitTimeAfterSCroll)54     public void setScrollValues(Integer scrollMargin, Integer waitTimeAfterSCroll) {
55         mSpectatioUiUtil.addScrollValues(scrollMargin, waitTimeAfterSCroll);
56     }
57 
58     /** Scroll using backward buttons or guesture on device screen */
scrollBackward( ScrollActions scrollAction, ScrollDirection scrollDirection, BySelector backwardButtonSelector, BySelector scrollableElementSelector, String action)59     public boolean scrollBackward(
60             ScrollActions scrollAction,
61             ScrollDirection scrollDirection,
62             BySelector backwardButtonSelector,
63             BySelector scrollableElementSelector,
64             String action) {
65         boolean scrollResult = false;
66         try {
67             switch (scrollAction) {
68                 case USE_BUTTON:
69                     scrollResult = mSpectatioUiUtil.scrollUsingButton(backwardButtonSelector);
70                     break;
71                 case USE_GESTURE:
72                     scrollResult =
73                             mSpectatioUiUtil.scrollBackward(
74                                     scrollableElementSelector,
75                                     (scrollDirection == ScrollDirection.VERTICAL));
76                     break;
77                 default:
78                     throw new IllegalStateException(
79                             String.format(
80                                     "Unable to %s, unknown scroll action %s.",
81                                     action, scrollAction));
82             }
83             return scrollResult;
84         } catch (MissingUiElementException ex) {
85             throw new IllegalStateException(
86                     String.format("Unable to %s. Error: %s", action, ex.getMessage()));
87         }
88     }
89 
90     /** Scroll using forward buttons or guesture on device screen */
scrollForward( ScrollActions scrollAction, ScrollDirection scrollDirection, BySelector forwardButtonSelector, BySelector scrollableElementSelector, String action)91     public boolean scrollForward(
92             ScrollActions scrollAction,
93             ScrollDirection scrollDirection,
94             BySelector forwardButtonSelector,
95             BySelector scrollableElementSelector,
96             String action) {
97         boolean scrollResult = false;
98         try {
99             switch (scrollAction) {
100                 case USE_BUTTON:
101                     scrollResult = mSpectatioUiUtil.scrollUsingButton(forwardButtonSelector);
102                     break;
103                 case USE_GESTURE:
104                     scrollResult =
105                             mSpectatioUiUtil.scrollForward(
106                                     scrollableElementSelector,
107                                     (scrollDirection == ScrollDirection.VERTICAL));
108                     break;
109                 default:
110                     throw new IllegalStateException(
111                             String.format(
112                                     "Unable to %s, unknown scroll action %s.",
113                                     action, scrollAction));
114             }
115             return scrollResult;
116         } catch (MissingUiElementException ex) {
117             throw new IllegalStateException(
118                     String.format("Unable to %s. Error: %s", action, ex.getMessage()));
119         }
120     }
121 
122     /**
123      * Scroll using forward and backward buttons or use guesture on device screen to find the object
124      */
scrollAndFindUiObject( ScrollActions scrollAction, ScrollDirection scrollDirection, BySelector forwardButtonSelector, BySelector backwardButtonSelector, BySelector scrollElementSelector, BySelector target, String action)125     public UiObject2 scrollAndFindUiObject(
126             ScrollActions scrollAction,
127             ScrollDirection scrollDirection,
128             BySelector forwardButtonSelector,
129             BySelector backwardButtonSelector,
130             BySelector scrollElementSelector,
131             BySelector target,
132             String action) {
133         UiObject2 scrollResult = null;
134         try {
135             switch (scrollAction) {
136                 case USE_BUTTON:
137                     scrollResult =
138                             mSpectatioUiUtil.scrollAndFindUiObject(
139                                     forwardButtonSelector, backwardButtonSelector, target);
140                     break;
141                 case USE_GESTURE:
142                     scrollResult =
143                             mSpectatioUiUtil.scrollAndFindUiObject(
144                                     scrollElementSelector,
145                                     target,
146                                     (scrollDirection == ScrollDirection.VERTICAL));
147                     break;
148                 default:
149                     throw new IllegalStateException(
150                             String.format(
151                                     "Unable to %s, unknown scroll action %s.",
152                                     action, scrollAction));
153             }
154             return scrollResult;
155         } catch (MissingUiElementException ex) {
156             throw new IllegalStateException(
157                     String.format("Unable to %s. Error: %s", action, ex.getMessage()));
158         }
159     }
160 
161     /** Scroll to beginning by using button or use guesture on device screen. */
scrollToBeginning( ScrollActions scrollAction, ScrollDirection scrollDirection, BySelector backwardButtonSelector, BySelector scrollElementSelector, String action)162     public void scrollToBeginning(
163             ScrollActions scrollAction,
164             ScrollDirection scrollDirection,
165             BySelector backwardButtonSelector,
166             BySelector scrollElementSelector,
167             String action) {
168         try {
169             switch (scrollAction) {
170                 case USE_BUTTON:
171                     mSpectatioUiUtil.scrollToBeginning(backwardButtonSelector);
172                     break;
173                 case USE_GESTURE:
174                     mSpectatioUiUtil.scrollToBeginning(
175                             scrollElementSelector, (scrollDirection == ScrollDirection.VERTICAL));
176                     break;
177                 default:
178                     throw new IllegalStateException(
179                             String.format(
180                                     "Unable to %s, unknown scroll action %s.",
181                                     action, scrollAction));
182             }
183         } catch (MissingUiElementException ex) {
184             throw new IllegalStateException(
185                     String.format("Unable to %s. Error: %s", action, ex.getMessage()));
186         }
187     }
188 
189     /**
190      * Find UI element by scrolling using forward and backward buttons or guesture on device screen
191      */
scrollAndCheckIfUiElementExist( ScrollActions scrollAction, ScrollDirection scrollDirection, BySelector forwardButtonSelector, BySelector backwardButtonSelector, BySelector scrollableElementSelector, BySelector elementSelector, String action)192     public boolean scrollAndCheckIfUiElementExist(
193             ScrollActions scrollAction,
194             ScrollDirection scrollDirection,
195             BySelector forwardButtonSelector,
196             BySelector backwardButtonSelector,
197             BySelector scrollableElementSelector,
198             BySelector elementSelector,
199             String action) {
200         boolean scrollResult = false;
201         try {
202             switch (scrollAction) {
203                 case USE_BUTTON:
204                     scrollResult =
205                             mSpectatioUiUtil.scrollAndCheckIfUiElementExist(
206                                     forwardButtonSelector, backwardButtonSelector, elementSelector);
207                     break;
208                 case USE_GESTURE:
209                     scrollResult =
210                             mSpectatioUiUtil.scrollAndCheckIfUiElementExist(
211                                     scrollableElementSelector,
212                                     elementSelector,
213                                     (scrollDirection == ScrollDirection.VERTICAL));
214                     break;
215                 default:
216                     throw new IllegalStateException(
217                             String.format(
218                                     "Unable to %s, unknown scroll action %s.",
219                                     action, scrollAction));
220             }
221             return scrollResult;
222         } catch (MissingUiElementException ex) {
223             throw new IllegalStateException(
224                     String.format("Unable to %s. Error: %s", action, ex.getMessage()));
225         }
226     }
227 }
228