blob: 5c128fd95782d63d4b11b779c96a8106d5428abb [file] [log] [blame]
Joseph Pirozzo66fb3f62016-05-31 16:57:39 -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"""Test script to test PBAP contact download between two devices which can run SL4A.
Joseph Pirozzo66fb3f62016-05-31 16:57:39 -070017"""
18
tturneye3934a22016-10-20 15:47:34 -070019import os
tturney46060782016-11-14 16:44:38 -080020import time
Joseph Pirozzo66fb3f62016-05-31 16:57:39 -070021
tturneye3934a22016-10-20 15:47:34 -070022from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
Joseph Pirozzo66fb3f62016-05-31 16:57:39 -070023from acts.base_test import BaseTestClass
Sanket Agarwalbd9689b2016-08-24 11:14:53 -070024from acts.test_utils.bt import bt_contacts_utils
Joseph Pirozzof304d362016-06-17 15:15:40 -070025from acts.test_utils.bt import bt_test_utils
Sanket Agarwalbd9689b2016-08-24 11:14:53 -070026from acts.test_utils.car import car_bt_utils
Joseph Pirozzo66fb3f62016-05-31 16:57:39 -070027from acts.utils import exe_cmd
Joseph Pirozzof304d362016-06-17 15:15:40 -070028import acts.test_utils.bt.BtEnum as BtEnum
Joseph Pirozzo66fb3f62016-05-31 16:57:39 -070029
Joseph Pirozzof304d362016-06-17 15:15:40 -070030# Offset call logs by 1 minute
31CALL_LOG_TIME_OFFSET_IN_MSEC = 60000
tturneye3934a22016-10-20 15:47:34 -070032PSE_CONTACTS_FILE = "psecontacts.vcf"
33PCE_CONTACTS_FILE = "pcecontacts.vcf"
Joseph Pirozzof304d362016-06-17 15:15:40 -070034
tturneye3934a22016-10-20 15:47:34 -070035
36class BtCarPbapTest(BluetoothBaseTest):
37 contacts_destination_path = ""
38
Joseph Pirozzo66fb3f62016-05-31 16:57:39 -070039 def __init__(self, controllers):
40 BaseTestClass.__init__(self, controllers)
41 self.pce = self.android_devices[0]
42 self.pse = self.android_devices[1]
tturneye3934a22016-10-20 15:47:34 -070043 self.contacts_destination_path = self.log_path + "/"
Joseph Pirozzo66fb3f62016-05-31 16:57:39 -070044
45 def setup_class(self):
tturneye3934a22016-10-20 15:47:34 -070046 if not super(BtCarPbapTest, self).setup_class():
47 return False
tturney1f7b7b52016-11-17 15:28:22 -080048 permissions_list = ["android.permission.READ_CONTACTS",
49 "android.permission.WRITE_CONTACTS",
50 "android.permission.READ_EXTERNAL_STORAGE"]
51 for permission in permissions_list:
52 self.pse.adb.shell(
53 "pm grant com.google.android.contacts {}".format(permission))
54 for permission in permissions_list:
55 self.pce.adb.shell(
56 "pm grant com.android.contacts {}".format(permission))
57
Joseph Pirozzo66fb3f62016-05-31 16:57:39 -070058
59 # Pair the devices.
60 # This call may block until some specified timeout in bt_test_utils.py.
tturney46060782016-11-14 16:44:38 -080061 # Grace time inbetween stack state changes
62
63 time.sleep(5)
64 if not bt_test_utils.pair_pri_to_sec(
65 self.pce, self.pse, attempts=4, auto_confirm=False):
Joseph Pirozzof304d362016-06-17 15:15:40 -070066 self.log.error("Failed to pair.")
Joseph Pirozzo66fb3f62016-05-31 16:57:39 -070067 return False
Joseph Pirozzo66fb3f62016-05-31 16:57:39 -070068
Sanket Agarwalbd9689b2016-08-24 11:14:53 -070069 # Disable the HFP and A2DP profiles. This will ensure only PBAP
70 # gets connected. Also, this will eliminate the auto-connect loop.
71 car_bt_utils.set_car_profile_priorities_off(self.pce, self.pse)
72
73 # Enable PBAP on PSE & PCE.
74 bt_test_utils.set_profile_priority(
75 self.pce, self.pse, [BtEnum.BluetoothProfile.PBAP_CLIENT],
76 BtEnum.BluetoothPriorityLevel.PRIORITY_ON)
77
Joseph Pirozzof304d362016-06-17 15:15:40 -070078 self.pse.droid.bluetoothChangeProfileAccessPermission(
79 self.pce.droid.bluetoothGetLocalAddress(),
80 BtEnum.BluetoothProfile.PBAP_SERVER.value,
81 BtEnum.BluetoothAccessLevel.ACCESS_ALLOWED.value)
82
Joseph Pirozzo66fb3f62016-05-31 16:57:39 -070083 return True
84
85 def setup_test(self):
tturneye3934a22016-10-20 15:47:34 -070086 if not super(BtCarPbapTest, self).setup_test():
87 return False
Joseph Pirozzof304d362016-06-17 15:15:40 -070088 self.pse.droid.callLogsEraseAll()
89 if not (bt_contacts_utils.erase_contacts(self.pse) and
90 bt_contacts_utils.erase_contacts(self.pce)):
Joseph Pirozzo66fb3f62016-05-31 16:57:39 -070091 return False
Joseph Pirozzof304d362016-06-17 15:15:40 -070092 # Allow all content providers to synchronize.
tturney46060782016-11-14 16:44:38 -080093 time.sleep(1)
Joseph Pirozzo66fb3f62016-05-31 16:57:39 -070094 return True
95
96 def teardown_test(self):
tturneye3934a22016-10-20 15:47:34 -070097 if not super(BtCarPbapTest, self).teardown_test():
98 return False
Joseph Pirozzof304d362016-06-17 15:15:40 -070099 self.pce.droid.bluetoothPbapClientDisconnect(
100 self.pse.droid.bluetoothGetLocalAddress())
101 bt_contacts_utils.erase_contacts(self.pse)
Joseph Pirozzo66fb3f62016-05-31 16:57:39 -0700102 return True
103
Joseph Pirozzof304d362016-06-17 15:15:40 -0700104 def verify_contacts_match(self):
tturneye3934a22016-10-20 15:47:34 -0700105 bt_contacts_utils.export_device_contacts_to_vcf(
106 self.pce, self.contacts_destination_path, PCE_CONTACTS_FILE)
Joseph Pirozzof304d362016-06-17 15:15:40 -0700107 return bt_contacts_utils.count_contacts_with_differences(
tturneye3934a22016-10-20 15:47:34 -0700108 self.contacts_destination_path, PCE_CONTACTS_FILE,
109 PSE_CONTACTS_FILE) == 0
Joseph Pirozzof304d362016-06-17 15:15:40 -0700110
111 def connect_and_verify(self, count):
112 bt_test_utils.connect_pri_to_sec(
tturneye3934a22016-10-20 15:47:34 -0700113 self.pce, self.pse,
Joseph Pirozzof304d362016-06-17 15:15:40 -0700114 set([BtEnum.BluetoothProfile.PBAP_CLIENT.value]))
115 bt_contacts_utils.wait_for_phone_number_update_complete(self.pce,
116 count)
117 contacts_added = self.verify_contacts_match()
118 self.pce.droid.bluetoothPbapClientDisconnect(
119 self.pse.droid.bluetoothGetLocalAddress())
120 contacts_removed = bt_contacts_utils.wait_for_phone_number_update_complete(
121 self.pce, 0)
122 return contacts_added and contacts_removed
123
Phillip Walker827112a2016-09-08 16:27:19 -0700124 #@BluetoothTest(UUID=7dcdecfc-42d1-4f41-b66e-823c8f161356)
tturneye3934a22016-10-20 15:47:34 -0700125 @BluetoothBaseTest.bt_test_wrap
Joseph Pirozzof304d362016-06-17 15:15:40 -0700126 def test_pbap_connect_and_disconnect(self):
127 """Test Connectivity
128
129 Test connecting with the server enabled and disabled
130
131 Precondition:
132 1. Devices are paired.
133
134 Steps:
135 1. Disable permission on PSE to prevent PCE from connecting
136 2. Attempt to connect PCE to PSE
137 3. Verify connection failed
138 4. Enable permission on PSE to allow PCE to connect
139 5. Attempt to connect PCE to PSE
140 6. Verify connection succeeded
141
142 Returns:
143 Pass if True
144 Fail if False
145 """
146 self.pse.droid.bluetoothChangeProfileAccessPermission(
147 self.pce.droid.bluetoothGetLocalAddress(),
148 BtEnum.BluetoothProfile.PBAP_SERVER.value,
149 BtEnum.BluetoothAccessLevel.ACCESS_DENIED.value)
150 if bt_test_utils.connect_pri_to_sec(
tturneye3934a22016-10-20 15:47:34 -0700151 self.pce, self.pse,
Joseph Pirozzof304d362016-06-17 15:15:40 -0700152 set([BtEnum.BluetoothProfile.PBAP_CLIENT.value])):
153 self.log.error("Client connected and shouldn't be.")
154 return False
Sanket Agarwalbd9689b2016-08-24 11:14:53 -0700155
Joseph Pirozzof304d362016-06-17 15:15:40 -0700156 self.pce.droid.bluetoothPbapClientDisconnect(
157 self.pse.droid.bluetoothGetLocalAddress())
158
159 self.pse.droid.bluetoothChangeProfileAccessPermission(
160 self.pce.droid.bluetoothGetLocalAddress(),
161 BtEnum.BluetoothProfile.PBAP_SERVER.value,
162 BtEnum.BluetoothAccessLevel.ACCESS_ALLOWED.value)
Sanket Agarwalbd9689b2016-08-24 11:14:53 -0700163
tturneye3934a22016-10-20 15:47:34 -0700164 if not bt_test_utils.connect_pri_to_sec(self.pce, self.pse, set(
165 [BtEnum.BluetoothProfile.PBAP_CLIENT.value])):
Joseph Pirozzof304d362016-06-17 15:15:40 -0700166 self.log.error("No client connected and should be.")
167 return False
168
169 return True
Joseph Pirozzo66fb3f62016-05-31 16:57:39 -0700170
Phillip Walker827112a2016-09-08 16:27:19 -0700171 #@BluetoothTest(UUID=1733efb9-71af-4956-bd3a-0d3167d94d0c)
tturneye3934a22016-10-20 15:47:34 -0700172 @BluetoothBaseTest.bt_test_wrap
Joseph Pirozzo66fb3f62016-05-31 16:57:39 -0700173 def test_contact_download(self):
174 """Test Contact Download
175
176 Test download of contacts from a clean state.
177
Joseph Pirozzof304d362016-06-17 15:15:40 -0700178 Precondition:
179 1. Devices are paired.
Joseph Pirozzo66fb3f62016-05-31 16:57:39 -0700180
Joseph Pirozzof304d362016-06-17 15:15:40 -0700181 Steps:
182 1. Erase contacts from PSE and PCE.
183 2. Add a predefined list of contacts to PSE.
184 3. Connect PCE to PSE to perform transfer.
185 4. Compare transfered contacts.
186 5. Disconnect.
187 6. Verify PCE cleaned up contact list.
188
189 Returns:
190 Pass if True
191 Fail if False
Joseph Pirozzo66fb3f62016-05-31 16:57:39 -0700192 """
tturneye3934a22016-10-20 15:47:34 -0700193 bt_contacts_utils.generate_contact_list(self.contacts_destination_path,
194 PSE_CONTACTS_FILE, 100)
Joseph Pirozzof304d362016-06-17 15:15:40 -0700195 phone_numbers_added = bt_contacts_utils.import_device_contacts_from_vcf(
tturneye3934a22016-10-20 15:47:34 -0700196 self.pse, self.contacts_destination_path, PSE_CONTACTS_FILE)
Joseph Pirozzof304d362016-06-17 15:15:40 -0700197 bt_test_utils.connect_pri_to_sec(
tturneye3934a22016-10-20 15:47:34 -0700198 self.pce, self.pse,
Joseph Pirozzof304d362016-06-17 15:15:40 -0700199 set([BtEnum.BluetoothProfile.PBAP_CLIENT.value]))
200 bt_contacts_utils.wait_for_phone_number_update_complete(
201 self.pce, phone_numbers_added)
202 if not self.verify_contacts_match():
203 return False
Phillip Walker827112a2016-09-08 16:27:19 -0700204 return bt_contacts_utils.erase_contacts(self.pce)
Joseph Pirozzof304d362016-06-17 15:15:40 -0700205
Phillip Walker827112a2016-09-08 16:27:19 -0700206 #@BluetoothTest(UUID=99dc6ac6-b7cf-45ce-927b-8c4ebf8ab664)
tturneye3934a22016-10-20 15:47:34 -0700207 @BluetoothBaseTest.bt_test_wrap
Joseph Pirozzof304d362016-06-17 15:15:40 -0700208 def test_modify_phonebook(self):
209 """Test Modify Phonebook
210
211 Test changing contacts and reconnecting PBAP.
212
213 Precondition:
214 1. Devices are paired.
215
216 Steps:
217 1. Add a predefined list of contacts to PSE.
218 2. Connect PCE to PSE to perform transfer.
219 3. Verify that contacts match.
220 4. Change some contacts on the PSE.
221 5. Reconnect PCE to PSE to perform transfer.
222 6. Verify that new contacts match.
223
224 Returns:
225 Pass if True
226 Fail if False
227 """
tturneye3934a22016-10-20 15:47:34 -0700228 bt_contacts_utils.generate_contact_list(self.contacts_destination_path,
229 PSE_CONTACTS_FILE, 100)
Joseph Pirozzof304d362016-06-17 15:15:40 -0700230 phone_numbers_added = bt_contacts_utils.import_device_contacts_from_vcf(
tturneye3934a22016-10-20 15:47:34 -0700231 self.pse, self.contacts_destination_path, PSE_CONTACTS_FILE)
Joseph Pirozzof304d362016-06-17 15:15:40 -0700232 if not self.connect_and_verify(phone_numbers_added):
233 return False
234
Joseph Pirozzo66fb3f62016-05-31 16:57:39 -0700235 bt_contacts_utils.erase_contacts(self.pse)
tturneye3934a22016-10-20 15:47:34 -0700236 bt_contacts_utils.generate_contact_list(self.contacts_destination_path,
237 PSE_CONTACTS_FILE, 110, 2)
Joseph Pirozzof304d362016-06-17 15:15:40 -0700238 phone_numbers_added = bt_contacts_utils.import_device_contacts_from_vcf(
tturneye3934a22016-10-20 15:47:34 -0700239 self.pse, self.contacts_destination_path, PSE_CONTACTS_FILE)
Joseph Pirozzof304d362016-06-17 15:15:40 -0700240 return self.connect_and_verify(phone_numbers_added)
241
Phillip Walker827112a2016-09-08 16:27:19 -0700242 #@BluetoothTest(UUID=bbe31bf5-51e8-4175-b266-1c7750e44f5b)
tturneye3934a22016-10-20 15:47:34 -0700243 @BluetoothBaseTest.bt_test_wrap
Joseph Pirozzof304d362016-06-17 15:15:40 -0700244 def test_special_contacts(self):
245 """Test Special Contacts
246
247 Test numerous special cases of contacts that could cause errors.
248
249 Precondition:
250 1. Devices are paired.
251
252 Steps:
253 1. Add a predefined list of contacts to PSE that includes special cases:
254 2. Connect PCE to PSE to perform transfer.
255 3. Verify that contacts match.
256
257 Returns:
258 Pass if True
259 Fail if False
260 """
261
262 vcards = []
263
264 # Generate a contact with no email address
265 current_contact = bt_contacts_utils.VCard()
266 current_contact.first_name = "Mr."
267 current_contact.last_name = "Smiley"
268 current_contact.add_phone_number(
269 bt_contacts_utils.generate_random_phone_number())
270 vcards.append(current_contact)
271
272 # Generate a 2nd contact with the same name but different phone number
273 current_contact = bt_contacts_utils.VCard()
274 current_contact.first_name = "Mr."
275 current_contact.last_name = "Smiley"
276 current_contact.add_phone_number(
277 bt_contacts_utils.generate_random_phone_number())
278 vcards.append(current_contact)
279
280 # Generate a contact with no name
281 current_contact = bt_contacts_utils.VCard()
282 current_contact.email = "{}@gmail.com".format(
283 bt_contacts_utils.generate_random_string())
284 current_contact.add_phone_number(
285 bt_contacts_utils.generate_random_phone_number())
286 vcards.append(current_contact)
287
288 # Generate a contact with random characters in its name
289 current_contact = bt_contacts_utils.VCard()
290 current_contact.first_name = bt_contacts_utils.generate_random_string()
291 current_contact.last_name = bt_contacts_utils.generate_random_string()
292 current_contact.add_phone_number(
293 bt_contacts_utils.generate_random_phone_number())
294 vcards.append(current_contact)
295
296 # Generate a contact with only a phone number
297 current_contact = bt_contacts_utils.VCard()
298 current_contact.add_phone_number(
299 bt_contacts_utils.generate_random_phone_number())
300 vcards.append(current_contact)
301
302 # Generate a 2nd contact with only a phone number
303 current_contact = bt_contacts_utils.VCard()
304 current_contact.add_phone_number(
305 bt_contacts_utils.generate_random_phone_number())
306 vcards.append(current_contact)
307
308 bt_contacts_utils.create_new_contacts_vcf_from_vcards(
tturneye3934a22016-10-20 15:47:34 -0700309 self.contacts_destination_path, PSE_CONTACTS_FILE, vcards)
Joseph Pirozzof304d362016-06-17 15:15:40 -0700310
311 phone_numbers_added = bt_contacts_utils.import_device_contacts_from_vcf(
tturneye3934a22016-10-20 15:47:34 -0700312 self.pse, self.contacts_destination_path, PSE_CONTACTS_FILE)
Joseph Pirozzof304d362016-06-17 15:15:40 -0700313
314 return self.connect_and_verify(phone_numbers_added)
315
Phillip Walker827112a2016-09-08 16:27:19 -0700316 #@BluetoothTest(UUID=2aa2bd00-86cc-4f39-a06a-90b17ea5b320)
tturneye3934a22016-10-20 15:47:34 -0700317 @BluetoothBaseTest.bt_test_wrap
Joseph Pirozzof304d362016-06-17 15:15:40 -0700318 def test_call_log(self):
319 """Test Call Log
320
321 Test that Call Logs are transfered
322
323 Precondition:
324 1. Devices are paired.
325
326 Steps:
327 1. Add a predefined list of calls to the PSE call log.
328 2. Connect PCE to PSE to allow call log transfer
329 3. Verify the Missed, Incoming, and Outgoing Call History
330
331 Returns:
332 Pass if True
333 Fail if False
334 """
335
336 bt_contacts_utils.add_call_log(
337 self.pse, bt_contacts_utils.INCOMMING_CALL_TYPE,
338 bt_contacts_utils.generate_random_phone_number().phone_number,
tturney46060782016-11-14 16:44:38 -0800339 int(time.time() * 1000))
Joseph Pirozzof304d362016-06-17 15:15:40 -0700340 bt_contacts_utils.add_call_log(
341 self.pse, bt_contacts_utils.INCOMMING_CALL_TYPE,
342 bt_contacts_utils.generate_random_phone_number().phone_number,
tturney46060782016-11-14 16:44:38 -0800343 int(time.time()) * 1000 - 4 * CALL_LOG_TIME_OFFSET_IN_MSEC)
Joseph Pirozzof304d362016-06-17 15:15:40 -0700344 bt_contacts_utils.add_call_log(
345 self.pse, bt_contacts_utils.OUTGOING_CALL_TYPE,
346 bt_contacts_utils.generate_random_phone_number().phone_number,
tturney46060782016-11-14 16:44:38 -0800347 int(time.time()) * 1000 - CALL_LOG_TIME_OFFSET_IN_MSEC)
Joseph Pirozzof304d362016-06-17 15:15:40 -0700348 bt_contacts_utils.add_call_log(
349 self.pse, bt_contacts_utils.MISSED_CALL_TYPE,
350 bt_contacts_utils.generate_random_phone_number().phone_number,
tturney46060782016-11-14 16:44:38 -0800351 int(time.time()) * 1000 - 2 * CALL_LOG_TIME_OFFSET_IN_MSEC)
Joseph Pirozzof304d362016-06-17 15:15:40 -0700352 bt_contacts_utils.add_call_log(
353 self.pse, bt_contacts_utils.MISSED_CALL_TYPE,
354 bt_contacts_utils.generate_random_phone_number().phone_number,
tturney46060782016-11-14 16:44:38 -0800355 int(time.time()) * 1000 - 2 * CALL_LOG_TIME_OFFSET_IN_MSEC)
Joseph Pirozzof304d362016-06-17 15:15:40 -0700356
357 bt_test_utils.connect_pri_to_sec(
tturneye3934a22016-10-20 15:47:34 -0700358 self.pce, self.pse,
Joseph Pirozzof304d362016-06-17 15:15:40 -0700359 set([BtEnum.BluetoothProfile.PBAP_CLIENT.value]))
360 pse_call_log_count = self.pse.droid.callLogGetCount()
361 self.log.info("Waiting for {} call logs to be transfered".format(
362 pse_call_log_count))
363 bt_contacts_utils.wait_for_call_log_update_complete(self.pce,
364 pse_call_log_count)
365
366 if not bt_contacts_utils.get_and_compare_call_logs(
367 self.pse, self.pce, bt_contacts_utils.INCOMMING_CALL_TYPE):
368 return False
369 if not bt_contacts_utils.get_and_compare_call_logs(
370 self.pse, self.pce, bt_contacts_utils.OUTGOING_CALL_TYPE):
371 return False
372 if not bt_contacts_utils.get_and_compare_call_logs(
373 self.pse, self.pce, bt_contacts_utils.MISSED_CALL_TYPE):
374 return False
375
376 return True