mozdevice 4.2.0__tar.gz → 4.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,15 +1,29 @@
1
- Metadata-Version: 2.1
2
- Name: mozdevice
3
- Version: 4.2.0
4
- Summary: Mozilla-authored device management
5
- Home-page: https://wiki.mozilla.org/Auto-tools/Projects/Mozbase
6
- Author: Mozilla Automation and Testing Team
7
- Author-email: tools@lists.mozilla.org
8
- License: MPL
9
- Classifier: Programming Language :: Python :: 3.8
10
- Classifier: Programming Language :: Python :: 3.9
11
- Classifier: Programming Language :: Python :: 3.10
12
- Classifier: Programming Language :: Python :: 3.11
13
- Requires-Dist: mozlog>=6.0
14
-
15
- see https://firefox-source-docs.mozilla.org/mozbase/index.html
1
+ Metadata-Version: 2.4
2
+ Name: mozdevice
3
+ Version: 4.3.0
4
+ Summary: Mozilla-authored device management
5
+ Home-page: https://wiki.mozilla.org/Auto-tools/Projects/Mozbase
6
+ Author: Mozilla Automation and Testing Team
7
+ Author-email: tools@lists.mozilla.org
8
+ License: MPL
9
+ Classifier: Programming Language :: Python
10
+ Classifier: Programming Language :: Python :: 3 :: Only
11
+ Classifier: Programming Language :: Python :: 3.8
12
+ Classifier: Programming Language :: Python :: 3.9
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Requires-Python: >=3.8
18
+ Requires-Dist: mozlog>=6.0
19
+ Dynamic: author
20
+ Dynamic: author-email
21
+ Dynamic: classifier
22
+ Dynamic: description
23
+ Dynamic: home-page
24
+ Dynamic: license
25
+ Dynamic: requires-dist
26
+ Dynamic: requires-python
27
+ Dynamic: summary
28
+
29
+ see https://firefox-source-docs.mozilla.org/mozbase/index.html
@@ -14,6 +14,7 @@ import sys
14
14
  import tempfile
15
15
  import time
16
16
  import traceback
17
+ from shutil import copy2 as shutil_copy2
17
18
  from shutil import copytree
18
19
  from threading import Thread
19
20
 
@@ -21,6 +22,11 @@ from . import version_codes
21
22
 
22
23
  _TEST_ROOT = None
23
24
 
25
+ # Caches the user's response to the clear-app-data confirmation prompt so they
26
+ # are asked at most once per process. None until the user has been prompted,
27
+ # then True (confirmed) or False (declined).
28
+ _CLEAR_APP_DATA_CONFIRMED = None
29
+
24
30
 
25
31
  class ADBProcess:
26
32
  """ADBProcess encapsulates the data related to executing the adb process."""
@@ -85,11 +91,7 @@ class ADBProcess:
85
91
  # to be independent of the individual failing device.
86
92
  arg_string = " ".join(self.args)
87
93
  arg_string = re.sub(r" -s [\w-]+", "", arg_string)
88
- return "args: {}, exitcode: {}, stdout: {}".format(
89
- arg_string,
90
- self.exitcode,
91
- self.stdout,
92
- )
94
+ return f"args: {arg_string}, exitcode: {self.exitcode}, stdout: {self.stdout}"
93
95
 
94
96
  def __iter__(self):
95
97
  assert self.stdout_file == subprocess.PIPE
@@ -561,8 +563,16 @@ class ADBHost(ADBCommand):
561
563
 
562
564
  ::
563
565
 
