Home | History | Annotate | Download | only in provider
      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 
     17 package com.android.exchange.provider;
     18 
     19 import com.android.emailcommon.mail.PackedString;
     20 import com.android.emailcommon.provider.Account;
     21 import com.android.exchange.provider.GalResult.GalData;
     22 import com.android.exchange.utility.ExchangeTestCase;
     23 
     24 import android.content.Context;
     25 import android.database.Cursor;
     26 import android.database.MatrixCursor;
     27 import android.net.Uri;
     28 import android.provider.ContactsContract.CommonDataKinds;
     29 import android.provider.ContactsContract.Contacts;
     30 import android.test.suitebuilder.annotation.SmallTest;
     31 
     32 /**
     33  * You can run this entire test case with:
     34  *   runtest -c com.android.exchange.provider.ExchangeDirectoryProviderTests exchange
     35  */
     36 @SmallTest
     37 public class ExchangeDirectoryProviderTests extends ExchangeTestCase {
     38 
     39     public ExchangeDirectoryProviderTests() {
     40     }
     41 
     42     // Create a test projection; we should only get back values for display name and email address
     43     private static final String[] GAL_RESULT_PROJECTION =
     44         new String[] {Contacts.DISPLAY_NAME, CommonDataKinds.Email.ADDRESS, Contacts.CONTENT_TYPE,
     45             Contacts.LOOKUP_KEY};
     46     private static final int GAL_RESULT_COLUMN_DISPLAY_NAME = 0;
     47     private static final int GAL_RESULT_COLUMN_EMAIL_ADDRESS = 1;
     48     private static final int GAL_RESULT_COLUMN_CONTENT_TYPE = 2;
     49     private static final int GAL_RESULT_COLUMN_LOOKUP_KEY = 3;
     50 
     51     public void testBuildSimpleGalResultCursor() {
     52         GalResult result = new GalResult();
     53         result.addGalData(1, "Alice Aardvark", "alice (at) aardvark.com");
     54         result.addGalData(2, "Bob Badger", "bob (at) badger.com");
     55         result.addGalData(3, "Clark Cougar", "clark (at) cougar.com");
     56         result.addGalData(4, "Dan Dolphin", "dan (at) dolphin.com");
     57         // Make sure our returned cursor has the expected contents
     58         ExchangeDirectoryProvider provider = new ExchangeDirectoryProvider();
     59         Cursor c = provider.buildGalResultCursor(GAL_RESULT_PROJECTION, result, null, 20, false, false);
     60         assertNotNull(c);
     61         assertEquals(MatrixCursor.class, c.getClass());
     62         assertEquals(4, c.getCount());
     63         for (int i = 0; i < 4; i++) {
     64             GalData data = result.galData.get(i);
     65             assertTrue(c.moveToNext());
     66             assertEquals(data.displayName, c.getString(GAL_RESULT_COLUMN_DISPLAY_NAME));
     67             assertEquals(data.emailAddress, c.getString(GAL_RESULT_COLUMN_EMAIL_ADDRESS));
     68             assertNull(c.getString(GAL_RESULT_COLUMN_CONTENT_TYPE));
     69         }
     70     }
     71 
     72     private static final String[][] DISPLAY_NAME_TEST_FIELDS = {
     73         {"Alice", "Aardvark", "Another Name"},
     74         {"Alice", "Aardvark", null},
     75         {"Alice", null, null},
     76         {null, "Aardvark", null},
     77         {null, null, null}
     78     };
     79     private static final int TEST_FIELD_FIRST_NAME = 0;
     80     private static final int TEST_FIELD_LAST_NAME = 1;
     81     private static final int TEST_FIELD_DISPLAY_NAME = 2;
     82     private static final String[] EXPECTED_DISPLAY_NAMES = new String[] {"Another Name",
     83         "Alice Aardvark", "Alice", "Aardvark", null};
     84 
     85     private GalResult getTestDisplayNameResult() {
     86         GalResult result = new GalResult();
     87         for (int i = 0; i < DISPLAY_NAME_TEST_FIELDS.length; i++) {
     88             GalData galData = new GalData();
     89             String[] names = DISPLAY_NAME_TEST_FIELDS[i];
     90             galData.put(GalData.FIRST_NAME, names[TEST_FIELD_FIRST_NAME]);
     91             galData.put(GalData.LAST_NAME, names[TEST_FIELD_LAST_NAME]);
     92             galData.put(GalData.DISPLAY_NAME, names[TEST_FIELD_DISPLAY_NAME]);
     93             result.addGalData(galData);
     94         }
     95         return result;
     96     }
     97 
     98     public void brokentestDisplayNameLogic() {
     99         GalResult result = getTestDisplayNameResult();
    100         // Make sure our returned cursor has the expected contents
    101         ExchangeDirectoryProvider provider = new ExchangeDirectoryProvider();
    102         Cursor c = provider.buildGalResultCursor(GAL_RESULT_PROJECTION, result, null, 20, false, false);
    103         assertNotNull(c);
    104         assertEquals(MatrixCursor.class, c.getClass());
    105         assertEquals(DISPLAY_NAME_TEST_FIELDS.length, c.getCount());
    106         for (int i = 0; i < EXPECTED_DISPLAY_NAMES.length; i++) {
    107             assertTrue(c.moveToNext());
    108             assertEquals(EXPECTED_DISPLAY_NAMES[i], c.getString(GAL_RESULT_COLUMN_DISPLAY_NAME));
    109         }
    110     }
    111 
    112     public void brokentestLookupKeyLogic() {
    113         GalResult result = getTestDisplayNameResult();
    114         // Make sure our returned cursor has the expected contents
    115         ExchangeDirectoryProvider provider = new ExchangeDirectoryProvider();
    116         Cursor c = provider.buildGalResultCursor(GAL_RESULT_PROJECTION, result, null, 20, false, false);
    117         assertNotNull(c);
    118         assertEquals(MatrixCursor.class, c.getClass());
    119         assertEquals(DISPLAY_NAME_TEST_FIELDS.length, c.getCount());
    120         for (int i = 0; i < EXPECTED_DISPLAY_NAMES.length; i++) {
    121             assertTrue(c.moveToNext());
    122             PackedString ps =
    123                 new PackedString(Uri.decode(c.getString(GAL_RESULT_COLUMN_LOOKUP_KEY)));
    124             String[] testFields = DISPLAY_NAME_TEST_FIELDS[i];
    125             assertEquals(testFields[TEST_FIELD_FIRST_NAME], ps.get(GalData.FIRST_NAME));
    126             assertEquals(testFields[TEST_FIELD_LAST_NAME], ps.get(GalData.LAST_NAME));
    127             assertEquals(EXPECTED_DISPLAY_NAMES[i], ps.get(GalData.DISPLAY_NAME));
    128         }
    129     }
    130 
    131     public void brokentestGetAccountIdByName() {
    132         Context context = getContext(); //getMockContext();
    133         ExchangeDirectoryProvider provider = new ExchangeDirectoryProvider();
    134         // Nothing up my sleeve
    135         assertNull(provider.mAccountIdMap.get("foo (at) android.com"));
    136         assertNull(provider.mAccountIdMap.get("bar (at) android.com"));
    137         // Create accounts; the email addresses will be the first argument + "@android.com"
    138         Account acctFoo = setupTestAccount("foo", true);
    139         Account acctBar = setupTestAccount("bar", true);
    140         // Make sure we can retrieve them, and that the map is populated
    141         assertEquals(acctFoo.mId, provider.getAccountIdByName(context, "foo (at) android.com"));
    142         assertEquals(acctBar.mId, provider.getAccountIdByName(context, "bar (at) android.com"));
    143         assertEquals((Long)acctFoo.mId, provider.mAccountIdMap.get("foo (at) android.com"));
    144         assertEquals((Long)acctBar.mId, provider.mAccountIdMap.get("bar (at) android.com"));
    145     }
    146 
    147     /**
    148      * The purpose of these next tests are to test the ExchangeDirectoryProvider. NameComparator comparison function
    149      * and not the Collator class. This means that we can test only with Western strings.
    150      * Note that there is a loose assumption that IDs always exist. If this is a valid
    151      * assumption, we should enforce it in the ExchangeDirectoryProvider.GalSortKey class or in the compare function.
    152      */
    153     public void testNameComparatorEqualsStringLhsIdGreater() {
    154         final ExchangeDirectoryProvider.GalSortKey lhs = new ExchangeDirectoryProvider.GalSortKey("A", 2);
    155         final ExchangeDirectoryProvider.GalSortKey rhs = new ExchangeDirectoryProvider.GalSortKey("A", 1);
    156 
    157         final ExchangeDirectoryProvider. NameComparator comparator = new ExchangeDirectoryProvider. NameComparator();
    158         final int ret = comparator.compare(lhs, rhs);
    159         assertEquals(ret, 1);
    160     }
    161 
    162     public void testNameComparatorEqualsStringRhsIdGreater() {
    163         final ExchangeDirectoryProvider.GalSortKey lhs = new ExchangeDirectoryProvider.GalSortKey("A", 1);
    164         final ExchangeDirectoryProvider.GalSortKey rhs = new ExchangeDirectoryProvider.GalSortKey("A", 2);
    165 
    166         final ExchangeDirectoryProvider. NameComparator comparator = new ExchangeDirectoryProvider. NameComparator();
    167         final int ret = comparator.compare(lhs, rhs);
    168         assertEquals(ret, -1);
    169     }
    170 
    171     public void testNameComparatorEqualsEverythingEqual() {
    172         final ExchangeDirectoryProvider.GalSortKey lhs = new ExchangeDirectoryProvider.GalSortKey("A", 1);
    173         final ExchangeDirectoryProvider.GalSortKey rhs = new ExchangeDirectoryProvider.GalSortKey("A", 1);
    174 
    175         final ExchangeDirectoryProvider. NameComparator comparator = new ExchangeDirectoryProvider. NameComparator();
    176         final int ret = comparator.compare(lhs, rhs);
    177         assertEquals(ret, 0);
    178     }
    179 
    180     public void testNameComparatorLhsGreaterString() {
    181         final ExchangeDirectoryProvider.GalSortKey lhs = new ExchangeDirectoryProvider.GalSortKey("B", 1);
    182         final ExchangeDirectoryProvider.GalSortKey rhs = new ExchangeDirectoryProvider.GalSortKey("A", 2);
    183 
    184         final ExchangeDirectoryProvider. NameComparator comparator = new ExchangeDirectoryProvider. NameComparator();
    185         final int ret = comparator.compare(lhs, rhs);
    186         assertEquals(ret, 1);
    187     }
    188 
    189     public void testNameComparatorRhsGreaterString() {
    190         final ExchangeDirectoryProvider.GalSortKey lhs = new ExchangeDirectoryProvider.GalSortKey("A", 1);
    191         final ExchangeDirectoryProvider.GalSortKey rhs = new ExchangeDirectoryProvider.GalSortKey("B", 2);
    192 
    193         final ExchangeDirectoryProvider. NameComparator comparator = new ExchangeDirectoryProvider. NameComparator();
    194         final int ret = comparator.compare(lhs, rhs);
    195         assertEquals(ret, -1);
    196     }
    197 
    198     public void testNameComparatorLhsNoStringLhsGreaterId() {
    199         final ExchangeDirectoryProvider.GalSortKey lhs = new ExchangeDirectoryProvider.GalSortKey(null, 2);
    200         final ExchangeDirectoryProvider.GalSortKey rhs = new ExchangeDirectoryProvider.GalSortKey("A", 1);
    201 
    202         final ExchangeDirectoryProvider. NameComparator comparator = new ExchangeDirectoryProvider. NameComparator();
    203         final int ret = comparator.compare(lhs, rhs);
    204         assertEquals(ret, -1);
    205     }
    206 
    207     public void testNameComparatorLhsNoStringRhsGreaterId() {
    208         final ExchangeDirectoryProvider.GalSortKey lhs = new ExchangeDirectoryProvider.GalSortKey(null, 1);
    209         final ExchangeDirectoryProvider.GalSortKey rhs = new ExchangeDirectoryProvider.GalSortKey("A", 2);
    210 
    211         final ExchangeDirectoryProvider. NameComparator comparator = new ExchangeDirectoryProvider. NameComparator();
    212         final int ret = comparator.compare(lhs, rhs);
    213         assertEquals(ret, -1);
    214     }
    215 
    216     public void testNameComparatorRhsNoStringLhsGreaterId() {
    217         final ExchangeDirectoryProvider.GalSortKey lhs = new ExchangeDirectoryProvider.GalSortKey("A", 2);
    218         final ExchangeDirectoryProvider.GalSortKey rhs = new ExchangeDirectoryProvider.GalSortKey(null, 1);
    219 
    220         final ExchangeDirectoryProvider. NameComparator comparator = new ExchangeDirectoryProvider. NameComparator();
    221         final int ret = comparator.compare(lhs, rhs);
    222         assertEquals(ret, 1);
    223     }
    224 
    225     public void testNameComparatorRhsNoStringRhsGreaterId() {
    226         final ExchangeDirectoryProvider.GalSortKey lhs = new ExchangeDirectoryProvider.GalSortKey("A", 1);
    227         final ExchangeDirectoryProvider.GalSortKey rhs = new ExchangeDirectoryProvider.GalSortKey(null, 2);
    228 
    229         final ExchangeDirectoryProvider. NameComparator comparator = new ExchangeDirectoryProvider. NameComparator();
    230         final int ret = comparator.compare(lhs, rhs);
    231         assertEquals(ret, 1);
    232     }
    233 
    234     public void testNameComparatorNoStringsLhsWins() {
    235         final ExchangeDirectoryProvider.GalSortKey lhs = new ExchangeDirectoryProvider.GalSortKey(null, 2);
    236         final ExchangeDirectoryProvider.GalSortKey rhs = new ExchangeDirectoryProvider.GalSortKey(null, 1);
    237 
    238         final ExchangeDirectoryProvider. NameComparator comparator = new ExchangeDirectoryProvider. NameComparator();
    239         final int ret = comparator.compare(lhs, rhs);
    240         assertEquals(ret, 1);
    241     }
    242 
    243     public void testNameComparatorNoStringsRhsWins() {
    244         final ExchangeDirectoryProvider.GalSortKey lhs = new ExchangeDirectoryProvider.GalSortKey(null, 1);
    245         final ExchangeDirectoryProvider.GalSortKey rhs = new ExchangeDirectoryProvider.GalSortKey(null, 2);
    246 
    247         final ExchangeDirectoryProvider. NameComparator comparator = new ExchangeDirectoryProvider. NameComparator();
    248         final int ret = comparator.compare(lhs, rhs);
    249         assertEquals(ret, -1);
    250     }
    251 
    252 }
    253