summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/dialer/calllog/CallLogFragmentTest.java
blob: 0c19799cc88f1e027341b2f23aa2479e52e9ed14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dialer.calllog;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.ComponentName;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.database.MatrixCursor;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.provider.CallLog.Calls;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.VoicemailContract;
import android.telephony.PhoneNumberUtils;
import android.telephony.TelephonyManager;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.MediumTest;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import com.android.contacts.common.test.FragmentTestActivity;
import com.android.dialer.CallDetailActivity;
import com.android.dialer.R;
import com.android.dialer.util.TestConstants;

import java.util.Date;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Random;

/**
 * Tests for the contact call list activity.
 *
 * Running all tests:
 *
 *   runtest contacts
 * or
 *   adb shell am instrument \
 *     -w com.android.dialer.tests/android.test.InstrumentationTestRunner
 */
@LargeTest
public class CallLogFragmentTest extends ActivityInstrumentationTestCase2<FragmentTestActivity> {
    private static final int RAND_DURATION = -1;
    private static final long NOW = -1L;

    /** A test value for the URI of a contact. */
    private static final Uri TEST_LOOKUP_URI = Uri.parse("content://contacts/2");
    /** A test value for the country ISO of the phone number in the call log. */
    private static final String TEST_COUNTRY_ISO = "US";
    /** A phone number to be used in tests. */
    private static final String TEST_NUMBER = "12125551000";
    /** The formatted version of {@link #TEST_NUMBER}. */
    private static final String TEST_FORMATTED_NUMBER = "1 212-555-1000";

    private static final String TEST_DEFAULT_CUSTOM_LABEL = "myLabel";

    /** The activity in which we are hosting the fragment. */
    private FragmentTestActivity mActivity;
    private CallLogFragment mFragment;
    private FrameLayout mParentView;
    /**
     * The adapter used by the fragment to build the rows in the call log. We use it with our own in
     * memory database.
     */
    private CallLogAdapter mAdapter;
    private String mVoicemail;

    // In memory array to hold the rows corresponding to the 'calls' table.
    private MatrixCursor mCursor;
    private int mIndex;  // Of the next row.

    private Random mRnd;

    // An item in the call list. All the methods performing checks use it.
    private CallLogListItemViewHolder mItem;

    // The list of view holderss representing the data in the DB, in reverse order from the DB.
    private CallLogListItemViewHolder[] mList;

    public CallLogFragmentTest() {
        super(FragmentTestActivity.class);
        mIndex = 1;
        mRnd = new Random();
    }

    @Override
    public void setUp() {
        mActivity = getActivity();
        // Needed by the CallLogFragment.
        mActivity.setTheme(R.style.DialtactsTheme);

        // Create the fragment and load it into the activity.
        mFragment = new CallLogFragment();
        FragmentManager fragmentManager = mActivity.getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(FragmentTestActivity.LAYOUT_ID, mFragment);
        transaction.commitAllowingStateLoss();
        // Wait for the fragment to be loaded.
        getInstrumentation().waitForIdleSync();

        final TelephonyManager telephonyManager =
                (TelephonyManager) mActivity.getSystemService(Context.TELEPHONY_SERVICE);
        mVoicemail = telephonyManager.getVoiceMailNumber();
        mAdapter = mFragment.getAdapter();
        // Do not process requests for details during tests. This would start a background thread,
        // which makes the tests flaky.
        mAdapter.disableRequestProcessingForTest();
        mAdapter.pauseCache();
        mParentView = new FrameLayout(mActivity);
        mCursor = new MatrixCursor(CallLogQuery._PROJECTION);
        mAdapter.setCursorForTesting(mCursor);
    }

