EditorUiUtilsTest.java revision 949d4e88400761dac02992978f6b79997b5f0055
1/*
2 * Copyright (C) 2015 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.editor;
18
19import android.content.Context;
20import android.media.RingtoneManager;
21import android.net.Uri;
22import android.os.Build;
23import android.provider.Settings;
24import android.test.AndroidTestCase;
25import android.test.suitebuilder.annotation.SmallTest;
26
27import com.android.contacts.common.model.account.AccountDisplayInfo;
28import com.android.contacts.R;
29import com.android.contacts.common.model.account.AccountType;
30import com.android.contacts.common.model.account.AccountWithDataSet;
31
32/**
33 * Tests {@link EditorUiUtils}.
34 */
35@SmallTest
36public class EditorUiUtilsTest extends AndroidTestCase {
37
38    private static final String ACCOUNT_NAME = "somebody@lunkedin.com";
39    private static final String DISPLAY_LABEL = "LunkedIn";
40
41    private static final String GOOGLE_ACCOUNT_NAME = "somebody@gmail.com";
42    private static final String GOOGLE_DISPLAY_LABEL = "Google";
43
44    private static final String RINGTONE = "content://media/external/audio/media/31";
45
46    private static final AccountWithDataSet ACCOUNT =
47            new AccountWithDataSet(ACCOUNT_NAME, "some.account.type", null);
48    private static final AccountWithDataSet GOOGLE_ACCOUNT =
49            new AccountWithDataSet(ACCOUNT_NAME, "com.google", null);
50
51    private static final class MockAccountType extends AccountType {
52
53        private final String mDisplayLabel;
54
55        private MockAccountType(String displayLabel) {
56            mDisplayLabel = displayLabel;
57        }
58
59        @Override
60        public boolean areContactsWritable() {
61            return false;
62        }
63
64        @Override
65        public boolean isGroupMembershipEditable() {
66            return false;
67        }
68
69        @Override
70        public CharSequence getDisplayLabel(Context context) {
71            return mDisplayLabel;
72        }
73    }
74
75    public void testGetProfileAccountInfo_NonLocalAccount() {
76        final AccountDisplayInfo account = new AccountDisplayInfo(ACCOUNT, ACCOUNT_NAME,
77                DISPLAY_LABEL, /*icon*/ null, /*isDeviceAccount*/ false);
78
79        final String label = EditorUiUtils.getAccountHeaderLabelForMyProfile(getContext(),
80                account);
81
82        // My LunkedIn profile
83        final String expected = getContext()
84                .getString(R.string.external_profile_title, DISPLAY_LABEL);
85        assertEquals(expected, label);
86    }
87
88
89    public void testGetProfileAccountInfo_DeviceLocalAccount() {
90        final AccountDisplayInfo account = new AccountDisplayInfo(ACCOUNT, "Device",
91                "Device", null, true);
92
93        final String label = EditorUiUtils.getAccountHeaderLabelForMyProfile(getContext(),
94                account);
95
96        // "My local profile"
97        final String expected = getContext().getString(R.string.local_profile_title);
98        assertEquals(expected, label);
99    }
100
101    public void testGetAccountInfo_AccountType_NonGoogle() {
102        final AccountDisplayInfo account = new AccountDisplayInfo(ACCOUNT, ACCOUNT_NAME,
103                DISPLAY_LABEL, /*icon*/ null, /*isDeviceAccount*/ false);
104
105        final String label = EditorUiUtils.getAccountTypeHeaderLabel(getContext(), account);
106
107        // LunkedIn Contact
108        final String expected = getContext().getString(R.string.account_type_format, DISPLAY_LABEL);
109        assertEquals(expected, label);
110    }
111
112    public void testGetAccountInfo_AccountType_Google() {
113        final AccountDisplayInfo account = new AccountDisplayInfo(GOOGLE_ACCOUNT, ACCOUNT_NAME,
114                GOOGLE_DISPLAY_LABEL, /*icon*/ null, /*isDeviceAccount*/ false);
115
116        final String label = EditorUiUtils.getAccountTypeHeaderLabel(getContext(), account);
117
118        // Google Account
119        final String expected = getContext().getString(R.string.google_account_type_format,
120                GOOGLE_DISPLAY_LABEL);
121        assertEquals(expected, label);
122    }
123
124  public void testGetAccountInfo_AccountType_DeviceAccount() {
125      final AccountWithDataSet deviceAccount = AccountWithDataSet.getNullAccount();
126      final AccountDisplayInfo account = new AccountDisplayInfo(deviceAccount, "Device",
127              "Device", /*icon*/ null, /*isDeviceAccount*/ true);
128
129      final String label = EditorUiUtils.getAccountTypeHeaderLabel(getContext(), account);
130
131      // "Device"
132      final String expected = getContext().getString(R.string.account_phone);
133      assertEquals(expected, label);
134    }
135
136    public void testGetRingtongStrFromUri_lessThanOrEqualsToM() {
137        final int currentVersion = Build.VERSION_CODES.M;
138        assertNull(EditorUiUtils.getRingtoneStringFromUri(null, currentVersion));
139        assertNull(EditorUiUtils.getRingtoneStringFromUri(Settings.System.DEFAULT_RINGTONE_URI,
140                currentVersion));
141        assertEquals(RINGTONE, EditorUiUtils.getRingtoneStringFromUri(Uri.parse(RINGTONE),
142                        currentVersion));
143    }
144
145    public void testGetRingtongStrFromUri_nOrGreater() {
146        final int currentVersion = Build.VERSION_CODES.M + 1;
147        assertEquals("", EditorUiUtils.getRingtoneStringFromUri(null, currentVersion));
148        assertNull(EditorUiUtils.getRingtoneStringFromUri(Settings.System.DEFAULT_RINGTONE_URI,
149                currentVersion));
150        assertEquals(RINGTONE, EditorUiUtils.getRingtoneStringFromUri(Uri.parse(RINGTONE),
151                        currentVersion));
152    }
153
154    public void testGetRingtongUriFromStr_lessThanOrEqualsToM() {
155        final int currentVersion = Build.VERSION_CODES.M;
156        assertEquals(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE), EditorUiUtils
157                        .getRingtoneUriFromString(null, currentVersion));
158        assertEquals(Uri.parse(""), EditorUiUtils.getRingtoneUriFromString("", currentVersion));
159        assertEquals(Uri.parse(RINGTONE), EditorUiUtils.getRingtoneUriFromString(RINGTONE,
160                currentVersion));
161    }
162
163    public void testGetRingtongUriFromStr_nOrGreater() {
164        final int currentVersion = Build.VERSION_CODES.M + 1;
165        assertEquals(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE), EditorUiUtils
166                        .getRingtoneUriFromString(null, currentVersion));
167        assertNull(EditorUiUtils.getRingtoneUriFromString("", currentVersion));
168        assertEquals(Uri.parse(RINGTONE), EditorUiUtils.getRingtoneUriFromString(RINGTONE,
169                currentVersion));
170    }
171
172    private AccountDisplayInfo createDisplayableAccount() {
173        return new AccountDisplayInfo(ACCOUNT, ACCOUNT_NAME, DISPLAY_LABEL, null, false);
174    }
175
176}
177