blob: aa3f725ab038cb70fdaf4b12c9d79f51c1325c7e [file] [log] [blame]
Walter Jangdefc69c2015-08-25 16:46:43 -07001/*
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
Walter Jangdefc69c2015-08-25 16:46:43 -070019import android.content.Context;
Wenyi Wang3daa9a32015-09-25 09:59:06 -070020import android.media.RingtoneManager;
21import android.net.Uri;
22import android.os.Build;
23import android.provider.Settings;
Walter Jangdefc69c2015-08-25 16:46:43 -070024import android.test.AndroidTestCase;
25import android.test.suitebuilder.annotation.SmallTest;
Marcus Hagerottfac695a2016-08-24 17:02:40 -070026
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;
Walter Jangdefc69c2015-08-25 16:46:43 -070031
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
Wenyi Wang3daa9a32015-09-25 09:59:06 -070044 private static final String RINGTONE = "content://media/external/audio/media/31";
45
Marcus Hagerottfac695a2016-08-24 17:02:40 -070046 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
Walter Jangdefc69c2015-08-25 16:46:43 -070051 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
Marcus Hagerottfac695a2016-08-24 17:02:40 -070075 public void testGetProfileAccountInfo_NonLocalAccount() {
76 final AccountDisplayInfo account = new AccountDisplayInfo(ACCOUNT, ACCOUNT_NAME,
77 DISPLAY_LABEL, /*icon*/ null, /*isDeviceAccount*/ false);
Walter Jangdefc69c2015-08-25 16:46:43 -070078
Marcus Hagerottfac695a2016-08-24 17:02:40 -070079 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);
Walter Jangdefc69c2015-08-25 16:46:43 -070086 }
87
Walter Jangdefc69c2015-08-25 16:46:43 -070088
Marcus Hagerottfac695a2016-08-24 17:02:40 -070089 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);
Walter Jangdefc69c2015-08-25 16:46:43 -070099 }
100
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700101 public void testGetAccountInfo_AccountType_NonGoogle() {
102 final AccountDisplayInfo account = new AccountDisplayInfo(ACCOUNT, ACCOUNT_NAME,
103 DISPLAY_LABEL, /*icon*/ null, /*isDeviceAccount*/ false);
Walter Jangdefc69c2015-08-25 16:46:43 -0700104
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700105 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);
Walter Jangdefc69c2015-08-25 16:46:43 -0700110 }
111
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700112 public void testGetAccountInfo_AccountType_Google() {
113 final AccountDisplayInfo account = new AccountDisplayInfo(GOOGLE_ACCOUNT, ACCOUNT_NAME,
114 GOOGLE_DISPLAY_LABEL, /*icon*/ null, /*isDeviceAccount*/ false);
Walter Jangdefc69c2015-08-25 16:46:43 -0700115
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700116 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);
Walter Jangdefc69c2015-08-25 16:46:43 -0700122 }
123
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700124 public void testGetAccountInfo_AccountType_DeviceAccount() {
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700125 final AccountWithDataSet deviceAccount = AccountWithDataSet.getNullAccount();
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700126 final AccountDisplayInfo account = new AccountDisplayInfo(deviceAccount, "Device",
127 "Device", /*icon*/ null, /*isDeviceAccount*/ true);
Walter Jangdefc69c2015-08-25 16:46:43 -0700128
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700129 final String label = EditorUiUtils.getAccountTypeHeaderLabel(getContext(), account);
Walter Jangdefc69c2015-08-25 16:46:43 -0700130
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700131 // "Device"
132 final String expected = getContext().getString(R.string.account_phone);
133 assertEquals(expected, label);
Walter Jangdefc69c2015-08-25 16:46:43 -0700134 }
Wenyi Wang3daa9a32015-09-25 09:59:06 -0700135
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
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700172 private AccountDisplayInfo createDisplayableAccount() {
173 return new AccountDisplayInfo(ACCOUNT, ACCOUNT_NAME, DISPLAY_LABEL, null, false);
174 }
175
Walter Jangdefc69c2015-08-25 16:46:43 -0700176}