    /**
     * Checks that the call icon is not visible for private and
     * unknown numbers.
     * Use 2 passes, one where new viewHolder are created and one where
     * half of the total viewHolder are updated and the other half created.
     */
    @MediumTest
    public void testCallViewIsNotVisibleForPrivateAndUnknownNumbers() {
        final int SIZE = 100;
        mList = new CallLogListItemViewHolder[SIZE];

        // Insert the first batch of entries.
        mCursor.moveToFirst();
        insertRandomEntries(SIZE / 2);
        int startOfSecondBatch = mCursor.getPosition();

        buildViewListFromDb();
        checkCallStatus();

        // Append the rest of the entries. We keep the first set of
        // viewHolder around so they get updated and not built from
        // scratch, this exposes some bugs that are not there when the
        // call log is launched for the 1st time but show up when the
        // call log gets updated afterwards.
        mCursor.move(startOfSecondBatch);
        insertRandomEntries(SIZE / 2);

        buildViewListFromDb();
        checkCallStatus();
    }

    @MediumTest
    public void testCallAndGroupviewHolder_GroupView() {
        mCursor.moveToFirst();
        insertPrivate(NOW, 0);
        insertPrivate(NOW, 0);
        insertPrivate(NOW, 0);
        CallLogListItemViewHolder viewHolder = (CallLogListItemViewHolder)
                mAdapter.onCreateViewHolder(mParentView, /* viewType */ 0);
        mAdapter.onBindViewHolder(viewHolder, /* position */ 0);
    }

    @MediumTest
    public void testCallAndGroupviewHolder_StandAloneView() {
        mCursor.moveToFirst();
        insertPrivate(NOW, 0);
        CallLogListItemViewHolder viewHolder = (CallLogListItemViewHolder)
                mAdapter.onCreateViewHolder(mParentView, /* viewType */ 0);
        bindViewForTest(viewHolder);
    }

    @MediumTest
    public void testCallAndGroupviewHolder_ChildView() {
        mCursor.moveToFirst();
        insertPrivate(NOW, 0);
        CallLogListItemViewHolder viewHolder = (CallLogListItemViewHolder)
                mAdapter.onCreateViewHolder(mParentView, /* viewType */ 0);
        mAdapter.onBindViewHolder(viewHolder, /* position */ 0);
    }

    @MediumTest
    public void testBindView_NumberOnlyNoCache() {
        mCursor.moveToFirst();
        insert(TEST_NUMBER, Calls.PRESENTATION_ALLOWED, NOW, 0, Calls.INCOMING_TYPE);
        CallLogListItemViewHolder viewHolder = (CallLogListItemViewHolder)
                mAdapter.onCreateViewHolder(mParentView, /* viewType */ 0);
        bindViewForTest(viewHolder);

        assertNameIs(viewHolder, TEST_NUMBER);
    }

    @MediumTest
    public void testBindView_NumberOnlyDbCachedFormattedNumber() {
        mCursor.moveToFirst();
        Object[] values = getValuesToInsert(TEST_NUMBER,
                Calls.PRESENTATION_ALLOWED, NOW, 0, Calls.INCOMING_TYPE);
        values[CallLogQuery.CACHED_FORMATTED_NUMBER] = TEST_FORMATTED_NUMBER;
        insertValues(values);
        CallLogListItemViewHolder viewHolder = (CallLogListItemViewHolder)
                mAdapter.onCreateViewHolder(mParentView, /* viewType */ 0);
        bindViewForTest(viewHolder);

        assertNameIs(viewHolder, TEST_FORMATTED_NUMBER);
    }

    @MediumTest
    public void testBindView_WithCachedName() {
        mCursor.moveToFirst();
        // provide a default custom label instead of an empty string, which corresponds to
        // {@value com.android.dialer.calllog.ContactInfo#GEOCODE_AS_LABEL}
        insertWithCachedValues(TEST_NUMBER, NOW, 0, Calls.INCOMING_TYPE,
                "John Doe", Phone.TYPE_HOME, TEST_DEFAULT_CUSTOM_LABEL);
        CallLogListItemViewHolder viewHolder = (CallLogListItemViewHolder)
                mAdapter.onCreateViewHolder(mParentView, /* viewType */ 0);
        bindViewForTest(viewHolder);

        assertNameIs(viewHolder, "John Doe");
        assertLabel(viewHolder, TEST_FORMATTED_NUMBER, getTypeLabel(Phone.TYPE_HOME));
    }

