diff options
author | Igor Murashkin <iam@google.com> | 2013-08-29 15:38:17 -0700 |
---|---|---|
committer | Igor Murashkin <iam@google.com> | 2013-09-12 16:57:57 -0700 |
commit | 5e712064dfe48992f8f732208fa4fc13f3455b30 (patch) | |
tree | 5b94b96e4dc152d88cc6b0cdb678f004e4d25cd5 /media/tests | |
parent | e850c973b0662975137cee8a05f8ee2cb82d9b2a (diff) | |
download | frameworks_base-5e712064dfe48992f8f732208fa4fc13f3455b30.zip frameworks_base-5e712064dfe48992f8f732208fa4fc13f3455b30.tar.gz frameworks_base-5e712064dfe48992f8f732208fa4fc13f3455b30.tar.bz2 |
media: Update ImageReader APIs
Bug: 10461757
Change-Id: Ic04e4c41965e3d417b29004f3f08e0cd56b8f4cb
Diffstat (limited to 'media/tests')
2 files changed, 219 insertions, 0 deletions
diff --git a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/MediaFrameworkUnitTestRunner.java b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/MediaFrameworkUnitTestRunner.java index ecdc287..64b12b7 100644 --- a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/MediaFrameworkUnitTestRunner.java +++ b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/MediaFrameworkUnitTestRunner.java @@ -49,6 +49,7 @@ public class MediaFrameworkUnitTestRunner extends InstrumentationTestRunner { addMediaPlayerStateUnitTests(suite); addMediaScannerUnitTests(suite); addCameraUnitTests(suite); + addImageReaderTests(suite); return suite; } @@ -65,6 +66,10 @@ public class MediaFrameworkUnitTestRunner extends InstrumentationTestRunner { suite.addTestSuite(CameraMetadataTest.class); } + private void addImageReaderTests(TestSuite suite) { + suite.addTestSuite(ImageReaderTest.class); + } + // Running all unit tests checking the state machine may be time-consuming. private void addMediaMetadataRetrieverStateUnitTests(TestSuite suite) { suite.addTestSuite(MediaMetadataRetrieverTest.class); diff --git a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/ImageReaderTest.java b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/ImageReaderTest.java new file mode 100644 index 0000000..900fff4 --- /dev/null +++ b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/unit/ImageReaderTest.java @@ -0,0 +1,214 @@ +/* + * Copyright (C) 2013 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.mediaframeworktest.unit; + +import static org.mockito.Mockito.*; + +import android.graphics.ImageFormat; +import android.media.Image; +import android.media.Image.Plane; +import android.media.ImageReader; +import android.media.ImageReader.OnImageAvailableListener; +import android.media.ImageReader.MaxImagesAcquiredException; +import android.test.AndroidTestCase; +import android.test.suitebuilder.annotation.SmallTest; + +public class ImageReaderTest extends AndroidTestCase { + + private static final String TAG = "ImageReaderTest-unit"; + + private static final int DEFAULT_WIDTH = 640; + private static final int DEFAULT_HEIGHT = 480; + private static final int DEFAULT_FORMAT = ImageFormat.YUV_420_888; + private static final int DEFAULT_MAX_IMAGES = 3; + + private ImageReader mReader; + private Image mImage1; + private Image mImage2; + private Image mImage3; + + @Override + protected void setUp() throws Exception { + super.setUp(); + + /** + * Workaround for mockito and JB-MR2 incompatibility + * + * Avoid java.lang.IllegalArgumentException: dexcache == null + * https://code.google.com/p/dexmaker/issues/detail?id=2 + */ + System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString()); + + // TODO: refactor above into one of the test runners + + mReader = spy(ImageReader.newInstance(DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_FORMAT, + DEFAULT_MAX_IMAGES)); + mImage1 = mock(Image.class); + mImage2 = mock(Image.class); + mImage3 = mock(Image.class); + + /** + * Ensure rest of classes are mockable + */ + { + mock(Plane.class); + mock(OnImageAvailableListener.class); + mock(MaxImagesAcquiredException.class); + } + + } + + @Override + protected void tearDown() throws Exception { + mReader.close(); + + super.tearDown(); + } + + /** + * Return null when there is nothing in the image queue. + */ + @SmallTest + public void testGetLatestImageEmpty() throws MaxImagesAcquiredException { + when(mReader.acquireNextImage()).thenReturn(null); + assertEquals(null, mReader.acquireLatestImage()); + } + + /** + * Return the last image from the image queue, close up the rest. + */ + @SmallTest + public void testGetLatestImage1() throws MaxImagesAcquiredException { + when(mReader.acquireNextImage()).thenReturn(mImage1).thenReturn(null); + assertEquals(mImage1, mReader.acquireLatestImage()); + verify(mImage1, never()).close(); + } + + /** + * Return the last image from the image queue, close up the rest. + */ + @SmallTest + public void testGetLatestImage2() throws MaxImagesAcquiredException { + when(mReader.acquireNextImage()).thenReturn(mImage1).thenReturn(mImage2).thenReturn(null); + assertEquals(mImage2, mReader.acquireLatestImage()); + verify(mImage1, atLeastOnce()).close(); + verify(mImage2, never()).close(); + } + + /** + * Return the last image from the image queue, close up the rest. + */ + @SmallTest + public void testGetLatestImage3() throws MaxImagesAcquiredException { + when(mReader.acquireNextImage()).thenReturn(mImage1).thenReturn(mImage2). + thenReturn(mImage3). + thenReturn(null); + assertEquals(mImage3, mReader.acquireLatestImage()); + verify(mImage1, atLeastOnce()).close(); + verify(mImage2, atLeastOnce()).close(); + verify(mImage3, never()).close(); + } + + /** + * Return null if get a MaxImagesAcquiredException with no images in the queue. + */ + @SmallTest + public void testGetLatestImageTooManyBuffersAcquiredEmpty() throws MaxImagesAcquiredException { + when(mReader.acquireNextImage()).thenThrow(new MaxImagesAcquiredException()); + try { + mReader.acquireLatestImage(); + fail("Expected MaxImagesAcquiredException to be thrown"); + } catch(MaxImagesAcquiredException e) { + } + } + + /** + * Return the last image before we get a MaxImagesAcquiredException. Close up the rest. + */ + @SmallTest + public void testGetLatestImageTooManyBuffersAcquired1() throws MaxImagesAcquiredException { + when(mReader.acquireNextImage()).thenReturn(mImage1). + thenThrow(new MaxImagesAcquiredException()); + assertEquals(mImage1, mReader.acquireLatestImage()); + verify(mImage1, never()).close(); + } + + /** + * Return the last image before we get a MaxImagesAcquiredException. Close up the rest. + */ + @SmallTest + public void testGetLatestImageTooManyBuffersAcquired2() throws MaxImagesAcquiredException { + + when(mReader.acquireNextImage()).thenReturn(mImage1).thenReturn(mImage2). + thenThrow(new MaxImagesAcquiredException()); + assertEquals(mImage2, mReader.acquireLatestImage()); + verify(mImage1, atLeastOnce()).close(); + verify(mImage2, never()).close(); + } + + /** + * Return the last image before we get a MaxImagesAcquiredException. Close up the rest. + */ + @SmallTest + public void testGetLatestImageTooManyBuffersAcquired3() throws MaxImagesAcquiredException { + when(mReader.acquireNextImage()).thenReturn(mImage1).thenReturn(mImage2). + thenReturn(mImage3). + thenThrow(new MaxImagesAcquiredException()); + assertEquals(mImage3, mReader.acquireLatestImage()); + verify(mImage1, atLeastOnce()).close(); + verify(mImage2, atLeastOnce()).close(); + verify(mImage3, never()).close(); + } + + /** + * All images are cleaned up when we get an unexpected Error. + */ + @SmallTest + public void testGetLatestImageExceptionalError() throws MaxImagesAcquiredException { + when(mReader.acquireNextImage()).thenReturn(mImage1).thenReturn(mImage2). + thenReturn(mImage3). + thenThrow(new OutOfMemoryError()); + try { + mReader.acquireLatestImage(); + fail("Impossible"); + } catch(OutOfMemoryError e) { + } + + verify(mImage1, atLeastOnce()).close(); + verify(mImage2, atLeastOnce()).close(); + verify(mImage3, atLeastOnce()).close(); + } + + /** + * All images are cleaned up when we get an unexpected RuntimeException. + */ + @SmallTest + public void testGetLatestImageExceptionalRuntime() throws MaxImagesAcquiredException { + when(mReader.acquireNextImage()).thenReturn(mImage1).thenReturn(mImage2). + thenReturn(mImage3). + thenThrow(new RuntimeException()); + try { + mReader.acquireLatestImage(); + fail("Impossible"); + } catch(RuntimeException e) { + } + + verify(mImage1, atLeastOnce()).close(); + verify(mImage2, atLeastOnce()).close(); + verify(mImage3, atLeastOnce()).close(); + } +} |