1#!/usr/bin/env python3
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"""
17This test is used to test basic functionality of bluetooth adapter by turning it ON/OFF.
18"""
19
20from acts.test_decorators import test_tracker_info
21from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
22from acts.test_utils.bt import bt_test_utils
23
24import random
25import time
26
27
28class BtCarToggleTest(BluetoothBaseTest):
29
30    def setup_class(self):
31        self.droid_ad = self.android_devices[0]
32        for ad in self.android_devices:
33            bt_test_utils.clear_bonded_devices(ad)
34
35    def setup_test(self):
36        self.droid_ad.ed.clear_all_events()
37        if not bt_test_utils.bluetooth_enabled_check(self.droid_ad):
38            return False
39
40    def on_fail(self, test_name, begin_time):
41        bt_test_utils.take_btsnoop_logs(self.android_devices, self, test_name)
42
43    def delay(self, delay_interval):
44        self.log.debug("Toggle delay interval {}".format(0.01 * delay_interval))
45
46        start_time = time.time()
47        while (start_time + 0.01 * delay_interval) > time.time():
48            time.sleep(0.1)
49
50    @test_tracker_info(uuid='290eb41f-6e66-4dc1-8f3e-55783901d116')
51    @BluetoothBaseTest.bt_test_wrap
52    def test_bluetooth_reset(self):
53        """Test resetting bluetooth.
54
55        Test the integrity of resetting bluetooth on Android.
56
57        Steps:
58        1. Toggle bluetooth off.
59        2. Toggle bluetooth on.
60
61        Returns:
62          Pass if True
63          Fail if False
64
65        """
66        return bt_test_utils.reset_bluetooth([self.droid_ad])
67
68    @test_tracker_info(uuid='5ff9dc37-67ff-4367-9803-4a58a22de610')
69    @BluetoothBaseTest.bt_test_wrap
70    def test_bluetooth_continuous_reset(self):
71        """Test resets bluetooth continuously with delay intervals .
72
73            Test the integrity of resetting bluetooth on Android.
74
75            Steps:
76                1. Toggle bluetooth off.
77                2. Toggle bluetooth on.
78                3. Repeat.
79
80            Returns:
81              Pass if True
82              Fail if False
83
84          """
85
86        for count in range(50):
87
88            delay_off_interval = random.randint(0, 1000)
89            delay_on_interval = random.randint(0, 1000)
90
91            passed = True
92
93            self.delay(delay_off_interval)
94
95            passed = self.droid_ad.droid.bluetoothToggleState(False)
96            self.delay(delay_on_interval)
97            passed = passed and self.droid_ad.droid.bluetoothToggleState(True)
98
99            if not passed:
100                self.log.error("Failed to reset bluetooth: Iteration count {}".format(count))
101                return False
102            return True
103