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""" 18Test script to test connect and disconnect sequence between two devices which can run 19SL4A. The script does the following: 20 Setup: 21 Clear up the bonded devices on both bluetooth adapters and bond the DUTs to each other. 22 Test (NUM_TEST_RUNS times): 23 1. Connect A2dpSink and HeadsetClient 24 1.1. Check that devices are connected. 25 2. Disconnect A2dpSink and HeadsetClient 26 2.1 Check that devices are disconnected. 27""" 28 29import time 30 31from acts.base_test import BaseTestClass 32from acts.test_utils.bt import bt_test_utils 33from acts.test_utils.bt import BtEnum 34from acts import asserts 35 36class BtCarPairedConnectDisconnectTest(BaseTestClass): 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 def setup_test(self): 44 # Reset the devices in a clean state. 45 bt_test_utils.setup_multiple_devices_for_bt_test(self.android_devices) 46 bt_test_utils.reset_bluetooth(self.android_devices) 47 for a in self.android_devices: 48 a.ed.clear_all_events() 49 50 # Pair the devices. 51 # This call may block until some specified timeout in bt_test_utils.py. 52 result = bt_test_utils.pair_pri_to_sec(self.car.droid, self.ph.droid) 53 54 asserts.assert_true(result, "pair_pri_to_sec returned false."); 55 56 # Check for successful setup of test. 57 devices = self.car.droid.bluetoothGetBondedDevices() 58 asserts.assert_equal(len(devices), 1, "pair_pri_to_sec succeeded but no bonded devices.") 59 60 def on_fail(self, test_name, begin_time): 61 bt_test_utils.take_btsnoop_logs(self.android_devices, self, test_name) 62 63 def test_connect_disconnect_paired(self): 64 """ 65 Tests if we can connect two devices over Headset, A2dp and then disconnect them with success 66 67 Precondition: 68 1. Devices are paired. 69 70 Steps: 71 1. Initiate connection over A2dp Sink and Headset client profiles. 72 2. Check if the connection succeeded. 73 74 Returns: 75 Pass if True 76 Fail if False 77 78 Priority: 0 79 """ 80 81 NUM_TEST_RUNS = 2 82 failure = 0 83 for i in range(NUM_TEST_RUNS): 84 self.log.info("Running test [" + str(i) + "/" + str(NUM_TEST_RUNS) + "]") 85 success = bt_test_utils.connect_pri_to_sec( 86 self.log, self.car, self.ph.droid, 87 set([BtEnum.BluetoothProfile.HEADSET_CLIENT.value, 88 BtEnum.BluetoothProfile.A2DP_SINK.value])) 89 90 # Check if we got connected. 91 if not success: 92 self.log.info("Not all profiles connected.") 93 failure = failure + 1 94 continue 95 96 # Disconnect the devices. 97 self.log.info("Attempt to disconnect.") 98 self.car.droid.bluetoothDisconnectConnected(self.ph_bt_addr) 99 100 end_time = time.time() + 10 101 disconnected = False 102 # Busy loop to check if we have successfully disconnected from the 103 # device 104 while time.time() < end_time: 105 connectedDevices = self.car.droid.bluetoothGetConnectedDevices() 106 exists = False 107 connected_devices = \ 108 self.car.droid.bluetoothGetConnectedDevices() 109 for d in connected_devices: 110 if d['address'] == self.ph_bt_addr: 111 exists = True 112 break 113 if exists is False: 114 disconnected = True 115 break 116 time.sleep(1) 117 118 if disconnected is False: 119 self.log.info("Still connected devices.") 120 failure = failure + 1 121 continue 122 self.log.info("Failure {} total tests {}".format(failure, NUM_TEST_RUNS)) 123 asserts.assert_equal(failure, 0, "") 124 125