Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed May 27, 2024
1 parent 32a6d25 commit 7b56fff
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
39 changes: 39 additions & 0 deletions tests/test_android.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from uiautodev.driver.android import parse_xml
from uiautodev.model import WindowSize

xml = """
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<hierarchy rotation="0">
<node display-id="0" index="0" text="" resource-id="" class="android.widget.FrameLayout" hint="" bounds="[20,30][1020,2030]">
<node display-id="1" index="1" text="" resource-id="com.android.systemui:id/backdrop" hint="">
<node index="0" text="" resource-id="com.android.systemui:id/backdrop_back" hint="" />
</node>
<node display-id="0" NAF="true" index="2" text="" resource-id="com.android.systemui:id/scrim_behind" class="android.view.View" package="com.android.systemui" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" visible-to-user="true" bounds="[0,0][1080,2340]" drawing-order="2" hint="" />
</node>
</hierarchy>
"""

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
11 changes: 6 additions & 5 deletions uiautodev/driver/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,24 +146,25 @@ 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:
raise AndroidDriverException("Failed to parse xml")
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
Expand Down

0 comments on commit 7b56fff

Please sign in to comment.