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; 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.PopupMenu; 33 import android.widget.TextView; 34 35 import com.android.dialer.calllog.CallLogAsyncTaskUtil; 36 import com.android.dialer.util.AppCompatConstants; 37 import com.android.dialer.util.AsyncTaskExecutors; 38 import com.android.dialer.util.FakeAsyncTaskExecutor; 39 40 /** 41 * Unit tests for the {@link CallDetailActivity}. NOTE: The screen needs to be on for the 42 * UI-related tests to pass. 43 */ 44 @LargeTest 45 public class CallDetailActivityTest extends ActivityInstrumentationTestCase2<CallDetailActivity> { 46 private static final String CONTACT_NUMBER = "+1412555555"; 47 private static final String VOICEMAIL_FILE_LOCATION = "/sdcard/sadlfj893w4j23o9sfu.mp3"; 48 49 private Uri mCallLogUri; 50 private Uri mVoicemailUri; 51 private FakeAsyncTaskExecutor mFakeAsyncTaskExecutor; 52 private CallDetailActivity mActivityUnderTest; 53 CallDetailActivityTest()54 public CallDetailActivityTest() { 55 super(CallDetailActivity.class); 56 } 57 58 @Override setUp()59 protected void setUp() throws Exception { 60 super.setUp(); 61 62 mFakeAsyncTaskExecutor = new FakeAsyncTaskExecutor(getInstrumentation()); 63 AsyncTaskExecutors.setFactoryForTest(mFakeAsyncTaskExecutor.getFactory()); 64 65 // I don't like the default of focus-mode for tests, the green focus border makes the 66 // screenshots look weak. 67 setActivityInitialTouchMode(true); 68 } 69 70 @Override tearDown()71 protected void tearDown() throws Exception { 72 cleanUpUri(); 73 74 AsyncTaskExecutors.setFactoryForTest(null); 75 CallLogAsyncTaskUtil.resetForTest(); 76 77 super.tearDown(); 78 } 79 80 /** Test for bug where missing Extras on intent used to start Activity causes NPE. */ testCallLogUriWithMissingExtrasShouldNotCauseNPE()81 public void testCallLogUriWithMissingExtrasShouldNotCauseNPE() throws Throwable { 82 setActivityIntentForTestCallEntry(); 83 startActivityUnderTest(); 84 } 85 86 /** 87 * Verifies the trash menu item is present and a voicemail URI is set. 88 */ 89 @Suppress testVoicemailDeleteButton()90 public void testVoicemailDeleteButton() throws Throwable { 91 setActivityIntentForTestVoicemailEntry(); 92 startActivityUnderTest(); 93 mFakeAsyncTaskExecutor.runTask(Tasks.GET_CALL_DETAILS); 94 95 Menu optionsMenu = (new PopupMenu(mActivityUnderTest, null)).getMenu(); 96 mActivityUnderTest.onCreateOptionsMenu(optionsMenu); 97 mActivityUnderTest.onPrepareOptionsMenu(optionsMenu); 98 99 assertTrue(mActivityUnderTest.hasVoicemail()); 100 mActivityUnderTest.runOnUiThread(new Runnable() { 101 public void run() { 102 mActivityUnderTest.findViewById(R.id.call_detail_delete_menu_item).performClick(); 103 } 104 }); 105 getInstrumentation().waitForIdleSync(); 106 mFakeAsyncTaskExecutor.runTask(Tasks.DELETE_VOICEMAIL); 107 } 108 109 /** 110 * Verifies the trash menu item is present and a voicemail URI is not set. 111 */ 112 @Suppress testRegularCallDoesHaveRemoveFromCallLog()113 public void testRegularCallDoesHaveRemoveFromCallLog() throws Throwable { 114 setActivityIntentForTestCallEntry(); 115 startActivityUnderTest(); 116 mFakeAsyncTaskExecutor.runTask(Tasks.GET_CALL_DETAILS); 117 118 Menu optionsMenu = (new PopupMenu(mActivityUnderTest, null)).getMenu(); 119 mActivityUnderTest.onCreateOptionsMenu(optionsMenu); 120 mActivityUnderTest.onPrepareOptionsMenu(optionsMenu); 121 122 assertFalse(mActivityUnderTest.hasVoicemail()); 123 mActivityUnderTest.runOnUiThread(new Runnable() { 124 public void run() { 125 mActivityUnderTest.findViewById(R.id.call_detail_delete_menu_item).performClick(); 126 } 127 }); 128 getInstrumentation().waitForIdleSync(); 129 mFakeAsyncTaskExecutor.runTask(Tasks.DELETE_CALL); 130 } 131 setActivityIntentForTestCallEntry()132 private void setActivityIntentForTestCallEntry() { 133 assertNull(mVoicemailUri); 134 assertNull(mCallLogUri); 135 ContentResolver contentResolver = getContentResolver(); 136 ContentValues values = new ContentValues(); 137 values.put(CallLog.Calls.NUMBER, CONTACT_NUMBER); 138 values.put(CallLog.Calls.NUMBER_PRESENTATION, CallLog.Calls.PRESENTATION_ALLOWED); 139 values.put(CallLog.Calls.TYPE, AppCompatConstants.CALLS_INCOMING_TYPE); 140 mCallLogUri = contentResolver.insert(CallLog.Calls.CONTENT_URI, values); 141 setActivityIntent(new Intent(Intent.ACTION_VIEW, mCallLogUri)); 142 } 143 setActivityIntentForTestVoicemailEntry()144 private void setActivityIntentForTestVoicemailEntry() { 145 assertNull(mVoicemailUri); 146 ContentResolver contentResolver = getContentResolver(); 147 ContentValues values = new ContentValues(); 148 values.put(VoicemailContract.Voicemails.NUMBER, CONTACT_NUMBER); 149 values.put(VoicemailContract.Voicemails.HAS_CONTENT, 1); 150 // VoicemailContract.Voicemails._DATA 151 values.put("_data", VOICEMAIL_FILE_LOCATION); 152 mVoicemailUri = contentResolver.insert(VoicemailContract.Voicemails.CONTENT_URI, values); 153 154 Uri callLogUri = ContentUris.withAppendedId(CallLog.Calls.CONTENT_URI_WITH_VOICEMAIL, 155 ContentUris.parseId(mVoicemailUri)); 156 Intent intent = new Intent(Intent.ACTION_VIEW, callLogUri); 157 intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_URI, mVoicemailUri); 158 setActivityIntent(intent); 159 } 160 cleanUpUri()161 private void cleanUpUri() { 162 if (mVoicemailUri != null) { 163 getContentResolver().delete(VoicemailContract.Voicemails.CONTENT_URI, 164 "_ID = ?", new String[] { String.valueOf(ContentUris.parseId(mVoicemailUri)) }); 165 mVoicemailUri = null; 166 } 167 if (mCallLogUri != null) { 168 getContentResolver().delete(CallLog.Calls.CONTENT_URI_WITH_VOICEMAIL, 169 "_ID = ?", new String[] { String.valueOf(ContentUris.parseId(mCallLogUri)) }); 170 mCallLogUri = null; 171 } 172 } 173 getContentResolver()174 private ContentResolver getContentResolver() { 175 return getInstrumentation().getTargetContext().getContentResolver(); 176 } 177 startActivityUnderTest()178 private void startActivityUnderTest() throws Throwable { 179 assertNull(mActivityUnderTest); 180 mActivityUnderTest = getActivity(); 181 assertNotNull("activity should not be null", mActivityUnderTest); 182 } 183 } 184