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);
     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 testDisplayNameLogic() {
     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);
    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 testLookupKeyLogic() {
    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);
    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 testGetAccountIdByName() {
    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