From 7b56fff987d586795f9f7ef80de2a91f960c5e0d Mon Sep 17 00:00:00 2001 From: codeskyblue Date: Mon, 27 May 2024 17:00:46 +0800 Subject: [PATCH] add more tests --- tests/test_android.py | 39 +++++++++++++++++++++++++++++++++++++ uiautodev/driver/android.py | 11 ++++++----- 2 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 tests/test_android.py diff --git a/tests/test_android.py b/tests/test_android.py new file mode 100644 index 0000000..b948b6c --- /dev/null +++ b/tests/test_android.py @@ -0,0 +1,39 @@ +from uiautodev.driver.android import parse_xml +from uiautodev.model import WindowSize + +xml = """ + + + + + + + + + +""" + +def test_parse_xml(): + node = parse_xml(xml.strip(), WindowSize(1000, 1000)) + assert node.name == "hierarchy" + assert len(node.children) == 1 + assert node.rect is None + + childnode = node.children[0] + assert childnode.name == "android.widget.FrameLayout" + assert len(childnode.children) == 2 + assert childnode.rect is not None + assert childnode.rect.x == 20 + assert childnode.rect.y == 30 + assert childnode.rect.width == 1000 + assert childnode.rect.height == 2000 + + +def test_parse_xml_display_id(): + node = parse_xml(xml.strip(), WindowSize(1000, 1000), display_id=0) + assert node.name == "hierarchy" + assert len(node.children) == 1 + + childnode = node.children[0] + assert childnode.name == "android.widget.FrameLayout" + assert len(childnode.children) == 1 \ No newline at end of file diff --git a/uiautodev/driver/android.py b/uiautodev/driver/android.py index b35af60..c27609c 100644 --- a/uiautodev/driver/android.py +++ b/uiautodev/driver/android.py @@ -146,7 +146,7 @@ def wake_up(self): self.adb_device.keyevent("WAKEUP") -def parse_xml(xml_data: str, wsize: WindowSize, display_id: int) -> Node: +def parse_xml(xml_data: str, wsize: WindowSize, display_id: Optional[int] = None) -> Node: root = ElementTree.fromstring(xml_data) node = parse_xml_element(root, wsize, display_id) if node is None: @@ -154,16 +154,17 @@ def parse_xml(xml_data: str, wsize: WindowSize, display_id: int) -> Node: return node -def parse_xml_element(element, wsize: WindowSize, display_id: int, indexes: List[int] = [0]) -> Optional[Node]: +def parse_xml_element(element, wsize: WindowSize, display_id: Optional[int], indexes: List[int] = [0]) -> Optional[Node]: """ Recursively parse an XML element into a dictionary format. """ name = element.tag if name == "node": name = element.attrib.get("class", "node") - curr_display_id = int(element.attrib.get("display-id", display_id)) - if curr_display_id != display_id: - return + if display_id is not None: + elem_display_id = int(element.attrib.get("display-id", display_id)) + if elem_display_id != display_id: + return bounds = None rect = None