• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#/usr/bin/env python3.4
2#
3# Copyright (C) 2016 The Android Open Source Project
4#
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"""
17Test script to execute Bluetooth basic functionality test cases.
18This test was designed to be run in a shield box.
19"""
20
21import time
22
23from queue import Empty
24from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
25from acts.test_utils.bt.BtEnum import BluetoothScanModeType
26from acts.test_utils.bt.bt_test_utils import check_device_supported_profiles
27from acts.test_utils.bt.bt_test_utils import reset_bluetooth
28from acts.test_utils.bt.bt_test_utils import set_device_name
29from acts.test_utils.bt.bt_test_utils import set_bt_scan_mode
30from acts.test_utils.bt.bt_test_utils import setup_multiple_devices_for_bt_test
31from acts.test_utils.bt.bt_test_utils import take_btsnoop_logs
32
33
34class BtBasicFunctionalityTest(BluetoothBaseTest):
35    default_timeout = 10
36    scan_discovery_time = 5
37
38    def __init__(self, controllers):
39        BluetoothBaseTest.__init__(self, controllers)
40        self.droid_ad = self.android_devices[0]
41        self.droid1_ad = self.android_devices[1]
42
43    def setup_class(self):
44        return setup_multiple_devices_for_bt_test(self.android_devices)
45
46    def setup_test(self):
47        for a in self.android_devices:
48            a.ed.clear_all_events()
49        return True
50
51    def teardown_test(self):
52        return True
53
54    def on_fail(self, test_name, begin_time):
55        take_btsnoop_logs(self.android_devices, self, test_name)
56        reset_bluetooth(self.android_devices)
57
58    @BluetoothBaseTest.bt_test_wrap
59    def test_bluetooth_reset(self):
60        """Test resetting bluetooth.
61
62        Test the integrity of resetting bluetooth on Android.
63
64        Steps:
65        1. Toggle bluetooth off.
66        2. Toggle bluetooth on.
67
68        Expected Result:
69        Bluetooth should toggle on and off without failing.
70
71        Returns:
72          Pass if True
73          Fail if False
74
75        TAGS: Classic
76        Priority: 1
77        """
78        return reset_bluetooth([self.droid_ad])
79
80    @BluetoothBaseTest.bt_test_wrap
81    def test_make_device_discoverable(self):
82        """Test device discoverablity.
83
84        Test that verifies devices is discoverable.
85
86        Steps:
87        1. Initialize two android devices
88        2. Make device1 discoverable
89        3. Check discoverable device1 scan mode
90        4. Make device2 start discovery
91        5. Use device2 get all discovered bluetooth devices list
92        6. Verify device1 is in the list
93
94        Expected Result:
95        Device1 is in the discovered devices list.
96
97        Returns:
98          Pass if True
99          Fail if False
100
101        TAGS: Classic
102        Priority: 1
103        """
104        self.droid_ad.droid.bluetoothMakeDiscoverable()
105        scan_mode = self.droid_ad.droid.bluetoothGetScanMode()
106        if (scan_mode ==
107                BluetoothScanModeType.SCAN_MODE_CONNECTABLE_DISCOVERABLE):
108            self.log.debug("Android device1 scan mode is "
109                           "SCAN_MODE_CONNECTABLE_DISCOVERABLE")
110        else:
111            self.log.debug("Android device1 scan mode is not "
112                           "SCAN_MODE_CONNECTABLE_DISCOVERABLE")
113            return False
114        if self.droid1_ad.droid.bluetoothStartDiscovery():
115            self.log.debug("Android device2 start discovery process success")
116            # Give Bluetooth time to discover advertising devices
117            time.sleep(self.scan_discovery_time)
118            droid_name = self.droid_ad.droid.bluetoothGetLocalName()
119            get_all_discovered_devices = self.droid1_ad.droid.bluetoothGetDiscoveredDevices(
120            )
121            find_flag = False
122            if get_all_discovered_devices:
123                self.log.debug(
124                    "Android device2 get all the discovered devices "
125                    "list {}".format(get_all_discovered_devices))
126                for i in get_all_discovered_devices:
127                    if 'name' in i and i['name'] == droid_name:
128                        self.log.debug("Android device1 is in the discovery "
129                                       "list of device2")
130                        find_flag = True
131                        break
132            else:
133                self.log.debug(
134                    "Android device2 get all the discovered devices "
135                    "list is empty")
136                return False
137        else:
138            self.log.debug("Android device2 start discovery process error")
139            return False
140        if not find_flag:
141            return False
142        return True
143
144    @BluetoothBaseTest.bt_test_wrap
145    def test_make_device_undiscoverable(self):
146        """Test device un-discoverability.
147
148        Test that verifies device is un-discoverable.
149
150        Steps:
151        1. Initialize two android devices
152        2. Make device1 un-discoverable
153        3. Check un-discoverable device1 scan mode
154        4. Make device2 start discovery
155        5. Use device2 get all discovered bluetooth devices list
156        6. Verify device1 is not in the list
157
158        Expected Result:
159        Device1 should not be in the discovered devices list.
160
161        Returns:
162          Pass if True
163          Fail if False
164
165        TAGS: Classic
166        Priority: 1
167        """
168        self.droid_ad.droid.bluetoothMakeUndiscoverable()
169        set_bt_scan_mode(self.droid1_ad,
170                         BluetoothScanModeType.SCAN_MODE_NONE)
171        scan_mode = self.droid1_ad.droid.bluetoothGetScanMode()
172        if scan_mode == BluetoothScanModeType.SCAN_MODE_NONE:
173            self.log.debug("Android device1 scan mode is SCAN_MODE_NONE")
174        else:
175            self.log.debug("Android device1 scan mode is not SCAN_MODE_NONE")
176            return False
177        if self.droid1_ad.droid.bluetoothStartDiscovery():
178            self.log.debug("Android device2 start discovery process success")
179            # Give Bluetooth time to discover advertising devices
180            time.sleep(self.scan_discovery_time)
181            droid_name = self.droid_ad.droid.bluetoothGetLocalName()
182            get_all_discovered_devices = self.droid1_ad.droid.bluetoothGetDiscoveredDevices(
183            )
184            find_flag = False
185            if get_all_discovered_devices:
186                self.log.debug(
187                    "Android device2 get all the discovered devices "
188                    "list {}".format(get_all_discovered_devices))
189                for i in get_all_discovered_devices:
190                    if 'name' in i and i['name'] == droid_name:
191                        self.log.debug(
192                            "Android device1 is in the discovery list of "
193                            "device2")
194                        find_flag = True
195                        break
196            else:
197                self.log.debug("Android device2 found no devices.")
198                return True
199        else:
200            self.log.debug("Android device2 start discovery process error")
201            return False
202        if find_flag:
203            return False
204        return True
205
206    @BluetoothBaseTest.bt_test_wrap
207    def test_set_device_name(self):
208        """Test bluetooth device name.
209
210        Test that a single device can be set device name.
211
212        Steps:
213        1. Initialize one android devices
214        2. Set device name
215        3. Return true is set device name success
216
217        Expected Result:
218        Bluetooth device name is set correctly.
219
220        Returns:
221          Pass if True
222          Fail if False
223
224        TAGS: Classic
225        Priority: 1
226        """
227        name = "SetDeviceName"
228        return set_device_name(self.droid_ad.droid, name)
229
230    def test_scan_mode_off(self):
231        """Test disabling bluetooth scanning.
232
233        Test that changes scan mode to off.
234
235        Steps:
236        1. Initialize android device.
237        2. Set scan mode STATE_OFF by disabling bluetooth.
238        3. Verify scan state.
239
240        Expected Result:
241        Verify scan state is off.
242
243        Returns:
244          Pass if True
245          Fail if False
246
247        TAGS: Classic
248        Priority: 1
249        """
250        self.log.debug("Test scan mode STATE_OFF.")
251        return set_bt_scan_mode(self.droid_ad,
252                                BluetoothScanModeType.STATE_OFF)
253
254    @BluetoothBaseTest.bt_test_wrap
255    def test_scan_mode_none(self):
256        """Test bluetooth scan mode none.
257
258        Test that changes scan mode to none.
259
260        Steps:
261        1. Initialize android device.
262        2. Set scan mode SCAN_MODE_NONE by disabling bluetooth.
263        3. Verify scan state.
264
265        Expected Result:
266        Verify that scan mode is set to none.
267
268        Returns:
269          Pass if True
270          Fail if False
271
272        TAGS: Classic
273        Priority: 1
274        """
275        self.log.debug("Test scan mode SCAN_MODE_NONE.")
276        return set_bt_scan_mode(self.droid_ad,
277                                BluetoothScanModeType.SCAN_MODE_NONE)
278
279    @BluetoothBaseTest.bt_test_wrap
280    def test_scan_mode_connectable(self):
281        """Test bluetooth scan mode connectable.
282
283        Test that changes scan mode to connectable.
284
285        Steps:
286        1. Initialize android device.
287        2. Set scan mode SCAN_MODE_CONNECTABLE.
288        3. Verify scan state.
289
290        Expected Result:
291        Verify that scan mode is set to connectable.
292
293        Returns:
294          Pass if True
295          Fail if False
296
297        TAGS: Classic
298        Priority: 2
299        """
300        self.log.debug("Test scan mode SCAN_MODE_CONNECTABLE.")
301        return set_bt_scan_mode(
302            self.droid_ad, BluetoothScanModeType.SCAN_MODE_CONNECTABLE)
303
304    @BluetoothBaseTest.bt_test_wrap
305    def test_scan_mode_connectable_discoverable(self):
306        """Test bluetooth scan mode connectable.
307
308        Test that changes scan mode to connectable.
309
310        Steps:
311        1. Initialize android device.
312        2. Set scan mode SCAN_MODE_DISCOVERABLE.
313        3. Verify scan state.
314
315        Expected Result:
316        Verify that scan mode is set to discoverable.
317
318        Returns:
319          Pass if True
320          Fail if False
321
322        TAGS: Classic
323        Priority: 2
324        """
325        self.log.debug("Test scan mode SCAN_MODE_CONNECTABLE_DISCOVERABLE.")
326        return set_bt_scan_mode(
327            self.droid_ad,
328            BluetoothScanModeType.SCAN_MODE_CONNECTABLE_DISCOVERABLE)
329
330    @BluetoothBaseTest.bt_test_wrap
331    def test_if_support_hid_profile(self):
332        """ Test that a single device can support HID profile.
333        Steps
334        1. Initialize one android devices
335        2. Check devices support profiles and return a dictionary
336        3. Check the value of key 'hid'
337
338        Expected Result:
339        Device1 is in the discovered devices list.
340
341        Returns:
342          Pass if True
343          Fail if False
344
345        TAGS: Classic
346        Priority: 1
347        """
348        profiles = check_device_supported_profiles(self.droid_ad.droid)
349        if not profiles['hid']:
350            self.log.debug("Android device do not support HID profile.")
351            return False
352        return True
353
354    @BluetoothBaseTest.bt_test_wrap
355    def test_if_support_hsp_profile(self):
356        """ Test that a single device can support HSP profile.
357        Steps
358        1. Initialize one android devices
359        2. Check devices support profiles and return a dictionary
360        3. Check the value of key 'hsp'
361        :return: test_result: bool
362        """
363        profiles = check_device_supported_profiles(self.droid_ad.droid)
364        if not profiles['hsp']:
365            self.log.debug("Android device do not support HSP profile.")
366            return False
367        return True
368
369    @BluetoothBaseTest.bt_test_wrap
370    def test_if_support_a2dp_profile(self):
371        """ Test that a single device can support A2DP profile.
372        Steps
373        1. Initialize one android devices
374        2. Check devices support profiles and return a dictionary
375        3. Check the value of key 'a2dp'
376        :return: test_result: bool
377        """
378        profiles = check_device_supported_profiles(self.droid_ad.droid)
379        if not profiles['a2dp']:
380            self.log.debug("Android device do not support A2DP profile.")
381            return False
382        return True
383