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