1#/usr/bin/env python3.4
2#
3# Copyright (C) 2017 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 pairing of an Android Device to a Fugu Remote
18"""
19import time
20
21from acts.controllers.relay_lib.relay import SynchronizeRelays
22from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
23
24class AndroidFuguRemotePairingTest(BluetoothBaseTest):
25    def __init__(self, controllers):
26        BluetoothBaseTest.__init__(self, controllers)
27        self.dut = self.android_devices[0]
28        self.fugu_remote = self.relay_devices[0]
29
30    def setup_test(self):
31        super(BluetoothBaseTest, self).setup_test()
32        self.fugu_remote.setup()
33        return True
34
35    def teardown_test(self):
36        super(BluetoothBaseTest, self).teardown_test()
37        self.fugu_remote.clean_up()
38        return True
39
40    @BluetoothBaseTest.bt_test_wrap
41    def test_pairing(self):
42        """Test pairing between a fugu device and a remote controller.
43
44        Test the remote controller can be paired to fugu.
45
46        Steps:
47        1. Find the MAC address of remote controller from relay config file.
48        2. Start the device paring process.
49        3. Enable remote controller in pairing mode.
50        4. Verify the remote is paired.
51
52        Expected Result:
53        Remote controller is paired.
54
55        Returns:
56          Pass if True
57          Fail if False
58
59        TAGS: fugu
60        Priority: 1
61        """
62
63        self.dut.droid.bluetoothStartPairingHelper(False)
64        self.fugu_remote.enter_pairing_mode()
65        self.dut.droid.bluetoothDiscoverAndBond(self.fugu_remote.mac_address)
66
67        end_time = time.time() + 20
68        self.dut.log.info("Verifying devices are bonded")
69        while time.time() < end_time:
70            bonded_devices = self.dut.droid.bluetoothGetBondedDevices()
71
72            for d in bonded_devices:
73                if d['address'] == self.fugu_remote.mac_address:
74                    self.dut.log.info("Successfully bonded to device.")
75                    self.log.info("Fugu Bonded devices:\n{}".format(
76                        bonded_devices))
77                    return True
78        # Timed out trying to bond.
79        self.dut.log.info("Failed to bond devices.")
80
81        return False
82