564
- [{'device_serial': 'b313b945', 'state': 'device', 'product': 'd2vzw',
565
- 'usb': '1-7', 'device': 'd2vzw', 'model': 'SCH_I535' }]
566
+ [
567
+ {
568
+ "device_serial": "b313b945",
569
+ "state": "device",
570
+ "product": "d2vzw",
571
+ "usb": "1-7",
572
+ "device": "d2vzw",
573
+ "model": "SCH_I535",
574
+ }
575
+ ]
566
576
  """
567
577
  # b313b945 device usb:1-7 product:d2vzw model:SCH_I535 device:d2vzw
568
578
  # from Android system/core/adb/transport.c statename()
@@ -586,7 +596,7 @@ class ADBHost(ADBCommand):
586
596
  )
587
597
  except ValueError:
588
598
  self._logger.warning(
589
- "devices: Unable to parse " "remainder for device %s" % line
599
+ "devices: Unable to parse remainder for device %s" % line
590
600
  )
591
601
  devices.append(device)
592
602
  for device in devices:
@@ -642,9 +652,9 @@ def ADBDeviceFactory(
642
652
  # already created an ADBDevice which means we must only have
643
653
  # one device connected and we can re-use the existing ADBDevice.
644
654
  devices = list(ADBDEVICES.keys())
645
- assert (
646
- len(devices) == 1
647
- ), "Only one device may be connected if the device serial number is not specified."
655
+ assert len(devices) == 1, (
656
+ "Only one device may be connected if the device serial number is not specified."
657
+ )
648
658
  adbdevice = ADBDEVICES[devices[0]]
649
659
  elif (
650
660
  device is not None
@@ -753,67 +763,65 @@ class ADBDevice(ADBCommand):
753
763
  # BUILTINS is used to determine which commands can not be executed
754
764
  # via su or run-as. This set of possible builtin commands was
755
765
  # obtained from `man builtin` on Linux.
756
- BUILTINS = set(
757
- [
758
- "alias",
759
- "bg",
760
- "bind",
761
- "break",
762
- "builtin",
763
- "caller",
764
- "cd",
765
- "command",
766
- "compgen",
767
- "complete",
768
- "compopt",
769
- "continue",
770
- "declare",
771
- "dirs",
772
- "disown",
773
- "echo",
774
- "enable",
775
- "eval",
776
- "exec",
777
- "exit",
778
- "export",
779
- "false",
780
- "fc",
781
- "fg",
782
- "getopts",
783
- "hash",
784
- "help",
785
- "history",
786
- "jobs",
787
- "kill",
788
- "let",
789
- "local",
790
- "logout",
791
- "mapfile",
792
- "popd",
793
- "printf",
794
- "pushd",
795
- "pwd",
796
- "read",
797
- "readonly",
798
- "return",
799
- "set",
800
- "shift",
801
- "shopt",
802
- "source",
803
- "suspend",
804
- "test",
805
- "times",
806
- "trap",
807
- "true",
808
- "type",
809
- "typeset",
810
- "ulimit",
811
- "umask",
812
- "unalias",
813
- "unset",
814
- "wait",
815
- ]
816
- )
766
+ BUILTINS = set([
767
+ "alias",
768
+ "bg",
769
+ "bind",
770
+ "break",
771
+ "builtin",
772
+ "caller",
773
+ "cd",
774
+ "command",
775
+ "compgen",
776
+ "complete",
777
+ "compopt",
778
+ "continue",
779
+ "declare",
780
+ "dirs",
781
+ "disown",
782
+ "echo",
783
+ "enable",
784
+ "eval",
785
+ "exec",
786
+ "exit",
787
+ "export",
788
+ "false",
789
+ "fc",
790
+ "fg",
791
+ "getopts",
792
+ "hash",
793
+ "help",
794
+ "history",
795
+ "jobs",
796
+ "kill",
797
+ "let",
798
+ "local",
799
+ "logout",
800
+ "mapfile",
801
+ "popd",
802
+ "printf",
803
+ "pushd",
804
+ "pwd",
805
+ "read",
806
+ "readonly",
807
+ "return",
808
+ "set",
809
+ "shift",
810
+ "shopt",
811
+ "source",
812
+ "suspend",
813
+ "test",
814
+ "times",
815
+ "trap",
816
+ "true",
817
+ "type",
818
+ "typeset",
819
+ "ulimit",
820
+ "umask",
821
+ "unalias",
822
+ "unset",
823
+ "wait",
824
+ ])
817
825
 
818
826
  def __init__(
819
827
  self,
@@ -1128,6 +1136,8 @@ class ADBDevice(ADBCommand):
1128
1136
  self.run_as_package = run_as_package
1129
1137
 
1130
1138
  self._logger.debug("ADBDevice: %s" % self.__dict__)
1139
+ self.shell("settings put system accelerometer_rotation 0")
1140
+ self.shell("settings put system user_rotation 0")
1131
1141
 
1132
1142
  @property
1133
1143
  def is_rooted(self):
@@ -1162,14 +1172,15 @@ class ADBDevice(ADBCommand):
1162
1172
  devices = ADBHost(
1163
1173
  adb=self._adb_path, adb_host=self._adb_host, adb_port=self._adb_port
1164
1174
  ).devices()
1165
- if len(devices) > 1:
1175
+ ready_devices = [d for d in devices if d["state"] == "device"]
1176
+ if len(ready_devices) > 1:
1166
1177
  raise ValueError(
1167
1178
  "ADBDevice called with multiple devices "
1168
- "attached and no device specified"
1179
+ "available and no device specified"
1169
1180
  )
1170
- if len(devices) == 0:
1171
- raise ADBError("No connected devices found.")
1172
- device = devices[0]
1181
+ if len(ready_devices) == 0:
1182
+ raise ADBError("No ready devices found.")
1183
+ device = ready_devices[0]
1173
1184
 
1174
1185
  # Allow : in device serial if it matches a tcpip device serial.
1175
1186
  re_device_serial_tcpip = re.compile(r"[^:]+:[0-9]+$")
@@ -1331,7 +1342,7 @@ class ADBDevice(ADBCommand):
1331
1342
  char = file_obj.read(1).decode()
1332
1343
  if not char:
1333
1344
  break
1334
- if char != "\r" and char != "\n":
1345
+ if char not in {"\r", "\n"}:
1335
1346
  line = char + line
1336
1347
  elif line:
1337
1348
  # we have collected everything up to the beginning of the line
@@ -1530,6 +1541,43 @@ class ADBDevice(ADBCommand):
1530
1541
  def enable_run_as_for_path(self, path):
1531
1542
  return self._run_as_package is not None and path.startswith(self.package_dir)
1532
1543
 
1544
+ @property
1545
+ def _host_gcov_prefix(self):
1546
+ return os.getenv("GCOV_PREFIX")
1547
+
1548
+ @property
1549
+ def _target_gcov_prefix_strip(self):
1550
+ return os.getenv("GCOV_PREFIX_STRIP")
1551
+
1552
+ @property
1553
+ def _host_jscov_prefix(self):
1554
+ return os.getenv("JS_CODE_COVERAGE_OUTPUT_DIR")
1555
+
1556
+ @property
1557
+ def _target_gcov_prefix(self):
1558
+ if self._host_gcov_prefix is None or self._test_root is None:
1559
+ return None
1560
+ return posixpath.join(self._test_root, os.path.basename(self._host_gcov_prefix))
1561
+
1562
+ @property
1563
+ def _target_jscov_prefix(self):
1564
+ if self._host_jscov_prefix is None or self._test_root is None:
1565
+ return None
1566
+ return posixpath.join(
1567
+ self._test_root, os.path.basename(self._host_jscov_prefix)
1568
+ )
1569
+
1570
+ @property
1571
+ def target_ccov_env(self):
1572
+ env = {}
1573
+ if self._target_gcov_prefix is not None:
1574
+ env["GCOV_PREFIX"] = self._target_gcov_prefix
1575
+ if self._target_gcov_prefix_strip is not None:
1576
+ env["GCOV_PREFIX_STRIP"] = self._target_gcov_prefix_strip
1577
+ if self._target_jscov_prefix is not None:
1578
+ env["JS_CODE_COVERAGE_OUTPUT_DIR"] = self._target_jscov_prefix
1579
+ return env
1580
+
1533
1581
  @property
1534
1582
  def test_root(self):
1535
1583
  """
