blob: 3cb2842b68f7fa14a33df318650d2252462f788f [file] [log] [blame]
tturney1bdf77d2015-12-28 17:46:13 -08001#/usr/bin/env python3.4
2#
3# Copyright (C) 2016 The Android Open Source Project
Ang Li73697b32015-12-03 00:41:53 +00004#
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 Li73697b32015-12-03 00:41:53 +000016"""
17This test script exercises background scan test scenarios.
18"""
19
20from queue import Empty
21
tturney11b65e42017-01-11 13:57:20 -080022from acts.test_decorators import test_tracker_info
Ang Li73697b32015-12-03 00:41:53 +000023from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
Ang Li73697b32015-12-03 00:41:53 +000024from acts.test_utils.bt.bt_test_utils import bluetooth_off
25from acts.test_utils.bt.bt_test_utils import bluetooth_on
26from acts.test_utils.bt.bt_test_utils import cleanup_scanners_and_advertisers
tturney69e3bb12017-10-26 14:15:05 -070027from acts.test_utils.bt.bt_test_utils import enable_bluetooth
Ang Li73697b32015-12-03 00:41:53 +000028from acts.test_utils.bt.bt_test_utils import generate_ble_advertise_objects
29from acts.test_utils.bt.bt_test_utils import generate_ble_scan_objects
tturneya4ebb032017-07-24 12:14:45 -070030from acts.test_utils.bt.bt_constants import bluetooth_le_off
31from acts.test_utils.bt.bt_constants import bluetooth_le_on
32from acts.test_utils.bt.bt_constants import bt_adapter_states
tturney69e3bb12017-10-26 14:15:05 -070033from acts.test_utils.bt.bt_constants import ble_scan_settings_modes
tturneya4ebb032017-07-24 12:14:45 -070034from acts.test_utils.bt.bt_constants import scan_result
35
36import time
Ang Li73697b32015-12-03 00:41:53 +000037
38
39class BleBackgroundScanTest(BluetoothBaseTest):
Ang Li73697b32015-12-03 00:41:53 +000040 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)
tturney1ce8dc62016-02-18 09:55:53 -080050 self.scn_ad = self.android_devices[0]
51 self.adv_ad = self.android_devices[1]
Ang Li73697b32015-12-03 00:41:53 +000052
53 def setup_test(self):
tturney69e3bb12017-10-26 14:15:05 -070054 # 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()
tturney653b2732016-02-24 10:40:50 -080057 for a in self.android_devices:
58 a.ed.clear_all_events()
Ang Li73697b32015-12-03 00:41:53 +000059 return True
60
61 def teardown_test(self):
Ang Li73697b32015-12-03 00:41:53 +000062 cleanup_scanners_and_advertisers(
tturneyc12b6ec2016-03-30 13:39:40 -070063 self.scn_ad, self.active_adv_callback_list, self.adv_ad,
64 self.active_adv_callback_list)
Ang Li73697b32015-12-03 00:41:53 +000065 self.active_adv_callback_list = []
66 self.active_scan_callback_list = []
67
68 def _setup_generic_advertisement(self):
tturney69e3bb12017-10-26 14:15:05 -070069 self.adv_ad.droid.bleSetAdvertiseDataIncludeDeviceName(True)
Ang Li73697b32015-12-03 00:41:53 +000070 adv_callback, adv_data, adv_settings = generate_ble_advertise_objects(
tturney1ce8dc62016-02-18 09:55:53 -080071 self.adv_ad.droid)
tturneyc12b6ec2016-03-30 13:39:40 -070072 self.adv_ad.droid.bleStartBleAdvertising(adv_callback, adv_data,
73 adv_settings)
Ang Li73697b32015-12-03 00:41:53 +000074 self.active_adv_callback_list.append(adv_callback)
75
Ang Li73697b32015-12-03 00:41:53 +000076 @BluetoothBaseTest.bt_test_wrap
tturney11b65e42017-01-11 13:57:20 -080077 @test_tracker_info(uuid='4d13c3a8-1805-44ef-a92a-e385540767f1')
Ang Li73697b32015-12-03 00:41:53 +000078 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 """
tturney69e3bb12017-10-26 14:15:05 -0700101 self.scn_ad.droid.bluetoothEnableBLE()
Ang Li73697b32015-12-03 00:41:53 +0000102 self._setup_generic_advertisement()
tturney69e3bb12017-10-26 14:15:05 -0700103 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)
tturney1ce8dc62016-02-18 09:55:53 -0800110 self.scn_ad.droid.bluetoothToggleState(False)
tturney0ab6f012016-05-09 13:59:27 -0700111 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
tturneyc12b6ec2016-03-30 13:39:40 -0700117 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
118 scan_callback)
tturney0ab6f012016-05-09 13:59:27 -0700119 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 Janssen1f333032016-10-25 16:49:35 -0700123 self.log.error("Scan Result event not found. Expected {}".format(
124 expected_event))
tturney0ab6f012016-05-09 13:59:27 -0700125 return False
Ang Li73697b32015-12-03 00:41:53 +0000126 return True
127
128 @BluetoothBaseTest.bt_test_wrap
tturney11b65e42017-01-11 13:57:20 -0800129 @test_tracker_info(uuid='9c4577f8-5e06-4034-b977-285956734974')
Ang Li73697b32015-12-03 00:41:53 +0000130 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()
tturney1ce8dc62016-02-18 09:55:53 -0800154 self.scn_ad.droid.bluetoothEnableBLE()
tturney69e3bb12017-10-26 14:15:05 -0700155 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)
tturney1ce8dc62016-02-18 09:55:53 -0800162 self.scn_ad.droid.bluetoothToggleState(False)
tturney0ab6f012016-05-09 13:59:27 -0700163 try:
164 self.scn_ad.ed.pop_event(bluetooth_off, self.default_timeout)
165 except Empty:
tturney69e3bb12017-10-26 14:15:05 -0700166 self.log.info(self.scn_ad.droid.bluetoothCheckState())
tturney0ab6f012016-05-09 13:59:27 -0700167 self.log.error("Bluetooth Off event not found. Expected {}".format(
168 bluetooth_off))
169 return False
Ang Li73697b32015-12-03 00:41:53 +0000170 try:
tturneyc12b6ec2016-03-30 13:39:40 -0700171 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
172 scan_callback)
tturney0ab6f012016-05-09 13:59:27 -0700173 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 Janssen1f333032016-10-25 16:49:35 -0700177 self.log.error("Scan Result event not found. Expected {}".
178 format(expected_event))
tturney0ab6f012016-05-09 13:59:27 -0700179 return False
Ang Li73697b32015-12-03 00:41:53 +0000180 except Exception:
181 self.log.info(
182 "Was not able to start a background scan as expected.")
183 return True
Marie Janssen1f333032016-10-25 16:49:35 -0700184
185 @BluetoothBaseTest.bt_test_wrap
tturney11b65e42017-01-11 13:57:20 -0800186 @test_tracker_info(uuid='0bdd1764-3dc6-4a82-b041-76e48ed0f424')
Marie Janssen1f333032016-10-25 16:49:35 -0700187 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)
tturney69e3bb12017-10-26 14:15:05 -0700216 self.scn_ad.droid.bluetoothEnableBLE()
Marie Janssen1f333032016-10-25 16:49:35 -0700217 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))
tturney69e3bb12017-10-26 14:15:05 -0700223 self.log.info(self.scn_ad.droid.bluetoothCheckState())
Marie Janssen1f333032016-10-25 16:49:35 -0700224 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()
tturneya4ebb032017-07-24 12:14:45 -0700229 if state != bt_adapter_states['ble_on']:
Marie Janssen1f333032016-10-25 16:49:35 -0700230 self.log.error(
tturney69e3bb12017-10-26 14:15:05 -0700231 ble_state_error_msg.format("after BT Disable",
tturneya4ebb032017-07-24 12:14:45 -0700232 bt_adapter_states['ble_on'], state))
Marie Janssen1f333032016-10-25 16:49:35 -0700233 return False
234
235 self.scn_ad.droid.bluetoothListenForBleStateChange()
236 self.scn_ad.droid.connectivityToggleAirplaneMode(True)
237 try:
tturneya4ebb032017-07-24 12:14:45 -0700238 self.scn_ad.ed.pop_event(bluetooth_le_off, self.default_timeout)
Marie Janssen1f333032016-10-25 16:49:35 -0700239 except Empty:
240 self.log.error("Bluetooth LE Off event not found. Expected {}".
tturneya4ebb032017-07-24 12:14:45 -0700241 format(bluetooth_le_off))
Marie Janssen1f333032016-10-25 16:49:35 -0700242 return False
243 state = self.scn_ad.droid.bluetoothGetLeState()
tturneya4ebb032017-07-24 12:14:45 -0700244 if state != bt_adapter_states['off']:
Marie Janssen1f333032016-10-25 16:49:35 -0700245 self.log.error(
tturneya4ebb032017-07-24 12:14:45 -0700246 ble_state_error_msg.format("after Airplane Mode ON",
247 bt_adapter_states['off'], state))
Marie Janssen1f333032016-10-25 16:49:35 -0700248 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()
tturneya4ebb032017-07-24 12:14:45 -0700256 if state != bt_adapter_states['off']:
Marie Janssen1f333032016-10-25 16:49:35 -0700257 self.log.error(
258 "Bluetooth LE State not OK after attempted enable. Expected {} got {}".
tturneya4ebb032017-07-24 12:14:45 -0700259 format(bt_adapter_states['off'], state))
Marie Janssen1f333032016-10-25 16:49:35 -0700260 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()
tturneya4ebb032017-07-24 12:14:45 -0700265 if state != bt_adapter_states['off']:
Marie Janssen1f333032016-10-25 16:49:35 -0700266 self.log.error(
tturneya4ebb032017-07-24 12:14:45 -0700267 ble_state_error_msg.format("after Airplane Mode OFF",
268 bt_adapter_states['off'], state))
Marie Janssen1f333032016-10-25 16:49:35 -0700269 return False
270 return True