Skip to content

Commit

Permalink
fix long_click, use version_compat to check apk version
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed May 22, 2024
1 parent 72ae3e2 commit 32437ef
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 23 deletions.
11 changes: 10 additions & 1 deletion mobile_tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,13 @@ def call(self):
assert 2 == a.n



def test_is_version_compatiable():
assert utils.is_version_compatiable("1.0.0", "1.0.0")
assert utils.is_version_compatiable("1.0.0", "1.0.1")
assert utils.is_version_compatiable("1.0.0", "1.2.0")
assert utils.is_version_compatiable("1.0.1", "1.1.0")

assert not utils.is_version_compatiable("1.0.1", "2.1.0")
assert not utils.is_version_compatiable("1.3.1", "1.3.0")
assert not utils.is_version_compatiable("1.3.1", "1.2.0")
assert not utils.is_version_compatiable("1.3.1", "1.2.2")
3 changes: 2 additions & 1 deletion uiautomator2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,13 @@ def double_click(self, x, y, duration=0.1):

def long_click(self, x, y, duration: float = .5):
'''long click at arbitrary coordinates.
Args:
duration (float): seconds of pressed
'''
x, y = self.pos_rel2abs(x, y)
with self._operation_delay("click"):
return self.touch.down(x, y).sleep(duration).up(x, y)
self.jsonrpc.click(x, y, int(duration*1000))

Check warning on line 418 in uiautomator2/__init__.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/__init__.py#L418

Added line #L418 was not covered by tests

