CameraTest.java revision f013e1afd1e68af5e3b868c26a653bbfb39538f8
1/*
2 * Copyright (C) 2008 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
17package com.android.mediaframeworktest.functional;
18
19import com.android.mediaframeworktest.MediaFrameworkTest;
20import com.android.mediaframeworktest.MediaNames;
21
22import java.io.*;
23
24import android.content.Context;
25import android.hardware.Camera;
26import android.hardware.Camera.PictureCallback;
27import android.hardware.Camera.PreviewCallback;
28import android.hardware.Camera.ShutterCallback;
29import android.test.ActivityInstrumentationTestCase;
30import android.util.Log;
31import android.view.SurfaceHolder;
32
33import android.os.Looper;
34
35import android.test.suitebuilder.annotation.LargeTest;
36
37/**
38 * Junit / Instrumentation test case for the camera api
39
40 */
41public class CameraTest extends ActivityInstrumentationTestCase<MediaFrameworkTest> {
42    private String TAG = "CameraTest";
43
44    private boolean rawPreviewCallbackResult = false;
45    private boolean shutterCallbackResult = false;
46    private boolean rawPictureCallbackResult = false;
47    private boolean jpegPictureCallbackResult = false;
48
49    private static int WAIT_FOR_COMMAND_TO_COMPLETE = 10000;  // Milliseconds.
50
51    private RawPreviewCallback mRawPreviewCallback = new RawPreviewCallback();
52    private TestShutterCallback mShutterCallback = new TestShutterCallback();
53    private RawPictureCallback mRawPictureCallback = new RawPictureCallback();
54    private JpegPictureCallback mJpegPictureCallback = new JpegPictureCallback();
55
56    private boolean mInitialized = false;
57    private Looper mLooper = null;
58    private final Object lock = new Object();
59    private final Object previewDone = new Object();
60
61    Camera mCamera;
62    Context mContext;
63
64    public CameraTest() {
65        super("com.android.mediaframeworktest", MediaFrameworkTest.class);
66    }
67
68    protected void setUp() throws Exception {
69        super.setUp();
70    }
71
72    /*
73     * Initializes the message looper so that the Camera object can
74     * receive the callback messages.
75     */
76    private void initializeMessageLooper() {
77        Log.v(TAG, "start looper");
78        new Thread() {
79            @Override
80            public void run() {
81                // Set up a looper to be used by camera.
82                Looper.prepare();
83                Log.v(TAG, "start loopRun");
84                // Save the looper so that we can terminate this thread
85                // after we are done with it.
86                mLooper = Looper.myLooper();
87                mCamera = Camera.open();
88                synchronized (lock) {
89                    mInitialized = true;
90                    lock.notify();
91                }
92                Looper.loop();  // Blocks forever until Looper.quit() is called.
93                Log.v(TAG, "initializeMessageLooper: quit.");
94            }
95        }.start();
96    }
97
98    /*
99     * Terminates the message looper thread.
100     */
101    private void terminateMessageLooper() {
102        mLooper.quit();
103        mCamera.release();
104    }
105
106    //Implement the previewCallback
107    private final class RawPreviewCallback implements PreviewCallback {
108        public void onPreviewFrame(byte [] rawData, Camera camera) {
109            Log.v(TAG, "Preview callback start");
110            int rawDataLength = 0;
111            if (rawData != null) {
112                rawDataLength = rawData.length;
113            }
114            if (rawDataLength > 0) {
115                rawPreviewCallbackResult = true;
116            } else {
117                rawPreviewCallbackResult = false;
118            }
119            synchronized (previewDone) {
120                Log.v(TAG, "notify the preview callback");
121                previewDone.notify();
122            }
123
124            Log.v(TAG, "Preview callback stop");
125        }
126    };
127
128    //Implement the shutterCallback
129    private final class TestShutterCallback implements ShutterCallback {
130        public void onShutter() {
131            shutterCallbackResult = true;
132            Log.v(TAG, "onShutter called");
133        }
134    };
135
136    //Implement the RawPictureCallback
137    private final class RawPictureCallback implements PictureCallback {
138        public void onPictureTaken(byte [] rawData, Camera camera) {
139           if (rawData != null) {
140               rawPictureCallbackResult = true;
141           } else {
142               rawPictureCallbackResult = false;
143           }
144            Log.v(TAG, "RawPictureCallback callback");
145        }
146    };
147
148    //Implement the JpegPictureCallback
149    private final class JpegPictureCallback implements PictureCallback {
150        public void onPictureTaken(byte [] rawData, Camera camera) {
151            try {
152                if (rawData != null) {
153                    int rawDataLength = rawData.length;
154                    File rawoutput = new File("/sdcard/test.bmp");
155                    FileOutputStream outstream = new FileOutputStream(rawoutput);
156                    outstream.write(rawData);
157                    Log.v(TAG, "JpegPictureCallback rawDataLength = " + rawDataLength);
158                    jpegPictureCallbackResult = true;
159                } else {
160                    jpegPictureCallbackResult = false;
161                }
162                Log.v(TAG, "Jpeg Picture callback");
163            } catch (Exception e) {
164                Log.v(TAG, e.toString());
165            }
166        }
167    };
168
169
170    private void checkTakePicture() {
171        SurfaceHolder mSurfaceHolder;
172        try {
173            mSurfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder();
174            mCamera.setPreviewDisplay(mSurfaceHolder);
175            Log.v(TAG, "Start preview");
176            mCamera.startPreview();
177            synchronized (previewDone) {
178                try {
179                    previewDone.wait(WAIT_FOR_COMMAND_TO_COMPLETE);
180                    Log.v(TAG, "Preview Done");
181                } catch (Exception e) {
182                    Log.v(TAG, "wait was interrupted.");
183                }
184            }
185            mCamera.setPreviewCallback(null);
186            mCamera.takePicture(mShutterCallback, mRawPictureCallback, mJpegPictureCallback);
187            Thread.sleep(MediaNames.WAIT_LONG);
188        } catch (Exception e) {
189            Log.v(TAG, e.toString());
190        }
191    }
192
193    private void checkPreviewCallback() {
194        SurfaceHolder mSurfaceHolder;
195        try {
196            mSurfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder();
197            mCamera.setPreviewDisplay(mSurfaceHolder);
198            Log.v(TAG, "start preview");
199            mCamera.startPreview();
200            synchronized (previewDone) {
201                try {
202                    previewDone.wait(WAIT_FOR_COMMAND_TO_COMPLETE);
203                    Log.v(TAG, "setPreview done");
204                } catch (Exception e) {
205                    Log.v(TAG, "wait was interrupted.");
206                }
207            }
208            mCamera.setPreviewCallback(null);
209        } catch (Exception e) {
210            Log.v(TAG, e.toString());
211        }
212    }
213
214    /*
215     * TODO(yslau): Need to setup the golden rawData and compare the
216     * the new captured rawData with the golden one.
217     *
218     * Test case 1: Take a picture and verify all the callback
219     * functions are called properly.
220     */
221    @LargeTest
222    public void testTakePicture() throws Exception {
223        initializeMessageLooper();
224        synchronized (lock) {
225            try {
226                lock.wait(WAIT_FOR_COMMAND_TO_COMPLETE);
227            } catch(Exception e) {
228                Log.v(TAG, "runTestOnMethod: wait was interrupted.");
229            }
230        }
231        mCamera.setPreviewCallback(mRawPreviewCallback);
232        checkTakePicture();
233        terminateMessageLooper();
234        assertTrue("shutterCallbackResult", shutterCallbackResult);
235        assertTrue("rawPictureCallbackResult", rawPictureCallbackResult);
236        assertTrue("jpegPictureCallbackResult", jpegPictureCallbackResult);
237    }
238
239    /*
240     * Test case 2: Set the preview and
241     * verify the RawPreviewCallback is called
242     */
243    @LargeTest
244    public void testCheckPreview() throws Exception {
245        initializeMessageLooper();
246        synchronized (lock) {
247            try {
248                lock.wait(WAIT_FOR_COMMAND_TO_COMPLETE);
249            } catch(Exception e) {
250                Log.v(TAG, "wait was interrupted.");
251            }
252        }
253        mCamera.setPreviewCallback(mRawPreviewCallback);
254        checkPreviewCallback();
255        terminateMessageLooper();
256        assertTrue("RawPreviewCallbackResult", rawPreviewCallbackResult);
257    }
258
259}
260
261