diff options
author | Sarmad Hashmi <mhashmi@google.com> | 2016-02-17 17:39:36 -0800 |
---|---|---|
committer | Sarmad Hashmi <mhashmi@google.com> | 2016-03-03 17:00:10 -0800 |
commit | 3694b119843c161c3fdcf0820ec8bf755c79158f (patch) | |
tree | b861a4248db8d561d787c27ac97265dfca0ff550 /tests/src/com/android/dialer/voicemail/VoicemailActivityInstrumentationTestCase2.java | |
parent | 0a8b79ec49d7a0539adb16c19638779252583d3c (diff) |
Add files for voicemail archive tab.
+ Added VoicemailArchiveActivity which displays all voicemails archived
by the user
+ VoicemailArchivePlaybackPresenter is the same as
VoicemailPlaybackPresenter except that it does not need to request
content from the voicemail server
+Added tests (combined common methods and attributes for regular
voicemail tab and voicemail archive used for tests)
BUG=22797391
Change-Id: I4064eea945b9e257ec6c7ada35cf5ac1657dc866
Diffstat (limited to 'tests/src/com/android/dialer/voicemail/VoicemailActivityInstrumentationTestCase2.java')
-rw-r--r-- | tests/src/com/android/dialer/voicemail/VoicemailActivityInstrumentationTestCase2.java | 212 |
1 files changed, 212 insertions, 0 deletions
diff --git a/tests/src/com/android/dialer/voicemail/VoicemailActivityInstrumentationTestCase2.java b/tests/src/com/android/dialer/voicemail/VoicemailActivityInstrumentationTestCase2.java new file mode 100644 index 000000000..a992e8bd0 --- /dev/null +++ b/tests/src/com/android/dialer/voicemail/VoicemailActivityInstrumentationTestCase2.java @@ -0,0 +1,212 @@ +package com.android.dialer.voicemail; + +import android.app.Activity; +import android.content.ContentResolver; +import android.content.ContentUris; +import android.content.ContentValues; +import android.content.res.AssetManager; +import android.net.Uri; +import android.provider.VoicemailContract; +import android.test.ActivityInstrumentationTestCase2; +import android.test.suitebuilder.annotation.Suppress; +import android.view.View; + +import com.android.dialer.R; +import com.android.dialer.util.AsyncTaskExecutors; +import com.android.dialer.util.FakeAsyncTaskExecutor; +import com.android.dialer.util.LocaleTestUtils; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Locale; + +import static com.android.dialer.voicemail.VoicemailPlaybackPresenter.Tasks.CHECK_FOR_CONTENT; + + +/** + * Common methods and attributes between {@link VoicemailArchiveTest} and + * {@link VoicemailPlaybackTest}. + */ +public class VoicemailActivityInstrumentationTestCase2<T extends Activity> + extends ActivityInstrumentationTestCase2<T> { + protected static final String TEST_ASSET_NAME = "quick_test_recording.mp3"; + protected static final String MIME_TYPE = "audio/mp3"; + protected static final String CONTACT_NUMBER = "+1412555555"; + protected static final String VOICEMAIL_FILE_LOCATION = "/sdcard/sadlfj893w4j23o9sfu.mp3"; + + private T mActivity; + protected VoicemailPlaybackPresenter mPresenter; + private VoicemailPlaybackLayout mLayout; + + protected Uri mVoicemailUri; + private LocaleTestUtils mLocaleTestUtils; + protected FakeAsyncTaskExecutor mFakeAsyncTaskExecutor; + + public VoicemailActivityInstrumentationTestCase2(Class<T> activityClass) { + super(activityClass); + } + + @Override + public void setUp() throws Exception { + super.setUp(); + + mFakeAsyncTaskExecutor = new FakeAsyncTaskExecutor(getInstrumentation()); + AsyncTaskExecutors.setFactoryForTest(mFakeAsyncTaskExecutor.getFactory()); + + // Some of the tests rely on the text - safest to force a specific locale. + mLocaleTestUtils = new LocaleTestUtils(getInstrumentation().getTargetContext()); + mLocaleTestUtils.setLocale(Locale.US); + + mActivity = getActivity(); + mLayout = new VoicemailPlaybackLayout(mActivity); + mLayout.onFinishInflate(); + } + + @Override + protected void tearDown() throws Exception { + cleanUpVoicemailUri(); + + mLocaleTestUtils.restoreLocale(); + mLocaleTestUtils = null; + + mPresenter.clearInstance(); + AsyncTaskExecutors.setFactoryForTest(null); + + mActivity = null; + mPresenter = null; + mLayout = null; + + super.tearDown(); + } + + @Suppress + public void testFetchingVoicemail() throws Throwable { + setUriForUnfetchedVoicemailEntry(); + setPlaybackViewForPresenter(); + + getInstrumentation().runOnMainSync(new Runnable() { + @Override + public void run() { + mPresenter.resumePlayback(); + assertStateTextContains("Loading voicemail"); + } + }); + } + + @Suppress + public void testInvalidVoicemailShowsErrorMessage() throws Throwable { + setUriForInvalidVoicemailEntry(); + setPlaybackViewForPresenter(); + + getInstrumentation().runOnMainSync(new Runnable() { + @Override + public void run() { + mPresenter.resumePlayback(); + } + }); + mFakeAsyncTaskExecutor.runTask(CHECK_FOR_CONTENT); + getInstrumentation().waitForIdleSync(); + + // The media player will have thrown an IOException since the file doesn't exist. + // This should have put a failed to play message on screen, buffering is gone. + assertStateTextContains("Couldn't play voicemail"); + assertStateTextNotContains("Buffering"); + } + + public void testClickingSpeakerphoneButton() throws Throwable { + setUriForRealFileVoicemailEntry(); + setPlaybackViewForPresenter(); + + // Check that the speakerphone is false to start. + assertFalse(mPresenter.isSpeakerphoneOn()); + + View speakerphoneButton = mLayout.findViewById(R.id.playback_speakerphone); + speakerphoneButton.performClick(); + assertTrue(mPresenter.isSpeakerphoneOn()); + } + + protected void cleanUpVoicemailUri() { + if (mVoicemailUri != null) { + getContentResolver().delete(VoicemailContract.Voicemails.CONTENT_URI, + "_ID = ?", new String[] { String.valueOf(ContentUris.parseId(mVoicemailUri)) }); + mVoicemailUri = null; + } + } + + protected void setUriForRealFileVoicemailEntry() throws IOException { + assertNull(mVoicemailUri); + ContentValues values = new ContentValues(); + values.put(VoicemailContract.Voicemails.DATE, String.valueOf(System.currentTimeMillis())); + values.put(VoicemailContract.Voicemails.NUMBER, CONTACT_NUMBER); + values.put(VoicemailContract.Voicemails.MIME_TYPE, MIME_TYPE); + values.put(VoicemailContract.Voicemails.HAS_CONTENT, 1); + String packageName = getInstrumentation().getTargetContext().getPackageName(); + mVoicemailUri = getContentResolver().insert( + VoicemailContract.Voicemails.buildSourceUri(packageName), values); + AssetManager assets = getAssets(); + try (InputStream inputStream = assets.open(TEST_ASSET_NAME); + OutputStream outputStream = getContentResolver().openOutputStream(mVoicemailUri)) { + copyBetweenStreams(inputStream, outputStream); + } + } + + protected void setUriForUnfetchedVoicemailEntry() { + assertNull(mVoicemailUri); + ContentValues values = new ContentValues(); + values.put(VoicemailContract.Voicemails.DATE, String.valueOf(System.currentTimeMillis())); + values.put(VoicemailContract.Voicemails.NUMBER, CONTACT_NUMBER); + values.put(VoicemailContract.Voicemails.MIME_TYPE, MIME_TYPE); + values.put(VoicemailContract.Voicemails.HAS_CONTENT, 0); + String packageName = getInstrumentation().getTargetContext().getPackageName(); + mVoicemailUri = getContentResolver().insert( + VoicemailContract.Voicemails.buildSourceUri(packageName), values); + } + + protected void setUriForInvalidVoicemailEntry() { + assertNull(mVoicemailUri); + ContentResolver contentResolver = getContentResolver(); + ContentValues values = new ContentValues(); + values.put(VoicemailContract.Voicemails.NUMBER, CONTACT_NUMBER); + values.put(VoicemailContract.Voicemails.HAS_CONTENT, 1); + // VoicemailContract.Voicemails._DATA + values.put("_data", VOICEMAIL_FILE_LOCATION); + mVoicemailUri = contentResolver.insert(VoicemailContract.Voicemails.CONTENT_URI, values); + } + + protected void setPlaybackViewForPresenter() { + getInstrumentation().runOnMainSync(new Runnable() { + @Override + public void run() { + mPresenter.setPlaybackView(mLayout, mVoicemailUri, false); + } + }); + } + + protected void copyBetweenStreams(InputStream in, OutputStream out) throws IOException { + byte[] buffer = new byte[1024]; + int bytesRead; + while ((bytesRead = in.read(buffer)) > 0) { + out.write(buffer, 0, bytesRead); + } + } + + protected void assertStateTextContains(String text) { + assertNotNull(mLayout); + assertTrue(mLayout.getStateText().contains(text)); + } + + protected void assertStateTextNotContains(String text) { + assertNotNull(mLayout); + assertFalse(mLayout.getStateText().contains(text)); + } + + protected ContentResolver getContentResolver() { + return getInstrumentation().getTargetContext().getContentResolver(); + } + + protected AssetManager getAssets() { + return getInstrumentation().getContext().getAssets(); + } + +} |