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