xref: /aosp_15_r20/external/autotest/server/cros/bluetooth/bluetooth_adapter_hidreports_tests.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Lint as: python2, python3
2# Copyright 2019 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Server side bluetooth tests about sending bluetooth HID reports."""
7
8from __future__ import absolute_import
9
10import logging
11import time
12
13import common
14from autotest_lib.server.cros.bluetooth import bluetooth_adapter_tests
15
16
17class BluetoothAdapterHIDReportTests(
18        bluetooth_adapter_tests.BluetoothAdapterTests):
19    """Server side bluetooth tests about sending bluetooth HID reports.
20
21    This test tries to send HID reports to a DUT and verifies if the DUT
22    could receive the reports correctly. For the time being, only bluetooth
23    mouse events are tested. Bluetooth keyboard events will be supported
24    later.
25    """
26
27    HID_TEST_SLEEP_SECS = 5
28
29    def run_mouse_tests(self, device):
30        """Run all bluetooth mouse reports tests.
31
32        @param device: the bluetooth HID device.
33
34        """
35        self.test_mouse_left_click(device)
36        self.test_mouse_right_click(device)
37        self.test_mouse_move_in_x(device, 80)
38        self.test_mouse_move_in_y(device, -50)
39        self.test_mouse_move_in_xy(device, -60, 100)
40        self.test_mouse_scroll_down(device, 70)
41        self.test_mouse_scroll_up(device, 40)
42        self.test_mouse_click_and_drag(device, 90, 30)
43
44
45    def run_keyboard_tests(self, device):
46        """Run all bluetooth keyboard reports tests.
47
48        @param device: the bluetooth HID device.
49
50        """
51
52        self.test_keyboard_input_from_trace(device, "simple_text")
53
54
55    def run_battery_reporting_tests(self, device):
56        """Run battery reporting tests.
57
58        @param device: the Bluetooth device.
59
60        """
61
62        self.test_battery_reporting(device)
63
64    def run_hid_reports_test(self,
65                             device,
66                             check_connected_method=lambda device: True,
67                             suspend_resume=False,
68                             reboot=False,
69                             restart=False):
70        """Running Bluetooth HID reports tests."""
71        logging.info("run hid reports test")
72        # Reset the adapter and set it pairable.
73        if not self.test_reset_on_adapter():
74            return
75        if not self.test_pairable():
76            return
77
78        def run_hid_test():
79            """Checks if the device is connected and can be used."""
80            time.sleep(self.HID_TEST_SLEEP_SECS)
81            if not self.test_device_name(device.address, device.name):
82                return False
83
84            time.sleep(self.HID_TEST_SLEEP_SECS)
85            if not check_connected_method(device):
86                return False
87            return True
88
89        dev_paired = False
90        dev_connected = False
91        try:
92            # Let the adapter pair, and connect to the target device.
93            self.test_discover_device(device.address)
94            dev_paired = self.test_pairing(device.address,
95                                           device.pin,
96                                           trusted=True)
97            if not dev_paired:
98                return
99            dev_connected = self.test_connection_by_adapter(device.address)
100            if not dev_connected:
101                return
102
103            # Run hid test to make sure profile is connected
104            if not run_hid_test():
105                return
106
107            if suspend_resume:
108                self.suspend_resume()
109
110                time.sleep(self.HID_TEST_SLEEP_SECS)
111                if not self.test_device_is_paired(device.address):
112                    return
113
114                # Check if peripheral is connected after suspend resume, reconnect
115                # and try again if it isn't.
116                if not self.ignore_failure(check_connected_method, device):
117                    logging.info("device not connected after suspend_resume")
118                    self.test_connection_by_device(device)
119                run_hid_test()
120
121            if reboot:
122                # If we expect the DUT to automatically reconnect to the peer on
123                # boot, we reset the peer into a connectable state
124                if self.platform_will_reconnect_on_boot():
125                    logging.info(
126                            "Restarting peer to accept DUT connection on boot")
127                    device_type = self.get_peer_device_type(device)
128                    self.reset_emulated_device(device, device_type)
129
130                self.reboot()
131
132                time.sleep(self.HID_TEST_SLEEP_SECS)
133                # TODO(b/173146480) - Power on the adapter for now until this bug
134                # is resolved.
135                if not self.bluetooth_facade.is_powered_on():
136                    self.test_power_on_adapter()
137
138                if not self.test_device_is_paired(device.address):
139                    return
140
141                time.sleep(self.HID_TEST_SLEEP_SECS)
142                if not self.platform_will_reconnect_on_boot():
143                    self.test_connection_by_device(device)
144
145                else:
146                    self.test_device_is_connected(device.address)
147                run_hid_test()
148
149            if restart:
150                self.test_stop_bluetoothd()
151                self.test_start_bluetoothd()
152
153                if not self.ignore_failure(self.test_device_is_connected,
154                                           device.address):
155                    self.test_connection_by_device(device)
156                run_hid_test()
157
158        finally:
159            # Cleans up the test
160            if dev_connected:
161                self.test_disconnection_by_adapter(device.address)
162            if dev_paired:
163                self.test_remove_pairing(device.address)
164