Home | History | Annotate | Download | only in account
      1 /*
      2  * Copyright (C) 2011 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.contacts.common.model.account;
     18 
     19 import android.content.Context;
     20 import android.test.AndroidTestCase;
     21 import android.test.suitebuilder.annotation.SmallTest;
     22 
     23 import com.android.contacts.common.unittest.R;
     24 
     25 /**
     26  * Test case for {@link AccountType}.
     27  *
     28  * adb shell am instrument -w -e class com.android.contacts.model.AccountTypeTest \
     29        com.android.contacts.tests/android.test.InstrumentationTestRunner
     30  */
     31 @SmallTest
     32 public class AccountTypeTest extends AndroidTestCase {
     33     public void testGetResourceText() {
     34         // In this test we use the test package itself as an external package.
     35         final String packageName = getTestContext().getPackageName();
     36 
     37         final Context c = getContext();
     38         final String DEFAULT = "ABC";
     39 
     40         // Package name null, resId -1, use the default
     41         assertEquals(DEFAULT, AccountType.getResourceText(c, null, -1, DEFAULT));
     42 
     43         // Resource ID -1, use the default
     44         assertEquals(DEFAULT, AccountType.getResourceText(c, packageName, -1, DEFAULT));
     45 
     46         // Load from an external package.  (here, we use this test package itself)
     47         final int externalResID = R.string.test_string;
     48         assertEquals(getTestContext().getString(externalResID),
     49                 AccountType.getResourceText(c, packageName, externalResID, DEFAULT));
     50 
     51         // Load from the contacts package itself.
     52         final int internalResId = com.android.contacts.common.R.string.contactsList;
     53         assertEquals(c.getString(internalResId),
     54                 AccountType.getResourceText(c, null, internalResId, DEFAULT));
     55     }
     56 
     57     /**
     58      * Verify if {@link AccountType#getInviteContactActionLabel} correctly gets the resource ID
     59      * from {@link AccountType#getInviteContactActionResId}
     60      */
     61     public void testGetInviteContactActionLabel() {
     62         final String packageName = getTestContext().getPackageName();
     63         final Context c = getContext();
     64 
     65         final int externalResID = R.string.test_string;
     66 
     67         AccountType accountType = new AccountType() {
     68             {
     69                 resourcePackageName = packageName;
     70                 syncAdapterPackageName = packageName;
     71             }
     72             @Override protected int getInviteContactActionResId() {
     73                 return externalResID;
     74             }
     75 
     76             @Override public boolean isGroupMembershipEditable() {
     77                 return false;
     78             }
     79 
     80             @Override public boolean areContactsWritable() {
     81                 return false;
     82             }
     83         };
     84 
     85         assertEquals(getTestContext().getString(externalResID),
     86                 accountType.getInviteContactActionLabel(c));
     87     }
     88 
     89     public void testDisplayLabelComparator() {
     90         final AccountTypeForDisplayLabelTest EMPTY = new AccountTypeForDisplayLabelTest("");
     91         final AccountTypeForDisplayLabelTest NULL = new AccountTypeForDisplayLabelTest(null);
     92         final AccountTypeForDisplayLabelTest AA = new AccountTypeForDisplayLabelTest("aa");
     93         final AccountTypeForDisplayLabelTest BBB = new AccountTypeForDisplayLabelTest("bbb");
     94         final AccountTypeForDisplayLabelTest C = new AccountTypeForDisplayLabelTest("c");
     95 
     96         assertTrue(compareDisplayLabel(AA, BBB) < 0);
     97         assertTrue(compareDisplayLabel(BBB, C) < 0);
     98         assertTrue(compareDisplayLabel(AA, C) < 0);
     99         assertTrue(compareDisplayLabel(AA, AA) == 0);
    100         assertTrue(compareDisplayLabel(BBB, AA) > 0);
    101 
    102         assertTrue(compareDisplayLabel(EMPTY, AA) < 0);
    103         assertTrue(compareDisplayLabel(EMPTY, NULL) == 0);
    104     }
    105 
    106     private int compareDisplayLabel(AccountType lhs, AccountType rhs) {
    107         return new AccountType.DisplayLabelComparator(getContext()).compare(lhs, rhs);
    108     }
    109 
    110     private class AccountTypeForDisplayLabelTest extends AccountType {
    111         private final String mDisplayLabel;
    112 
    113         public AccountTypeForDisplayLabelTest(String displayLabel) {
    114             mDisplayLabel = displayLabel;
    115         }
    116 
    117         @Override
    118         public CharSequence getDisplayLabel(Context context) {
    119             return mDisplayLabel;
    120         }
    121 
    122         @Override
    123         public boolean isGroupMembershipEditable() {
    124             return false;
    125         }
    126 
    127         @Override
    128         public boolean areContactsWritable() {
    129             return false;
    130         }
    131     }
    132 }
    133