1 package com.xtremelabs.robolectric.shadows;
2 
3 
4 import android.graphics.Canvas;
5 import android.graphics.ImageFormat;
6 import android.graphics.Rect;
7 import android.hardware.Camera;
8 import android.view.Surface;
9 import android.view.SurfaceHolder;
10 import com.xtremelabs.robolectric.Robolectric;
11 import com.xtremelabs.robolectric.WithTestDefaultsRunner;
12 
13 import org.junit.After;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 
18 import static org.hamcrest.CoreMatchers.equalTo;
19 import static org.hamcrest.CoreMatchers.not;
20 import static org.hamcrest.CoreMatchers.notNullValue;
21 import static org.hamcrest.CoreMatchers.nullValue;
22 import static org.hamcrest.CoreMatchers.sameInstance;
23 import static org.junit.Assert.assertThat;
24 
25 @RunWith(WithTestDefaultsRunner.class)
26 public class CameraTest {
27 
28     private Camera camera;
29     private ShadowCamera shadowCamera;
30 
31     @Before
setUp()32     public void setUp() throws Exception {
33         camera = Camera.open();
34         shadowCamera = Robolectric.shadowOf(camera);
35     }
36 
37     @After
tearDown()38     public void tearDown() throws Exception {
39     	ShadowCamera.clearCameraInfo();
40     }
41 
42     @Test
testOpen()43     public void testOpen() throws Exception {
44         assertThat(camera, notNullValue());
45     }
46 
47     @Test
testUnlock()48     public void testUnlock() throws Exception {
49         assertThat(shadowCamera.isLocked(), equalTo(true));
50         camera.unlock();
51         assertThat(shadowCamera.isLocked(), equalTo(false));
52     }
53 
54     @Test
testReconnect()55     public void testReconnect() throws Exception {
56         camera.unlock();
57         assertThat(shadowCamera.isLocked(), equalTo(false));
58         camera.reconnect();
59         assertThat(shadowCamera.isLocked(), equalTo(true));
60     }
61 
62     @Test
testGetParameters()63     public void testGetParameters() throws Exception {
64         Camera.Parameters parameters = camera.getParameters();
65         assertThat(parameters, notNullValue());
66         assertThat(parameters.getSupportedPreviewFormats(), notNullValue());
67         assertThat(parameters.getSupportedPreviewFormats().size(), not(equalTo(0)));
68     }
69 
70     @Test
testSetParameters()71     public void testSetParameters() throws Exception {
72         Camera.Parameters parameters = camera.getParameters();
73         assertThat(parameters.getPreviewFormat(), equalTo(ImageFormat.NV21));
74         parameters.setPreviewFormat(ImageFormat.JPEG);
75         camera.setParameters(parameters);
76         assertThat(camera.getParameters().getPreviewFormat(), equalTo(ImageFormat.JPEG));
77     }
78 
79     @Test
testSetPreviewDisplay()80     public void testSetPreviewDisplay() throws Exception {
81         SurfaceHolder previewSurfaceHolder = new TestSurfaceHolder();
82         camera.setPreviewDisplay(previewSurfaceHolder);
83         assertThat(shadowCamera.getPreviewDisplay(), sameInstance(previewSurfaceHolder));
84     }
85 
86     @Test
testStartPreview()87     public void testStartPreview() throws Exception {
88         assertThat(shadowCamera.isPreviewing(), equalTo(false));
89         camera.startPreview();
90         assertThat(shadowCamera.isPreviewing(), equalTo(true));
91     }
92 
93     @Test
testStopPreview()94     public void testStopPreview() throws Exception {
95         camera.startPreview();
96         assertThat(shadowCamera.isPreviewing(), equalTo(true));
97         camera.stopPreview();
98         assertThat(shadowCamera.isPreviewing(), equalTo(false));
99     }
100 
101     @Test
testRelease()102     public void testRelease() throws Exception {
103         assertThat(shadowCamera.isReleased(), equalTo(false));
104         camera.release();
105         assertThat(shadowCamera.isReleased(), equalTo(true));
106     }
107 
108     @Test
testSetPreviewCallbacks()109     public void testSetPreviewCallbacks() throws Exception {
110     	TestPreviewCallback callback = new TestPreviewCallback();
111     	assertThat(callback.camera, nullValue());
112     	assertThat(callback.data, nullValue());
113 
114     	camera.setPreviewCallback(callback);
115     	shadowCamera.invokePreviewCallback("foobar".getBytes());
116 
117     	assertThat(callback.camera, sameInstance(camera));
118     	assertThat(callback.data, equalTo("foobar".getBytes()));
119     }
120 
121     @Test
testSetOneShotPreviewCallbacks()122     public void testSetOneShotPreviewCallbacks() throws Exception {
123     	TestPreviewCallback callback = new TestPreviewCallback();
124     	assertThat(callback.camera, nullValue());
125     	assertThat(callback.data, nullValue());
126 
127     	camera.setOneShotPreviewCallback(callback);
128     	shadowCamera.invokePreviewCallback("foobar".getBytes());
129 
130     	assertThat(callback.camera, sameInstance(camera));
131     	assertThat(callback.data, equalTo("foobar".getBytes()));
132     }
133 
134     @Test
testPreviewCallbacksWithBuffers()135     public void testPreviewCallbacksWithBuffers() throws Exception {
136     	TestPreviewCallback callback = new TestPreviewCallback();
137     	assertThat(callback.camera, nullValue());
138     	assertThat(callback.data, nullValue());
139 
140     	camera.setPreviewCallbackWithBuffer(callback);
141     	shadowCamera.invokePreviewCallback("foobar".getBytes());
142 
143     	assertThat(callback.camera, sameInstance(camera));
144     	assertThat(callback.data, equalTo("foobar".getBytes()));
145     }
146 
147     @Test
testClearPreviewCallback()148     public void testClearPreviewCallback() throws Exception {
149     	TestPreviewCallback callback = new TestPreviewCallback();
150     	assertThat(callback.camera, nullValue());
151     	assertThat(callback.data, nullValue());
152 
153     	camera.setPreviewCallback(callback);
154     	camera.setPreviewCallback(null);
155 
156     	shadowCamera.invokePreviewCallback("foobar".getBytes());
157     	assertThat(callback.camera, nullValue());
158     	assertThat(callback.data, nullValue());
159 
160     	camera.setOneShotPreviewCallback(callback);
161     	camera.setOneShotPreviewCallback(null);
162 
163     	shadowCamera.invokePreviewCallback("foobar".getBytes());
164     	assertThat(callback.camera, nullValue());
165     	assertThat(callback.data, nullValue());
166 
167     	camera.setPreviewCallbackWithBuffer(callback);
168     	camera.setPreviewCallbackWithBuffer(null);
169 
170     	shadowCamera.invokePreviewCallback("foobar".getBytes());
171     	assertThat(callback.camera, nullValue());
172     	assertThat(callback.data, nullValue());
173     }
174 
175     @Test
testCameraInfoNoCameras()176     public void testCameraInfoNoCameras() throws Exception {
177     	assertThat(Camera.getNumberOfCameras(), equalTo(0));
178     }
179 
180     @Test
testCameraInfoBackOnly()181     public void testCameraInfoBackOnly() throws Exception {
182     	Camera.CameraInfo cameraQuery = new Camera.CameraInfo();
183 
184 		addBackCamera();
185     	Camera.getCameraInfo(0, cameraQuery);
186 
187     	assertThat(Camera.getNumberOfCameras(), equalTo(1));
188     	assertThat(cameraQuery.facing, equalTo(Camera.CameraInfo.CAMERA_FACING_BACK));
189     	assertThat(cameraQuery.orientation, equalTo(0));
190     }
191 
192     @Test
testCameraInfoBackAndFront()193     public void testCameraInfoBackAndFront() throws Exception {
194     	Camera.CameraInfo cameraQuery = new Camera.CameraInfo();
195 		addBackCamera();
196     	addFrontCamera();
197 
198     	assertThat( Camera.getNumberOfCameras(), equalTo(2) );
199     	Camera.getCameraInfo(0, cameraQuery);
200     	assertThat( cameraQuery.facing, equalTo(Camera.CameraInfo.CAMERA_FACING_BACK) );
201     	assertThat( cameraQuery.orientation, equalTo(0) );
202     	Camera.getCameraInfo(1, cameraQuery);
203     	assertThat( cameraQuery.facing, equalTo(Camera.CameraInfo.CAMERA_FACING_FRONT) );
204     	assertThat( cameraQuery.orientation, equalTo(90) );
205     }
206 
addBackCamera()207 	private void addBackCamera() {
208 		Camera.CameraInfo frontCamera = new Camera.CameraInfo();
209 		frontCamera.facing = Camera.CameraInfo.CAMERA_FACING_BACK;
210 		frontCamera.orientation = 0;
211 		ShadowCamera.addCameraInfo(0, frontCamera);
212 	}
213 
addFrontCamera()214 	private void addFrontCamera() {
215 		Camera.CameraInfo backCamera = new Camera.CameraInfo();
216 		backCamera.facing = Camera.CameraInfo.CAMERA_FACING_FRONT;
217 		backCamera.orientation = 90;
218 		ShadowCamera.addCameraInfo(1, backCamera);
219 	}
220 
221     private class TestPreviewCallback implements Camera.PreviewCallback {
222     	public Camera camera = null;
223     	public byte[] data = null;
224 
225 		@Override
onPreviewFrame(byte[] data, Camera camera)226 		public void onPreviewFrame(byte[] data, Camera camera) {
227 			this.data = data;
228 			this.camera = camera;
229 		}
230     }
231 
232     private class TestSurfaceHolder implements SurfaceHolder {
233 
234         @Override
addCallback(Callback callback)235         public void addCallback(Callback callback) {
236         }
237 
238         @Override
getSurface()239         public Surface getSurface() {
240             return null;
241         }
242 
243         @Override
getSurfaceFrame()244         public Rect getSurfaceFrame() {
245             return null;
246         }
247 
248         @Override
isCreating()249         public boolean isCreating() {
250             return false;
251         }
252 
253         @Override
lockCanvas()254         public Canvas lockCanvas() {
255             return null;
256         }
257 
258         @Override
lockCanvas(Rect dirty)259         public Canvas lockCanvas(Rect dirty) {
260             return null;
261         }
262 
263         @Override
removeCallback(Callback callback)264         public void removeCallback(Callback callback) {
265         }
266 
267         @Override
setFixedSize(int width, int height)268         public void setFixedSize(int width, int height) {
269         }
270 
271         @Override
setFormat(int format)272         public void setFormat(int format) {
273         }
274 
275         @Override
setKeepScreenOn(boolean screenOn)276         public void setKeepScreenOn(boolean screenOn) {
277         }
278 
279         @Override
setSizeFromLayout()280         public void setSizeFromLayout() {
281         }
282 
283         @Override
setType(int type)284         public void setType(int type) {
285         }
286 
287         @Override
unlockCanvasAndPost(Canvas canvas)288         public void unlockCanvasAndPost(Canvas canvas) {
289         }
290     }
291 }
292