FileDocCategorySizeDatePackage
CameraStressTest.javaAPI DocAndroid 5.1 API8679Thu Mar 12 22:22:30 GMT 2015com.android.mediaframeworktest.stress

CameraStressTest

public class CameraStressTest extends android.test.ActivityInstrumentationTestCase2
Junit / Instrumentation test case for the following camera APIs: - camera zoom - scene mode adb shell am instrument -e class com.android.mediaframeworktest.stress.CameraStressTest -w com.android.mediaframeworktest/.CameraStressTestRunner

Fields Summary
private static final int
NUMBER_OF_SCENE_MODE_LOOPS
private static final int
NUMBER_OF_ZOOM_LOOPS
private static final long
WAIT_TIMEOUT
private static final String
CAMERA_STRESS_OUTPUT
private com.android.mediaframeworktest.CameraTestHelper
mCameraTestHelper
private android.os.Handler
mHandler
private Thread
mLooperThread
private Writer
mOutput
private String
TAG
Constructors Summary
public CameraStressTest()


      
        super("com.android.mediaframeworktest", MediaFrameworkTest.class);
    
Methods Summary
private voidrunOnLooper(java.lang.Runnable command)

        final Semaphore sem = new Semaphore(0);
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    command.run();
                } finally {
                    sem.release();
                }
            }
        });
        if (!sem.tryAcquire(WAIT_TIMEOUT, TimeUnit.MILLISECONDS)) {
            fail("Failed to run the command on the looper.");
        }
    
protected voidsetUp()

        final Semaphore sem = new Semaphore(0);
        mLooperThread = new Thread() {
            @Override
            public void run() {
                Log.v(TAG, "starting looper");
                Looper.prepare();
                mHandler = new Handler();
                sem.release();
                Looper.loop();
                Log.v(TAG, "quit looper");
            }
        };
        mLooperThread.start();
        if (!sem.tryAcquire(WAIT_TIMEOUT, TimeUnit.MILLISECONDS)) {
            fail("Failed to start the looper.");
        }
        getActivity();
        super.setUp();

        mCameraTestHelper = new CameraTestHelper();
        File stressOutFile = new File(String.format("%s/%s",
                Environment.getExternalStorageDirectory(), CAMERA_STRESS_OUTPUT));
        mOutput = new BufferedWriter(new FileWriter(stressOutFile, true));
        mOutput.write(this.getName() + "\n");
    
protected voidtearDown()

        if (mHandler != null) {
            mHandler.getLooper().quit();
            mHandler = null;
        }
        if (mLooperThread != null) {
            mLooperThread.join(WAIT_TIMEOUT);
            if (mLooperThread.isAlive()) {
                fail("Failed to stop the looper.");
            }
            mLooperThread = null;
        }
        mOutput.write("\n\n");
        mOutput.close();
        super.tearDown();
    
public voidtestStressCameraSceneModes()
Stress test iterating on the various scene modes (action, night, party, etc.)

        try {
            SurfaceHolder surfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder();
            Parameters params = mCameraTestHelper.getCameraParameters();
            List<String> supportedSceneModes = params.getSupportedSceneModes();
            assertNotNull("No scene modes supported", supportedSceneModes);

            mOutput.write("Total number of loops: " +
                    (NUMBER_OF_SCENE_MODE_LOOPS * supportedSceneModes.size()) + "\n");
            Log.v(TAG, "Start preview");
            mOutput.write("No of loop: ");

            for (int i = 0; i < supportedSceneModes.size(); i++) {
                for (int j = 0; j < NUMBER_OF_SCENE_MODE_LOOPS; j++) {
                    runOnLooper(new Runnable() {
                        @Override
                        public void run() {
                            mCameraTestHelper.setupCameraTest();
                        }
                    });
                    Log.v(TAG, "Setting scene mode to " + supportedSceneModes.get(i));
                    params.setSceneMode(supportedSceneModes.get(i));
                    mCameraTestHelper.setParameters(params);
                    mCameraTestHelper.startCameraPreview(surfaceHolder);
                    mCameraTestHelper.capturePhoto();

                    if (i == 0 && j == 0) {
                        mOutput.write(Integer.toString(j + i * NUMBER_OF_SCENE_MODE_LOOPS));
                    } else {
                        mOutput.write(", " + (j + i * NUMBER_OF_SCENE_MODE_LOOPS));
                    }
                }
            }
            mCameraTestHelper.cleanupTestImages();
        } catch (Exception e) {
            Log.e(TAG, e.toString());
            fail("Camera scene mode test Exception");
        }
    
public voidtestStressCameraZoom()
Stress test iterating on the range of supported camera zoom levels

        try {
            SurfaceHolder surfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder();
            Parameters params = mCameraTestHelper.getCameraParameters();

            if (!params.isSmoothZoomSupported() && !params.isZoomSupported()) {
                Log.v(TAG, "Device camera does not support zoom");
                fail("Camera zoom stress test failed due to unsupported feature");
            } else {
                Log.v(TAG, "Device camera does support zoom");
                Log.v(TAG, "Start preview");
                mOutput.write("Total number of loops: " + NUMBER_OF_ZOOM_LOOPS + "\n");
                mOutput.write("No of loops: ");

                int nextZoomLevel = 0;

                for (int i = 0; i < NUMBER_OF_ZOOM_LOOPS; i++) {
                    runOnLooper(new Runnable() {
                        @Override
                        public void run() {
                            mCameraTestHelper.setupCameraTest();
                        }
                    });

                    mCameraTestHelper.startCameraPreview(surfaceHolder);
                    params = mCameraTestHelper.mCamera.getParameters();
                    int currentZoomLevel = params.getZoom();

                    if (nextZoomLevel >= params.getMaxZoom()) {
                        nextZoomLevel = 0;
                    }
                    ++nextZoomLevel;

                    if (params.isSmoothZoomSupported()) {
                        mCameraTestHelper.mCamera.startSmoothZoom(nextZoomLevel);
                    } else {
                        params.setZoom(nextZoomLevel);
                        mCameraTestHelper.setParameters(params);
                    }
                    mCameraTestHelper.capturePhoto();

                    if (i == 0) {
                        mOutput.write(Integer.toString(i));
                    } else {
                        mOutput.write(", " + i);
                    }
                }
            }
            mCameraTestHelper.cleanupTestImages();
        } catch (Exception e) {
            Log.e(TAG, e.toString());
            fail("Camera zoom stress test Exception");
        }