194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng/*
294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * Copyright (C) 2011 The Android Open Source Project
394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *
494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * Licensed under the Apache License, Version 2.0 (the "License");
594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * you may not use this file except in compliance with the License.
694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * You may obtain a copy of the License at
794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *
894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *      http://www.apache.org/licenses/LICENSE-2.0
994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *
1094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * Unless required by applicable law or agreed to in writing, software
1194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * distributed under the License is distributed on an "AS IS" BASIS,
1294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * See the License for the specific language governing permissions and
1494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * limitations under the License.
1594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng */
1694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
1794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengpackage com.android.dialer;
1894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
1994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport static com.android.dialer.CallDetailActivity.Tasks.UPDATE_PHONE_CALL_DETAILS;
2094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport static com.android.dialer.voicemail.VoicemailPlaybackPresenter.Tasks.CHECK_FOR_CONTENT;
2194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport static com.android.dialer.voicemail.VoicemailPlaybackPresenter.Tasks.PREPARE_MEDIA_PLAYER;
2294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
2394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.content.ContentResolver;
2494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.content.ContentUris;
2594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.content.ContentValues;
2694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.content.Intent;
2794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.content.res.AssetManager;
2894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.net.Uri;
2994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.provider.CallLog;
3094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.provider.VoicemailContract;
3194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.test.ActivityInstrumentationTestCase2;
3294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.test.suitebuilder.annotation.LargeTest;
3394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.test.suitebuilder.annotation.Suppress;
3494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.view.Menu;
3594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.widget.TextView;
3694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
3791197049c458f07092b31501d2ed512180b13d58Chiao Chengimport com.android.dialer.util.AsyncTaskExecutors;
3894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport com.android.dialer.util.FakeAsyncTaskExecutor;
3994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport com.android.contacts.common.test.IntegrationTestUtils;
4094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport com.android.dialer.util.LocaleTestUtils;
4194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport com.android.internal.view.menu.ContextMenuBuilder;
4294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport com.google.common.io.Closeables;
4394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
4494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport java.io.IOException;
4594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport java.io.InputStream;
4694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport java.io.OutputStream;
4794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport java.util.List;
4894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport java.util.Locale;
4994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
5094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng/**
5194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * Unit tests for the {@link CallDetailActivity}.
5294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng */
5394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng@LargeTest
5494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengpublic class CallDetailActivityTest extends ActivityInstrumentationTestCase2<CallDetailActivity> {
5594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String TEST_ASSET_NAME = "quick_test_recording.mp3";
5694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String MIME_TYPE = "audio/mp3";
5794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String CONTACT_NUMBER = "+1412555555";
5894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private static final String VOICEMAIL_FILE_LOCATION = "/sdcard/sadlfj893w4j23o9sfu.mp3";
5994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
6094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private Uri mCallLogUri;
6194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private Uri mVoicemailUri;
6294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private IntegrationTestUtils mTestUtils;
6394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private LocaleTestUtils mLocaleTestUtils;
6494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private FakeAsyncTaskExecutor mFakeAsyncTaskExecutor;
6594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private CallDetailActivity mActivityUnderTest;
6694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
6794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public CallDetailActivityTest() {
6894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        super(CallDetailActivity.class);
6994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
7094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
7194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
7294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    protected void setUp() throws Exception {
7394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        super.setUp();
7494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mFakeAsyncTaskExecutor = new FakeAsyncTaskExecutor(getInstrumentation());
7594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        AsyncTaskExecutors.setFactoryForTest(mFakeAsyncTaskExecutor.getFactory());
7694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // I don't like the default of focus-mode for tests, the green focus border makes the
7794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // screenshots look weak.
7894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        setActivityInitialTouchMode(true);
7994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mTestUtils = new IntegrationTestUtils(getInstrumentation());
8094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // Some of the tests rely on the text that appears on screen - safest to force a
8194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // specific locale.
8294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mLocaleTestUtils = new LocaleTestUtils(getInstrumentation().getTargetContext());
8394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mLocaleTestUtils.setLocale(Locale.US);
8494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
8594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
8694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Override
8794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    protected void tearDown() throws Exception {
8894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mLocaleTestUtils.restoreLocale();
8994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mLocaleTestUtils = null;
9094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        cleanUpUri();
9194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mTestUtils = null;
9294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        AsyncTaskExecutors.setFactoryForTest(null);
9394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        super.tearDown();
9494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
9594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
9694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void testInitialActivityStartsWithFetchingVoicemail() throws Throwable {
9794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        setActivityIntentForTestVoicemailEntry();
9894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        startActivityUnderTest();
9994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // When the activity first starts, we will show "Fetching voicemail" on the screen.
10094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // The duration should not be visible.
10194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        assertHasOneTextViewContaining("Fetching voicemail");
10294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        assertZeroTextViewsContaining("00:00");
10394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
10494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
10594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void testWhenCheckForContentCompletes_UiShowsBuffering() throws Throwable {
10694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        setActivityIntentForTestVoicemailEntry();
10794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        startActivityUnderTest();
10894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // There is a background check that is testing to see if we have the content available.
10994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // Once that task completes, we shouldn't be showing the fetching message, we should
11094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // be showing "Buffering".
11194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mFakeAsyncTaskExecutor.runTask(CHECK_FOR_CONTENT);
11294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        assertHasOneTextViewContaining("Buffering");
11394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        assertZeroTextViewsContaining("Fetching voicemail");
11494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
11594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
11694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void testInvalidVoicemailShowsErrorMessage() throws Throwable {
11794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        setActivityIntentForTestVoicemailEntry();
11894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        startActivityUnderTest();
11994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mFakeAsyncTaskExecutor.runTask(CHECK_FOR_CONTENT);
12094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // There should be exactly one background task ready to prepare the media player.
12194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // Preparing the media player will have thrown an IOException since the file doesn't exist.
12294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // This should have put a failed to play message on screen, buffering is gone.
12394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mFakeAsyncTaskExecutor.runTask(PREPARE_MEDIA_PLAYER);
12494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        assertHasOneTextViewContaining("Couldn't play voicemail");
12594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        assertZeroTextViewsContaining("Buffering");
12694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
12794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
12894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void testOnResumeDoesNotCreateManyFragments() throws Throwable {
12994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // There was a bug where every time the activity was resumed, a new fragment was created.
13094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // Before the fix, this was failing reproducibly with at least 3 "Buffering" views.
13194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        setActivityIntentForTestVoicemailEntry();
13294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        startActivityUnderTest();
13394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mFakeAsyncTaskExecutor.runTask(CHECK_FOR_CONTENT);
13494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        getInstrumentation().runOnMainSync(new Runnable() {
13594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            @Override
13694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            public void run() {
13794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                getInstrumentation().callActivityOnPause(mActivityUnderTest);
13894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                getInstrumentation().callActivityOnResume(mActivityUnderTest);
13994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                getInstrumentation().callActivityOnPause(mActivityUnderTest);
14094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                getInstrumentation().callActivityOnResume(mActivityUnderTest);
14194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            }
14294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        });
14394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        assertHasOneTextViewContaining("Buffering");
14494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
14594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
14694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
14794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Test for bug where increase rate button with invalid voicemail causes a crash.
14894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * <p>
14994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * The repro steps for this crash were to open a voicemail that does not have an attachment,
15094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * then click the play button (which just reported an error), then after that try to adjust the
15194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * rate.  See http://b/5047879.
15294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
15394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void testClickIncreaseRateButtonWithInvalidVoicemailDoesNotCrash() throws Throwable {
15494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        setActivityIntentForTestVoicemailEntry();
15594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        startActivityUnderTest();
15694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mTestUtils.clickButton(mActivityUnderTest, R.id.playback_start_stop);
15794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mTestUtils.clickButton(mActivityUnderTest, R.id.rate_increase_button);
15894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
15994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
16094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** Test for bug where missing Extras on intent used to start Activity causes NPE. */
16194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void testCallLogUriWithMissingExtrasShouldNotCauseNPE() throws Throwable {
16294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        setActivityIntentForTestCallEntry();
16394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        startActivityUnderTest();
16494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
16594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
16694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
16794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Test for bug where voicemails should not have remove-from-call-log entry.
16894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * <p>
16994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * See http://b/5054103.
17094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
17194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void testVoicemailDoesNotHaveRemoveFromCallLog() throws Throwable {
17294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        setActivityIntentForTestVoicemailEntry();
17394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        startActivityUnderTest();
17494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        Menu menu = new ContextMenuBuilder(mActivityUnderTest);
17594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mActivityUnderTest.onCreateOptionsMenu(menu);
17694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mActivityUnderTest.onPrepareOptionsMenu(menu);
17794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        assertFalse(menu.findItem(R.id.menu_remove_from_call_log).isVisible());
17894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
17994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
18094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /** Test to check that I haven't broken the remove-from-call-log entry from regular calls. */
18194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void testRegularCallDoesHaveRemoveFromCallLog() throws Throwable {
18294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        setActivityIntentForTestCallEntry();
18394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        startActivityUnderTest();
18494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        Menu menu = new ContextMenuBuilder(mActivityUnderTest);
18594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mActivityUnderTest.onCreateOptionsMenu(menu);
18694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mActivityUnderTest.onPrepareOptionsMenu(menu);
18794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        assertTrue(menu.findItem(R.id.menu_remove_from_call_log).isVisible());
18894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
18994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
19094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
19194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Test to show that we are correctly displaying playback rate on the ui.
19294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * <p>
19394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * See bug http://b/5044075.
19494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
19594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Suppress
19694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void testVoicemailPlaybackRateDisplayedOnUi() throws Throwable {
19794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        setActivityIntentForTestVoicemailEntry();
19894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        startActivityUnderTest();
19994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // Find the TextView containing the duration.  It should be initially displaying "00:00".
20094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        List<TextView> views = mTestUtils.getTextViewsWithString(mActivityUnderTest, "00:00");
20194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        assertEquals(1, views.size());
20294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        TextView timeDisplay = views.get(0);
20394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // Hit the plus button.  At this point we should be displaying "fast speed".
20494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mTestUtils.clickButton(mActivityUnderTest, R.id.rate_increase_button);
20594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        assertEquals("fast speed", mTestUtils.getText(timeDisplay));
20694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // Hit the minus button.  We should be back to "normal" speed.
20794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mTestUtils.clickButton(mActivityUnderTest, R.id.rate_decrease_button);
20894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        assertEquals("normal speed", mTestUtils.getText(timeDisplay));
20994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // Wait for one and a half seconds.  The timer will be back.
21094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        Thread.sleep(1500);
21194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        assertEquals("00:00", mTestUtils.getText(timeDisplay));
21294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
21394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
21494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    @Suppress
21594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void testClickingCallStopsPlayback() throws Throwable {
21694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        setActivityIntentForRealFileVoicemailEntry();
21794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        startActivityUnderTest();
21894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mFakeAsyncTaskExecutor.runTask(CHECK_FOR_CONTENT);
21994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mFakeAsyncTaskExecutor.runTask(PREPARE_MEDIA_PLAYER);
22094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mTestUtils.clickButton(mActivityUnderTest, R.id.playback_speakerphone);
22194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mTestUtils.clickButton(mActivityUnderTest, R.id.playback_start_stop);
22294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mTestUtils.clickButton(mActivityUnderTest, R.id.call_and_sms_main_action);
22394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        Thread.sleep(2000);
22494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // TODO: Suppressed the test for now, because I'm looking for an easy way to say "the audio
22594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // is not playing at this point", and I can't find it without doing dirty things.
22694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
22794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
22894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void setActivityIntentForTestCallEntry() {
22994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        assertNull(mCallLogUri);
23094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        ContentResolver contentResolver = getContentResolver();
23194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        ContentValues values = new ContentValues();
23294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        values.put(CallLog.Calls.NUMBER, CONTACT_NUMBER);
23394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        values.put(CallLog.Calls.TYPE, CallLog.Calls.INCOMING_TYPE);
23494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mCallLogUri = contentResolver.insert(CallLog.Calls.CONTENT_URI, values);
23594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        setActivityIntent(new Intent(Intent.ACTION_VIEW, mCallLogUri));
23694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
23794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
23894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void setActivityIntentForTestVoicemailEntry() {
23994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        assertNull(mVoicemailUri);
24094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        ContentResolver contentResolver = getContentResolver();
24194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        ContentValues values = new ContentValues();
24294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        values.put(VoicemailContract.Voicemails.NUMBER, CONTACT_NUMBER);
24394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        values.put(VoicemailContract.Voicemails.HAS_CONTENT, 1);
24494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        values.put(VoicemailContract.Voicemails._DATA, VOICEMAIL_FILE_LOCATION);
24594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mVoicemailUri = contentResolver.insert(VoicemailContract.Voicemails.CONTENT_URI, values);
24694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        Uri callLogUri = ContentUris.withAppendedId(CallLog.Calls.CONTENT_URI_WITH_VOICEMAIL,
24794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                ContentUris.parseId(mVoicemailUri));
24894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        Intent intent = new Intent(Intent.ACTION_VIEW, callLogUri);
24994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_URI, mVoicemailUri);
25094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        setActivityIntent(intent);
25194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
25294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
25394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void setActivityIntentForRealFileVoicemailEntry() throws IOException {
25494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        assertNull(mVoicemailUri);
25594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        ContentValues values = new ContentValues();
25694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        values.put(VoicemailContract.Voicemails.DATE, String.valueOf(System.currentTimeMillis()));
25794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        values.put(VoicemailContract.Voicemails.NUMBER, CONTACT_NUMBER);
25894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        values.put(VoicemailContract.Voicemails.MIME_TYPE, MIME_TYPE);
25994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        values.put(VoicemailContract.Voicemails.HAS_CONTENT, 1);
26094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        String packageName = getInstrumentation().getTargetContext().getPackageName();
26194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mVoicemailUri = getContentResolver().insert(
26294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                VoicemailContract.Voicemails.buildSourceUri(packageName), values);
26394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        AssetManager assets = getAssets();
26494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        OutputStream outputStream = null;
26594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        InputStream inputStream = null;
26694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        try {
26794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            inputStream = assets.open(TEST_ASSET_NAME);
26894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            outputStream = getContentResolver().openOutputStream(mVoicemailUri);
26994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            copyBetweenStreams(inputStream, outputStream);
27094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        } finally {
27194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            Closeables.closeQuietly(outputStream);
27294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            Closeables.closeQuietly(inputStream);
27394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
27494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        Uri callLogUri = ContentUris.withAppendedId(CallLog.Calls.CONTENT_URI_WITH_VOICEMAIL,
27594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                ContentUris.parseId(mVoicemailUri));
27694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        Intent intent = new Intent(Intent.ACTION_VIEW, callLogUri);
27794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_URI, mVoicemailUri);
27894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        setActivityIntent(intent);
27994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
28094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
28194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void copyBetweenStreams(InputStream in, OutputStream out) throws IOException {
28294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        byte[] buffer = new byte[1024];
28394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        int bytesRead;
28494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        int total = 0;
28594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        while ((bytesRead = in.read(buffer)) != -1) {
28694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            total += bytesRead;
28794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            out.write(buffer, 0, bytesRead);
28894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
28994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
29094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
29194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void cleanUpUri() {
29294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (mVoicemailUri != null) {
29394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            getContentResolver().delete(VoicemailContract.Voicemails.CONTENT_URI,
29494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    "_ID = ?", new String[] { String.valueOf(ContentUris.parseId(mVoicemailUri)) });
29594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            mVoicemailUri = null;
29694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
29794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (mCallLogUri != null) {
29894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            getContentResolver().delete(CallLog.Calls.CONTENT_URI_WITH_VOICEMAIL,
29994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    "_ID = ?", new String[] { String.valueOf(ContentUris.parseId(mCallLogUri)) });
30094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            mCallLogUri = null;
30194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
30294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
30394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
30494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private ContentResolver getContentResolver() {
30594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return getInstrumentation().getTargetContext().getContentResolver();
30694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
30794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
30894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private TextView assertHasOneTextViewContaining(String text) throws Throwable {
30994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        assertNotNull(mActivityUnderTest);
31094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        List<TextView> views = mTestUtils.getTextViewsWithString(mActivityUnderTest, text);
31194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        assertEquals("There should have been one TextView with text '" + text + "' but found "
31294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                + views, 1, views.size());
31394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return views.get(0);
31494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
31594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
31694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void assertZeroTextViewsContaining(String text) throws Throwable {
31794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        assertNotNull(mActivityUnderTest);
31894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        List<TextView> views = mTestUtils.getTextViewsWithString(mActivityUnderTest, text);
31994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        assertEquals("There should have been no TextViews with text '" + text + "' but found "
32094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                + views, 0,  views.size());
32194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
32294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
32394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private void startActivityUnderTest() throws Throwable {
32494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        assertNull(mActivityUnderTest);
32594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mActivityUnderTest = getActivity();
32694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        assertNotNull("activity should not be null", mActivityUnderTest);
32794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // We have to run all tasks, not just one.
32894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // This is because it seems that we can have onResume, onPause, onResume during the course
32994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        // of a single unit test.
33094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mFakeAsyncTaskExecutor.runAllTasks(UPDATE_PHONE_CALL_DETAILS);
33194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
33294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
33394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private AssetManager getAssets() {
33494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return getInstrumentation().getContext().getAssets();
33594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
33694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng}
337