Skip to content

Commit

Permalink
fix an issue for getting orientation from "dumpsys input" caused by p…
Browse files Browse the repository at this point in the history
…ossible attribute name change.

PiperOrigin-RevId: 622962792
  • Loading branch information
DeepMind authored and copybara-github committed Apr 8, 2024
1 parent 57c57ce commit df730c3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
15 changes: 11 additions & 4 deletions android_env/components/adb_call_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,18 +500,25 @@ def _get_orientation(
if physical_width:
skip_next = int(physical_width.group(1)) < 0

surface_orientation = re.match(r'\s+SurfaceOrientation:\s+(\d)', line)
surface_orientation = re.match(
r'\s+(SurfaceOrientation|InputDeviceOrientation):\s+(\d)', line
)

if surface_orientation is not None:
if skip_next:
continue
orientation = surface_orientation.group(1)
if surface_orientation.re.groups < 2:
continue
orientation = surface_orientation.group(2)
logging.info('Done getting orientation: %r', orientation)
response.get_orientation.orientation = int(orientation)
return response

response.status = adb_pb2.AdbResponse.Status.INTERNAL_ERROR
response.error_message = ('Could not find SurfaceOrientation in dumpsys '
'output')
response.error_message = (
'Could not find SurfaceOrientation/InputDeviceOrientation in dumpsys '
'output'
)
return response

def _push(
Expand Down
22 changes: 14 additions & 8 deletions android_env/components/adb_call_parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,18 +440,24 @@ def test_get_orientation_invalid_device_no_surface_orientation(self):
None)

@parameterized.named_parameters(
('rotation_0', b'0', 0),
('rotation_90', b'1', 1),
('rotation_180', b'2', 2),
('rotation_270', b'3', 3),
('rotation_0', b""" SurfaceOrientation: 0""", 0),
('rotation_90', b""" SurfaceOrientation: 1""", 1),
('rotation_180', b""" SurfaceOrientation: 2""", 2),
('rotation_270', b""" SurfaceOrientation: 3""", 3),
('rotation_0_new', b""" InputDeviceOrientation: 0""", 0),
('rotation_90_new', b""" InputDeviceOrientation: 1""", 1),
('rotation_180_new', b""" InputDeviceOrientation: 2""", 2),
('rotation_270_new', b""" InputDeviceOrientation: 3""", 3),
)
def test_get_orientation_success(self, orientation, expected_orientation):
def test_get_orientation_success(
self, orientation: bytes, expected_orientation: int
):
adb = mock.create_autospec(adb_controller.AdbController)
adb.execute_command.return_value = b"""
SomeRandomKey: 12345
SurfaceOrientation: """ + orientation + b"""
adb.execute_command.return_value = (
b"""SomeRandomKey: 12345\n""" + orientation + b"""
MoreRandomStuff: awesome_value
"""
)

parser = adb_call_parser.AdbCallParser(
adb, tmp_dir=absltest.get_default_test_tmpdir())
Expand Down

0 comments on commit df730c3

Please sign in to comment.