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