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 17package com.android.contacts.common.model.account; 18 19import android.content.Context; 20import android.test.InstrumentationTestCase; 21import android.test.suitebuilder.annotation.SmallTest; 22 23import com.android.contacts.common.tests.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 32public class AccountTypeTest extends InstrumentationTestCase { 33 public void testGetResourceText() { 34 // In this test we use the test package itself as an external package. 35 final String packageName = getInstrumentation().getContext().getPackageName(); 36 37 final Context c = getInstrumentation().getTargetContext(); 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(getInstrumentation().getContext().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 = getInstrumentation().getContext().getPackageName(); 63 final Context c = getInstrumentation().getTargetContext(); 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(getInstrumentation().getContext().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( 108 getInstrumentation().getTargetContext()).compare(lhs, rhs); 109 } 110 111 private class AccountTypeForDisplayLabelTest extends AccountType { 112 private final String mDisplayLabel; 113 114 public AccountTypeForDisplayLabelTest(String displayLabel) { 115 mDisplayLabel = displayLabel; 116 } 117 118 @Override 119 public CharSequence getDisplayLabel(Context context) { 120 return mDisplayLabel; 121 } 122 123 @Override 124 public boolean isGroupMembershipEditable() { 125 return false; 126 } 127 128 @Override 129 public boolean areContactsWritable() { 130 return false; 131 } 132 } 133} 134