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.content.Context; 21 22 import androidx.test.InstrumentationRegistry; 23 import androidx.test.uiautomator.By; 24 import androidx.test.uiautomator.BySelector; 25 import androidx.test.uiautomator.UiObject2; 26 27 /** Bluetooth Setting Helper class for Android Auto platform functional tests */ 28 public class SettingsBluetoothHelperImpl extends AbstractStandardAppHelper 29 implements IAutoBluetoothSettingsHelper { 30 31 private static final String LOG_TAG = SettingHelperImpl.class.getSimpleName(); 32 private static final int WAIT_TIME_MAX = 15; 33 34 private final ScrollUtility mScrollUtility; 35 private final SeekUtility mSeekUtility; 36 private Context mContext; 37 SettingsBluetoothHelperImpl(Instrumentation instr)38 public SettingsBluetoothHelperImpl(Instrumentation instr) { 39 super(instr); 40 mContext = InstrumentationRegistry.getContext(); 41 mScrollUtility = ScrollUtility.getInstance(getSpectatioUiUtil()); 42 mSeekUtility = SeekUtility.getInstance(getSpectatioUiUtil()); 43 } 44 45 /** {@inheritDoc} */ 46 @Override isBluetoothPreferenceChecked()47 public boolean isBluetoothPreferenceChecked() { 48 return buttonChecked(AutomotiveConfigConstants.TOGGLE_DEVICE_BLUETOOTH); 49 } 50 51 /** {@inheritDoc} */ 52 @Override isUseBluetoothToggleChecked()53 public boolean isUseBluetoothToggleChecked() { 54 return buttonChecked(AutomotiveConfigConstants.USE_BLUETOOTH_SETTINGS_TOGGLE); 55 } 56 57 /** {@inheritDoc} */ 58 @Override isMediaPreferenceChecked()59 public boolean isMediaPreferenceChecked() { 60 return buttonChecked(AutomotiveConfigConstants.MEDIA_BUTTON); 61 } 62 63 /** {@inheritDoc} */ 64 @Override isPhonePreferenceChecked()65 public boolean isPhonePreferenceChecked() { 66 return buttonChecked(AutomotiveConfigConstants.PHONE_BUTTON); 67 } 68 69 /** {@inheritDoc} */ 70 @Override isMediaPreferenceEnabled()71 public boolean isMediaPreferenceEnabled() { 72 return buttonEnabled(AutomotiveConfigConstants.MEDIA_BUTTON); 73 } 74 75 /** {@inheritDoc} */ 76 @Override isPhonePreferenceEnabled()77 public boolean isPhonePreferenceEnabled() { 78 return buttonEnabled(AutomotiveConfigConstants.PHONE_BUTTON); 79 } 80 81 /** 82 * Assumes passed in BySelector title is a checkable button that is currently onscreen 83 * 84 * @param buttonName - The button whose status is to be checked. 85 * @return - Whether or not the button element with the given name as a descriptor is checked. 86 */ buttonChecked(String buttonName)87 public boolean buttonChecked(String buttonName) { 88 BySelector toggleSelector = getUiElementFromConfig(buttonName); 89 UiObject2 toggleButton = getSpectatioUiUtil().findUiObject(toggleSelector); 90 getSpectatioUiUtil().validateUiObject(toggleButton, buttonName); 91 92 return toggleButton.isChecked(); 93 } 94 95 /** 96 * Assumes passed in BySelector title is a button that is currently onscreen 97 * 98 * @param buttonName - The button whose enabled status is to be checked. 99 * @return - Whether or not the button element with the given name as a descriptor is enabled. 100 */ buttonEnabled(String buttonName)101 public boolean buttonEnabled(String buttonName) { 102 BySelector toggleSelector = getUiElementFromConfig(buttonName); 103 UiObject2 toggleButton = getSpectatioUiUtil().findUiObject(toggleSelector); 104 getSpectatioUiUtil().validateUiObject(toggleButton, buttonName); 105 106 return toggleButton.isEnabled(); 107 } 108 109 /** {@inheritDoc} */ 110 @Override pressBluetoothToggleOnDevice(String deviceName)111 public void pressBluetoothToggleOnDevice(String deviceName) { 112 113 /** 114 * Note: this method currently does not select strictly within the passed-in device name. 115 */ 116 BySelector toggleSelector = 117 getUiElementFromConfig(AutomotiveConfigConstants.TOGGLE_DEVICE_BLUETOOTH); 118 119 UiObject2 toggleButton = getSpectatioUiUtil().findUiObject(toggleSelector); 120 121 getSpectatioUiUtil() 122 .validateUiObject(toggleButton, AutomotiveConfigConstants.TOGGLE_DEVICE_BLUETOOTH); 123 getSpectatioUiUtil().clickAndWait(toggleButton); 124 } 125 126 /** {@inheritDoc} */ 127 @Override pressMediaToggleOnDevice(String deviceName)128 public void pressMediaToggleOnDevice(String deviceName) { 129 130 /** 131 * Note: this method currently does not select strictly within the passed-in device name. 132 */ 133 BySelector toggleSelector = getUiElementFromConfig(AutomotiveConfigConstants.MEDIA_BUTTON); 134 135 UiObject2 toggleButton = getSpectatioUiUtil().findUiObject(toggleSelector); 136 137 getSpectatioUiUtil() 138 .validateUiObject(toggleButton, AutomotiveConfigConstants.TOGGLE_DEVICE_BLUETOOTH); 139 getSpectatioUiUtil().clickAndWait(toggleButton); 140 } 141 142 /** {@inheritDoc} */ 143 @Override pressPhoneToggleOnDevice(String deviceName)144 public void pressPhoneToggleOnDevice(String deviceName) { 145 146 /* 147 * Note: this method currently does not select strictly within the passed-in device name. 148 * TODO: Extend all Preference Toggle selectors to be specific to the device name 149 */ 150 BySelector toggleSelector = getUiElementFromConfig(AutomotiveConfigConstants.PHONE_BUTTON); 151 152 UiObject2 toggleButton = getSpectatioUiUtil().findUiObject(toggleSelector); 153 154 getSpectatioUiUtil().validateUiObject(toggleButton, AutomotiveConfigConstants.PHONE_BUTTON); 155 getSpectatioUiUtil().clickAndWait(toggleButton); 156 } 157 158 /** {@inheritDoc} */ 159 @Override pressDevice(String deviceName)160 public void pressDevice(String deviceName) { 161 BySelector nameField = By.text(deviceName); 162 BySelector clickable = By.hasDescendant(By.hasDescendant(nameField)); 163 UiObject2 clickableDevice = getSpectatioUiUtil().findUiObject(clickable); 164 getSpectatioUiUtil().waitForUiObject(clickable, WAIT_TIME_MAX); 165 getSpectatioUiUtil() 166 .validateUiObject(clickableDevice, "Viewgroup ancestor of " + deviceName); 167 getSpectatioUiUtil().clickAndWait(clickableDevice); 168 } 169 170 /** {@inheritDoc} */ 171 @Override waitUntilConnectionStatus(String status)172 public void waitUntilConnectionStatus(String status) { 173 BySelector statusSelector = By.text(status); 174 getSpectatioUiUtil().waitForUiObject(statusSelector); 175 } 176 177 /** {@inheritDoc} */ 178 @Override getDeviceSummary()179 public String getDeviceSummary() { 180 181 BySelector statusSelector = 182 getUiElementFromConfig(AutomotiveConfigConstants.DEVICE_HEADER_SUMMARY); 183 getSpectatioUiUtil().waitForUiObject(statusSelector); 184 UiObject2 statusField = getSpectatioUiUtil().findUiObject(statusSelector); 185 getSpectatioUiUtil() 186 .validateUiObject(statusField, AutomotiveConfigConstants.DEVICE_HEADER_SUMMARY); 187 return statusField.getText(); 188 } 189 190 /** {@inheritDoc} */ 191 @Override isConnected()192 public boolean isConnected() { 193 // Recall that a connected device will show 'Disconnected' 194 return !getDeviceSummary().contains("Disconnected"); 195 } 196 197 /** {@inheritDoc} */ 198 @Override pressForget()199 public void pressForget() { 200 BySelector forgetSelector = 201 getUiElementFromConfig(AutomotiveConfigConstants.DEVICE_FORGET_BUTTON); 202 UiObject2 forgetButton = getSpectatioUiUtil().findUiObject(forgetSelector); 203 getSpectatioUiUtil() 204 .validateUiObject(forgetButton, AutomotiveConfigConstants.DEVICE_FORGET_BUTTON); 205 getSpectatioUiUtil().clickAndWait(forgetButton); 206 } 207 208 /** {@inheritDoc} */ 209 @Override pressConnectionToggle()210 public void pressConnectionToggle() { 211 BySelector connectionSelector = 212 getUiElementFromConfig(AutomotiveConfigConstants.DEVICE_CONNECTION_BUTTON); 213 UiObject2 connectionButton = getSpectatioUiUtil().findUiObject(connectionSelector); 214 getSpectatioUiUtil() 215 .validateUiObject( 216 connectionButton, AutomotiveConfigConstants.DEVICE_CONNECTION_BUTTON); 217 getSpectatioUiUtil().clickAndWait(connectionButton); 218 } 219 220 /** {@inheritDoc} */ 221 @Override goBackToBluetoothSettings()222 public void goBackToBluetoothSettings() { 223 BySelector connectionSelector = 224 getUiElementFromConfig(AutomotiveConfigConstants.DEVICE_CONNECTION_BACK_BUTTON); 225 UiObject2 connectionButton = getSpectatioUiUtil().findUiObject(connectionSelector); 226 getSpectatioUiUtil() 227 .validateUiObject( 228 connectionButton, AutomotiveConfigConstants.DEVICE_CONNECTION_BACK_BUTTON); 229 getSpectatioUiUtil().clickAndWait(connectionButton); 230 } 231 232 /** {@inheritDoc} */ 233 @Override isPhoneProfileEnabled()234 public boolean isPhoneProfileEnabled() { 235 BySelector phoneToogleSelector = 236 getUiElementFromConfig(AutomotiveConfigConstants.TOGGLE_PHONE); 237 UiObject2 phoneToogleSwitch = getSpectatioUiUtil().findUiObject(phoneToogleSelector); 238 getSpectatioUiUtil() 239 .validateUiObject(phoneToogleSwitch, AutomotiveConfigConstants.TOGGLE_PHONE); 240 return phoneToogleSwitch.isChecked(); 241 } 242 243 /** {@inheritDoc} */ 244 @Override getPackage()245 public String getPackage() { 246 return getPackageFromConfig(AutomotiveConfigConstants.SETTINGS_PACKAGE); 247 } 248 249 /** {@inheritDoc} */ 250 @Override getLauncherName()251 public String getLauncherName() { 252 throw new UnsupportedOperationException("Operation not supported."); 253 } 254 255 /** {@inheritDoc} */ 256 @Override dismissInitialDialogs()257 public void dismissInitialDialogs() { 258 // Nothing to dismiss 259 } 260 } 261