1#!/usr/bin/env python3
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 various ThreeButtonDongle devices
18"""
19import time
20
21from acts.controllers.relay_lib.relay import SynchronizeRelays
22from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
23from acts.test_utils.bt.bt_test_utils import clear_bonded_devices
24
25
26class ThreeButtonDongleTest(BluetoothBaseTest):
27    iterations = 10
28
29    def setup_class(self):
30        super().setup_class()
31        self.dut = self.android_devices[0]
32        self.dongle = self.relay_devices[0]
33        self.log.info("Target dongle is {}".format(self.dongle.name))
34
35    def setup_test(self):
36        super(BluetoothBaseTest, self).setup_test()
37        self.dongle.setup()
38        return True
39
40    def teardown_test(self):
41        super(BluetoothBaseTest, self).teardown_test()
42        self.dongle.clean_up()
43        clear_bonded_devices(self.dut)
44        return True
45
46    def _pair_devices(self):
47        self.dut.droid.bluetoothStartPairingHelper(False)
48        self.dongle.enter_pairing_mode()
49
50        self.dut.droid.bluetoothBond(self.dongle.mac_address)
51
52        end_time = time.time() + 20
53        self.dut.log.info("Verifying devices are bonded")
54        while time.time() < end_time:
55            bonded_devices = self.dut.droid.bluetoothGetBondedDevices()
56
57            for d in bonded_devices:
58                if d['address'] == self.dongle.mac_address:
59                    self.dut.log.info("Successfully bonded to device.")
60                    self.log.info("Bonded devices:\n{}".format(bonded_devices))
61                return True
62        self.dut.log.info("Failed to bond devices.")
63        return False
64
65    @BluetoothBaseTest.bt_test_wrap
66    def test_pairing(self):
67        """Test pairing between a three button dongle and an Android device.
68
69        Test the dongle can be paired to Android device.
70
71        Steps:
72        1. Find the MAC address of remote controller from relay config file.
73        2. Start the device paring process.
74        3. Enable remote controller in pairing mode.
75        4. Verify the remote is paired.
76
77        Expected Result:
78          Remote controller is paired.
79
80        Returns:
81          Pass if True
82          Fail if False
83
84        TAGS: Bluetooth, bonding, relay
85        Priority: 3
86        """
87        if not self._pair_devices():
88            return False
89        return True
90
91    @BluetoothBaseTest.bt_test_wrap
92    def test_pairing_multiple_iterations(self):
93        """Test pairing between a three button dongle and an Android device.
94
95        Test the dongle can be paired to Android device.
96
97        Steps:
98        1. Find the MAC address of remote controller from relay config file.
99        2. Start the device paring process.
100        3. Enable remote controller in pairing mode.
101        4. Verify the remote is paired.
102
103        Expected Result:
104          Remote controller is paired.
105
106        Returns:
107          Pass if True
108          Fail if False
109
110        TAGS: Bluetooth, bonding, relay
111        Priority: 3
112        """
113        for i in range(self.iterations):
114            self.log.info("Testing iteration {}.".format(i))
115            if not self._pair_devices():
116                return False
117            self.log.info("Unbonding devices.")
118            self.dut.droid.bluetoothUnbond(self.dongle.mac_address)
119            # Sleep for relax time for the relay
120            time.sleep(2)
121        return True
122
123    @BluetoothBaseTest.bt_test_wrap
124    def test_next_multiple_iterations(self):
125        """Test pairing for multiple iterations.
126
127        Test the dongle can be paired to Android device.
128
129        Steps:
130        1. Pair devices
131        2. Press the next button on dongle for pre-definied iterations.
132
133        Expected Result:
134          Test is successful
135
136        Returns:
137          Pass if True
138          Fail if False
139
140        TAGS: Bluetooth, bonding, relay
141        Priority: 3
142        """
143        if not self._pair_devices():
144            return False
145        for _ in range(self.iterations):
146            self.dongle.press_next()
147        return True
148
149    @BluetoothBaseTest.bt_test_wrap
150    def test_play_pause_multiple_iterations(self):
151        """Test play/pause button on a three button dongle.
152
153        Test the dongle can be paired to Android device.
154
155        Steps:
156        1. Pair devices
157        2. Press the next button on dongle for pre-definied iterations.
158
159        Expected Result:
160          Test is successful
161
162        Returns:
163          Pass if True
164          Fail if False
165
166        TAGS: Bluetooth, bonding, relay
167        Priority: 3
168        """
169        if not self._pair_devices():
170            return False
171        for _ in range(self.iterations):
172            self.dongle.press_play_pause()
173        return True
174
175    @BluetoothBaseTest.bt_test_wrap
176    def test_previous_mulitple_iterations(self):
177        """Test previous button on a three button dongle.
178
179        Test the dongle can be paired to Android device.
180
181        Steps:
182        1. Pair devices
183        2. Press the next button on dongle for pre-definied iterations.
184
185        Expected Result:
186          Test is successful
187
188        Returns:
189          Pass if True
190          Fail if False
191
192        TAGS: Bluetooth, bonding, relay
193        Priority: 3
194        """
195        if not self._pair_devices():
196            return False
197        for _ in range(100):
198            self.dongle.press_previous()
199        return True
200