summaryrefslogtreecommitdiffstats
path: root/chips/tests/src/com/android/ex/chips/RecipientAlternatesAdapterTest.java
blob: d4c0460278edc3fda786fcfd0be8a6cf80d7efd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Copyright (C) 2012 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.ex.chips;

import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.provider.ContactsContract.DisplayNameSources;
import android.test.AndroidTestCase;

import com.android.ex.chips.RecipientAlternatesAdapter;
import com.android.ex.chips.RecipientEntry;

public class RecipientAlternatesAdapterTest extends AndroidTestCase {

    public void testRemoveDuplicateDestinations() {
        MatrixCursor c = new MatrixCursor(Queries.EMAIL.getProjection());
        Cursor result;

        // Test: Empty input
        assertEquals(0, RecipientAlternatesAdapter.removeDuplicateDestinations(c).getCount());


        // Test: One row
        addRow(c, "a", "1@android.com", 1, "home", 1000, 2000, "x", 0);

        result = RecipientAlternatesAdapter.removeDuplicateDestinations(c);
        assertEquals(1, result.getCount());
        assertRow(result, 0, "a", "1@android.com", 1, "home", 1000, 2000, "x", 0);

        // Test: two unique rows, different destinations
        addRow(c, "a", "2@android.com", 1, "home", 1000, 2000, "x", 0);

        result = RecipientAlternatesAdapter.removeDuplicateDestinations(c);
        assertEquals(2, result.getCount());
        assertRow(result, 0, "a", "1@android.com", 1, "home", 1000, 2000, "x", 0);
        assertRow(result, 1, "a", "2@android.com", 1, "home", 1000, 2000, "x", 0);

        // Test: add a third row with a non-unique destination.
        addRow(c, "ax", "1@android.com", 11, "homex", 10001, 2000, "xx", 1);

        // Third row should be removed.
        result = RecipientAlternatesAdapter.removeDuplicateDestinations(c);
        assertEquals(2, result.getCount());
        assertRow(result, 0, "a", "1@android.com", 1, "home", 1000, 2000, "x", 0);
        assertRow(result, 1, "a", "2@android.com", 1, "home", 1000, 2000, "x", 0);

        // Test: add a forth row with a non-unique destination again.
        addRow(c, "ax", "2@android.com", 11, "homex", 10001, 2000, "xx", 1);

        // Forth row should also be removed.
        result = RecipientAlternatesAdapter.removeDuplicateDestinations(c);
        assertEquals(2, result.getCount());
        assertRow(result, 0, "a", "1@android.com", 1, "home", 1000, 2000, "x", 0);
        assertRow(result, 1, "a", "2@android.com", 1, "home", 1000, 2000, "x", 0);
    }

    private static MatrixCursor addRow(MatrixCursor c,
            String displayName,
            String destination,
            int destinationType,
            String destinationLabel,
            long contactId,
            long dataId,
            String photoUri,
            int displayNameSource
            ) {
        c.addRow(new Object[] {displayName, destination, destinationType, destinationLabel,
                contactId, dataId, photoUri, displayNameSource});
        return c;
    }

    private static void assertRow(Cursor c, int position,
            String displayName,
            String destination,
            int destinationType,
            String destinationLabel,
            long contactId,
            long dataId,
            String photoUri,
            int displayNameSource
            ) {
        assertTrue(c.moveToPosition(position));
        assertEquals(displayName, c.getString(0));
        assertEquals(destination, c.getString(1));
        assertEquals(destinationType, c.getInt(2));
        assertEquals(destinationLabel, c.getString(3));
        assertEquals(contactId, c.getLong(4));
        assertEquals(dataId, c.getLong(5));
        assertEquals(photoUri, c.getString(6));
        assertEquals(displayNameSource, c.getInt(7));
    }

    public void testGetBetterRecipient() {
        // Ensure that if either (but not both) parameters are null, the other is returned
        {
            final RecipientEntry entry1 =
                    RecipientEntry.constructFakeEntry("1@android.com", true);
            final RecipientEntry entry2 = null;

            assertEquals(RecipientAlternatesAdapter.getBetterRecipient(entry1, entry2), entry1);
            assertEquals(RecipientAlternatesAdapter.getBetterRecipient(entry2, entry1), entry1);
        }

        // Ensure that if only one has a display name, it is used
        {
            final RecipientEntry entry1 =
                    RecipientEntry.constructTopLevelEntry("Android", DisplayNameSources.NICKNAME,
                            "1@android.com", 0, null, 0, 0, (Uri) null, true,
                            false /* isGalContact */);
            final RecipientEntry entry2 = RecipientEntry.constructFakeEntry("1@android.com", true);

            assertEquals(RecipientAlternatesAdapter.getBetterRecipient(entry1, entry2), entry1);
            assertEquals(RecipientAlternatesAdapter.getBetterRecipient(entry2, entry1), entry1);
        }

        // Ensure that if one has a display name different from its destination, and the other's
        // is equal to its destination, we use the unique one
        {
            final RecipientEntry entry1 =
                    RecipientEntry.constructTopLevelEntry("Android", DisplayNameSources.NICKNAME,
                            "1@android.com", 0, null, 0, 0, (Uri) null, true,
                            false /* isGalContact */);
            final RecipientEntry entry2 =
                    RecipientEntry.constructTopLevelEntry("2@android.com", DisplayNameSources.EMAIL,
                            "2@android.com", 0, null, 0, 0, (Uri) null, true,
                            false /* isGalContact */);

            assertEquals(RecipientAlternatesAdapter.getBetterRecipient(entry1, entry2), entry1);
            assertEquals(RecipientAlternatesAdapter.getBetterRecipient(entry2, entry1), entry1);
        }

        // Ensure that if only one has a photo, it is used
        {
            final RecipientEntry entry1 =
                    RecipientEntry.constructTopLevelEntry("Android", DisplayNameSources.NICKNAME,
                            "1@android.com", 0, null, 0, 0, Uri.parse("http://www.android.com"),
                            true, false /* isGalContact */);
            final RecipientEntry entry2 =
                    RecipientEntry.constructTopLevelEntry("Android", DisplayNameSources.EMAIL,
                            "2@android.com", 0, null, 0, 0, (Uri) null, true,
                            false /* isGalContact */);

            assertEquals(RecipientAlternatesAdapter.getBetterRecipient(entry1, entry2), entry1);
            assertEquals(RecipientAlternatesAdapter.getBetterRecipient(entry2, entry1), entry1);
        }
    }
}