Skip to content

Commit 74ab3af

Browse files
authored
add landscape args to window_size (#139)
1 parent e194766 commit 74ab3af

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,14 @@ d.list_packages()
279279
# example output: ["com.example.hello"]
280280

281281
d.window_size()
282-
# example output: (1080, 1920)
283-
284-
d.rotation()
285-
# example output: 1
286-
# other possible valus: 0, 1, 2, 3
282+
# example output: (1080, 1920) when phone is portrait
283+
# example output: (1920, 1080) when phone is landscape
284+
d.window_size(landscape=True) # force landscape mode
285+
# example output: (1920, 1080)
286+
287+
d.rotation() -> int
288+
# example output: 0
289+
# 0: natural, 1: left, 2: right, 3: upsidedown
287290

288291
d.app_info("com.github.uiautomator")
289292
# example output: {"version_name": "1.1.7", "version_code": "1007"}

adbutils/shell.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -154,32 +154,24 @@ def switch_wifi(self, enable: bool):
154154
cmdargs = ["svc", "wifi", arglast]
155155
self.shell(cmdargs)
156156

157-
def window_size(self) -> WindowSize:
157+
def window_size(self, landscape: Optional[bool] = None) -> WindowSize:
158158
"""
159159
Return screen (width, height) in pixel, width and height will be swapped if rotation is 90 or 270
160160
161+
Args:
162+
landscape: bool, default None, if True, return (width, height), else return (height, width)
163+
161164
Returns:
162165
WindowSize
163166
164167
Raises:
165168
AdbError
166169
"""
167170
wsize = self._wm_size()
168-
horizontal = self.rotation() % 2 == 1
169-
logger.debug("get window size from 'wm size'", wsize, horizontal)
170-
return WindowSize(wsize.height, wsize.width) if horizontal else wsize
171-
172-
# the ", deviceHeight=xxx}]" is not correct
173-
# def _dumpsys_window_size(self) -> WindowSize:
174-
# output = self.shell("dumpsys display")
175-
# for line in output.splitlines():
176-
# m = _DISPLAY_RE.search(line, 0)
177-
# if not m:
178-
# continue
179-
# w = int(m.group("width"))
180-
# h = int(m.group("height"))
181-
# return WindowSize(w, h)
182-
# raise AdbError("get window size from 'dumpsys display' failed", output)
171+
if landscape is None:
172+
landscape = self.rotation() % 2 == 1
173+
logger.debug("get window size from 'wm size'", wsize, landscape)
174+
return WindowSize(wsize.height, wsize.width) if landscape else wsize
183175

184176
def _wm_size(self) -> WindowSize:
185177
output = self.shell("wm size")

test_real_device/test_device.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ def test_window_size(device: AdbDevice):
217217
w, h = device.window_size()
218218
assert isinstance(w, int)
219219
assert isinstance(h, int)
220+
221+
is_landscape = device.rotation() % 2 == 1
222+
nw, nh = device.window_size(not is_landscape)
223+
assert w == nh and h == nw
220224

221225

222226
def test_is_screen_on(device: AdbDevice):

0 commit comments

Comments
 (0)