Skip to content

Commit

Permalink
Implement an alternate screen rotation detection method to avoid the …
Browse files Browse the repository at this point in the history
…problem of unable to view the screen due to installation failure of RotationWatcher.apk

(cherry picked from commit b315ac0)
(cherry picked from commit f57e5ad)
  • Loading branch information
yimelia committed Feb 3, 2021
1 parent a6e8f68 commit 1b85fc7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
16 changes: 11 additions & 5 deletions airtest/core/android/adb.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,15 @@ def getprop(self, key, strip=True):
"""
prop = self.raw_shell(['getprop', key])
if strip:
prop = prop.rstrip('\r\n')
if "\r\r\n" in prop:
# Some mobile phones will output multiple lines of extra log
prop = prop.split("\r\r\n")
if len(prop) > 1:
prop = prop[-2]
else:
prop = prop[-1]
else:
prop = prop.strip("\r\n")
return prop

@property
Expand Down Expand Up @@ -572,8 +580,7 @@ def install_app(self, filepath, replace=False, install_options=None):
out = self.cmd(cmds)

if re.search(r"Failure \[.*?\]", out):
print(out)
raise AirtestError("Installation Failure")
raise AdbShellError("Installation Failure", repr(out))

return out

Expand Down Expand Up @@ -617,8 +624,7 @@ def install_multiple_app(self, filepath, replace=False, install_options=None):
return self.install_app(filepath, replace)

if re.search(r"Failure \[.*?\]", out):
print(out)
raise AirtestError("Installation Failure")
raise AdbShellError("Installation Failure", repr(out))

return out

Expand Down
8 changes: 7 additions & 1 deletion airtest/core/android/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,13 @@ def get_display_info(self):
"""
if self.ori_method == ORI_METHOD.MINICAP:
self.rotation_watcher.get_ready()
try:
self.rotation_watcher.get_ready()
except AdbShellError:
LOGGING.error("RotationWatcher.apk update failed, please try to reinstall manually.")
self.ori_method = ORI_METHOD.ADB
return self.adb.get_display_info()

try:
return self.minicap.get_display_info()
except (RuntimeError, AdbShellError, AdbError):
Expand Down
25 changes: 21 additions & 4 deletions airtest/core/android/rotation.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# -*- coding: utf-8 -*-
import time
import threading
import traceback
from airtest.core.error import AirtestError
from airtest.utils.snippet import reg_cleanup, is_exiting, on_method_ready
from airtest.utils.logger import get_logger
from airtest.core.android.constant import ROTATIONWATCHER_APK, ROTATIONWATCHER_PACKAGE
from airtest.core.android.constant import ROTATIONWATCHER_APK, ROTATIONWATCHER_PACKAGE, ORI_METHOD
LOGGING = get_logger(__name__)


Expand Down Expand Up @@ -58,7 +59,13 @@ def start(self):
initial orientation
"""
self._install_and_setup()
ori_method = ORI_METHOD.MINICAP
try:
self._install_and_setup()
except:
# install failed
LOGGING.error("RotationWatcher.apk update failed, please try to reinstall manually.")
ori_method = ORI_METHOD.ADB

def _refresh_by_ow():
line = self.ow_proc.stdout.readline()
Expand All @@ -72,9 +79,19 @@ def _refresh_by_ow():
ori = int(int(line) / 90)
return ori

def _refresh_by_adb():
ori = self.adb.getDisplayOrientation()
return ori

def _run():
while True:
ori = _refresh_by_ow()
if ori_method == ORI_METHOD.ADB:
ori = _refresh_by_adb()
if self.current_orientation == ori:
time.sleep(3)
continue
else:
ori = _refresh_by_ow()
if ori is None:
break
LOGGING.info('update orientation %s->%s' % (self.current_orientation, ori))
Expand All @@ -88,7 +105,7 @@ def _run():
LOGGING.error("cb: %s error" % cb)
traceback.print_exc()

self.current_orientation = _refresh_by_ow()
self.current_orientation = _refresh_by_ow() if ori_method != ORI_METHOD.ADB else _refresh_by_adb()

self._t = threading.Thread(target=_run, name="rotationwatcher")
# self._t.daemon = True
Expand Down

0 comments on commit 1b85fc7

Please sign in to comment.