    @MediumTest
    public void testBindView_UriNumber() {
        mCursor.moveToFirst();
        insertWithCachedValues("sip:johndoe@gmail.com", NOW, 0, Calls.INCOMING_TYPE,
                "John Doe", Phone.TYPE_HOME, TEST_DEFAULT_CUSTOM_LABEL);
        CallLogListItemViewHolder viewHolder = (CallLogListItemViewHolder)
                mAdapter.onCreateViewHolder(mParentView, /* viewType */ 0);
        bindViewForTest(viewHolder);

        assertNameIs(viewHolder, "John Doe");
        assertLabel(viewHolder, "sip:johndoe@gmail.com", "sip:johndoe@gmail.com");
    }

    @MediumTest
    public void testBindView_HomeLabel() {
        mCursor.moveToFirst();
        // provide a default custom label instead of an empty string, which corresponds to
        // {@value com.android.dialer.calllog.ContactInfo#GEOCODE_AS_LABEL}
        insertWithCachedValues(TEST_NUMBER, NOW, 0, Calls.INCOMING_TYPE,
                "John Doe", Phone.TYPE_HOME, TEST_DEFAULT_CUSTOM_LABEL);
        CallLogListItemViewHolder viewHolder = (CallLogListItemViewHolder)
                mAdapter.onCreateViewHolder(mParentView, /* viewType */ 0);
        bindViewForTest(viewHolder);

        assertNameIs(viewHolder, "John Doe");
        assertLabel(viewHolder, TEST_FORMATTED_NUMBER, getTypeLabel(Phone.TYPE_HOME));
    }

    @MediumTest
    public void testBindView_WorkLabel() {
        mCursor.moveToFirst();
        // provide a default custom label instead of an empty string, which corresponds to
        // {@link com.android.dialer.calllog.ContactInfo#GEOCODE_AS_LABEL}
        insertWithCachedValues(TEST_NUMBER, NOW, 0, Calls.INCOMING_TYPE,
                "John Doe", Phone.TYPE_WORK, TEST_DEFAULT_CUSTOM_LABEL);
        CallLogListItemViewHolder viewHolder = (CallLogListItemViewHolder)
                mAdapter.onCreateViewHolder(mParentView, /* viewType */ 0);
        bindViewForTest(viewHolder);

        assertNameIs(viewHolder, "John Doe");
        assertLabel(viewHolder, TEST_FORMATTED_NUMBER, getTypeLabel(Phone.TYPE_WORK));
    }

    @MediumTest
    public void testBindView_CustomLabel() {
        mCursor.moveToFirst();
        String numberLabel = "My label";
        insertWithCachedValues(TEST_NUMBER, NOW, 0, Calls.INCOMING_TYPE,
                "John Doe", Phone.TYPE_CUSTOM, numberLabel);
        CallLogListItemViewHolder viewHolder = (CallLogListItemViewHolder)
                mAdapter.onCreateViewHolder(mParentView, /* viewType */ 0);
        bindViewForTest(viewHolder);

        assertNameIs(viewHolder, "John Doe");
        assertLabel(viewHolder, TEST_FORMATTED_NUMBER, numberLabel);
    }

    @MediumTest
    public void testBindView_WithQuickContactBadge() {
        mCursor.moveToFirst();
        insertWithCachedValues(TEST_NUMBER, NOW, 0, Calls.INCOMING_TYPE,
                "John Doe", Phone.TYPE_HOME, "");
        CallLogListItemViewHolder viewHolder = (CallLogListItemViewHolder)
                mAdapter.onCreateViewHolder(mParentView, /* viewType */ 0);
        bindViewForTest(viewHolder);

        assertTrue(viewHolder.quickContactView.isEnabled());
    }

