blob: 05d3ea88083a6d11fa80661c19f069a65d9180dd [file] [log] [blame]
Ang Li73697b32015-12-03 00:41:53 +00001# python3.4
2# Copyright (C) 2015 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may not
5# use this file except in compliance with the License. You may obtain a copy of
6# the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations under
14# the License.
15
16"""
17This test script exercises background scan test scenarios.
18"""
19
20from queue import Empty
21
22from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
23from acts.test_utils.bt.BleEnum import BluetoothAdapterState
24from 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
27from acts.test_utils.bt.bt_test_utils import log_energy_info
28from 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
30from acts.test_utils.bt.bt_test_utils import get_advanced_droid_list
31from acts.test_utils.bt.bt_test_utils import scan_result
32
33
34class BleBackgroundScanTest(BluetoothBaseTest):
35 tests = None
36 default_timeout = 10
37 max_scan_instances = 28
38 report_delay = 2000
39 scan_callbacks = []
40 adv_callbacks = []
41 active_scan_callback_list = []
42 active_adv_callback_list = []
43
44 def __init__(self, controllers):
45 BluetoothBaseTest.__init__(self, controllers)
46 self.droid_list = get_advanced_droid_list(self.droids, self.eds)
47 self.scn_droid, self.scn_ed = self.droids[0], self.eds[0]
48 self.adv_droid, self.adv_ed = self.droids[1], self.eds[1]
49 if self.droid_list[1]['max_advertisements'] == 0:
50 self.tests = ()
51 return
52 self.tests = (
53 "test_background_scan",
54 "test_background_scan_ble_disabled",
55 )
56
57 def setup_test(self):
58 self.log.debug(log_energy_info(self.droids, "Start"))
59 if (self.scn_droid.bluetoothGetLeState() ==
60 BluetoothAdapterState.STATE_OFF.value):
61 self.scn_droid.bluetoothEnableBLE()
62 self.scn_ed.pop_event("BleStateChangedOn")
63 for e in self.eds:
64 e.clear_all_events()
65 return True
66
67 def teardown_test(self):
68 self.log.debug(log_energy_info(self.droids, "End"))
69 cleanup_scanners_and_advertisers(
70 self.scn_droid, self.scn_ed, self.active_adv_callback_list,
71 self.adv_droid, self.adv_ed, self.active_adv_callback_list)
72 self.active_adv_callback_list = []
73 self.active_scan_callback_list = []
74
75 def _setup_generic_advertisement(self):
76 adv_callback, adv_data, adv_settings = generate_ble_advertise_objects(
77 self.adv_droid)
78 self.adv_droid.bleStartBleAdvertising(
79 adv_callback, adv_data, adv_settings)
80 self.active_adv_callback_list.append(adv_callback)
81
82 def _verify_no_events_found(self, event_name):
83 try:
84 self.scn_ed.pop_event(event_name, self.default_timeout)
85 self.log.error("Found an event when none was expected.")
86 return False
87 except Empty:
88 self.log.info("No scan result found as expected.")
89 return True
90
91 def _delete_me(self):
92 import time
93 time.sleep(5)
94
95 @BluetoothBaseTest.bt_test_wrap
96 def test_background_scan(self):
97 """Test generic background scan.
98
99 Tests LE background scan. The goal is to find scan results even though
100 Bluetooth is turned off.
101
102 Steps:
103 1. Setup an advertisement on dut1
104 2. Enable LE on the Bluetooth Adapter on dut0
105 3. Toggle BT off on dut1
106 4. Start a LE scan on dut0
107 5. Find the advertisement from dut1
108
109 Expected Result:
110 Find a advertisement from the scan instance.
111
112 Returns:
113 Pass if True
114 Fail if False
115
116 TAGS: LE, Advertising, Scanning, Background Scanning
117 Priority: 0
118 """
119 import time
120 self._setup_generic_advertisement()
121 self.scn_droid.bluetoothToggleState(False)
122 self.scn_ed.pop_event(bluetooth_off, self.default_timeout)
123 self.scn_droid.bluetoothDisableBLE()
124 self.scn_ed.pop_event(bluetooth_off, self.default_timeout)
125 self.scn_droid.bluetoothEnableBLE()
126 self._delete_me()
127 self.scn_ed.pop_event(bluetooth_on, self.default_timeout * 2)
128 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
129 self.scn_droid)
130 self.scn_droid.bleStartBleScan(
131 filter_list, scan_settings, scan_callback)
132 self.scn_ed.pop_event(
133 scan_result.format(scan_callback), self.default_timeout)
134 return True
135
136 @BluetoothBaseTest.bt_test_wrap
137 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()
161 self.scn_droid.bluetoothEnableBLE()
162 self.scn_droid.bluetoothToggleState(False)
163 self.scn_ed.pop_event(bluetooth_off, self.default_timeout)
164 self._delete_me()
165 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
166 self.scn_droid)
167 try:
168 self.scn_droid.bleStartBleScan(
169 filter_list, scan_settings, scan_callback)
170 self.scn_ed.pop_event(scan_result.format(scan_callback))
171 self.log.info("Was able to start background scan even though ble "
172 "was disabled.")
173 return False
174 except Exception:
175 self.log.info(
176 "Was not able to start a background scan as expected.")
177 return True