blob: 225f709f0d2e3b04ec6944bb211fc04714bcaddc [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.
16
17"""
18This test script exercises background scan test scenarios.
19"""
20
21from queue import Empty
22
23from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
24from acts.test_utils.bt.BleEnum import BluetoothAdapterState
25from 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
28from acts.test_utils.bt.bt_test_utils import log_energy_info
29from 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
31from acts.test_utils.bt.bt_test_utils import get_advanced_droid_list
32from acts.test_utils.bt.bt_test_utils import scan_result
33
34
35class BleBackgroundScanTest(BluetoothBaseTest):
36 tests = None
37 default_timeout = 10
38 max_scan_instances = 28
39 report_delay = 2000
40 scan_callbacks = []
41 adv_callbacks = []
42 active_scan_callback_list = []
43 active_adv_callback_list = []
44
45 def __init__(self, controllers):
46 BluetoothBaseTest.__init__(self, controllers)
47 self.droid_list = get_advanced_droid_list(self.droids, self.eds)
48 self.scn_droid, self.scn_ed = self.droids[0], self.eds[0]
49 self.adv_droid, self.adv_ed = self.droids[1], self.eds[1]
50 if self.droid_list[1]['max_advertisements'] == 0:
51 self.tests = ()
52 return
53 self.tests = (
54 "test_background_scan",
55 "test_background_scan_ble_disabled",
56 )
57
58 def setup_test(self):
59 self.log.debug(log_energy_info(self.droids, "Start"))
60 if (self.scn_droid.bluetoothGetLeState() ==
61 BluetoothAdapterState.STATE_OFF.value):
62 self.scn_droid.bluetoothEnableBLE()
63 self.scn_ed.pop_event("BleStateChangedOn")
64 for e in self.eds:
65 e.clear_all_events()
66 return True
67
68 def teardown_test(self):
69 self.log.debug(log_energy_info(self.droids, "End"))
70 cleanup_scanners_and_advertisers(
71 self.scn_droid, self.scn_ed, self.active_adv_callback_list,
72 self.adv_droid, self.adv_ed, self.active_adv_callback_list)
73 self.active_adv_callback_list = []
74 self.active_scan_callback_list = []
75
76 def _setup_generic_advertisement(self):
77 adv_callback, adv_data, adv_settings = generate_ble_advertise_objects(
78 self.adv_droid)
79 self.adv_droid.bleStartBleAdvertising(
80 adv_callback, adv_data, adv_settings)
81 self.active_adv_callback_list.append(adv_callback)
82
83 def _verify_no_events_found(self, event_name):
84 try:
85 self.scn_ed.pop_event(event_name, self.default_timeout)
86 self.log.error("Found an event when none was expected.")
87 return False
88 except Empty:
89 self.log.info("No scan result found as expected.")
90 return True
91
92 def _delete_me(self):
93 import time
94 time.sleep(5)
95
96 @BluetoothBaseTest.bt_test_wrap
97 def test_background_scan(self):
98 """Test generic background scan.
99
100 Tests LE background scan. The goal is to find scan results even though
101 Bluetooth is turned off.
102
103 Steps:
104 1. Setup an advertisement on dut1
105 2. Enable LE on the Bluetooth Adapter on dut0
106 3. Toggle BT off on dut1
107 4. Start a LE scan on dut0
108 5. Find the advertisement from dut1
109
110 Expected Result:
111 Find a advertisement from the scan instance.
112
113 Returns:
114 Pass if True
115 Fail if False
116
117 TAGS: LE, Advertising, Scanning, Background Scanning
118 Priority: 0
119 """
120 import time
121 self._setup_generic_advertisement()
122 self.scn_droid.bluetoothToggleState(False)
123 self.scn_ed.pop_event(bluetooth_off, self.default_timeout)
124 self.scn_droid.bluetoothDisableBLE()
125 self.scn_ed.pop_event(bluetooth_off, self.default_timeout)
126 self.scn_droid.bluetoothEnableBLE()
127 self._delete_me()
128 self.scn_ed.pop_event(bluetooth_on, self.default_timeout * 2)
129 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
130 self.scn_droid)
131 self.scn_droid.bleStartBleScan(
132 filter_list, scan_settings, scan_callback)
133 self.scn_ed.pop_event(
134 scan_result.format(scan_callback), self.default_timeout)
135 return True
136
137 @BluetoothBaseTest.bt_test_wrap
138 def test_background_scan_ble_disabled(self):
139 """Test background LE scanning with LE disabled.
140
141 Tests LE background scan. The goal is to find scan results even though
142 Bluetooth is turned off.
143
144 Steps:
145 1. Setup an advertisement on dut1
146 2. Enable LE on the Bluetooth Adapter on dut0
147 3. Toggle BT off on dut1
148 4. Start a LE scan on dut0
149 5. Find the advertisement from dut1
150
151 Expected Result:
152 Find a advertisement from the scan instance.
153
154 Returns:
155 Pass if True
156 Fail if False
157
158 TAGS: LE, Advertising, Scanning, Background Scanning
159 Priority: 0
160 """
161 self._setup_generic_advertisement()
162 self.scn_droid.bluetoothEnableBLE()
163 self.scn_droid.bluetoothToggleState(False)
164 self.scn_ed.pop_event(bluetooth_off, self.default_timeout)
165 self._delete_me()
166 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
167 self.scn_droid)
168 try:
169 self.scn_droid.bleStartBleScan(
170 filter_list, scan_settings, scan_callback)
171 self.scn_ed.pop_event(scan_result.format(scan_callback))
172 self.log.info("Was able to start background scan even though ble "
173 "was disabled.")
174 return False
175 except Exception:
176 self.log.info(
177 "Was not able to start a background scan as expected.")
178 return True