    @MediumTest
    public void testBindView_WithoutQuickContactBadge() {
        mCursor.moveToFirst();
        insert(TEST_NUMBER, Calls.PRESENTATION_ALLOWED, NOW, 0, Calls.INCOMING_TYPE);
        CallLogListItemViewHolder viewHolder = (CallLogListItemViewHolder)
                mAdapter.onCreateViewHolder(mParentView, /* viewType */ 0);
        bindViewForTest(viewHolder);

        assertFalse(viewHolder.quickContactView.isEnabled());
    }

    @MediumTest
    public void testBindView_CallButton() {
        mCursor.moveToFirst();
        insert(TEST_NUMBER, Calls.PRESENTATION_ALLOWED, NOW, 0, Calls.INCOMING_TYPE);
        mAdapter.changeCursor(mCursor);
        CallLogListItemViewHolder viewHolder = (CallLogListItemViewHolder)
                mAdapter.onCreateViewHolder(mParentView, /* viewType */ 0);
        bindViewForTest(viewHolder);
        viewHolder.updateCallButton();

        // The primaryActionView tag is set in the
        // {@link com.android.dialer.calllog.CallLogAdapter#bindView} method.  If it is possible
        // to place a call to the phone number, a call intent will have been created for the
        // primaryActionView.
        IntentProvider intentProvider = (IntentProvider) viewHolder.callActionView.getTag();
        Intent intent = intentProvider.getIntent(mActivity);
        // Starts a call.
        assertEquals(TestConstants.CALL_INTENT_ACTION, intent.getAction());
        // To the entry's number.
        assertEquals(Uri.parse("tel:" + TEST_NUMBER), intent.getData());
    }

