1"""Tests for blueberry.tests.bluetooth_pairing.""" 2from __future__ import absolute_import 3from __future__ import division 4from __future__ import print_function 5 6import logging 7 8from mobly import test_runner 9from mobly import signals 10from blueberry.utils import asserts 11from blueberry.utils import blueberry_base_test 12 13 14class BluetoothPairingTest(blueberry_base_test.BlueberryBaseTest): 15 """Test Class for Bluetooth Pairing Test. 16 17 Test will reset the bluetooth settings on the phone and attempt to pair 18 with the derived_bt_device specified in the configuration file. 19 """ 20 21 def setup_class(self): 22 """Standard Mobly setup class.""" 23 super(BluetoothPairingTest, self).setup_class() 24 # Adds a use case that derived_bt_device initiates a pairing request to 25 # primary_device. Enable this case if allow_pairing_reverse is 1. 26 self.allow_pairing_reverse = int(self.user_params.get( 27 'allow_pairing_reverse', 0)) 28 29 if self.android_devices: 30 self.primary_device = self.android_devices[0] 31 self.primary_device.init_setup() 32 self.primary_device.sl4a_setup() 33 34 if len(self.android_devices) > 1 and not self.derived_bt_devices: 35 # In the case of pairing phone to phone, we need to treat the 36 # secondary phone as a derived_bt_device in order for the generic script 37 # to work with this android phone properly. 38 self.derived_bt_device = self.android_devices[1] 39 self.derived_bt_devices.append(self.derived_bt_device) 40 self.derived_bt_device.init_setup() 41 self.derived_bt_device.sl4a_setup() 42 else: 43 self.derived_bt_device = self.derived_bt_devices[0] 44 self.derived_bt_device.factory_reset_bluetooth() 45 else: 46 # In the case of pairing mock to mock, at least 2 derived_bt_device is 47 # required. The first derived_bt_device is treated as primary_device. 48 self.primary_device = self.derived_bt_devices[0] 49 self.primary_device.init_setup() 50 self.derived_bt_device = self.derived_bt_devices[1] 51 self.derived_bt_device.factory_reset_bluetooth() 52 53 def setup_test(self): 54 """Setup for pairing test.""" 55 logging.info('Setup Test for test_pair_and_bond') 56 super(BluetoothPairingTest, self).setup_test() 57 58 def test_pair_and_bond(self): 59 """Test for pairing and bonding a phone with a bluetooth device. 60 61 Initiates pairing from the phone and checks for 20 seconds that 62 the device is connected. 63 """ 64 device_list = [(self.primary_device, self.derived_bt_device)] 65 if self.allow_pairing_reverse: 66 device_list.append((self.derived_bt_device, self.primary_device)) 67 for initiator, receiver in device_list: 68 # get mac address of device to pair with 69 mac_address = receiver.get_bluetooth_mac_address() 70 logging.info('Receiver BT MAC Address: %s', mac_address) 71 # put device into pairing mode 72 receiver.activate_pairing_mode() 73 # initiate pairing from initiator 74 initiator.set_target(receiver) 75 with asserts.assert_not_raises(signals.ControllerError): 76 initiator.pair_and_connect_bluetooth(mac_address) 77 if self.allow_pairing_reverse and initiator != self.derived_bt_device: 78 logging.info('===== Reversing Pairing =====') 79 # Resets Bluetooth status for two sides. 80 initiator.factory_reset_bluetooth() 81 receiver.factory_reset_bluetooth() 82 83 84if __name__ == '__main__': 85 test_runner.main() 86