From 731aec1050cea7a930084f94a1ad854e1485a76a Mon Sep 17 00:00:00 2001 From: Chris Rawles Date: Wed, 18 Sep 2024 11:48:49 -0700 Subject: [PATCH] Internal change. PiperOrigin-RevId: 676074130 --- android_world/env/representation_utils.py | 51 ----------------- .../env/representation_utils_test.py | 57 ------------------- 2 files changed, 108 deletions(-) diff --git a/android_world/env/representation_utils.py b/android_world/env/representation_utils.py index 330ab4c..9fe70ad 100644 --- a/android_world/env/representation_utils.py +++ b/android_world/env/representation_utils.py @@ -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 @@ -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 diff --git a/android_world/env/representation_utils_test.py b/android_world/env/representation_utils_test.py index 2ab1783..e5b7f9b 100644 --- a/android_world/env/representation_utils_test.py +++ b/android_world/env/representation_utils_test.py @@ -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 @@ -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()