    @MediumTest
    public void testBindView_PlayButton() {
        mCursor.moveToFirst();
        insertVoicemail(TEST_NUMBER, Calls.PRESENTATION_ALLOWED, NOW, 0);
        CallLogListItemViewHolder viewHolder = (CallLogListItemViewHolder)
                mAdapter.onCreateViewHolder(mParentView, /* viewType */ 0);
        bindViewForTest(viewHolder);

        IntentProvider intentProvider = (IntentProvider) viewHolder.voicemailButtonView.getTag();
        Intent intent = intentProvider.getIntent(mActivity);
        // Starts the call detail activity.
        assertEquals(new ComponentName(mActivity, CallDetailActivity.class),
                intent.getComponent());
        // With the given entry.
        assertEquals(ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, 1),
                intent.getData());
        // With the URI of the voicemail.
        assertEquals(
                ContentUris.withAppendedId(VoicemailContract.Voicemails.CONTENT_URI, 1),
                intent.getParcelableExtra(CallDetailActivity.EXTRA_VOICEMAIL_URI));
        // And starts playback.
        assertTrue(
                intent.getBooleanExtra(CallDetailActivity.EXTRA_VOICEMAIL_START_PLAYBACK, false));
    }

    /** Returns the label associated with a given phone type. */
    private CharSequence getTypeLabel(int phoneType) {
        return Phone.getTypeLabel(getActivity().getResources(), phoneType, "");
    }

    //
    // HELPERS to check conditions on the DB/viewHolder
    //
    /**
     * Go over the viewHolder in the list and check to ensure that
     * callable numbers have an associated call intent, where numbers
     * which are not callable have a null intent.
     */
    private void checkCallStatus() {
        for (int i = 0; i < mList.length; i++) {
            if (null == mList[i]) {
                break;
            }
            mItem = (CallLogListItemViewHolder) mList[i];
            int presentation = getPhoneNumberPresentationForListEntry(i);
            if (presentation == Calls.PRESENTATION_RESTRICTED ||
                    presentation == Calls.PRESENTATION_UNKNOWN) {
                //If number is not callable, the primary action view should have a null tag.
                assertNull(mItem.callActionView.getTag());
            } else {
                //If the number is callable, the primary action view should have a non-null tag.
                assertNotNull(mItem.callActionView.getTag());

                IntentProvider intentProvider = (IntentProvider)mItem.callActionView.getTag();
                Intent callIntent = intentProvider.getIntent(mActivity);

                //The intent should be to make the call
                assertEquals(TestConstants.CALL_INTENT_ACTION, callIntent.getAction());
            }
        }
    }


    //
    // HELPERS to setup the tests.
    //

    /**
     * Get the Bitmap from the icons in the contacts package.
     */
    private Bitmap getBitmap(String resName) {
        Resources r = mActivity.getResources();
        int resid = r.getIdentifier(resName, "drawable",
                getInstrumentation().getTargetContext().getPackageName());
        BitmapDrawable d = (BitmapDrawable) r.getDrawable(resid);
        assertNotNull(d);
        return d.getBitmap();
    }

    //
    // HELPERS to build/update the call entries (viewHolder) from the DB.
    //

    /**
     * Read the DB and foreach call either update the existing view if
     * one exists already otherwise create one.
     * The list is build from a DESC view of the DB (last inserted entry is first).
     */
    private void buildViewListFromDb() {
        int i = 0;
        mCursor.moveToLast();
        while (!mCursor.isBeforeFirst()) {
            if (null == mList[i]) {
                mList[i] = (CallLogListItemViewHolder)
                        mAdapter.onCreateViewHolder(mParentView, /* itemType */ 0);
            }
            // Bind to the proper position, despite iterating in reverse.
            bindViewForTest(mList[i], mCursor.getCount() - i - 1);
            mCursor.moveToPrevious();
            i++;
        }
    }

    /** Returns the number presentation associated with the given entry in {{@link #mList}. */
    private int getPhoneNumberPresentationForListEntry(int index) {
        // The entries are added backward, so count from the end of the cursor.
        mCursor.moveToPosition(mCursor.getCount() - index - 1);
        return mCursor.getInt(CallLogQuery.NUMBER_PRESENTATION);
    }

    //
    // HELPERS to insert numbers in the call log DB.
    //

    /**
     * Bind a call log entry view for testing purposes.  Also inflates the action view stub so
     * unit tests can access the buttons contained within.
     *
     * @param view The current call log row.
     * @param position The position of hte item.
     */
    private void bindViewForTest(CallLogListItemViewHolder viewHolder, int position) {
        mAdapter.onBindViewHolder(viewHolder, position);
        viewHolder.inflateActionViewStub(null);
        viewHolder.updateCallButton();
    }

    private void bindViewForTest(CallLogListItemViewHolder viewHolder) {
        bindViewForTest(viewHolder, /* position */ 0);
    }

    /**
     * Insert a certain number of random numbers in the DB. Makes sure
     * there is at least one private and one unknown number in the DB.
     * @param num Of entries to be inserted.
     */
    private void insertRandomEntries(int num) {
        if (num < 10) {
            throw new IllegalArgumentException("num should be >= 10");
        }
        boolean privateOrUnknownOrVm[];
        privateOrUnknownOrVm = insertRandomRange(0, num - 2);

        if (privateOrUnknownOrVm[0] && privateOrUnknownOrVm[1]) {
            insertRandomRange(num - 2, num);
        } else {
            insertPrivate(NOW, RAND_DURATION);
            insertUnknown(NOW, RAND_DURATION);
        }
    }

    /**
     * Insert a new call entry in the test DB.
     *
     * It includes the values for the cached contact associated with the number.
     *
     * @param number The phone number.
     * @param date In millisec since epoch. Use NOW to use the current time.
     * @param duration In seconds of the call. Use RAND_DURATION to pick a random one.
     * @param type Either Call.OUTGOING_TYPE or Call.INCOMING_TYPE or Call.MISSED_TYPE.
     * @param cachedName the name of the contact with this number
     * @param cachedNumberType the type of the number, from the contact with this number
     * @param cachedNumberLabel the label of the number, from the contact with this number
     */
    private void insertWithCachedValues(String number, long date, int duration, int type,
            String cachedName, int cachedNumberType, String cachedNumberLabel) {
        insert(number, Calls.PRESENTATION_ALLOWED, date, duration, type);
        ContactInfo contactInfo = new ContactInfo();
        contactInfo.lookupUri = TEST_LOOKUP_URI;
        contactInfo.name = cachedName;
        contactInfo.type = cachedNumberType;
        contactInfo.label = cachedNumberLabel;
        String formattedNumber = PhoneNumberUtils.formatNumber(number, TEST_COUNTRY_ISO);
        if (formattedNumber == null) {
            formattedNumber = number;
        }
        contactInfo.formattedNumber = formattedNumber;
        contactInfo.normalizedNumber = number;
        contactInfo.photoId = 0;
        mAdapter.injectContactInfoForTest(number, TEST_COUNTRY_ISO, contactInfo);
    }

    /**
     * Insert a new call entry in the test DB.
     * @param number The phone number.
     * @param presentation Number representing display rules for "allowed",
     *               "payphone", "restricted", or "unknown".
     * @param date In millisec since epoch. Use NOW to use the current time.
     * @param duration In seconds of the call. Use RAND_DURATION to pick a random one.
     * @param type Either Call.OUTGOING_TYPE or Call.INCOMING_TYPE or Call.MISSED_TYPE.
     */
    private void insert(String number, int presentation, long date, int duration, int type) {
        insertValues(getValuesToInsert(number, presentation, date, duration, type));
    }

    /** Inserts the given values in the cursor. */
    private void insertValues(Object[] values) {
        mCursor.addRow(values);
        ++mIndex;
    }

    /**
     * Returns the values for a new call entry.
     *
     * @param number The phone number.
     * @param presentation Number representing display rules for "allowed",
     *               "payphone", "restricted", or "unknown".
     * @param date In millisec since epoch. Use NOW to use the current time.
     * @param duration In seconds of the call. Use RAND_DURATION to pick a random one.
     * @param type Either Call.OUTGOING_TYPE or Call.INCOMING_TYPE or Call.MISSED_TYPE.
     */
    private Object[] getValuesToInsert(String number, int presentation,
            long date, int duration, int type) {
        Object[] values = CallLogQueryTestUtils.createTestValues();
        values[CallLogQuery.ID] = mIndex;
        values[CallLogQuery.NUMBER] = number;
        values[CallLogQuery.NUMBER_PRESENTATION] = presentation;
        values[CallLogQuery.DATE] = date == NOW ? new Date().getTime() : date;
        values[CallLogQuery.DURATION] = duration < 0 ? mRnd.nextInt(10 * 60) : duration;
        if (mVoicemail != null && mVoicemail.equals(number)) {
            assertEquals(Calls.OUTGOING_TYPE, type);
        }
        values[CallLogQuery.CALL_TYPE] = type;
        values[CallLogQuery.COUNTRY_ISO] = TEST_COUNTRY_ISO;
        return values;
    }

    /**
     * Insert a new voicemail entry in the test DB.
     * @param number The phone number.
     * @param presentation Number representing display rules for "allowed",
     *               "payphone", "restricted", or "unknown".
     * @param date In millisec since epoch. Use NOW to use the current time.
     * @param duration In seconds of the call. Use RAND_DURATION to pick a random one.
     */
    private void insertVoicemail(String number, int presentation, long date, int duration) {
        Object[] values = getValuesToInsert(number, presentation, date, duration, Calls.VOICEMAIL_TYPE);
        // Must have the same index as the row.
        values[CallLogQuery.VOICEMAIL_URI] =
                ContentUris.withAppendedId(VoicemailContract.Voicemails.CONTENT_URI, mIndex);
        insertValues(values);
    }

    /**
     * Insert a new private call entry in the test DB.
     * @param date In millisec since epoch. Use NOW to use the current time.
     * @param duration In seconds of the call. Use RAND_DURATION to pick a random one.
     */
    private void insertPrivate(long date, int duration) {
        insert("", Calls.PRESENTATION_RESTRICTED, date, duration, Calls.INCOMING_TYPE);
    }

    /**
     * Insert a new unknown call entry in the test DB.
     * @param date In millisec since epoch. Use NOW to use the current time.
     * @param duration In seconds of the call. Use RAND_DURATION to pick a random one.
     */
    private void insertUnknown(long date, int duration) {
        insert("", Calls.PRESENTATION_UNKNOWN, date, duration, Calls.INCOMING_TYPE);
    }

    /**
     * Insert a new call to voicemail entry in the test DB.
     * @param date In millisec since epoch. Use NOW to use the current time.
     * @param duration In seconds of the call. Use RAND_DURATION to pick a random one.
     */
    private void insertCalltoVoicemail(long date, int duration) {
        // mVoicemail may be null
        if (mVoicemail != null) {
            insert(mVoicemail, Calls.PRESENTATION_ALLOWED, date, duration, Calls.OUTGOING_TYPE);
        }
    }

    /**
     * Insert a range [start, end) of random numbers in the DB. For
     * each row, there is a 1/10 probability that the number will be
     * marked as PRIVATE or UNKNOWN or VOICEMAIL. For regular numbers, a number is
     * inserted, its last 4 digits will be the number of the iteration
     * in the range.
     * @param start Of the range.
     * @param end Of the range (excluded).
     * @return An array with 2 booleans [0 = private number, 1 =
     * unknown number, 2 = voicemail] to indicate if at least one
     * private or unknown or voicemail number has been inserted. Since
     * the numbers are random some tests may want to enforce the
     * insertion of such numbers.
     */
    // TODO: Should insert numbers with contact entries too.
    private boolean[] insertRandomRange(int start, int end) {
        boolean[] privateOrUnknownOrVm = new boolean[] {false, false, false};

        for (int i = start; i < end; i++ ) {
            int type = mRnd.nextInt(10);

            if (0 == type) {
                insertPrivate(NOW, RAND_DURATION);
                privateOrUnknownOrVm[0] = true;
            } else if (1 == type) {
                insertUnknown(NOW, RAND_DURATION);
                privateOrUnknownOrVm[1] = true;
            } else if (2 == type) {
                insertCalltoVoicemail(NOW, RAND_DURATION);
                privateOrUnknownOrVm[2] = true;
            } else {
                int inout = mRnd.nextBoolean() ? Calls.OUTGOING_TYPE :  Calls.INCOMING_TYPE;
                final Formatter formatter = new Formatter();
                String number = formatter.format("1800123%04d", i).toString();
                formatter.close();
                insert(number, Calls.PRESENTATION_ALLOWED, NOW, RAND_DURATION, inout);
            }
        }
        return privateOrUnknownOrVm;
    }

    /** Asserts that the name text view is shown and contains the given text. */
    private void assertNameIs(CallLogListItemViewHolder viewHolder, String name) {
        assertEquals(View.VISIBLE, viewHolder.phoneCallDetailsViews.nameView.getVisibility());
        assertEquals(name, viewHolder.phoneCallDetailsViews.nameView.getText().toString());
    }

    /** Asserts that the label text view contains the given text. */
    private void assertLabel(CallLogListItemViewHolder viewHolder, CharSequence number,
            CharSequence label) {
        if (label != null) {
            assertTrue(viewHolder.phoneCallDetailsViews.callLocationAndDate.getText()
                    .toString().contains(label));
        }
    }
}