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"""A Batch of Bluetooth stand alone health tests"""
7
8from __future__ import absolute_import
9from __future__ import division
10from __future__ import print_function
11
12import logging
13
14from autotest_lib.server.cros.bluetooth.bluetooth_adapter_quick_tests import (
15        BluetoothAdapterQuickTests)
16from autotest_lib.server.cros.bluetooth import bluetooth_dbus_api_tests
17from autotest_lib.server.cros.bluetooth import bluetooth_default_state_test
18from autotest_lib.server.cros.bluetooth import bluetooth_valid_address_test
19from six.moves import range
20
21
22class bluetooth_AdapterSAHealth(BluetoothAdapterQuickTests,
23        bluetooth_default_state_test.bluetooth_Health_DefaultStateTest,
24        bluetooth_valid_address_test.bluetooth_Health_ValidAddressTest,
25        bluetooth_dbus_api_tests.BluetoothDBusAPITests):
26
27    """A Batch of Bluetooth stand alone health tests. This test is written as
28       a batch of tests in order to reduce test time, since auto-test ramp up
29       time is costy. The batch is using BluetoothAdapterQuickTests wrapper
30       methods to start and end a test and a batch of tests.
31
32       This class can be called to run the entire test batch or to run a
33       specific test only
34    """
35
36    test_wrapper = BluetoothAdapterQuickTests.quick_test_test_decorator
37    batch_wrapper = BluetoothAdapterQuickTests.quick_test_batch_decorator
38
39    @test_wrapper('Stand Alone noop test', supports_floss=True)
40    def sa_noop(self):
41        """A no-op test to validate Floss"""
42        logging.info("sa_noop ran")
43
44    @test_wrapper('Stand Alone basic test')
45    def sa_basic_test(self):
46        """Set of basic stand alone test"""
47
48        # The bluetoothd must be running in the beginning.
49        self.test_bluetoothd_running()
50
51        # It is possible that the adapter is not powered on yet.
52        # Power it on anyway and verify that it is actually powered on.
53        self.test_power_on_adapter()
54
55        # Verify that the adapter could be powered off and powered on,
56        # and that it is in the correct working state.
57        self.test_power_off_adapter()
58        self.test_power_on_adapter()
59        self.test_adapter_work_state()
60
61        # Verify that the bluetoothd could be simply stopped and then
62        # be started again, and that it is in the correct working state.
63        self.test_stop_bluetoothd()
64        self.test_start_bluetoothd()
65        self.test_adapter_work_state()
66
67        # Verify that the adapter could be reset off and on which includes
68        # removing all cached information.
69        self.test_reset_off_adapter()
70        self.test_reset_on_adapter()
71        self.test_adapter_work_state()
72
73        # Verify that the adapter supports basic profiles.
74        self.test_UUIDs()
75
76        # Verify that the adapter could start and stop discovery.
77        self.test_start_discovery()
78        self.test_stop_discovery()
79        self.test_start_discovery()
80
81        # Verify that the adapter could set both discoverable and
82        # non-discoverable successfully.
83        self.test_discoverable()
84        self.test_nondiscoverable()
85        self.test_discoverable()
86
87        # Verify that the adapter could set pairable and non-pairable.
88        self.test_pairable()
89        self.test_nonpairable()
90        self.test_pairable()
91
92
93    # Remove flags=['Quick Health'] when this test is migrated to stable suite.
94    @test_wrapper('Stand Alone power reset test', flags=['Quick Health'])
95    def sa_power_reset(self):
96        """Adapter power reset test
97
98        Repeatedly reset adapter power to expect that bluetoothd is running.
99        Note that the test takes about 2 minutes to complete and is suitable
100        to run in function verification test suite.
101        """
102        for _ in range(20):
103            self.test_reset_on_adapter()
104            self.test_bluetoothd_running()
105
106
107    # TODO(b/182172118) - Winky has suspend test issues
108    @test_wrapper('Adapter suspend resume test', skip_models=['winky'])
109    def sa_adapter_suspend_resume_test(self):
110        """Test dapter power states is perserved through suspend resume."""
111        def adapter_on_SR_test():
112            """Test Case: Power on - SR"""
113            self.test_power_on_adapter()
114            self.test_bluetoothd_running()
115            self.suspend_resume()
116            self.test_bluetoothd_running()
117            self.test_adapter_work_state()
118            self.test_power_on_adapter()
119
120
121        def adapter_off_SR_test():
122            """Test Case: Power off - SR"""
123            self.test_power_off_adapter()
124            self.test_bluetoothd_running()
125            self.suspend_resume()
126            self.test_power_off_adapter()
127            self.test_bluetoothd_running()
128
129        adapter_on_SR_test()
130        adapter_off_SR_test()
131
132
133    @test_wrapper('Adapter present test', supports_floss=True)
134    def sa_adapter_present_test(self):
135        """Verify that the client has a Bluetooth adapter."""
136
137        # Reset the adapter (if any) to the powered off state.
138        self.test_reset_off_adapter()
139
140        # Verify that there is an adapter. This will only return True if both
141        # the kernel and bluetooth daemon see the adapter.
142        self.test_has_adapter()
143
144    @test_wrapper('Adapter reboot test')
145    def sa_adapter_reboot_test(self):
146        """Verify that adapter power setting persist over reboot
147
148        Test whether power setting persist after a reboot and whether
149        adapter can be turned on after reboot
150        """
151
152        def test_case_adapter_on_reboot():
153            """Test Case: Power on - reboot"""
154            self.test_power_on_adapter()
155            self.test_bluetoothd_running()
156            self.reboot()
157            self.test_bluetoothd_running()
158            self.test_adapter_work_state()
159
160        def test_case_adapter_off_reboot():
161            """Test Case: Power on - reboot"""
162            self.test_power_off_adapter()
163            self.test_bluetoothd_running()
164            self.reboot()
165            self.test_has_adapter()
166            self.test_is_powered_off()
167            self.test_power_on_adapter()
168            self.test_bluetoothd_running()
169
170        NUM_ITERATIONS = 3
171        for i in range(NUM_ITERATIONS):
172            logging.debug('Starting reboot test loop number #%d', i)
173            test_case_adapter_on_reboot()
174            test_case_adapter_off_reboot()
175
176
177
178    # TODO(b/145302986): Silencing known firmware issue with AC7260 (WP2)
179    @test_wrapper('Adapter DiscoverableTimeout test',
180                  skip_chipsets=['Intel-AC7260'])
181    def sa_adapter_discoverable_timeout_test(self):
182        """Verify that DiscoverableTimout Property works."""
183        result = self.test_discoverable_timeout(timeout_values=[0, 7, 15])
184        logging.info("Result is %s", result)
185
186    @test_wrapper('Adapter PairableTimeout test')
187    def sa_adapter_pairable_timeout_test(self):
188        """Verify that PairableTimout Property works."""
189        result = self.test_pairable_timeout(timeout_values=[0, 7, 15])
190        logging.info("Result is %s", result)
191
192
193    @test_wrapper('Default state test')
194    def sa_default_state_test(self):
195        """Verify that the Bluetooth adapter has correct state."""
196        self.default_state_test()
197
198
199    @test_wrapper('Valid address test', supports_floss=True)
200    def sa_valid_address_test(self):
201        """Verify that the client Bluetooth adapter has a valid address."""
202        self.valid_address_test()
203
204
205    @test_wrapper('Valid adapter ID test')
206    def sa_valid_id_test(self):
207        """Verify that the adapter has a correctly-formatted ID"""
208        self.test_check_valid_adapter_id()
209
210
211    @test_wrapper('Valid adapter alias test')
212    def sa_valid_alias_test(self):
213        """Verify that the adapter has a correctly-formatted alias"""
214        self.test_check_valid_alias()
215
216
217    @test_wrapper('DBUS API tests')
218    def sa_dbus_api_tests(self):
219        """ Verify that the Bluetooth DBus API calls work."""
220        self.test_dbus_start_discovery_success()
221        self.test_dbus_start_discovery_fail_discovery_in_progress()
222        self.test_dbus_start_discovery_fail_power_off()
223
224        self.test_dbus_stop_discovery_success()
225        self.test_dbus_stop_discovery_fail_discovery_not_in_progress()
226        self.test_dbus_stop_discovery_fail_power_off()
227
228        self.test_dbus_get_supported_capabilities_success()
229        self.test_dbus_get_supported_capabilities_success_power_off()
230
231    @test_wrapper('EIR Max Alias Size test')
232    def sa_eir_max_name_size_test(self):
233        """ Verify that the non-default max eir name size is used """
234        EIR_80_char_name = ('1234567890123456789012345678901234567890'
235                            '1234567890123456789012345678901234567890')
236
237        self.test_set_adapter_alias(EIR_80_char_name)
238
239
240    @batch_wrapper('Stand Alone Health')
241    def sa_health_batch_run(self, num_iterations=1, test_name=None):
242        """Run the stand alone health test batch or a specific given test.
243           The wrapper of this method is implemented in batch_decorator.
244           Using the decorator a test batch method can implement the only its
245           core tests invocations and let the decorator handle the wrapper,
246           which is taking care for whether to run a specific test or the
247           batch as a whole, and running the batch in iterations
248
249           @param num_iterations: how many interations to run
250           @param test_name: specifc test to run otherwise None to run the
251                             whole batch
252        """
253        self.sa_noop()
254        self.sa_basic_test()
255        self.sa_adapter_suspend_resume_test()
256        self.sa_adapter_present_test()
257        # self.sa_adapter_reboot_test() disabled since the test is not stable
258        self.sa_adapter_discoverable_timeout_test()
259        self.sa_default_state_test()
260        self.sa_valid_address_test()
261        self.sa_dbus_api_tests()
262        self.sa_power_reset()
263
264
265    def run_once(self,
266                 host,
267                 num_iterations=1,
268                 args_dict=None,
269                 test_name=None,
270                 flag='Quick Health',
271                 floss=False):
272        """Run the batch of Bluetooth stand health tests
273
274        @param host: the DUT, usually a chromebook
275        @param num_iterations: the number of rounds to execute the test
276        """
277        # Initialize and run the test batch or the requested specific test
278        self.quick_test_init(host,
279                             use_btpeer=False,
280                             flag=flag,
281                             start_browser=False,
282                             floss=floss)
283        self.sa_health_batch_run(num_iterations, test_name)
284        self.quick_test_cleanup()
285