1/* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.bluetooth.pbapclient; 18 19import android.accounts.Account; 20import android.content.res.Resources; 21import android.database.Cursor; 22import android.provider.CallLog.Calls; 23import android.test.AndroidTestCase; 24 25import java.io.IOException; 26import java.io.InputStream; 27 28import org.junit.Before; 29import org.junit.Test; 30 31public class PbapParserTest extends AndroidTestCase { 32 private Account mAccount; 33 private Resources testResources; 34 private static final String mTestAccountName = "PBAPTESTACCOUNT"; 35 private static final String mTestPackageName = "com.android.bluetooth.tests"; 36 37 @Before 38 public void setUp() { 39 mAccount = new Account(mTestAccountName, 40 mContext.getString(com.android.bluetooth.R.string.pbap_account_type)); 41 try { 42 testResources = 43 mContext.getPackageManager().getResourcesForApplication(mTestPackageName); 44 } catch (Exception e) { 45 fail("Setup Failure Unable to get resources" + e.toString()); 46 } 47 } 48 49 // testNoTimestamp should parse 1 poorly formed vcard and not crash. 50 @Test 51 public void testNoTimestamp() throws IOException { 52 InputStream fileStream; 53 fileStream = testResources.openRawResource( 54 com.android.bluetooth.tests.R.raw.no_timestamp_call_log); 55 BluetoothPbapVcardList pbapVCardList = new BluetoothPbapVcardList( 56 mAccount, fileStream, PbapClientConnectionHandler.VCARD_TYPE_30); 57 assertEquals(1, pbapVCardList.getCount()); 58 CallLogPullRequest processor = 59 new CallLogPullRequest(mContext, PbapClientConnectionHandler.MCH_PATH); 60 processor.setResults(pbapVCardList.getList()); 61 62 // Verify that these entries aren't in the call log to start. 63 assertFalse(verifyCallLog("555-0001", null, "3")); 64 65 // Finish processing the data and verify entries were added to the call log. 66 processor.onPullComplete(); 67 assertTrue(verifyCallLog("555-0001", null, "3")); 68 } 69 70 // testMissedCall should parse one phonecall correctly. 71 @Test 72 public void testMissedCall() throws IOException { 73 InputStream fileStream; 74 fileStream = 75 testResources.openRawResource(com.android.bluetooth.tests.R.raw.single_missed_call); 76 BluetoothPbapVcardList pbapVCardList = new BluetoothPbapVcardList( 77 mAccount, fileStream, PbapClientConnectionHandler.VCARD_TYPE_30); 78 assertEquals(1, pbapVCardList.getCount()); 79 CallLogPullRequest processor = 80 new CallLogPullRequest(mContext, PbapClientConnectionHandler.MCH_PATH); 81 processor.setResults(pbapVCardList.getList()); 82 83 // Verify that these entries aren't in the call log to start. 84 assertFalse(verifyCallLog("555-0002", "1483232460000", "3")); 85 86 // Finish processing the data and verify entries were added to the call log. 87 processor.onPullComplete(); 88 assertTrue(verifyCallLog("555-0002", "1483232460000", "3")); 89 } 90 91 // testUnknownCall should parse two calls with no phone number. 92 @Test 93 public void testUnknownCall() throws IOException { 94 InputStream fileStream; 95 fileStream = testResources.openRawResource( 96 com.android.bluetooth.tests.R.raw.unknown_number_call); 97 BluetoothPbapVcardList pbapVCardList = new BluetoothPbapVcardList( 98 mAccount, fileStream, PbapClientConnectionHandler.VCARD_TYPE_30); 99 assertEquals(2, pbapVCardList.getCount()); 100 CallLogPullRequest processor = 101 new CallLogPullRequest(mContext, PbapClientConnectionHandler.MCH_PATH); 102 processor.setResults(pbapVCardList.getList()); 103 104 // Verify that these entries aren't in the call log to start. 105 assertFalse(verifyCallLog("", "1483232520000", "3")); 106 assertFalse(verifyCallLog("", "1483232580000", "3")); 107 108 // Finish processing the data and verify entries were added to the call log. 109 processor.onPullComplete(); 110 assertTrue(verifyCallLog("", "1483232520000", "3")); 111 assertTrue(verifyCallLog("", "1483232580000", "3")); 112 } 113 114 // Find Entries in call log with type matching number and date. 115 // If number or date is null it will match any number or date respectively. 116 boolean verifyCallLog(String number, String date, String type) { 117 String[] query = new String[] {Calls.NUMBER, Calls.DATE, Calls.TYPE}; 118 Cursor cursor = mContext.getContentResolver().query(Calls.CONTENT_URI, query, 119 Calls.TYPE + "= " + type, null, Calls.DATE + ", " + Calls.NUMBER); 120 if (cursor != null) { 121 while (cursor.moveToNext()) { 122 String foundNumber = cursor.getString(cursor.getColumnIndex(Calls.NUMBER)); 123 String foundDate = cursor.getString(cursor.getColumnIndex(Calls.DATE)); 124 if ((number == null || number.equals(foundNumber)) 125 && (date == null || date.equals(foundDate))) { 126 return true; 127 } 128 } 129 cursor.close(); 130 } 131 return false; 132 } 133} 134