1/* 2 * Copyright (C) 2015 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.tests.pbap; 18 19import com.android.bluetooth.pbap.BluetoothPbapObexServer; 20import com.android.bluetooth.pbap.BluetoothPbapVcardManager; 21import com.android.bluetooth.tests.mock.BluetoothMockContext; 22import com.android.bluetooth.tests.mock.SimpleMockContentProvider; 23 24import android.database.Cursor; 25import android.database.MatrixCursor; 26import android.provider.ContactsContract; 27import android.provider.ContactsContract.PhoneLookup; 28import android.test.AndroidTestCase; 29import android.test.mock.MockContentProvider; 30import android.test.mock.MockContentResolver; 31import android.provider.ContactsContract.CommonDataKinds.Phone; 32import android.text.TextUtils; 33 34 35import java.util.ArrayList; 36 37public class BluetoothPbapVcardManagerTest extends AndroidTestCase { 38 39 public void testGetContactNamesByNumberWithEmptyPhoneNumber() { 40 getContactNamesByNumberInternal(""); 41 } 42 43 public void testGetContactNamesByNumberWithPhoneNumber() { 44 getContactNamesByNumberInternal("111-111-111"); 45 } 46 47 private void getContactNamesByNumberInternal(String phoneNumber) { 48 String[] columnNames; 49 if (TextUtils.isEmpty(phoneNumber)) { 50 columnNames = new String[]{Phone.CONTACT_ID, Phone.DISPLAY_NAME}; 51 } else { 52 columnNames = new String[]{PhoneLookup._ID, PhoneLookup.DISPLAY_NAME}; 53 } 54 55 MatrixCursor mc = new MatrixCursor(columnNames); 56 mc.addRow(new Object[]{1L, "A"}); 57 mc.addRow(new Object[]{1L, "A (1)"}); 58 mc.addRow(new Object[]{2L, "B"}); 59 mc.addRow(new Object[]{2L, "B (1)"}); 60 mc.addRow(new Object[]{3L, "C"}); 61 mc.addRow(new Object[]{3L, "C (1)"}); 62 mc.addRow(new Object[]{3L, "C (2)"}); 63 mc.addRow(new Object[]{4L, "D"}); 64 BluetoothPbapVcardManager manager = createBluetoothPbapVcardManager(mc); 65 ArrayList<String> nameList = manager.getContactNamesByNumber(phoneNumber); 66 67 // If there are multiple display name per id, first one is picked. 68 assertEquals("A,1", nameList.get(0)); 69 assertEquals("B,2", nameList.get(1)); 70 assertEquals("C,3", nameList.get(2)); 71 assertEquals("D,4", nameList.get(3)); 72 } 73 74 public void testGetDistinctContactIdSize() { 75 MatrixCursor mc = new MatrixCursor(new String[]{ContactsContract.Data.CONTACT_ID}); 76 mc.addRow(new String[]{"1"}); 77 mc.addRow(new String[]{"1"}); 78 mc.addRow(new String[]{"2"}); 79 mc.addRow(new String[]{"2"}); 80 mc.addRow(new String[]{"3"}); 81 mc.addRow(new String[]{"3"}); 82 mc.addRow(new String[]{"3"}); 83 mc.addRow(new String[]{"4"}); 84 mc.addRow(new String[]{"5"}); 85 BluetoothPbapVcardManager manager = createBluetoothPbapVcardManager(mc); 86 int size = manager.getContactsSize(); 87 88 assertEquals(5 + 1, size); // +1 becoz of always has the 0.vcf 89 } 90 91 public void testGetPhonebookNameListOrderByIndex() { 92 MatrixCursor mc = new MatrixCursor( 93 new String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID, 94 ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME}); 95 // test name duplication. 96 mc.addRow(new Object[]{1L, "A"}); 97 mc.addRow(new Object[]{1L, "A (1)"}); 98 mc.addRow(new Object[]{2L, "B"}); 99 mc.addRow(new Object[]{2L, "B (1)"}); 100 mc.addRow(new Object[]{3L, "C"}); 101 mc.addRow(new Object[]{3L, "C (1)"}); 102 mc.addRow(new Object[]{3L, "C (2)"}); 103 mc.addRow(new Object[]{4L, "D"}); 104 // test default name. 105 mc.addRow(new Object[]{5L, null}); 106 BluetoothPbapVcardManager manager = createBluetoothPbapVcardManager(mc); 107 ArrayList<String> nameList = manager 108 .getPhonebookNameList(BluetoothPbapObexServer.ORDER_BY_INDEXED); 109 110 // Skip the first one which is supposed to be owner name. 111 assertEquals("A,1", nameList.get(1)); 112 assertEquals("B,2", nameList.get(2)); 113 assertEquals("C,3", nameList.get(3)); 114 assertEquals("D,4", nameList.get(4)); 115 assertEquals(getContext().getString(android.R.string.unknownName) + ",5", nameList.get(5)); 116 } 117 118 public void testGetPhonebookNameListOrderByAlphabetical() { 119 MatrixCursor mc = new MatrixCursor( 120 new String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID, 121 ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME}); 122 // test sorting order. 123 mc.addRow(new Object[]{1L, "D"}); 124 mc.addRow(new Object[]{1L, "D (1)"}); 125 mc.addRow(new Object[]{2L, "C"}); 126 mc.addRow(new Object[]{2L, "C (1)"}); 127 mc.addRow(new Object[]{3L, "A"}); 128 mc.addRow(new Object[]{3L, "A (1)"}); 129 mc.addRow(new Object[]{3L, "A (2)"}); 130 mc.addRow(new Object[]{4L, "B"}); 131 BluetoothPbapVcardManager manager = createBluetoothPbapVcardManager(mc); 132 ArrayList<String> nameList = manager 133 .getPhonebookNameList(BluetoothPbapObexServer.ORDER_BY_ALPHABETICAL); 134 135 // Skip the first one which is supposed to be owner name. 136 assertEquals("A,3", nameList.get(1)); 137 assertEquals("B,4", nameList.get(2)); 138 assertEquals("C,2", nameList.get(3)); 139 assertEquals("D,1", nameList.get(4)); 140 } 141 142 private BluetoothPbapVcardManager createBluetoothPbapVcardManager(Cursor result) { 143 MockContentProvider contentProvider = new SimpleMockContentProvider(result); 144 MockContentResolver contentResolver = new MockContentResolver(); 145 contentResolver.addProvider(ContactsContract.AUTHORITY, contentProvider); 146 BluetoothMockContext mockContext = new BluetoothMockContext(contentResolver, getContext()); 147 return new BluetoothPbapVcardManager(mockContext); 148 } 149} 150