ExchangeDirectoryProviderTests.java revision d41eaba645dcb4573c5dc33077fc87799ef06195
1/*
2 * Copyright (C) 2010 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.exchange.provider;
18
19import com.android.email.mail.PackedString;
20import com.android.exchange.provider.GalResult.GalData;
21
22import android.database.Cursor;
23import android.database.MatrixCursor;
24import android.provider.ContactsContract.CommonDataKinds;
25import android.provider.ContactsContract.Contacts;
26import android.test.AndroidTestCase;
27
28/**
29 * You can run this entire test case with:
30 *   runtest -c com.android.exchange.provider.ExchangeDirectoryProviderTests email
31 */
32public class ExchangeDirectoryProviderTests extends AndroidTestCase {
33
34    // Create a test projection; we should only get back values for display name and email address
35    private static final String[] GAL_RESULT_PROJECTION =
36        new String[] {Contacts.DISPLAY_NAME, CommonDataKinds.Email.ADDRESS, Contacts.CONTENT_TYPE,
37            Contacts.LOOKUP_KEY};
38    private static final int GAL_RESULT_COLUMN_DISPLAY_NAME = 0;
39    private static final int GAL_RESULT_COLUMN_EMAIL_ADDRESS = 1;
40    private static final int GAL_RESULT_COLUMN_CONTENT_TYPE = 2;
41    private static final int GAL_RESULT_COLUMN_LOOKUP_KEY = 3;
42
43    public void testBuildSimpleGalResultCursor() {
44        GalResult result = new GalResult();
45        result.addGalData(1, "Alice Aardvark", "alice@aardvark.com");
46        result.addGalData(2, "Bob Badger", "bob@badger.com");
47        result.addGalData(3, "Clark Cougar", "clark@cougar.com");
48        result.addGalData(4, "Dan Dolphin", "dan@dolphin.com");
49        // Make sure our returned cursor has the expected contents
50        ExchangeDirectoryProvider provider = new ExchangeDirectoryProvider();
51        Cursor c = provider.buildGalResultCursor(GAL_RESULT_PROJECTION, result);
52        assertNotNull(c);
53        assertEquals(MatrixCursor.class, c.getClass());
54        assertEquals(4, c.getCount());
55        for (int i = 0; i < 4; i++) {
56            GalData data = result.galData.get(i);
57            assertTrue(c.moveToNext());
58            assertEquals(data.displayName, c.getString(GAL_RESULT_COLUMN_DISPLAY_NAME));
59            assertEquals(data.emailAddress, c.getString(GAL_RESULT_COLUMN_EMAIL_ADDRESS));
60            assertNull(c.getString(GAL_RESULT_COLUMN_CONTENT_TYPE));
61        }
62    }
63
64    private static final String[][] DISPLAY_NAME_TEST_FIELDS = {
65        {"Alice", "Aardvark", "Another Name"},
66        {"Alice", "Aardvark", null},
67        {"Alice", null, null},
68        {null, "Aardvark", null},
69        {null, null, null}
70    };
71    private static final int TEST_FIELD_FIRST_NAME = 0;
72    private static final int TEST_FIELD_LAST_NAME = 1;
73    private static final int TEST_FIELD_DISPLAY_NAME = 2;
74    private static final String[] EXPECTED_DISPLAY_NAMES = new String[] {"Another Name",
75        "Alice Aardvark", "Alice", "Aardvark", null};
76
77    private GalResult getTestDisplayNameResult() {
78        GalResult result = new GalResult();
79        for (int i = 0; i < DISPLAY_NAME_TEST_FIELDS.length; i++) {
80            GalData galData = new GalData();
81            String[] names = DISPLAY_NAME_TEST_FIELDS[i];
82            galData.put(GalData.FIRST_NAME, names[TEST_FIELD_FIRST_NAME]);
83            galData.put(GalData.LAST_NAME, names[TEST_FIELD_LAST_NAME]);
84            galData.put(GalData.DISPLAY_NAME, names[TEST_FIELD_DISPLAY_NAME]);
85            result.addGalData(galData);
86        }
87        return result;
88    }
89
90    public void testDisplayNameLogic() {
91        GalResult result = getTestDisplayNameResult();
92        // Make sure our returned cursor has the expected contents
93        ExchangeDirectoryProvider provider = new ExchangeDirectoryProvider();
94        Cursor c = provider.buildGalResultCursor(GAL_RESULT_PROJECTION, result);
95        assertNotNull(c);
96        assertEquals(MatrixCursor.class, c.getClass());
97        assertEquals(DISPLAY_NAME_TEST_FIELDS.length, c.getCount());
98        for (int i = 0; i < EXPECTED_DISPLAY_NAMES.length; i++) {
99            assertTrue(c.moveToNext());
100            assertEquals(EXPECTED_DISPLAY_NAMES[i], c.getString(GAL_RESULT_COLUMN_DISPLAY_NAME));
101        }
102    }
103
104    public void testLookupKeyLogic() {
105        GalResult result = getTestDisplayNameResult();
106        // Make sure our returned cursor has the expected contents
107        ExchangeDirectoryProvider provider = new ExchangeDirectoryProvider();
108        Cursor c = provider.buildGalResultCursor(GAL_RESULT_PROJECTION, result);
109        assertNotNull(c);
110        assertEquals(MatrixCursor.class, c.getClass());
111        assertEquals(DISPLAY_NAME_TEST_FIELDS.length, c.getCount());
112        for (int i = 0; i < EXPECTED_DISPLAY_NAMES.length; i++) {
113            assertTrue(c.moveToNext());
114            PackedString ps = new PackedString(c.getString(GAL_RESULT_COLUMN_LOOKUP_KEY));
115            String[] testFields = DISPLAY_NAME_TEST_FIELDS[i];
116            assertEquals(testFields[TEST_FIELD_FIRST_NAME], ps.get(GalData.FIRST_NAME));
117            assertEquals(testFields[TEST_FIELD_LAST_NAME], ps.get(GalData.LAST_NAME));
118            assertEquals(EXPECTED_DISPLAY_NAMES[i], ps.get(GalData.DISPLAY_NAME));
119        }
120    }
121}
122