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 test connect and disconnect sequence between two devices which can run 18 SL4A. The script does the following: 19 Setup: 20 Clear up the bonded devices on both bluetooth adapters and bond the DUTs to each other. 21 Test (NUM_TEST_RUNS times): 22 1. Connect A2dpSink and HeadsetClient 23 1.1. Check that devices are connected. 24 2. Disconnect A2dpSink and HeadsetClient 25 2.1 Check that devices are disconnected. 26 """ 27 28 import time 29 30 from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest 31 from acts.base_test import BaseTestClass 32 from acts.test_utils.bt import bt_test_utils 33 from acts.test_utils.bt import BtEnum 34 from acts import asserts 35 36 37 class BtCarPairedConnectDisconnectTest(BluetoothBaseTest): 38 def setup_class(self): 39 self.car = self.android_devices[0] 40 self.ph = self.android_devices[1] 41 self.car_bt_addr = self.car.droid.bluetoothGetLocalAddress() 42 self.ph_bt_addr = self.ph.droid.bluetoothGetLocalAddress() 43 44 bt_test_utils.setup_multiple_devices_for_bt_test([self.car, self.ph]) 45 46 # Pair the devices. 47 # This call may block until some specified timeout in bt_test_utils.py. 48 result = bt_test_utils.pair_pri_to_sec( 49 self.car, self.ph, auto_confirm=False) 50 51 asserts.assert_true(result, "pair_pri_to_sec returned false.") 52 53 # Check for successful setup of test. 54 devices = self.car.droid.bluetoothGetBondedDevices() 55 asserts.assert_equal( 56 len(devices), 1, 57 "pair_pri_to_sec succeeded but no bonded devices.") 58 59 #@BluetoothTest(UUID=b0babf3b-8049-4b64-9125-408efb1bbcd2) 60 @BluetoothBaseTest.bt_test_wrap 61 def test_pairing(self): 62 """ 63 Tests if we can connect two devices over A2dp and then disconnect 64 65 Precondition: 66 1. Devices are paired. 67 68 Steps: 69 1. Set the priority to OFF for all profiles. 70 2. Initiate connection over A2dp Sink client profile. 71 72 Returns: 73 Pass if True 74 Fail if False 75 76 """ 77 # Set the priority to OFF for all profiles. 78 self.car.droid.bluetoothHfpClientSetPriority( 79 self.ph.droid.bluetoothGetLocalAddress(), 80 BtEnum.BluetoothPriorityLevel.PRIORITY_OFF.value) 81 self.ph.droid.bluetoothHspSetPriority( 82 self.car.droid.bluetoothGetLocalAddress(), 83 BtEnum.BluetoothPriorityLevel.PRIORITY_OFF.value) 84 addr = self.ph.droid.bluetoothGetLocalAddress() 85 if not bt_test_utils.connect_pri_to_sec(self.car, self.ph, set( 86 [BtEnum.BluetoothProfile.A2DP_SINK.value])): 87 if not bt_test_utils.is_a2dp_snk_device_connected(self.car, addr): 88 return False 89 return True 90 91 #@BluetoothTest(UUID=a44f13e2-c012-4292-8dd5-9f32a023e297) 92 @BluetoothBaseTest.bt_test_wrap 93 def test_connect_disconnect_paired(self): 94 """ 95 Tests if we can connect two devices over Headset, A2dp and then disconnect them with success 96 97 Precondition: 98 1. Devices are paired. 99 100 Steps: 101 1. Initiate connection over A2dp Sink and Headset client profiles. 102 2. Check if the connection succeeded. 103 104 Returns: 105 Pass if True 106 Fail if False 107 108 Priority: 0 109 """ 110 111 NUM_TEST_RUNS = 2 112 failure = 0 113 addr = self.ph.droid.bluetoothGetLocalAddress() 114 for i in range(NUM_TEST_RUNS): 115 self.log.info("Running test [" + str(i) + "/" + str(NUM_TEST_RUNS) 116 + "]") 117 success = bt_test_utils.connect_pri_to_sec(self.car, self.ph, set( 118 [BtEnum.BluetoothProfile.HEADSET_CLIENT.value, 119 BtEnum.BluetoothProfile.A2DP_SINK.value])) 120 121 # Check if we got connected. 122 if not success: 123 self.car.log.info("Not all profiles connected.") 124 if (bt_test_utils.is_hfp_client_device_connected(self.car, 125 addr) and 126 bt_test_utils.is_a2dp_snk_device_connected(self.car, 127 addr)): 128 self.car.log.info( 129 "HFP Client or A2DP SRC connected successfully.") 130 else: 131 failure = failure + 1 132 continue 133 134 # Disconnect the devices. 135 success = bt_test_utils.disconnect_pri_from_sec( 136 self.car, self.ph, 137 [BtEnum.BluetoothProfile.HEADSET_CLIENT.value, 138 BtEnum.BluetoothProfile.A2DP_SINK.value]) 139 140 if success is False: 141 self.car.log.info("Disconnect failed.") 142 if (bt_test_utils.is_hfp_client_device_connected(self.car, 143 addr) or 144 bt_test_utils.is_a2dp_snk_device_connected(self.car, 145 addr)): 146 self.car.log.info( 147 "HFP Client or A2DP SRC failed to disconnect.") 148 failure = failure + 1 149 continue 150 151 self.log.info("Failure {} total tests {}".format(failure, 152 NUM_TEST_RUNS)) 153 if failure > 0: 154 return False 155 return True 156 157