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