@@ -1600,13 +1648,11 @@ class ADBDevice(ADBCommand):
1600
1648
  if not self.is_rooted:
1601
1649
  # Note that /sdcard may be accessible while
1602
1650
  # /mnt/sdcard is not.
1603
- paths.extend(
1604
- [
1605
- "/sdcard/test_root",
1606
- "/storage/sdcard/test_root",
1607
- "/mnt/sdcard/test_root",
1608
- ]
1609
- )
1651
+ paths.extend([
1652
+ "/sdcard/test_root",
1653
+ "/storage/sdcard/test_root",
1654
+ "/mnt/sdcard/test_root",
1655
+ ])
1610
1656
 
1611
1657
  return self._try_test_root_candidates(paths)
1612
1658
 
@@ -2033,6 +2079,10 @@ class ADBDevice(ADBCommand):
2033
2079
  else:
2034
2080
  pass
2035
2081
 
2082
+ if env is None:
2083
+ env = {}
2084
+ env.update(self.target_ccov_env)
2085
+
2036
2086
  # prepend cwd and env to command if necessary
2037
2087
  if cwd:
2038
2088
  cmd = f"cd {cwd} && {cmd}"
@@ -2559,7 +2609,7 @@ class ADBDevice(ADBCommand):
2559
2609
  tmpf = tempfile.NamedTemporaryFile(mode="w", delete=False)
