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 com.android.contacts.R;
20import com.android.contacts.common.model.account.AccountType;
21import com.android.contacts.common.model.account.GoogleAccountType;
22
23import android.content.Context;
24import android.media.RingtoneManager;
25import android.net.Uri;
26import android.os.Build;
27import android.provider.Settings;
28import android.test.AndroidTestCase;
29import android.test.suitebuilder.annotation.SmallTest;
30import android.util.Pair;
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 class MockAccountType extends AccountType {
47
48        private final String mDisplayLabel;
49
50        private MockAccountType(String displayLabel) {
51            mDisplayLabel = displayLabel;
52        }
53
54        @Override
55        public boolean areContactsWritable() {
56            return false;
57        }
58
59        @Override
60        public boolean isGroupMembershipEditable() {
61            return false;
62        }
63
64        @Override
65        public CharSequence getDisplayLabel(Context context) {
66            return mDisplayLabel;
67        }
68    }
69
70    public void testGetProfileAccountInfo_AccountName() {
71        final Pair pair = EditorUiUtils.getLocalAccountInfo(getContext(),
72                ACCOUNT_NAME, new MockAccountType(DISPLAY_LABEL));
73
74        assertNotNull(pair);
75        assertEquals(ACCOUNT_NAME, pair.first);
76        assertEquals(getContext().getString(R.string.external_profile_title, DISPLAY_LABEL),
77                pair.second); // My LunkedIn profile
78    }
79
80    public void testGetProfileAccountInfo_NoAccountName() {
81        final Pair pair = EditorUiUtils.getLocalAccountInfo(getContext(),
82                /* accountName =*/ null, new MockAccountType(DISPLAY_LABEL));
83
84        assertNotNull(pair);
85        assertNull(pair.first);
86        assertEquals(getContext().getString(R.string.local_profile_title),
87                pair.second); // "My local profile
88    }
89
90    public void testGetAccountInfo_AccountName_DisplayLabel() {
91        final Pair pair = EditorUiUtils.getAccountInfo(getContext(),
92                ACCOUNT_NAME, new MockAccountType(DISPLAY_LABEL));
93
94        assertNotNull(pair);
95        assertEquals(getContext().getString(R.string.from_account_format, ACCOUNT_NAME),
96                pair.first); // somebody@lunkedin.com
97        assertEquals(getContext().getString(R.string.account_type_format, DISPLAY_LABEL),
98                pair.second); // LunkedIn Contact
99    }
100
101    public void testGetAccountInfo_AccountName_DisplayLabel_GoogleAccountType() {
102        final AccountType accountType = new MockAccountType(GOOGLE_DISPLAY_LABEL);
103        accountType.accountType = GoogleAccountType.ACCOUNT_TYPE;
104        final Pair pair = EditorUiUtils.getAccountInfo(getContext(),
105                GOOGLE_ACCOUNT_NAME, accountType);
106
107        assertNotNull(pair);
108        assertEquals(getContext().getString(R.string.from_account_format, GOOGLE_ACCOUNT_NAME),
109                pair.first); // somebody@gmail.com
110        assertEquals(
111                getContext().getString(R.string.google_account_type_format, GOOGLE_DISPLAY_LABEL),
112                pair.second); // Google Account
113    }
114
115    public void testGetAccountInfo_AccountName_NoDisplayLabel() {
116        final Pair pair = EditorUiUtils.getAccountInfo(getContext(),
117                ACCOUNT_NAME, new MockAccountType(/* displayLabel =*/ null));
118
119        assertNotNull(pair);
120        assertEquals(getContext().getString(R.string.from_account_format, ACCOUNT_NAME),
121                pair.first); // somebody@lunkedin.com
122        assertEquals(
123                getContext().getString(R.string.account_type_format,
124                        getContext().getString(R.string.account_phone)),
125                pair.second); // "Phone-only, unsynced contact"
126    }
127
128    public void testGetAccountInfo_NoAccountName_DisplayLabel() {
129        final Pair pair = EditorUiUtils.getAccountInfo(getContext(),
130                /* accountName =*/ null, new MockAccountType(DISPLAY_LABEL));
131
132        assertNotNull(pair);
133        assertNull(pair.first);
134        assertEquals(getContext().getString(R.string.account_type_format, DISPLAY_LABEL),
135                pair.second); // LunkedIn contact
136    }
137
138    public void testGetAccountInfo_NoAccountName_NoDisplayLabel() {
139        final Pair pair = EditorUiUtils.getAccountInfo(getContext(),
140                /* accountName =*/ null, new MockAccountType(/* displayLabel =*/ null));
141
142        assertNotNull(pair);
143        assertNull(pair.first);
144        assertEquals(
145                getContext().getString(R.string.account_type_format,
146                        getContext().getString(R.string.account_phone)),
147                pair.second); // "Phone-only, unsynced contact"
148    }
149
150    public void testGetRingtongStrFromUri_lessThanOrEqualsToM() {
151        final int currentVersion = Build.VERSION_CODES.M;
152        assertNull(EditorUiUtils.getRingtoneStringFromUri(null, currentVersion));
153        assertNull(EditorUiUtils.getRingtoneStringFromUri(Settings.System.DEFAULT_RINGTONE_URI,
154                currentVersion));
155        assertEquals(RINGTONE, EditorUiUtils.getRingtoneStringFromUri(Uri.parse(RINGTONE),
156                        currentVersion));
157    }
158
159    public void testGetRingtongStrFromUri_nOrGreater() {
160        final int currentVersion = Build.VERSION_CODES.M + 1;
161        assertEquals("", EditorUiUtils.getRingtoneStringFromUri(null, currentVersion));
162        assertNull(EditorUiUtils.getRingtoneStringFromUri(Settings.System.DEFAULT_RINGTONE_URI,
163                currentVersion));
164        assertEquals(RINGTONE, EditorUiUtils.getRingtoneStringFromUri(Uri.parse(RINGTONE),
165                        currentVersion));
166    }
167
168    public void testGetRingtongUriFromStr_lessThanOrEqualsToM() {
169        final int currentVersion = Build.VERSION_CODES.M;
170        assertEquals(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE), EditorUiUtils
171                        .getRingtoneUriFromString(null, currentVersion));
172        assertEquals(Uri.parse(""), EditorUiUtils.getRingtoneUriFromString("", currentVersion));
173        assertEquals(Uri.parse(RINGTONE), EditorUiUtils.getRingtoneUriFromString(RINGTONE,
174                currentVersion));
175    }
176
177    public void testGetRingtongUriFromStr_nOrGreater() {
178        final int currentVersion = Build.VERSION_CODES.M + 1;
179        assertEquals(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE), EditorUiUtils
180                        .getRingtoneUriFromString(null, currentVersion));
181        assertNull(EditorUiUtils.getRingtoneUriFromString("", currentVersion));
182        assertEquals(Uri.parse(RINGTONE), EditorUiUtils.getRingtoneUriFromString(RINGTONE,
183                currentVersion));
184    }
185
186}
187