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