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