CallDetailActivityTest.java revision c8c551e0b9ba873ab65f3e214ab73706ade09ad0
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.provider.VoicemailContract.Voicemails;
32import android.test.ActivityInstrumentationTestCase2;
33import android.test.suitebuilder.annotation.LargeTest;
34import android.test.suitebuilder.annotation.Suppress;
35import android.view.Menu;
36import android.widget.TextView;
37
38import java.util.List;
39import java.util.Locale;
40
41/**
42 * Unit tests for the {@link CallDetailActivity}.
43 */
44@LargeTest
45public class CallDetailActivityTest extends ActivityInstrumentationTestCase2<CallDetailActivity> {
46    private static final String FAKE_VOICEMAIL_URI_STRING = ContentUris.withAppendedId(
47            Voicemails.CONTENT_URI, Integer.MAX_VALUE).toString();
48    private Uri mUri;
49    private IntegrationTestUtils mTestUtils;
50    private LocaleTestUtils mLocaleTestUtils;
51
52    public CallDetailActivityTest() {
53        super(CallDetailActivity.class);
54    }
55
56    @Override
57    protected void setUp() throws Exception {
58        super.setUp();
59        // I don't like the default of focus-mode for tests, the green focus border makes the
60        // screenshots look weak.
61        setActivityInitialTouchMode(true);
62        mTestUtils = new IntegrationTestUtils(getInstrumentation());
63        // Some of the tests rely on the text that appears on screen - safest to force a
64        // specific locale.
65        mLocaleTestUtils = new LocaleTestUtils(getInstrumentation().getTargetContext());
66        mLocaleTestUtils.setLocale(Locale.US);
67    }
68
69    @Override
70    protected void tearDown() throws Exception {
71        mLocaleTestUtils.restoreLocale();
72        mLocaleTestUtils = null;
73        cleanUpUri();
74        mTestUtils = null;
75        super.tearDown();
76    }
77
78    /**
79     * Test for bug where increase rate button with invalid voicemail causes a crash.
80     * <p>
81     * The repro steps for this crash were to open a voicemail that does not have an attachment,
82     * then click the play button (which just reported an error), then after that try to adjust the
83     * rate.  See http://b/5047879.
84     */
85    public void testClickIncreaseRateButtonWithInvalidVoicemailDoesNotCrash() throws Throwable {
86        setActivityIntentForTestVoicemailEntry();
87        Activity activity = getActivity();
88        mTestUtils.clickButton(activity, R.id.playback_start_stop);
89        mTestUtils.clickButton(activity, R.id.rate_increase_button);
90    }
91
92    /** Test for bug where missing Extras on intent used to start Activity causes NPE. */
93    public void testCallLogUriWithMissingExtrasShouldNotCauseNPE() throws Exception {
94        setActivityIntentForTestCallEntry();
95        getActivity();
96    }
97
98    /**
99     * Test for bug where voicemails should not have remove-from-call-log entry.
100     * <p>
101     * See http://b/5054103.
102     */
103    public void testVoicemailDoesNotHaveRemoveFromCallLog() throws Throwable {
104        setActivityIntentForTestVoicemailEntry();
105        CallDetailActivity activity = getActivity();
106        Menu menu = new ContextMenuBuilder(activity);
107        activity.onCreateOptionsMenu(menu);
108        activity.onPrepareOptionsMenu(menu);
109        assertFalse(menu.findItem(R.id.menu_remove_from_call_log).isVisible());
110    }
111
112    /** Test to check that I haven't broken the remove-from-call-log entry from regular calls. */
113    public void testRegularCallDoesHaveRemoveFromCallLog() throws Throwable {
114        setActivityIntentForTestCallEntry();
115        CallDetailActivity activity = getActivity();
116        Menu menu = new ContextMenuBuilder(activity);
117        activity.onCreateOptionsMenu(menu);
118        activity.onPrepareOptionsMenu(menu);
119        assertTrue(menu.findItem(R.id.menu_remove_from_call_log).isVisible());
120    }
121
122    /**
123     * Test to show that we are correctly displaying playback rate on the ui.
124     * <p>
125     * See bug http://b/5044075.
126     */
127    @Suppress
128    public void testVoicemailPlaybackRateDisplayedOnUi() throws Throwable {
129        setActivityIntentForTestVoicemailEntry();
130        CallDetailActivity activity = getActivity();
131        // Find the TextView containing the duration.  It should be initially displaying "00:00".
132        List<TextView> views = mTestUtils.getTextViewsWithString(activity, "00:00");
133        assertEquals(1, views.size());
134        TextView timeDisplay = views.get(0);
135        // Hit the plus button.  At this point we should be displaying "fast speed".
136        mTestUtils.clickButton(activity, R.id.rate_increase_button);
137        assertEquals("fast speed", mTestUtils.getText(timeDisplay));
138        // Hit the minus button.  We should be back to "normal" speed.
139        mTestUtils.clickButton(activity, R.id.rate_decrease_button);
140        assertEquals("normal speed", mTestUtils.getText(timeDisplay));
141        // Wait for one and a half seconds.  The timer will be back.
142        Thread.sleep(1500);
143        assertEquals("00:00", mTestUtils.getText(timeDisplay));
144    }
145
146    private void setActivityIntentForTestCallEntry() {
147        createTestCallEntry(false);
148        setActivityIntent(new Intent(Intent.ACTION_VIEW, mUri));
149    }
150
151    private void setActivityIntentForTestVoicemailEntry() {
152        createTestCallEntry(true);
153        Intent intent = new Intent(Intent.ACTION_VIEW, mUri);
154        Uri voicemailUri = Uri.parse(FAKE_VOICEMAIL_URI_STRING);
155        intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_URI, voicemailUri);
156        setActivityIntent(intent);
157    }
158
159    /** Inserts an entry into the call log. */
160    private void createTestCallEntry(boolean isVoicemail) {
161        Preconditions.checkState(mUri == null, "mUri should be null");
162        ContentResolver contentResolver = getContentResolver();
163        ContentValues contentValues = new ContentValues();
164        contentValues.put(CallLog.Calls.NUMBER, "01234567890");
165        if (isVoicemail) {
166            contentValues.put(CallLog.Calls.TYPE, CallLog.Calls.VOICEMAIL_TYPE);
167            contentValues.put(CallLog.Calls.VOICEMAIL_URI, FAKE_VOICEMAIL_URI_STRING);
168        } else {
169            contentValues.put(CallLog.Calls.TYPE, CallLog.Calls.INCOMING_TYPE);
170        }
171        contentValues.put(CallLog.Calls.VOICEMAIL_URI, FAKE_VOICEMAIL_URI_STRING);
172        mUri = contentResolver.insert(CallLog.Calls.CONTENT_URI_WITH_VOICEMAIL, contentValues);
173    }
174
175    private void cleanUpUri() {
176        if (mUri != null) {
177            getContentResolver().delete(CallLog.Calls.CONTENT_URI_WITH_VOICEMAIL,
178                    "_ID = ?", new String[] { String.valueOf(ContentUris.parseId(mUri)) });
179            mUri = null;
180        }
181    }
182
183    private ContentResolver getContentResolver() {
184        return getInstrumentation().getTargetContext().getContentResolver();
185    }
186}
187