Skip to content

Commit

Permalink
add landscape args to window_size (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue authored Aug 29, 2024
1 parent e194766 commit 74ab3af
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,14 @@ d.list_packages()
# example output: ["com.example.hello"]

d.window_size()
# example output: (1080, 1920)

d.rotation()
# example output: 1
# other possible valus: 0, 1, 2, 3
# example output: (1080, 1920) when phone is portrait
# example output: (1920, 1080) when phone is landscape
d.window_size(landscape=True) # force landscape mode
# example output: (1920, 1080)

d.rotation() -> int
# example output: 0
# 0: natural, 1: left, 2: right, 3: upsidedown

d.app_info("com.github.uiautomator")
# example output: {"version_name": "1.1.7", "version_code": "1007"}
Expand Down
24 changes: 8 additions & 16 deletions adbutils/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,32 +154,24 @@ def switch_wifi(self, enable: bool):
cmdargs = ["svc", "wifi", arglast]
self.shell(cmdargs)

def window_size(self) -> WindowSize:
def window_size(self, landscape: Optional[bool] = None) -> WindowSize:
"""
Return screen (width, height) in pixel, width and height will be swapped if rotation is 90 or 270
Args:
landscape: bool, default None, if True, return (width, height), else return (height, width)
Returns:
WindowSize
Raises:
AdbError
"""
wsize = self._wm_size()
horizontal = self.rotation() % 2 == 1
logger.debug("get window size from 'wm size'", wsize, horizontal)
return WindowSize(wsize.height, wsize.width) if horizontal else wsize

# the ", deviceHeight=xxx}]" is not correct
# def _dumpsys_window_size(self) -> WindowSize:
# output = self.shell("dumpsys display")
# for line in output.splitlines():
# m = _DISPLAY_RE.search(line, 0)
# if not m:
# continue
# w = int(m.group("width"))
# h = int(m.group("height"))
# return WindowSize(w, h)
# raise AdbError("get window size from 'dumpsys display' failed", output)
if landscape is None:
landscape = self.rotation() % 2 == 1
logger.debug("get window size from 'wm size'", wsize, landscape)
return WindowSize(wsize.height, wsize.width) if landscape else wsize

def _wm_size(self) -> WindowSize:
output = self.shell("wm size")
Expand Down
4 changes: 4 additions & 0 deletions test_real_device/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ def test_window_size(device: AdbDevice):
w, h = device.window_size()
assert isinstance(w, int)
assert isinstance(h, int)

is_landscape = device.rotation() % 2 == 1
nw, nh = device.window_size(not is_landscape)
assert w == nh and h == nw


def test_is_screen_on(device: AdbDevice):
Expand Down

0 comments on commit 74ab3af

Please sign in to comment.