android-env 1.2.2__tar.gz → 1.2.3__tar.gz
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.
- android_env-1.2.3/MANIFEST.in +1 -0
- {android-env-1.2.2 → android_env-1.2.3}/PKG-INFO +16 -4
- {android-env-1.2.2 → android_env-1.2.3}/android_env/__init__.py +1 -1
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/__init__.py +1 -1
- {android-env-1.2.2/android_env/components/simulators/emulator → android_env-1.2.3/android_env/components/a11y}/__init__.py +1 -1
- android_env-1.2.3/android_env/components/a11y/a11y_events.py +118 -0
- android_env-1.2.3/android_env/components/a11y/a11y_events_test.py +173 -0
- android_env-1.2.3/android_env/components/a11y/a11y_forests.py +128 -0
- android_env-1.2.3/android_env/components/a11y/a11y_forests_test.py +237 -0
- android_env-1.2.3/android_env/components/a11y/a11y_servicer.py +199 -0
- android_env-1.2.3/android_env/components/a11y/a11y_servicer_test.py +224 -0
- android_env-1.2.3/android_env/components/action_fns.py +132 -0
- android_env-1.2.3/android_env/components/action_fns_test.py +227 -0
- android_env-1.2.3/android_env/components/action_type.py +52 -0
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/adb_call_parser.py +233 -185
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/adb_call_parser_test.py +165 -163
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/adb_controller.py +19 -28
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/adb_controller_test.py +100 -9
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/adb_log_stream.py +3 -3
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/adb_log_stream_test.py +1 -1
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/app_screen_checker.py +15 -13
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/app_screen_checker_test.py +1 -1
- android_env-1.2.3/android_env/components/config_classes.py +203 -0
- android_env-1.2.3/android_env/components/coordinator.py +282 -0
- android_env-1.2.3/android_env/components/coordinator_test.py +283 -0
- android_env-1.2.3/android_env/components/device_settings.py +174 -0
- android_env-1.2.3/android_env/components/device_settings_test.py +228 -0
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/dumpsys_thread.py +3 -4
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/dumpsys_thread_test.py +1 -1
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/errors.py +2 -5
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/errors_test.py +1 -1
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/log_stream.py +2 -2
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/log_stream_test.py +1 -1
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/logcat_thread.py +9 -8
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/logcat_thread_test.py +2 -3
- android-env-1.2.2/android_env/components/utils.py → android_env-1.2.3/android_env/components/pixel_fns.py +19 -20
- android-env-1.2.2/android_env/components/utils_test.py → android_env-1.2.3/android_env/components/pixel_fns_test.py +20 -15
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/setup_step_interpreter.py +45 -37
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/setup_step_interpreter_test.py +1 -1
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/simulators/__init__.py +1 -1
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/simulators/base_simulator.py +79 -23
- android_env-1.2.3/android_env/components/simulators/base_simulator_test.py +181 -0
- android_env-1.2.3/android_env/components/simulators/emulator/__init__.py +15 -0
- android_env-1.2.3/android_env/components/simulators/emulator/emulator_launcher.py +169 -0
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/simulators/emulator/emulator_launcher_test.py +120 -43
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/simulators/emulator/emulator_simulator.py +111 -98
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/simulators/emulator/emulator_simulator_test.py +174 -138
- android_env-1.2.3/android_env/components/simulators/fake/__init__.py +15 -0
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/simulators/fake/fake_simulator.py +9 -17
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/simulators/fake/fake_simulator_test.py +23 -8
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/specs.py +1 -1
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/specs_test.py +1 -1
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/task_manager.py +26 -31
- {android-env-1.2.2 → android_env-1.2.3}/android_env/components/task_manager_test.py +1 -18
- {android-env-1.2.2 → android_env-1.2.3}/android_env/env_interface.py +1 -17
- {android-env-1.2.2 → android_env-1.2.3}/android_env/environment.py +27 -17
- {android-env-1.2.2 → android_env-1.2.3}/android_env/environment_test.py +51 -25
- android_env-1.2.3/android_env/loader.py +89 -0
- android_env-1.2.3/android_env/loader_test.py +182 -0
- android_env-1.2.3/android_env/proto/__init__.py +15 -0
- android_env-1.2.3/android_env/proto/a11y/__init__.py +15 -0
- android_env-1.2.3/android_env/proto/a11y/a11y.proto +75 -0
- android_env-1.2.3/android_env/proto/a11y/android_accessibility_action.proto +32 -0
- android_env-1.2.3/android_env/proto/a11y/android_accessibility_forest.proto +29 -0
- android_env-1.2.3/android_env/proto/a11y/android_accessibility_node_info.proto +122 -0
- android_env-1.2.3/android_env/proto/a11y/android_accessibility_node_info_clickable_span.proto +49 -0
- android_env-1.2.3/android_env/proto/a11y/android_accessibility_tree.proto +29 -0
- android_env-1.2.3/android_env/proto/a11y/android_accessibility_window_info.proto +84 -0
- android_env-1.2.3/android_env/proto/a11y/rect.proto +30 -0
- {android-env-1.2.2 → android_env-1.2.3}/android_env/proto/adb.proto +13 -1
- {android-env-1.2.2 → android_env-1.2.3}/android_env/proto/emulator_controller.proto +1 -1
- {android-env-1.2.2 → android_env-1.2.3}/android_env/proto/snapshot.proto +1 -1
- {android-env-1.2.2 → android_env-1.2.3}/android_env/proto/snapshot_service.proto +1 -1
- {android-env-1.2.2 → android_env-1.2.3}/android_env/proto/state.proto +1 -1
- {android-env-1.2.2 → android_env-1.2.3}/android_env/proto/task.proto +4 -1
- android_env-1.2.3/android_env/wrappers/__init__.py +15 -0
- android_env-1.2.3/android_env/wrappers/a11y_grpc_wrapper.py +500 -0
- android_env-1.2.3/android_env/wrappers/a11y_grpc_wrapper_test.py +849 -0
- {android-env-1.2.2 → android_env-1.2.3}/android_env/wrappers/base_wrapper.py +1 -5
- {android-env-1.2.2 → android_env-1.2.3}/android_env/wrappers/base_wrapper_test.py +1 -7
- {android-env-1.2.2 → android_env-1.2.3}/android_env/wrappers/discrete_action_wrapper.py +15 -14
- {android-env-1.2.2 → android_env-1.2.3}/android_env/wrappers/discrete_action_wrapper_test.py +1 -1
- {android-env-1.2.2 → android_env-1.2.3}/android_env/wrappers/flat_interface_wrapper.py +5 -5
- {android-env-1.2.2 → android_env-1.2.3}/android_env/wrappers/flat_interface_wrapper_test.py +1 -1
- {android-env-1.2.2 → android_env-1.2.3}/android_env/wrappers/float_pixels_wrapper.py +5 -4
- {android-env-1.2.2 → android_env-1.2.3}/android_env/wrappers/float_pixels_wrapper_test.py +1 -1
- {android-env-1.2.2 → android_env-1.2.3}/android_env/wrappers/gym_wrapper.py +1 -1
- {android-env-1.2.2 → android_env-1.2.3}/android_env/wrappers/gym_wrapper_test.py +1 -1
- {android-env-1.2.2 → android_env-1.2.3}/android_env/wrappers/image_rescale_wrapper.py +13 -10
- {android-env-1.2.2 → android_env-1.2.3}/android_env/wrappers/image_rescale_wrapper_test.py +1 -1
- {android-env-1.2.2 → android_env-1.2.3}/android_env/wrappers/last_action_wrapper.py +5 -4
- {android-env-1.2.2 → android_env-1.2.3}/android_env/wrappers/last_action_wrapper_test.py +1 -1
- {android-env-1.2.2 → android_env-1.2.3}/android_env/wrappers/rate_limit_wrapper.py +1 -1
- {android-env-1.2.2 → android_env-1.2.3}/android_env/wrappers/rate_limit_wrapper_test.py +1 -1
- {android-env-1.2.2 → android_env-1.2.3}/android_env/wrappers/tap_action_wrapper.py +12 -12
- {android-env-1.2.2 → android_env-1.2.3}/android_env/wrappers/tap_action_wrapper_test.py +49 -14
- {android-env-1.2.2 → android_env-1.2.3}/android_env.egg-info/PKG-INFO +16 -4
- {android-env-1.2.2 → android_env-1.2.3}/android_env.egg-info/SOURCES.txt +26 -2
- {android-env-1.2.2 → android_env-1.2.3}/android_env.egg-info/requires.txt +0 -2
- {android-env-1.2.2 → android_env-1.2.3}/pyproject.toml +1 -3
- {android-env-1.2.2 → android_env-1.2.3}/setup.py +17 -15
- android-env-1.2.2/android_env/components/action_type.py +0 -29
- android-env-1.2.2/android_env/components/coordinator.py +0 -567
- android-env-1.2.2/android_env/components/coordinator_test.py +0 -540
- android-env-1.2.2/android_env/components/simulators/base_simulator_test.py +0 -59
- android-env-1.2.2/android_env/components/simulators/emulator/emulator_launcher.py +0 -188
- android-env-1.2.2/android_env/components/simulators/fake/__init__.py +0 -15
- android-env-1.2.2/android_env/loader.py +0 -75
- android-env-1.2.2/android_env/loader_test.py +0 -102
- android-env-1.2.2/android_env/proto/__init__.py +0 -15
- android-env-1.2.2/android_env/wrappers/__init__.py +0 -15
- {android-env-1.2.2 → android_env-1.2.3}/LICENSE +0 -0
- {android-env-1.2.2 → android_env-1.2.3}/README.md +0 -0
- {android-env-1.2.2 → android_env-1.2.3}/android_env.egg-info/dependency_links.txt +0 -0
- {android-env-1.2.2 → android_env-1.2.3}/android_env.egg-info/top_level.txt +0 -0
- {android-env-1.2.2 → android_env-1.2.3}/setup.cfg +0 -0
@@ -0,0 +1 @@
|
|
1
|
+
recursive-include android_env/proto *.proto
|
@@ -1,8 +1,7 @@
|
|
1
|
-
Metadata-Version: 2.
|
1
|
+
Metadata-Version: 2.4
|
2
2
|
Name: android-env
|
3
|
-
Version: 1.2.
|
3
|
+
Version: 1.2.3
|
4
4
|
Summary: AndroidEnv environment and library for training agents.
|
5
|
-
Home-page: https://github.com/deepmind/android_env
|
6
5
|
Author: DeepMind
|
7
6
|
License:
|
8
7
|
Apache License
|
@@ -212,9 +211,22 @@ Project-URL: arxiv, https://arxiv.org/abs/2105.13231
|
|
212
211
|
Keywords: Android,OS,reinforcement-learning
|
213
212
|
Requires-Python: >=3.10
|
214
213
|
Description-Content-Type: text/plain
|
214
|
+
License-File: LICENSE
|
215
|
+
Requires-Dist: absl-py>=0.1.0
|
216
|
+
Requires-Dist: dm_env
|
217
|
+
Requires-Dist: grpcio
|
218
|
+
Requires-Dist: numpy>=1.21
|
219
|
+
Requires-Dist: portpicker>=1.2.0
|
220
|
+
Requires-Dist: protobuf>=2.6
|
221
|
+
Requires-Dist: pygame
|
215
222
|
Provides-Extra: testing
|
223
|
+
Requires-Dist: gym; extra == "testing"
|
224
|
+
Requires-Dist: pillow; extra == "testing"
|
225
|
+
Requires-Dist: pytype; extra == "testing"
|
216
226
|
Provides-Extra: acme
|
227
|
+
Requires-Dist: dm-acme; extra == "acme"
|
217
228
|
Provides-Extra: gym
|
218
|
-
|
229
|
+
Requires-Dist: gym; extra == "gym"
|
230
|
+
Dynamic: license-file
|
219
231
|
|
220
232
|
Read the README at https://github.com/deepmind/android_env for more information.
|
@@ -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:]
|