1#!/usr/bin/env python3 2# 3# Copyright (C) 2018 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""" 17This test script exercises test scenarios for connections to Bluetooth headphones. 18 19This test script was designed with this setup in mind: 20Shield box one: Android Device and 3 headset. 21""" 22 23import time 24 25from acts import logger 26from acts.asserts import assert_false 27from acts.asserts import assert_true 28from acts.keys import Config 29from acts.test_decorators import test_tracker_info 30from acts_contrib.test_utils.bt.bt_constants import ble_scan_settings_modes 31from acts_contrib.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest 32from acts_contrib.test_utils.bt.bt_test_utils import bluetooth_enabled_check 33from acts_contrib.test_utils.bt.bt_test_utils import clear_bonded_devices 34from acts_contrib.test_utils.bt.bt_test_utils import disable_bluetooth 35from acts_contrib.test_utils.bt.bt_test_utils import reset_bluetooth 36from acts.controllers.relay_lib.sony_xb2_speaker import SonyXB2Speaker 37 38 39class HeadphoneTest(BluetoothBaseTest): 40 41 WAIT_TIME = 10 42 iterations = 10 43 44 def _discover_and_pair(self, headphone): 45 self.ad.droid.bluetoothStartDiscovery() 46 time.sleep(10) 47 self.ad.droid.bluetoothCancelDiscovery() 48 for device in self.ad.droid.bluetoothGetDiscoveredDevices(): 49 if device['address'] == headphone.mac_address: 50 self.ad.droid.bluetoothDiscoverAndBond(headphone.mac_address) 51 end_time = time.time() + 20 52 self.log.info("Found the mac address") 53 self.log.info("Verifying devices are bonded") 54 while time.time() < end_time: 55 bonded_devices = self.ad.droid.bluetoothGetBondedDevices() 56 for d in bonded_devices: 57 if d['address'] == headphone.mac_address: 58 self.log.info("Successfully bonded to device.") 59 self.log.info('Bonded devices:\n%s', 60 bonded_devices) 61 return True 62 return False 63 64 def setup_class(self): 65 self.ad.droid.bluetoothFactoryReset() 66 # Factory reset requires a short delay to take effect 67 time.sleep(3) 68 69 self.ad.log.info("Making sure BT phone is enabled here during setup") 70 if not bluetooth_enabled_check(self.ad): 71 self.log.error("Failed to turn Bluetooth on DUT") 72 # Give a breathing time of short delay to take effect 73 time.sleep(3) 74 75 reset_bluetooth([self.ad]) 76 77 # Determine if we have a relay-based device 78 if hasattr(self, 'relay_devices'): 79 for headphone in self.relay_devices: 80 headphone.setup() 81 ''' 82 # Turn of the Power Supply for the headphones. 83 if hasattr(self, 'relay_devices'): 84 power_supply = self.relay_devices[0] 85 power_supply.power_off() 86 ''' 87 super(HeadphoneTest, self).setup_class() 88 return clear_bonded_devices(self.ad) 89 90 def teardown_class(self): 91 if self.headphone_list is not None: 92 for headphone in self.headphone_list: 93 headphone.power_off() 94 headphone.clean_up() 95 ''' 96 power_supply = self.relay_devices[0] 97 power_supply.power_off() 98 ''' 99 100 return clear_bonded_devices(self.ad) 101 102 @property 103 def ad(self): 104 return self.android_devices[0] 105 106 @property 107 def headphone_list(self): 108 return self.relay_devices 109 110 @BluetoothBaseTest.bt_test_wrap 111 @test_tracker_info(uuid='157c1fa1-3d6f-4cfc-8f86-ad267746af71') 112 def test_pair_and_unpair_bt_device(self): 113 """ Test Pairing and Unpairing Bluetooth Headphones to the Android device. 114 115 Steps: 116 1. Pair and Connect Bluetooth headset. 117 2. Disconnect Bleutooth Headset. 118 119 Expected Result: Bluetooth pairing and unpairing should succeed. 120 121 Returns: 122 Pass if True 123 Fail if False 124 125 TAGS: Classic. 126 Priority: 0 127 """ 128 for headphone in self.headphone_list: 129 self.log.info("Start testing on " + headphone.name) 130 131 if self._discover_and_pair(headphone) is False: 132 # The device is probably not in pairing mode, put in pairing mode. 133 134 headphone.turn_power_on_and_enter_pairing_mode() 135 time.sleep(6) 136 if self._discover_and_pair(headphone) is False: 137 # Device must be on, but not in pairing mode. 138 headphone.power_off() 139 headphone.turn_power_on_and_enter_pairing_mode() 140 time.sleep(6) 141 msg = "Unable to pair to %s", headphone.name 142 assert_true(self._discover_and_pair(headphone), msg) 143 144 if len(self.ad.droid.bluetoothGetConnectedDevices()) == 0: 145 self.log.error("Not connected to relay based device.") 146 return False 147 clear_bonded_devices(self.ad) 148 149 @BluetoothBaseTest.bt_test_wrap 150 @test_tracker_info(uuid='7ba73c39-2a69-4a72-b708-d603ce658740') 151 def test_pair_unpair_stress(self): 152 """ Stress Test for Pairing and Unpairing of Bluetooth Headphones. 153 154 Steps: 155 1. Pairing and Connect Bluetooth headset. 156 2. Disconnect Bleutooth Headset. 157 3. Repeat step 1-2 for 10 iterations. 158 159 Expected Result: Bluetooth pairing and unpairing should succeed. 160 161 Returns: 162 Pass if True 163 Fail if False 164 165 TAGS: Classic, Stress 166 Priority: 1 167 """ 168 for n in range(self.iterations): 169 self.log.info("Pair headphone iteration %s.", (n + 1)) 170 171 for headphone in self.headphone_list: 172 if self._discover_and_pair(headphone) is False: 173 # The device is probably not in pairing mode, put in pairing mode. 174 headphone.turn_power_on_and_enter_pairing_mode() 175 time.sleep(6) 176 if self._discover_and_pair(headphone) is False: 177 # Device must be on, but not in pairing mode. 178 headphone.power_off() 179 headphone.turn_power_on_and_enter_pairing_mode() 180 time.sleep(6) 181 msg = "Unable to pair to %s", headphone.name 182 assert_true(self._discover_and_pair(headphone), msg) 183 184 if len(self.ad.droid.bluetoothGetConnectedDevices()) == 0: 185 self.log.error("Not connected to relay based device.") 186 return False 187 clear_bonded_devices(self.ad) 188 189 @BluetoothBaseTest.bt_test_wrap 190 @test_tracker_info(uuid='e2e5fe90-16f1-4830-9b81-6632ae5aab52') 191 def test_multi_a2dp_test(self): 192 """ Test for Pairing and Unpairing mutliple A2dp devices. 193 194 Steps: 195 1. Pairing and Connect multiple Bluetooth headsets. 196 2. Disconnect all Bleutooth Headset. 197 198 Expected Result: All headphones should be connected simultaneously. 199 200 Returns: 201 Pass if True 202 Fail if False 203 204 TAGS: Classic, Multi-A2dp. 205 Priority: 1 206 """ 207 208 for n, headphone in enumerate(self.headphone_list): 209 if self._discover_and_pair(headphone) is False: 210 # The device is probably not in pairing mode, put in pairing mode. 211 headphone.turn_power_on_and_enter_pairing_mode() 212 time.sleep(6) 213 if self._discover_and_pair(headphone) is False: 214 # Device must be on, but not in pairing mode. 215 headphone.power_off() 216 headphone.turn_power_on_and_enter_pairing_mode() 217 time.sleep(6) 218 msg = "Unable to pair to %s", headphone.name 219 assert_true(self._discover_and_pair(headphone), msg) 220 221 if len(self.ad.droid.bluetoothGetConnectedDevices()) != n + 1: 222 self.log.error("Not connected to %s", headphone.name) 223 return False 224