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