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