tturney | 1bdf77d | 2015-12-28 17:46:13 -0800 | [diff] [blame] | 1 | #/usr/bin/env python3.4 |
| 2 | # |
| 3 | # Copyright (C) 2016 The Android Open Source Project |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 6 | # use this file except in compliance with the License. You may obtain a copy of |
| 7 | # the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations under |
| 15 | # the License. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 16 | """ |
| 17 | This test script exercises background scan test scenarios. |
| 18 | """ |
| 19 | |
| 20 | from queue import Empty |
| 21 | |
tturney | 11b65e4 | 2017-01-11 13:57:20 -0800 | [diff] [blame] | 22 | from acts.test_decorators import test_tracker_info |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 23 | from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 24 | from acts.test_utils.bt.bt_test_utils import bluetooth_off |
| 25 | from acts.test_utils.bt.bt_test_utils import bluetooth_on |
| 26 | from acts.test_utils.bt.bt_test_utils import cleanup_scanners_and_advertisers |
tturney | 69e3bb1 | 2017-10-26 14:15:05 -0700 | [diff] [blame^] | 27 | from acts.test_utils.bt.bt_test_utils import enable_bluetooth |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 28 | from acts.test_utils.bt.bt_test_utils import generate_ble_advertise_objects |
| 29 | from acts.test_utils.bt.bt_test_utils import generate_ble_scan_objects |
tturney | a4ebb03 | 2017-07-24 12:14:45 -0700 | [diff] [blame] | 30 | from acts.test_utils.bt.bt_constants import bluetooth_le_off |
| 31 | from acts.test_utils.bt.bt_constants import bluetooth_le_on |
| 32 | from acts.test_utils.bt.bt_constants import bt_adapter_states |
tturney | 69e3bb1 | 2017-10-26 14:15:05 -0700 | [diff] [blame^] | 33 | from acts.test_utils.bt.bt_constants import ble_scan_settings_modes |
tturney | a4ebb03 | 2017-07-24 12:14:45 -0700 | [diff] [blame] | 34 | from acts.test_utils.bt.bt_constants import scan_result |
| 35 | |
| 36 | import time |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 37 | |
| 38 | |
| 39 | class BleBackgroundScanTest(BluetoothBaseTest): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 40 | default_timeout = 10 |
| 41 | max_scan_instances = 28 |
| 42 | report_delay = 2000 |
| 43 | scan_callbacks = [] |
| 44 | adv_callbacks = [] |
| 45 | active_scan_callback_list = [] |
| 46 | active_adv_callback_list = [] |
| 47 | |
| 48 | def __init__(self, controllers): |
| 49 | BluetoothBaseTest.__init__(self, controllers) |
tturney | 1ce8dc6 | 2016-02-18 09:55:53 -0800 | [diff] [blame] | 50 | self.scn_ad = self.android_devices[0] |
| 51 | self.adv_ad = self.android_devices[1] |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 52 | |
| 53 | def setup_test(self): |
tturney | 69e3bb1 | 2017-10-26 14:15:05 -0700 | [diff] [blame^] | 54 | # Always start tests with Bluetooth enabled and BLE disabled. |
| 55 | enable_bluetooth(self.scn_ad.droid, self.scn_ad.ed) |
| 56 | self.scn_ad.droid.bluetoothDisableBLE() |
tturney | 653b273 | 2016-02-24 10:40:50 -0800 | [diff] [blame] | 57 | for a in self.android_devices: |
| 58 | a.ed.clear_all_events() |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 59 | return True |
| 60 | |
| 61 | def teardown_test(self): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 62 | cleanup_scanners_and_advertisers( |
tturney | c12b6ec | 2016-03-30 13:39:40 -0700 | [diff] [blame] | 63 | self.scn_ad, self.active_adv_callback_list, self.adv_ad, |
| 64 | self.active_adv_callback_list) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 65 | self.active_adv_callback_list = [] |
| 66 | self.active_scan_callback_list = [] |
| 67 | |
| 68 | def _setup_generic_advertisement(self): |
tturney | 69e3bb1 | 2017-10-26 14:15:05 -0700 | [diff] [blame^] | 69 | self.adv_ad.droid.bleSetAdvertiseDataIncludeDeviceName(True) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 70 | adv_callback, adv_data, adv_settings = generate_ble_advertise_objects( |
tturney | 1ce8dc6 | 2016-02-18 09:55:53 -0800 | [diff] [blame] | 71 | self.adv_ad.droid) |
tturney | c12b6ec | 2016-03-30 13:39:40 -0700 | [diff] [blame] | 72 | self.adv_ad.droid.bleStartBleAdvertising(adv_callback, adv_data, |
| 73 | adv_settings) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 74 | self.active_adv_callback_list.append(adv_callback) |
| 75 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 76 | @BluetoothBaseTest.bt_test_wrap |
tturney | 11b65e4 | 2017-01-11 13:57:20 -0800 | [diff] [blame] | 77 | @test_tracker_info(uuid='4d13c3a8-1805-44ef-a92a-e385540767f1') |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 78 | def test_background_scan(self): |
| 79 | """Test generic background scan. |
| 80 | |
| 81 | Tests LE background scan. The goal is to find scan results even though |
| 82 | Bluetooth is turned off. |
| 83 | |
| 84 | Steps: |
| 85 | 1. Setup an advertisement on dut1 |
| 86 | 2. Enable LE on the Bluetooth Adapter on dut0 |
| 87 | 3. Toggle BT off on dut1 |
| 88 | 4. Start a LE scan on dut0 |
| 89 | 5. Find the advertisement from dut1 |
| 90 | |
| 91 | Expected Result: |
| 92 | Find a advertisement from the scan instance. |
| 93 | |
| 94 | Returns: |
| 95 | Pass if True |
| 96 | Fail if False |
| 97 | |
| 98 | TAGS: LE, Advertising, Scanning, Background Scanning |
| 99 | Priority: 0 |
| 100 | """ |
tturney | 69e3bb1 | 2017-10-26 14:15:05 -0700 | [diff] [blame^] | 101 | self.scn_ad.droid.bluetoothEnableBLE() |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 102 | self._setup_generic_advertisement() |
tturney | 69e3bb1 | 2017-10-26 14:15:05 -0700 | [diff] [blame^] | 103 | self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[ |
| 104 | 'low_latency']) |
| 105 | filter_list, scan_settings, scan_callback = generate_ble_scan_objects( |
| 106 | self.scn_ad.droid) |
| 107 | self.scn_ad.droid.bleSetScanFilterDeviceName( |
| 108 | self.adv_ad.droid.bluetoothGetLocalName()) |
| 109 | self.scn_ad.droid.bleBuildScanFilter(filter_list) |
tturney | 1ce8dc6 | 2016-02-18 09:55:53 -0800 | [diff] [blame] | 110 | self.scn_ad.droid.bluetoothToggleState(False) |
tturney | 0ab6f01 | 2016-05-09 13:59:27 -0700 | [diff] [blame] | 111 | try: |
| 112 | self.scn_ad.ed.pop_event(bluetooth_off, self.default_timeout) |
| 113 | except Empty: |
| 114 | self.log.error("Bluetooth Off event not found. Expected {}".format( |
| 115 | bluetooth_off)) |
| 116 | return False |
tturney | c12b6ec | 2016-03-30 13:39:40 -0700 | [diff] [blame] | 117 | self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings, |
| 118 | scan_callback) |
tturney | 0ab6f01 | 2016-05-09 13:59:27 -0700 | [diff] [blame] | 119 | expected_event = scan_result.format(scan_callback) |
| 120 | try: |
| 121 | self.scn_ad.ed.pop_event(expected_event, self.default_timeout) |
| 122 | except Empty: |
Marie Janssen | 1f33303 | 2016-10-25 16:49:35 -0700 | [diff] [blame] | 123 | self.log.error("Scan Result event not found. Expected {}".format( |
| 124 | expected_event)) |
tturney | 0ab6f01 | 2016-05-09 13:59:27 -0700 | [diff] [blame] | 125 | return False |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 126 | return True |
| 127 | |
| 128 | @BluetoothBaseTest.bt_test_wrap |
tturney | 11b65e4 | 2017-01-11 13:57:20 -0800 | [diff] [blame] | 129 | @test_tracker_info(uuid='9c4577f8-5e06-4034-b977-285956734974') |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 130 | def test_background_scan_ble_disabled(self): |
| 131 | """Test background LE scanning with LE disabled. |
| 132 | |
| 133 | Tests LE background scan. The goal is to find scan results even though |
| 134 | Bluetooth is turned off. |
| 135 | |
| 136 | Steps: |
| 137 | 1. Setup an advertisement on dut1 |
| 138 | 2. Enable LE on the Bluetooth Adapter on dut0 |
| 139 | 3. Toggle BT off on dut1 |
| 140 | 4. Start a LE scan on dut0 |
| 141 | 5. Find the advertisement from dut1 |
| 142 | |
| 143 | Expected Result: |
| 144 | Find a advertisement from the scan instance. |
| 145 | |
| 146 | Returns: |
| 147 | Pass if True |
| 148 | Fail if False |
| 149 | |
| 150 | TAGS: LE, Advertising, Scanning, Background Scanning |
| 151 | Priority: 0 |
| 152 | """ |
| 153 | self._setup_generic_advertisement() |
tturney | 1ce8dc6 | 2016-02-18 09:55:53 -0800 | [diff] [blame] | 154 | self.scn_ad.droid.bluetoothEnableBLE() |
tturney | 69e3bb1 | 2017-10-26 14:15:05 -0700 | [diff] [blame^] | 155 | self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[ |
| 156 | 'low_latency']) |
| 157 | filter_list, scan_settings, scan_callback = generate_ble_scan_objects( |
| 158 | self.scn_ad.droid) |
| 159 | self.scn_ad.droid.bleSetScanFilterDeviceName( |
| 160 | self.adv_ad.droid.bluetoothGetLocalName()) |
| 161 | self.scn_ad.droid.bleBuildScanFilter(filter_list) |
tturney | 1ce8dc6 | 2016-02-18 09:55:53 -0800 | [diff] [blame] | 162 | self.scn_ad.droid.bluetoothToggleState(False) |
tturney | 0ab6f01 | 2016-05-09 13:59:27 -0700 | [diff] [blame] | 163 | try: |
| 164 | self.scn_ad.ed.pop_event(bluetooth_off, self.default_timeout) |
| 165 | except Empty: |
tturney | 69e3bb1 | 2017-10-26 14:15:05 -0700 | [diff] [blame^] | 166 | self.log.info(self.scn_ad.droid.bluetoothCheckState()) |
tturney | 0ab6f01 | 2016-05-09 13:59:27 -0700 | [diff] [blame] | 167 | self.log.error("Bluetooth Off event not found. Expected {}".format( |
| 168 | bluetooth_off)) |
| 169 | return False |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 170 | try: |
tturney | c12b6ec | 2016-03-30 13:39:40 -0700 | [diff] [blame] | 171 | self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings, |
| 172 | scan_callback) |
tturney | 0ab6f01 | 2016-05-09 13:59:27 -0700 | [diff] [blame] | 173 | expected_event = scan_result.format(scan_callback) |
| 174 | try: |
| 175 | self.scn_ad.ed.pop_event(expected_event, self.default_timeout) |
| 176 | except Empty: |
Marie Janssen | 1f33303 | 2016-10-25 16:49:35 -0700 | [diff] [blame] | 177 | self.log.error("Scan Result event not found. Expected {}". |
| 178 | format(expected_event)) |
tturney | 0ab6f01 | 2016-05-09 13:59:27 -0700 | [diff] [blame] | 179 | return False |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 180 | except Exception: |
| 181 | self.log.info( |
| 182 | "Was not able to start a background scan as expected.") |
| 183 | return True |
Marie Janssen | 1f33303 | 2016-10-25 16:49:35 -0700 | [diff] [blame] | 184 | |
| 185 | @BluetoothBaseTest.bt_test_wrap |
tturney | 11b65e4 | 2017-01-11 13:57:20 -0800 | [diff] [blame] | 186 | @test_tracker_info(uuid='0bdd1764-3dc6-4a82-b041-76e48ed0f424') |
Marie Janssen | 1f33303 | 2016-10-25 16:49:35 -0700 | [diff] [blame] | 187 | def test_airplane_mode_disables_ble(self): |
| 188 | """Try to start LE mode in Airplane Mode. |
| 189 | |
| 190 | This test will enable airplane mode, then attempt to start LE scanning |
| 191 | mode. This should result in bluetooth still being turned off, LE |
| 192 | not enabled. |
| 193 | |
| 194 | Steps: |
| 195 | 1. Start LE only mode. |
| 196 | 2. Bluetooth should be in LE ONLY mode |
| 197 | 2. Turn on airplane mode. |
| 198 | 3. Bluetooth should be OFF |
| 199 | 4. Try to start LE only mode. |
| 200 | 5. Bluetooth should stay in OFF mode (LE only start should fail) |
| 201 | 6. Turn off airplane mode. |
| 202 | 7. Bluetooth should be OFF. |
| 203 | |
| 204 | Expected Result: |
| 205 | No unexpected bluetooth state changes. |
| 206 | |
| 207 | Returns: |
| 208 | Pass if True |
| 209 | Fail if False |
| 210 | |
| 211 | TAGS: LE, Airplane |
| 212 | Priority: 1 |
| 213 | """ |
| 214 | ble_state_error_msg = "Bluetooth LE State not OK {}. Expected {} got {}" |
| 215 | # Enable BLE always available (effectively enabling BT in location) |
tturney | 69e3bb1 | 2017-10-26 14:15:05 -0700 | [diff] [blame^] | 216 | self.scn_ad.droid.bluetoothEnableBLE() |
Marie Janssen | 1f33303 | 2016-10-25 16:49:35 -0700 | [diff] [blame] | 217 | self.scn_ad.droid.bluetoothToggleState(False) |
| 218 | try: |
| 219 | self.scn_ad.ed.pop_event(bluetooth_off, self.default_timeout) |
| 220 | except Empty: |
| 221 | self.log.error("Bluetooth Off event not found. Expected {}".format( |
| 222 | bluetooth_off)) |
tturney | 69e3bb1 | 2017-10-26 14:15:05 -0700 | [diff] [blame^] | 223 | self.log.info(self.scn_ad.droid.bluetoothCheckState()) |
Marie Janssen | 1f33303 | 2016-10-25 16:49:35 -0700 | [diff] [blame] | 224 | return False |
| 225 | |
| 226 | # Sleep because LE turns off after the bluetooth off event fires |
| 227 | time.sleep(self.default_timeout) |
| 228 | state = self.scn_ad.droid.bluetoothGetLeState() |
tturney | a4ebb03 | 2017-07-24 12:14:45 -0700 | [diff] [blame] | 229 | if state != bt_adapter_states['ble_on']: |
Marie Janssen | 1f33303 | 2016-10-25 16:49:35 -0700 | [diff] [blame] | 230 | self.log.error( |
tturney | 69e3bb1 | 2017-10-26 14:15:05 -0700 | [diff] [blame^] | 231 | ble_state_error_msg.format("after BT Disable", |
tturney | a4ebb03 | 2017-07-24 12:14:45 -0700 | [diff] [blame] | 232 | bt_adapter_states['ble_on'], state)) |
Marie Janssen | 1f33303 | 2016-10-25 16:49:35 -0700 | [diff] [blame] | 233 | return False |
| 234 | |
| 235 | self.scn_ad.droid.bluetoothListenForBleStateChange() |
| 236 | self.scn_ad.droid.connectivityToggleAirplaneMode(True) |
| 237 | try: |
tturney | a4ebb03 | 2017-07-24 12:14:45 -0700 | [diff] [blame] | 238 | self.scn_ad.ed.pop_event(bluetooth_le_off, self.default_timeout) |
Marie Janssen | 1f33303 | 2016-10-25 16:49:35 -0700 | [diff] [blame] | 239 | except Empty: |
| 240 | self.log.error("Bluetooth LE Off event not found. Expected {}". |
tturney | a4ebb03 | 2017-07-24 12:14:45 -0700 | [diff] [blame] | 241 | format(bluetooth_le_off)) |
Marie Janssen | 1f33303 | 2016-10-25 16:49:35 -0700 | [diff] [blame] | 242 | return False |
| 243 | state = self.scn_ad.droid.bluetoothGetLeState() |
tturney | a4ebb03 | 2017-07-24 12:14:45 -0700 | [diff] [blame] | 244 | if state != bt_adapter_states['off']: |
Marie Janssen | 1f33303 | 2016-10-25 16:49:35 -0700 | [diff] [blame] | 245 | self.log.error( |
tturney | a4ebb03 | 2017-07-24 12:14:45 -0700 | [diff] [blame] | 246 | ble_state_error_msg.format("after Airplane Mode ON", |
| 247 | bt_adapter_states['off'], state)) |
Marie Janssen | 1f33303 | 2016-10-25 16:49:35 -0700 | [diff] [blame] | 248 | return False |
| 249 | result = self.scn_ad.droid.bluetoothEnableBLE() |
| 250 | if result: |
| 251 | self.log.error( |
| 252 | "Bluetooth Enable command succeded when it should have failed (in airplane mode)" |
| 253 | ) |
| 254 | return False |
| 255 | state = self.scn_ad.droid.bluetoothGetLeState() |
tturney | a4ebb03 | 2017-07-24 12:14:45 -0700 | [diff] [blame] | 256 | if state != bt_adapter_states['off']: |
Marie Janssen | 1f33303 | 2016-10-25 16:49:35 -0700 | [diff] [blame] | 257 | self.log.error( |
| 258 | "Bluetooth LE State not OK after attempted enable. Expected {} got {}". |
tturney | a4ebb03 | 2017-07-24 12:14:45 -0700 | [diff] [blame] | 259 | format(bt_adapter_states['off'], state)) |
Marie Janssen | 1f33303 | 2016-10-25 16:49:35 -0700 | [diff] [blame] | 260 | return False |
| 261 | self.scn_ad.droid.connectivityToggleAirplaneMode(False) |
| 262 | # Sleep to let Airplane Mode disable propogate through the system |
| 263 | time.sleep(self.default_timeout) |
| 264 | state = self.scn_ad.droid.bluetoothGetLeState() |
tturney | a4ebb03 | 2017-07-24 12:14:45 -0700 | [diff] [blame] | 265 | if state != bt_adapter_states['off']: |
Marie Janssen | 1f33303 | 2016-10-25 16:49:35 -0700 | [diff] [blame] | 266 | self.log.error( |
tturney | a4ebb03 | 2017-07-24 12:14:45 -0700 | [diff] [blame] | 267 | ble_state_error_msg.format("after Airplane Mode OFF", |
| 268 | bt_adapter_states['off'], state)) |
Marie Janssen | 1f33303 | 2016-10-25 16:49:35 -0700 | [diff] [blame] | 269 | return False |
| 270 | return True |