diff options
Diffstat (limited to 'tests/src/com')
9 files changed, 896 insertions, 13 deletions
diff --git a/tests/src/com/android/dialer/compat/FilteredNumberCompatTest.java b/tests/src/com/android/dialer/compat/FilteredNumberCompatTest.java index b9eb21424..a57e1c29c 100644 --- a/tests/src/com/android/dialer/compat/FilteredNumberCompatTest.java +++ b/tests/src/com/android/dialer/compat/FilteredNumberCompatTest.java @@ -16,19 +16,30 @@ package com.android.dialer.compat; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.when; + import android.content.ContentUris; import android.content.ContentValues; +import android.content.Context; +import android.content.SharedPreferences; import android.net.Uri; import android.provider.BlockedNumberContract.BlockedNumbers; import android.test.AndroidTestCase; import android.test.suitebuilder.annotation.SmallTest; import com.android.contacts.common.compat.CompatUtils; +import com.android.dialer.DialerApplication; import com.android.dialer.database.FilteredNumberContract.FilteredNumber; import com.android.dialer.database.FilteredNumberContract.FilteredNumberColumns; import com.android.dialer.database.FilteredNumberContract.FilteredNumberSources; import com.android.dialer.database.FilteredNumberContract.FilteredNumberTypes; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + import java.util.Arrays; @SmallTest @@ -41,27 +52,52 @@ public class FilteredNumberCompatTest extends AndroidTestCase { private static final Uri EXPECTED_BASE_URI = CompatUtils.isNCompatible() ? BlockedNumbers.CONTENT_URI : FilteredNumber.CONTENT_URI; + @Mock private Context mContext; + @Mock private SharedPreferences mSharedPreferences; + @Override public void setUp() throws Exception { super.setUp(); + MockitoAnnotations.initMocks(this); + DialerApplication.setContextForTest(mContext); + when(mContext.getSharedPreferences(anyString(), anyInt())).thenReturn(mSharedPreferences); FilteredNumberCompat.setIsEnabledForTest(true); } public void testIsNewFilteringEnabled_TestValueFalse() { FilteredNumberCompat.setIsEnabledForTest(false); - assertFalse(FilteredNumberCompat.useNewFiltering()); + assertFalse(FilteredNumberCompat.canUseNewFiltering()); } public void testIsNewFilteringEnabled_TestValueTrue() { FilteredNumberCompat.setIsEnabledForTest(true); - assertEquals(CompatUtils.isNCompatible(), FilteredNumberCompat.useNewFiltering()); + assertEquals(CompatUtils.isNCompatible(), FilteredNumberCompat.canUseNewFiltering()); + } + + public void testHasMigratedToNewBlocking_False() { + assertFalse(FilteredNumberCompat.hasMigratedToNewBlocking()); + } + + public void testHasMigratedToNewBlocking_Migrated() { + when(mSharedPreferences + .getBoolean(FilteredNumberCompat.HAS_MIGRATED_TO_NEW_BLOCKING_KEY, false)) + .thenReturn(true); + assertTrue(FilteredNumberCompat.hasMigratedToNewBlocking()); } public void testGetContentUri_NullId() { - assertEquals(EXPECTED_BASE_URI, FilteredNumberCompat.getContentUri(null)); + assertEquals(FilteredNumber.CONTENT_URI, FilteredNumberCompat.getContentUri(null)); } - public void testGetContentUri() { + public void testGetContentUri_NotMigrated() { + assertEquals(ContentUris.withAppendedId(FilteredNumber.CONTENT_URI, 1), + FilteredNumberCompat.getContentUri(1)); + } + + public void testGetContentUri_Migrated() { + when(mSharedPreferences + .getBoolean(FilteredNumberCompat.HAS_MIGRATED_TO_NEW_BLOCKING_KEY, false)) + .thenReturn(true); assertEquals(ContentUris.withAppendedId(EXPECTED_BASE_URI, 1), FilteredNumberCompat.getContentUri(1)); } @@ -87,12 +123,23 @@ public class FilteredNumberCompatTest extends AndroidTestCase { } catch (NullPointerException e) {} } - public void testNewBlockNumberContentValues_N() { + public void testNewBlockNumberContentValues_N_NotMigrated() { + if (!CompatUtils.isNCompatible()) { + return; + } + assertEquals(newExpectedContentValuesM(NON_E164_NUMBER, null, null), + FilteredNumberCompat.newBlockNumberContentValues(NON_E164_NUMBER, null, null)); + } + + public void testNewBlockNumberContentValues_N_Migrated() { if (!CompatUtils.isNCompatible()) { return; } ContentValues contentValues = new ContentValues(); contentValues.put(BlockedNumbers.COLUMN_ORIGINAL_NUMBER, NON_E164_NUMBER); + when(mSharedPreferences + .getBoolean(FilteredNumberCompat.HAS_MIGRATED_TO_NEW_BLOCKING_KEY, false)) + .thenReturn(true); assertEquals(contentValues, FilteredNumberCompat.newBlockNumberContentValues( NON_E164_NUMBER, null, null)); diff --git a/tests/src/com/android/dialer/database/VoicemailArchiveProviderTest.java b/tests/src/com/android/dialer/database/VoicemailArchiveProviderTest.java index cec7e050d..abc2dc5b0 100644 --- a/tests/src/com/android/dialer/database/VoicemailArchiveProviderTest.java +++ b/tests/src/com/android/dialer/database/VoicemailArchiveProviderTest.java @@ -79,6 +79,33 @@ public class VoicemailArchiveProviderTest extends assertTrue(doesFileExist()); } + public void testQuery_createFileMimeTypeIsAMR() throws Exception { + insertVoicemailWithMimeType("audio/amr"); + assertTrue(doesRowExist()); + assertFalse(doesFileExist()); + createFile(); + assertTrue(doesFileExist()); + assertEquals("amr", getFileExtension(getFilePath())); + } + + public void testQuery_createFileMimeTypeIsMP3() throws Exception { + insertVoicemailWithMimeType("audio/mpeg"); + assertTrue(doesRowExist()); + assertFalse(doesFileExist()); + createFile(); + assertTrue(doesFileExist()); + assertEquals("mp3", getFileExtension(getFilePath())); + } + + public void testQuery_createFileMimeTypeNotExists() throws Exception { + insertVoicemailWithMimeType(TEST_STRING); + assertTrue(doesRowExist()); + assertFalse(doesFileExist()); + createFile(); + assertTrue(doesFileExist()); + assertEquals("", getFileExtension(getFilePath())); + } + public void testQuery() { insertVoicemail(); updateCursor(); @@ -179,6 +206,13 @@ public class VoicemailArchiveProviderTest extends return voicemailFile.exists(); } + private static String getFileExtension(String filePath) { + File file = new File(filePath); + String fileName = file.getName(); + int index = fileName.lastIndexOf("."); + return index > 0 ? fileName.substring(index + 1) : ""; + } + private void assertCursorCount(int count) { assertEquals(count, mCursor.getCount()); } @@ -196,6 +230,12 @@ public class VoicemailArchiveProviderTest extends mVoicemailUri = mResolver.insert(VoicemailArchive.CONTENT_URI, getTestValues()); } + private void insertVoicemailWithMimeType(String mimeType) { + ContentValues values = getTestValues(); + values.put(VoicemailArchive.MIME_TYPE, mimeType); + mVoicemailUri = mResolver.insert(VoicemailArchive.CONTENT_URI, values); + } + private void updateCursor() { mCursor = mResolver.query(mVoicemailUri, null, null, null, null); assertEquals(true, mCursor.getCount() > 0); @@ -226,7 +266,7 @@ public class VoicemailArchiveProviderTest extends return mCursor.getString(mCursor.getColumnIndex(VoicemailArchive._DATA)); } - private ContentValues getTestValues() { + private static ContentValues getTestValues() { ContentValues values = new ContentValues(); values.put(VoicemailArchive.NUMBER, TEST_NUMBER); values.put(VoicemailArchive.MIME_TYPE, TEST_MIME_TYPE); diff --git a/tests/src/com/android/dialer/filterednumber/BlockedNumbersMigratorTest.java b/tests/src/com/android/dialer/filterednumber/BlockedNumbersMigratorTest.java new file mode 100644 index 000000000..565c206d8 --- /dev/null +++ b/tests/src/com/android/dialer/filterednumber/BlockedNumbersMigratorTest.java @@ -0,0 +1,160 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.dialer.filterednumber; + +import android.content.ContentUris; +import android.content.ContentValues; +import android.provider.BlockedNumberContract; +import android.provider.BlockedNumberContract.BlockedNumbers; +import android.test.AndroidTestCase; +import android.test.mock.MockContentResolver; + +import com.android.contacts.common.compat.CompatUtils; +import com.android.contacts.common.test.mocks.MockContentProvider; +import com.android.dialer.compat.FilteredNumberCompat; +import com.android.dialer.database.FilteredNumberContract; +import com.android.dialer.database.FilteredNumberContract.FilteredNumber; +import com.android.dialer.database.FilteredNumberContract.FilteredNumberColumns; + +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +public class BlockedNumbersMigratorTest extends AndroidTestCase { + + private static final String NUMBER = "6502530000"; + private static final String NUMBER1 = "6502530001"; + private static final String NUMBER2 = "6502530002"; + + @Mock private BlockedNumbersMigrator.Listener mListener; + private final MockContentResolver mContentResolver = new MockContentResolver(); + private final MockContentProvider mContentProvider = new MockContentProvider(); + private BlockedNumbersMigrator mMigrator; + + @Override + public void setUp() throws Exception { + super.setUp(); + MockitoAnnotations.initMocks(this); + FilteredNumberCompat.setIsEnabledForTest(true); + mContentResolver.addProvider(FilteredNumberContract.AUTHORITY, mContentProvider); + mContentResolver.addProvider(BlockedNumberContract.AUTHORITY, mContentProvider); + mMigrator = new BlockedNumbersMigrator(mContentResolver); + } + + public void testConstructor_NullContentResolver() { + try { + new BlockedNumbersMigrator(null); + fail(); + } catch (NullPointerException e) {} + } + + public void testMigrate_M() { + if (CompatUtils.isNCompatible()) { + return; + } + assertFalse(mMigrator.migrate(mListener)); + } + + public void testMigrate_N_Disabled() { + if (!CompatUtils.isNCompatible()) { + return; + } + FilteredNumberCompat.setIsEnabledForTest(false); + assertFalse(mMigrator.migrate(mListener)); + } + + public void testMigrate_N_NullListener() { + if (!CompatUtils.isNCompatible()) { + return; + } + try { + mMigrator.migrate(null); + fail(); + } catch (NullPointerException e) {} + } + + public void testMigrate_N() throws InterruptedException { + if (!CompatUtils.isNCompatible()) { + return; + } + mContentProvider.expectQuery(FilteredNumber.CONTENT_URI) + .withProjection(FilteredNumberColumns.NUMBER).returnRow(NUMBER).returnRow(NUMBER1) + .returnRow(NUMBER2); + + setUpNewBlockedNumberExpectations(mContentProvider, NUMBER, 0); + setUpNewBlockedNumberExpectations(mContentProvider, NUMBER1, 1); + setUpNewBlockedNumberExpectations(mContentProvider, NUMBER2, 2); + + MigrationListener listener = new MigrationListener(); + assertTrue(mMigrator.migrate(listener)); + listener.waitForCallback(); + assertTrue(FilteredNumberCompat.hasMigratedToNewBlocking()); + mContentProvider.verify(); + } + + public void testMigrate_N_AlreadyBlocked() throws InterruptedException { + if (!CompatUtils.isNCompatible()) { + return; + } + mContentProvider.expectQuery(FilteredNumber.CONTENT_URI) + .withProjection(FilteredNumberColumns.NUMBER).returnRow(NUMBER); + mContentProvider.expectQuery(BlockedNumbers.CONTENT_URI) + .withProjection(BlockedNumbers.COLUMN_ID) + .withSelection(BlockedNumbers.COLUMN_ORIGINAL_NUMBER + " = ?", NUMBER).returnRow(0); + // No expectation for insert into BlockedNumbers.CONTENT_URI because it's already there + + MigrationListener listener = new MigrationListener(); + assertTrue(mMigrator.migrate(listener)); + listener.waitForCallback(); + assertTrue(FilteredNumberCompat.hasMigratedToNewBlocking()); + mContentProvider.verify(); + } + + private void setUpNewBlockedNumberExpectations(MockContentProvider contentProvider, + String number, int returnId) { + contentProvider.expectQuery(BlockedNumbers.CONTENT_URI) + .withProjection(BlockedNumbers.COLUMN_ID) + .withSelection(BlockedNumbers.COLUMN_ORIGINAL_NUMBER + " = ?", number).returnEmptyCursor(); + contentProvider.expectInsert(BlockedNumbers.CONTENT_URI, + createBlockedNumberInsertValues(number), + ContentUris.withAppendedId(BlockedNumbers.CONTENT_URI, returnId)); + } + + private ContentValues createBlockedNumberInsertValues(String number) { + ContentValues values = new ContentValues(); + values.put(BlockedNumbers.COLUMN_ORIGINAL_NUMBER, number); + return values; + } + + private static class MigrationListener implements BlockedNumbersMigrator.Listener { + + private final CountDownLatch mOnCompleteCalled = new CountDownLatch(1); + + @Override + public void onComplete() { + mOnCompleteCalled.countDown(); + } + + public void waitForCallback() throws InterruptedException { + if (!mOnCompleteCalled.await(5000, TimeUnit.MILLISECONDS)) { + throw new IllegalStateException("Waiting on callback timed out."); + } + } + } +} diff --git a/tests/src/com/android/dialer/filterednumber/MigrateBlockedNumbersDialogFragmentInstrumentationTest.java b/tests/src/com/android/dialer/filterednumber/MigrateBlockedNumbersDialogFragmentInstrumentationTest.java new file mode 100644 index 000000000..16b6f3fac --- /dev/null +++ b/tests/src/com/android/dialer/filterednumber/MigrateBlockedNumbersDialogFragmentInstrumentationTest.java @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.dialer.filterednumber; + +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.app.AlertDialog; +import android.app.DialogFragment; +import android.content.DialogInterface; +import android.test.ActivityInstrumentationTestCase2; + +import com.android.dialer.DialtactsActivity; +import com.android.dialer.filterednumber.BlockedNumbersMigrator.Listener; + +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +/** + * Instrumentation tests for {@link MigrateBlockedNumbersDialogFragment}. Note for these tests to + * work properly, the device's screen must be on. + */ +public class MigrateBlockedNumbersDialogFragmentInstrumentationTest extends + ActivityInstrumentationTestCase2<DialtactsActivity> { + + private static final String SHOW_TAG = "ShowTag"; + + @Mock private BlockedNumbersMigrator mBlockedNumbersMigrator; + @Mock private Listener mListener; + private DialtactsActivity mActivity; + private DialogFragment mMigrateDialogFragment; + + public MigrateBlockedNumbersDialogFragmentInstrumentationTest() { + super(DialtactsActivity.class); + } + + @Override + public void setUp() throws Exception { + super.setUp(); + MockitoAnnotations.initMocks(this); + mActivity = getActivity(); + mMigrateDialogFragment = MigrateBlockedNumbersDialogFragment + .newInstance(mBlockedNumbersMigrator, mListener); + getInstrumentation().runOnMainSync(new Runnable() { + @Override + public void run() { + mMigrateDialogFragment.show(mActivity.getFragmentManager(), SHOW_TAG); + } + }); + getInstrumentation().waitForIdleSync(); + } + + public void testDialogAppears() { + assertTrue(mMigrateDialogFragment.getDialog().isShowing()); + } + + public void testDialogPositiveButtonPress() { + when(mBlockedNumbersMigrator.migrate(mListener)).thenReturn(true); + getInstrumentation().runOnMainSync(new Runnable() { + @Override + public void run() { + ((AlertDialog) mMigrateDialogFragment.getDialog()) + .getButton(DialogInterface.BUTTON_POSITIVE).performClick(); + } + }); + getInstrumentation().waitForIdleSync(); + // Dialog was dismissed + assertNull(mMigrateDialogFragment.getDialog()); + verify(mBlockedNumbersMigrator).migrate(mListener); + } +} diff --git a/tests/src/com/android/dialer/filterednumber/MigrateBlockedNumbersDialogFragmentTest.java b/tests/src/com/android/dialer/filterednumber/MigrateBlockedNumbersDialogFragmentTest.java new file mode 100644 index 000000000..1b419cee8 --- /dev/null +++ b/tests/src/com/android/dialer/filterednumber/MigrateBlockedNumbersDialogFragmentTest.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.dialer.filterednumber; + +import android.test.AndroidTestCase; +import android.test.suitebuilder.annotation.SmallTest; + +import com.android.dialer.filterednumber.BlockedNumbersMigrator.Listener; + +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + + +/** + * Unit tests for {@link MigrateBlockedNumbersDialogFragment} + */ +@SmallTest +public class MigrateBlockedNumbersDialogFragmentTest extends AndroidTestCase { + + @Mock private BlockedNumbersMigrator mBlockedNumbersMigrator; + @Mock private Listener mListener; + + @Override + public void setUp() throws Exception { + super.setUp(); + MockitoAnnotations.initMocks(this); + } + + public void testNewInstance_NullMigrator() { + try { + MigrateBlockedNumbersDialogFragment.newInstance(null, mListener); + fail(); + } catch (NullPointerException e) {} + } + + public void testNewInstance_NullListener() { + try { + MigrateBlockedNumbersDialogFragment.newInstance(mBlockedNumbersMigrator, null); + fail(); + } catch (NullPointerException e) {} + } + + public void testNewInstance_WithListener() { + assertNotNull(MigrateBlockedNumbersDialogFragment.newInstance(mBlockedNumbersMigrator, + mListener)); + } +} diff --git a/tests/src/com/android/dialer/voicemail/VoicemailActivityInstrumentationTestCase2.java b/tests/src/com/android/dialer/voicemail/VoicemailActivityInstrumentationTestCase2.java index a992e8bd0..cabaf6732 100644 --- a/tests/src/com/android/dialer/voicemail/VoicemailActivityInstrumentationTestCase2.java +++ b/tests/src/com/android/dialer/voicemail/VoicemailActivityInstrumentationTestCase2.java @@ -1,3 +1,19 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.android.dialer.voicemail; import android.app.Activity; @@ -23,7 +39,6 @@ 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}. diff --git a/tests/src/com/android/dialer/voicemail/VoicemailArchiveTest.java b/tests/src/com/android/dialer/voicemail/VoicemailArchiveTest.java index 8cbd344a2..bbd5edc48 100644 --- a/tests/src/com/android/dialer/voicemail/VoicemailArchiveTest.java +++ b/tests/src/com/android/dialer/voicemail/VoicemailArchiveTest.java @@ -15,15 +15,13 @@ */ package com.android.dialer.voicemail; + import android.content.ContentUris; import android.content.ContentValues; +import android.content.res.AssetManager; -import com.android.dialer.R; import com.android.dialer.database.VoicemailArchiveContract.VoicemailArchive; -import android.content.res.AssetManager; -import android.test.suitebuilder.annotation.Suppress; - import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; diff --git a/tests/src/com/android/dialer/voicemail/VoicemailAsyncTaskUtilTest.java b/tests/src/com/android/dialer/voicemail/VoicemailAsyncTaskUtilTest.java new file mode 100644 index 000000000..d98d9fa59 --- /dev/null +++ b/tests/src/com/android/dialer/voicemail/VoicemailAsyncTaskUtilTest.java @@ -0,0 +1,388 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.dialer.voicemail; + +import android.content.ContentUris; +import android.content.ContentValues; +import android.content.res.AssetFileDescriptor; +import android.net.Uri; +import android.os.Bundle; +import android.os.ParcelFileDescriptor; +import android.provider.CallLog; +import android.provider.VoicemailContract; +import android.test.InstrumentationTestCase; +import android.test.mock.MockContentResolver; + +import com.android.contacts.common.test.mocks.MockContentProvider; +import com.android.contacts.common.test.mocks.MockContentProvider.Query; +import com.android.dialer.calllog.CallLogQuery; +import com.android.dialer.database.VoicemailArchiveContract; +import com.android.dialer.util.AsyncTaskExecutors; +import com.android.dialer.util.FakeAsyncTaskExecutor; +import com.android.dialer.voicemail.VoicemailAsyncTaskUtil.OnGetArchivedVoicemailFilePathListener; +import com.android.dialer.voicemail.VoicemailAsyncTaskUtil.OnArchiveVoicemailListener; +import com.android.dialer.voicemail.VoicemailAsyncTaskUtil.OnSetVoicemailArchiveStatusListener; + +import org.junit.Rule; +import org.junit.rules.TemporaryFolder; +import static org.mockito.Mockito.*; + +import java.io.FileNotFoundException; +import java.io.IOException; + +public class VoicemailAsyncTaskUtilTest extends InstrumentationTestCase { + private static final String TEST_MIME_TYPE = "audio/mp3"; + private static final String TEST_NUMBER = "+1412555555"; + private static final String TEST_FILE_PATH = "TEST_PATH"; + private static final String TEST_TRANSCRIPTION = "TEST_TRANSCRIPTION"; + private static final long TEST_DATE = 0; + private static final long TEST_DURATION = 0; + private static final int TEST_SERVER_ID = 1; + private static final int TEST_ID = 2; + private static final Uri ARCHIVED_VOICEMAIL_URI = + VoicemailArchiveContract.VoicemailArchive.buildWithId(TEST_ID); + private static final Uri VOICEMAIL_URI = VoicemailContract.Voicemails.CONTENT_URI + .buildUpon().appendPath(Integer.toString(TEST_SERVER_ID)).build(); + private static final String[] CALLLOG_QUERY_PROJECTION = new String[] { + CallLog.Calls._ID, + CallLog.Calls.NUMBER, + CallLog.Calls.DATE, + CallLog.Calls.DURATION, + CallLog.Calls.TYPE, + CallLog.Calls.COUNTRY_ISO, + CallLog.Calls.VOICEMAIL_URI, + CallLog.Calls.GEOCODED_LOCATION, + CallLog.Calls.CACHED_NAME, + CallLog.Calls.CACHED_NUMBER_TYPE, + CallLog.Calls.CACHED_NUMBER_LABEL, + CallLog.Calls.CACHED_LOOKUP_URI, + CallLog.Calls.CACHED_MATCHED_NUMBER, + CallLog.Calls.CACHED_NORMALIZED_NUMBER, + CallLog.Calls.CACHED_PHOTO_ID, + CallLog.Calls.CACHED_FORMATTED_NUMBER, + CallLog.Calls.IS_READ, + CallLog.Calls.NUMBER_PRESENTATION, + CallLog.Calls.PHONE_ACCOUNT_COMPONENT_NAME, + CallLog.Calls.PHONE_ACCOUNT_ID, + CallLog.Calls.FEATURES, + CallLog.Calls.DATA_USAGE, + CallLog.Calls.TRANSCRIPTION, + CallLog.Calls.CACHED_PHOTO_URI + }; + private static final String[] VOICEMAIL_PROJECTION = new String[] { + VoicemailContract.Voicemails._ID, + VoicemailContract.Voicemails.NUMBER, + VoicemailContract.Voicemails.DATE, + VoicemailContract.Voicemails.DURATION, + VoicemailContract.Voicemails.MIME_TYPE, + VoicemailContract.Voicemails.TRANSCRIPTION, + }; + + private final MockContentResolver mContentResolver = new MockContentResolver(); + private final MockContentProvider mArchiveContentProvider = new TestVoicemailContentProvider(); + private final MockContentProvider mVoicemailContentProvider = + new TestVoicemailContentProvider(); + private final MockContentProvider mCallLogContentProvider = new MockContentProvider(); + @Rule + private final TemporaryFolder mTemporaryFolder = new TemporaryFolder(); + + private VoicemailAsyncTaskUtil mVoicemailAsyncTaskUtil; + private FakeAsyncTaskExecutor mFakeAsyncTaskExecutor; + + @Override + protected void setUp() throws Exception { + super.setUp(); + mFakeAsyncTaskExecutor = new FakeAsyncTaskExecutor(getInstrumentation()); + AsyncTaskExecutors.setFactoryForTest(mFakeAsyncTaskExecutor.getFactory()); + mContentResolver.addProvider(VoicemailArchiveContract.AUTHORITY, mArchiveContentProvider); + mContentResolver.addProvider(VoicemailContract.AUTHORITY, mVoicemailContentProvider); + mContentResolver.addProvider(CallLog.AUTHORITY, mCallLogContentProvider); + mVoicemailAsyncTaskUtil = new VoicemailAsyncTaskUtil(mContentResolver); + } + + @Override + protected void tearDown() throws Exception { + AsyncTaskExecutors.setFactoryForTest(null); + super.tearDown(); + } + + public void testGetVoicemailFilePath_VoicemailExists() throws Throwable { + newVoicemailArchiveQuery().withAnyProjection() + .returnRow(getCombinedContentValuesWithData()); + final OnGetArchivedVoicemailFilePathListener listener = + mock(OnGetArchivedVoicemailFilePathListener.class); + runTestOnUiThread(new Runnable() { + @Override + public void run() { + mVoicemailAsyncTaskUtil.getVoicemailFilePath(listener, ARCHIVED_VOICEMAIL_URI); + } + }); + mFakeAsyncTaskExecutor.runTask(VoicemailAsyncTaskUtil.Tasks.GET_VOICEMAIL_FILE_PATH); + verify(listener).onGetArchivedVoicemailFilePath(TEST_FILE_PATH); + verifyQueries(); + } + + public void testGetVoicemailFilePath_VoicemailNotExists() throws Throwable { + newVoicemailArchiveFakeQuery().withAnyProjection().returnEmptyCursor(); + final OnGetArchivedVoicemailFilePathListener listener = + mock(OnGetArchivedVoicemailFilePathListener.class); + runTestOnUiThread(new Runnable() { + @Override + public void run() { + mVoicemailAsyncTaskUtil.getVoicemailFilePath(listener, getFakeVoicemailUri()); + } + }); + mFakeAsyncTaskExecutor.runTask(VoicemailAsyncTaskUtil.Tasks.GET_VOICEMAIL_FILE_PATH); + verify(listener).onGetArchivedVoicemailFilePath(null); + verifyQueries(); + } + + public void testSetVoicemailArchiveStatus_VoicemailNotExists() throws Throwable { + newVoicemailArchiveNotExistsUpdateQuery(true); + final OnSetVoicemailArchiveStatusListener listener = + mock(OnSetVoicemailArchiveStatusListener.class); + runTestOnUiThread(new Runnable() { + @Override + public void run() { + mVoicemailAsyncTaskUtil.setVoicemailArchiveStatus(listener, + getFakeVoicemailUri(), true); + } + }); + mFakeAsyncTaskExecutor.runTask(VoicemailAsyncTaskUtil.Tasks.SET_VOICEMAIL_ARCHIVE_STATUS); + verify(listener).onSetVoicemailArchiveStatus(false); + verifyQueries(); + } + + public void testSetVoicemailArchiveStatus_VoicemailExists() throws Throwable { + newVoicemailArchiveExistsUpdateQuery(true); + final OnSetVoicemailArchiveStatusListener listener = + mock(OnSetVoicemailArchiveStatusListener.class); + runTestOnUiThread(new Runnable() { + @Override + public void run() { + mVoicemailAsyncTaskUtil.setVoicemailArchiveStatus(listener, + ARCHIVED_VOICEMAIL_URI, true); + } + }); + mFakeAsyncTaskExecutor.runTask(VoicemailAsyncTaskUtil.Tasks.SET_VOICEMAIL_ARCHIVE_STATUS); + verify(listener).onSetVoicemailArchiveStatus(true); + verifyQueries(); + } + + public void testArchiveVoicemailContent_ArchiveNotExists() throws Throwable { + newVoicemailArchiveExistsQuery().returnEmptyCursor(); + newQueryCallLogInfo().returnRow(getCallLogContentValues()); + newVoicemailQuery().returnRow(getVoicemailContentValues()); + newVoicemailArchiveInsert(); + final OnArchiveVoicemailListener listener = mock(OnArchiveVoicemailListener.class); + runTestOnUiThread(new Runnable() { + @Override + public void run() { + mVoicemailAsyncTaskUtil.archiveVoicemailContent(listener, VOICEMAIL_URI); + } + }); + mFakeAsyncTaskExecutor.runTask(VoicemailAsyncTaskUtil.Tasks.ARCHIVE_VOICEMAIL_CONTENT); + verify(listener).onArchiveVoicemail(ARCHIVED_VOICEMAIL_URI); + verifyQueries(); + } + + public void testArchiveVoicemailContent_ArchiveExists() throws Throwable { + newVoicemailArchiveExistsQuery().returnRow(getCombinedValuesWithId()); + final OnArchiveVoicemailListener listener = mock(OnArchiveVoicemailListener.class); + runTestOnUiThread(new Runnable() { + @Override + public void run() { + mVoicemailAsyncTaskUtil.archiveVoicemailContent(listener, VOICEMAIL_URI); + } + }); + mFakeAsyncTaskExecutor.runTask(VoicemailAsyncTaskUtil.Tasks.ARCHIVE_VOICEMAIL_CONTENT); + verify(listener).onArchiveVoicemail(ARCHIVED_VOICEMAIL_URI); + verifyQueries(); + } + + public void testArchiveVoicemailContent_CallLogInfoNotExists() throws Throwable { + newVoicemailArchiveExistsQuery().returnEmptyCursor(); + newQueryCallLogInfo().returnEmptyCursor(); + newVoicemailQuery().returnEmptyCursor(); + final OnArchiveVoicemailListener listener = mock(OnArchiveVoicemailListener.class); + runTestOnUiThread(new Runnable() { + @Override + public void run() { + mVoicemailAsyncTaskUtil.archiveVoicemailContent(listener, VOICEMAIL_URI); + } + }); + mFakeAsyncTaskExecutor.runTask(VoicemailAsyncTaskUtil.Tasks.ARCHIVE_VOICEMAIL_CONTENT); + verify(listener).onArchiveVoicemail(null); + verifyQueries(); + } + + private Query newVoicemailArchiveQuery() { + return mArchiveContentProvider.expectQuery(ARCHIVED_VOICEMAIL_URI); + } + + private Query newVoicemailArchiveFakeQuery() { + return mArchiveContentProvider.expectQuery(getFakeVoicemailUri()); + } + + private Query newQueryCallLogInfo() { + return mCallLogContentProvider.expectQuery(ContentUris.withAppendedId( + CallLog.Calls.CONTENT_URI_WITH_VOICEMAIL, ContentUris.parseId(VOICEMAIL_URI))) + .withProjection(CALLLOG_QUERY_PROJECTION); + } + + private Query newVoicemailQuery() { + return mVoicemailContentProvider.expectQuery(VOICEMAIL_URI).withAnySelection() + .withProjection(VOICEMAIL_PROJECTION); + } + + private Query newVoicemailArchiveExistsQuery() { + return mArchiveContentProvider.expectQuery( + VoicemailArchiveContract.VoicemailArchive.CONTENT_URI) + .withSelection(VoicemailArchiveContract.VoicemailArchive.SERVER_ID + "=" + + ContentUris.parseId(VOICEMAIL_URI), (String[]) null) + .withProjection(VoicemailArchiveContract.VoicemailArchive._ID); + } + + private void newVoicemailArchiveInsert() { + mArchiveContentProvider.expectInsert( + VoicemailArchiveContract.VoicemailArchive.CONTENT_URI, getCombinedContentValues(), + ARCHIVED_VOICEMAIL_URI); + } + + private void newVoicemailArchiveNotExistsUpdateQuery(boolean status) { + mArchiveContentProvider.expectUpdate(getFakeVoicemailUri(), + getArchiveStatusUpdateValues(status), null, null).returnRowsAffected(0); + } + + private void newVoicemailArchiveExistsUpdateQuery(boolean status) { + mArchiveContentProvider.expectUpdate(ARCHIVED_VOICEMAIL_URI, + getArchiveStatusUpdateValues(status), null, null).returnRowsAffected(1); + } + + private static ContentValues getCallLogContentValues() { + ContentValues values = new ContentValues(); + values.put(CALLLOG_QUERY_PROJECTION[CallLogQuery.GEOCODED_LOCATION], ""); + values.put(CALLLOG_QUERY_PROJECTION[CallLogQuery.CACHED_NAME], ""); + values.put(CALLLOG_QUERY_PROJECTION[CallLogQuery.COUNTRY_ISO], ""); + values.put(CALLLOG_QUERY_PROJECTION[CallLogQuery.CACHED_NUMBER_TYPE], 0); + values.put(CALLLOG_QUERY_PROJECTION[CallLogQuery.CACHED_NUMBER_LABEL], ""); + values.put(CALLLOG_QUERY_PROJECTION[CallLogQuery.CACHED_LOOKUP_URI], ""); + values.put(CALLLOG_QUERY_PROJECTION[CallLogQuery.CACHED_MATCHED_NUMBER], ""); + values.put(CALLLOG_QUERY_PROJECTION[CallLogQuery.CACHED_NORMALIZED_NUMBER], ""); + values.put(CALLLOG_QUERY_PROJECTION[CallLogQuery.CACHED_FORMATTED_NUMBER], ""); + values.put(CALLLOG_QUERY_PROJECTION[CallLogQuery.NUMBER_PRESENTATION], 0); + values.put(CALLLOG_QUERY_PROJECTION[CallLogQuery.ACCOUNT_COMPONENT_NAME], ""); + values.put(CALLLOG_QUERY_PROJECTION[CallLogQuery.ACCOUNT_ID], ""); + values.put(CALLLOG_QUERY_PROJECTION[CallLogQuery.FEATURES], 0); + values.put(CALLLOG_QUERY_PROJECTION[23], ""); + return values; + } + + private static ContentValues getVoicemailContentValues() { + ContentValues values = new ContentValues(); + values.put(VoicemailContract.Voicemails.NUMBER, TEST_NUMBER); + values.put(VoicemailContract.Voicemails.DATE, TEST_DATE); + values.put(VoicemailContract.Voicemails.DURATION, TEST_DURATION); + values.put(VoicemailContract.Voicemails.MIME_TYPE, TEST_MIME_TYPE); + values.put(VoicemailContract.Voicemails._ID, TEST_SERVER_ID); + values.put(VoicemailContract.Voicemails.TRANSCRIPTION, TEST_TRANSCRIPTION); + values.put("_data", TEST_FILE_PATH); // VoicemailContract.Voicemails._DATA + return values; + } + + private static ContentValues getCombinedContentValues() { + ContentValues values = new ContentValues(); + // Call log info + values.put(VoicemailArchiveContract.VoicemailArchive.GEOCODED_LOCATION, ""); + values.put(VoicemailArchiveContract.VoicemailArchive.CACHED_NAME, ""); + values.put(VoicemailArchiveContract.VoicemailArchive.CACHED_NUMBER_TYPE, 0); + values.put(VoicemailArchiveContract.VoicemailArchive.CACHED_NUMBER_LABEL, ""); + values.put(VoicemailArchiveContract.VoicemailArchive.CACHED_LOOKUP_URI, ""); + values.put(VoicemailArchiveContract.VoicemailArchive.CACHED_MATCHED_NUMBER, ""); + values.put(VoicemailArchiveContract.VoicemailArchive.CACHED_NORMALIZED_NUMBER, ""); + values.put(VoicemailArchiveContract.VoicemailArchive.CACHED_FORMATTED_NUMBER, ""); + values.put(VoicemailArchiveContract.VoicemailArchive.NUMBER_PRESENTATION, 0); + values.put(VoicemailArchiveContract.VoicemailArchive.ACCOUNT_COMPONENT_NAME, ""); + values.put(VoicemailArchiveContract.VoicemailArchive.ACCOUNT_ID, ""); + values.put(VoicemailArchiveContract.VoicemailArchive.FEATURES, 0); + values.put(VoicemailArchiveContract.VoicemailArchive.CACHED_PHOTO_URI, ""); + values.put(VoicemailArchiveContract.VoicemailArchive.COUNTRY_ISO, ""); + + // Voicemail content info + values.put(VoicemailArchiveContract.VoicemailArchive.NUMBER, TEST_NUMBER); + values.put(VoicemailArchiveContract.VoicemailArchive.DATE, TEST_DATE); + values.put(VoicemailArchiveContract.VoicemailArchive.DURATION, TEST_DURATION); + values.put(VoicemailArchiveContract.VoicemailArchive.MIME_TYPE, TEST_MIME_TYPE); + values.put(VoicemailArchiveContract.VoicemailArchive.SERVER_ID, TEST_SERVER_ID); + values.put(VoicemailArchiveContract.VoicemailArchive.TRANSCRIPTION, TEST_TRANSCRIPTION); + + // Custom fields + values.put(VoicemailArchiveContract.VoicemailArchive.ARCHIVED, false); + return values; + } + + private static ContentValues getCombinedContentValuesWithData() { + ContentValues values = getCombinedContentValues(); + values.put(VoicemailArchiveContract.VoicemailArchive._DATA, TEST_FILE_PATH); + return values; + } + + private static ContentValues getCombinedValuesWithId() { + ContentValues values = getCombinedContentValuesWithData(); + values.put(VoicemailArchiveContract.VoicemailArchive._ID, TEST_ID); + return values; + } + + private static ContentValues getArchiveStatusUpdateValues(boolean status) { + ContentValues values = new ContentValues(); + values.put(VoicemailArchiveContract.VoicemailArchive.ARCHIVED, status); + return values; + } + + private static Uri getFakeVoicemailUri() { + return VoicemailArchiveContract.VoicemailArchive.buildWithId(0); + } + + private void verifyQueries() { + mArchiveContentProvider.verify(); + mCallLogContentProvider.verify(); + mVoicemailContentProvider.verify(); + } + + private class TestVoicemailContentProvider extends MockContentProvider { + @Override + public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { + int modeBits = ParcelFileDescriptor.parseMode(mode); + try { + return ParcelFileDescriptor.open(mTemporaryFolder.newFile(), modeBits); + } catch (IOException e) { + return null; + } + } + + @Override + public AssetFileDescriptor openTypedAssetFile(Uri uri, String mimeTypeFilter, Bundle opts) { + try { + return new AssetFileDescriptor(openFile(uri, "r"), 0, + AssetFileDescriptor.UNKNOWN_LENGTH); + } catch (FileNotFoundException e) { + return null; + } + } + } +} + diff --git a/tests/src/com/android/dialer/voicemail/VoicemailPlaybackTest.java b/tests/src/com/android/dialer/voicemail/VoicemailPlaybackTest.java index abd582bec..be9905edd 100644 --- a/tests/src/com/android/dialer/voicemail/VoicemailPlaybackTest.java +++ b/tests/src/com/android/dialer/voicemail/VoicemailPlaybackTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 The Android Open Source Project + * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,10 +16,14 @@ package com.android.dialer.voicemail; +import android.content.ContentUris; +import android.database.Cursor; +import android.net.Uri; import android.test.suitebuilder.annotation.Suppress; import com.android.dialer.calllog.CallLogActivity; - +import com.android.dialer.database.VoicemailArchiveContract; +import static com.android.dialer.voicemail.VoicemailAsyncTaskUtil.Tasks.ARCHIVE_VOICEMAIL_CONTENT; import static com.android.dialer.voicemail.VoicemailPlaybackPresenter.Tasks.CHECK_FOR_CONTENT; /** @@ -38,6 +42,12 @@ public class VoicemailPlaybackTest mPresenter = VoicemailPlaybackPresenter.getInstance(getActivity(), null); } + @Override + public void tearDown() throws Exception { + cleanUpArchivedVoicemailUri(); + super.tearDown(); + } + @Suppress public void testWhenCheckForContentCompletes() throws Throwable { setUriForRealFileVoicemailEntry(); @@ -54,4 +64,82 @@ public class VoicemailPlaybackTest assertStateTextContains("Loading voicemail"); } + + public void testArchiveContent() throws Throwable { + setUriForRealFileVoicemailEntry(); + setPlaybackViewForPresenter(); + mFakeAsyncTaskExecutor.runTask(CHECK_FOR_CONTENT); + + getInstrumentation().runOnMainSync(new Runnable() { + @Override + public void run() { + mPresenter.archiveContent(mVoicemailUri, true); + } + }); + mFakeAsyncTaskExecutor.runTask(CHECK_FOR_CONTENT); + mFakeAsyncTaskExecutor.runTask(ARCHIVE_VOICEMAIL_CONTENT); + getInstrumentation().waitForIdleSync(); + assertVoicemailArchived(); + } + + public void testShareContent() throws Throwable { + setUriForRealFileVoicemailEntry(); + setPlaybackViewForPresenter(); + mFakeAsyncTaskExecutor.runTask(CHECK_FOR_CONTENT); + + getInstrumentation().runOnMainSync(new Runnable() { + @Override + public void run() { + mPresenter.archiveContent(mVoicemailUri, false); + } + }); + mFakeAsyncTaskExecutor.runTask(CHECK_FOR_CONTENT); + mFakeAsyncTaskExecutor.runTask(ARCHIVE_VOICEMAIL_CONTENT); + getInstrumentation().waitForIdleSync(); + assertVoicemailArchived(); + } + + private void assertVoicemailArchived() { + try (Cursor cursor = getArchivedVoicemailCursor()) { + assertTrue(hasContent(cursor)); + assertEquals(ContentUris.parseId(mVoicemailUri), getRowServerId(cursor)); + } catch (Exception e) { + fail("Voicemail was not archived: " + e.toString()); + } + } + + private void cleanUpArchivedVoicemailUri() { + try (Cursor cursor = getArchivedVoicemailCursor()) { + if (hasContent(cursor)) { + getContentResolver().delete(getRowUri(cursor), null, null); + } + } + } + + private Cursor getArchivedVoicemailCursor() { + return getContentResolver().query( + VoicemailArchiveContract.VoicemailArchive.CONTENT_URI, + new String[] { + VoicemailArchiveContract.VoicemailArchive._ID, + VoicemailArchiveContract.VoicemailArchive.SERVER_ID + }, + VoicemailArchiveContract.VoicemailArchive.SERVER_ID + "=" + + ContentUris.parseId(mVoicemailUri), + null, + null); + } + + private int getRowServerId(Cursor cursor) { + return cursor + .getInt(cursor.getColumnIndex(VoicemailArchiveContract.VoicemailArchive.SERVER_ID)); + } + + private Uri getRowUri(Cursor cursor) { + return VoicemailArchiveContract.VoicemailArchive.buildWithId(cursor.getInt( + cursor.getColumnIndex(VoicemailArchiveContract.VoicemailArchive._ID))); + } + + private boolean hasContent(Cursor cursor) { + return cursor != null && cursor.moveToFirst(); + } }
\ No newline at end of file |