xref: /aosp_15_r20/tools/asuite/atest/usb_speed_detect_unittest.py (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1*c2e18aaaSAndroid Build Coastguard Worker# Copyright (C) 2024 The Android Open Source Project
2*c2e18aaaSAndroid Build Coastguard Worker#
3*c2e18aaaSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*c2e18aaaSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*c2e18aaaSAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*c2e18aaaSAndroid Build Coastguard Worker#
7*c2e18aaaSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
8*c2e18aaaSAndroid Build Coastguard Worker#
9*c2e18aaaSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*c2e18aaaSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*c2e18aaaSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*c2e18aaaSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*c2e18aaaSAndroid Build Coastguard Worker# limitations under the License.
14*c2e18aaaSAndroid Build Coastguard Worker
15*c2e18aaaSAndroid Build Coastguard Workerimport subprocess
16*c2e18aaaSAndroid Build Coastguard Workerimport unittest
17*c2e18aaaSAndroid Build Coastguard Workerfrom unittest import mock
18*c2e18aaaSAndroid Build Coastguard Worker
19*c2e18aaaSAndroid Build Coastguard Workerfrom atest import atest_utils
20*c2e18aaaSAndroid Build Coastguard Workerfrom atest import usb_speed_detect as usb
21*c2e18aaaSAndroid Build Coastguard Worker
22*c2e18aaaSAndroid Build Coastguard Worker
23*c2e18aaaSAndroid Build Coastguard Workerclass UsbIgnoredSpeedPatterns(unittest.TestCase):
24*c2e18aaaSAndroid Build Coastguard Worker
25*c2e18aaaSAndroid Build Coastguard Worker  def _usb_speed_assert_no_warning(self, negotiated_speed, max_speed):
26*c2e18aaaSAndroid Build Coastguard Worker    """Parametrized test to verify whether a usb speed warning is printed."""
27*c2e18aaaSAndroid Build Coastguard Worker    warning = usb.verify_and_print_usb_speed_warning(
28*c2e18aaaSAndroid Build Coastguard Worker        device_ids=usb.DeviceIds('', '', '', '', ''),
29*c2e18aaaSAndroid Build Coastguard Worker        negotiated_speed=negotiated_speed,
30*c2e18aaaSAndroid Build Coastguard Worker        max_speed=max_speed,
31*c2e18aaaSAndroid Build Coastguard Worker    )
32*c2e18aaaSAndroid Build Coastguard Worker
33*c2e18aaaSAndroid Build Coastguard Worker    self.assertFalse(warning)
34*c2e18aaaSAndroid Build Coastguard Worker
35*c2e18aaaSAndroid Build Coastguard Worker  def test_verify_print_speed_unknown_speed_doesnt_print(self):
36*c2e18aaaSAndroid Build Coastguard Worker    self._usb_speed_assert_no_warning(0, 0)
37*c2e18aaaSAndroid Build Coastguard Worker
38*c2e18aaaSAndroid Build Coastguard Worker  def test_verify_print_speed_low_speed_doesnt_print(self):
39*c2e18aaaSAndroid Build Coastguard Worker    self._usb_speed_assert_no_warning(480, 480)
40*c2e18aaaSAndroid Build Coastguard Worker
41*c2e18aaaSAndroid Build Coastguard Worker  def test_verify_print_speed_expected_speed_doesnt_print(self):
42*c2e18aaaSAndroid Build Coastguard Worker    self._usb_speed_assert_no_warning(5000, 5000)
43*c2e18aaaSAndroid Build Coastguard Worker
44*c2e18aaaSAndroid Build Coastguard Worker  def test_verify_print_speed_high_speed_doesnt_print(self):
45*c2e18aaaSAndroid Build Coastguard Worker    self._usb_speed_assert_no_warning(5000, 10000)
46*c2e18aaaSAndroid Build Coastguard Worker
47*c2e18aaaSAndroid Build Coastguard Worker
48*c2e18aaaSAndroid Build Coastguard Workerclass UsbSpeedDetectTest(unittest.TestCase):
49*c2e18aaaSAndroid Build Coastguard Worker
50*c2e18aaaSAndroid Build Coastguard Worker  def test_verify_print_speed_slow_speed_prints_warning(self):
51*c2e18aaaSAndroid Build Coastguard Worker    warning = usb.verify_and_print_usb_speed_warning(
52*c2e18aaaSAndroid Build Coastguard Worker        device_ids=usb.DeviceIds('', '', '', '', ''),
53*c2e18aaaSAndroid Build Coastguard Worker        negotiated_speed=480,
54*c2e18aaaSAndroid Build Coastguard Worker        max_speed=10000,
55*c2e18aaaSAndroid Build Coastguard Worker    )
56*c2e18aaaSAndroid Build Coastguard Worker
57*c2e18aaaSAndroid Build Coastguard Worker    self.assertTrue(warning)
58*c2e18aaaSAndroid Build Coastguard Worker
59*c2e18aaaSAndroid Build Coastguard Worker
60*c2e18aaaSAndroid Build Coastguard Workerclass UdcDriverPatterns(unittest.TestCase):
61*c2e18aaaSAndroid Build Coastguard Worker
62*c2e18aaaSAndroid Build Coastguard Worker  def _udc_driver_response(
63*c2e18aaaSAndroid Build Coastguard Worker      self, attr_name: usb.UsbAttributeName, expected_response: int
64*c2e18aaaSAndroid Build Coastguard Worker  ):
65*c2e18aaaSAndroid Build Coastguard Worker    """Parametrized test for handling the responses from the usb driver."""
66*c2e18aaaSAndroid Build Coastguard Worker
67*c2e18aaaSAndroid Build Coastguard Worker    speed = usb.get_udc_driver_usb_device_attribute_speed_value('', attr_name)
68*c2e18aaaSAndroid Build Coastguard Worker
69*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(speed, expected_response)
70*c2e18aaaSAndroid Build Coastguard Worker
71*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch('subprocess.check_output', return_value='not found')
72*c2e18aaaSAndroid Build Coastguard Worker  def test_udc_driver_unexpected_subprocess_response_returns_0(
73*c2e18aaaSAndroid Build Coastguard Worker      self, mock_output
74*c2e18aaaSAndroid Build Coastguard Worker  ):
75*c2e18aaaSAndroid Build Coastguard Worker    self._udc_driver_response(usb.UsbAttributeName.MAXIMUM_SPEED, 0)
76*c2e18aaaSAndroid Build Coastguard Worker
77*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch('subprocess.check_output', return_value='UNKNOWN')
78*c2e18aaaSAndroid Build Coastguard Worker  def test_udc_driver_unknown_speed_returns_0(self, mock_output):
79*c2e18aaaSAndroid Build Coastguard Worker    self._udc_driver_response(usb.UsbAttributeName.MAXIMUM_SPEED, 0)
80*c2e18aaaSAndroid Build Coastguard Worker
81*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch('subprocess.check_output', return_value='wireless')
82*c2e18aaaSAndroid Build Coastguard Worker  def test_udc_driver_irrelevant_speed_returns_0(self, mock_output):
83*c2e18aaaSAndroid Build Coastguard Worker    self._udc_driver_response(usb.UsbAttributeName.NEGOTIATED_SPEED, 0)
84*c2e18aaaSAndroid Build Coastguard Worker
85*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch('subprocess.check_output', return_value='high-speed')
86*c2e18aaaSAndroid Build Coastguard Worker  def test_udc_driver_high_speed_returns_numeric_speed(self, mock_output):
87*c2e18aaaSAndroid Build Coastguard Worker    self._udc_driver_response(usb.UsbAttributeName.MAXIMUM_SPEED, 480)
88*c2e18aaaSAndroid Build Coastguard Worker
89*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch('subprocess.check_output', return_value='high-speed\n')
90*c2e18aaaSAndroid Build Coastguard Worker  def test_udc_driver_high_speed_output_has_newline_returns_numeric_speed(
91*c2e18aaaSAndroid Build Coastguard Worker      self, mock_output
92*c2e18aaaSAndroid Build Coastguard Worker  ):
93*c2e18aaaSAndroid Build Coastguard Worker    self._udc_driver_response(usb.UsbAttributeName.MAXIMUM_SPEED, 480)
94*c2e18aaaSAndroid Build Coastguard Worker
95*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch('subprocess.check_output', return_value='super-speed')
96*c2e18aaaSAndroid Build Coastguard Worker  def test_udc_driver_super_speed_returns_numeric_speed(self, mock_output):
97*c2e18aaaSAndroid Build Coastguard Worker    self._udc_driver_response(usb.UsbAttributeName.MAXIMUM_SPEED, 5000)
98*c2e18aaaSAndroid Build Coastguard Worker
99*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch('subprocess.check_output', return_value='super-speed-plus')
100*c2e18aaaSAndroid Build Coastguard Worker  def test_udc_driver_super_speed_plus_returns_numeric_speed(self, mock_output):
101*c2e18aaaSAndroid Build Coastguard Worker    self._udc_driver_response(usb.UsbAttributeName.MAXIMUM_SPEED, 10000)
102*c2e18aaaSAndroid Build Coastguard Worker
103*c2e18aaaSAndroid Build Coastguard Worker
104*c2e18aaaSAndroid Build Coastguard Workerclass DeviceIdentifierPatterns(unittest.TestCase):
105*c2e18aaaSAndroid Build Coastguard Worker
106*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(atest_utils, 'has_command', return_value=True)
107*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(subprocess, 'check_output')
108*c2e18aaaSAndroid Build Coastguard Worker  def test_get_adb_device_identifiers_port_fwd_device_returns_address(
109*c2e18aaaSAndroid Build Coastguard Worker      self, mock_output, mock_utils
110*c2e18aaaSAndroid Build Coastguard Worker  ):
111*c2e18aaaSAndroid Build Coastguard Worker    def check_output_side_effect_port_fwd_device(*args, **kwargs):
112*c2e18aaaSAndroid Build Coastguard Worker      for arg in args:
113*c2e18aaaSAndroid Build Coastguard Worker        if 'ro.serialno' in arg:
114*c2e18aaaSAndroid Build Coastguard Worker          return 'SERIAL'
115*c2e18aaaSAndroid Build Coastguard Worker        if all(cmd_arg in ['adb', 'devices'] for cmd_arg in arg):
116*c2e18aaaSAndroid Build Coastguard Worker          return 'List of devices\nlocalhost:27030     device'
117*c2e18aaaSAndroid Build Coastguard Worker        if any(
118*c2e18aaaSAndroid Build Coastguard Worker            cmd_arg
119*c2e18aaaSAndroid Build Coastguard Worker            in {
120*c2e18aaaSAndroid Build Coastguard Worker                'ro.product.manufacturer',
121*c2e18aaaSAndroid Build Coastguard Worker                'ro.product.model',
122*c2e18aaaSAndroid Build Coastguard Worker                'ro.product.name',
123*c2e18aaaSAndroid Build Coastguard Worker            }
124*c2e18aaaSAndroid Build Coastguard Worker            for cmd_arg in arg
125*c2e18aaaSAndroid Build Coastguard Worker        ):
126*c2e18aaaSAndroid Build Coastguard Worker          return ''
127*c2e18aaaSAndroid Build Coastguard Worker
128*c2e18aaaSAndroid Build Coastguard Worker    mock_output.side_effect = check_output_side_effect_port_fwd_device
129*c2e18aaaSAndroid Build Coastguard Worker
130*c2e18aaaSAndroid Build Coastguard Worker    device_ids = usb.get_adb_device_identifiers()
131*c2e18aaaSAndroid Build Coastguard Worker
132*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(device_ids.address, 'localhost:27030')
133*c2e18aaaSAndroid Build Coastguard Worker
134*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(atest_utils, 'has_command', return_value=True)
135*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(subprocess, 'check_output')
136*c2e18aaaSAndroid Build Coastguard Worker  def test_get_adb_device_identifiers_tcp_device_returns_address(
137*c2e18aaaSAndroid Build Coastguard Worker      self, mock_output, mock_utils
138*c2e18aaaSAndroid Build Coastguard Worker  ):
139*c2e18aaaSAndroid Build Coastguard Worker    def check_output_side_effect_tcp_device(*args, **kwargs):
140*c2e18aaaSAndroid Build Coastguard Worker      for arg in args:
141*c2e18aaaSAndroid Build Coastguard Worker        if 'ro.serialno' in arg:
142*c2e18aaaSAndroid Build Coastguard Worker          return 'SERIAL'
143*c2e18aaaSAndroid Build Coastguard Worker        if all(cmd_arg in ['adb', 'devices'] for cmd_arg in arg):
144*c2e18aaaSAndroid Build Coastguard Worker          return (
145*c2e18aaaSAndroid Build Coastguard Worker              '* daemon not running; starting now at tcp:1111\n * daemon '
146*c2e18aaaSAndroid Build Coastguard Worker              'started successfully\n List of devices\n33a832a820  device'
147*c2e18aaaSAndroid Build Coastguard Worker          )
148*c2e18aaaSAndroid Build Coastguard Worker        if any(
149*c2e18aaaSAndroid Build Coastguard Worker            # If check_output is called with any of ('model', 'name',
150*c2e18aaaSAndroid Build Coastguard Worker            # 'manufacturer', return an empty placeholder value.
151*c2e18aaaSAndroid Build Coastguard Worker            cmd_arg
152*c2e18aaaSAndroid Build Coastguard Worker            in {
153*c2e18aaaSAndroid Build Coastguard Worker                'ro.product.manufacturer',
154*c2e18aaaSAndroid Build Coastguard Worker                'ro.product.model',
155*c2e18aaaSAndroid Build Coastguard Worker                'ro.product.name',
156*c2e18aaaSAndroid Build Coastguard Worker            }
157*c2e18aaaSAndroid Build Coastguard Worker            for cmd_arg in arg
158*c2e18aaaSAndroid Build Coastguard Worker        ):
159*c2e18aaaSAndroid Build Coastguard Worker          return ''
160*c2e18aaaSAndroid Build Coastguard Worker
161*c2e18aaaSAndroid Build Coastguard Worker    mock_output.side_effect = check_output_side_effect_tcp_device
162*c2e18aaaSAndroid Build Coastguard Worker
163*c2e18aaaSAndroid Build Coastguard Worker    device_ids = usb.get_adb_device_identifiers()
164*c2e18aaaSAndroid Build Coastguard Worker
165*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(device_ids.address, '33a832a820')
166*c2e18aaaSAndroid Build Coastguard Worker
167*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(atest_utils, 'has_command', return_value=True)
168*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(subprocess, 'check_output')
169*c2e18aaaSAndroid Build Coastguard Worker  def test_get_adb_device_identifiers_multiple_devices_returns_none(
170*c2e18aaaSAndroid Build Coastguard Worker      self, mock_output, mock_utils
171*c2e18aaaSAndroid Build Coastguard Worker  ):
172*c2e18aaaSAndroid Build Coastguard Worker    def check_output_side_effect_multiple_devices(*args, **kwargs):
173*c2e18aaaSAndroid Build Coastguard Worker      for arg in args:
174*c2e18aaaSAndroid Build Coastguard Worker        # When multiple devices are connected, ADB will display an error "adb:
175*c2e18aaaSAndroid Build Coastguard Worker        # more than one device/emulator" and no serial will be returned.
176*c2e18aaaSAndroid Build Coastguard Worker        if 'ro.serialno' in arg:
177*c2e18aaaSAndroid Build Coastguard Worker          return None
178*c2e18aaaSAndroid Build Coastguard Worker
179*c2e18aaaSAndroid Build Coastguard Worker    mock_output.side_effect = check_output_side_effect_multiple_devices
180*c2e18aaaSAndroid Build Coastguard Worker
181*c2e18aaaSAndroid Build Coastguard Worker    device_ids = usb.get_adb_device_identifiers()
182*c2e18aaaSAndroid Build Coastguard Worker
183*c2e18aaaSAndroid Build Coastguard Worker    self.assertIsNone(device_ids)
184