FileDocCategorySizeDatePackage
ImageReaderTest.javaAPI DocAndroid 5.1 API6162Thu Mar 12 22:22:30 GMT 2015com.android.mediaframeworktest.unit

ImageReaderTest

public class ImageReaderTest extends android.test.AndroidTestCase

Fields Summary
private static final String
TAG
private static final int
DEFAULT_WIDTH
private static final int
DEFAULT_HEIGHT
private static final int
DEFAULT_FORMAT
private static final int
DEFAULT_MAX_IMAGES
private android.media.ImageReader
mReader
private android.media.Image
mImage1
private android.media.Image
mImage2
private android.media.Image
mImage3
Constructors Summary
Methods Summary
protected voidsetUp()


    
         
        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);
        }

    
protected voidtearDown()

        mReader.close();

        super.tearDown();
    
public voidtestGetLatestImage1()
Return the last image from the image queue, close up the rest.

        when(mReader.acquireNextImage()).thenReturn(mImage1);
        when(mReader.acquireNextImageNoThrowISE()).thenReturn(null);
        assertEquals(mImage1, mReader.acquireLatestImage());
        verify(mImage1, never()).close();
    
public voidtestGetLatestImage2()
Return the last image from the image queue, close up the rest.

        when(mReader.acquireNextImage()).thenReturn(mImage1);
        when(mReader.acquireNextImageNoThrowISE()).thenReturn(mImage2).thenReturn(null);
        assertEquals(mImage2, mReader.acquireLatestImage());
        verify(mImage1, atLeastOnce()).close();
        verify(mImage2, never()).close();
    
public voidtestGetLatestImage3()
Return the last image from the image queue, close up the rest.

        when(mReader.acquireNextImage()).thenReturn(mImage1);
        when(mReader.acquireNextImageNoThrowISE()).thenReturn(mImage2).
                                                   thenReturn(mImage3).
                                                   thenReturn(null);
        assertEquals(mImage3, mReader.acquireLatestImage());
        verify(mImage1, atLeastOnce()).close();
        verify(mImage2, atLeastOnce()).close();
        verify(mImage3, never()).close();
    
public voidtestGetLatestImageEmpty()
Return null when there is nothing in the image queue.

        when(mReader.acquireNextImage()).thenReturn(null);
        when(mReader.acquireNextImageNoThrowISE()).thenReturn(null);
        assertEquals(null, mReader.acquireLatestImage());
    
public voidtestGetLatestImageExceptionalError()
All images are cleaned up when we get an unexpected Error.

        when(mReader.acquireNextImage()).thenReturn(mImage1);
        when(mReader.acquireNextImageNoThrowISE()).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();
    
public voidtestGetLatestImageExceptionalRuntime()
All images are cleaned up when we get an unexpected RuntimeException.


        when(mReader.acquireNextImage()).thenReturn(mImage1);
        when(mReader.acquireNextImageNoThrowISE()).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();
    
public voidtestGetLatestImageTooManyBuffersAcquiredEmpty()
Return null if get a IllegalStateException with no images in the queue.

        when(mReader.acquireNextImage()).thenThrow(new IllegalStateException());
        try {
            mReader.acquireLatestImage();
            fail("Expected IllegalStateException to be thrown");
        } catch(IllegalStateException e) {
        }