Walter Jang | defc69c | 2015-08-25 16:46:43 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.contacts.editor; |
| 18 | |
Walter Jang | defc69c | 2015-08-25 16:46:43 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Wenyi Wang | 3daa9a3 | 2015-09-25 09:59:06 -0700 | [diff] [blame] | 20 | import android.media.RingtoneManager; |
| 21 | import android.net.Uri; |
| 22 | import android.os.Build; |
| 23 | import android.provider.Settings; |
Walter Jang | defc69c | 2015-08-25 16:46:43 -0700 | [diff] [blame] | 24 | import android.test.AndroidTestCase; |
| 25 | import android.test.suitebuilder.annotation.SmallTest; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 26 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 27 | import com.android.contacts.R; |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 28 | import com.android.contacts.model.account.AccountDisplayInfo; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 29 | import com.android.contacts.model.account.AccountType; |
| 30 | import com.android.contacts.model.account.AccountWithDataSet; |
Walter Jang | defc69c | 2015-08-25 16:46:43 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Tests {@link EditorUiUtils}. |
| 34 | */ |
| 35 | @SmallTest |
| 36 | public 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 Wang | 3daa9a3 | 2015-09-25 09:59:06 -0700 | [diff] [blame] | 44 | private static final String RINGTONE = "content://media/external/audio/media/31"; |
| 45 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 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 | |
Walter Jang | defc69c | 2015-08-25 16:46:43 -0700 | [diff] [blame] | 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 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 75 | public void testGetProfileAccountInfo_NonLocalAccount() { |
| 76 | final AccountDisplayInfo account = new AccountDisplayInfo(ACCOUNT, ACCOUNT_NAME, |
| 77 | DISPLAY_LABEL, /*icon*/ null, /*isDeviceAccount*/ false); |
Walter Jang | defc69c | 2015-08-25 16:46:43 -0700 | [diff] [blame] | 78 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 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); |
Walter Jang | defc69c | 2015-08-25 16:46:43 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Walter Jang | defc69c | 2015-08-25 16:46:43 -0700 | [diff] [blame] | 88 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 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); |
Walter Jang | defc69c | 2015-08-25 16:46:43 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 101 | public void testGetAccountInfo_AccountType_NonGoogle() { |
| 102 | final AccountDisplayInfo account = new AccountDisplayInfo(ACCOUNT, ACCOUNT_NAME, |
| 103 | DISPLAY_LABEL, /*icon*/ null, /*isDeviceAccount*/ false); |
Walter Jang | defc69c | 2015-08-25 16:46:43 -0700 | [diff] [blame] | 104 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 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); |
Walter Jang | defc69c | 2015-08-25 16:46:43 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 112 | public void testGetAccountInfo_AccountType_Google() { |
| 113 | final AccountDisplayInfo account = new AccountDisplayInfo(GOOGLE_ACCOUNT, ACCOUNT_NAME, |
| 114 | GOOGLE_DISPLAY_LABEL, /*icon*/ null, /*isDeviceAccount*/ false); |
Walter Jang | defc69c | 2015-08-25 16:46:43 -0700 | [diff] [blame] | 115 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 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); |
Walter Jang | defc69c | 2015-08-25 16:46:43 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 124 | public void testGetAccountInfo_AccountType_DeviceAccount() { |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 125 | final AccountWithDataSet deviceAccount = AccountWithDataSet.getNullAccount(); |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 126 | final AccountDisplayInfo account = new AccountDisplayInfo(deviceAccount, "Device", |
| 127 | "Device", /*icon*/ null, /*isDeviceAccount*/ true); |
Walter Jang | defc69c | 2015-08-25 16:46:43 -0700 | [diff] [blame] | 128 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 129 | final String label = EditorUiUtils.getAccountTypeHeaderLabel(getContext(), account); |
Walter Jang | defc69c | 2015-08-25 16:46:43 -0700 | [diff] [blame] | 130 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 131 | // "Device" |
| 132 | final String expected = getContext().getString(R.string.account_phone); |
| 133 | assertEquals(expected, label); |
Walter Jang | defc69c | 2015-08-25 16:46:43 -0700 | [diff] [blame] | 134 | } |
Wenyi Wang | 3daa9a3 | 2015-09-25 09:59:06 -0700 | [diff] [blame] | 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 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 172 | private AccountDisplayInfo createDisplayableAccount() { |
| 173 | return new AccountDisplayInfo(ACCOUNT, ACCOUNT_NAME, DISPLAY_LABEL, null, false); |
| 174 | } |
| 175 | |
Walter Jang | defc69c | 2015-08-25 16:46:43 -0700 | [diff] [blame] | 176 | } |