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 test connect and disconnect sequence between two devices which can run 18SL4A. 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 28from acts.test_decorators import test_tracker_info 29from acts_contrib.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest 30from acts.base_test import BaseTestClass 31from acts_contrib.test_utils.bt import bt_test_utils 32from acts_contrib.test_utils.bt import BtEnum 33from acts import asserts 34 35 36class BtCarPairedConnectDisconnectTest(BluetoothBaseTest): 37 def setup_class(self): 38 self.car = self.android_devices[0] 39 self.ph = self.android_devices[1] 40 self.car_bt_addr = self.car.droid.bluetoothGetLocalAddress() 41 self.ph_bt_addr = self.ph.droid.bluetoothGetLocalAddress() 42 43 bt_test_utils.setup_multiple_devices_for_bt_test([self.car, self.ph]) 44 45 # Pair the devices. 46 # This call may block until some specified timeout in bt_test_utils.py. 47 result = bt_test_utils.pair_pri_to_sec(self.car, 48 self.ph, 49 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 @test_tracker_info(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.bluetoothHspSetConnectionPolicy( 82 self.car.droid.bluetoothGetLocalAddress(), 83 BtEnum.BluetoothConnectionPolicy.CONNECTION_POLICY_FORBIDDEN.value) 84 addr = self.ph.droid.bluetoothGetLocalAddress() 85 if not bt_test_utils.connect_pri_to_sec( 86 self.car, self.ph, 87 set([BtEnum.BluetoothProfile.A2DP_SINK.value])): 88 if not bt_test_utils.is_a2dp_snk_device_connected(self.car, addr): 89 return False 90 return True 91 92 @test_tracker_info(uuid='a44f13e2-c012-4292-8dd5-9f32a023e297') 93 @BluetoothBaseTest.bt_test_wrap 94 def test_connect_disconnect_paired(self): 95 """ 96 Tests if we can connect two devices over Headset, A2dp and then disconnect them with success 97 98 Precondition: 99 1. Devices are paired. 100 101 Steps: 102 1. Initiate connection over A2dp Sink and Headset client profiles. 103 2. Check if the connection succeeded. 104 105 Returns: 106 Pass if True 107 Fail if False 108 109 Priority: 0 110 """ 111 112 NUM_TEST_RUNS = 2 113 failure = 0 114 addr = self.ph.droid.bluetoothGetLocalAddress() 115 for i in range(NUM_TEST_RUNS): 116 self.log.info("Running test [" + str(i) + "/" + 117 str(NUM_TEST_RUNS) + "]") 118 success = bt_test_utils.connect_pri_to_sec( 119 self.car, self.ph, 120 set([ 121 BtEnum.BluetoothProfile.HEADSET_CLIENT.value, 122 BtEnum.BluetoothProfile.A2DP_SINK.value 123 ])) 124 125 # Check if we got connected. 126 if not success: 127 self.car.log.info("Not all profiles connected.") 128 if (bt_test_utils.is_hfp_client_device_connected( 129 self.car, addr) 130 and bt_test_utils.is_a2dp_snk_device_connected( 131 self.car, addr)): 132 self.car.log.info( 133 "HFP Client or A2DP SRC connected successfully.") 134 else: 135 failure = failure + 1 136 continue 137 138 # Disconnect the devices. 139 success = bt_test_utils.disconnect_pri_from_sec( 140 self.car, self.ph, [ 141 BtEnum.BluetoothProfile.HEADSET_CLIENT.value, 142 BtEnum.BluetoothProfile.A2DP_SINK.value 143 ]) 144 145 if success is False: 146 self.car.log.info("Disconnect failed.") 147 if (bt_test_utils.is_hfp_client_device_connected( 148 self.car, addr) 149 or bt_test_utils.is_a2dp_snk_device_connected( 150 self.car, addr)): 151 self.car.log.info( 152 "HFP Client or A2DP SRC failed to disconnect.") 153 failure = failure + 1 154 continue 155 156 self.log.info("Failure {} total tests {}".format( 157 failure, NUM_TEST_RUNS)) 158 if failure > 0: 159 return False 160 return True 161