diff options
| author | 2016-08-19 16:54:51 +0000 | |
|---|---|---|
| committer | 2016-08-19 16:54:53 +0000 | |
| commit | 28d4e5146665e5f68c29f2d6e523a6f9d86940b7 (patch) | |
| tree | e367aaf57d6b4edd4ca244c850928ecf87edc4dd | |
| parent | c8c44e73f2418d8fe5d48fa5f9c3aaae6a4939c1 (diff) | |
| parent | a725f622b60b075e15c2d758185463f1a0cfbfbc (diff) | |
Merge "Refactored MediaInserterTest to use Mockito instead of EasyMock."
| -rw-r--r-- | media/tests/MediaFrameworkTest/Android.mk | 2 | ||||
| -rw-r--r-- | media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/MediaInserterTest.java | 103 |
2 files changed, 46 insertions, 59 deletions
diff --git a/media/tests/MediaFrameworkTest/Android.mk b/media/tests/MediaFrameworkTest/Android.mk index 7e438a12f31b..5d737308c8cf 100644 --- a/media/tests/MediaFrameworkTest/Android.mk +++ b/media/tests/MediaFrameworkTest/Android.mk @@ -9,7 +9,7 @@ LOCAL_JAVA_LIBRARIES := android.test.runner LOCAL_JAVA_LANGUAGE_VERSION := 1.8 -LOCAL_STATIC_JAVA_LIBRARIES := easymocklib \ +LOCAL_STATIC_JAVA_LIBRARIES := \ mockito-target \ android-support-test \ android-ex-camera2 diff --git a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/MediaInserterTest.java b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/MediaInserterTest.java index 06efa906af04..86c2284e86ff 100644 --- a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/MediaInserterTest.java +++ b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/MediaInserterTest.java @@ -16,6 +16,14 @@ package com.android.mediaframeworktest.unit; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.mockito.Matchers.argThat; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; + import android.content.ContentProviderClient; import android.content.ContentValues; import android.content.IContentProvider; @@ -28,14 +36,16 @@ import android.provider.MediaStore.Video; import android.test.InstrumentationTestCase; import android.test.suitebuilder.annotation.SmallTest; -import org.easymock.EasyMock; -import org.easymock.IArgumentMatcher; +import org.hamcrest.Description; +import org.mockito.ArgumentMatcher; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; public class MediaInserterTest extends InstrumentationTestCase { private MediaInserter mMediaInserter; private static final int TEST_BUFFER_SIZE = 10; - private IContentProvider mMockProvider; + private @Mock IContentProvider mMockProvider; private String mPackageName; private int mFilesCounter; @@ -49,8 +59,8 @@ public class MediaInserterTest extends InstrumentationTestCase { private static final Uri sImagesUri = Images.Media.getContentUri(sVolumeName); private static final Uri sFilesUri = Files.getContentUri(sVolumeName); - private static class MediaUriMatcher implements IArgumentMatcher { - private Uri mUri; + private static class MediaUriMatcher extends ArgumentMatcher<Uri> { + private final Uri mUri; private MediaUriMatcher(Uri uri) { mUri = uri; @@ -63,25 +73,30 @@ public class MediaInserterTest extends InstrumentationTestCase { } Uri actualUri = (Uri) argument; - if (actualUri == mUri) return true; + if (actualUri == mUri) + return true; return false; } @Override - public void appendTo(StringBuffer buffer) { - buffer.append("expected a TableUri '").append(mUri).append("'"); + public void describeTo(Description description) { + description + .appendText("expected a TableUri '") + .appendText(mUri.toString()) + .appendText("'"); } - private static Uri expectMediaUri(Uri in) { - EasyMock.reportMatcher(new MediaUriMatcher(in)); - return null; - } + } + + private static Uri eqUri(Uri in) { + return argThat(new MediaUriMatcher(in)); } @Override protected void setUp() throws Exception { super.setUp(); - mMockProvider = EasyMock.createMock(IContentProvider.class); + MockitoAnnotations.initMocks(this); + final ContentProviderClient client = new ContentProviderClient( getInstrumentation().getContext().getContentResolver(), mMockProvider, true); mMediaInserter = new MediaInserter(client, TEST_BUFFER_SIZE); @@ -134,61 +149,46 @@ public class MediaInserterTest extends InstrumentationTestCase { @SmallTest public void testInsertContentsLessThanBufferSize() throws Exception { - EasyMock.replay(mMockProvider); - fillBuffer(sFilesUri, TEST_BUFFER_SIZE - 4); fillBuffer(sAudioUri, TEST_BUFFER_SIZE - 3); fillBuffer(sVideoUri, TEST_BUFFER_SIZE - 2); fillBuffer(sImagesUri, TEST_BUFFER_SIZE - 1); - EasyMock.verify(mMockProvider); + verify(mMockProvider, never()).bulkInsert(eq(mPackageName), any(), any()); } @SmallTest public void testInsertContentsEqualToBufferSize() throws Exception { - EasyMock.expect(mMockProvider.bulkInsert(mPackageName, - (Uri) EasyMock.anyObject(), (ContentValues[]) EasyMock.anyObject())).andReturn(1); - EasyMock.expectLastCall().times(4); - EasyMock.replay(mMockProvider); + when(mMockProvider.bulkInsert(eq(mPackageName), any(), any())).thenReturn(1); fillBuffer(sFilesUri, TEST_BUFFER_SIZE); fillBuffer(sAudioUri, TEST_BUFFER_SIZE); fillBuffer(sVideoUri, TEST_BUFFER_SIZE); fillBuffer(sImagesUri, TEST_BUFFER_SIZE); - EasyMock.verify(mMockProvider); + verify(mMockProvider, times(4)).bulkInsert(eq(mPackageName), any(), any()); } @SmallTest public void testInsertContentsMoreThanBufferSize() throws Exception { - EasyMock.expect(mMockProvider.bulkInsert(mPackageName, - (Uri) EasyMock.anyObject(), (ContentValues[]) EasyMock.anyObject())).andReturn(1); - EasyMock.expectLastCall().times(4); - EasyMock.replay(mMockProvider); + when(mMockProvider.bulkInsert(eq(mPackageName), any(), any())).thenReturn(1); fillBuffer(sFilesUri, TEST_BUFFER_SIZE + 1); fillBuffer(sAudioUri, TEST_BUFFER_SIZE + 2); fillBuffer(sVideoUri, TEST_BUFFER_SIZE + 3); fillBuffer(sImagesUri, TEST_BUFFER_SIZE + 4); - EasyMock.verify(mMockProvider); + verify(mMockProvider, times(4)).bulkInsert(eq(mPackageName), any(), any()); } @SmallTest public void testFlushAllWithEmptyContents() throws Exception { - EasyMock.replay(mMockProvider); - mMediaInserter.flushAll(); - - EasyMock.verify(mMockProvider); } @SmallTest public void testFlushAllWithSomeContents() throws Exception { - EasyMock.expect(mMockProvider.bulkInsert(mPackageName, - (Uri) EasyMock.anyObject(), (ContentValues[]) EasyMock.anyObject())).andReturn(1); - EasyMock.expectLastCall().times(4); - EasyMock.replay(mMockProvider); + when(mMockProvider.bulkInsert(eq(mPackageName), any(), any())).thenReturn(1); fillBuffer(sFilesUri, TEST_BUFFER_SIZE - 4); fillBuffer(sAudioUri, TEST_BUFFER_SIZE - 3); @@ -196,15 +196,12 @@ public class MediaInserterTest extends InstrumentationTestCase { fillBuffer(sImagesUri, TEST_BUFFER_SIZE - 1); mMediaInserter.flushAll(); - EasyMock.verify(mMockProvider); + verify(mMockProvider, times(4)).bulkInsert(eq(mPackageName), any(), any()); } @SmallTest public void testInsertContentsAfterFlushAll() throws Exception { - EasyMock.expect(mMockProvider.bulkInsert(mPackageName, - (Uri) EasyMock.anyObject(), (ContentValues[]) EasyMock.anyObject())).andReturn(1); - EasyMock.expectLastCall().times(8); - EasyMock.replay(mMockProvider); + when(mMockProvider.bulkInsert(eq(mPackageName), any(), any())).thenReturn(1); fillBuffer(sFilesUri, TEST_BUFFER_SIZE - 4); fillBuffer(sAudioUri, TEST_BUFFER_SIZE - 3); @@ -217,28 +214,15 @@ public class MediaInserterTest extends InstrumentationTestCase { fillBuffer(sVideoUri, TEST_BUFFER_SIZE + 3); fillBuffer(sImagesUri, TEST_BUFFER_SIZE + 4); - EasyMock.verify(mMockProvider); + verify(mMockProvider, times(8)).bulkInsert(eq(mPackageName), any(), any()); } @SmallTest public void testInsertContentsWithDifferentSizePerContentType() throws Exception { - EasyMock.expect(mMockProvider.bulkInsert(mPackageName, - MediaUriMatcher.expectMediaUri(sFilesUri), - (ContentValues[]) EasyMock.anyObject())).andReturn(1); - EasyMock.expectLastCall().times(1); - EasyMock.expect(mMockProvider.bulkInsert(mPackageName, - MediaUriMatcher.expectMediaUri(sAudioUri), - (ContentValues[]) EasyMock.anyObject())).andReturn(1); - EasyMock.expectLastCall().times(2); - EasyMock.expect(mMockProvider.bulkInsert(mPackageName, - MediaUriMatcher.expectMediaUri(sVideoUri), - (ContentValues[]) EasyMock.anyObject())).andReturn(1); - EasyMock.expectLastCall().times(3); - EasyMock.expect(mMockProvider.bulkInsert(mPackageName, - MediaUriMatcher.expectMediaUri(sImagesUri), - (ContentValues[]) EasyMock.anyObject())).andReturn(1); - EasyMock.expectLastCall().times(4); - EasyMock.replay(mMockProvider); + when(mMockProvider.bulkInsert(eq(mPackageName), eqUri(sFilesUri), any())).thenReturn(1); + when(mMockProvider.bulkInsert(eq(mPackageName), eqUri(sAudioUri), any())).thenReturn(1); + when(mMockProvider.bulkInsert(eq(mPackageName), eqUri(sVideoUri), any())).thenReturn(1); + when(mMockProvider.bulkInsert(eq(mPackageName), eqUri(sImagesUri), any())).thenReturn(1); for (int i = 0; i < TEST_BUFFER_SIZE; ++i) { fillBuffer(sFilesUri, 1); @@ -247,6 +231,9 @@ public class MediaInserterTest extends InstrumentationTestCase { fillBuffer(sImagesUri, 4); } - EasyMock.verify(mMockProvider); + verify(mMockProvider, times(1)).bulkInsert(eq(mPackageName), eqUri(sFilesUri), any()); + verify(mMockProvider, times(2)).bulkInsert(eq(mPackageName), eqUri(sAudioUri), any()); + verify(mMockProvider, times(3)).bulkInsert(eq(mPackageName), eqUri(sVideoUri), any()); + verify(mMockProvider, times(4)).bulkInsert(eq(mPackageName), eqUri(sImagesUri), any()); } } |