CallDetailActivityTest.java revision 94d7bab07a69efbe5a383affec95d1d9ba9dc203
1/*
2 * Copyright (C) 2011 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.IntegrationTestUtils;
20import com.android.contacts.util.LocaleTestUtils;
21import com.android.internal.view.menu.ContextMenuBuilder;
22import com.google.common.base.Preconditions;
23
24import android.app.Activity;
25import android.content.ContentResolver;
26import android.content.ContentUris;
27import android.content.ContentValues;
28import android.content.Intent;
29import android.net.Uri;
30import android.provider.CallLog;
31import android.test.ActivityInstrumentationTestCase2;
32import android.view.Menu;
33
34import java.util.Locale;
35
36/**
37 * Unit tests for the {@link CallDetailActivity}.
38 */
39public class CallDetailActivityTest extends ActivityInstrumentationTestCase2<CallDetailActivity> {
40    private static final String FAKE_VOICEMAIL_URI_STRING = "content://fake_uri";
41    private Uri mUri;
42    private IntegrationTestUtils mTestUtils;
43    private LocaleTestUtils mLocaleTestUtils;
44
45    public CallDetailActivityTest() {
46        super(CallDetailActivity.class);
47    }
48
49    @Override
50    protected void setUp() throws Exception {
51        super.setUp();
52        // I don't like the default of focus-mode for tests, the green focus border makes the
53        // screenshots look weak.
54        setActivityInitialTouchMode(true);
55        mTestUtils = new IntegrationTestUtils(getInstrumentation());
56        // Some of the tests rely on the text that appears on screen - safest to force a
57        // specific locale.
58        mLocaleTestUtils = new LocaleTestUtils(getInstrumentation().getTargetContext());
59        mLocaleTestUtils.setLocale(Locale.US);
60    }
61
62    @Override
63    protected void tearDown() throws Exception {
64        mLocaleTestUtils.restoreLocale();
65        mLocaleTestUtils = null;
66        cleanUpUri();
67        mTestUtils = null;
68        super.tearDown();
69    }
70
71    /**
72     * Test for bug where increase rate button with invalid voicemail causes a crash.
73     * <p>
74     * The repro steps for this crash were to open a voicemail that does not have an attachment,
75     * then click the play button (which just reported an error), then after that try to adjust the
76     * rate.  See http://b/5047879.
77     */
78    public void testClickIncreaseRateButtonWithInvalidVoicemailDoesNotCrash() throws Throwable {
79        setActivityIntentForTestVoicemailEntry();
80        Activity activity = getActivity();
81        mTestUtils.clickButton(activity, R.id.playback_start_stop);
82        mTestUtils.clickButton(activity, R.id.rate_increase_button);
83    }
84
85    /** Test for bug where missing Extras on intent used to start Activity causes NPE. */
86    public void testCallLogUriWithMissingExtrasShouldNotCauseNPE() throws Exception {
87        setActivityIntentForTestCallEntry();
88        getActivity();
89    }
90
91    /**
92     * Test for bug where voicemails should not have remove-from-call-log entry.
93     * <p>
94     * See http://b/5054103.
95     */
96    public void testVoicemailDoesNotHaveRemoveFromCallLog() throws Throwable {
97        setActivityIntentForTestVoicemailEntry();
98        CallDetailActivity activity = getActivity();
99        Menu menu = new ContextMenuBuilder(activity);
100        activity.onCreateOptionsMenu(menu);
101        activity.onPrepareOptionsMenu(menu);
102        assertFalse(menu.findItem(R.id.remove_from_call_log).isVisible());
103    }
104
105    /** Test to check that I haven't broken the remove-from-call-log entry from regular calls. */
106    public void testRegularCallDoesHaveRemoveFromCallLog() throws Throwable {
107        setActivityIntentForTestCallEntry();
108        CallDetailActivity activity = getActivity();
109        Menu menu = new ContextMenuBuilder(activity);
110        activity.onCreateOptionsMenu(menu);
111        activity.onPrepareOptionsMenu(menu);
112        assertTrue(menu.findItem(R.id.remove_from_call_log).isVisible());
113    }
114
115    private void setActivityIntentForTestCallEntry() {
116        createTestCallEntry(false);
117        setActivityIntent(new Intent(Intent.ACTION_VIEW, mUri));
118    }
119
120    private void setActivityIntentForTestVoicemailEntry() {
121        createTestCallEntry(true);
122        Intent intent = new Intent(Intent.ACTION_VIEW, mUri);
123        Uri voicemailUri = Uri.parse(FAKE_VOICEMAIL_URI_STRING);
124        intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_URI, voicemailUri);
125        setActivityIntent(intent);
126    }
127
128    /** Inserts an entry into the call log. */
129    private void createTestCallEntry(boolean isVoicemail) {
130        Preconditions.checkState(mUri == null, "mUri should be null");
131        ContentResolver contentResolver = getContentResolver();
132        ContentValues contentValues = new ContentValues();
133        contentValues.put(CallLog.Calls.NUMBER, "01234567890");
134        if (isVoicemail) {
135            contentValues.put(CallLog.Calls.TYPE, CallLog.Calls.VOICEMAIL_TYPE);
136            contentValues.put(CallLog.Calls.VOICEMAIL_URI, FAKE_VOICEMAIL_URI_STRING);
137        } else {
138            contentValues.put(CallLog.Calls.TYPE, CallLog.Calls.INCOMING_TYPE);
139        }
140        contentValues.put(CallLog.Calls.VOICEMAIL_URI, FAKE_VOICEMAIL_URI_STRING);
141        mUri = contentResolver.insert(CallLog.Calls.CONTENT_URI_WITH_VOICEMAIL, contentValues);
142    }
143
144    private void cleanUpUri() {
145        if (mUri != null) {
146            getContentResolver().delete(CallLog.Calls.CONTENT_URI_WITH_VOICEMAIL,
147                    "_ID = ?", new String[] { String.valueOf(ContentUris.parseId(mUri)) });
148            mUri = null;
149        }
150    }
151
152    private ContentResolver getContentResolver() {
153        return getInstrumentation().getTargetContext().getContentResolver();
154    }
155}
156