blob: cf6afc102941939eeefe93fcedaa5f083434ec87 [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
Hansong Zhangaf1d7892018-05-21 15:48:01 -070022from acts import utils
tturney11b65e42017-01-11 13:57:20 -080023from acts.test_decorators import test_tracker_info
Ang Li73697b32015-12-03 00:41:53 +000024from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
Ang Li73697b32015-12-03 00:41:53 +000025from acts.test_utils.bt.bt_test_utils import bluetooth_off
26from acts.test_utils.bt.bt_test_utils import bluetooth_on
27from acts.test_utils.bt.bt_test_utils import cleanup_scanners_and_advertisers
tturney69e3bb12017-10-26 14:15:05 -070028from acts.test_utils.bt.bt_test_utils import enable_bluetooth
Ang Li73697b32015-12-03 00:41:53 +000029from acts.test_utils.bt.bt_test_utils import generate_ble_advertise_objects
30from acts.test_utils.bt.bt_test_utils import generate_ble_scan_objects
tturneya4ebb032017-07-24 12:14:45 -070031from acts.test_utils.bt.bt_constants import bluetooth_le_off
32from acts.test_utils.bt.bt_constants import bluetooth_le_on
33from acts.test_utils.bt.bt_constants import bt_adapter_states
tturney69e3bb12017-10-26 14:15:05 -070034from acts.test_utils.bt.bt_constants import ble_scan_settings_modes
tturneya4ebb032017-07-24 12:14:45 -070035from acts.test_utils.bt.bt_constants import scan_result
36
37import time
Ang Li73697b32015-12-03 00:41:53 +000038
39
40class BleBackgroundScanTest(BluetoothBaseTest):
Ang Li73697b32015-12-03 00:41:53 +000041 default_timeout = 10
42 max_scan_instances = 28
43 report_delay = 2000
44 scan_callbacks = []
45 adv_callbacks = []
46 active_scan_callback_list = []
47 active_adv_callback_list = []
48
49 def __init__(self, controllers):
50 BluetoothBaseTest.__init__(self, controllers)
tturney1ce8dc62016-02-18 09:55:53 -080051 self.scn_ad = self.android_devices[0]
52 self.adv_ad = self.android_devices[1]
Ang Li73697b32015-12-03 00:41:53 +000053
Hansong Zhangaf1d7892018-05-21 15:48:01 -070054 def setup_class(self):
55 super(BluetoothBaseTest, self).setup_class()
56 utils.set_location_service(self.scn_ad, True)
57 utils.set_location_service(self.adv_ad, True)
58 return True
59
Ang Li73697b32015-12-03 00:41:53 +000060 def setup_test(self):
tturney69e3bb12017-10-26 14:15:05 -070061 # Always start tests with Bluetooth enabled and BLE disabled.
62 enable_bluetooth(self.scn_ad.droid, self.scn_ad.ed)
63 self.scn_ad.droid.bluetoothDisableBLE()
tturney653b2732016-02-24 10:40:50 -080064 for a in self.android_devices:
65 a.ed.clear_all_events()
Ang Li73697b32015-12-03 00:41:53 +000066 return True
67
68 def teardown_test(self):
Ang Li73697b32015-12-03 00:41:53 +000069 cleanup_scanners_and_advertisers(
tturneyc12b6ec2016-03-30 13:39:40 -070070 self.scn_ad, self.active_adv_callback_list, self.adv_ad,
71 self.active_adv_callback_list)
Ang Li73697b32015-12-03 00:41:53 +000072 self.active_adv_callback_list = []
73 self.active_scan_callback_list = []
74
75 def _setup_generic_advertisement(self):
tturney69e3bb12017-10-26 14:15:05 -070076 self.adv_ad.droid.bleSetAdvertiseDataIncludeDeviceName(True)
Ang Li73697b32015-12-03 00:41:53 +000077 adv_callback, adv_data, adv_settings = generate_ble_advertise_objects(
tturney1ce8dc62016-02-18 09:55:53 -080078 self.adv_ad.droid)
tturneyc12b6ec2016-03-30 13:39:40 -070079 self.adv_ad.droid.bleStartBleAdvertising(adv_callback, adv_data,
80 adv_settings)
Ang Li73697b32015-12-03 00:41:53 +000081 self.active_adv_callback_list.append(adv_callback)
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 """
tturney69e3bb12017-10-26 14:15:05 -0700108 self.scn_ad.droid.bluetoothEnableBLE()
Ang Li73697b32015-12-03 00:41:53 +0000109 self._setup_generic_advertisement()
Ashutosh Singh6c71aa02018-05-14 15:51:14 -0700110 self.scn_ad.droid.bleSetScanSettingsScanMode(
111 ble_scan_settings_modes['low_latency'])
tturney69e3bb12017-10-26 14:15:05 -0700112 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
113 self.scn_ad.droid)
114 self.scn_ad.droid.bleSetScanFilterDeviceName(
115 self.adv_ad.droid.bluetoothGetLocalName())
116 self.scn_ad.droid.bleBuildScanFilter(filter_list)
tturney1ce8dc62016-02-18 09:55:53 -0800117 self.scn_ad.droid.bluetoothToggleState(False)
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
tturneyc12b6ec2016-03-30 13:39:40 -0700124 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
125 scan_callback)
tturney0ab6f012016-05-09 13:59:27 -0700126 expected_event = scan_result.format(scan_callback)
127 try:
128 self.scn_ad.ed.pop_event(expected_event, self.default_timeout)
129 except Empty:
Marie Janssen1f333032016-10-25 16:49:35 -0700130 self.log.error("Scan Result event not found. Expected {}".format(
131 expected_event))
tturney0ab6f012016-05-09 13:59:27 -0700132 return False
Ang Li73697b32015-12-03 00:41:53 +0000133 return True
134
135 @BluetoothBaseTest.bt_test_wrap
tturney11b65e42017-01-11 13:57:20 -0800136 @test_tracker_info(uuid='9c4577f8-5e06-4034-b977-285956734974')
Ang Li73697b32015-12-03 00:41:53 +0000137 def test_background_scan_ble_disabled(self):
138 """Test background LE scanning with LE disabled.
139
140 Tests LE background scan. The goal is to find scan results even though
141 Bluetooth is turned off.
142
143 Steps:
144 1. Setup an advertisement on dut1
145 2. Enable LE on the Bluetooth Adapter on dut0
146 3. Toggle BT off on dut1
147 4. Start a LE scan on dut0
148 5. Find the advertisement from dut1
149
150 Expected Result:
151 Find a advertisement from the scan instance.
152
153 Returns:
154 Pass if True
155 Fail if False
156
157 TAGS: LE, Advertising, Scanning, Background Scanning
158 Priority: 0
159 """
160 self._setup_generic_advertisement()
tturney1ce8dc62016-02-18 09:55:53 -0800161 self.scn_ad.droid.bluetoothEnableBLE()
Ashutosh Singh6c71aa02018-05-14 15:51:14 -0700162 self.scn_ad.droid.bleSetScanSettingsScanMode(
163 ble_scan_settings_modes['low_latency'])
tturney69e3bb12017-10-26 14:15:05 -0700164 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
165 self.scn_ad.droid)
166 self.scn_ad.droid.bleSetScanFilterDeviceName(
167 self.adv_ad.droid.bluetoothGetLocalName())
168 self.scn_ad.droid.bleBuildScanFilter(filter_list)
tturney1ce8dc62016-02-18 09:55:53 -0800169 self.scn_ad.droid.bluetoothToggleState(False)
tturney0ab6f012016-05-09 13:59:27 -0700170 try:
171 self.scn_ad.ed.pop_event(bluetooth_off, self.default_timeout)
172 except Empty:
tturney69e3bb12017-10-26 14:15:05 -0700173 self.log.info(self.scn_ad.droid.bluetoothCheckState())
tturney0ab6f012016-05-09 13:59:27 -0700174 self.log.error("Bluetooth Off event not found. Expected {}".format(
175 bluetooth_off))
176 return False
Ang Li73697b32015-12-03 00:41:53 +0000177 try:
tturneyc12b6ec2016-03-30 13:39:40 -0700178 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
179 scan_callback)
tturney0ab6f012016-05-09 13:59:27 -0700180 expected_event = scan_result.format(scan_callback)
181 try:
182 self.scn_ad.ed.pop_event(expected_event, self.default_timeout)
183 except Empty:
Ashutosh Singh6c71aa02018-05-14 15:51:14 -0700184 self.log.error(
185 "Scan Result event not found. Expected {}".format(
186 expected_event))
tturney0ab6f012016-05-09 13:59:27 -0700187 return False
Ang Li73697b32015-12-03 00:41:53 +0000188 except Exception:
189 self.log.info(
190 "Was not able to start a background scan as expected.")
191 return True
Marie Janssen1f333032016-10-25 16:49:35 -0700192
193 @BluetoothBaseTest.bt_test_wrap
tturney11b65e42017-01-11 13:57:20 -0800194 @test_tracker_info(uuid='0bdd1764-3dc6-4a82-b041-76e48ed0f424')
Marie Janssen1f333032016-10-25 16:49:35 -0700195 def test_airplane_mode_disables_ble(self):
196 """Try to start LE mode in Airplane Mode.
197
198 This test will enable airplane mode, then attempt to start LE scanning
199 mode. This should result in bluetooth still being turned off, LE
200 not enabled.
201
202 Steps:
203 1. Start LE only mode.
204 2. Bluetooth should be in LE ONLY mode
205 2. Turn on airplane mode.
206 3. Bluetooth should be OFF
207 4. Try to start LE only mode.
208 5. Bluetooth should stay in OFF mode (LE only start should fail)
209 6. Turn off airplane mode.
210 7. Bluetooth should be OFF.
211
212 Expected Result:
213 No unexpected bluetooth state changes.
214
215 Returns:
216 Pass if True
217 Fail if False
218
219 TAGS: LE, Airplane
220 Priority: 1
221 """
222 ble_state_error_msg = "Bluetooth LE State not OK {}. Expected {} got {}"
223 # Enable BLE always available (effectively enabling BT in location)
Ashutosh Singh6c71aa02018-05-14 15:51:14 -0700224 self.scn_ad.shell.enable_ble_scanning()
tturney69e3bb12017-10-26 14:15:05 -0700225 self.scn_ad.droid.bluetoothEnableBLE()
Marie Janssen1f333032016-10-25 16:49:35 -0700226 self.scn_ad.droid.bluetoothToggleState(False)
227 try:
228 self.scn_ad.ed.pop_event(bluetooth_off, self.default_timeout)
229 except Empty:
230 self.log.error("Bluetooth Off event not found. Expected {}".format(
231 bluetooth_off))
tturney69e3bb12017-10-26 14:15:05 -0700232 self.log.info(self.scn_ad.droid.bluetoothCheckState())
Marie Janssen1f333032016-10-25 16:49:35 -0700233 return False
234
235 # Sleep because LE turns off after the bluetooth off event fires
236 time.sleep(self.default_timeout)
237 state = self.scn_ad.droid.bluetoothGetLeState()
tturneya4ebb032017-07-24 12:14:45 -0700238 if state != bt_adapter_states['ble_on']:
Marie Janssen1f333032016-10-25 16:49:35 -0700239 self.log.error(
tturney69e3bb12017-10-26 14:15:05 -0700240 ble_state_error_msg.format("after BT Disable",
tturneya4ebb032017-07-24 12:14:45 -0700241 bt_adapter_states['ble_on'], state))
Marie Janssen1f333032016-10-25 16:49:35 -0700242 return False
243
244 self.scn_ad.droid.bluetoothListenForBleStateChange()
245 self.scn_ad.droid.connectivityToggleAirplaneMode(True)
246 try:
tturneya4ebb032017-07-24 12:14:45 -0700247 self.scn_ad.ed.pop_event(bluetooth_le_off, self.default_timeout)
Marie Janssen1f333032016-10-25 16:49:35 -0700248 except Empty:
Ashutosh Singh6c71aa02018-05-14 15:51:14 -0700249 self.log.error(
250 "Bluetooth LE Off event not found. Expected {}".format(
251 bluetooth_le_off))
Marie Janssen1f333032016-10-25 16:49:35 -0700252 return False
253 state = self.scn_ad.droid.bluetoothGetLeState()
tturneya4ebb032017-07-24 12:14:45 -0700254 if state != bt_adapter_states['off']:
Marie Janssen1f333032016-10-25 16:49:35 -0700255 self.log.error(
tturneya4ebb032017-07-24 12:14:45 -0700256 ble_state_error_msg.format("after Airplane Mode ON",
257 bt_adapter_states['off'], state))
Marie Janssen1f333032016-10-25 16:49:35 -0700258 return False
259 result = self.scn_ad.droid.bluetoothEnableBLE()
260 if result:
261 self.log.error(
262 "Bluetooth Enable command succeded when it should have failed (in airplane mode)"
263 )
264 return False
265 state = self.scn_ad.droid.bluetoothGetLeState()
tturneya4ebb032017-07-24 12:14:45 -0700266 if state != bt_adapter_states['off']:
Marie Janssen1f333032016-10-25 16:49:35 -0700267 self.log.error(
268 "Bluetooth LE State not OK after attempted enable. Expected {} got {}".
tturneya4ebb032017-07-24 12:14:45 -0700269 format(bt_adapter_states['off'], state))
Marie Janssen1f333032016-10-25 16:49:35 -0700270 return False
271 self.scn_ad.droid.connectivityToggleAirplaneMode(False)
272 # Sleep to let Airplane Mode disable propogate through the system
273 time.sleep(self.default_timeout)
274 state = self.scn_ad.droid.bluetoothGetLeState()
tturneya4ebb032017-07-24 12:14:45 -0700275 if state != bt_adapter_states['off']:
Marie Janssen1f333032016-10-25 16:49:35 -0700276 self.log.error(
tturneya4ebb032017-07-24 12:14:45 -0700277 ble_state_error_msg.format("after Airplane Mode OFF",
278 bt_adapter_states['off'], state))
Marie Janssen1f333032016-10-25 16:49:35 -0700279 return False
280 return True