RecipientAlternatesAdapterTest.java revision 514f8a75f72fa2e735418ffb5d6e30aa914fbe7b
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                            false /* isGalContact */);
125            final RecipientEntry entry2 = RecipientEntry.constructFakeEntry("1@android.com", true);
126
127            assertEquals(RecipientAlternatesAdapter.getBetterRecipient(entry1, entry2), entry1);
128            assertEquals(RecipientAlternatesAdapter.getBetterRecipient(entry2, entry1), entry1);
129        }
130
131        // Ensure that if one has a display name different from its destination, and the other's
132        // is equal to its destination, we use the unique one
133        {
134            final RecipientEntry entry1 =
135                    RecipientEntry.constructTopLevelEntry("Android", DisplayNameSources.NICKNAME,
136                            "1@android.com", 0, null, 0, 0, (Uri) null, true,
137                            false /* isGalContact */);
138            final RecipientEntry entry2 =
139                    RecipientEntry.constructTopLevelEntry("2@android.com", DisplayNameSources.EMAIL,
140                            "2@android.com", 0, null, 0, 0, (Uri) null, true,
141                            false /* isGalContact */);
142
143            assertEquals(RecipientAlternatesAdapter.getBetterRecipient(entry1, entry2), entry1);
144            assertEquals(RecipientAlternatesAdapter.getBetterRecipient(entry2, entry1), entry1);
145        }
146
147        // Ensure that if only one has a photo, it is used
148        {
149            final RecipientEntry entry1 =
150                    RecipientEntry.constructTopLevelEntry("Android", DisplayNameSources.NICKNAME,
151                            "1@android.com", 0, null, 0, 0, Uri.parse("http://www.android.com"),
152                            true, false /* isGalContact */);
153            final RecipientEntry entry2 =
154                    RecipientEntry.constructTopLevelEntry("Android", DisplayNameSources.EMAIL,
155                            "2@android.com", 0, null, 0, 0, (Uri) null, true,
156                            false /* isGalContact */);
157
158            assertEquals(RecipientAlternatesAdapter.getBetterRecipient(entry1, entry2), entry1);
159            assertEquals(RecipientAlternatesAdapter.getBetterRecipient(entry2, entry1), entry1);
160        }
161    }
162}
163