Home | History | Annotate | Download | only in car_bt
      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 """
     17 Automated tests for the testing Connectivity of Avrcp/A2dp profile.
     18 """
     19 
     20 import time
     21 
     22 from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
     23 from acts.test_utils.bt import bt_test_utils
     24 from acts.test_utils.car import car_bt_utils
     25 from acts.test_utils.car import car_media_utils
     26 from acts.test_utils.bt import BtEnum
     27 
     28 
     29 class BtCarMediaConnectionTest(BluetoothBaseTest):
     30     def setup_class(self):
     31         # AVRCP roles
     32         self.CT = self.android_devices[0]
     33         self.TG = self.android_devices[1]
     34         # A2DP roles for the same devices
     35         self.SNK = self.CT
     36         self.SRC = self.TG
     37 
     38         # Setup devices
     39         bt_test_utils.setup_multiple_devices_for_bt_test([self.CT, self.TG])
     40 
     41         self.btAddrCT = self.CT.droid.bluetoothGetLocalAddress()
     42         self.btAddrTG = self.TG.droid.bluetoothGetLocalAddress()
     43 
     44         # Additional time from the stack reset in setup.
     45         time.sleep(4)
     46         # Pair the devices.
     47         if not bt_test_utils.pair_pri_to_sec(
     48                 self.CT, self.TG, attempts=4, auto_confirm=False):
     49             self.log.error("Failed to pair")
     50             return False
     51 
     52         # Disable all
     53         car_bt_utils.set_car_profile_priorities_off(self.SNK, self.SRC)
     54 
     55         # Enable A2DP
     56         bt_test_utils.set_profile_priority(
     57             self.SNK, self.SRC, [BtEnum.BluetoothProfile.A2DP_SINK],
     58             BtEnum.BluetoothPriorityLevel.PRIORITY_ON)
     59 
     60     def is_a2dp_connected(self, device1, device2):
     61         """
     62         Convenience Function to see if the 2 devices are connected on
     63         A2dp.
     64         ToDo: Move to bt_test_utils if used in more places.
     65         Args:
     66             device1:    Device 1
     67             device2:    Device 2
     68         Returns:
     69             True if Connected
     70             False if Not connected
     71         """
     72         devices = device1.droid.bluetoothA2dpSinkGetConnectedDevices()
     73         for device in devices:
     74             self.device1.log.info("A2dp Connected device {}".format(device[
     75                 "name"]))
     76             if (device["address"] == device2.droid.bluetoothGetLocalAddress()):
     77                 return True
     78         return False
     79 
     80     #@BluetoothTest(UUID=1934c0d5-3fa3-43e5-a91f-2c8a4424f5cd)
     81     @BluetoothBaseTest.bt_test_wrap
     82     def test_a2dp_connect_disconnect_from_src(self):
     83         """
     84         Test Connect/Disconnect on A2DP profile.
     85 
     86         Pre-Condition:
     87         1. Devices previously bonded and NOT connected on A2dp
     88 
     89         Steps:
     90         1. Initiate a connection on A2DP profile from SRC
     91         2. Check if they connected.
     92         3. Initiate a disconnect on A2DP profile from SRC
     93         4. Ensure they disconnected on A2dp alone
     94 
     95         Returns:
     96         True    if we connected/disconnected successfully
     97         False   if we did not connect/disconnect successfully
     98 
     99         Priority: 0
    100         """
    101         if (car_media_utils.is_a2dp_connected(self.log, self.SNK, self.SRC)):
    102             self.log.info("Already Connected")
    103         else:
    104             if (not bt_test_utils.connect_pri_to_sec(self.SRC, self.SNK, set(
    105                 [BtEnum.BluetoothProfile.A2DP.value]))):
    106                 return False
    107 
    108         result = bt_test_utils.disconnect_pri_from_sec(
    109             self.SRC, self.SNK, [BtEnum.BluetoothProfile.A2DP.value])
    110         # Grace timeout to allow a2dp time to disconnect
    111         time.sleep(3)
    112         if not result:
    113             # Additional profile connection check for b/
    114             if bt_test_utils.is_a2dp_src_device_connected(
    115                     self.SRC, self.SNK.droid.bluetoothGetLocalAddress()):
    116                 self.SRC.log.error("Failed to disconnect on A2dp")
    117                 return False
    118         # Logging if we connected right back, since that happens sometimes
    119         # Not failing the test if it did though
    120         if (car_media_utils.is_a2dp_connected(self.log, self.SNK, self.SRC)):
    121             self.SNK.log.error("Still connected after a disconnect")
    122 
    123         return True
    124 
    125     #@BluetoothTest(UUID=70d30007-540a-4e86-bd75-ab218774350e)
    126     @BluetoothBaseTest.bt_test_wrap
    127     def test_a2dp_connect_disconnect_from_snk(self):
    128         """
    129         Test Connect/Disconnect on A2DP Sink profile.
    130 
    131         Pre-Condition:
    132         1. Devices previously bonded and NOT connected on A2dp
    133 
    134         Steps:
    135         1. Initiate a connection on A2DP Sink profile from SNK
    136         2. Check if they connected.
    137         3. Initiate a disconnect on A2DP Sink profile from SNK
    138         4. Ensure they disconnected on A2dp alone
    139 
    140         Returns:
    141         True    if we connected/disconnected successfully
    142         False   if we did not connect/disconnect successfully
    143 
    144         Priority: 0
    145         """
    146         # Connect
    147         if car_media_utils.is_a2dp_connected(self.log, self.SNK, self.SRC):
    148             self.log.info("Already Connected")
    149         else:
    150             if (not bt_test_utils.connect_pri_to_sec(self.SNK, self.SRC, set(
    151                 [BtEnum.BluetoothProfile.A2DP_SINK.value]))):
    152                 return False
    153         # Disconnect
    154         result = bt_test_utils.disconnect_pri_from_sec(
    155             self.SNK, self.SRC, [BtEnum.BluetoothProfile.A2DP_SINK.value])
    156         # Grace timeout to allow a2dp time to disconnect
    157         time.sleep(3)
    158         if not result:
    159             # Additional profile connection check for b/
    160             if bt_test_utils.is_a2dp_snk_device_connected(
    161                     self.SNK, self.SRC.droid.bluetoothGetLocalAddress()):
    162                 self.SNK.log.error("Failed to disconnect on A2dp Sink")
    163                 return False
    164         # Logging if we connected right back, since that happens sometimes
    165         # Not failing the test if it did though
    166         if car_media_utils.is_a2dp_connected(self.log, self.SNK, self.SRC):
    167             self.SNK.log.error("Still connected after a disconnect")
    168         return True
    169