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.test.AndroidTestCase;
22
23public class RecipientAlternatesAdapterTest extends AndroidTestCase {
24
25    public void testRemoveDuplicateDestinations() {
26        MatrixCursor c = new MatrixCursor(Queries.EMAIL.getProjection());
27        Cursor result;
28
29        // Test: Empty input
30        assertEquals(0, RecipientAlternatesAdapter.removeDuplicateDestinations(c).getCount());
31
32
33        // Test: One row
34        addRow(c, "a", "1@android.com", 1, "home", 1000, 2000, "x", 0);
35
36        result = RecipientAlternatesAdapter.removeDuplicateDestinations(c);
37        assertEquals(1, result.getCount());
38        assertRow(result, 0, "a", "1@android.com", 1, "home", 1000, 2000, "x", 0);
39
40        // Test: two unique rows, different destinations
41        addRow(c, "a", "2@android.com", 1, "home", 1000, 2000, "x", 0);
42
43        result = RecipientAlternatesAdapter.removeDuplicateDestinations(c);
44        assertEquals(2, result.getCount());
45        assertRow(result, 0, "a", "1@android.com", 1, "home", 1000, 2000, "x", 0);
46        assertRow(result, 1, "a", "2@android.com", 1, "home", 1000, 2000, "x", 0);
47
48        // Test: add a third row with a non-unique destination.
49        addRow(c, "ax", "1@android.com", 11, "homex", 10001, 2000, "xx", 1);
50
51        // Third row should be removed.
52        result = RecipientAlternatesAdapter.removeDuplicateDestinations(c);
53        assertEquals(2, result.getCount());
54        assertRow(result, 0, "a", "1@android.com", 1, "home", 1000, 2000, "x", 0);
55        assertRow(result, 1, "a", "2@android.com", 1, "home", 1000, 2000, "x", 0);
56
57        // Test: add a forth row with a non-unique destination again.
58        addRow(c, "ax", "2@android.com", 11, "homex", 10001, 2000, "xx", 1);
59
60        // Forth row should also be removed.
61        result = RecipientAlternatesAdapter.removeDuplicateDestinations(c);
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
67    private static MatrixCursor addRow(MatrixCursor c,
68            String displayName,
69            String destination,
70            int destinationType,
71            String destinationLabel,
72            long contactId,
73            long dataId,
74            String photoUri,
75            int displayNameSource
76            ) {
77        c.addRow(new Object[] {displayName, destination, destinationType, destinationLabel,
78                contactId, dataId, photoUri, displayNameSource});
79        return c;
80    }
81
82    private static void assertRow(Cursor c, int position,
83            String displayName,
84            String destination,
85            int destinationType,
86            String destinationLabel,
87            long contactId,
88            long dataId,
89            String photoUri,
90            int displayNameSource
91            ) {
92        assertTrue(c.moveToPosition(position));
93        assertEquals(displayName, c.getString(0));
94        assertEquals(destination, c.getString(1));
95        assertEquals(destinationType, c.getInt(2));
96        assertEquals(destinationLabel, c.getString(3));
97        assertEquals(contactId, c.getLong(4));
98        assertEquals(dataId, c.getLong(5));
99        assertEquals(photoUri, c.getString(6));
100        assertEquals(displayNameSource, c.getInt(7));
101    }
102}
103