blob: 15aaa18c0e9fd6b5acc5f8d305fcd03d0fd9cf61 [file] [log] [blame]
Chiao Chenge88fcd32012-11-13 18:38:05 -08001/*
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
Gary Mai69c182a2016-12-05 13:07:03 -080017package com.android.contacts.model.account;
Chiao Chenge88fcd32012-11-13 18:38:05 -080018
19import android.content.Context;
Jay Shrauner7bae0632015-08-27 16:23:44 -070020import android.test.InstrumentationTestCase;
Chiao Chenge88fcd32012-11-13 18:38:05 -080021import android.test.suitebuilder.annotation.SmallTest;
22
Yorke Leed0a33882015-10-28 18:26:19 -070023import com.android.contacts.common.tests.R;
Chiao Chenge88fcd32012-11-13 18:38:05 -080024
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 Shrauner7bae0632015-08-27 16:23:44 -070032public class AccountTypeTest extends InstrumentationTestCase {
Chiao Chenge88fcd32012-11-13 18:38:05 -080033 public void testGetResourceText() {
34 // In this test we use the test package itself as an external package.
Jay Shrauner7bae0632015-08-27 16:23:44 -070035 final String packageName = getInstrumentation().getContext().getPackageName();
Chiao Chenge88fcd32012-11-13 18:38:05 -080036
Jay Shrauner05452582015-08-29 11:34:25 -070037 final Context c = getInstrumentation().getTargetContext();
Chiao Chenge88fcd32012-11-13 18:38:05 -080038 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 Shrauner7bae0632015-08-27 16:23:44 -070048 assertEquals(getInstrumentation().getContext().getString(externalResID),
Chiao Chenge88fcd32012-11-13 18:38:05 -080049 AccountType.getResourceText(c, packageName, externalResID, DEFAULT));
50
51 // Load from the contacts package itself.
Arthur Wang3f6a2442016-12-05 14:51:59 -080052 final int internalResId = com.android.contacts.R.string.contactsList;
Chiao Chenge88fcd32012-11-13 18:38:05 -080053 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 Shrauner7bae0632015-08-27 16:23:44 -070062 final String packageName = getInstrumentation().getContext().getPackageName();
Jay Shrauner05452582015-08-29 11:34:25 -070063 final Context c = getInstrumentation().getTargetContext();
Chiao Chenge88fcd32012-11-13 18:38:05 -080064
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 Shrauner7bae0632015-08-27 16:23:44 -070085 assertEquals(getInstrumentation().getContext().getString(externalResID),
Chiao Chenge88fcd32012-11-13 18:38:05 -080086 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 Shrauner05452582015-08-29 11:34:25 -0700107 return new AccountType.DisplayLabelComparator(
108 getInstrumentation().getTargetContext()).compare(lhs, rhs);
Chiao Chenge88fcd32012-11-13 18:38:05 -0800109 }
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}