Borjan Tchakaloff | a4bdae1 | 2017-11-21 14:58:12 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 3 | import os |
Borjan Tchakaloff | a4bdae1 | 2017-11-21 14:58:12 +0100 | [diff] [blame] | 4 | import pathlib |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 5 | import re |
| 6 | import subprocess |
| 7 | import sys |
| 8 | import time |
| 9 | |
| 10 | from uiautomator import Device |
| 11 | |
| 12 | VWS_CREDENTIALS = {"user": "fairphonetesting@gmail.com", "password": "aish3echi:uwaiSh"} |
Borjan Tchakaloff | a4bdae1 | 2017-11-21 14:58:12 +0100 | [diff] [blame] | 13 | |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 14 | |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 15 | PREBUILTS_PATH = "../../vendor/smartviser/viser/prebuilts/apk" |
Borjan Tchakaloff | d8445d3 | 2018-04-18 18:01:20 +0200 | [diff] [blame] | 16 | |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 17 | PREBUILT_PROXY_APK_PATTERN = "com.lunarlabs.panda.proxy-latest-sdk{sdk}-{flavour}.apk" |
Borjan Tchakaloff | d8445d3 | 2018-04-18 18:01:20 +0200 | [diff] [blame] | 18 | |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 19 | PREBUILT_APKS = ["com.smartviser.demogame-latest.apk", "com.lunarlabs.panda-latest.apk"] |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 20 | |
Borjan Tchakaloff | 74d962a | 2018-04-11 17:47:14 +0200 | [diff] [blame] | 21 | FAIRPHONE_WIFI_NETWORKS = { |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 22 | "Fairphone Guest": {"security": "WPA/WPA2 PSK", "password": "fairwifi"}, |
| 23 | "Fairphone DEV (2.4 GHz)": {"security": "WPA/WPA2 PSK", "password": "fdev@adm"}, |
| 24 | "Fairphone DEV (5 GHz)": {"security": "WPA/WPA2 PSK", "password": "fdev@adm"}, |
Borjan Tchakaloff | 74d962a | 2018-04-11 17:47:14 +0200 | [diff] [blame] | 25 | } |
| 26 | |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 27 | ADB_DEVICES_PATTERN = re.compile(r"^([a-z0-9-]+)\s+device$", flags=re.M) |
Franz-Xaver Geiger | b0f5542 | 2018-04-17 10:33:37 +0200 | [diff] [blame] | 28 | |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 29 | |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 30 | class HostCommandError(BaseException): |
| 31 | """An error happened while issuing a command on the host.""" |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 32 | |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 33 | def __init__(self, command, error_message): |
| 34 | self.command = command |
| 35 | self.error_message = error_message |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 36 | message = "Command `{}` failed: {}".format(command, error_message) |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 37 | super(HostCommandError, self).__init__(message) |
| 38 | |
| 39 | |
| 40 | class DeviceCommandError(BaseException): |
| 41 | """An error happened while sending a command to a device.""" |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 42 | |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 43 | def __init__(self, serial, command, error_message): |
| 44 | self.serial = serial |
| 45 | self.command = command |
| 46 | self.error_message = error_message |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 47 | message = "Command `{}` failed on {}: {}".format(command, serial, error_message) |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 48 | super(DeviceCommandError, self).__init__(message) |
| 49 | |
| 50 | |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 51 | def adb(*args, serial=None, raise_on_error=True): |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 52 | """Run ADB command attached to serial. |
| 53 | |
| 54 | Example: |
| 55 | >>> process = adb('shell', 'getprop', 'ro.build.fingerprint', serial='cc60c021') |
| 56 | >>> process.returncode |
| 57 | 0 |
| 58 | >>> process.stdout.strip() |
| 59 | 'Fairphone/FP2/FP2:6.0.1/FP2-gms-18.02.0/FP2-gms-18.02.0:user/release-keys' |
| 60 | |
| 61 | :param *args: |
| 62 | List of options to ADB (including command). |
| 63 | :param str serial: |
| 64 | Identifier for ADB connection to device. |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 65 | :param raise_on_error bool: |
| 66 | Whether to raise a DeviceCommandError exception if the return code is |
| 67 | less than 0. |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 68 | :returns subprocess.CompletedProcess: |
| 69 | Completed process. |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 70 | :raises DeviceCommandError: |
| 71 | If the command failed. |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 72 | """ |
| 73 | |
| 74 | # Make sure the adb server is started to avoid the infamous "out of date" |
| 75 | # message that pollutes stdout. |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 76 | ret = subprocess.run( |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 77 | ["adb", "start-server"], |
| 78 | stdout=subprocess.PIPE, |
| 79 | stderr=subprocess.PIPE, |
| 80 | universal_newlines=True, |
| 81 | ) |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 82 | if ret.returncode < 0: |
| 83 | if raise_on_error: |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 84 | raise DeviceCommandError(serial if serial else "??", str(args), ret.stderr) |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 85 | else: |
| 86 | return None |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 87 | |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 88 | command = ["adb"] |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 89 | if serial: |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 90 | command += ["-s", serial] |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 91 | if args: |
| 92 | command += list(args) |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 93 | ret = subprocess.run( |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 94 | command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True |
| 95 | ) |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 96 | |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 97 | if raise_on_error and ret.returncode < 0: |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 98 | raise DeviceCommandError(serial if serial else "??", str(args), ret.stderr) |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 99 | |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 100 | return ret |
| 101 | |
| 102 | |
Franz-Xaver Geiger | b0f5542 | 2018-04-17 10:33:37 +0200 | [diff] [blame] | 103 | def list_devices(): |
| 104 | """List serial numbers of devices attached to adb. |
| 105 | |
| 106 | Raises: |
| 107 | DeviceCommandError: If the underlying adb command failed. |
| 108 | """ |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 109 | process = adb("devices") |
Franz-Xaver Geiger | b0f5542 | 2018-04-17 10:33:37 +0200 | [diff] [blame] | 110 | return ADB_DEVICES_PATTERN.findall(process.stdout) |
| 111 | |
| 112 | |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 113 | def aapt(*args, raise_on_error=True): |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 114 | """Run an AAPT command. |
| 115 | |
| 116 | :param *args: |
| 117 | The AAPT command with its options. |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 118 | :param raise_on_error bool: |
| 119 | Whether to raise a DeviceCommandError exception if the return code is |
| 120 | less than 0. |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 121 | :returns subprocess.CompletedProcess: |
| 122 | Completed process. |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 123 | :raises HostCommandError: |
| 124 | If the command failed. |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 125 | """ |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 126 | command = ["aapt"] |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 127 | if args: |
| 128 | command += list(args) |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 129 | ret = subprocess.run( |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 130 | command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True |
| 131 | ) |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 132 | |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 133 | if raise_on_error and ret.returncode < 0: |
| 134 | raise HostCommandError(str(args), ret.stderr) |
| 135 | |
| 136 | return ret |
| 137 | |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 138 | |
Borjan Tchakaloff | 64ec42b | 2018-02-07 11:33:15 -0800 | [diff] [blame] | 139 | def force_awake(serial, always=True): |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 140 | """Force the device to stay awake. |
| 141 | |
| 142 | Raises: |
| 143 | DeviceCommandError: If the underlying adb command failed. |
| 144 | """ |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 145 | adb( |
| 146 | "shell", |
| 147 | "svc power stayon {}".format("true" if always else "false"), |
| 148 | serial=serial, |
| 149 | ) |
Borjan Tchakaloff | 64ec42b | 2018-02-07 11:33:15 -0800 | [diff] [blame] | 150 | |
| 151 | |
Borjan Tchakaloff | d4ce807 | 2018-05-16 18:20:13 +0200 | [diff] [blame] | 152 | def unlock(dut): |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 153 | """Wake-up the device and unlock it. |
| 154 | |
| 155 | Raises: |
| 156 | DeviceCommandError: If the underlying adb commands failed. |
| 157 | """ |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 158 | if not dut.info["screenOn"]: |
| 159 | adb("shell", "input keyevent KEYCODE_POWER", serial=dut.serial) |
Borjan Tchakaloff | d4ce807 | 2018-05-16 18:20:13 +0200 | [diff] [blame] | 160 | time.sleep(1) |
| 161 | # The KEYCODE_MENU input is enough to unlock a "swipe up to unlock" |
| 162 | # lockscreen on Android 6, but unfortunately not Android 7. So we use a |
| 163 | # swipe up (that depends on the screen resolution) instead. |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 164 | adb("shell", "input touchscreen swipe 930 880 930 380", serial=dut.serial) |
Borjan Tchakaloff | 64ec42b | 2018-02-07 11:33:15 -0800 | [diff] [blame] | 165 | time.sleep(1) |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 166 | adb("shell", "input keyevent KEYCODE_HOME", serial=dut.serial) |
Borjan Tchakaloff | 64ec42b | 2018-02-07 11:33:15 -0800 | [diff] [blame] | 167 | |
| 168 | |
Franz-Xaver Geiger | b3f7c98 | 2018-04-12 09:29:32 +0200 | [diff] [blame] | 169 | def getprop(serial, key): |
| 170 | """Get system property of device. |
| 171 | |
| 172 | Example: |
| 173 | >>> getprop('167eb6e8', 'ro.build.id') |
| 174 | 'FP2-gms-18.02.0' |
| 175 | |
| 176 | :param str serial: |
| 177 | Identifier for ADB connection to device. |
| 178 | :param str key: |
| 179 | Key of property to get. |
| 180 | :returns str: |
| 181 | Value of system property. |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 182 | :raise DeviceCommandError: If the underlying adb command failed. |
Franz-Xaver Geiger | b3f7c98 | 2018-04-12 09:29:32 +0200 | [diff] [blame] | 183 | """ |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 184 | process = adb("shell", "getprop", key, serial=serial) |
Franz-Xaver Geiger | b3f7c98 | 2018-04-12 09:29:32 +0200 | [diff] [blame] | 185 | return process.stdout.strip() |
| 186 | |
| 187 | |
| 188 | def is_gms_device(serial): |
| 189 | """Test if device runs GMS or sibon. |
| 190 | |
| 191 | Example: |
| 192 | >>> is_gms_device('167eb6e8') |
| 193 | True |
| 194 | |
| 195 | :param str serial: |
| 196 | Identifier for ADB connection to device. |
| 197 | :returns bool: |
| 198 | True if device runs GMS, false otherwise. |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 199 | :raise DeviceCommandError: If the underlying adb command failed. |
Franz-Xaver Geiger | b3f7c98 | 2018-04-12 09:29:32 +0200 | [diff] [blame] | 200 | """ |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 201 | return getprop(serial, "ro.build.id").startswith("FP2-gms-") or getprop( |
| 202 | serial, "ro.build.version.incremental" |
| 203 | ).startswith("gms-") |
Franz-Xaver Geiger | b3f7c98 | 2018-04-12 09:29:32 +0200 | [diff] [blame] | 204 | |
| 205 | |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 206 | def uninstall_apk(serial, filename, prebuilts_dir): |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 207 | """Uninstall apk from prebuilts_dir on device. |
| 208 | |
| 209 | Raises: |
| 210 | ValueError: If the package name could not be read from the apk. |
| 211 | DeviceCommandError: If the uninstall command failed. |
| 212 | """ |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 213 | ret = aapt("dump", "badging", "{}/{}".format(prebuilts_dir, filename)) |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 214 | package = None |
| 215 | for line in ret.stdout.splitlines(): |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 216 | if line.startswith("package"): |
| 217 | for token in line.split(" "): |
| 218 | if token.startswith("name="): |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 219 | # Extract the package name out of the token |
| 220 | # (name='some.package.name') |
| 221 | package = token[6:-1] |
| 222 | break |
| 223 | if not package: |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 224 | raise ValueError("Could not find package of app `{}`".format(filename)) |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 225 | |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 226 | adb("uninstall", package, serial=serial) |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 227 | |
| 228 | |
Borjan Tchakaloff | d2c92ce | 2018-08-24 17:13:57 +0200 | [diff] [blame] | 229 | def install_apk(dut, filename, prebuilts_dir): |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 230 | """Install apk from prebuilts_dir on device. |
| 231 | |
| 232 | Raises: |
| 233 | DeviceCommandError: If the install command failed. |
| 234 | """ |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 235 | path = os.path.join(prebuilts_dir, filename) |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 236 | command = ["install", "-r"] |
Borjan Tchakaloff | d2c92ce | 2018-08-24 17:13:57 +0200 | [diff] [blame] | 237 | if dut.sdk >= 23: |
| 238 | # From Marshmallow onwards, adb has a flag to grant default permissions |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 239 | command.append("-g") |
Borjan Tchakaloff | d2c92ce | 2018-08-24 17:13:57 +0200 | [diff] [blame] | 240 | adb(*command, path, serial=dut.serial) |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 241 | |
| 242 | |
Borjan Tchakaloff | 74d962a | 2018-04-11 17:47:14 +0200 | [diff] [blame] | 243 | def configure_wifi_networks(dut, networks): |
| 244 | """Configure Wi-Fi networks. |
| 245 | |
| 246 | The `networks` parameters is a list of networks to configure hashed by |
| 247 | their SSID. Each network value should have the following format: |
| 248 | - security (str): The security value as can be found in the Wi-Fi |
| 249 | settings dialog. Common values are 'None' and 'WPA/WPA2 PSK'. |
| 250 | - password (str, optional): The network password if the security is |
| 251 | 'WPA/WPA2 PSK'. |
| 252 | |
| 253 | Parameters: |
| 254 | dut (Device): The device object. |
| 255 | networks (dict(dict(str))): The list of networks to configure. |
| 256 | Raises: |
| 257 | DeviceCommandError: If the UI automation fails. |
| 258 | """ |
| 259 | # Open the Wi-Fi settings |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 260 | adb( |
| 261 | "shell", |
| 262 | "am start -a android.settings.WIFI_SETTINGS --activity-clear-task", |
| 263 | serial=dut.serial, |
| 264 | ) |
Borjan Tchakaloff | 74d962a | 2018-04-11 17:47:14 +0200 | [diff] [blame] | 265 | |
| 266 | # Make sure Wi-Fi is enabled |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 267 | wifi_enabler = dut(text="OFF", resourceId="com.android.settings:id/switch_widget") |
Borjan Tchakaloff | 74d962a | 2018-04-11 17:47:14 +0200 | [diff] [blame] | 268 | if wifi_enabler.exists: |
| 269 | wifi_enabler.click() |
| 270 | |
| 271 | # Check for registered networks |
| 272 | registered_networks = set() |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 273 | dut(description="More options").click.wait() |
Borjan Tchakaloff | 74d962a | 2018-04-11 17:47:14 +0200 | [diff] [blame] | 274 | time.sleep(1) |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 275 | saved_networks = dut(text="Saved networks") |
Borjan Tchakaloff | 74d962a | 2018-04-11 17:47:14 +0200 | [diff] [blame] | 276 | if saved_networks.exists: |
Borjan Tchakaloff | 1d83e56 | 2018-05-16 18:47:04 +0200 | [diff] [blame] | 277 | saved_networks.click.wait() |
Borjan Tchakaloff | 74d962a | 2018-04-11 17:47:14 +0200 | [diff] [blame] | 278 | for ssid in networks.keys(): |
| 279 | if dut(text=ssid).exists: |
| 280 | registered_networks.add(ssid) |
| 281 | dut.press.back() |
| 282 | |
| 283 | missing_networks = networks.keys() - registered_networks |
| 284 | |
| 285 | for ssid in registered_networks: |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 286 | print("Ignoring `{}` Wi-Fi network, already configured.".format(ssid)) |
Borjan Tchakaloff | 74d962a | 2018-04-11 17:47:14 +0200 | [diff] [blame] | 287 | |
| 288 | for ssid in missing_networks: |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 289 | print("Configuring `{}` Wi-Fi network…".format(ssid)) |
Borjan Tchakaloff | 74d962a | 2018-04-11 17:47:14 +0200 | [diff] [blame] | 290 | |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 291 | dut(description="More options").click.wait() |
| 292 | dut(text="Add network").click.wait() |
| 293 | dut(resourceId="com.android.settings:id/ssid").set_text(ssid) |
| 294 | dut(resourceId="com.android.settings:id/security").click() |
| 295 | dut(text=networks[ssid]["security"]).click() |
| 296 | password_field = dut(resourceId="com.android.settings:id/password") |
Karsten Tausche | c20f4f3 | 2019-03-04 14:32:04 +0100 | [diff] [blame] | 297 | time.sleep(1) |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 298 | if "password" in networks[ssid] and networks[ssid]["password"]: |
Borjan Tchakaloff | 74d962a | 2018-04-11 17:47:14 +0200 | [diff] [blame] | 299 | if not password_field.exists: |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 300 | dut(text="Cancel").click() |
| 301 | raise DeviceCommandError(dut, "UI: add Wi-Fi", "missing password field") |
| 302 | password_field.set_text(networks[ssid]["password"]) |
Borjan Tchakaloff | 74d962a | 2018-04-11 17:47:14 +0200 | [diff] [blame] | 303 | elif password_field.exists: |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 304 | dut(text="Cancel").click() |
| 305 | raise DeviceCommandError(dut, "UI: add Wi-Fi", "missing password data") |
| 306 | save_button = dut(text="Save") |
Borjan Tchakaloff | 74d962a | 2018-04-11 17:47:14 +0200 | [diff] [blame] | 307 | if not save_button.click(): |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 308 | dut(text="Cancel").click() |
| 309 | raise DeviceCommandError(dut, "UI: add Wi-Fi", "could not save network") |
Borjan Tchakaloff | 74d962a | 2018-04-11 17:47:14 +0200 | [diff] [blame] | 310 | |
| 311 | # Force the Wi-Fi on and off to pick the best network available |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 312 | dut(text="ON", resourceId="com.android.settings:id/switch_widget").click() |
| 313 | dut(text="OFF", resourceId="com.android.settings:id/switch_widget").click() |
Borjan Tchakaloff | 74d962a | 2018-04-11 17:47:14 +0200 | [diff] [blame] | 314 | |
| 315 | # Leave the settings |
| 316 | dut.press.back() |
| 317 | |
| 318 | |
Karsten Tausche | b36529f | 2019-03-02 14:13:29 +0100 | [diff] [blame] | 319 | def disable_privacy_impact_popup(dut): |
| 320 | """Disable Privacy Impact popup on Android 5. |
| 321 | |
| 322 | This simplifies UI automation. Disabling the feature globally is more robust |
| 323 | than clicking through the the Privacy Impact screen per app. |
| 324 | """ |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 325 | print("Disabling the Privacy Impact screen…") |
| 326 | adb( |
| 327 | "shell", |
| 328 | ( |
| 329 | "am start -a android.intent.action.MAIN " |
| 330 | "com.fairphone.privacyimpact/.PrivacyImpactPreferenceActivity" |
| 331 | ), |
| 332 | serial=dut.serial, |
Karsten Tausche | b36529f | 2019-03-02 14:13:29 +0100 | [diff] [blame] | 333 | ) |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 334 | disable_privacy_impact_checkbox = dut(className="android.widget.CheckBox") |
Karsten Tausche | b36529f | 2019-03-02 14:13:29 +0100 | [diff] [blame] | 335 | if not disable_privacy_impact_checkbox.checked: |
| 336 | disable_privacy_impact_checkbox.click() |
| 337 | |
| 338 | |
Borjan Tchakaloff | d4ce807 | 2018-05-16 18:20:13 +0200 | [diff] [blame] | 339 | def get_proxy_apk(android_sdk, flavour): |
| 340 | if android_sdk >= 24: |
| 341 | return PREBUILT_PROXY_APK_PATTERN.format(sdk=24, flavour=flavour) |
| 342 | else: |
| 343 | return PREBUILT_PROXY_APK_PATTERN.format(sdk=19, flavour=flavour) |
| 344 | |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 345 | |
Borjan Tchakaloff | a4bdae1 | 2017-11-21 14:58:12 +0100 | [diff] [blame] | 346 | # Prepare the DUT |
Borjan Tchakaloff | d4ce807 | 2018-05-16 18:20:13 +0200 | [diff] [blame] | 347 | def prepare_dut(dut, scenarios_dir, data_dir, prebuilts_dir): |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 348 | flavour = "gms" if is_gms_device(dut.serial) else "sibon" |
Borjan Tchakaloff | d4ce807 | 2018-05-16 18:20:13 +0200 | [diff] [blame] | 349 | proxy_apk = get_proxy_apk(dut.sdk, flavour) |
Franz-Xaver Geiger | b3f7c98 | 2018-04-12 09:29:32 +0200 | [diff] [blame] | 350 | |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 351 | # Uninstall the smartviser apps |
Borjan Tchakaloff | d4ce807 | 2018-05-16 18:20:13 +0200 | [diff] [blame] | 352 | for app in PREBUILT_APKS + [proxy_apk]: |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 353 | print("Uninstalling `{}`…".format(app)) |
Borjan Tchakaloff | d4ce807 | 2018-05-16 18:20:13 +0200 | [diff] [blame] | 354 | uninstall_apk(dut.serial, app, prebuilts_dir) |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 355 | |
Borjan Tchakaloff | a4bdae1 | 2017-11-21 14:58:12 +0100 | [diff] [blame] | 356 | # Copy the scenarios |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 357 | print("Pushing scenarios from `{}`…".format(scenarios_dir)) |
| 358 | adb("push", scenarios_dir, "/sdcard/viser", serial=dut.serial) |
Borjan Tchakaloff | a4bdae1 | 2017-11-21 14:58:12 +0100 | [diff] [blame] | 359 | |
| 360 | # Copy the scenarios data |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 361 | print("Pushing scenarios data from `{}`…".format(data_dir)) |
| 362 | adb("push", data_dir, "/sdcard/viser/data", serial=dut.serial) |
Borjan Tchakaloff | a4bdae1 | 2017-11-21 14:58:12 +0100 | [diff] [blame] | 363 | |
Franz-Xaver Geiger | b3f7c98 | 2018-04-12 09:29:32 +0200 | [diff] [blame] | 364 | # Install the smartviser apps (starting with the proxy app) |
Borjan Tchakaloff | d4ce807 | 2018-05-16 18:20:13 +0200 | [diff] [blame] | 365 | for app in [proxy_apk] + PREBUILT_APKS: |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 366 | print("Installing `{}`…".format(app)) |
Borjan Tchakaloff | d2c92ce | 2018-08-24 17:13:57 +0200 | [diff] [blame] | 367 | install_apk(dut, app, prebuilts_dir) |
Franz-Xaver Geiger | b28348e | 2018-02-19 14:41:40 +0100 | [diff] [blame] | 368 | |
Borjan Tchakaloff | a4bdae1 | 2017-11-21 14:58:12 +0100 | [diff] [blame] | 369 | |
| 370 | # Grant the permissions through the UI |
| 371 | def configure_perms(dut): |
Borjan Tchakaloff | a4bdae1 | 2017-11-21 14:58:12 +0100 | [diff] [blame] | 372 | # Input the credentials |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 373 | dut(resourceId="android:id/content").child(text="Username").child( |
| 374 | className="android.widget.EditText" |
| 375 | ).set_text(VWS_CREDENTIALS["user"]) |
| 376 | dut(resourceId="android:id/content").child(text="Password").child( |
| 377 | className="android.widget.EditText" |
| 378 | ).set_text(VWS_CREDENTIALS["password"]) |
Borjan Tchakaloff | a4bdae1 | 2017-11-21 14:58:12 +0100 | [diff] [blame] | 379 | |
| 380 | # Sign in |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 381 | signin_label = "SIGN IN" if dut.sdk >= 24 else "Sign in" |
| 382 | dut(resourceId="android:id/content").child( |
| 383 | text=signin_label, className="android.widget.Button" |
| 384 | ).click() |
| 385 | |
Borjan Tchakaloff | a4bdae1 | 2017-11-21 14:58:12 +0100 | [diff] [blame] | 386 | |
| 387 | def configure_sms(dut): |
| 388 | # TODO wait for the connection to be established and time-out |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 389 | prompt = dut(resourceId="android:id/content").child( |
| 390 | text="Viser must be your SMS app to send messages" |
| 391 | ) |
Borjan Tchakaloff | a4bdae1 | 2017-11-21 14:58:12 +0100 | [diff] [blame] | 392 | while not prompt.exists: |
| 393 | time.sleep(1) |
| 394 | |
| 395 | # Make viser the default SMS app |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 396 | dut(resourceId="android:id/content").child_by_text( |
| 397 | "Viser must be your SMS app to send messages", |
| 398 | className="android.widget.LinearLayout", |
| 399 | ).child(text="OK", className="android.widget.Button").click() |
Borjan Tchakaloff | a4bdae1 | 2017-11-21 14:58:12 +0100 | [diff] [blame] | 400 | |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 401 | dut(resourceId="android:id/content").child_by_text( |
| 402 | "Change SMS app?", className="android.widget.LinearLayout" |
| 403 | ).child(text="Yes", className="android.widget.Button").click() |
| 404 | |
Borjan Tchakaloff | a4bdae1 | 2017-11-21 14:58:12 +0100 | [diff] [blame] | 405 | |
| 406 | def configure_settings(dut): |
| 407 | # Set the e-mail account |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 408 | dut(text="Settings", className="android.widget.TextView").click() |
| 409 | dut(resourceId="android:id/list").child_by_text( |
| 410 | "User settings", className="android.widget.LinearLayout" |
| 411 | ).click() |
| 412 | dut(resourceId="android:id/list").child_by_text( |
| 413 | "Email account", className="android.widget.LinearLayout" |
| 414 | ).click() |
| 415 | prompt = dut(resourceId="android:id/content").child_by_text( |
| 416 | "Email account", className="android.widget.LinearLayout" |
| 417 | ) |
| 418 | prompt.child(resourceId="android:id/edit").set_text("fairphone.viser@gmail.com") |
| 419 | prompt.child(text="OK", className="android.widget.Button").click() |
| 420 | dut(resourceId="android:id/list").child_by_text( |
| 421 | "Email password", className="android.widget.LinearLayout" |
| 422 | ).click() |
| 423 | dut(text="Password :").child(className="android.widget.EditText").set_text( |
| 424 | "fairphoneviser2017" |
| 425 | ) |
| 426 | dut(description="OK", className="android.widget.TextView").click() |
Borjan Tchakaloff | a4bdae1 | 2017-11-21 14:58:12 +0100 | [diff] [blame] | 427 | dut.press.back() |
| 428 | dut.press.back() |
| 429 | |
Borjan Tchakaloff | a4bdae1 | 2017-11-21 14:58:12 +0100 | [diff] [blame] | 430 | |
Franz-Xaver Geiger | d1079e2 | 2018-02-19 14:34:15 +0100 | [diff] [blame] | 431 | def deploy(): |
| 432 | serials = [] |
Borjan Tchakaloff | a4bdae1 | 2017-11-21 14:58:12 +0100 | [diff] [blame] | 433 | |
Franz-Xaver Geiger | d1079e2 | 2018-02-19 14:34:15 +0100 | [diff] [blame] | 434 | if len(sys.argv) > 1: |
| 435 | serials.append(sys.argv[1]) |
| 436 | else: |
Franz-Xaver Geiger | b0f5542 | 2018-04-17 10:33:37 +0200 | [diff] [blame] | 437 | serials = list_devices() |
Borjan Tchakaloff | a4bdae1 | 2017-11-21 14:58:12 +0100 | [diff] [blame] | 438 | |
Franz-Xaver Geiger | d1079e2 | 2018-02-19 14:34:15 +0100 | [diff] [blame] | 439 | for serial in serials: |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 440 | print("Configuring device {}…".format(serial)) |
Borjan Tchakaloff | a4bdae1 | 2017-11-21 14:58:12 +0100 | [diff] [blame] | 441 | |
Franz-Xaver Geiger | d1079e2 | 2018-02-19 14:34:15 +0100 | [diff] [blame] | 442 | dut = Device(serial) |
Borjan Tchakaloff | 74d962a | 2018-04-11 17:47:14 +0200 | [diff] [blame] | 443 | # Work around the not-so-easy Device class |
| 444 | dut.serial = serial |
Borjan Tchakaloff | d4ce807 | 2018-05-16 18:20:13 +0200 | [diff] [blame] | 445 | # Cache the Android SDK version (dut.info fetches system properties) |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 446 | dut.sdk = dut.info["sdkInt"] |
Borjan Tchakaloff | a4bdae1 | 2017-11-21 14:58:12 +0100 | [diff] [blame] | 447 | |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 448 | try: |
| 449 | # Make sure the screen stays on - we're going to use UI automation |
| 450 | force_awake(serial) |
Borjan Tchakaloff | d4ce807 | 2018-05-16 18:20:13 +0200 | [diff] [blame] | 451 | unlock(dut) |
Borjan Tchakaloff | 64ec42b | 2018-02-07 11:33:15 -0800 | [diff] [blame] | 452 | |
Borjan Tchakaloff | 74d962a | 2018-04-11 17:47:14 +0200 | [diff] [blame] | 453 | # Configure common Fairphone Wi-Fi networks |
Borjan Tchakaloff | d4ce807 | 2018-05-16 18:20:13 +0200 | [diff] [blame] | 454 | if dut.sdk < 24: |
| 455 | configure_wifi_networks(dut, FAIRPHONE_WIFI_NETWORKS) |
| 456 | else: |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 457 | print( |
| 458 | "Uh oh, the device is running Android SDK {} on which we " |
| 459 | "do not deploy Wi-Fi networks yet.".format(dut.sdk) |
| 460 | ) |
Borjan Tchakaloff | 74d962a | 2018-04-11 17:47:14 +0200 | [diff] [blame] | 461 | |
Karsten Tausche | b36529f | 2019-03-02 14:13:29 +0100 | [diff] [blame] | 462 | # Disable Privacy Impact popup on Android 5. |
| 463 | if dut.sdk <= 22: |
| 464 | disable_privacy_impact_popup(dut) |
| 465 | |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 466 | # Push the scenarios, their data, and install the apps |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 467 | prepare_dut(dut, "../scenarios", "../scenarios-data", PREBUILTS_PATH) |
Borjan Tchakaloff | a4bdae1 | 2017-11-21 14:58:12 +0100 | [diff] [blame] | 468 | |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 469 | # Start the viser app |
| 470 | adb( |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 471 | "shell", |
| 472 | "monkey", |
| 473 | "-p", |
| 474 | "com.lunarlabs.panda", |
| 475 | "-c", |
| 476 | "android.intent.category.LAUNCHER", |
| 477 | "1", |
| 478 | serial=serial, |
| 479 | ) |
Borjan Tchakaloff | a4bdae1 | 2017-11-21 14:58:12 +0100 | [diff] [blame] | 480 | |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 481 | configure_perms(dut) |
Borjan Tchakaloff | a4bdae1 | 2017-11-21 14:58:12 +0100 | [diff] [blame] | 482 | |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 483 | # TODO DO NOT DO THE FOLLOWING IF NO SIM CARD IS IN THE DUT |
| 484 | # time.sleep(10) |
| 485 | # configure_sms(dut) |
Franz-Xaver Geiger | d1079e2 | 2018-02-19 14:34:15 +0100 | [diff] [blame] | 486 | |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 487 | configure_settings(dut) |
| 488 | except (HostCommandError, DeviceCommandError) as e: |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 489 | print("ERROR {}".format(e), file=sys.stderr) |
Borjan Tchakaloff | de9fa0f | 2018-04-12 12:05:48 +0200 | [diff] [blame] | 490 | finally: |
| 491 | try: |
| 492 | # Leave the device alone now |
| 493 | force_awake(serial, always=False) |
| 494 | except DeviceCommandError as e: |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 495 | print("WARNING {}".format(e), file=sys.stderr) |
Borjan Tchakaloff | 64ec42b | 2018-02-07 11:33:15 -0800 | [diff] [blame] | 496 | |
Franz-Xaver Geiger | d1079e2 | 2018-02-19 14:34:15 +0100 | [diff] [blame] | 497 | |
Borjan Tchakaloff | 7d4f615 | 2020-01-24 15:41:00 +0100 | [diff] [blame^] | 498 | if __name__ == "__main__": |
Franz-Xaver Geiger | d1079e2 | 2018-02-19 14:34:15 +0100 | [diff] [blame] | 499 | deploy() |