1#!/usr/bin/env python3 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 acts.test_decorators import test_tracker_info 24from acts_contrib.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest 25from acts_contrib.test_utils.bt.bt_constants import bt_scan_mode_types 26from acts_contrib.test_utils.bt.bt_test_utils import check_device_supported_profiles 27from acts_contrib.test_utils.bt.bt_test_utils import reset_bluetooth 28from acts_contrib.test_utils.bt.bt_test_utils import set_device_name 29from acts_contrib.test_utils.bt.bt_test_utils import set_bt_scan_mode 30from acts_contrib.test_utils.bt.bt_test_utils import setup_multiple_devices_for_bt_test 31from acts_contrib.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 setup_class(self): 39 super().setup_class() 40 self.droid_ad = self.android_devices[0] 41 self.droid1_ad = self.android_devices[1] 42 43 return setup_multiple_devices_for_bt_test(self.android_devices) 44 45 def setup_test(self): 46 for a in self.android_devices: 47 a.ed.clear_all_events() 48 return True 49 50 def teardown_test(self): 51 return True 52 53 def on_fail(self, test_name, begin_time): 54 take_btsnoop_logs(self.android_devices, self, test_name) 55 reset_bluetooth(self.android_devices) 56 57 @BluetoothBaseTest.bt_test_wrap 58 @test_tracker_info(uuid='5a5dcf94-8114-405c-a048-b80d73e80ecc') 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 @test_tracker_info(uuid='fc205cb8-6878-4f97-b9c8-7ed532742a1b') 82 def test_make_device_discoverable(self): 83 """Test device discoverablity. 84 85 Test that verifies devices is discoverable. 86 87 Steps: 88 1. Initialize two android devices 89 2. Make device1 discoverable 90 3. Check discoverable device1 scan mode 91 4. Make device2 start discovery 92 5. Use device2 get all discovered bluetooth devices list 93 6. Verify device1 is in the list 94 95 Expected Result: 96 Device1 is in the discovered devices list. 97 98 Returns: 99 Pass if True 100 Fail if False 101 102 TAGS: Classic 103 Priority: 1 104 """ 105 self.droid_ad.droid.bluetoothMakeDiscoverable() 106 scan_mode = self.droid_ad.droid.bluetoothGetScanMode() 107 if (scan_mode == bt_scan_mode_types['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 @test_tracker_info(uuid='c4d77bde-04ed-4805-9185-9bc46dc8af4b') 146 def test_make_device_undiscoverable(self): 147 """Test device un-discoverability. 148 149 Test that verifies device is un-discoverable. 150 151 Steps: 152 1. Initialize two android devices 153 2. Make device1 un-discoverable 154 3. Check un-discoverable device1 scan mode 155 4. Make device2 start discovery 156 5. Use device2 get all discovered bluetooth devices list 157 6. Verify device1 is not in the list 158 159 Expected Result: 160 Device1 should not be in the discovered devices list. 161 162 Returns: 163 Pass if True 164 Fail if False 165 166 TAGS: Classic 167 Priority: 1 168 """ 169 self.droid_ad.droid.bluetoothMakeUndiscoverable() 170 set_bt_scan_mode(self.droid1_ad, bt_scan_mode_types['none']) 171 scan_mode = self.droid1_ad.droid.bluetoothGetScanMode() 172 if scan_mode == bt_scan_mode_types['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 @test_tracker_info(uuid='2bcb6288-64c3-437e-bc89-bcd416310135') 208 def test_set_device_name(self): 209 """Test bluetooth device name. 210 211 Test that a single device can be set device name. 212 213 Steps: 214 1. Initialize one android devices 215 2. Set device name 216 3. Return true is set device name success 217 218 Expected Result: 219 Bluetooth device name is set correctly. 220 221 Returns: 222 Pass if True 223 Fail if False 224 225 TAGS: Classic 226 Priority: 1 227 """ 228 name = "SetDeviceName" 229 return set_device_name(self.droid_ad.droid, name) 230 231 @BluetoothBaseTest.bt_test_wrap 232 @test_tracker_info(uuid='b38fb110-a707-47cf-b1c3-981266373786') 233 def test_scan_mode_off(self): 234 """Test disabling bluetooth scanning. 235 236 Test that changes scan mode to off. 237 238 Steps: 239 1. Initialize android device. 240 2. Set scan mode STATE_OFF by disabling bluetooth. 241 3. Verify scan state. 242 243 Expected Result: 244 Verify scan state is off. 245 246 Returns: 247 Pass if True 248 Fail if False 249 250 TAGS: Classic 251 Priority: 1 252 """ 253 self.log.debug("Test scan mode STATE_OFF.") 254 return set_bt_scan_mode(self.droid_ad, bt_scan_mode_types['state_off']) 255 256 #@BluetoothTest(UUID=27576aa8-d52f-45ad-986a-f44fb565167d) 257 @BluetoothBaseTest.bt_test_wrap 258 @test_tracker_info(uuid='702c3d58-94fd-47ee-9323-2421ce182ddb') 259 def test_scan_mode_none(self): 260 """Test bluetooth scan mode none. 261 262 Test that changes scan mode to none. 263 264 Steps: 265 1. Initialize android device. 266 2. Set scan mode SCAN_MODE_NONE by disabling bluetooth. 267 3. Verify scan state. 268 269 Expected Result: 270 Verify that scan mode is set to none. 271 272 Returns: 273 Pass if True 274 Fail if False 275 276 TAGS: Classic 277 Priority: 1 278 """ 279 self.log.debug("Test scan mode SCAN_MODE_NONE.") 280 return set_bt_scan_mode(self.droid_ad, bt_scan_mode_types['none']) 281 282 @BluetoothBaseTest.bt_test_wrap 283 @test_tracker_info(uuid='cb998a99-31a6-46b6-9de6-a9a17081a604') 284 def test_scan_mode_connectable(self): 285 """Test bluetooth scan mode connectable. 286 287 Test that changes scan mode to connectable. 288 289 Steps: 290 1. Initialize android device. 291 2. Set scan mode SCAN_MODE_CONNECTABLE. 292 3. Verify scan state. 293 294 Expected Result: 295 Verify that scan mode is set to connectable. 296 297 Returns: 298 Pass if True 299 Fail if False 300 301 TAGS: Classic 302 Priority: 2 303 """ 304 self.log.debug("Test scan mode SCAN_MODE_CONNECTABLE.") 305 return set_bt_scan_mode(self.droid_ad, 306 bt_scan_mode_types['connectable']) 307 308 @BluetoothBaseTest.bt_test_wrap 309 @test_tracker_info(uuid='59bec55c-c64e-43e4-9a9a-e44408a801d7') 310 def test_scan_mode_connectable_discoverable(self): 311 """Test bluetooth scan mode connectable. 312 313 Test that changes scan mode to connectable. 314 315 Steps: 316 1. Initialize android device. 317 2. Set scan mode SCAN_MODE_DISCOVERABLE. 318 3. Verify scan state. 319 320 Expected Result: 321 Verify that scan mode is set to discoverable. 322 323 Returns: 324 Pass if True 325 Fail if False 326 327 TAGS: Classic 328 Priority: 2 329 """ 330 self.log.debug("Test scan mode SCAN_MODE_CONNECTABLE_DISCOVERABLE.") 331 return set_bt_scan_mode(self.droid_ad, 332 bt_scan_mode_types['connectable_discoverable']) 333 334 @BluetoothBaseTest.bt_test_wrap 335 @test_tracker_info(uuid='cd20a09d-a68d-4f55-b016-ba283b0460df') 336 def test_if_support_hid_profile(self): 337 """ Test that a single device can support HID profile. 338 Steps 339 1. Initialize one android devices 340 2. Check devices support profiles and return a dictionary 341 3. Check the value of key 'hid' 342 343 Expected Result: 344 Device1 is in the discovered devices list. 345 346 Returns: 347 Pass if True 348 Fail if False 349 350 TAGS: Classic 351 Priority: 1 352 """ 353 profiles = check_device_supported_profiles(self.droid_ad.droid) 354 if not profiles['hid']: 355 self.log.debug("Android device do not support HID profile.") 356 return False 357 return True 358 359 @BluetoothBaseTest.bt_test_wrap 360 @test_tracker_info(uuid='a110d330-7090-4784-a33b-33089dc5f67f') 361 def test_if_support_hsp_profile(self): 362 """ Test that a single device can support HSP profile. 363 Steps 364 1. Initialize one android devices 365 2. Check devices support profiles and return a dictionary 366 3. Check the value of key 'hsp' 367 :return: test_result: bool 368 """ 369 profiles = check_device_supported_profiles(self.droid_ad.droid) 370 if not profiles['hsp']: 371 self.log.debug("Android device do not support HSP profile.") 372 return False 373 return True 374 375 @BluetoothBaseTest.bt_test_wrap 376 @test_tracker_info(uuid='9ccefdd9-62a9-4aed-b4d9-7b0a55c338b2') 377 def test_if_support_a2dp_profile(self): 378 """ Test that a single device can support A2DP profile. 379 Steps 380 1. Initialize one android devices 381 2. Check devices support profiles and return a dictionary 382 3. Check the value of key 'a2dp' 383 :return: test_result: bool 384 """ 385 profiles = check_device_supported_profiles(self.droid_ad.droid) 386 if not profiles['a2dp']: 387 self.log.debug("Android device do not support A2DP profile.") 388 return False 389 return True 390