blob: ff6436d68c8df847d2779c3b1413873dd76a77ef [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
22from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
23from acts.test_utils.bt import bt_test_utils
Sanket Agarwalbd9689b2016-08-24 11:14:53 -070024from acts.test_utils.car import car_bt_utils
Ram Periathiruvadicb34f7c2016-10-06 16:00:29 -070025from acts.test_utils.car import car_media_utils
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -070026from acts.test_utils.bt import BtEnum
27
tturneye3934a22016-10-20 15:47:34 -070028
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -070029class 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
Sanket Agarwalbd9689b2016-08-24 11:14:53 -070037
tturneye3934a22016-10-20 15:47:34 -070038 # Setup devices
Sanket Agarwalbd9689b2016-08-24 11:14:53 -070039 bt_test_utils.setup_multiple_devices_for_bt_test([self.CT, self.TG])
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -070040
41 self.btAddrCT = self.CT.droid.bluetoothGetLocalAddress()
42 self.btAddrTG = self.TG.droid.bluetoothGetLocalAddress()
43
Sanket Agarwalbd9689b2016-08-24 11:14:53 -070044 # Pair the devices.
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -070045 if not bt_test_utils.pair_pri_to_sec(self.CT.droid, self.TG.droid):
46 self.log.error("Failed to pair")
47 return False
48
Sanket Agarwalbd9689b2016-08-24 11:14:53 -070049 # Disable all
50 car_bt_utils.set_car_profile_priorities_off(self.SNK, self.SRC)
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -070051
Sanket Agarwalbd9689b2016-08-24 11:14:53 -070052 # Enable A2DP
53 bt_test_utils.set_profile_priority(
54 self.SNK, self.SRC, [BtEnum.BluetoothProfile.A2DP_SINK],
55 BtEnum.BluetoothPriorityLevel.PRIORITY_ON)
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -070056
Phillip Walker827112a2016-09-08 16:27:19 -070057 def is_a2dp_connected(self, device1, device2):
58 """
59 Convenience Function to see if the 2 devices are connected on
60 A2dp.
61 ToDo: Move to bt_test_utils if used in more places.
62 Args:
63 device1: Device 1
64 device2: Device 2
65 Returns:
66 True if Connected
67 False if Not connected
68 """
69 devices = device1.droid.bluetoothA2dpSinkGetConnectedDevices()
70 for device in devices:
71 self.log.info("A2dp Connected device {}".format(device["name"]))
72 if (device["address"] == device2.droid.bluetoothGetLocalAddress()):
73 return True
74 return False
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -070075
Phillip Walker827112a2016-09-08 16:27:19 -070076 #@BluetoothTest(UUID=1934c0d5-3fa3-43e5-a91f-2c8a4424f5cd)
tturneye3934a22016-10-20 15:47:34 -070077 @BluetoothBaseTest.bt_test_wrap
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -070078 def test_a2dp_connect_disconnect_from_src(self):
79 """
80 Test Connect/Disconnect on A2DP profile.
81
82 Pre-Condition:
83 1. Devices previously bonded and NOT connected on A2dp
84
85 Steps:
86 1. Initiate a connection on A2DP profile from SRC
87 2. Check if they connected.
88 3. Initiate a disconnect on A2DP profile from SRC
89 4. Ensure they disconnected on A2dp alone
90
91 Returns:
92 True if we connected/disconnected successfully
93 False if we did not connect/disconnect successfully
94
95 Priority: 0
96 """
Ram Periathiruvadicb34f7c2016-10-06 16:00:29 -070097 if (car_media_utils.is_a2dp_connected(self.log, self.SNK, self.SRC)):
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -070098 self.log.info("Already Connected")
99 else:
100 result = bt_test_utils.connect_pri_to_sec(
tturneye3934a22016-10-20 15:47:34 -0700101 self.SRC, self.SNK, set([BtEnum.BluetoothProfile.A2DP.value]))
102 if not result:
103 # Additional profile connection check for b/
104 if not bt_test_utils.is_a2dp_src_device_connected(
105 self.SRC, self.SNK.droid.bluetoothGetLocalAddress()):
106 self.log.error("Failed to connect on A2dp")
107 return False
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -0700108
109 result = bt_test_utils.disconnect_pri_from_sec(
tturneye3934a22016-10-20 15:47:34 -0700110 self.SRC, self.SNK, [BtEnum.BluetoothProfile.A2DP.value])
111 # Grace timeout to allow a2dp time to disconnect
112 time.sleep(3)
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -0700113 if not result:
tturneye3934a22016-10-20 15:47:34 -0700114 # Additional profile connection check for b/
115 if bt_test_utils.is_a2dp_src_device_connected(
116 self.SRC, self.SNK.droid.bluetoothGetLocalAddress()):
117 self.log.error("Failed to disconnect on A2dp")
118 return False
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -0700119 # Logging if we connected right back, since that happens sometimes
120 # Not failing the test if it did though
Ram Periathiruvadicb34f7c2016-10-06 16:00:29 -0700121 if (car_media_utils.is_a2dp_connected(self.log, self.SNK, self.SRC)):
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -0700122 self.log.error("Still connected after a disconnect")
123
124 return True
125
Phillip Walker827112a2016-09-08 16:27:19 -0700126 #@BluetoothTest(UUID=70d30007-540a-4e86-bd75-ab218774350e)
tturneye3934a22016-10-20 15:47:34 -0700127 @BluetoothBaseTest.bt_test_wrap
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -0700128 def test_a2dp_connect_disconnect_from_snk(self):
129 """
130 Test Connect/Disconnect on A2DP Sink profile.
131
132 Pre-Condition:
133 1. Devices previously bonded and NOT connected on A2dp
134
135 Steps:
136 1. Initiate a connection on A2DP Sink profile from SNK
137 2. Check if they connected.
138 3. Initiate a disconnect on A2DP Sink profile from SNK
139 4. Ensure they disconnected on A2dp alone
140
141 Returns:
142 True if we connected/disconnected successfully
143 False if we did not connect/disconnect successfully
144
145 Priority: 0
146 """
147 # Connect
tturneye3934a22016-10-20 15:47:34 -0700148 if car_media_utils.is_a2dp_connected(self.log, self.SNK, self.SRC):
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -0700149 self.log.info("Already Connected")
150 else:
151 result = bt_test_utils.connect_pri_to_sec(
tturneye3934a22016-10-20 15:47:34 -0700152 self.SNK, self.SRC,
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -0700153 set([BtEnum.BluetoothProfile.A2DP_SINK.value]))
tturneye3934a22016-10-20 15:47:34 -0700154 if not result:
155 # Additional profile connection check for b/
156 if not bt_test_utils.is_a2dp_snk_device_connected(
157 self.SNK, self.SRC.droid.bluetoothGetLocalAddress()):
158 self.log.error("Failed to connect on A2dp Sink")
159 return False
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -0700160 # Disconnect
161 result = bt_test_utils.disconnect_pri_from_sec(
tturneye3934a22016-10-20 15:47:34 -0700162 self.SNK, self.SRC, [BtEnum.BluetoothProfile.A2DP_SINK.value])
163 # Grace timeout to allow a2dp time to disconnect
164 time.sleep(3)
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -0700165 if not result:
tturneye3934a22016-10-20 15:47:34 -0700166 # Additional profile connection check for b/
167 if bt_test_utils.is_a2dp_snk_device_connected(
168 self.SNK, self.SRC.droid.bluetoothGetLocalAddress()):
169 self.log.error("Failed to disconnect on A2dp Sink")
170 return False
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -0700171 # Logging if we connected right back, since that happens sometimes
172 # Not failing the test if it did though
tturneye3934a22016-10-20 15:47:34 -0700173 if car_media_utils.is_a2dp_connected(self.log, self.SNK, self.SRC):
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -0700174 self.log.error("Still connected after a disconnect")
Ram Periathiruvadie3f9a702016-07-26 17:41:59 -0700175 return True