2560
2610
  tmpf.write("\n".join(commands))
2561
2611
  tmpf.close()
2562
- script = f"/sdcard/{os.path.basename(tmpf.name)}"
2612
+ script = posixpath.join("/data/local/tmp", os.path.basename(tmpf.name))
2563
2613
  self.push(tmpf.name, script)
2564
2614
  self.shell_output(
2565
2615
  f"sh {script}", enable_run_as=enable_run_as, timeout=timeout
@@ -3009,7 +3059,28 @@ class ADBDevice(ADBCommand):
3009
3059
  temp_parent = tempfile.mkdtemp()
3010
3060
  remote_name = os.path.basename(remote)
3011
3061
  new_local = os.path.join(temp_parent, remote_name)
3012
- copytree(local, new_local)
3062
+
3063
+ # The build system puts the tests in objdir, as symlinks pointing
3064
+ # to the actual file in the source tree. When the symlink target is
3065
+ # removed (e.g. by updating the checkout), the symlink itself is
3066
+ # not removed by `./mach build`. By default, shutil.copytree tries
3067
+ # to read symlinks and raises an error in this case (bug 1950855).
3068
+ # Ignore dangling symlinks to avoid this issue.
3069
+ #
3070
+ # shutil.copytree's ignore_dangling_symlinks=True should be used,
3071
+ # but https://bugzilla.mozilla.org/show_bug.cgi?id=1950855#c1 shows
3072
+ # that it is buggy. As an alternative, customize copy_function:
3073
+ def copy_ignore_broken_link(src, dst):
3074
+ try:
3075
+ return shutil_copy2(src, dst)
3076
+ except OSError as e:
3077
+ if e.errno == 2 and os.path.islink(src):
3078
+ self._logger.debug("Ignoring broken symlink: %s" % src)
3079
+ else:
3080
+ raise e
3081
+
3082
+ copytree(local, new_local, copy_function=copy_ignore_broken_link)
3083
+
3013
3084
  local = new_local
3014
3085
  # See do_sync_push in
3015
3086
  # https://android.googlesource.com/platform/system/core/+/master/adb/file_sync_client.cpp
@@ -3052,9 +3123,7 @@ class ADBDevice(ADBCommand):
3052
3123
  if "remote secure_mkdirs failed" not in str(e):
3053
3124
  raise
3054
3125
  self._logger.warning(
3055
- "remote secure_mkdirs failed push('{}', '{}') {}".format(
3056
- local, remote, str(e)
3057
- )
3126
+ f"remote secure_mkdirs failed push('{local}', '{remote}') {str(e)}"
3058
3127
  )
3059
3128
  # Work around change in Android where push creates
3060
3129
  # directories which can not be written by "other" by first
@@ -3165,6 +3234,32 @@ class ADBDevice(ADBCommand):
3165
3234
  copytree(local, original_local, dirs_exist_ok=True)
3166
3235
  shutil.rmtree(temp_parent)
3167
3236
 
3237
+ def pull_ccov(self):
3238
+ self._logger.info("Trying to pull code coverage data.")
3239
+
3240
+ if self._target_gcov_prefix is not None and self._host_gcov_prefix is not None:
3241
+ self._logger.info(
3242
+ f"Pulling gcov data from {self._target_gcov_prefix} to {self._host_gcov_prefix}."
3243
+ )
3244
+ try:
3245
+ self.pull(self._target_gcov_prefix, self._host_gcov_prefix)
3246
+ except ADBError as e:
3247
+ self._logger.warning(f"Unable to pull gcov artifacts due to {e}.")
3248
+
3249
+ if (
3250
+ self._target_jscov_prefix is not None
3251
+ and self._host_jscov_prefix is not None
3252
+ ):
3253
+ self._logger.info(
3254
+ f"Pulling js coverage data from {self._target_jscov_prefix} to {self._host_jscov_prefix}."
3255
+ )
3256
+ try:
3257
+ self.pull(self._target_jscov_prefix, self._host_jscov_prefix)
3258
+ except ADBError as e:
3259
+ self._logger.warning(
3260
+ f"Unable to pull js coverage artifacts due to {e}."
3261
+ )
3262
+
3168
3263
  def get_file(self, remote, offset=None, length=None, timeout=None):
3169
3264
  """Pull file from device and return the file's content
3170
3265
 
@@ -3366,9 +3461,9 @@ class ADBDevice(ADBCommand):
3366
3461
  if "No such process" not in str(e):
3367
3462
  raise
3368
3463
  pid_set = set(pid_list)
3369
- current_pid_set = set(
3370
- [str(proc[0]) for proc in self.get_process_list(timeout=timeout)]
3371
- )
3464
+ current_pid_set = set([
3465
+ str(proc[0]) for proc in self.get_process_list(timeout=timeout)
3466
+ ])
3372
3467
  pid_list = list(pid_set.intersection(current_pid_set))
3373
3468
  if not pid_list:
3374
3469
  break
@@ -3664,9 +3759,9 @@ class ADBDevice(ADBCommand):
3664
3759
  if uptime:
3665
3760
  m = re.match(r"up time: ((\d+) days, )*(\d{2}):(\d{2}):(\d{2})", uptime)
3666
3761
  if m:
3667
- uptime = "%d days %d hours %d minutes %d seconds" % tuple(
3668
- [int(g or 0) for g in m.groups()[1:]]
3669
- )
3762
+ uptime = "%d days %d hours %d minutes %d seconds" % tuple([
3763
+ int(g or 0) for g in m.groups()[1:]
3764
+ ])
3670
3765
  info["uptime"] = uptime
3671
3766
  return info
3672
3767
 
@@ -3929,16 +4024,17 @@ class ADBDevice(ADBCommand):
3929
4024
  """
3930
4025
  if self.version >= version_codes.M:
3931
4026
  permissions = [
3932
- "android.permission.READ_EXTERNAL_STORAGE",
3933
4027
  "android.permission.ACCESS_COARSE_LOCATION",
3934
4028
  "android.permission.ACCESS_FINE_LOCATION",
3935
4029
  "android.permission.CAMERA",
3936
4030
  "android.permission.RECORD_AUDIO",
3937
4031
  ]
3938
4032
  if self.version < version_codes.R:
3939
- # WRITE_EXTERNAL_STORAGE is no longer available
3940
- # in Android 11+
4033
+ # WRITE_EXTERNAL_STORAGE is no longer available in Android 11+
3941
4034
  permissions.append("android.permission.WRITE_EXTERNAL_STORAGE")
4035
+ if self.version < version_codes.TIRAMISU:
4036
+ # READ_EXTERNAL_STORAGE is no longer available in Android 13+
4037
+ permissions.append("android.permission.READ_EXTERNAL_STORAGE")
3942
4038
  self._logger.info("Granting important runtime permissions to %s" % app_name)
3943
4039
  for permission in permissions:
3944
4040
  try:
@@ -4076,6 +4172,64 @@ class ADBDevice(ADBCommand):
4076
4172
  break
4077
4173
  return package_name
4078
4174
 
4175
+ def install_app_baseline_profile(self, apk_path, replace=False, timeout=None):
4176
+ """Installs an app on the device. Utilize profgen to first extract out the
4177
+ .dm profile and use `adb install-multiple` to properly install the apk along
4178
+ with the generated baseline profile.
4179
+
4180
+ :param str apk_path: The apk file name to be installed.
4181
+ :param bool replace: If True, replace existing application.
4182
+ :param int timeout: The maximum time in
4183
+ seconds for any spawned adb process to complete before
4184
+ throwing an ADBTimeoutError.
4185
+ This timeout is per adb call. The total time spent
4186
+ may exceed this value. If it is not specified, the value
4187
+ set in the ADB constructor is used.
4188
+ :return: string - name of installed package.
4189
+ :raises: :exc:`ADBTimeoutError`
4190
+ :exc:`ADBError`
4191
+ """
4192
+ # dm files will always share the same stem as the apk file.
4193
+ dm_path = apk_path[:-3] + "dm"
4194
+ command = [
4195
+ "profgen",
4196
+ "extractProfile",
4197
+ "--apk",
4198
+ apk_path,
4199
+ "--output-dex-metadata",
4200
+ dm_path,
4201
+ "--profile-format",
4202
+ "V0_1_5_S",
4203
+ ]
4204
+ subprocess.run(command, check=True)
4205
+ dump_packages = "dumpsys package packages"
4206
+ packages_before = set(self.shell_output(dump_packages, attempts=3).split("\n"))
4207
+ cmd = ["install-multiple"]
4208
+ if replace:
4209
+ cmd.append("-r")
4210
+ cmd.append(apk_path)
4211
+ cmd.append(dm_path)
4212
+ data = self.command_output(cmd, timeout=timeout)
4213
+ if data.find("Success") == -1:
4214
+ raise ADBError(f"install failed for {apk_path}. Got: {data}")
4215
+ packages_after = set(self.shell_output(dump_packages, attempts=3).split("\n"))
4216
+ packages_diff = packages_after - packages_before
4217
+ package_name = None
4218
+ re_pkg = re.compile(r"\s+pkg=Package{[^ ]+ (.*)}")
4219
+ for diff in packages_diff:
4220
+ match = re_pkg.match(diff)
4221
+ if match:
4222
+ package_name = match.group(1)
4223
+ break
4224
+ output = self.shell_output(f"dumpsys package dexopt | grep -A 1 {package_name}")
4225
+ print(output)
4226
+ if "status=speed-profile" not in output:
4227
+ raise Exception(
4228
+ f"{package_name} did not install the baseline profile correctly"
4229
+ )
4230
+
4231
+ return package_name
4232
+
4079
4233
  def is_app_installed(self, app_name, timeout=None):
4080
4234
  """Returns True if an app is installed on the device.
4081
4235
 
@@ -4141,9 +4295,7 @@ class ADBDevice(ADBCommand):
4141
4295
  # starting a new instance may not be what we want depending on what
4142
4296
  # we want to do
4143
4297
  if fail_if_running and self.process_exist(app_name, timeout=timeout):
4144
- raise ADBError(
4145
- "Only one instance of an application may be running " "at once"
4146
- )
4298
+ raise ADBError("Only one instance of an application may be running at once")
4147
4299
 
4148
4300
  if grant_runtime_permissions:
4149
4301
  self.grant_runtime_permissions(app_name)
@@ -4151,12 +4303,10 @@ class ADBDevice(ADBCommand):
4151
4303
  acmd = ["am"] + ["startservice" if is_service else "start"]
4152
4304
  if wait:
4153
4305
  acmd.extend(["-W"])
4154
- acmd.extend(
4155
- [
4156
- "-n",
4157
- f"{app_name}/{activity_name}",
4158
- ]
4159
- )
4306
+ acmd.extend([
4307
+ "-n",
4308
+ f"{app_name}/{activity_name}",
4309
+ ])
4160
4310
  if intent:
4161
4311
  acmd.extend(["-a", intent])
4162
4312
 
@@ -4221,6 +4371,10 @@ class ADBDevice(ADBCommand):
4221
4371
  """
4222
4372
  extras = {}
4223
4373
 
4374
+ if moz_env is None:
4375
+ moz_env = {}
4376
+ moz_env.update(self.target_ccov_env)
4377
+
4224
4378
  if moz_env:
4225
4379
  # moz_env is expected to be a dictionary of environment variables:
4226
4380
  # Fennec itself will set them when launched
@@ -4286,6 +4440,10 @@ class ADBDevice(ADBCommand):
4286
4440
  """
4287
4441
  extras = {}
4288
4442
 
4443
+ if moz_env is None:
4444
+ moz_env = {}
4445
+ moz_env.update(self.target_ccov_env)
4446
+
4289
4447
  if moz_env:
4290
4448
  # moz_env is expected to be a dictionary of environment variables:
4291
4449
  # geckoview_example itself will set them when launched
@@ -4353,6 +4511,10 @@ class ADBDevice(ADBCommand):
4353
4511
  """
4354
4512
  extras = {}
4355
4513
 
4514
+ if moz_env is None:
4515
+ moz_env = {}
4516
+ moz_env.update(self.target_ccov_env)
4517
+
4356
4518
  if moz_env:
4357
4519
  # moz_env is expected to be a dictionary of environment variables:
4358
4520
  # geckoview_example itself will set them when launched
@@ -4419,6 +4581,45 @@ class ADBDevice(ADBCommand):
4419
4581
  # racey, but it's the best we can do)
4420
4582
  time.sleep(1)
4421
4583
 
4584
+ def confirm_clear_app_data(self, app_name):
4585
+ """Confirm with the user that the app's data may be cleared.
4586
+
4587
+ On non-rooted devices the app's data (i.e. profile) must be wiped with
4588
+ `pm clear` since the shell can't remove app-owned files. This prompts
4589
+ the user to confirm that destruction once and caches the response in a
4590
+ module-level global so subsequent callers in the same process reuse it
4591
+ instead of prompting again. The prompt is skipped (returning True) when
4592
+ the device is rooted or when running in CI (MOZ_AUTOMATION is set). The
4593
+ MOZ_ADB_PM_CLEAR_CHECK environment variable, when defined, overrides the
4594
+ prompt: "1" allows clearing the app data and "0" forbids it.
4595
+
4596
+ :param str app_name: The name of the app whose data will be cleared.
4597
+ :return: True if clearing the app data is confirmed, False otherwise.
4598
+ """
4599
+ global _CLEAR_APP_DATA_CONFIRMED # noqa
4600
+
4601
+ if self.is_rooted or "MOZ_AUTOMATION" in os.environ:
4602
+ return True
4603
+
4604
+ env_check = os.environ.get("MOZ_ADB_PM_CLEAR_CHECK")
4605
+ if env_check is not None:
4606
+ return env_check.strip() == "1"
4607
+
4608
+ if _CLEAR_APP_DATA_CONFIRMED is not None:
4609
+ return _CLEAR_APP_DATA_CONFIRMED
4610
+
4611
+ self._logger.info(
4612
+ f"WARNING: Device is not rooted, and the app data (i.e. profile) for "
4613
+ f"{app_name} will be cleared."
4614
+ )
4615
+ self._logger.info(
4616
+ "Confirm that you understand the full app data for the app will be "
4617
+ "removed. The entire profile of the app will be deleted (Yes/No): "
4618
+ )
4619
+ answer = input().strip().lower()
4620
+ _CLEAR_APP_DATA_CONFIRMED = answer in ("yes", "y")
4621
+ return _CLEAR_APP_DATA_CONFIRMED
4622
+
4422
4623
  def uninstall_app(self, app_name, reboot=False, timeout=None):
4423
4624
  """Uninstalls an app on the device.
4424
4625
 
@@ -4466,3 +4667,21 @@ class ADBDevice(ADBCommand):
4466
4667
  output = self.command_output(cmd, timeout=timeout)
4467
4668
  self.reboot(timeout=timeout)
4468
4669
  return output
4670
+
4671
+ def enable_notifications(self, package_id):
4672
+ """Using pm grant we enable notifications for an app
4673
+
4674
+ :param str package_id: The package_id for the app we are enabling notifications for
4675
+ :raises: :exc:`ADBTimeoutError`
4676
+ :exc:`ADBError`
4677
+ """
4678
+ self.shell(f"pm grant {package_id} android.permission.POST_NOTIFICATIONS")
4679
+
4680
+ def disable_notifications(self, package_id):
4681
+ """Using pm revoke we disable notifications for an app
4682
+
4683
+ :param str package_id: The package_id for the app we are disabling notifications for
4684
+ :raises: :exc:`ADBTimeoutError`
4685
+ :exc:`ADBError`
4686
+ """
4687
+ self.shell(f"pm revoke {package_id} android.permission.POST_NOTIFICATIONS")