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 17 package com.android.dialer; 18 19 import static com.android.dialer.calllog.CallLogAsyncTaskUtil.Tasks.GET_CALL_DETAILS; 20 21 import android.content.ContentResolver; 22 import android.content.ContentUris; 23 import android.content.ContentValues; 24 import android.content.Intent; 25 import android.net.Uri; 26 import android.provider.CallLog; 27 import android.provider.VoicemailContract; 28 import android.test.ActivityInstrumentationTestCase2; 29 import android.test.suitebuilder.annotation.LargeTest; 30 import android.test.suitebuilder.annotation.Suppress; 31 import android.view.Menu; 32 import android.widget.TextView; 33 34 import com.android.dialer.calllog.CallLogAsyncTaskUtil; 35 import com.android.dialer.util.AsyncTaskExecutors; 36 import com.android.dialer.util.FakeAsyncTaskExecutor; 37 import com.android.internal.view.menu.ContextMenuBuilder; 38 39 /** 40 * Unit tests for the {@link CallDetailActivity}. NOTE: The screen needs to be on for the 41 * UI-related tests to pass. 42 */ 43 @LargeTest 44 public class CallDetailActivityTest extends ActivityInstrumentationTestCase2<CallDetailActivity> { 45 private static final String CONTACT_NUMBER = "+1412555555"; 46 private static final String VOICEMAIL_FILE_LOCATION = "/sdcard/sadlfj893w4j23o9sfu.mp3"; 47 48 private Uri mCallLogUri; 49 private Uri mVoicemailUri; 50 private FakeAsyncTaskExecutor mFakeAsyncTaskExecutor; 51 private CallDetailActivity mActivityUnderTest; 52 CallDetailActivityTest()53 public CallDetailActivityTest() { 54 super(CallDetailActivity.class); 55 } 56 57 @Override setUp()58 protected void setUp() throws Exception { 59 super.setUp(); 60 61 mFakeAsyncTaskExecutor = new FakeAsyncTaskExecutor(getInstrumentation()); 62 AsyncTaskExecutors.setFactoryForTest(mFakeAsyncTaskExecutor.getFactory()); 63 64 // I don't like the default of focus-mode for tests, the green focus border makes the 65 // screenshots look weak. 66 setActivityInitialTouchMode(true); 67 } 68 69 @Override tearDown()70 protected void tearDown() throws Exception { 71 cleanUpUri(); 72 73 AsyncTaskExecutors.setFactoryForTest(null); 74 CallLogAsyncTaskUtil.resetForTest(); 75 76 super.tearDown(); 77 } 78 79 /** Test for bug where missing Extras on intent used to start Activity causes NPE. */ testCallLogUriWithMissingExtrasShouldNotCauseNPE()80 public void testCallLogUriWithMissingExtrasShouldNotCauseNPE() throws Throwable { 81 setActivityIntentForTestCallEntry(); 82 startActivityUnderTest(); 83 } 84 85 /** 86 * Test for bug where voicemails should not have remove-from-call-log entry. 87 * <p> 88 * See http://b/5054103. 89 */ testVoicemailDoesNotHaveRemoveFromCallLog()90 public void testVoicemailDoesNotHaveRemoveFromCallLog() throws Throwable { 91 setActivityIntentForTestVoicemailEntry(); 92 startActivityUnderTest(); 93 mFakeAsyncTaskExecutor.runTask(GET_CALL_DETAILS); 94 95 Menu menu = new ContextMenuBuilder(mActivityUnderTest); 96 mActivityUnderTest.onCreateOptionsMenu(menu); 97 mActivityUnderTest.onPrepareOptionsMenu(menu); 98 assertFalse(menu.findItem(R.id.menu_remove_from_call_log).isVisible()); 99 assertTrue(menu.findItem(R.id.menu_trash).isVisible()); 100 } 101 102 /** 103 * Test to check that I haven't broken the remove-from-call-log entry from regular calls. 104 */ testRegularCallDoesHaveRemoveFromCallLog()105 public void testRegularCallDoesHaveRemoveFromCallLog() throws Throwable { 106 setActivityIntentForTestCallEntry(); 107 startActivityUnderTest(); 108 mFakeAsyncTaskExecutor.runTask(GET_CALL_DETAILS); 109 110 Menu menu = new ContextMenuBuilder(mActivityUnderTest); 111 mActivityUnderTest.onCreateOptionsMenu(menu); 112 mActivityUnderTest.onPrepareOptionsMenu(menu); 113 assertTrue(menu.findItem(R.id.menu_remove_from_call_log).isVisible()); 114 assertFalse(menu.findItem(R.id.menu_trash).isVisible()); 115 } 116 setActivityIntentForTestCallEntry()117 private void setActivityIntentForTestCallEntry() { 118 assertNull(mVoicemailUri); 119 assertNull(mCallLogUri); 120 ContentResolver contentResolver = getContentResolver(); 121 ContentValues values = new ContentValues(); 122 values.put(CallLog.Calls.NUMBER, CONTACT_NUMBER); 123 values.put(CallLog.Calls.NUMBER_PRESENTATION, CallLog.Calls.PRESENTATION_ALLOWED); 124 values.put(CallLog.Calls.TYPE, CallLog.Calls.INCOMING_TYPE); 125 mCallLogUri = contentResolver.insert(CallLog.Calls.CONTENT_URI, values); 126 setActivityIntent(new Intent(Intent.ACTION_VIEW, mCallLogUri)); 127 } 128 setActivityIntentForTestVoicemailEntry()129 private void setActivityIntentForTestVoicemailEntry() { 130 assertNull(mVoicemailUri); 131 ContentResolver contentResolver = getContentResolver(); 132 ContentValues values = new ContentValues(); 133 values.put(VoicemailContract.Voicemails.NUMBER, CONTACT_NUMBER); 134 values.put(VoicemailContract.Voicemails.HAS_CONTENT, 1); 135 values.put(VoicemailContract.Voicemails._DATA, VOICEMAIL_FILE_LOCATION); 136 mVoicemailUri = contentResolver.insert(VoicemailContract.Voicemails.CONTENT_URI, values); 137 138 Uri callLogUri = ContentUris.withAppendedId(CallLog.Calls.CONTENT_URI_WITH_VOICEMAIL, 139 ContentUris.parseId(mVoicemailUri)); 140 Intent intent = new Intent(Intent.ACTION_VIEW, callLogUri); 141 intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_URI, mVoicemailUri); 142 setActivityIntent(intent); 143 } 144 cleanUpUri()145 private void cleanUpUri() { 146 if (mVoicemailUri != null) { 147 getContentResolver().delete(VoicemailContract.Voicemails.CONTENT_URI, 148 "_ID = ?", new String[] { String.valueOf(ContentUris.parseId(mVoicemailUri)) }); 149 mVoicemailUri = null; 150 } 151 if (mCallLogUri != null) { 152 getContentResolver().delete(CallLog.Calls.CONTENT_URI_WITH_VOICEMAIL, 153 "_ID = ?", new String[] { String.valueOf(ContentUris.parseId(mCallLogUri)) }); 154 mCallLogUri = null; 155 } 156 } 157 getContentResolver()158 private ContentResolver getContentResolver() { 159 return getInstrumentation().getTargetContext().getContentResolver(); 160 } 161 startActivityUnderTest()162 private void startActivityUnderTest() throws Throwable { 163 assertNull(mActivityUnderTest); 164 mActivityUnderTest = getActivity(); 165 assertNotNull("activity should not be null", mActivityUnderTest); 166 } 167 } 168