1#/usr/bin/env python3.4
2#
3# Copyright (C) 2016 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
17import time
18
19from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
20from acts.test_utils.bt.bt_test_utils import *
21
22
23class BtReconnectTest(BluetoothBaseTest):
24    tests = None
25    default_timeout = 10
26
27    def __init__(self, controllers):
28        BluetoothBaseTest.__init__(self, controllers)
29        self.tests = ("test_tool_reconnect", )
30
31    def setup_class(self):
32        return setup_multiple_devices_for_bt_test(self.android_devices)
33
34    def setup_test(self):
35        return reset_bluetooth(self.android_devices)
36
37    def setup_test(self):
38        setup_result = reset_bluetooth(self.android_devices)
39        return setup_result
40
41    @BluetoothBaseTest.bt_test_wrap
42    def test_tool_reconnect(self):
43        droid, ed = self.android_devices[0].droid, self.android_devices[0].ed
44        n = 0
45        test_result = True
46        test_result_list = []
47        sleep_time = input(
48            "Assumption: Android Device is already paired.\nEnter sleep time before toggling bluetooth back on in milliseconds:")
49        sleep_time_ms = int(sleep_time) / 1000
50        iteration_count = input("Enter number of iterations:")
51        while n < int(iteration_count):
52            self.log.info("Test iteration {}.".format(n))
53            test_result = True
54            self.log.info("Toggling BT state off...")
55            droid.bluetoothToggleState(False)
56            self.log.info("Sleeping {} milliseconds".format(sleep_time))
57            time.sleep(sleep_time_ms)
58            self.log.info("Toggling BT state on...")
59            droid.bluetoothToggleState(True)
60            start_time = time.time()
61            connected_devices = droid.bluetoothGetConnectedDevices()
62            self.log.info(
63                "Waiting up to 10 seconds for device to reconnect...")
64            while time.time() < start_time + 10 and len(
65                    connected_devices) != 1:
66                connected_devices = droid.bluetoothGetConnectedDevices()
67                if len(connected_devices) > 0:
68                    break
69            if len(connected_devices) != 1:
70                print(
71                    "Failed to reconnect at iteration {}... continuing".format(
72                        n))
73            test_result_list.append(test_result)
74            n += 1
75        if False in test_result_list:
76            return False
77        return test_result
78