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