1 /* 2 * Copyright (C) 2016 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.voicemail; 18 19 import android.app.Activity; 20 import android.content.ContentResolver; 21 import android.content.ContentUris; 22 import android.content.ContentValues; 23 import android.content.res.AssetManager; 24 import android.net.Uri; 25 import android.provider.VoicemailContract; 26 import android.test.ActivityInstrumentationTestCase2; 27 import android.test.suitebuilder.annotation.Suppress; 28 import android.view.View; 29 30 import com.android.dialer.R; 31 import com.android.dialer.util.AsyncTaskExecutors; 32 import com.android.dialer.util.FakeAsyncTaskExecutor; 33 import com.android.dialer.util.LocaleTestUtils; 34 35 import java.io.IOException; 36 import java.io.InputStream; 37 import java.io.OutputStream; 38 import java.util.Locale; 39 40 import static com.android.dialer.voicemail.VoicemailPlaybackPresenter.Tasks.CHECK_FOR_CONTENT; 41 42 /** 43 * Common methods and attributes between {@link VoicemailArchiveTest} and 44 * {@link VoicemailPlaybackTest}. 45 */ 46 public class VoicemailActivityInstrumentationTestCase2<T extends Activity> 47 extends ActivityInstrumentationTestCase2<T> { 48 protected static final String TEST_ASSET_NAME = "quick_test_recording.mp3"; 49 protected static final String MIME_TYPE = "audio/mp3"; 50 protected static final String CONTACT_NUMBER = "+1412555555"; 51 protected static final String VOICEMAIL_FILE_LOCATION = "/sdcard/sadlfj893w4j23o9sfu.mp3"; 52 53 private T mActivity; 54 protected VoicemailPlaybackPresenter mPresenter; 55 private VoicemailPlaybackLayout mLayout; 56 57 protected Uri mVoicemailUri; 58 private LocaleTestUtils mLocaleTestUtils; 59 protected FakeAsyncTaskExecutor mFakeAsyncTaskExecutor; 60 VoicemailActivityInstrumentationTestCase2(Class<T> activityClass)61 public VoicemailActivityInstrumentationTestCase2(Class<T> activityClass) { 62 super(activityClass); 63 } 64 65 @Override setUp()66 public void setUp() throws Exception { 67 super.setUp(); 68 69 mFakeAsyncTaskExecutor = new FakeAsyncTaskExecutor(getInstrumentation()); 70 AsyncTaskExecutors.setFactoryForTest(mFakeAsyncTaskExecutor.getFactory()); 71 72 // Some of the tests rely on the text - safest to force a specific locale. 73 mLocaleTestUtils = new LocaleTestUtils(getInstrumentation().getTargetContext()); 74 mLocaleTestUtils.setLocale(Locale.US); 75 76 mActivity = getActivity(); 77 mLayout = new VoicemailPlaybackLayout(mActivity); 78 mLayout.onFinishInflate(); 79 } 80 81 @Override tearDown()82 protected void tearDown() throws Exception { 83 cleanUpVoicemailUri(); 84 85 mLocaleTestUtils.restoreLocale(); 86 mLocaleTestUtils = null; 87 88 mPresenter.clearInstance(); 89 AsyncTaskExecutors.setFactoryForTest(null); 90 91 mActivity = null; 92 mPresenter = null; 93 mLayout = null; 94 95 super.tearDown(); 96 } 97 98 @Suppress testFetchingVoicemail()99 public void testFetchingVoicemail() throws Throwable { 100 setUriForUnfetchedVoicemailEntry(); 101 setPlaybackViewForPresenter(); 102 103 getInstrumentation().runOnMainSync(new Runnable() { 104 @Override 105 public void run() { 106 mPresenter.resumePlayback(); 107 assertStateTextContains("Loading voicemail"); 108 } 109 }); 110 } 111 112 @Suppress testInvalidVoicemailShowsErrorMessage()113 public void testInvalidVoicemailShowsErrorMessage() throws Throwable { 114 setUriForInvalidVoicemailEntry(); 115 setPlaybackViewForPresenter(); 116 117 getInstrumentation().runOnMainSync(new Runnable() { 118 @Override 119 public void run() { 120 mPresenter.resumePlayback(); 121 } 122 }); 123 mFakeAsyncTaskExecutor.runTask(CHECK_FOR_CONTENT); 124 getInstrumentation().waitForIdleSync(); 125 126 // The media player will have thrown an IOException since the file doesn't exist. 127 // This should have put a failed to play message on screen, buffering is gone. 128 assertStateTextContains("Couldn't play voicemail"); 129 assertStateTextNotContains("Buffering"); 130 } 131 testClickingSpeakerphoneButton()132 public void testClickingSpeakerphoneButton() throws Throwable { 133 setUriForRealFileVoicemailEntry(); 134 setPlaybackViewForPresenter(); 135 136 // Check that the speakerphone is false to start. 137 assertFalse(mPresenter.isSpeakerphoneOn()); 138 139 View speakerphoneButton = mLayout.findViewById(R.id.playback_speakerphone); 140 speakerphoneButton.performClick(); 141 assertTrue(mPresenter.isSpeakerphoneOn()); 142 } 143 cleanUpVoicemailUri()144 protected void cleanUpVoicemailUri() { 145 if (mVoicemailUri != null) { 146 getContentResolver().delete(VoicemailContract.Voicemails.CONTENT_URI, 147 "_ID = ?", new String[] { String.valueOf(ContentUris.parseId(mVoicemailUri)) }); 148 mVoicemailUri = null; 149 } 150 } 151 setUriForRealFileVoicemailEntry()152 protected void setUriForRealFileVoicemailEntry() throws IOException { 153 assertNull(mVoicemailUri); 154 ContentValues values = new ContentValues(); 155 values.put(VoicemailContract.Voicemails.DATE, String.valueOf(System.currentTimeMillis())); 156 values.put(VoicemailContract.Voicemails.NUMBER, CONTACT_NUMBER); 157 values.put(VoicemailContract.Voicemails.MIME_TYPE, MIME_TYPE); 158 values.put(VoicemailContract.Voicemails.HAS_CONTENT, 1); 159 String packageName = getInstrumentation().getTargetContext().getPackageName(); 160 mVoicemailUri = getContentResolver().insert( 161 VoicemailContract.Voicemails.buildSourceUri(packageName), values); 162 AssetManager assets = getAssets(); 163 try (InputStream inputStream = assets.open(TEST_ASSET_NAME); 164 OutputStream outputStream = getContentResolver().openOutputStream(mVoicemailUri)) { 165 copyBetweenStreams(inputStream, outputStream); 166 } 167 } 168 setUriForUnfetchedVoicemailEntry()169 protected void setUriForUnfetchedVoicemailEntry() { 170 assertNull(mVoicemailUri); 171 ContentValues values = new ContentValues(); 172 values.put(VoicemailContract.Voicemails.DATE, String.valueOf(System.currentTimeMillis())); 173 values.put(VoicemailContract.Voicemails.NUMBER, CONTACT_NUMBER); 174 values.put(VoicemailContract.Voicemails.MIME_TYPE, MIME_TYPE); 175 values.put(VoicemailContract.Voicemails.HAS_CONTENT, 0); 176 String packageName = getInstrumentation().getTargetContext().getPackageName(); 177 mVoicemailUri = getContentResolver().insert( 178 VoicemailContract.Voicemails.buildSourceUri(packageName), values); 179 } 180 setUriForInvalidVoicemailEntry()181 protected void setUriForInvalidVoicemailEntry() { 182 assertNull(mVoicemailUri); 183 ContentResolver contentResolver = getContentResolver(); 184 ContentValues values = new ContentValues(); 185 values.put(VoicemailContract.Voicemails.NUMBER, CONTACT_NUMBER); 186 values.put(VoicemailContract.Voicemails.HAS_CONTENT, 1); 187 // VoicemailContract.Voicemails._DATA 188 values.put("_data", VOICEMAIL_FILE_LOCATION); 189 mVoicemailUri = contentResolver.insert(VoicemailContract.Voicemails.CONTENT_URI, values); 190 } 191 setPlaybackViewForPresenter()192 protected void setPlaybackViewForPresenter() { 193 getInstrumentation().runOnMainSync(new Runnable() { 194 @Override 195 public void run() { 196 mPresenter.setPlaybackView(mLayout, mVoicemailUri, false); 197 } 198 }); 199 } 200 copyBetweenStreams(InputStream in, OutputStream out)201 protected void copyBetweenStreams(InputStream in, OutputStream out) throws IOException { 202 byte[] buffer = new byte[1024]; 203 int bytesRead; 204 while ((bytesRead = in.read(buffer)) > 0) { 205 out.write(buffer, 0, bytesRead); 206 } 207 } 208 assertStateTextContains(String text)209 protected void assertStateTextContains(String text) { 210 assertNotNull(mLayout); 211 assertTrue(mLayout.getStateText().contains(text)); 212 } 213 assertStateTextNotContains(String text)214 protected void assertStateTextNotContains(String text) { 215 assertNotNull(mLayout); 216 assertFalse(mLayout.getStateText().contains(text)); 217 } 218 getContentResolver()219 protected ContentResolver getContentResolver() { 220 return getInstrumentation().getTargetContext().getContentResolver(); 221 } 222 getAssets()223 protected AssetManager getAssets() { 224 return getInstrumentation().getContext().getAssets(); 225 } 226 227 } 228