summaryrefslogtreecommitdiff
path: root/tests/src/com/android/contacts/model/account/AccountTypeTest.java
blob: b9a11ee1a15a97f86a2cc09fbb384b41f6e39b81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.contacts.model.account;

import android.content.Context;
import android.test.InstrumentationTestCase;

import androidx.test.filters.SmallTest;

import com.android.contacts.tests.R;

/**
 * Test case for {@link AccountType}.
 *
 * adb shell am instrument -w -e class com.android.contacts.model.AccountTypeTest \
       com.android.contacts.tests/android.test.InstrumentationTestRunner
 */
@SmallTest
public class AccountTypeTest extends InstrumentationTestCase {
    public void testGetResourceText() {
        // In this test we use the test package itself as an external package.
        final String packageName = getInstrumentation().getContext().getPackageName();

        final Context c = getInstrumentation().getTargetContext();
        final String DEFAULT = "ABC";

        // Package name null, resId -1, use the default
        assertEquals(DEFAULT, AccountType.getResourceText(c, null, -1, DEFAULT));

        // Resource ID -1, use the default
        assertEquals(DEFAULT, AccountType.getResourceText(c, packageName, -1, DEFAULT));

        // Load from an external package.  (here, we use this test package itself)
        final int externalResID = R.string.test_string;
        assertEquals(getInstrumentation().getContext().getString(externalResID),
                AccountType.getResourceText(c, packageName, externalResID, DEFAULT));

        // Load from the contacts package itself.
        final int internalResId = com.android.contacts.R.string.contactsList;
        assertEquals(c.getString(internalResId),
                AccountType.getResourceText(c, null, internalResId, DEFAULT));
    }

    /**
     * Verify if {@link AccountType#getInviteContactActionLabel} correctly gets the resource ID
     * from {@link AccountType#getInviteContactActionResId}
     */
    public void testGetInviteContactActionLabel() {
        final String packageName = getInstrumentation().getContext().getPackageName();
        final Context c = getInstrumentation().getTargetContext();

        final int externalResID = R.string.test_string;

        AccountType accountType = new AccountType() {
            {
                resourcePackageName = packageName;
                syncAdapterPackageName = packageName;
            }
            @Override protected int getInviteContactActionResId() {
                return externalResID;
            }

            @Override public boolean isGroupMembershipEditable() {
                return false;
            }

            @Override public boolean areContactsWritable() {
                return false;
            }
        };

        assertEquals(getInstrumentation().getContext().getString(externalResID),
                accountType.getInviteContactActionLabel(c));
    }

    public void testDisplayLabelComparator() {
        final AccountTypeForDisplayLabelTest EMPTY = new AccountTypeForDisplayLabelTest("");
        final AccountTypeForDisplayLabelTest NULL = new AccountTypeForDisplayLabelTest(null);
        final AccountTypeForDisplayLabelTest AA = new AccountTypeForDisplayLabelTest("aa");
        final AccountTypeForDisplayLabelTest BBB = new AccountTypeForDisplayLabelTest("bbb");
        final AccountTypeForDisplayLabelTest C = new AccountTypeForDisplayLabelTest("c");

        assertTrue(compareDisplayLabel(AA, BBB) < 0);
        assertTrue(compareDisplayLabel(BBB, C) < 0);
        assertTrue(compareDisplayLabel(AA, C) < 0);
        assertTrue(compareDisplayLabel(AA, AA) == 0);
        assertTrue(compareDisplayLabel(BBB, AA) > 0);

        assertTrue(compareDisplayLabel(EMPTY, AA) < 0);
        assertTrue(compareDisplayLabel(EMPTY, NULL) == 0);
    }

    private int compareDisplayLabel(AccountType lhs, AccountType rhs) {
        return new AccountType.DisplayLabelComparator(
                getInstrumentation().getTargetContext()).compare(lhs, rhs);
    }

    private class AccountTypeForDisplayLabelTest extends AccountType {
        private final String mDisplayLabel;

        public AccountTypeForDisplayLabelTest(String displayLabel) {
            mDisplayLabel = displayLabel;
        }

        @Override
        public CharSequence getDisplayLabel(Context context) {
            return mDisplayLabel;
        }

        @Override
        public boolean isGroupMembershipEditable() {
            return false;
        }

        @Override
        public boolean areContactsWritable() {
            return false;
        }
    }
}