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