PhoneCallDetailsHelperTest.java revision 223a843a7290517eccf340817cdae424658dd0da
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.util.LocaleTestUtils;
20import com.android.internal.telephony.CallerInfo;
21
22import android.content.Context;
23import android.graphics.Color;
24import android.graphics.drawable.ColorDrawable;
25import android.graphics.drawable.Drawable;
26import android.provider.CallLog.Calls;
27import android.test.AndroidTestCase;
28import android.view.View;
29import android.widget.ImageView;
30import android.widget.LinearLayout;
31import android.widget.TextView;
32
33import java.util.GregorianCalendar;
34import java.util.Locale;
35
36/**
37 * Unit tests for {@link PhoneCallDetailsHelper}.
38 */
39public class PhoneCallDetailsHelperTest extends AndroidTestCase {
40    /** The number to be used to access the voicemail. */
41    private static final String TEST_VOICEMAIL_NUMBER = "125";
42    /** The date of the call log entry. */
43    private static final long TEST_DATE = 1300000000;
44    /** The number of the caller/callee in the log entry. */
45    private static final String TEST_NUMBER = "14125555555";
46    /** The formatted version of {@link #TEST_NUMBER}. */
47    private static final String TEST_FORMATTED_NUMBER = "1-412-255-5555";
48    /** A drawable to be used for incoming calls. */
49    private static final Drawable TEST_INCOMING_DRAWABLE = new ColorDrawable(Color.BLACK);
50    /** A drawable to be used for outgoing calls. */
51    private static final Drawable TEST_OUTGOING_DRAWABLE = new ColorDrawable(Color.BLUE);
52    /** A drawable to be used for missed calls. */
53    private static final Drawable TEST_MISSED_DRAWABLE = new ColorDrawable(Color.RED);
54    /** A drawable to be used for voicemails. */
55    private static final Drawable TEST_VOICEMAIL_DRAWABLE = new ColorDrawable(Color.CYAN);
56
57    /** The object under test. */
58    private PhoneCallDetailsHelper mHelper;
59    /** The views to fill. */
60    private PhoneCallDetailsViews mViews;
61
62    @Override
63    protected void setUp() throws Exception {
64        super.setUp();
65        Context context = getContext();
66        mHelper = new PhoneCallDetailsHelper(context, context.getResources(),
67                TEST_VOICEMAIL_NUMBER, TEST_INCOMING_DRAWABLE, TEST_OUTGOING_DRAWABLE,
68                TEST_MISSED_DRAWABLE, TEST_VOICEMAIL_DRAWABLE);
69        mViews = PhoneCallDetailsViews.createForTest(new TextView(context),
70                new LinearLayout(context), new TextView(context), new View(context),
71                new TextView(context), new TextView(context));
72    }
73
74    @Override
75    protected void tearDown() throws Exception {
76        mViews = null;
77        mHelper = null;
78        super.tearDown();
79    }
80
81    public void testSetPhoneCallDetails_Unknown() {
82        setPhoneCallDetailsWithNumber(CallerInfo.UNKNOWN_NUMBER, CallerInfo.UNKNOWN_NUMBER);
83        assertNameEqualsResource(R.string.unknown);
84    }
85
86    public void testSetPhoneCallDetails_Private() {
87        setPhoneCallDetailsWithNumber(CallerInfo.PRIVATE_NUMBER, CallerInfo.PRIVATE_NUMBER);
88        assertNameEqualsResource(R.string.private_num);
89    }
90
91    public void testSetPhoneCallDetails_Payphone() {
92        setPhoneCallDetailsWithNumber(CallerInfo.PAYPHONE_NUMBER, CallerInfo.PAYPHONE_NUMBER);
93        assertNameEqualsResource(R.string.payphone);
94    }
95
96    public void testSetPhoneCallDetails_Voicemail() {
97        setPhoneCallDetailsWithNumber(TEST_VOICEMAIL_NUMBER, TEST_VOICEMAIL_NUMBER);
98        assertNameEqualsResource(R.string.voicemail);
99    }
100
101    public void testSetPhoneCallDetails_Normal() {
102        setPhoneCallDetailsWithNumber("14125551212", "1-412-555-1212");
103        assertNameEquals("1-412-555-1212");
104    }
105
106    public void testSetPhoneCallDetails_Date() {
107        LocaleTestUtils localeTestUtils = new LocaleTestUtils(getContext());
108        localeTestUtils.setLocale(Locale.US);
109        try {
110            mHelper.setCurrentTimeForTest(
111                    new GregorianCalendar(2011, 5, 3, 13, 0, 0).getTimeInMillis());
112
113            setPhoneCallDetailsWithDate(
114                    new GregorianCalendar(2011, 5, 3, 13, 0, 0).getTimeInMillis());
115            assertDateEquals("0 mins ago");
116
117            setPhoneCallDetailsWithDate(
118                    new GregorianCalendar(2011, 5, 3, 12, 0, 0).getTimeInMillis());
119            assertDateEquals("1 hour ago");
120
121            setPhoneCallDetailsWithDate(
122                    new GregorianCalendar(2011, 5, 2, 13, 0, 0).getTimeInMillis());
123            assertDateEquals("yesterday");
124
125            setPhoneCallDetailsWithDate(
126                    new GregorianCalendar(2011, 5, 1, 13, 0, 0).getTimeInMillis());
127            assertDateEquals("2 days ago");
128        } finally {
129            localeTestUtils.restoreLocale();
130        }
131    }
132
133    public void testSetPhoneCallDetails_CallTypeIcons() {
134        setPhoneCallDetailsWithCallTypeIcons(Calls.INCOMING_TYPE);
135        assertCallTypeIconsEquals(TEST_INCOMING_DRAWABLE);
136
137        setPhoneCallDetailsWithCallTypeIcons(Calls.OUTGOING_TYPE);
138        assertCallTypeIconsEquals(TEST_OUTGOING_DRAWABLE);
139
140        setPhoneCallDetailsWithCallTypeIcons(Calls.MISSED_TYPE);
141        assertCallTypeIconsEquals(TEST_MISSED_DRAWABLE);
142
143        setPhoneCallDetailsWithCallTypeIcons(Calls.VOICEMAIL_TYPE);
144        assertCallTypeIconsEquals(TEST_VOICEMAIL_DRAWABLE);
145    }
146
147    public void testSetPhoneCallDetails_CallTypeText() {
148        LocaleTestUtils localeTestUtils = new LocaleTestUtils(getContext());
149        localeTestUtils.setLocale(Locale.US);
150        try {
151            setPhoneCallDetailsWithCallTypeText(Calls.INCOMING_TYPE);
152            assertCallTypeTextEquals("Incoming call");
153
154            setPhoneCallDetailsWithCallTypeText(Calls.OUTGOING_TYPE);
155            assertCallTypeTextEquals("Outgoing call");
156
157            setPhoneCallDetailsWithCallTypeText(Calls.MISSED_TYPE);
158            assertCallTypeTextEquals("Missed call");
159
160            setPhoneCallDetailsWithCallTypeText(Calls.VOICEMAIL_TYPE);
161            assertCallTypeTextEquals("Voicemail");
162        } finally {
163            localeTestUtils.restoreLocale();
164        }
165    }
166
167    /** Asserts that the name text field contains the value of the given string resource. */
168    private void assertNameEqualsResource(int resId) {
169        assertNameEquals(getContext().getString(resId));
170    }
171
172    /** Asserts that the name text field contains the given string value. */
173    private void assertNameEquals(String text) {
174        assertEquals(text, mViews.nameView.getText().toString());
175    }
176
177    /** Asserts that the date text field contains the given string value. */
178    private void assertDateEquals(String text) {
179        assertEquals(text, mViews.dateView.getText().toString());
180    }
181
182    /** Asserts that the call type contains the images with the given drawables. */
183    private void assertCallTypeIconsEquals(Drawable... drawables) {
184        assertEquals(drawables.length, mViews.callTypeIcons.getChildCount());
185        for (int index = 0; index < drawables.length; ++index) {
186            Drawable drawable = drawables[index];
187            ImageView imageView = (ImageView) mViews.callTypeIcons.getChildAt(index);
188            assertEquals(drawable, imageView.getDrawable());
189        }
190        assertEquals(View.VISIBLE, mViews.callTypeIcons.getVisibility());
191        assertEquals(View.GONE, mViews.callTypeText.getVisibility());
192        assertEquals(View.GONE, mViews.callTypeSeparator.getVisibility());
193    }
194
195    /** Asserts that the call type contains the given text. */
196    private void assertCallTypeTextEquals(String text) {
197        assertEquals(text, mViews.callTypeText.getText().toString());
198        assertEquals(View.GONE, mViews.callTypeIcons.getVisibility());
199        assertEquals(View.VISIBLE, mViews.callTypeText.getVisibility());
200        assertEquals(View.VISIBLE, mViews.callTypeSeparator.getVisibility());
201    }
202
203    /** Sets the phone call details with default values and the given number. */
204    private void setPhoneCallDetailsWithNumber(String number, String formattedNumber) {
205        mHelper.setPhoneCallDetails(mViews,
206                new PhoneCallDetails(number, formattedNumber, new int[]{ Calls.INCOMING_TYPE },
207                        TEST_DATE),
208                false);
209    }
210
211    /** Sets the phone call details with default values and the given date. */
212    private void setPhoneCallDetailsWithDate(long date) {
213        mHelper.setPhoneCallDetails(mViews,
214                new PhoneCallDetails(TEST_NUMBER, TEST_FORMATTED_NUMBER,
215                        new int[]{ Calls.INCOMING_TYPE }, date),
216                false);
217    }
218
219    /** Sets the phone call details with default values and the given call types using icons. */
220    private void setPhoneCallDetailsWithCallTypeIcons(int... callTypes) {
221        setPhoneCallDetailsWithCallTypes(true, callTypes);
222    }
223
224    /** Sets the phone call details with default values and the given call types using text. */
225    private void setPhoneCallDetailsWithCallTypeText(int... callTypes) {
226        setPhoneCallDetailsWithCallTypes(false, callTypes);
227    }
228
229    private void setPhoneCallDetailsWithCallTypes(boolean useIcons, int... callTypes) {
230        mHelper.setPhoneCallDetails(mViews,
231                new PhoneCallDetails(TEST_NUMBER, TEST_FORMATTED_NUMBER, callTypes, TEST_DATE),
232                useIcons);
233    }
234}
235