CameraTest.java revision 43a335c3cc8c78d64703b9f4755c3cef595e3839
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 //TODO yslau : take out the sleep until bug#1693519 fix 104 try { 105 Thread.sleep(1000); 106 } catch (Exception e){ 107 Log.v(TAG, e.toString()); 108 } 109 mCamera.release(); 110 } 111 112 //Implement the previewCallback 113 private final class RawPreviewCallback implements PreviewCallback { 114 public void onPreviewFrame(byte [] rawData, Camera camera) { 115 Log.v(TAG, "Preview callback start"); 116 int rawDataLength = 0; 117 if (rawData != null) { 118 rawDataLength = rawData.length; 119 } 120 if (rawDataLength > 0) { 121 rawPreviewCallbackResult = true; 122 } else { 123 rawPreviewCallbackResult = false; 124 } 125 synchronized (previewDone) { 126 Log.v(TAG, "notify the preview callback"); 127 previewDone.notify(); 128 } 129 130 Log.v(TAG, "Preview callback stop"); 131 } 132 }; 133 134 //Implement the shutterCallback 135 private final class TestShutterCallback implements ShutterCallback { 136 public void onShutter() { 137 shutterCallbackResult = true; 138 Log.v(TAG, "onShutter called"); 139 } 140 }; 141 142 //Implement the RawPictureCallback 143 private final class RawPictureCallback implements PictureCallback { 144 public void onPictureTaken(byte [] rawData, Camera camera) { 145 // no support for raw data - success if we get the callback 146 rawPictureCallbackResult = true; 147 //if (rawData != null) { 148 // rawPictureCallbackResult = true; 149 //} else { 150 // rawPictureCallbackResult = false; 151 //} 152 Log.v(TAG, "RawPictureCallback callback"); 153 } 154 }; 155 156 //Implement the JpegPictureCallback 157 private final class JpegPictureCallback implements PictureCallback { 158 public void onPictureTaken(byte [] rawData, Camera camera) { 159 try { 160 if (rawData != null) { 161 int rawDataLength = rawData.length; 162 File rawoutput = new File("/sdcard/test.bmp"); 163 FileOutputStream outstream = new FileOutputStream(rawoutput); 164 outstream.write(rawData); 165 Log.v(TAG, "JpegPictureCallback rawDataLength = " + rawDataLength); 166 jpegPictureCallbackResult = true; 167 } else { 168 jpegPictureCallbackResult = false; 169 } 170 Log.v(TAG, "Jpeg Picture callback"); 171 } catch (Exception e) { 172 Log.v(TAG, e.toString()); 173 } 174 } 175 }; 176 177 178 private void checkTakePicture() { 179 SurfaceHolder mSurfaceHolder; 180 try { 181 mSurfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder(); 182 mCamera.setPreviewDisplay(mSurfaceHolder); 183 Log.v(TAG, "Start preview"); 184 mCamera.startPreview(); 185 synchronized (previewDone) { 186 try { 187 previewDone.wait(WAIT_FOR_COMMAND_TO_COMPLETE); 188 Log.v(TAG, "Preview Done"); 189 } catch (Exception e) { 190 Log.v(TAG, "wait was interrupted."); 191 } 192 } 193 mCamera.setPreviewCallback(null); 194 mCamera.takePicture(mShutterCallback, mRawPictureCallback, mJpegPictureCallback); 195 Thread.sleep(MediaNames.WAIT_LONG); 196 } catch (Exception e) { 197 Log.v(TAG, e.toString()); 198 } 199 } 200 201 private void checkPreviewCallback() { 202 SurfaceHolder mSurfaceHolder; 203 try { 204 mSurfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder(); 205 mCamera.setPreviewDisplay(mSurfaceHolder); 206 Log.v(TAG, "start preview"); 207 mCamera.startPreview(); 208 synchronized (previewDone) { 209 try { 210 previewDone.wait(WAIT_FOR_COMMAND_TO_COMPLETE); 211 Log.v(TAG, "setPreview done"); 212 } catch (Exception e) { 213 Log.v(TAG, "wait was interrupted."); 214 } 215 } 216 mCamera.setPreviewCallback(null); 217 } catch (Exception e) { 218 Log.v(TAG, e.toString()); 219 } 220 } 221 222 /* 223 * TODO(yslau): Need to setup the golden rawData and compare the 224 * the new captured rawData with the golden one. 225 * 226 * Test case 1: Take a picture and verify all the callback 227 * functions are called properly. 228 */ 229 // TODO: add this back to LargeTest once bug 2141755 is fixed 230 // @LargeTest 231 public void testTakePicture() throws Exception { 232 synchronized (lock) { 233 initializeMessageLooper(); 234 try { 235 lock.wait(WAIT_FOR_COMMAND_TO_COMPLETE); 236 } catch(Exception e) { 237 Log.v(TAG, "runTestOnMethod: wait was interrupted."); 238 } 239 } 240 mCamera.setPreviewCallback(mRawPreviewCallback); 241 checkTakePicture(); 242 terminateMessageLooper(); 243 assertTrue("shutterCallbackResult", shutterCallbackResult); 244 assertTrue("rawPictureCallbackResult", rawPictureCallbackResult); 245 assertTrue("jpegPictureCallbackResult", jpegPictureCallbackResult); 246 } 247 248 /* 249 * Test case 2: Set the preview and 250 * verify the RawPreviewCallback is called 251 */ 252 @LargeTest 253 public void testCheckPreview() throws Exception { 254 synchronized (lock) { 255 initializeMessageLooper(); 256 try { 257 lock.wait(WAIT_FOR_COMMAND_TO_COMPLETE); 258 } catch(Exception e) { 259 Log.v(TAG, "wait was interrupted."); 260 } 261 } 262 mCamera.setPreviewCallback(mRawPreviewCallback); 263 checkPreviewCallback(); 264 terminateMessageLooper(); 265 assertTrue("RawPreviewCallbackResult", rawPreviewCallbackResult); 266 } 267 268} 269 270