RecipientAlternatesAdapterTest.java revision 87b683e238bbf8f64237fd4cf20950e8e84fd763
1/* 2 * Copyright (C) 2012 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.ex.chips; 18 19import android.database.Cursor; 20import android.database.MatrixCursor; 21import android.net.Uri; 22import android.provider.ContactsContract.DisplayNameSources; 23import android.test.AndroidTestCase; 24 25import com.android.ex.chips.RecipientAlternatesAdapter; 26import com.android.ex.chips.RecipientEntry; 27 28public class RecipientAlternatesAdapterTest extends AndroidTestCase { 29 30 public void testRemoveDuplicateDestinations() { 31 MatrixCursor c = new MatrixCursor(Queries.EMAIL.getProjection()); 32 Cursor result; 33 34 // Test: Empty input 35 assertEquals(0, RecipientAlternatesAdapter.removeDuplicateDestinations(c).getCount()); 36 37 38 // Test: One row 39 addRow(c, "a", "1@android.com", 1, "home", 1000, 2000, "x", 0); 40 41 result = RecipientAlternatesAdapter.removeDuplicateDestinations(c); 42 assertEquals(1, result.getCount()); 43 assertRow(result, 0, "a", "1@android.com", 1, "home", 1000, 2000, "x", 0); 44 45 // Test: two unique rows, different destinations 46 addRow(c, "a", "2@android.com", 1, "home", 1000, 2000, "x", 0); 47 48 result = RecipientAlternatesAdapter.removeDuplicateDestinations(c); 49 assertEquals(2, result.getCount()); 50 assertRow(result, 0, "a", "1@android.com", 1, "home", 1000, 2000, "x", 0); 51 assertRow(result, 1, "a", "2@android.com", 1, "home", 1000, 2000, "x", 0); 52 53 // Test: add a third row with a non-unique destination. 54 addRow(c, "ax", "1@android.com", 11, "homex", 10001, 2000, "xx", 1); 55 56 // Third row should be removed. 57 result = RecipientAlternatesAdapter.removeDuplicateDestinations(c); 58 assertEquals(2, result.getCount()); 59 assertRow(result, 0, "a", "1@android.com", 1, "home", 1000, 2000, "x", 0); 60 assertRow(result, 1, "a", "2@android.com", 1, "home", 1000, 2000, "x", 0); 61 62 // Test: add a forth row with a non-unique destination again. 63 addRow(c, "ax", "2@android.com", 11, "homex", 10001, 2000, "xx", 1); 64 65 // Forth row should also be removed. 66 result = RecipientAlternatesAdapter.removeDuplicateDestinations(c); 67 assertEquals(2, result.getCount()); 68 assertRow(result, 0, "a", "1@android.com", 1, "home", 1000, 2000, "x", 0); 69 assertRow(result, 1, "a", "2@android.com", 1, "home", 1000, 2000, "x", 0); 70 } 71 72 private static MatrixCursor addRow(MatrixCursor c, 73 String displayName, 74 String destination, 75 int destinationType, 76 String destinationLabel, 77 long contactId, 78 long dataId, 79 String photoUri, 80 int displayNameSource 81 ) { 82 c.addRow(new Object[] {displayName, destination, destinationType, destinationLabel, 83 contactId, dataId, photoUri, displayNameSource}); 84 return c; 85 } 86 87 private static void assertRow(Cursor c, int position, 88 String displayName, 89 String destination, 90 int destinationType, 91 String destinationLabel, 92 long contactId, 93 long dataId, 94 String photoUri, 95 int displayNameSource 96 ) { 97 assertTrue(c.moveToPosition(position)); 98 assertEquals(displayName, c.getString(0)); 99 assertEquals(destination, c.getString(1)); 100 assertEquals(destinationType, c.getInt(2)); 101 assertEquals(destinationLabel, c.getString(3)); 102 assertEquals(contactId, c.getLong(4)); 103 assertEquals(dataId, c.getLong(5)); 104 assertEquals(photoUri, c.getString(6)); 105 assertEquals(displayNameSource, c.getInt(7)); 106 } 107 108 public void testGetBetterRecipient() { 109 // Ensure that if either (but not both) parameters are null, the other is returned 110 { 111 final RecipientEntry entry1 = 112 RecipientEntry.constructFakeEntry("1@android.com", true); 113 final RecipientEntry entry2 = null; 114 115 assertEquals(RecipientAlternatesAdapter.getBetterRecipient(entry1, entry2), entry1); 116 assertEquals(RecipientAlternatesAdapter.getBetterRecipient(entry2, entry1), entry1); 117 } 118 119 // Ensure that if only one has a display name, it is used 120 { 121 final RecipientEntry entry1 = 122 RecipientEntry.constructTopLevelEntry("Android", DisplayNameSources.NICKNAME, 123 "1@android.com", 0, null, 0, 0, (Uri) null, true); 124 final RecipientEntry entry2 = RecipientEntry.constructFakeEntry("1@android.com", true); 125 126 assertEquals(RecipientAlternatesAdapter.getBetterRecipient(entry1, entry2), entry1); 127 assertEquals(RecipientAlternatesAdapter.getBetterRecipient(entry2, entry1), entry1); 128 } 129 130 // Ensure that if one has a display name different from its destination, and the other's 131 // is equal to its destination, we use the unique one 132 { 133 final RecipientEntry entry1 = 134 RecipientEntry.constructTopLevelEntry("Android", DisplayNameSources.NICKNAME, 135 "1@android.com", 0, null, 0, 0, (Uri) null, true); 136 final RecipientEntry entry2 = 137 RecipientEntry.constructTopLevelEntry("2@android.com", DisplayNameSources.EMAIL, 138 "2@android.com", 0, null, 0, 0, (Uri) null, true); 139 140 assertEquals(RecipientAlternatesAdapter.getBetterRecipient(entry1, entry2), entry1); 141 assertEquals(RecipientAlternatesAdapter.getBetterRecipient(entry2, entry1), entry1); 142 } 143 144 // Ensure that if only one has a photo, it is used 145 { 146 final RecipientEntry entry1 = 147 RecipientEntry.constructTopLevelEntry("Android", DisplayNameSources.NICKNAME, 148 "1@android.com", 0, null, 0, 0, Uri.parse("http://www.android.com"), 149 true); 150 final RecipientEntry entry2 = 151 RecipientEntry.constructTopLevelEntry("Android", DisplayNameSources.EMAIL, 152 "2@android.com", 0, null, 0, 0, (Uri) null, true); 153 154 assertEquals(RecipientAlternatesAdapter.getBetterRecipient(entry1, entry2), entry1); 155 assertEquals(RecipientAlternatesAdapter.getBetterRecipient(entry2, entry1), entry1); 156 } 157 } 158} 159