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 17 package com.android.bluetooth.pbapclient; 18 19 import android.accounts.Account; 20 import android.content.res.Resources; 21 import android.database.Cursor; 22 import android.provider.CallLog.Calls; 23 import android.test.AndroidTestCase; 24 25 import java.io.IOException; 26 import java.io.InputStream; 27 28 import org.junit.Before; 29 import org.junit.Test; 30 31 public 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 // EST is default Time Zone 85 assertFalse(verifyCallLog("555-0002", "1483250460000", "3")); 86 // Finish processing the data and verify entries were added to the call log. 87 processor.onPullComplete(); 88 assertTrue(verifyCallLog("555-0002", "1483250460000", "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 // EST is default Time Zone 106 assertFalse(verifyCallLog("", "1483250520000", "3")); 107 assertFalse(verifyCallLog("", "1483250580000", "3")); 108 109 // Finish processing the data and verify entries were added to the call log. 110 processor.onPullComplete(); 111 assertTrue(verifyCallLog("", "1483250520000", "3")); 112 assertTrue(verifyCallLog("", "1483250580000", "3")); 113 } 114 115 // Find Entries in call log with type matching number and date. 116 // If number or date is null it will match any number or date respectively. 117 boolean verifyCallLog(String number, String date, String type) { 118 String[] query = new String[] {Calls.NUMBER, Calls.DATE, Calls.TYPE}; 119 Cursor cursor = mContext.getContentResolver().query(Calls.CONTENT_URI, query, 120 Calls.TYPE + "= " + type, null, Calls.DATE + ", " + Calls.NUMBER); 121 if (cursor != null) { 122 while (cursor.moveToNext()) { 123 String foundNumber = cursor.getString(cursor.getColumnIndex(Calls.NUMBER)); 124 String foundDate = cursor.getString(cursor.getColumnIndex(Calls.DATE)); 125 if ((number == null || number.equals(foundNumber)) 126 && (date == null || date.equals(foundDate))) { 127 return true; 128 } 129 } 130 cursor.close(); 131 } 132 return false; 133 } 134 } 135