def swipe(self, fx, fy, tx, ty, duration: Optional[float] = None, steps: Optional[int] = None):
"""
Expand Down
7 changes: 4 additions & 3 deletions uiautomator2/assets/sync.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

set -e

APK_VERSION="2.3.11"
# AGENT_VERSION="0.10.1"
APK_VERSION=$(cat ../version.py| grep apk_version | awk '{print $NF}')
APK_VERSION=${APK_VERSION//[\"\']}

cd "$(dirname $0)"

Expand All @@ -30,7 +30,8 @@ function download_apk(){
# download_atx_agent "$AGENT_VERSION"
# echo "atx_agent_version: $AGENT_VERSION" >> version.txt

echo "APK_VERSION: $APK_VERSION"

download_apk "$APK_VERSION" "app-uiautomator.apk"
download_apk "$APK_VERSION" "app-uiautomator-test.apk"

echo "apk_version: $APK_VERSION" > version.txt
32 changes: 18 additions & 14 deletions uiautomator2/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

from uiautomator2.exceptions import RPCInvalidError, RPCStackOverflowError, UiAutomationNotConnectedError, HTTPError, LaunchUiAutomationError, UiObjectNotFoundError, RPCUnknownError, APKSignatureError, AccessibilityServiceAlreadyRegisteredError
from uiautomator2.abstract import AbstractUiautomatorServer
from uiautomator2.utils import is_version_compatiable
from uiautomator2.version import __apk_version__


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -157,7 +159,7 @@ def __init__(self, dev: adbutils.AdbDevice) -> None:
self._lock = threading.Lock()
self._debug = False
self.start_uiautomator()
atexit.register(self.stop_uiautomator)
atexit.register(self.stop_uiautomator, wait=False)

Check warning on line 162 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L162

Added line #L162 was not covered by tests

@property
def debug(self) -> bool:
Expand Down Expand Up @@ -191,30 +193,24 @@ def _do_start_uiautomator(self):
if not self._check_alive():
self._process = launch_uiautomator(self._dev)
self._wait_ready()

def _setup_apks(self):
assets_dir = Path(__file__).parent / "assets"
main_apk = assets_dir / "app-uiautomator.apk"
test_apk = assets_dir / "app-uiautomator-test.apk"

# get apk version
version_text = assets_dir / "version.txt"
apk_version = None
for line in version_text.read_text('utf-8').splitlines():
k, v = line.split(":")
if k == "apk_version":
apk_version = v.strip()
break
logger.debug("use apk_version: %s", apk_version)
logger.debug("use apk_version: %s", __apk_version__)

Check warning on line 202 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L202

Added line #L202 was not covered by tests
# install apk when not installed or version not match, dev version always keep
main_apk_info = self._dev.app_info("com.github.uiautomator")
if main_apk_info is None:
self._install_apk(main_apk)
elif main_apk_info.version_name != apk_version:
elif main_apk_info.version_name != __apk_version__:
if "dev" in main_apk_info.version_name or "dirty" in main_apk_info.version_name:
logger.debug("skip version check for %s", main_apk_info.version_name)
elif is_version_compatiable(__apk_version__, main_apk_info.version_name):
logger.debug("apk version compatiable, expect %s, actual %s", __apk_version__, main_apk_info.version_name)

Check warning on line 211 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L211

Added line #L211 was not covered by tests
else:
logger.debug("apk version not match, reinstall")
logger.debug("apk version not ok, expect %s, actual %s", __apk_version__, main_apk_info.version_name)

Check warning on line 213 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L213

Added line #L213 was not covered by tests
self._dev.uninstall("com.github.uiautomator")
self._dev.uninstall("com.github.uiautomator.test")
self._install_apk(main_apk)
Expand Down Expand Up @@ -281,12 +277,20 @@ def _wait_ready(self, launch_timeout=30, service_timeout=30):
self._dev.shell("am startservice -a com.github.uiautomator.ACTION_START")
self._dev.shell("am start -n com.github.uiautomator/.ToastActivity -e showFloatWindow true")
self._wait_stub_ready(service_timeout)
time.sleep(1) # wait ATX goto background

Check warning on line 280 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L280

Added line #L280 was not covered by tests

def stop_uiautomator(self):
def stop_uiautomator(self, wait=True):
with self._lock:
if self._process:
self._process.kill()
self._process = None
# wait server quit
if wait:
deadline = time.time() + 10

Check warning on line 289 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L289

Added line #L289 was not covered by tests
while time.time() < deadline:
if not self._check_alive():
return
time.sleep(.5)

Check warning on line 293 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L292-L293

Added lines #L292 - L293 were not covered by tests

def jsonrpc_call(self, method: str, params: Any = None, timeout: float = 10) -> Any:
"""Send jsonrpc call to uiautomator2 server"""
Expand Down
26 changes: 26 additions & 0 deletions uiautomator2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,32 @@ def inner(self, *args, **kwargs):
return inner



def is_version_compatiable(expect_version: str, actual_version: str) -> bool:
"""
Check if the actual version is compatiable with the expect version
Args:
expect_version: expect version, e.g. 1.0.0
actual_version: actual version, e.g. 1.0.0
Returns:
bool: True if compatiable, otherwise False
"""
def _parse_version(version: str):
return tuple(map(int, version.split(".")))

Check warning on line 225 in uiautomator2/utils.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/utils.py#L224-L225

Added lines #L224 - L225 were not covered by tests

evs = _parse_version(expect_version)
avs = _parse_version(actual_version)
assert len(evs) == len(avs) == 3, "version format error"

Check warning on line 229 in uiautomator2/utils.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/utils.py#L227-L229

Added lines #L227 - L229 were not covered by tests
if evs[0] == avs[0]:
if evs[1] < avs[1]:
return True

Check warning on line 232 in uiautomator2/utils.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/utils.py#L232

Added line #L232 was not covered by tests
if evs[1] == avs[1]:
return evs[2] <= avs[2]
return False

Check warning on line 235 in uiautomator2/utils.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/utils.py#L234-L235

Added lines #L234 - L235 were not covered by tests


if __name__ == "__main__":
for n in (1, 10000, 10000000, 10000000000):
print(n, natualsize(n))
10 changes: 6 additions & 4 deletions uiautomator2/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
#

# version managed by poetry
__version__ = "0.0.0"
__version__ = '0.0.0'

# See ChangeLog for details

# __apk_version__ = '2.3.3'
# see release note for details <https://github.com/openatx/android-uiautomator-server/releases>
__apk_version__ = '2.3.11'

# old apk version history
# 2.3.3 make float windows smaller
# 2.3.2 merge pull requests # require atx-agent>=0.10.0
# 2.3.1 support minicapagent, rotationagent, minitouchagent
Expand Down Expand Up @@ -39,7 +41,7 @@
# __jar_version__ = 'v0.1.6' # no useless for now.
# v0.1.6 first release version

__atx_agent_version__ = '0.10.1' # sync.sh verison should also be updated
# __atx_agent_version__ = '0.10.1' # sync.sh verison should also be updated
# 0.10.1 update androidbinary version, https://github.com/openatx/atx-agent/issues/115
# 0.10.0 remove tunnel code, use androidx.test.runner
# 0.9.6 fix security reason for remote control device
Expand Down

0 comments on commit 32437ef

Please sign in to comment.