• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (C) 2016 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14import logging
15import time
16from google import protobuf
17
18from acts import asserts
19from acts_contrib.test_utils.bt.BtMetricsBaseTest import BtMetricsBaseTest
20from acts_contrib.test_utils.bt.bt_test_utils import clear_bonded_devices
21from acts_contrib.test_utils.bt.bt_test_utils import pair_pri_to_sec
22from acts_contrib.test_utils.bt.bt_test_utils import reset_bluetooth
23from acts_contrib.test_utils.bt.protos import bluetooth_pb2
24from acts.utils import get_current_epoch_time, sync_device_time
25
26
27class BtMetricsTest(BtMetricsBaseTest):
28    def __init__(self, controllers):
29        BtMetricsBaseTest.__init__(self, controllers)
30        self.iterations = 1
31
32    def setup_class(self):
33        return super(BtMetricsTest, self).setup_class()
34
35    def setup_test(self):
36        # Reset bluetooth
37        reset_bluetooth(self.android_devices)
38        for ad in self.android_devices:
39            if not clear_bonded_devices(ad):
40                logging.error("Failed to unbound device")
41                return False
42            # Sync device time for timestamp comparison
43            sync_device_time(ad)
44        return super(BtMetricsTest, self).setup_test()
45
46    def test_pairing_metric(self):
47        """Test if a pairing event generates the correct metric entry
48
49        This test tries to pair two Bluetooth devices and dumps metrics after
50        pairing. A correctly implemented stack should record 8 pairing events.
51
52        Steps:
53        1. Start pairing between two Bluetooth devices
54        2. After pairing is done, dump and parse the metrics
55        3. Compare the number of pairing events and the time stamp of the
56        pairing event
57
58        Expected Result:
59        No errors, 8 pairing events should be generated
60        Returns:
61          Pass if True
62          Fail if False
63
64        TAGS: Classic
65        Priority: 1
66        """
67        time_bonds = []
68        for n in range(self.iterations):
69            start_time = get_current_epoch_time()
70            self.log.info("Pair bluetooth iteration {}.".format(n + 1))
71            if (not pair_pri_to_sec(
72                    self.android_devices[0],
73                    self.android_devices[1],
74                    attempts=1,
75                    auto_confirm=False)):
76                self.log.error("Failed to bond devices.")
77                return False
78            end_time = get_current_epoch_time()
79            time_bonds.append((start_time, end_time))
80            # A device bond will trigger a number of system routines that need
81            # to settle before unbond
82            time.sleep(2)
83            for ad in self.android_devices:
84                if not clear_bonded_devices(ad):
85                    return False
86                # Necessary sleep time for entries to update unbonded state
87                time.sleep(2)
88                bonded_devices = ad.droid.bluetoothGetBondedDevices()
89                if len(bonded_devices) > 0:
90                    self.log.error("Failed to unbond devices: {}".format(
91                        bonded_devices))
92                    return False
93        end_time = get_current_epoch_time()
94        bluetooth_logs, bluetooth_logs_ascii = \
95            self.collect_bluetooth_manager_metrics_logs(
96                [self.android_devices[0]])
97        bluetooth_log = bluetooth_logs[0]
98        bluetooth_log_ascii = bluetooth_logs_ascii[0]
99        asserts.assert_equal(
100            len(bluetooth_log.pair_event), 8, extras=bluetooth_log_ascii)
101        for pair_event in bluetooth_log.pair_event:
102            t = pair_event.event_time_millis
103            asserts.assert_true(start_time <= t <= end_time,
104                                "Event time %d not within limit [%d, %d]" %
105                                (t, start_time, end_time))
106            device_info = pair_event.device_paired_with
107            asserts.assert_true(device_info, "Device info is none")
108            asserts.assert_equal(device_info.device_type,
109                                 bluetooth_pb2.DeviceInfo.DEVICE_TYPE_BREDR,
110                                 "Device type does not match")
111
112    def test_bluetooth_metrics_parsing(self):
113        """Test if metrics could be dumped and parsed
114
115        This test simply dumps Bluetooth metrics and print out the ASCII
116        representation
117
118        Steps:
119        1. For the first Android device, dump metrics
120        2. Parse and print metrics in INFO log using ASCII format
121
122        Expected Result:
123        No errors, metrics should be printed to INFO log
124
125        Returns:
126          Pass if True
127          Fail if False
128
129        TAGS: Classic
130        Priority: 1
131        """
132        bluetooth_logs, bluetooth_logs_ascii = \
133            self.collect_bluetooth_manager_metrics_logs(
134                [self.android_devices[0]])
135        bluetooth_log = bluetooth_logs[0]
136        self.log.info(protobuf.text_format.MessageToString(bluetooth_log))
137        return True
138