Skip to content

Commit

Permalink
little bugfix for check com.github.uiautomator.test version
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Mar 20, 2024
1 parent 3b0dfd7 commit d94a304
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions uiautomator2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,11 @@ def reset_uiautomator(self, reason="unknown", depth=0):
- start uiautomator keeper(am instrument -w ...)
- wait until uiautomator service is ready
"""
# https://developer.android.google.cn/training/monitoring-device-state/doze-standby
# 让uiautomator进程不进入doze模式
# help: dumpsys deviceidle help
self.shell("dumpsys deviceidle whitelist +com.github.uiautomator; dumpsys deviceidle whitelist +com.github.uiautomator.test")

with self._filelock:
if depth >= 2:
raise GatewayError(
Expand Down Expand Up @@ -671,7 +676,6 @@ def _force_reset_uiautomator_v2(self, launch_test_app=False):
self._grant_app_permissions()
self.shell(['am', 'start', '-a', 'android.intent.action.MAIN', '-c',
'android.intent.category.LAUNCHER', '-n', package_name + "/" + ".ToastActivity"])

self.uiautomator.start()

# wait until uiautomator2 service is working
Expand Down Expand Up @@ -708,7 +712,7 @@ def _is_apk_required(self) -> bool:
return True

# 检查测试apk是否存在
if self._package_version("com.github.uiautomator.test") is None:
if not self._package_exists("com.github.uiautomator.test"):
return True
return False

Expand All @@ -727,12 +731,18 @@ def _is_apk_outdated(self):
return True
return False

def _package_exists(self, package_name: str) -> bool:
return self.shell(['pm', 'path', package_name]).exit_code == 0

def _package_version(self, package_name: str) -> Optional[packaging.version.Version]:
if self.shell(['pm', 'path', package_name]).exit_code != 0:
return None
dump_output = self.shell(['dumpsys', 'package', package_name]).output
m = re.compile(r'versionName=(?P<name>[\d.]+)').search(dump_output)
return packaging.version.parse(m.group('name') if m else "")
if m is None:
return None
try:
return packaging.version.parse(m.group('name'))
except packaging.version.InvalidVersion:
return None

def _grant_app_permissions(self):
self.logger.debug("grant permissions")
Expand Down

0 comments on commit d94a304

Please sign in to comment.