blob: 115b960dd2e5fe9bec41a8bb37fddfdab542bf28 [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
Ang Li73697b32015-12-03 00:41:53 +000027from acts.test_utils.bt.bt_test_utils import generate_ble_advertise_objects
28from acts.test_utils.bt.bt_test_utils import generate_ble_scan_objects
tturney684935b2017-07-24 12:14:45 -070029from acts.test_utils.bt.bt_constants import bluetooth_le_off
30from acts.test_utils.bt.bt_constants import bluetooth_le_on
31from acts.test_utils.bt.bt_constants import bt_adapter_states
32from acts.test_utils.bt.bt_constants import scan_result
33
34import time
Ang Li73697b32015-12-03 00:41:53 +000035
36
37class BleBackgroundScanTest(BluetoothBaseTest):
Ang Li73697b32015-12-03 00:41:53 +000038 default_timeout = 10
39 max_scan_instances = 28
40 report_delay = 2000
41 scan_callbacks = []
42 adv_callbacks = []
43 active_scan_callback_list = []
44 active_adv_callback_list = []
45
46 def __init__(self, controllers):
47 BluetoothBaseTest.__init__(self, controllers)
tturney1ce8dc62016-02-18 09:55:53 -080048 self.scn_ad = self.android_devices[0]
49 self.adv_ad = self.android_devices[1]
Ang Li73697b32015-12-03 00:41:53 +000050
51 def setup_test(self):
tturney1ce8dc62016-02-18 09:55:53 -080052 if (self.scn_ad.droid.bluetoothGetLeState() ==
tturney684935b2017-07-24 12:14:45 -070053 bt_adapter_states['off']):
tturney1ce8dc62016-02-18 09:55:53 -080054 self.scn_ad.droid.bluetoothEnableBLE()
tturney684935b2017-07-24 12:14:45 -070055 self.scn_ad.ed.pop_event(bluetooth_le_on)
tturney653b2732016-02-24 10:40:50 -080056 for a in self.android_devices:
57 a.ed.clear_all_events()
Ang Li73697b32015-12-03 00:41:53 +000058 return True
59
60 def teardown_test(self):
Ang Li73697b32015-12-03 00:41:53 +000061 cleanup_scanners_and_advertisers(
tturneyc12b6ec2016-03-30 13:39:40 -070062 self.scn_ad, self.active_adv_callback_list, self.adv_ad,
63 self.active_adv_callback_list)
Ang Li73697b32015-12-03 00:41:53 +000064 self.active_adv_callback_list = []
65 self.active_scan_callback_list = []
66
67 def _setup_generic_advertisement(self):
68 adv_callback, adv_data, adv_settings = generate_ble_advertise_objects(
tturney1ce8dc62016-02-18 09:55:53 -080069 self.adv_ad.droid)
tturneyc12b6ec2016-03-30 13:39:40 -070070 self.adv_ad.droid.bleStartBleAdvertising(adv_callback, adv_data,
71 adv_settings)
Ang Li73697b32015-12-03 00:41:53 +000072 self.active_adv_callback_list.append(adv_callback)
73
74 def _verify_no_events_found(self, event_name):
75 try:
tturney1ce8dc62016-02-18 09:55:53 -080076 self.scn_ad.ed.pop_event(event_name, self.default_timeout)
Ang Li73697b32015-12-03 00:41:53 +000077 self.log.error("Found an event when none was expected.")
78 return False
79 except Empty:
80 self.log.info("No scan result found as expected.")
81 return True
82
Ang Li73697b32015-12-03 00:41:53 +000083 @BluetoothBaseTest.bt_test_wrap
tturney11b65e42017-01-11 13:57:20 -080084 @test_tracker_info(uuid='4d13c3a8-1805-44ef-a92a-e385540767f1')
Ang Li73697b32015-12-03 00:41:53 +000085 def test_background_scan(self):
86 """Test generic background scan.
87
88 Tests LE background scan. The goal is to find scan results even though
89 Bluetooth is turned off.
90
91 Steps:
92 1. Setup an advertisement on dut1
93 2. Enable LE on the Bluetooth Adapter on dut0
94 3. Toggle BT off on dut1
95 4. Start a LE scan on dut0
96 5. Find the advertisement from dut1
97
98 Expected Result:
99 Find a advertisement from the scan instance.
100
101 Returns:
102 Pass if True
103 Fail if False
104
105 TAGS: LE, Advertising, Scanning, Background Scanning
106 Priority: 0
107 """
108 import time
109 self._setup_generic_advertisement()
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
tturney1ce8dc62016-02-18 09:55:53 -0800117 self.scn_ad.droid.bluetoothDisableBLE()
tturney0ab6f012016-05-09 13:59:27 -0700118 try:
119 self.scn_ad.ed.pop_event(bluetooth_off, self.default_timeout)
120 except Empty:
121 self.log.error("Bluetooth Off event not found. Expected {}".format(
122 bluetooth_off))
123 return False
tturney1ce8dc62016-02-18 09:55:53 -0800124 self.scn_ad.droid.bluetoothEnableBLE()
tturney0ab6f012016-05-09 13:59:27 -0700125 try:
Marie Janssen1f333032016-10-25 16:49:35 -0700126 self.scn_ad.ed.pop_event(bluetooth_off, self.default_timeout * 2)
tturney0ab6f012016-05-09 13:59:27 -0700127 except Empty:
128 self.log.error("Bluetooth On event not found. Expected {}".format(
129 bluetooth_on))
130 return False
Ang Li73697b32015-12-03 00:41:53 +0000131 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
tturney1ce8dc62016-02-18 09:55:53 -0800132 self.scn_ad.droid)
tturneyc12b6ec2016-03-30 13:39:40 -0700133 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
134 scan_callback)
tturney0ab6f012016-05-09 13:59:27 -0700135 expected_event = scan_result.format(scan_callback)
136 try:
137 self.scn_ad.ed.pop_event(expected_event, self.default_timeout)
138 except Empty:
Marie Janssen1f333032016-10-25 16:49:35 -0700139 self.log.error("Scan Result event not found. Expected {}".format(
140 expected_event))
tturney0ab6f012016-05-09 13:59:27 -0700141 return False
Ang Li73697b32015-12-03 00:41:53 +0000142 return True
143
144 @BluetoothBaseTest.bt_test_wrap
tturney11b65e42017-01-11 13:57:20 -0800145 @test_tracker_info(uuid='9c4577f8-5e06-4034-b977-285956734974')
Ang Li73697b32015-12-03 00:41:53 +0000146 def test_background_scan_ble_disabled(self):
147 """Test background LE scanning with LE disabled.
148
149 Tests LE background scan. The goal is to find scan results even though
150 Bluetooth is turned off.
151
152 Steps:
153 1. Setup an advertisement on dut1
154 2. Enable LE on the Bluetooth Adapter on dut0
155 3. Toggle BT off on dut1
156 4. Start a LE scan on dut0
157 5. Find the advertisement from dut1
158
159 Expected Result:
160 Find a advertisement from the scan instance.
161
162 Returns:
163 Pass if True
164 Fail if False
165
166 TAGS: LE, Advertising, Scanning, Background Scanning
167 Priority: 0
168 """
169 self._setup_generic_advertisement()
tturney1ce8dc62016-02-18 09:55:53 -0800170 self.scn_ad.droid.bluetoothEnableBLE()
171 self.scn_ad.droid.bluetoothToggleState(False)
tturney0ab6f012016-05-09 13:59:27 -0700172 try:
173 self.scn_ad.ed.pop_event(bluetooth_off, self.default_timeout)
174 except Empty:
175 self.log.error("Bluetooth Off event not found. Expected {}".format(
176 bluetooth_off))
177 return False
Ang Li73697b32015-12-03 00:41:53 +0000178 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
tturney1ce8dc62016-02-18 09:55:53 -0800179 self.scn_ad.droid)
Ang Li73697b32015-12-03 00:41:53 +0000180 try:
tturneyc12b6ec2016-03-30 13:39:40 -0700181 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
182 scan_callback)
tturney0ab6f012016-05-09 13:59:27 -0700183 expected_event = scan_result.format(scan_callback)
184 try:
185 self.scn_ad.ed.pop_event(expected_event, self.default_timeout)
186 except Empty:
Marie Janssen1f333032016-10-25 16:49:35 -0700187 self.log.error("Scan Result event not found. Expected {}".
188 format(expected_event))
tturney0ab6f012016-05-09 13:59:27 -0700189 return False
Ang Li73697b32015-12-03 00:41:53 +0000190 self.log.info("Was able to start background scan even though ble "
191 "was disabled.")
192 return False
193 except Exception:
194 self.log.info(
195 "Was not able to start a background scan as expected.")
196 return True
Marie Janssen1f333032016-10-25 16:49:35 -0700197
198 @BluetoothBaseTest.bt_test_wrap
tturney11b65e42017-01-11 13:57:20 -0800199 @test_tracker_info(uuid='0bdd1764-3dc6-4a82-b041-76e48ed0f424')
Marie Janssen1f333032016-10-25 16:49:35 -0700200 def test_airplane_mode_disables_ble(self):
201 """Try to start LE mode in Airplane Mode.
202
203 This test will enable airplane mode, then attempt to start LE scanning
204 mode. This should result in bluetooth still being turned off, LE
205 not enabled.
206
207 Steps:
208 1. Start LE only mode.
209 2. Bluetooth should be in LE ONLY mode
210 2. Turn on airplane mode.
211 3. Bluetooth should be OFF
212 4. Try to start LE only mode.
213 5. Bluetooth should stay in OFF mode (LE only start should fail)
214 6. Turn off airplane mode.
215 7. Bluetooth should be OFF.
216
217 Expected Result:
218 No unexpected bluetooth state changes.
219
220 Returns:
221 Pass if True
222 Fail if False
223
224 TAGS: LE, Airplane
225 Priority: 1
226 """
227 ble_state_error_msg = "Bluetooth LE State not OK {}. Expected {} got {}"
228 # Enable BLE always available (effectively enabling BT in location)
229 self.scn_ad.adb.shell(
230 "shell settings put global ble_scan_always_enabled 1")
231
232 self.scn_ad.droid.bluetoothToggleState(False)
233 try:
234 self.scn_ad.ed.pop_event(bluetooth_off, self.default_timeout)
235 except Empty:
236 self.log.error("Bluetooth Off event not found. Expected {}".format(
237 bluetooth_off))
238 return False
239
240 # Sleep because LE turns off after the bluetooth off event fires
241 time.sleep(self.default_timeout)
242 state = self.scn_ad.droid.bluetoothGetLeState()
tturney684935b2017-07-24 12:14:45 -0700243 if state != bt_adapter_states['off']:
Marie Janssen1f333032016-10-25 16:49:35 -0700244 self.log.error(
tturney684935b2017-07-24 12:14:45 -0700245 ble_state_error_msg.format("after BT Disable",
246 bt_adapter_states['off'], state))
Marie Janssen1f333032016-10-25 16:49:35 -0700247 return False
248
249 # TODO: BleStateChangedOn got generated as we shut off bluetooth above?
250 self.scn_ad.ed.clear_all_events()
251 result = self.scn_ad.droid.bluetoothEnableBLE()
252 try:
tturney684935b2017-07-24 12:14:45 -0700253 self.scn_ad.ed.pop_event(bluetooth_le_on, self.default_timeout)
Marie Janssen1f333032016-10-25 16:49:35 -0700254 except Empty:
255 self.log.error("Bluetooth LE On event not found. Expected {}".
tturney684935b2017-07-24 12:14:45 -0700256 format(bluetooth_le_on))
Marie Janssen1f333032016-10-25 16:49:35 -0700257 return False
258 state = self.scn_ad.droid.bluetoothGetLeState()
tturney684935b2017-07-24 12:14:45 -0700259 if state != bt_adapter_states['ble_on']:
Marie Janssen1f333032016-10-25 16:49:35 -0700260 self.log.error(
tturney684935b2017-07-24 12:14:45 -0700261 ble_state_error_msg.format("before Airplane Mode OFF",
262 bt_adapter_states['ble_on'], state))
Marie Janssen1f333032016-10-25 16:49:35 -0700263 return False
264
265 self.scn_ad.droid.bluetoothListenForBleStateChange()
266 self.scn_ad.droid.connectivityToggleAirplaneMode(True)
267 try:
tturney684935b2017-07-24 12:14:45 -0700268 self.scn_ad.ed.pop_event(bluetooth_le_off, self.default_timeout)
Marie Janssen1f333032016-10-25 16:49:35 -0700269 except Empty:
270 self.log.error("Bluetooth LE Off event not found. Expected {}".
tturney684935b2017-07-24 12:14:45 -0700271 format(bluetooth_le_off))
Marie Janssen1f333032016-10-25 16:49:35 -0700272 return False
273 state = self.scn_ad.droid.bluetoothGetLeState()
tturney684935b2017-07-24 12:14:45 -0700274 if state != bt_adapter_states['off']:
Marie Janssen1f333032016-10-25 16:49:35 -0700275 self.log.error(
tturney684935b2017-07-24 12:14:45 -0700276 ble_state_error_msg.format("after Airplane Mode ON",
277 bt_adapter_states['off'], state))
Marie Janssen1f333032016-10-25 16:49:35 -0700278 return False
279 result = self.scn_ad.droid.bluetoothEnableBLE()
280 if result:
281 self.log.error(
282 "Bluetooth Enable command succeded when it should have failed (in airplane mode)"
283 )
284 return False
285 state = self.scn_ad.droid.bluetoothGetLeState()
tturney684935b2017-07-24 12:14:45 -0700286 if state != bt_adapter_states['off']:
Marie Janssen1f333032016-10-25 16:49:35 -0700287 self.log.error(
288 "Bluetooth LE State not OK after attempted enable. Expected {} got {}".
tturney684935b2017-07-24 12:14:45 -0700289 format(bt_adapter_states['off'], state))
Marie Janssen1f333032016-10-25 16:49:35 -0700290 return False
291 self.scn_ad.droid.connectivityToggleAirplaneMode(False)
292 # Sleep to let Airplane Mode disable propogate through the system
293 time.sleep(self.default_timeout)
294 state = self.scn_ad.droid.bluetoothGetLeState()
tturney684935b2017-07-24 12:14:45 -0700295 if state != bt_adapter_states['off']:
Marie Janssen1f333032016-10-25 16:49:35 -0700296 self.log.error(
tturney684935b2017-07-24 12:14:45 -0700297 ble_state_error_msg.format("after Airplane Mode OFF",
298 bt_adapter_states['off'], state))
Marie Janssen1f333032016-10-25 16:49:35 -0700299 return False
300 return True