Skip to content

Commit

Permalink
Internal change.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 676074130
  • Loading branch information
Chris Rawles authored and The android_world Authors committed Sep 19, 2024
1 parent eaf564a commit 731aec1
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 108 deletions.
51 changes: 0 additions & 51 deletions android_world/env/representation_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

"""Tools for processing and representing accessibility trees."""

import copy
import dataclasses
from typing import Any, Optional
import xml.etree.ElementTree as ET
Expand Down Expand Up @@ -217,53 +216,3 @@ def process_node(node, is_root):

process_node(parsed_hierarchy, is_root=True)
return ui_elements


def resize_forest_bounds(
forest: android_accessibility_forest_pb2.AndroidAccessibilityForest | Any,
factor: int,
) -> android_accessibility_forest_pb2.AndroidAccessibilityForest | Any:
"""Creates a new accessibility forest with reduced bounds in screen by a factor of `factor`.
This includes reducing bounds for both windows and nodes.
Args:
forest: The original accessibility forest.
factor: The factor to change the bounds by.
Returns:
A new accessibility forest with modified bounds.
"""

def reduce_bounds(bounds):
"""Helper function to create new bounds reduced by half."""
return type(bounds)(
left=bounds.left // factor,
top=bounds.top // factor,
right=bounds.right // factor,
bottom=bounds.bottom // factor,
)

def copy_node_with_reduced_bounds(node):
"""Create a copy of the node with reduced bounds."""
new_node = type(node)()
new_node.CopyFrom(node)
if hasattr(new_node, 'bounds_in_screen'):
new_node.bounds_in_screen.CopyFrom(reduce_bounds(node.bounds_in_screen))
return new_node

new_forest = copy.deepcopy(forest)

for window in new_forest.windows:
# Reduce bounds for the window itself
if hasattr(window, 'bounds_in_screen'):
window.bounds_in_screen.CopyFrom(reduce_bounds(window.bounds_in_screen))

# Reduce bounds for all nodes in the window
new_nodes = []
for node in window.tree.nodes:
new_nodes.append(copy_node_with_reduced_bounds(node))
window.tree.ClearField('nodes')
window.tree.nodes.extend(new_nodes)

return new_forest
57 changes: 0 additions & 57 deletions android_world/env/representation_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

from absl.testing import absltest
from absl.testing import parameterized
from android_env.proto.a11y import android_accessibility_forest_pb2
from android_world.env import representation_utils


Expand Down Expand Up @@ -116,61 +115,5 @@ def test_normalize_bboxes(
self.assertEqual(ui_element.bbox, expected_normalized_bbox)


class TestResizeForestBounds(absltest.TestCase):

def setUp(self):
super().setUp()
# Create a mock forest for testing
self.forest = android_accessibility_forest_pb2.AndroidAccessibilityForest()

# Add a window
window = self.forest.windows.add()
window.bounds_in_screen.left = 0
window.bounds_in_screen.top = 0
window.bounds_in_screen.right = 1000
window.bounds_in_screen.bottom = 2000

# Add a node to the window
node = window.tree.nodes.add()
node.bounds_in_screen.left = 100
node.bounds_in_screen.top = 200
node.bounds_in_screen.right = 300
node.bounds_in_screen.bottom = 400

def test_resize_forest_bounds(self):
# Resize the forest by a factor of 2
resized_forest = representation_utils.resize_forest_bounds(self.forest, 2)

# Check the window bounds
self.assertEqual(resized_forest.windows[0].bounds_in_screen.left, 0)
self.assertEqual(resized_forest.windows[0].bounds_in_screen.top, 0)
self.assertEqual(resized_forest.windows[0].bounds_in_screen.right, 500)
self.assertEqual(resized_forest.windows[0].bounds_in_screen.bottom, 1000)

# Check the node bounds
self.assertEqual(
resized_forest.windows[0].tree.nodes[0].bounds_in_screen.left, 50
)
self.assertEqual(
resized_forest.windows[0].tree.nodes[0].bounds_in_screen.top, 100
)
self.assertEqual(
resized_forest.windows[0].tree.nodes[0].bounds_in_screen.right, 150
)
self.assertEqual(
resized_forest.windows[0].tree.nodes[0].bounds_in_screen.bottom, 200
)

# Ensure the original forest is unchanged
self.assertEqual(self.forest.windows[0].bounds_in_screen.right, 1000)
self.assertEqual(self.forest.windows[0].bounds_in_screen.bottom, 2000)
self.assertEqual(
self.forest.windows[0].tree.nodes[0].bounds_in_screen.right, 300
)
self.assertEqual(
self.forest.windows[0].tree.nodes[0].bounds_in_screen.bottom, 400
)


if __name__ == '__main__':
absltest.main()

0 comments on commit 731aec1

Please sign in to comment.