1/*
2 * Copyright (C) 2010 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;
18
19import com.android.contacts.calllog.CallTypeHelper;
20import com.android.contacts.calllog.PhoneNumberHelper;
21import com.android.contacts.calllog.TestPhoneNumberHelper;
22import com.android.contacts.util.LocaleTestUtils;
23import com.android.internal.telephony.CallerInfo;
24
25import android.content.Context;
26import android.content.res.Resources;
27import android.provider.CallLog.Calls;
28import android.test.AndroidTestCase;
29import android.text.Html;
30import android.text.Spanned;
31import android.view.View;
32import android.widget.TextView;
33
34import java.util.GregorianCalendar;
35import java.util.Locale;
36
37/**
38 * Unit tests for {@link PhoneCallDetailsHelper}.
39 */
40public class PhoneCallDetailsHelperTest extends AndroidTestCase {
41    /** The number to be used to access the voicemail. */
42    private static final String TEST_VOICEMAIL_NUMBER = "125";
43    /** The date of the call log entry. */
44    private static final long TEST_DATE =
45        new GregorianCalendar(2011, 5, 3, 13, 0, 0).getTimeInMillis();
46    /** A test duration value for phone calls. */
47    private static final long TEST_DURATION = 62300;
48    /** The number of the caller/callee in the log entry. */
49    private static final String TEST_NUMBER = "14125555555";
50    /** The formatted version of {@link #TEST_NUMBER}. */
51    private static final String TEST_FORMATTED_NUMBER = "1-412-255-5555";
52    /** The country ISO name used in the tests. */
53    private static final String TEST_COUNTRY_ISO = "US";
54    /** The geocoded location used in the tests. */
55    private static final String TEST_GEOCODE = "United States";
56
57    /** The object under test. */
58    private PhoneCallDetailsHelper mHelper;
59    /** The views to fill. */
60    private PhoneCallDetailsViews mViews;
61    private TextView mNameView;
62    private PhoneNumberHelper mPhoneNumberHelper;
63    private LocaleTestUtils mLocaleTestUtils;
64
65    @Override
66    protected void setUp() throws Exception {
67        super.setUp();
68        Context context = getContext();
69        Resources resources = context.getResources();
70        CallTypeHelper callTypeHelper = new CallTypeHelper(resources);
71        mPhoneNumberHelper = new TestPhoneNumberHelper(resources, TEST_VOICEMAIL_NUMBER);
72        mHelper = new PhoneCallDetailsHelper(resources, callTypeHelper, mPhoneNumberHelper);
73        mHelper.setCurrentTimeForTest(
74                new GregorianCalendar(2011, 5, 4, 13, 0, 0).getTimeInMillis());
75        mViews = PhoneCallDetailsViews.createForTest(context);
76        mNameView = new TextView(context);
77        mLocaleTestUtils = new LocaleTestUtils(getContext());
78        mLocaleTestUtils.setLocale(Locale.US);
79    }
80
81    @Override
82    protected void tearDown() throws Exception {
83        mLocaleTestUtils.restoreLocale();
84        mNameView = null;
85        mViews = null;
86        mHelper = null;
87        mPhoneNumberHelper = null;
88        super.tearDown();
89    }
90
91    public void testSetPhoneCallDetails_Unknown() {
92        setPhoneCallDetailsWithNumber(CallerInfo.UNKNOWN_NUMBER, CallerInfo.UNKNOWN_NUMBER);
93        assertNameEqualsResource(R.string.unknown);
94    }
95
96    public void testSetPhoneCallDetails_Private() {
97        setPhoneCallDetailsWithNumber(CallerInfo.PRIVATE_NUMBER, CallerInfo.PRIVATE_NUMBER);
98        assertNameEqualsResource(R.string.private_num);
99    }
100
101    public void testSetPhoneCallDetails_Payphone() {
102        setPhoneCallDetailsWithNumber(CallerInfo.PAYPHONE_NUMBER, CallerInfo.PAYPHONE_NUMBER);
103        assertNameEqualsResource(R.string.payphone);
104    }
105
106    public void testSetPhoneCallDetails_Voicemail() {
107        setPhoneCallDetailsWithNumber(TEST_VOICEMAIL_NUMBER, TEST_VOICEMAIL_NUMBER);
108        assertNameEqualsResource(R.string.voicemail);
109    }
110
111    public void testSetPhoneCallDetails_Normal() {
112        setPhoneCallDetailsWithNumber("14125551212", "1-412-555-1212");
113        assertEquals("yesterday", mViews.callTypeAndDate.getText().toString());
114        assertEqualsHtml("<font color='#33b5e5'><b>yesterday</b></font>",
115                mViews.callTypeAndDate.getText());
116    }
117
118    /** Asserts that a char sequence is actually a Spanned corresponding to the expected HTML. */
119    private void assertEqualsHtml(String expectedHtml, CharSequence actualText) {
120        // In order to contain HTML, the text should actually be a Spanned.
121        assertTrue(actualText instanceof Spanned);
122        Spanned actualSpanned = (Spanned) actualText;
123        // Convert from and to HTML to take care of alternative formatting of HTML.
124        assertEquals(Html.toHtml(Html.fromHtml(expectedHtml)), Html.toHtml(actualSpanned));
125
126    }
127
128    public void testSetPhoneCallDetails_Date() {
129        mHelper.setCurrentTimeForTest(
130                new GregorianCalendar(2011, 5, 3, 13, 0, 0).getTimeInMillis());
131
132        setPhoneCallDetailsWithDate(
133                new GregorianCalendar(2011, 5, 3, 13, 0, 0).getTimeInMillis());
134        assertDateEquals("0 mins ago");
135
136        setPhoneCallDetailsWithDate(
137                new GregorianCalendar(2011, 5, 3, 12, 0, 0).getTimeInMillis());
138        assertDateEquals("1 hour ago");
139
140        setPhoneCallDetailsWithDate(
141                new GregorianCalendar(2011, 5, 2, 13, 0, 0).getTimeInMillis());
142        assertDateEquals("yesterday");
143
144        setPhoneCallDetailsWithDate(
145                new GregorianCalendar(2011, 5, 1, 13, 0, 0).getTimeInMillis());
146        assertDateEquals("2 days ago");
147    }
148
149    public void testSetPhoneCallDetails_CallTypeIcons() {
150        setPhoneCallDetailsWithCallTypeIcons(Calls.INCOMING_TYPE);
151        assertCallTypeIconsEquals(Calls.INCOMING_TYPE);
152
153        setPhoneCallDetailsWithCallTypeIcons(Calls.OUTGOING_TYPE);
154        assertCallTypeIconsEquals(Calls.OUTGOING_TYPE);
155
156        setPhoneCallDetailsWithCallTypeIcons(Calls.MISSED_TYPE);
157        assertCallTypeIconsEquals(Calls.MISSED_TYPE);
158
159        setPhoneCallDetailsWithCallTypeIcons(Calls.VOICEMAIL_TYPE);
160        assertCallTypeIconsEquals(Calls.VOICEMAIL_TYPE);
161    }
162
163    public void testSetPhoneCallDetails_MultipleCallTypeIcons() {
164        setPhoneCallDetailsWithCallTypeIcons(Calls.INCOMING_TYPE, Calls.OUTGOING_TYPE);
165        assertCallTypeIconsEquals(Calls.INCOMING_TYPE, Calls.OUTGOING_TYPE);
166
167        setPhoneCallDetailsWithCallTypeIcons(Calls.MISSED_TYPE, Calls.MISSED_TYPE);
168        assertCallTypeIconsEquals(Calls.MISSED_TYPE, Calls.MISSED_TYPE);
169    }
170
171    public void testSetPhoneCallDetails_MultipleCallTypeIconsLastOneDropped() {
172        setPhoneCallDetailsWithCallTypeIcons(Calls.MISSED_TYPE, Calls.MISSED_TYPE,
173                Calls.INCOMING_TYPE, Calls.OUTGOING_TYPE);
174        assertCallTypeIconsEqualsPlusOverflow("(4)",
175                Calls.MISSED_TYPE, Calls.MISSED_TYPE, Calls.INCOMING_TYPE);
176    }
177
178    public void testSetPhoneCallDetails_Geocode() {
179        setPhoneCallDetailsWithNumberAndGeocode("+14125555555", "1-412-555-5555", "Pennsylvania");
180        assertNameEquals("1-412-555-5555");  // The phone number is shown as the name.
181        assertNumberEquals("Pennsylvania");  // The geocode is shown as the number.
182    }
183
184    public void testSetPhoneCallDetails_NoGeocode() {
185        setPhoneCallDetailsWithNumberAndGeocode("+14125555555", "1-412-555-5555", null);
186        assertNameEquals("1-412-555-5555");  // The phone number is shown as the name.
187        assertNumberEquals("-");  // The empty geocode is shown as the number.
188    }
189
190    public void testSetPhoneCallDetails_EmptyGeocode() {
191        setPhoneCallDetailsWithNumberAndGeocode("+14125555555", "1-412-555-5555", "");
192        assertNameEquals("1-412-555-5555");  // The phone number is shown as the name.
193        assertNumberEquals("-");  // The empty geocode is shown as the number.
194    }
195
196    public void testSetPhoneCallDetails_NoGeocodeForVoicemail() {
197        setPhoneCallDetailsWithNumberAndGeocode(TEST_VOICEMAIL_NUMBER, "", "United States");
198        assertNumberEquals("-");  // The empty geocode is shown as the number.
199    }
200
201    public void testSetPhoneCallDetails_Highlighted() {
202        setPhoneCallDetailsWithNumber(TEST_VOICEMAIL_NUMBER, "");
203    }
204
205    public void testSetCallDetailsHeader_NumberOnly() {
206        setCallDetailsHeaderWithNumberOnly(TEST_NUMBER);
207        assertEquals(View.VISIBLE, mNameView.getVisibility());
208        assertEquals("Add to contacts", mNameView.getText().toString());
209    }
210
211    public void testSetCallDetailsHeader_UnknownNumber() {
212        setCallDetailsHeaderWithNumberOnly(CallerInfo.UNKNOWN_NUMBER);
213        assertEquals(View.VISIBLE, mNameView.getVisibility());
214        assertEquals("Unknown", mNameView.getText().toString());
215    }
216
217    public void testSetCallDetailsHeader_PrivateNumber() {
218        setCallDetailsHeaderWithNumberOnly(CallerInfo.PRIVATE_NUMBER);
219        assertEquals(View.VISIBLE, mNameView.getVisibility());
220        assertEquals("Private number", mNameView.getText().toString());
221    }
222
223    public void testSetCallDetailsHeader_PayphoneNumber() {
224        setCallDetailsHeaderWithNumberOnly(CallerInfo.PAYPHONE_NUMBER);
225        assertEquals(View.VISIBLE, mNameView.getVisibility());
226        assertEquals("Pay phone", mNameView.getText().toString());
227    }
228
229    public void testSetCallDetailsHeader_VoicemailNumber() {
230        setCallDetailsHeaderWithNumberOnly(TEST_VOICEMAIL_NUMBER);
231        assertEquals(View.VISIBLE, mNameView.getVisibility());
232        assertEquals("Voicemail", mNameView.getText().toString());
233    }
234
235    public void testSetCallDetailsHeader() {
236        setCallDetailsHeader("John Doe");
237        assertEquals(View.VISIBLE, mNameView.getVisibility());
238        assertEquals("John Doe", mNameView.getText().toString());
239    }
240
241    /** Asserts that the name text field contains the value of the given string resource. */
242    private void assertNameEqualsResource(int resId) {
243        assertNameEquals(getContext().getString(resId));
244    }
245
246    /** Asserts that the name text field contains the given string value. */
247    private void assertNameEquals(String text) {
248        assertEquals(text, mViews.nameView.getText().toString());
249    }
250
251    /** Asserts that the number text field contains the given string value. */
252    private void assertNumberEquals(String text) {
253        assertEquals(text, mViews.numberView.getText().toString());
254    }
255
256    /** Asserts that the date text field contains the given string value. */
257    private void assertDateEquals(String text) {
258        assertEquals(text, mViews.callTypeAndDate.getText().toString());
259    }
260
261    /** Asserts that the call type contains the images with the given drawables. */
262    private void assertCallTypeIconsEquals(int... ids) {
263        assertEquals(ids.length, mViews.callTypeIcons.getCount());
264        for (int index = 0; index < ids.length; ++index) {
265            int id = ids[index];
266            assertEquals(id, mViews.callTypeIcons.getCallType(index));
267        }
268        assertEquals(View.VISIBLE, mViews.callTypeIcons.getVisibility());
269        assertEquals("yesterday", mViews.callTypeAndDate.getText().toString());
270    }
271
272    /**
273     * Asserts that the call type contains the images with the given drawables and shows the given
274     * text next to the icons.
275     */
276    private void assertCallTypeIconsEqualsPlusOverflow(String overflowText, int... ids) {
277        assertEquals(ids.length, mViews.callTypeIcons.getCount());
278        for (int index = 0; index < ids.length; ++index) {
279            int id = ids[index];
280            assertEquals(id, mViews.callTypeIcons.getCallType(index));
281        }
282        assertEquals(View.VISIBLE, mViews.callTypeIcons.getVisibility());
283        assertEquals(overflowText + " yesterday", mViews.callTypeAndDate.getText().toString());
284    }
285
286    /** Sets the phone call details with default values and the given number. */
287    private void setPhoneCallDetailsWithNumber(String number, String formattedNumber) {
288        setPhoneCallDetailsWithNumberAndGeocode(number, formattedNumber, TEST_GEOCODE);
289    }
290
291    /** Sets the phone call details with default values and the given number. */
292    private void setPhoneCallDetailsWithNumberAndGeocode(String number, String formattedNumber,
293            String geocodedLocation) {
294        mHelper.setPhoneCallDetails(mViews,
295                new PhoneCallDetails(number, formattedNumber, TEST_COUNTRY_ISO, geocodedLocation,
296                        new int[]{ Calls.VOICEMAIL_TYPE }, TEST_DATE, TEST_DURATION),
297                true);
298    }
299
300    /** Sets the phone call details with default values and the given date. */
301    private void setPhoneCallDetailsWithDate(long date) {
302        mHelper.setPhoneCallDetails(mViews,
303                new PhoneCallDetails(TEST_NUMBER, TEST_FORMATTED_NUMBER, TEST_COUNTRY_ISO,
304                        TEST_GEOCODE, new int[]{ Calls.INCOMING_TYPE }, date, TEST_DURATION),
305                false);
306    }
307
308    /** Sets the phone call details with default values and the given call types using icons. */
309    private void setPhoneCallDetailsWithCallTypeIcons(int... callTypes) {
310        mHelper.setPhoneCallDetails(mViews,
311                new PhoneCallDetails(TEST_NUMBER, TEST_FORMATTED_NUMBER, TEST_COUNTRY_ISO,
312                        TEST_GEOCODE, callTypes, TEST_DATE, TEST_DURATION),
313                false);
314    }
315
316    private void setCallDetailsHeaderWithNumberOnly(String number) {
317        mHelper.setCallDetailsHeader(mNameView,
318                new PhoneCallDetails(number, TEST_FORMATTED_NUMBER, TEST_COUNTRY_ISO,
319                        TEST_GEOCODE, new int[]{ Calls.INCOMING_TYPE }, TEST_DATE, TEST_DURATION));
320    }
321
322    private void setCallDetailsHeader(String name) {
323        mHelper.setCallDetailsHeader(mNameView,
324                new PhoneCallDetails(TEST_NUMBER, TEST_FORMATTED_NUMBER, TEST_COUNTRY_ISO,
325                        TEST_GEOCODE, new int[]{ Calls.INCOMING_TYPE }, TEST_DATE, TEST_DURATION,
326                        name, 0, "", null, null));
327    }
328}
329