android-env 1.2.1__py3-none-any.whl → 1.2.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (145) hide show
  1. android_env/__init__.py +1 -1
  2. android_env/components/__init__.py +1 -1
  3. android_env/components/a11y/__init__.py +15 -0
  4. android_env/components/a11y/a11y_events.py +118 -0
  5. android_env/components/a11y/a11y_events_test.py +173 -0
  6. android_env/components/a11y/a11y_forests.py +128 -0
  7. android_env/components/a11y/a11y_forests_test.py +237 -0
  8. android_env/components/a11y/a11y_servicer.py +199 -0
  9. android_env/components/a11y/a11y_servicer_test.py +224 -0
  10. android_env/components/action_fns.py +132 -0
  11. android_env/components/action_fns_test.py +227 -0
  12. android_env/components/action_type.py +26 -3
  13. android_env/components/adb_call_parser.py +239 -196
  14. android_env/components/adb_call_parser_test.py +179 -209
  15. android_env/components/adb_controller.py +90 -52
  16. android_env/components/adb_controller_test.py +187 -16
  17. android_env/components/adb_log_stream.py +17 -5
  18. android_env/components/adb_log_stream_test.py +17 -3
  19. android_env/components/app_screen_checker.py +17 -15
  20. android_env/components/app_screen_checker_test.py +7 -8
  21. android_env/components/config_classes.py +203 -0
  22. android_env/components/coordinator.py +102 -338
  23. android_env/components/coordinator_test.py +59 -199
  24. android_env/components/device_settings.py +174 -0
  25. android_env/components/device_settings_test.py +228 -0
  26. android_env/components/dumpsys_thread.py +3 -4
  27. android_env/components/dumpsys_thread_test.py +1 -1
  28. android_env/components/errors.py +52 -10
  29. android_env/components/errors_test.py +110 -0
  30. android_env/components/log_stream.py +7 -5
  31. android_env/components/log_stream_test.py +1 -1
  32. android_env/components/logcat_thread.py +9 -8
  33. android_env/components/logcat_thread_test.py +3 -4
  34. android_env/components/{utils.py → pixel_fns.py} +20 -20
  35. android_env/components/{utils_test.py → pixel_fns_test.py} +20 -15
  36. android_env/components/setup_step_interpreter.py +47 -39
  37. android_env/components/setup_step_interpreter_test.py +4 -4
  38. android_env/components/simulators/__init__.py +1 -1
  39. android_env/components/simulators/base_simulator.py +116 -44
  40. android_env/components/simulators/base_simulator_test.py +131 -9
  41. android_env/components/simulators/emulator/__init__.py +1 -1
  42. android_env/components/simulators/emulator/emulator_launcher.py +67 -77
  43. android_env/components/simulators/emulator/emulator_launcher_test.py +153 -49
  44. android_env/components/simulators/emulator/emulator_simulator.py +276 -95
  45. android_env/components/simulators/emulator/emulator_simulator_test.py +314 -89
  46. android_env/components/simulators/fake/__init__.py +1 -1
  47. android_env/components/simulators/fake/fake_simulator.py +17 -25
  48. android_env/components/simulators/fake/fake_simulator_test.py +29 -12
  49. android_env/components/specs.py +18 -28
  50. android_env/components/specs_test.py +1 -44
  51. android_env/components/task_manager.py +48 -48
  52. android_env/components/task_manager_test.py +71 -60
  53. android_env/env_interface.py +37 -23
  54. android_env/environment.py +83 -51
  55. android_env/environment_test.py +68 -29
  56. android_env/loader.py +57 -43
  57. android_env/loader_test.py +115 -35
  58. android_env/proto/__init__.py +1 -1
  59. android_env/proto/a11y/__init__.py +15 -0
  60. android_env/proto/a11y/a11y.proto +75 -0
  61. android_env/proto/a11y/a11y_pb2.py +54 -0
  62. android_env/proto/a11y/a11y_pb2.pyi +49 -0
  63. android_env/proto/a11y/a11y_pb2_grpc.py +202 -0
  64. android_env/proto/a11y/android_accessibility_action.proto +32 -0
  65. android_env/proto/a11y/android_accessibility_action_pb2.py +37 -0
  66. android_env/proto/a11y/android_accessibility_action_pb2.pyi +13 -0
  67. android_env/proto/a11y/android_accessibility_action_pb2_grpc.py +24 -0
  68. android_env/proto/a11y/android_accessibility_forest.proto +29 -0
  69. android_env/proto/a11y/android_accessibility_forest_pb2.py +38 -0
  70. android_env/proto/a11y/android_accessibility_forest_pb2.pyi +13 -0
  71. android_env/proto/a11y/android_accessibility_forest_pb2_grpc.py +24 -0
  72. android_env/proto/a11y/android_accessibility_node_info.proto +122 -0
  73. android_env/proto/a11y/android_accessibility_node_info_clickable_span.proto +49 -0
  74. android_env/proto/a11y/android_accessibility_node_info_clickable_span_pb2.py +39 -0
  75. android_env/proto/a11y/android_accessibility_node_info_clickable_span_pb2.pyi +28 -0
  76. android_env/proto/a11y/android_accessibility_node_info_clickable_span_pb2_grpc.py +24 -0
  77. android_env/proto/a11y/android_accessibility_node_info_pb2.py +42 -0
  78. android_env/proto/a11y/android_accessibility_node_info_pb2.pyi +75 -0
  79. android_env/proto/a11y/android_accessibility_node_info_pb2_grpc.py +24 -0
  80. android_env/proto/a11y/android_accessibility_tree.proto +29 -0
  81. android_env/proto/a11y/android_accessibility_tree_pb2.py +38 -0
  82. android_env/proto/a11y/android_accessibility_tree_pb2.pyi +13 -0
  83. android_env/proto/a11y/android_accessibility_tree_pb2_grpc.py +24 -0
  84. android_env/proto/a11y/android_accessibility_window_info.proto +84 -0
  85. android_env/proto/a11y/android_accessibility_window_info_pb2.py +41 -0
  86. android_env/proto/a11y/android_accessibility_window_info_pb2.pyi +48 -0
  87. android_env/proto/a11y/android_accessibility_window_info_pb2_grpc.py +24 -0
  88. android_env/proto/a11y/rect.proto +30 -0
  89. android_env/proto/a11y/rect_pb2.py +37 -0
  90. android_env/proto/a11y/rect_pb2.pyi +17 -0
  91. android_env/proto/a11y/rect_pb2_grpc.py +24 -0
  92. android_env/proto/adb.proto +17 -6
  93. android_env/proto/adb_pb2.py +120 -107
  94. android_env/proto/adb_pb2.pyi +396 -0
  95. android_env/proto/adb_pb2_grpc.py +20 -0
  96. android_env/proto/emulator_controller.proto +68 -63
  97. android_env/proto/emulator_controller_pb2.py +142 -131
  98. android_env/proto/emulator_controller_pb2.pyi +672 -0
  99. android_env/proto/emulator_controller_pb2_grpc.py +505 -142
  100. android_env/proto/snapshot.proto +169 -0
  101. android_env/proto/snapshot_pb2.py +47 -0
  102. android_env/proto/snapshot_pb2.pyi +117 -0
  103. android_env/proto/snapshot_pb2_grpc.py +24 -0
  104. android_env/proto/snapshot_service.proto +289 -0
  105. android_env/proto/snapshot_service_pb2.py +54 -0
  106. android_env/proto/snapshot_service_pb2.pyi +86 -0
  107. android_env/proto/snapshot_service_pb2_grpc.py +487 -0
  108. android_env/proto/state.proto +63 -0
  109. android_env/proto/state_pb2.py +63 -0
  110. android_env/proto/state_pb2.pyi +85 -0
  111. android_env/proto/state_pb2_grpc.py +24 -0
  112. android_env/proto/task.proto +5 -1
  113. android_env/proto/task_pb2.py +42 -31
  114. android_env/proto/task_pb2.pyi +160 -0
  115. android_env/proto/task_pb2_grpc.py +20 -0
  116. android_env/wrappers/__init__.py +1 -1
  117. android_env/wrappers/a11y_grpc_wrapper.py +500 -0
  118. android_env/wrappers/a11y_grpc_wrapper_test.py +849 -0
  119. android_env/wrappers/base_wrapper.py +34 -13
  120. android_env/wrappers/base_wrapper_test.py +22 -16
  121. android_env/wrappers/discrete_action_wrapper.py +18 -17
  122. android_env/wrappers/discrete_action_wrapper_test.py +4 -4
  123. android_env/wrappers/flat_interface_wrapper.py +5 -5
  124. android_env/wrappers/flat_interface_wrapper_test.py +7 -11
  125. android_env/wrappers/float_pixels_wrapper.py +9 -10
  126. android_env/wrappers/float_pixels_wrapper_test.py +3 -3
  127. android_env/wrappers/gym_wrapper.py +19 -13
  128. android_env/wrappers/gym_wrapper_test.py +3 -5
  129. android_env/wrappers/image_rescale_wrapper.py +18 -21
  130. android_env/wrappers/image_rescale_wrapper_test.py +25 -37
  131. android_env/wrappers/last_action_wrapper.py +16 -13
  132. android_env/wrappers/last_action_wrapper_test.py +44 -51
  133. android_env/wrappers/rate_limit_wrapper.py +6 -3
  134. android_env/wrappers/rate_limit_wrapper_test.py +22 -1
  135. android_env/wrappers/tap_action_wrapper.py +16 -17
  136. android_env/wrappers/tap_action_wrapper_test.py +51 -16
  137. {android_env-1.2.1.dist-info → android_env-1.2.3.dist-info}/METADATA +14 -18
  138. android_env-1.2.3.dist-info/RECORD +141 -0
  139. {android_env-1.2.1.dist-info → android_env-1.2.3.dist-info}/WHEEL +1 -1
  140. android_env/proto/raw_observation.proto +0 -39
  141. android_env/proto/raw_observation_pb2.py +0 -27
  142. android_env/proto/raw_observation_pb2_grpc.py +0 -4
  143. android_env-1.2.1.dist-info/RECORD +0 -81
  144. {android_env-1.2.1.dist-info → android_env-1.2.3.dist-info/licenses}/LICENSE +0 -0
  145. {android_env-1.2.1.dist-info → android_env-1.2.3.dist-info}/top_level.txt +0 -0
