1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.mediaframeworktest.unit; 18 19 import static org.mockito.Mockito.*; 20 21 import android.graphics.ImageFormat; 22 import android.media.Image; 23 import android.media.Image.Plane; 24 import android.media.ImageReader; 25 import android.media.ImageReader.OnImageAvailableListener; 26 import android.test.AndroidTestCase; 27 import android.test.suitebuilder.annotation.SmallTest; 28 29 public class ImageReaderTest extends AndroidTestCase { 30 31 private static final String TAG = "ImageReaderTest-unit"; 32 33 private static final int DEFAULT_WIDTH = 640; 34 private static final int DEFAULT_HEIGHT = 480; 35 private static final int DEFAULT_FORMAT = ImageFormat.YUV_420_888; 36 private static final int DEFAULT_MAX_IMAGES = 3; 37 38 private ImageReader mReader; 39 private Image mImage1; 40 private Image mImage2; 41 private Image mImage3; 42 43 @Override 44 protected void setUp() throws Exception { 45 super.setUp(); 46 47 /** 48 * Workaround for mockito and JB-MR2 incompatibility 49 * 50 * Avoid java.lang.IllegalArgumentException: dexcache == null 51 * https://code.google.com/p/dexmaker/issues/detail?id=2 52 */ 53 System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString()); 54 55 // TODO: refactor above into one of the test runners 56 57 mReader = spy(ImageReader.newInstance(DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_FORMAT, 58 DEFAULT_MAX_IMAGES)); 59 mImage1 = mock(Image.class); 60 mImage2 = mock(Image.class); 61 mImage3 = mock(Image.class); 62 63 /** 64 * Ensure rest of classes are mockable 65 */ 66 { 67 mock(Plane.class); 68 mock(OnImageAvailableListener.class); 69 } 70 71 } 72 73 @Override 74 protected void tearDown() throws Exception { 75 mReader.close(); 76 77 super.tearDown(); 78 } 79 80 /** 81 * Return null when there is nothing in the image queue. 82 */ 83 @SmallTest 84 public void testGetLatestImageEmpty() { 85 when(mReader.acquireNextImage()).thenReturn(null); 86 when(mReader.acquireNextImageNoThrowISE()).thenReturn(null); 87 assertEquals(null, mReader.acquireLatestImage()); 88 } 89 90 /** 91 * Return the last image from the image queue, close up the rest. 92 */ 93 @SmallTest 94 public void testGetLatestImage1() { 95 when(mReader.acquireNextImage()).thenReturn(mImage1); 96 when(mReader.acquireNextImageNoThrowISE()).thenReturn(null); 97 assertEquals(mImage1, mReader.acquireLatestImage()); 98 verify(mImage1, never()).close(); 99 } 100 101 /** 102 * Return the last image from the image queue, close up the rest. 103 */ 104 @SmallTest 105 public void testGetLatestImage2() { 106 when(mReader.acquireNextImage()).thenReturn(mImage1); 107 when(mReader.acquireNextImageNoThrowISE()).thenReturn(mImage2).thenReturn(null); 108 assertEquals(mImage2, mReader.acquireLatestImage()); 109 verify(mImage1, atLeastOnce()).close(); 110 verify(mImage2, never()).close(); 111 } 112 113 /** 114 * Return the last image from the image queue, close up the rest. 115 */ 116 @SmallTest 117 public void testGetLatestImage3() { 118 when(mReader.acquireNextImage()).thenReturn(mImage1); 119 when(mReader.acquireNextImageNoThrowISE()).thenReturn(mImage2). 120 thenReturn(mImage3). 121 thenReturn(null); 122 assertEquals(mImage3, mReader.acquireLatestImage()); 123 verify(mImage1, atLeastOnce()).close(); 124 verify(mImage2, atLeastOnce()).close(); 125 verify(mImage3, never()).close(); 126 } 127 128 /** 129 * Return null if get a IllegalStateException with no images in the queue. 130 */ 131 @SmallTest 132 public void testGetLatestImageTooManyBuffersAcquiredEmpty() { 133 when(mReader.acquireNextImage()).thenThrow(new IllegalStateException()); 134 try { 135 mReader.acquireLatestImage(); 136 fail("Expected IllegalStateException to be thrown"); 137 } catch(IllegalStateException e) { 138 } 139 } 140 141 /** 142 * All images are cleaned up when we get an unexpected Error. 143 */ 144 @SmallTest 145 public void testGetLatestImageExceptionalError() { 146 when(mReader.acquireNextImage()).thenReturn(mImage1); 147 when(mReader.acquireNextImageNoThrowISE()).thenReturn(mImage2). 148 thenReturn(mImage3). 149 thenThrow(new OutOfMemoryError()); 150 try { 151 mReader.acquireLatestImage(); 152 fail("Impossible"); 153 } catch(OutOfMemoryError e) { 154 } 155 156 verify(mImage1, atLeastOnce()).close(); 157 verify(mImage2, atLeastOnce()).close(); 158 verify(mImage3, atLeastOnce()).close(); 159 } 160 161 /** 162 * All images are cleaned up when we get an unexpected RuntimeException. 163 */ 164 @SmallTest 165 public void testGetLatestImageExceptionalRuntime() { 166 167 when(mReader.acquireNextImage()).thenReturn(mImage1); 168 when(mReader.acquireNextImageNoThrowISE()).thenReturn(mImage2). 169 thenReturn(mImage3). 170 thenThrow(new RuntimeException()); 171 try { 172 mReader.acquireLatestImage(); 173 fail("Impossible"); 174 } catch(RuntimeException e) { 175 } 176 177 verify(mImage1, atLeastOnce()).close(); 178 verify(mImage2, atLeastOnce()).close(); 179 verify(mImage3, atLeastOnce()).close(); 180 } 181 } 182