CameraTest.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
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 // no support for raw data - success if we get the callback 140 rawPictureCallbackResult = true; 141 //if (rawData != null) { 142 // rawPictureCallbackResult = true; 143 //} else { 144 // rawPictureCallbackResult = false; 145 //} 146 Log.v(TAG, "RawPictureCallback callback"); 147 } 148 }; 149 150 //Implement the JpegPictureCallback 151 private final class JpegPictureCallback implements PictureCallback { 152 public void onPictureTaken(byte [] rawData, Camera camera) { 153 try { 154 if (rawData != null) { 155 int rawDataLength = rawData.length; 156 File rawoutput = new File("/sdcard/test.bmp"); 157 FileOutputStream outstream = new FileOutputStream(rawoutput); 158 outstream.write(rawData); 159 Log.v(TAG, "JpegPictureCallback rawDataLength = " + rawDataLength); 160 jpegPictureCallbackResult = true; 161 } else { 162 jpegPictureCallbackResult = false; 163 } 164 Log.v(TAG, "Jpeg Picture callback"); 165 } catch (Exception e) { 166 Log.v(TAG, e.toString()); 167 } 168 } 169 }; 170 171 172 private void checkTakePicture() { 173 SurfaceHolder mSurfaceHolder; 174 try { 175 mSurfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder(); 176 mCamera.setPreviewDisplay(mSurfaceHolder); 177 Log.v(TAG, "Start preview"); 178 mCamera.startPreview(); 179 synchronized (previewDone) { 180 try { 181 previewDone.wait(WAIT_FOR_COMMAND_TO_COMPLETE); 182 Log.v(TAG, "Preview Done"); 183 } catch (Exception e) { 184 Log.v(TAG, "wait was interrupted."); 185 } 186 } 187 mCamera.setPreviewCallback(null); 188 mCamera.takePicture(mShutterCallback, mRawPictureCallback, mJpegPictureCallback); 189 Thread.sleep(MediaNames.WAIT_LONG); 190 } catch (Exception e) { 191 Log.v(TAG, e.toString()); 192 } 193 } 194 195 private void checkPreviewCallback() { 196 SurfaceHolder mSurfaceHolder; 197 try { 198 mSurfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder(); 199 mCamera.setPreviewDisplay(mSurfaceHolder); 200 Log.v(TAG, "start preview"); 201 mCamera.startPreview(); 202 synchronized (previewDone) { 203 try { 204 previewDone.wait(WAIT_FOR_COMMAND_TO_COMPLETE); 205 Log.v(TAG, "setPreview done"); 206 } catch (Exception e) { 207 Log.v(TAG, "wait was interrupted."); 208 } 209 } 210 mCamera.setPreviewCallback(null); 211 } catch (Exception e) { 212 Log.v(TAG, e.toString()); 213 } 214 } 215 216 /* 217 * TODO(yslau): Need to setup the golden rawData and compare the 218 * the new captured rawData with the golden one. 219 * 220 * Test case 1: Take a picture and verify all the callback 221 * functions are called properly. 222 */ 223 @LargeTest 224 public void testTakePicture() throws Exception { 225 initializeMessageLooper(); 226 synchronized (lock) { 227 try { 228 lock.wait(WAIT_FOR_COMMAND_TO_COMPLETE); 229 } catch(Exception e) { 230 Log.v(TAG, "runTestOnMethod: wait was interrupted."); 231 } 232 } 233 mCamera.setPreviewCallback(mRawPreviewCallback); 234 checkTakePicture(); 235 terminateMessageLooper(); 236 assertTrue("shutterCallbackResult", shutterCallbackResult); 237 assertTrue("rawPictureCallbackResult", rawPictureCallbackResult); 238 assertTrue("jpegPictureCallbackResult", jpegPictureCallbackResult); 239 } 240 241 /* 242 * Test case 2: Set the preview and 243 * verify the RawPreviewCallback is called 244 */ 245 @LargeTest 246 public void testCheckPreview() throws Exception { 247 initializeMessageLooper(); 248 synchronized (lock) { 249 try { 250 lock.wait(WAIT_FOR_COMMAND_TO_COMPLETE); 251 } catch(Exception e) { 252 Log.v(TAG, "wait was interrupted."); 253 } 254 } 255 mCamera.setPreviewCallback(mRawPreviewCallback); 256 checkPreviewCallback(); 257 terminateMessageLooper(); 258 assertTrue("RawPreviewCallbackResult", rawPreviewCallbackResult); 259 } 260 261} 262 263