1package com.xtremelabs.robolectric.shadows;
2
3
4import android.graphics.Canvas;
5import android.graphics.ImageFormat;
6import android.graphics.Rect;
7import android.hardware.Camera;
8import android.view.Surface;
9import android.view.SurfaceHolder;
10import com.xtremelabs.robolectric.Robolectric;
11import com.xtremelabs.robolectric.WithTestDefaultsRunner;
12
13import org.junit.After;
14import org.junit.Before;
15import org.junit.Test;
16import org.junit.runner.RunWith;
17
18import static org.hamcrest.CoreMatchers.equalTo;
19import static org.hamcrest.CoreMatchers.not;
20import static org.hamcrest.CoreMatchers.notNullValue;
21import static org.hamcrest.CoreMatchers.nullValue;
22import static org.hamcrest.CoreMatchers.sameInstance;
23import static org.junit.Assert.assertThat;
24
25@RunWith(WithTestDefaultsRunner.class)
26public class CameraTest {
27
28    private Camera camera;
29    private ShadowCamera shadowCamera;
30
31    @Before
32    public void setUp() throws Exception {
33        camera = Camera.open();
34        shadowCamera = Robolectric.shadowOf(camera);
35    }
36
37    @After
38    public void tearDown() throws Exception {
39    	ShadowCamera.clearCameraInfo();
40    }
41
42    @Test
43    public void testOpen() throws Exception {
44        assertThat(camera, notNullValue());
45    }
46
47    @Test
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
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
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
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
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
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
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
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
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
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
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
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
176    public void testCameraInfoNoCameras() throws Exception {
177    	assertThat(Camera.getNumberOfCameras(), equalTo(0));
178    }
179
180    @Test
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
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
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
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
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
235        public void addCallback(Callback callback) {
236        }
237
238        @Override
239        public Surface getSurface() {
240            return null;
241        }
242
243        @Override
244        public Rect getSurfaceFrame() {
245            return null;
246        }
247
248        @Override
249        public boolean isCreating() {
250            return false;
251        }
252
253        @Override
254        public Canvas lockCanvas() {
255            return null;
256        }
257
258        @Override
259        public Canvas lockCanvas(Rect dirty) {
260            return null;
261        }
262
263        @Override
264        public void removeCallback(Callback callback) {
265        }
266
267        @Override
268        public void setFixedSize(int width, int height) {
269        }
270
271        @Override
272        public void setFormat(int format) {
273        }
274
275        @Override
276        public void setKeepScreenOn(boolean screenOn) {
277        }
278
279        @Override
280        public void setSizeFromLayout() {
281        }
282
283        @Override
284        public void setType(int type) {
285        }
286
287        @Override
288        public void unlockCanvasAndPost(Canvas canvas) {
289        }
290    }
291}
292