FileDocCategorySizeDatePackage
CameraTest.javaAPI DocAndroid 5.1 API9493Thu Mar 12 22:22:30 GMT 2015com.android.mediaframeworktest.functional

CameraTest

public class CameraTest extends android.test.ActivityInstrumentationTestCase
Junit / Instrumentation test case for the camera api To run only tests in this class: adb shell am instrument \ -e class com.android.mediaframeworktest.functional.CameraTest \ -w com.android.mediaframeworktest/.MediaFrameworkTestRunner

Fields Summary
private String
TAG
private boolean
rawPreviewCallbackResult
private boolean
shutterCallbackResult
private boolean
rawPictureCallbackResult
private boolean
jpegPictureCallbackResult
private static int
WAIT_FOR_COMMAND_TO_COMPLETE
private static final int
CAMERA_ID
private RawPreviewCallback
mRawPreviewCallback
private TestShutterCallback
mShutterCallback
private RawPictureCallback
mRawPictureCallback
private JpegPictureCallback
mJpegPictureCallback
private boolean
mInitialized
private android.os.Looper
mLooper
private final android.os.ConditionVariable
mPreviewDone
private final android.os.ConditionVariable
mSnapshotDone
android.hardware.Camera
mCamera
android.content.Context
mContext
Constructors Summary
public CameraTest()

  
      
        super("com.android.mediaframeworktest", MediaFrameworkTest.class);
    
Methods Summary
private voidcheckPreviewCallback()

 
        SurfaceHolder mSurfaceHolder;
        try {
            mSurfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder();
            mCamera.setPreviewDisplay(mSurfaceHolder);
            Log.v(TAG, "start preview");
            mCamera.startPreview();
            waitForPreviewDone();
            mCamera.setPreviewCallback(null);
        } catch (Exception e) {
            Log.v(TAG, e.toString());
        }      
    
private voidcheckTakePicture()

 
        SurfaceHolder mSurfaceHolder;
        try {
            mSurfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder();
            mCamera.setPreviewDisplay(mSurfaceHolder);
            Log.v(TAG, "Start preview");
            mCamera.startPreview();
            waitForPreviewDone();
            mCamera.setPreviewCallback(null);
            mCamera.takePicture(mShutterCallback, mRawPictureCallback, mJpegPictureCallback);
            waitForSnapshotDone();
        } catch (Exception e) {
            Log.v(TAG, e.toString());
        }      
    
private voidinitializeMessageLooper()

        final ConditionVariable startDone = new ConditionVariable();
        Log.v(TAG, "start looper");
        new Thread() {
            @Override
            public void run() {
                // Set up a looper to be used by camera.
                Looper.prepare();
                Log.v(TAG, "start loopRun");
                // Save the looper so that we can terminate this thread 
                // after we are done with it.
                mLooper = Looper.myLooper();
                mCamera = Camera.open(CAMERA_ID);
                startDone.open();
                Looper.loop();  // Blocks forever until Looper.quit() is called.
                Log.v(TAG, "initializeMessageLooper: quit.");
            }
        }.start();

        if (!startDone.block(WAIT_FOR_COMMAND_TO_COMPLETE)) {
            fail("initializeMessageLooper: start timeout");
        }
    
protected voidsetUp()

        super.setUp(); 
    
private voidterminateMessageLooper()

        mLooper.quit();
        // Looper.quit() is asynchronous. The looper may still has some
        // preview callbacks in the queue after quit is called. The preview
        // callback still uses the camera object (setHasPreviewCallback).
        // After camera is released, RuntimeException will be thrown from
        // the method. So we need to join the looper thread here.
        mLooper.getThread().join();
        mCamera.release();
    
public voidtestCheckPreview()

  
        initializeMessageLooper();
        mCamera.setPreviewCallback(mRawPreviewCallback);
        checkPreviewCallback();     
        terminateMessageLooper();
        assertTrue("RawPreviewCallbackResult", rawPreviewCallbackResult);
    
public voidtestTakePicture()

  
        initializeMessageLooper();
        mCamera.setPreviewCallback(mRawPreviewCallback);
        checkTakePicture();
        terminateMessageLooper();
        assertTrue("shutterCallbackResult", shutterCallbackResult);
        assertTrue("rawPictureCallbackResult", rawPictureCallbackResult);
        assertTrue("jpegPictureCallbackResult", jpegPictureCallbackResult);
    
private voidwaitForPreviewDone()

        if (!mPreviewDone.block(WAIT_FOR_COMMAND_TO_COMPLETE)) {
            Log.v(TAG, "waitForPreviewDone: timeout");
        }
        mPreviewDone.close();
    
private voidwaitForSnapshotDone()

        if (!mSnapshotDone.block(MediaNames.WAIT_SNAPSHOT_TIME)) {
            // timeout could be expected or unexpected. The caller will decide.
            Log.v(TAG, "waitForSnapshotDone: timeout");
        }
        mSnapshotDone.close();