summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorTony Mak <tonymak@google.com>2015-06-01 16:09:10 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2015-06-01 16:09:11 +0000
commit686377aa851b5cb6d5c32234dd340089b7959b49 (patch)
tree55f3ee784163d28b2a06ce723f2a0db911c3ae6c /tests
parent0f040c63cc9934918cd5bd6ac2fcb7644639a0c2 (diff)
parent0ff315d60bb44c5cc4223abff48e086defaec8ad (diff)
downloadandroid_packages_apps_Bluetooth-686377aa851b5cb6d5c32234dd340089b7959b49.tar.gz
android_packages_apps_Bluetooth-686377aa851b5cb6d5c32234dd340089b7959b49.tar.bz2
android_packages_apps_Bluetooth-686377aa851b5cb6d5c32234dd340089b7959b49.zip
Merge "Add unit test for commit "Support work contacts in HFP, PBAP, MAP"" into mnc-dev
Diffstat (limited to 'tests')
-rw-r--r--tests/src/com/android/bluetooth/tests/mock/BluetoothMockContext.java51
-rw-r--r--tests/src/com/android/bluetooth/tests/mock/SimpleMockContentProvider.java38
-rw-r--r--tests/src/com/android/bluetooth/tests/pbap/BluetoothPhabVcardManagerTest.java132
-rw-r--r--tests/src/com/android/bluetooth/tests/pbap/ContactCursorFilterTest.java161
4 files changed, 382 insertions, 0 deletions
diff --git a/tests/src/com/android/bluetooth/tests/mock/BluetoothMockContext.java b/tests/src/com/android/bluetooth/tests/mock/BluetoothMockContext.java
new file mode 100644
index 000000000..43f3d46dd
--- /dev/null
+++ b/tests/src/com/android/bluetooth/tests/mock/BluetoothMockContext.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.bluetooth.tests.mock;
+
+
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.res.Resources;
+import android.test.mock.MockContext;
+
+/**
+ * A context that return our {@link android.test.mock.MockContentResolver}.
+ */
+public class BluetoothMockContext extends MockContext {
+ private ContentResolver mMockContentResolver;
+ private Context mOriginalContext;
+
+ public BluetoothMockContext(ContentResolver mockContentResolver, Context originalContext) {
+ mMockContentResolver = mockContentResolver;
+ mOriginalContext = originalContext;
+ }
+
+ @Override
+ public ContentResolver getContentResolver() {
+ return mMockContentResolver;
+ }
+
+ @Override
+ public Resources getResources() {
+ return mOriginalContext.getResources();
+ }
+
+ @Override
+ public Object getSystemService(String name) {
+ return mOriginalContext.getSystemService(name);
+ }
+} \ No newline at end of file
diff --git a/tests/src/com/android/bluetooth/tests/mock/SimpleMockContentProvider.java b/tests/src/com/android/bluetooth/tests/mock/SimpleMockContentProvider.java
new file mode 100644
index 000000000..0c35021c2
--- /dev/null
+++ b/tests/src/com/android/bluetooth/tests/mock/SimpleMockContentProvider.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.bluetooth.tests.mock;
+
+
+import android.database.Cursor;
+import android.net.Uri;
+import android.test.mock.MockContentProvider;
+
+/**
+ * A provider that always return the result you want it to return
+ */
+public class SimpleMockContentProvider extends MockContentProvider {
+ private Cursor mResult;
+
+ public SimpleMockContentProvider(Cursor result) {
+ mResult = result;
+ }
+
+ @Override
+ public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
+ return mResult;
+ }
+} \ No newline at end of file
diff --git a/tests/src/com/android/bluetooth/tests/pbap/BluetoothPhabVcardManagerTest.java b/tests/src/com/android/bluetooth/tests/pbap/BluetoothPhabVcardManagerTest.java
new file mode 100644
index 000000000..72168b503
--- /dev/null
+++ b/tests/src/com/android/bluetooth/tests/pbap/BluetoothPhabVcardManagerTest.java
@@ -0,0 +1,132 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.bluetooth.tests.pbap;
+
+import android.database.Cursor;
+import android.database.MatrixCursor;
+import android.provider.ContactsContract;
+import android.test.AndroidTestCase;
+import android.test.mock.MockContentProvider;
+import android.test.mock.MockContentResolver;
+
+import com.android.bluetooth.pbap.BluetoothPbapObexServer;
+import com.android.bluetooth.pbap.BluetoothPbapVcardManager;
+import com.android.bluetooth.tests.mock.BluetoothMockContext;
+import com.android.bluetooth.tests.mock.SimpleMockContentProvider;
+
+import java.util.ArrayList;
+
+public class BluetoothPhabVcardManagerTest extends AndroidTestCase {
+
+ public void testGetContactNamesByNumber() {
+ MatrixCursor mc = new MatrixCursor(
+ new String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
+ ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME});
+ mc.addRow(new Object[]{1L, "A"});
+ mc.addRow(new Object[]{1L, "A (1)"});
+ mc.addRow(new Object[]{2L, "B"});
+ mc.addRow(new Object[]{2L, "B (1)"});
+ mc.addRow(new Object[]{3L, "C"});
+ mc.addRow(new Object[]{3L, "C (1)"});
+ mc.addRow(new Object[]{3L, "C (2)"});
+ mc.addRow(new Object[]{4L, "D"});
+ BluetoothPbapVcardManager manager = createBluetoothPbapVcardManager(mc);
+ ArrayList<String> nameList = manager.getContactNamesByNumber("111-111-111");
+
+ // If there are multiple display name per id, first one is picked.
+ assertEquals("A,1", nameList.get(0));
+ assertEquals("B,2", nameList.get(1));
+ assertEquals("C,3", nameList.get(2));
+ assertEquals("D,4", nameList.get(3));
+ }
+
+ public void testGetDistinctContactIdSize() {
+ MatrixCursor mc = new MatrixCursor(new String[]{ContactsContract.Data.CONTACT_ID});
+ mc.addRow(new String[]{"1"});
+ mc.addRow(new String[]{"1"});
+ mc.addRow(new String[]{"2"});
+ mc.addRow(new String[]{"2"});
+ mc.addRow(new String[]{"3"});
+ mc.addRow(new String[]{"3"});
+ mc.addRow(new String[]{"3"});
+ mc.addRow(new String[]{"4"});
+ mc.addRow(new String[]{"5"});
+ BluetoothPbapVcardManager manager = createBluetoothPbapVcardManager(mc);
+ int size = manager.getContactsSize();
+
+ assertEquals(5 + 1, size); // +1 becoz of always has the 0.vcf
+ }
+
+ public void testGetPhonebookNameListOrderByIndex() {
+ MatrixCursor mc = new MatrixCursor(
+ new String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
+ ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME});
+ // test name duplication.
+ mc.addRow(new Object[]{1L, "A"});
+ mc.addRow(new Object[]{1L, "A (1)"});
+ mc.addRow(new Object[]{2L, "B"});
+ mc.addRow(new Object[]{2L, "B (1)"});
+ mc.addRow(new Object[]{3L, "C"});
+ mc.addRow(new Object[]{3L, "C (1)"});
+ mc.addRow(new Object[]{3L, "C (2)"});
+ mc.addRow(new Object[]{4L, "D"});
+ // test default name.
+ mc.addRow(new Object[]{5L, null});
+ BluetoothPbapVcardManager manager = createBluetoothPbapVcardManager(mc);
+ ArrayList<String> nameList = manager
+ .getPhonebookNameList(BluetoothPbapObexServer.ORDER_BY_INDEXED);
+
+ // Skip the first one which is supposed to be owner name.
+ assertEquals("A,1", nameList.get(1));
+ assertEquals("B,2", nameList.get(2));
+ assertEquals("C,3", nameList.get(3));
+ assertEquals("D,4", nameList.get(4));
+ assertEquals(getContext().getString(android.R.string.unknownName) + ",5", nameList.get(5));
+ }
+
+ public void testGetPhonebookNameListOrderByAlphabetical() {
+ MatrixCursor mc = new MatrixCursor(
+ new String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
+ ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME});
+ // test sorting order.
+ mc.addRow(new Object[]{1L, "D"});
+ mc.addRow(new Object[]{1L, "D (1)"});
+ mc.addRow(new Object[]{2L, "C"});
+ mc.addRow(new Object[]{2L, "C (1)"});
+ mc.addRow(new Object[]{3L, "A"});
+ mc.addRow(new Object[]{3L, "A (1)"});
+ mc.addRow(new Object[]{3L, "A (2)"});
+ mc.addRow(new Object[]{4L, "B"});
+ BluetoothPbapVcardManager manager = createBluetoothPbapVcardManager(mc);
+ ArrayList<String> nameList = manager
+ .getPhonebookNameList(BluetoothPbapObexServer.ORDER_BY_ALPHABETICAL);
+
+ // Skip the first one which is supposed to be owner name.
+ assertEquals("A,3", nameList.get(1));
+ assertEquals("B,4", nameList.get(2));
+ assertEquals("C,2", nameList.get(3));
+ assertEquals("D,1", nameList.get(4));
+ }
+
+ private BluetoothPbapVcardManager createBluetoothPbapVcardManager(Cursor result) {
+ MockContentProvider contentProvider = new SimpleMockContentProvider(result);
+ MockContentResolver contentResolver = new MockContentResolver();
+ contentResolver.addProvider(ContactsContract.AUTHORITY, contentProvider);
+ BluetoothMockContext mockContext = new BluetoothMockContext(contentResolver, getContext());
+ return new BluetoothPbapVcardManager(mockContext);
+ }
+}
diff --git a/tests/src/com/android/bluetooth/tests/pbap/ContactCursorFilterTest.java b/tests/src/com/android/bluetooth/tests/pbap/ContactCursorFilterTest.java
new file mode 100644
index 000000000..66908c942
--- /dev/null
+++ b/tests/src/com/android/bluetooth/tests/pbap/ContactCursorFilterTest.java
@@ -0,0 +1,161 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.bluetooth.tests.pbap;
+
+import android.database.Cursor;
+import android.database.MatrixCursor;
+import android.provider.ContactsContract;
+import android.test.AndroidTestCase;
+
+import com.android.bluetooth.pbap.BluetoothPbapVcardManager;
+
+public class ContactCursorFilterTest extends AndroidTestCase {
+
+ public void testFilterByRangeWithoutDup() {
+ MatrixCursor mc = new MatrixCursor(new String[]{
+ ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
+ ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME});
+ mc.addRow(new Object[]{1L, "Test"});
+ mc.addRow(new Object[]{2L, "Test"});
+ mc.addRow(new Object[]{3L, "Test"});
+
+ Cursor cursor = BluetoothPbapVcardManager.ContactCursorFilter.filterByRange(mc, 1, 2);
+ assertEquals(2, cursor.getCount());
+ assertEquals(1L, getContactsIdFromCursor(cursor, 0));
+ assertEquals(2L, getContactsIdFromCursor(cursor, 1));
+ cursor.close();
+
+ mc.moveToPosition(-1);
+ cursor = BluetoothPbapVcardManager.ContactCursorFilter.filterByRange(mc, 1, 3);
+ assertEquals(3, cursor.getCount());
+ assertEquals(1L, getContactsIdFromCursor(cursor, 0));
+ assertEquals(2L, getContactsIdFromCursor(cursor, 1));
+ assertEquals(3L, getContactsIdFromCursor(cursor, 2));
+ cursor.close();
+
+ mc.moveToPosition(-1);
+ cursor = BluetoothPbapVcardManager.ContactCursorFilter.filterByRange(mc, 2, 3);
+ assertEquals(2, cursor.getCount());
+ assertEquals(2L, getContactsIdFromCursor(cursor, 0));
+ assertEquals(3L, getContactsIdFromCursor(cursor, 1));
+ cursor.close();
+
+ mc.moveToPosition(-1);
+ cursor = BluetoothPbapVcardManager.ContactCursorFilter.filterByRange(mc, 3, 3);
+ assertEquals(1, cursor.getCount());
+ assertEquals(3L, getContactsIdFromCursor(cursor, 0));
+ cursor.close();
+ }
+
+
+ public void testFilterByRangeWithDup() {
+ MatrixCursor mc = new MatrixCursor(new String[]{ContactsContract.CommonDataKinds.Phone
+ .CONTACT_ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME});
+ mc.addRow(new Object[]{1L, "Test"});
+ mc.addRow(new Object[]{1L, "Test"});
+ mc.addRow(new Object[]{2L, "Test"});
+ mc.addRow(new Object[]{2L, "Test"});
+ mc.addRow(new Object[]{3L, "Test"});
+
+ Cursor cursor = BluetoothPbapVcardManager.ContactCursorFilter.filterByRange(mc, 1, 2);
+ assertEquals(2, cursor.getCount());
+ assertEquals(1L, getContactsIdFromCursor(cursor, 0));
+ assertEquals(2L, getContactsIdFromCursor(cursor, 1));
+ cursor.close();
+
+ mc.moveToPosition(-1);
+ cursor = BluetoothPbapVcardManager.ContactCursorFilter.filterByRange(mc, 1, 3);
+ assertEquals(3, cursor.getCount());
+ assertEquals(1L, getContactsIdFromCursor(cursor, 0));
+ assertEquals(2L, getContactsIdFromCursor(cursor, 1));
+ assertEquals(3L, getContactsIdFromCursor(cursor, 2));
+ cursor.close();
+
+ mc.moveToPosition(-1);
+ cursor = BluetoothPbapVcardManager.ContactCursorFilter.filterByRange(mc, 2, 3);
+ assertEquals(2, cursor.getCount());
+ assertEquals(2L, getContactsIdFromCursor(cursor, 0));
+ assertEquals(3L, getContactsIdFromCursor(cursor, 1));
+ cursor.close();
+
+ mc.moveToPosition(-1);
+ cursor = BluetoothPbapVcardManager.ContactCursorFilter.filterByRange(mc, 3, 3);
+ assertEquals(1, cursor.getCount());
+ assertEquals(3L, getContactsIdFromCursor(cursor, 0));
+ cursor.close();
+ }
+
+ public void testFilterByOffsetWithoutDup() {
+ MatrixCursor mc = new MatrixCursor(new String[]{ContactsContract.CommonDataKinds.Phone
+ .CONTACT_ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME});
+ mc.addRow(new Object[]{1L, "Test"});
+ mc.addRow(new Object[]{2L, "Test"});
+ mc.addRow(new Object[]{3L, "Test"});
+
+ Cursor cursor = BluetoothPbapVcardManager.ContactCursorFilter.filterByOffset(mc, 1);
+ assertEquals(1, cursor.getCount());
+ assertEquals(1L, getContactsIdFromCursor(cursor, 0));
+ cursor.close();
+
+ mc.moveToPosition(-1);
+ cursor = BluetoothPbapVcardManager.ContactCursorFilter.filterByOffset(mc, 2);
+ assertEquals(1, cursor.getCount());
+ assertEquals(2L, getContactsIdFromCursor(cursor, 0));
+ cursor.close();
+
+ mc.moveToPosition(-1);
+ cursor = BluetoothPbapVcardManager.ContactCursorFilter.filterByOffset(mc, 3);
+ assertEquals(1, cursor.getCount());
+ assertEquals(3L, getContactsIdFromCursor(cursor, 0));
+ cursor.close();
+ }
+
+ public void testFilterByOffsetWithDup() {
+ MatrixCursor mc = new MatrixCursor(new String[]{ContactsContract.CommonDataKinds.Phone
+ .CONTACT_ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME});
+ mc.addRow(new Object[]{1L, "Test"});
+ mc.addRow(new Object[]{1L, "Test"});
+ mc.addRow(new Object[]{2L, "Test"});
+ mc.addRow(new Object[]{2L, "Test"});
+ mc.addRow(new Object[]{3L, "Test"});
+
+ Cursor cursor = BluetoothPbapVcardManager.ContactCursorFilter.filterByOffset(mc, 1);
+ assertEquals(1, cursor.getCount());
+ assertEquals(1L, getContactsIdFromCursor(cursor, 0));
+ cursor.close();
+
+ mc.moveToPosition(-1);
+ cursor = BluetoothPbapVcardManager.ContactCursorFilter.filterByOffset(mc, 2);
+ assertEquals(1, cursor.getCount());
+ assertEquals(2L, getContactsIdFromCursor(cursor, 0));
+ cursor.close();
+
+ mc.moveToPosition(-1);
+ cursor = BluetoothPbapVcardManager.ContactCursorFilter.filterByOffset(mc, 3);
+ assertEquals(1, cursor.getCount());
+ assertEquals(3L, getContactsIdFromCursor(cursor, 0));
+ cursor.close();
+ mc.moveToFirst();
+
+ }
+
+ private long getContactsIdFromCursor(Cursor cursor, int offset) {
+ int index = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
+ cursor.moveToPosition(offset);
+ return cursor.getLong(index);
+ }
+}