android_env/__init__.py CHANGED
@@ -1,5 +1,5 @@
1
1
  # coding=utf-8
2
- # Copyright 2022 DeepMind Technologies Limited.
2
+ # Copyright 2024 DeepMind Technologies Limited.
3
3
  #
4
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
5
  # you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  # coding=utf-8
2
- # Copyright 2022 DeepMind Technologies Limited.
2
+ # Copyright 2024 DeepMind Technologies Limited.
3
3
  #
4
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
5
  # you may not use this file except in compliance with the License.
@@ -0,0 +1,15 @@
1
+ # coding=utf-8
2
+ # Copyright 2024 DeepMind Technologies Limited.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
@@ -0,0 +1,118 @@
1
+ # coding=utf-8
2
+ # Copyright 2024 DeepMind Technologies Limited.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ """Tools for accessing accessibility events."""
17
+
18
+ from collections.abc import Mapping
19
+ from typing import Any
20
+
21
+ from absl import logging
22
+ from android_env.proto.a11y import a11y_pb2
23
+ import numpy as np
24
+
25
+ from google.protobuf import any_pb2
26
+
27
+
28
+ _A11Y_EVENT_KEY = 'full_event'
29
+
30
+
31
+ def package_events_to_task_extras(
32
+ events: list[a11y_pb2.EventRequest],
33
+ ) -> Mapping[str, np.ndarray]:
34
+ if not events:
35
+ return {}
36
+ events = np.stack(events, axis=0)
37
+ return {_A11Y_EVENT_KEY: events}
38
+
39
+
40
+ def extract_events_from_task_extras(
41
+ task_extras: Mapping[str, Any] | None = None,
42
+ ) -> list[Mapping[str, str]]:
43
+ """Inspects task_extras and extracts all accessibility events detected.
44
+
45
+ Args:
46
+ task_extras: Task extras forwarded by AndroidEnv. If 'full_event' is not a
47
+ key in task_extras, then this function returns an empty string. Otherwise,
48
+ full_event is expected to be list to be a numpy array with one dimension,
49
+ and contains a list of dictionary describing accessibility events that are
50
+ present in the given task extras. e.g. 'event_type:
51
+ TYPE_WINDOW_CONTENT_CHANGED // event_package_name:
52
+ com.google.android.deskclock // source_class_name:
53
+ android.widget.ImageView'.
54
+
55
+ Returns:
56
+ List of all events detected
57
+ """
58
+ if task_extras is None or _A11Y_EVENT_KEY not in task_extras:
59
+ return []
60
+
61
+ if (
62
+ not isinstance(task_extras[_A11Y_EVENT_KEY], np.ndarray)
63
+ or task_extras[_A11Y_EVENT_KEY].ndim != 1
64
+ ):
65
+ raise ValueError(
66
+ f'{_A11Y_EVENT_KEY} task extra should be a numpy array with one'
67
+ ' dimension.'
68
+ )
69
+
70
+ if task_extras[_A11Y_EVENT_KEY].size == 0:
71
+ return []
72
+
73
+ events = []
74
+ for e in task_extras[_A11Y_EVENT_KEY]:
75
+ if isinstance(e, a11y_pb2.EventRequest):
76
+ events.append(dict(e.event))
77
+ elif isinstance(e, dict):
78
+ events.append(e)
79
+ logging.warning(
80
+ 'The event should come only from the a11y_grpc_wrapper. '
81
+ 'Please verify that the upacking operation has not been '
82
+ 'called twice. See here for full task_extras: %s',
83
+ task_extras,
84
+ )
85
+ elif isinstance(e, any_pb2.Any):
86
+ ev = a11y_pb2.EventRequest()
87
+ new_any = any_pb2.Any()
88
+ new_any.CopyFrom(e)
89
+ new_any.Unpack(ev)
90
+ events.append(dict(ev.event))
91
+
92
+ else:
93
+ raise TypeError(
94
+ f'Unexpected event type: {type(e)}. See here for full '
95
+ f'task_extras: {task_extras}.'
96
+ )
97
+
98
+ return events
99
+
100
+
101
+ def keep_latest_event_only(task_extras: dict[str, Any]):
102
+ """Removes all a11y events except the last one observed."""
103
+ if task_extras is None or 'full_event' not in task_extras:
104
+ return
105
+
106
+ if (
107
+ not isinstance(task_extras[_A11Y_EVENT_KEY], np.ndarray)
108
+ or task_extras[_A11Y_EVENT_KEY].ndim != 1
109
+ ):
110
+ raise ValueError(
111
+ f'{_A11Y_EVENT_KEY} task extra should be a numpy array with one'
112
+ ' dimension.'
113
+ )
114
+
115
+ if task_extras[_A11Y_EVENT_KEY].size == 0:
116
+ return []
117
+
118
+ task_extras[_A11Y_EVENT_KEY] = task_extras[_A11Y_EVENT_KEY][-1:]
@@ -0,0 +1,173 @@
1
+ # coding=utf-8
2
+ # Copyright 2024 DeepMind Technologies Limited.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ """Tests for a11y_events."""
17
+
18
+ from absl.testing import absltest
19
+ from absl.testing import parameterized
20
+ from android_env.components.a11y import a11y_events
21
+ from android_env.proto.a11y import a11y_pb2
22
+ import numpy as np
23
+
24
+ from google.protobuf import any_pb2
25
+
26
+
27
+ def _event_request(d: dict[str, str]) -> a11y_pb2.EventRequest:
28
+ event_request = a11y_pb2.EventRequest()
29
+ for k, v in d.items():
30
+ event_request.event[k] = v
31
+ return event_request
32
+
33
+
34
+ def _event_request_as_any(d: dict[str, str]) -> any_pb2.Any:
35
+ event_request = _event_request(d)
36
+ response = any_pb2.Any()
37
+ response.Pack(event_request)
38
+ return response
39
+
40
+
41
+ class A11yEventsTest(parameterized.TestCase):
42
+
43
+ @parameterized.parameters(
44
+ dict(task_extras={}),
45
+ dict(
46
+ task_extras={'no_full_event': [{'1': '1'}, {'2': '2'}, {'3': '3'}]},
47
+ ),
48
+ dict(
49
+ task_extras={'full_event': np.array([])},
50
+ ),
51
+ dict(
52
+ task_extras={},
53
+ ),
54
+ )
55
+ def test_no_events_in_task_extras(self, task_extras):
56
+ events = a11y_events.extract_events_from_task_extras(task_extras)
57
+ self.assertEmpty(events)
58
+
59
+ @parameterized.parameters(
60
+ dict(
61
+ task_extras={'full_event': [{'1': '1'}, {'2': '2'}]},
62
+ expected_events=[{'1': '1'}, {'2': '2'}],
63
+ ),
64
+ dict(
65
+ task_extras={'full_event': [{}]},
66
+ expected_events=[{}],
67
+ ),
68
+ dict(
69
+ task_extras={
70
+ 'full_event_wrong_key': [1, 2, 3],
71
+ 'full_event': [{'1': '1'}, {'2': '2'}, {'3': '3'}],
72
+ },
73
+ expected_events=[{'1': '1'}, {'2': '2'}, {'3': '3'}],
74
+ ),
75
+ )
76
+ def test_task_extras(self, task_extras, expected_events):
77
+ event_requests = [_event_request(e) for e in task_extras['full_event']]
78
+ task_extras['full_event'] = np.stack(event_requests, axis=0)
79
+ events = a11y_events.extract_events_from_task_extras(task_extras)
80
+ self.assertEqual(len(events), len(expected_events))
81
+ for i, event in enumerate(expected_events):
82
+ self.assertEqual(len(event), len(expected_events[i]))
83
+ for k, v in event.items():
84
+ self.assertIn(k, expected_events[i])
85
+ self.assertEqual(v, expected_events[i][k])
86
+
87
+ def test_events_key_has_dict_event_requrests(self):
88
+ event_requests = [
89
+ _event_request({'1': '1'}),
90
+ {'2': '2'},
91
+ _event_request({'3': '3'}),
92
+ ]
93
+ expected_events = [
94
+ {'1': '1'},
95
+ {'2': '2'},
96
+ {'3': '3'},
97
+ ]
98
+ task_extras = {'full_event': np.stack(event_requests, axis=0)}
99
+ events = a11y_events.extract_events_from_task_extras(task_extras)
100
+ self.assertEqual(len(events), len(expected_events))
101
+ for i, event in enumerate(expected_events):
102
+ self.assertEqual(len(event), len(expected_events[i]))
103
+ for k, v in event.items():
104
+ self.assertIn(k, expected_events[i])
105
+ self.assertEqual(v, expected_events[i][k])
106
+
107
+ def test_events_key_has__event_requrests_packed_as_any(self):
108
+ event_requests = [
109
+ _event_request_as_any({'1': '1'}),
110
+ {'2': '2'},
111
+ _event_request_as_any({'3': '3'}),
112
+ ]
113
+ expected_events = [
114
+ {'1': '1'},
115
+ {'2': '2'},
116
+ {'3': '3'},
117
+ ]
118
+ task_extras = {'full_event': np.stack(event_requests, axis=0)}
119
+ events = a11y_events.extract_events_from_task_extras(task_extras)
120
+ self.assertEqual(len(events), len(expected_events))
121
+ for i, event in enumerate(expected_events):
122
+ self.assertEqual(len(event), len(expected_events[i]))
123
+ for k, v in event.items():
124
+ self.assertIn(k, expected_events[i])
125
+ self.assertEqual(v, expected_events[i][k])
126
+
127
+ def test_events_key_has_non_event_requrests(self):
128
+ event_requests = [
129
+ _event_request({'1': '1'}),
130
+ 3, # Not an even and not a dict.
131
+ _event_request({'3': '3'}),
132
+ ]
133
+ task_extras = {'full_event': np.stack(event_requests, axis=0)}
134
+ with self.assertRaises(TypeError):
135
+ _ = a11y_events.extract_events_from_task_extras(task_extras)
136
+
137
+ @parameterized.parameters(
138
+ dict(task_extras={}, expected_extras={}),
139
+ dict(
140
+ task_extras={
141
+ 'no_full_event': 42,
142
+ },
143
+ expected_extras={
144
+ 'no_full_event': 42,
145
+ },
146
+ ),
147
+ dict(
148
+ task_extras={'full_event': np.array([1, 2]), 'no_full_event': 43},
149
+ expected_extras={'full_event': np.array([2]), 'no_full_event': 43},
150
+ ),
151
+ dict(
152
+ task_extras={'full_event': np.array([1, 2, 3])},
153
+ expected_extras={'full_event': np.array([3])},
154
+ ),
155
+ dict(
156
+ task_extras={'full_event': np.array([]), 'no_full_event': 44},
157
+ expected_extras={'full_event': np.array([]), 'no_full_event': 44},
158
+ ),
159
+ )
160
+ def test_keep_latest_only(self, task_extras, expected_extras):
161
+ a11y_events.keep_latest_event_only(task_extras)
162
+ self.assertEqual(len(task_extras), len(expected_extras))
163
+ for k, v in task_extras.items():
164
+ self.assertIn(k, expected_extras)
165
+ if k == 'full_event':
166
+ np.testing.assert_array_equal(v, expected_extras['full_event'])
167
+ else:
168
+ self.assertEqual(v, expected_extras[k])
169
+ pass
170
+
171
+
172
+ if __name__ == '__main__':
173
+ absltest.main()
@@ -0,0 +1,128 @@
1
+ # coding=utf-8
2
+ # Copyright 2024 DeepMind Technologies Limited.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ """Tools for accessing accessibility events."""
17
+
18
+ from collections.abc import Mapping
19
+ from typing import Any
20
+
21
+ from android_env.proto.a11y import android_accessibility_forest_pb2
22
+ import numpy as np
23
+
24
+ from google.protobuf import any_pb2
25
+
26
+
27
+ _A11Y_FORESTS_KEY = 'accessibility_tree'
28
+
29
+
30
+ def package_forests_to_task_extras(
31
+ forests: list[android_accessibility_forest_pb2.AndroidAccessibilityForest],
32
+ ) -> Mapping[str, np.ndarray]:
33
+ if not forests:
34
+ return {}
35
+ forests = np.stack(forests, axis=0)
36
+ return {_A11Y_FORESTS_KEY: forests}
37
+
38
+
39
+ def task_extras_has_forests(task_extras: Mapping[str, Any]) -> bool:
40
+ """Checks that the task_extras has any a11y forest information."""
41
+ if _A11Y_FORESTS_KEY not in task_extras:
42
+ return False
43
+
44
+ payload = task_extras[_A11Y_FORESTS_KEY]
45
+ if not isinstance(payload, np.ndarray) or payload.ndim != 1:
46
+ raise ValueError(
47
+ f'{_A11Y_FORESTS_KEY} task extra should be a numpy array with one'
48
+ f' dimension. payload: {payload}'
49
+ )
50
+
51
+ if payload.size == 0:
52
+ return False
53
+
54
+ if any(isinstance(f, any_pb2.Any) for f in payload):
55
+ # Forests were packed as Any.
56
+ return True
57
+
58
+ return any(
59
+ isinstance(f, android_accessibility_forest_pb2.AndroidAccessibilityForest)
60
+ for f in payload
61
+ )
62
+
63
+
64
+ def convert_to_forest(
65
+ forest: android_accessibility_forest_pb2.AndroidAccessibilityForest
66
+ | any_pb2.Any
67
+ | None,
68
+ ) -> android_accessibility_forest_pb2.AndroidAccessibilityForest | None:
69
+ """Takes an object and attempts to convert it to a forest."""
70
+ if forest is None:
71
+ return None
72
+
73
+ if isinstance(forest, any_pb2.Any):
74
+ output = android_accessibility_forest_pb2.AndroidAccessibilityForest()
75
+ new_any = any_pb2.Any()
76
+ new_any.CopyFrom(forest)
77
+ new_any.Unpack(output)
78
+ return output
79
+ elif isinstance(
80
+ forest, android_accessibility_forest_pb2.AndroidAccessibilityForest
81
+ ):
82
+ return forest
83
+ else:
84
+ return None
85
+
86
+
87
+ def extract_forests_from_task_extras(
88
+ task_extras: Mapping[str, Any] | None = None,
89
+ ) -> list[android_accessibility_forest_pb2.AndroidAccessibilityForest]:
90
+ """Inspects task_extras and extracts all accessibility forests detected.
91
+
92
+ Args:
93
+ task_extras: Task extras forwarded by AndroidEnv. If 'full_event' is not a
94
+ key in task_extras, then this function returns an empty string. Otherwise,
95
+ full_event is expected to be list to be a numpy array with one dimension,
96
+ and contains a list of dictionary describing accessibility forests that
97
+ are present in the given task extras.
98
+
99
+ Returns:
100
+ List of all forests detected
101
+ """
102
+ if task_extras is None or not task_extras_has_forests(task_extras):
103
+ return []
104
+
105
+ forests = []
106
+ for f in task_extras[_A11Y_FORESTS_KEY]:
107
+ f = convert_to_forest(f)
108
+ if f is not None:
109
+ forests.append(f)
110
+ return forests
111
+
112
+
113
+ def keep_latest_forest_only(task_extras: dict[str, Any]):
114
+ """Removes all a11y forests except the last one observed."""
115
+ if _A11Y_FORESTS_KEY not in task_extras.keys():
116
+ return
117
+
118
+ payload = task_extras[_A11Y_FORESTS_KEY]
119
+ if not isinstance(payload, np.ndarray) or payload.ndim != 1:
120
+ raise ValueError(
121
+ f'{_A11Y_FORESTS_KEY} task extra should be a numpy array with one'
122
+ f' dimension. payload: {payload}'
123
+ )
124
+
125
+ if payload.size == 0:
126
+ return
127
+
128
+ task_extras[_A11Y_FORESTS_KEY] = payload[-1:]
@@ -0,0 +1,237 @@
1
+ # coding=utf-8
2
+ # Copyright 2024 DeepMind Technologies Limited.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ """Tests for a11y_forests."""
17
+
18
+ from absl.testing import absltest
19
+ from absl.testing import parameterized
20
+ from android_env.components.a11y import a11y_forests
21
+ from android_env.proto.a11y import android_accessibility_forest_pb2
22
+ import numpy as np
23
+
24
+ from google.protobuf import any_pb2
25
+
26
+
27
+ def _pack_any(proto_message) -> any_pb2.Any:
28
+ response = any_pb2.Any()
29
+ response.Pack(proto_message)
30
+ return response
31
+
32
+
33
+ def _empty_forest() -> (
34
+ android_accessibility_forest_pb2.AndroidAccessibilityForest
35
+ ):
36
+ return android_accessibility_forest_pb2.AndroidAccessibilityForest()
37
+
38
+
39
+ def _one_empty_window_forest() -> (
40
+ android_accessibility_forest_pb2.AndroidAccessibilityForest
41
+ ):
42
+ forest = android_accessibility_forest_pb2.AndroidAccessibilityForest()
43
+ forest.windows.add()
44
+ return forest
45
+
46
+
47
+ def _two_window_forest() -> (
48
+ android_accessibility_forest_pb2.AndroidAccessibilityForest
49
+ ):
50
+ forest = android_accessibility_forest_pb2.AndroidAccessibilityForest()
51
+ window = forest.windows.add()
52
+ window.tree.nodes.add(
53
+ class_name='foo', is_clickable=True, hint_text='Foo hint'
54
+ )
55
+ forest.windows.add()
56
+ return forest
57
+
58
+
59
+ class A11YForestsTest(parameterized.TestCase):
60
+
61
+ @parameterized.parameters(
62
+ dict(task_extras={}, expected_forests=[], convert_to_np=[]),
63
+ dict(
64
+ task_extras={'accessibility_tree': []},
65
+ convert_to_np=['accessibility_tree'],
66
+ expected_forests=[],
67
+ ),
68
+ dict(
69
+ task_extras={
70
+ 'not_accessibility_tree': [
71
+ _empty_forest(),
72
+ _one_empty_window_forest(),
73
+ _two_window_forest(),
74
+ ],
75
+ },
76
+ convert_to_np=['not_accessibility_tree'],
77
+ expected_forests=[],
78
+ ),
79
+ dict(
80
+ task_extras={
81
+ 'accessibility_tree': [
82
+ _empty_forest(),
83
+ {'not_a_forest_key': 'nor_a_forest_value'},
84
+ _two_window_forest(),
85
+ ]
86
+ },
87
+ convert_to_np=['accessibility_tree'],
88
+ expected_forests=[_empty_forest(), _two_window_forest()],
89
+ ),
90
+ dict(
91
+ task_extras={
92
+ 'accessibility_tree': [
93
+ {'not_a_forest_key': 'nor_a_forest_value'},
94
+ 3,
95
+ 4,
96
+ {'not_a_forest_key': _empty_forest()},
97
+ ],
98
+ },
99
+ convert_to_np=['accessibility_tree'],
100
+ expected_forests=[],
101
+ ),
102
+ dict(
103
+ task_extras={'accessibility_tree': []},
104
+ convert_to_np=['accessibility_tree'],
105
+ expected_forests=[],
106
+ ),
107
+ dict(
108
+ task_extras={
109
+ 'accessibility_tree_wrong_key': [1, 2, 3],
110
+ 'accessibility_tree': [
111
+ _empty_forest(),
112
+ None,
113
+ None,
114
+ _one_empty_window_forest(),
115
+ _two_window_forest(),
116
+ ],
117
+ },
118
+ convert_to_np=['accessibility_tree', 'accessibility_tree_wrong_key'],
119
+ expected_forests=[
120
+ _empty_forest(),
121
+ _one_empty_window_forest(),
122
+ _two_window_forest(),
123
+ ],
124
+ ),
125
+ dict(
126
+ task_extras={
127
+ 'accessibility_tree_wrong_key': [1, 2, 3],
128
+ 'accessibility_tree': [
129
+ None,
130
+ _pack_any(_empty_forest()),
131
+ _pack_any(_one_empty_window_forest()),
132
+ _pack_any(_two_window_forest()),
133
+ ],
134
+ },
135
+ convert_to_np=['accessibility_tree', 'accessibility_tree_wrong_key'],
136
+ expected_forests=[
137
+ _empty_forest(),
138
+ _one_empty_window_forest(),
139
+ _two_window_forest(),
140
+ ],
141
+ ),
142
+ dict(
143
+ task_extras={
144
+ 'accessibility_tree': [
145
+ _pack_any(_empty_forest()),
146
+ {'not_a_forest_key': 'nor_a_forest_value'},
147
+ None,
148
+ _two_window_forest(),
149
+ None,
150
+ ]
151
+ },
152
+ convert_to_np=['accessibility_tree'],
153
+ expected_forests=[_empty_forest(), _two_window_forest()],
154
+ ),
155
+ )
156
+ def test_task_extras(self, task_extras, expected_forests, convert_to_np):
157
+ for k in convert_to_np:
158
+ if task_extras[k]:
159
+ task_extras[k] = np.stack(task_extras[k], axis=0)
160
+ else:
161
+ task_extras[k] = np.array([])
162
+ forests = a11y_forests.extract_forests_from_task_extras(task_extras)
163
+ self.assertEqual(len(forests), len(expected_forests))
164
+ for idx, f in enumerate(forests):
165
+ self.assertEqual(f, expected_forests[idx])
166
+
167
+ @parameterized.parameters(
168
+ dict(task_extras={}, expected_extras={}),
169
+ dict(
170
+ task_extras={
171
+ 'no_accessibility_tree': 42,
172
+ },
173
+ expected_extras={
174
+ 'no_accessibility_tree': 42,
175
+ },
176
+ ),
177
+ dict(
178
+ task_extras={'accessibility_tree': []},
179
+ expected_extras={'accessibility_tree': []},
180
+ ),
181
+ dict(
182
+ task_extras={
183
+ 'accessibility_tree': [
184
+ _empty_forest(),
185
+ _one_empty_window_forest(),
186
+ ],
187
+ 'no_accessibility_tree': 43,
188
+ },
189
+ expected_extras={
190
+ 'accessibility_tree': [_one_empty_window_forest()],
191
+ 'no_accessibility_tree': 43,
192
+ },
193
+ ),
194
+ dict(
195
+ task_extras={
196
+ 'accessibility_tree': [
197
+ _empty_forest(),
198
+ _one_empty_window_forest(),
199
+ _two_window_forest(),
200
+ ]
201
+ },
202
+ expected_extras={'accessibility_tree': [_two_window_forest()]},
203
+ ),
204
+ dict(
205
+ task_extras={
206
+ 'accessibility_tree': [],
207
+ 'no_accessibility_tree': 44,
208
+ },
209
+ expected_extras={
210
+ 'accessibility_tree': [],
211
+ 'no_accessibility_tree': 44,
212
+ },
213
+ ),
214
+ )
215
+ def test_keep_latest_only(self, task_extras, expected_extras):
216
+ if 'accessibility_tree' in task_extras:
217
+ if task_extras['accessibility_tree']:
218
+ task_extras['accessibility_tree'] = np.stack(
219
+ task_extras['accessibility_tree'], axis=0
220
+ )
221
+ else:
222
+ task_extras['accessibility_tree'] = np.array([])
223
+
224
+ a11y_forests.keep_latest_forest_only(task_extras)
225
+ self.assertSameElements(task_extras.keys(), expected_extras.keys())
226
+ for k in task_extras.keys():
227
+ if k == 'accessibility_tree':
228
+ self.assertEqual(len(task_extras[k]), len(expected_extras[k]))
229
+ for idx, f in enumerate(task_extras[k]):
230
+ self.assertEqual(f, expected_extras[k][idx])
231
+ else:
232
+ self.assertEqual(task_extras[k], expected_extras[k])
233
+ pass
234
+
235
+
236
+ if __name__ == '__main__':
237
+ absltest.main()