blob: 7865bc197317dc5154d48fcdaf4f5aba5d8354d4 [file] [log] [blame]
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -07001#/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"""
17Automated tests for the testing Connectivity of Avrcp/A2dp profile.
18"""
19
20import time
21
tturney03233972017-08-29 17:46:50 -070022from acts.test_decorators import test_tracker_info
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -070023from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
24from acts.test_utils.bt import bt_test_utils
Sanket Agarwalbd9689b2016-08-24 11:14:53 -070025from acts.test_utils.car import car_bt_utils
Ram Periathiruvadicb34f7c2016-10-06 16:00:29 -070026from acts.test_utils.car import car_media_utils
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -070027from acts.test_utils.bt import BtEnum
28
tturneye3934a22016-10-20 15:47:34 -070029
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -070030class BtCarMediaConnectionTest(BluetoothBaseTest):
31 def setup_class(self):
32 # AVRCP roles
33 self.CT = self.android_devices[0]
34 self.TG = self.android_devices[1]
35 # A2DP roles for the same devices
36 self.SNK = self.CT
37 self.SRC = self.TG
Sanket Agarwalbd9689b2016-08-24 11:14:53 -070038
tturneye3934a22016-10-20 15:47:34 -070039 # Setup devices
Sanket Agarwalbd9689b2016-08-24 11:14:53 -070040 bt_test_utils.setup_multiple_devices_for_bt_test([self.CT, self.TG])
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -070041
42 self.btAddrCT = self.CT.droid.bluetoothGetLocalAddress()
43 self.btAddrTG = self.TG.droid.bluetoothGetLocalAddress()
44
tturney46060782016-11-14 16:44:38 -080045 # Additional time from the stack reset in setup.
46 time.sleep(4)
Sanket Agarwalbd9689b2016-08-24 11:14:53 -070047 # Pair the devices.
tturney46060782016-11-14 16:44:38 -080048 if not bt_test_utils.pair_pri_to_sec(
49 self.CT, self.TG, attempts=4, auto_confirm=False):
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -070050 self.log.error("Failed to pair")
51 return False
52
Sanket Agarwalbd9689b2016-08-24 11:14:53 -070053 # Disable all
54 car_bt_utils.set_car_profile_priorities_off(self.SNK, self.SRC)
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -070055
Sanket Agarwalbd9689b2016-08-24 11:14:53 -070056 # Enable A2DP
57 bt_test_utils.set_profile_priority(
58 self.SNK, self.SRC, [BtEnum.BluetoothProfile.A2DP_SINK],
59 BtEnum.BluetoothPriorityLevel.PRIORITY_ON)
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -070060
Phillip Walker827112a2016-09-08 16:27:19 -070061 def is_a2dp_connected(self, device1, device2):
62 """
63 Convenience Function to see if the 2 devices are connected on
64 A2dp.
65 ToDo: Move to bt_test_utils if used in more places.
66 Args:
67 device1: Device 1
68 device2: Device 2
69 Returns:
70 True if Connected
71 False if Not connected
72 """
73 devices = device1.droid.bluetoothA2dpSinkGetConnectedDevices()
74 for device in devices:
tturney34ee54d2016-11-16 15:29:02 -080075 self.device1.log.info("A2dp Connected device {}".format(device[
76 "name"]))
Phillip Walker827112a2016-09-08 16:27:19 -070077 if (device["address"] == device2.droid.bluetoothGetLocalAddress()):
78 return True
79 return False
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -070080
tturney03233972017-08-29 17:46:50 -070081 @test_tracker_info(uuid='1934c0d5-3fa3-43e5-a91f-2c8a4424f5cd')
tturneye3934a22016-10-20 15:47:34 -070082 @BluetoothBaseTest.bt_test_wrap
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -070083 def test_a2dp_connect_disconnect_from_src(self):
84 """
85 Test Connect/Disconnect on A2DP profile.
86
87 Pre-Condition:
88 1. Devices previously bonded and NOT connected on A2dp
89
90 Steps:
91 1. Initiate a connection on A2DP profile from SRC
92 2. Check if they connected.
93 3. Initiate a disconnect on A2DP profile from SRC
94 4. Ensure they disconnected on A2dp alone
95
96 Returns:
97 True if we connected/disconnected successfully
98 False if we did not connect/disconnect successfully
99
100 Priority: 0
101 """
Ram Periathiruvadicb34f7c2016-10-06 16:00:29 -0700102 if (car_media_utils.is_a2dp_connected(self.log, self.SNK, self.SRC)):
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -0700103 self.log.info("Already Connected")
104 else:
tturney03233972017-08-29 17:46:50 -0700105 if (not bt_test_utils.connect_pri_to_sec(
106 self.SRC, self.SNK,
107 set([BtEnum.BluetoothProfile.A2DP.value]))):
tturney46060782016-11-14 16:44:38 -0800108 return False
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -0700109
110 result = bt_test_utils.disconnect_pri_from_sec(
tturneye3934a22016-10-20 15:47:34 -0700111 self.SRC, self.SNK, [BtEnum.BluetoothProfile.A2DP.value])
112 # Grace timeout to allow a2dp time to disconnect
113 time.sleep(3)
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -0700114 if not result:
tturneye3934a22016-10-20 15:47:34 -0700115 # Additional profile connection check for b/
116 if bt_test_utils.is_a2dp_src_device_connected(
117 self.SRC, self.SNK.droid.bluetoothGetLocalAddress()):
tturney34ee54d2016-11-16 15:29:02 -0800118 self.SRC.log.error("Failed to disconnect on A2dp")
tturneye3934a22016-10-20 15:47:34 -0700119 return False
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -0700120 # Logging if we connected right back, since that happens sometimes
121 # Not failing the test if it did though
Ram Periathiruvadicb34f7c2016-10-06 16:00:29 -0700122 if (car_media_utils.is_a2dp_connected(self.log, self.SNK, self.SRC)):
tturney34ee54d2016-11-16 15:29:02 -0800123 self.SNK.log.error("Still connected after a disconnect")
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -0700124
125 return True
126
tturney03233972017-08-29 17:46:50 -0700127 @test_tracker_info(uuid='70d30007-540a-4e86-bd75-ab218774350e')
tturneye3934a22016-10-20 15:47:34 -0700128 @BluetoothBaseTest.bt_test_wrap
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -0700129 def test_a2dp_connect_disconnect_from_snk(self):
130 """
131 Test Connect/Disconnect on A2DP Sink profile.
132
133 Pre-Condition:
134 1. Devices previously bonded and NOT connected on A2dp
135
136 Steps:
137 1. Initiate a connection on A2DP Sink profile from SNK
138 2. Check if they connected.
139 3. Initiate a disconnect on A2DP Sink profile from SNK
140 4. Ensure they disconnected on A2dp alone
141
142 Returns:
143 True if we connected/disconnected successfully
144 False if we did not connect/disconnect successfully
145
146 Priority: 0
147 """
148 # Connect
tturneye3934a22016-10-20 15:47:34 -0700149 if car_media_utils.is_a2dp_connected(self.log, self.SNK, self.SRC):
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -0700150 self.log.info("Already Connected")
151 else:
tturney03233972017-08-29 17:46:50 -0700152 if (not bt_test_utils.connect_pri_to_sec(
153 self.SNK, self.SRC,
154 set([BtEnum.BluetoothProfile.A2DP_SINK.value]))):
tturney46060782016-11-14 16:44:38 -0800155 return False
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -0700156 # Disconnect
157 result = bt_test_utils.disconnect_pri_from_sec(
tturneye3934a22016-10-20 15:47:34 -0700158 self.SNK, self.SRC, [BtEnum.BluetoothProfile.A2DP_SINK.value])
159 # Grace timeout to allow a2dp time to disconnect
160 time.sleep(3)
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -0700161 if not result:
tturneye3934a22016-10-20 15:47:34 -0700162 # Additional profile connection check for b/
163 if bt_test_utils.is_a2dp_snk_device_connected(
164 self.SNK, self.SRC.droid.bluetoothGetLocalAddress()):
tturney34ee54d2016-11-16 15:29:02 -0800165 self.SNK.log.error("Failed to disconnect on A2dp Sink")
tturneye3934a22016-10-20 15:47:34 -0700166 return False
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -0700167 # Logging if we connected right back, since that happens sometimes
168 # Not failing the test if it did though
tturneye3934a22016-10-20 15:47:34 -0700169 if car_media_utils.is_a2dp_connected(self.log, self.SNK, self.SRC):
tturney34ee54d2016-11-16 15:29:02 -0800170 self.SNK.log.error("Still connected after a disconnect")
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -0700171 return True