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.app.Instrumentation;
20 import android.platform.helpers.ScrollUtility.ScrollActions;
21 import android.platform.helpers.ScrollUtility.ScrollDirection;
22 
23 import androidx.test.uiautomator.By;
24 import androidx.test.uiautomator.BySelector;
25 import androidx.test.uiautomator.UiObject2;
26 
27 /** Implementation of {@link IAutoUserHelper} to support tests of account settings */
28 public class SettingUserHelperImpl extends AbstractStandardAppHelper implements IAutoUserHelper {
29 
30     // constants
31     private static final String TAG = "SettingUserHelperImpl";
32     private static final int MAX_WAIT_COUNT = 4;
33     private static final int WAIT_SEC = 20000;
34 
35     private ScrollUtility mScrollUtility;
36     private ScrollActions mScrollAction;
37     private BySelector mBackwardButtonSelector;
38     private BySelector mForwardButtonSelector;
39     private BySelector mScrollableElementSelector;
40     private ScrollDirection mScrollDirection;
41     private UiObject2 mEnableOption;
42 
SettingUserHelperImpl(Instrumentation instr)43     public SettingUserHelperImpl(Instrumentation instr) {
44         super(instr);
45         mScrollUtility = ScrollUtility.getInstance(getSpectatioUiUtil());
46         mScrollAction =
47                 ScrollActions.valueOf(
48                         getActionFromConfig(AutomotiveConfigConstants.USER_SETTINGS_SCROLL_ACTION));
49         mBackwardButtonSelector =
50                 getUiElementFromConfig(AutomotiveConfigConstants.USER_SETTINGS_SCROLL_BACKWARD);
51         mForwardButtonSelector =
52                 getUiElementFromConfig(AutomotiveConfigConstants.USER_SETTINGS_SCROLL_FORWARD);
53         mScrollableElementSelector =
54                 getUiElementFromConfig(AutomotiveConfigConstants.USER_SETTINGS_SCROLL_ELEMENT);
55         mScrollDirection =
56                 ScrollDirection.valueOf(
57                         getActionFromConfig(
58                                 AutomotiveConfigConstants.USER_SETTINGS_SCROLL_DIRECTION));
59         mScrollUtility.setScrollValues(
60                 Integer.valueOf(
61                         getActionFromConfig(AutomotiveConfigConstants.USER_SETTINGS_SCROLL_MARGIN)),
62                 Integer.valueOf(
63                         getActionFromConfig(
64                                 AutomotiveConfigConstants.USER_SETTINGS_SCROLL_WAIT_TIME)));
65     }
66 
67     /** {@inheritDoc} */
68     @Override
getPackage()69     public String getPackage() {
70         return getPackageFromConfig(AutomotiveConfigConstants.USER_SETTINGS_PACKAGE);
71     }
72 
73     /** {@inheritDoc} */
74     @Override
getLauncherName()75     public String getLauncherName() {
76         throw new UnsupportedOperationException("Operation not supported.");
77     }
78 
79     /** {@inheritDoc} */
80     @Override
dismissInitialDialogs()81     public void dismissInitialDialogs() {
82         // Nothing to dismiss
83     }
84 
85     /** {@inheritDoc} */
86     // Add a new user
87     @Override
addUser()88     public void addUser() {
89         clickbutton(AutomotiveConfigConstants.USER_SETTINGS_ADD_PROFILE);
90         clickbutton(AutomotiveConfigConstants.USER_SETTINGS_OK);
91         getSpectatioUiUtil().waitNSeconds(WAIT_SEC);
92     }
93     // opens permission page of a user
94     @Override
openPermissionsPage(String user)95     public void openPermissionsPage(String user) {
96         clickbutton(AutomotiveConfigConstants.USER_SETTINGS_MANAGE_OTHER_PROFILES);
97         BySelector userSelector = By.text(user);
98         UiObject2 userObject = getSpectatioUiUtil().findUiObject(userSelector);
99         getSpectatioUiUtil().validateUiObject(userObject, String.format("User %s", user));
100         getSpectatioUiUtil().clickAndWait(userObject);
101     }
102 
103     // delete an existing user
104     @Override
deleteUser(String user)105     public void deleteUser(String user) {
106         if (isUserPresent(user)) {
107             BySelector userSelector = By.text(user);
108             UiObject2 userObject = getSpectatioUiUtil().findUiObject(userSelector);
109             getSpectatioUiUtil().validateUiObject(userObject, String.format("User %s", user));
110             getSpectatioUiUtil().clickAndWait(userObject);
111             clickbutton(AutomotiveConfigConstants.USER_SETTINGS_DELETE);
112             clickbutton(AutomotiveConfigConstants.USER_SETTINGS_DELETE);
113             getSpectatioUiUtil().wait5Seconds();
114         }
115     }
116 
117     // delete self User
118     @Override
deleteCurrentUser()119     public void deleteCurrentUser() {
120         clickbutton(AutomotiveConfigConstants.USER_SETTINGS_DELETE_SELF);
121         clickbutton(AutomotiveConfigConstants.USER_SETTINGS_DELETE);
122         getSpectatioUiUtil().wait5Seconds();
123     }
124 
125     /** {@inheritDoc} */
126     // check if a user is present in the list of existing users
127     @Override
isUserPresent(String user)128     public boolean isUserPresent(String user) {
129         BySelector buttonSelector =
130                 getUiElementFromConfig(AutomotiveConfigConstants.USER_SETTINGS_ADD_PROFILE);
131         UiObject2 addUserButton =
132                 mScrollUtility.scrollAndFindUiObject(
133                         mScrollAction,
134                         mScrollDirection,
135                         mForwardButtonSelector,
136                         mBackwardButtonSelector,
137                         mScrollableElementSelector,
138                         buttonSelector,
139                         String.format("Scroll to find %s", user));
140         // Log.v(
141         //         TAG,
142         //        String.format(
143         //              "AddProfileButton = %s ; UI_Obj = %s",
144         //             AutomotiveConfigConstants.USER_SETTINGS_ADD_PROFILE, addUserButton));
145         if (addUserButton == null) {
146             clickbutton(AutomotiveConfigConstants.USER_SETTINGS_MANAGE_OTHER_PROFILES);
147             getSpectatioUiUtil().wait5Seconds();
148             UiObject2 UserObject =
149                     mScrollUtility.scrollAndFindUiObject(
150                             mScrollAction,
151                             mScrollDirection,
152                             mForwardButtonSelector,
153                             mBackwardButtonSelector,
154                             mScrollableElementSelector,
155                             By.text(user),
156                             String.format("Scroll to find %s", user));
157             getSpectatioUiUtil().wait1Second();
158             return UserObject != null;
159         }
160         return false;
161     }
162 
163     // switch User from current user to given user
164     @Override
switchUser(String userFrom, String userTo)165     public void switchUser(String userFrom, String userTo) {
166         int count = 0;
167         BySelector userFromSelector = By.text(userFrom);
168         UiObject2 userFromObject = getSpectatioUiUtil().findUiObject(userFromSelector);
169         getSpectatioUiUtil().validateUiObject(userFromObject, String.format("User %s", userFrom));
170         getSpectatioUiUtil().clickAndWait(userFromObject);
171         BySelector userToSelector = By.text(userTo);
172         UiObject2 userToObject = getSpectatioUiUtil().findUiObject(userToSelector);
173         getSpectatioUiUtil().validateUiObject(userToObject, String.format("User %s", userTo));
174         getSpectatioUiUtil().clickAndWait(userToObject);
175         while ((getSpectatioUiUtil()
176                                 .findUiObject(
177                                         getUiElementFromConfig(
178                                                 AutomotiveConfigConstants.HOME_BOTTOM_CARD))
179                         == null)
180                 && count < MAX_WAIT_COUNT) {
181             getSpectatioUiUtil().waitNSeconds(WAIT_SEC);
182             count++;
183         }
184     }
185 
186     @Override
switchUsingUserIcon(String userNameConfigKey)187     public void switchUsingUserIcon(String userNameConfigKey) {
188         int count = 0;
189         clickbutton(AutomotiveConfigConstants.HOME_PROFILE_ICON_BUTTON);
190         clickbutton(userNameConfigKey);
191         while ((getSpectatioUiUtil()
192                                 .findUiObject(
193                                         getUiElementFromConfig(
194                                                 AutomotiveConfigConstants.HOME_BOTTOM_CARD))
195                         == null)
196                 && count < MAX_WAIT_COUNT) {
197             getSpectatioUiUtil().waitNSeconds(WAIT_SEC);
198             count++;
199         }
200     }
201 
202     @Override
getProfileNameFromSettings()203     public String getProfileNameFromSettings() {
204         BySelector profileNameSelector =
205                 getUiElementFromConfig(AutomotiveConfigConstants.DEVICE_HEADER_TITLE);
206         UiObject2 profileName = getSpectatioUiUtil().findUiObject(profileNameSelector);
207         String profileNameText = profileName.getText();
208         return profileNameText;
209     }
210 
211     // add User via quick settings
212     @Override
addUserQuickSettings(String userFrom)213     public void addUserQuickSettings(String userFrom) {
214         goToQuickSettings();
215         getSpectatioUiUtil().wait1Second();
216         addUser();
217     }
218 
219     // make an existing user admin
220     @Override
makeUserAdmin(String user)221     public void makeUserAdmin(String user) {
222         if (isUserPresent(user)) {
223             BySelector userSelector = By.text(user);
224             UiObject2 userObject = getSpectatioUiUtil().findUiObject(userSelector);
225             getSpectatioUiUtil().validateUiObject(userObject, String.format("user %s", user));
226             getSpectatioUiUtil().clickAndWait(userObject);
227             clickbutton(AutomotiveConfigConstants.USER_SETTINGS_MAKE_ADMIN);
228             clickbutton(AutomotiveConfigConstants.USER_SETTINGS_MAKE_ADMIN_CONFIRM);
229         }
230     }
231 
232     // click an on-screen element if expected text for that element is present
clickbutton(String buttonText)233     private void clickbutton(String buttonText) {
234         BySelector buttonSelector = getUiElementFromConfig(buttonText);
235         UiObject2 buttonObject =
236                 mScrollUtility.scrollAndFindUiObject(
237                         mScrollAction,
238                         mScrollDirection,
239                         mForwardButtonSelector,
240                         mBackwardButtonSelector,
241                         mScrollableElementSelector,
242                         buttonSelector,
243                         String.format("Scroll to find %s", buttonText));
244         // Log.v(TAG, String.format("button =  %s ; UI_Obj = %s", buttonText, buttonObject));
245         getSpectatioUiUtil().validateUiObject(buttonObject, String.format("button %s", buttonText));
246         getSpectatioUiUtil().clickAndWait(buttonObject);
247         getSpectatioUiUtil().wait1Second();
248     }
249 
250     @Override
isNewUserAnAdmin(String user)251     public boolean isNewUserAnAdmin(String user) {
252         boolean isUserAdmin = true;
253         clickbutton(AutomotiveConfigConstants.USER_SETTINGS_MANAGE_OTHER_PROFILES);
254         getSpectatioUiUtil().wait5Seconds();
255         UiObject2 UserObject =
256                 mScrollUtility.scrollAndFindUiObject(
257                         mScrollAction,
258                         mScrollDirection,
259                         mForwardButtonSelector,
260                         mBackwardButtonSelector,
261                         mScrollableElementSelector,
262                         By.text(user),
263                         String.format("Scroll to find %s", user));
264         getSpectatioUiUtil().clickAndWait(UserObject);
265         getSpectatioUiUtil().wait1Second();
266         if (getSpectatioUiUtil()
267                 .hasUiElement(
268                         getUiElementFromConfig(
269                                 AutomotiveConfigConstants.USER_SETTINGS_MAKE_ADMIN))) {
270             return !isUserAdmin;
271         }
272         return isUserAdmin;
273     }
274 
275     // go to quick Settings for switching User
goToQuickSettings()276     private void goToQuickSettings() {
277         clickbutton(AutomotiveConfigConstants.HOME_PROFILE_ICON_BUTTON);
278     }
279     // checks whether the Add profile button is visible
280     @Override
isVisibleAddProfile()281     public boolean isVisibleAddProfile() {
282         boolean isVisibleAddProfile = true;
283         if (getSpectatioUiUtil()
284                 .hasUiElement(
285                         getUiElementFromConfig(
286                                 AutomotiveConfigConstants.USER_SETTINGS_ADD_PROFILE))) {
287             return isVisibleAddProfile;
288         }
289         return !isVisibleAddProfile;
290     }
291 
292     @Override
isToggleOn(String buttonText)293     public boolean isToggleOn(String buttonText) {
294         BySelector mEnableOptionSelector = getUiElementFromConfig(buttonText);
295         mEnableOption =
296                 mScrollUtility.scrollAndFindUiObject(
297                         mScrollAction,
298                         mScrollDirection,
299                         mForwardButtonSelector,
300                         mBackwardButtonSelector,
301                         mScrollableElementSelector,
302                         mEnableOptionSelector,
303                         String.format("Scroll to find %s", buttonText));
304         getSpectatioUiUtil().validateUiObject(mEnableOption, buttonText);
305         return mEnableOption.isChecked();
306     }
307     // Toggles the switch from one mode to the other
308     @Override
toggle(String buttonText)309     public boolean toggle(String buttonText) {
310         boolean currentStatus = isToggleOn(buttonText);
311         getSpectatioUiUtil().clickAndWait(mEnableOption);
312         boolean finalStatus = isToggleOn(buttonText);
313         return finalStatus != currentStatus;
314     }
315 }
316