Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 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; |
Jay Shrauner | 7bae063 | 2015-08-27 16:23:44 -0700 | [diff] [blame] | 20 | import android.test.InstrumentationTestCase; |
Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 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 |
Jay Shrauner | 7bae063 | 2015-08-27 16:23:44 -0700 | [diff] [blame] | 32 | public class AccountTypeTest extends InstrumentationTestCase { |
Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 33 | public void testGetResourceText() { |
| 34 | // In this test we use the test package itself as an external package. |
Jay Shrauner | 7bae063 | 2015-08-27 16:23:44 -0700 | [diff] [blame] | 35 | final String packageName = getInstrumentation().getContext().getPackageName(); |
Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 36 | |
Jay Shrauner | 0545258 | 2015-08-29 11:34:25 -0700 | [diff] [blame^] | 37 | final Context c = getInstrumentation().getTargetContext(); |
Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 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; |
Jay Shrauner | 7bae063 | 2015-08-27 16:23:44 -0700 | [diff] [blame] | 48 | assertEquals(getInstrumentation().getContext().getString(externalResID), |
Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 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() { |
Jay Shrauner | 7bae063 | 2015-08-27 16:23:44 -0700 | [diff] [blame] | 62 | final String packageName = getInstrumentation().getContext().getPackageName(); |
Jay Shrauner | 0545258 | 2015-08-29 11:34:25 -0700 | [diff] [blame^] | 63 | final Context c = getInstrumentation().getTargetContext(); |
Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 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 | |
Jay Shrauner | 7bae063 | 2015-08-27 16:23:44 -0700 | [diff] [blame] | 85 | assertEquals(getInstrumentation().getContext().getString(externalResID), |
Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 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) { |
Jay Shrauner | 0545258 | 2015-08-29 11:34:25 -0700 | [diff] [blame^] | 107 | return new AccountType.DisplayLabelComparator( |
| 108 | getInstrumentation().getTargetContext()).compare(lhs, rhs); |
Chiao Cheng | e88fcd3 | 2012-11-13 18:38:05 -0800 | [diff] [blame] | 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 | } |