CameraLatency.java revision 5a717676643985dcd40f9fbf29558c2ec99509c2
1/*
2 * Copyright (C) 2009 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.camera.stress;
18
19import com.android.camera.Camera;
20
21import java.io.BufferedWriter;
22import java.io.FileWriter;
23
24import android.app.Instrumentation;
25import android.test.ActivityInstrumentationTestCase2;
26import android.test.suitebuilder.annotation.LargeTest;
27import android.util.Log;
28import android.view.KeyEvent;
29
30/**
31 * Junit / Instrumentation test case for camera test
32 *
33 */
34
35public class CameraLatency extends ActivityInstrumentationTestCase2 <Camera> {
36    private String TAG = "CameraLatency";
37    private static final int TOTAL_NUMBER_OF_IMAGECAPTURE = 20;
38    private static final long WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN = 3000;
39    private static final String CAMERA_TEST_OUTPUT_FILE = "/sdcard/mediaStressOut.txt";
40
41    private long mTotalAutoFocusTime;
42    private long mTotalShutterLag;
43    private long mTotalShutterToPictureDisplayedTime;
44    private long mTotalPictureDisplayedToJpegCallbackTime;
45    private long mTotalJpegCallbackToFirstFrameTime;
46    private long mAvgAutoFocusTime;
47    private long mAvgShutterLag = mTotalShutterLag;
48    private long mAvgShutterToPictureDisplayedTime;
49    private long mAvgPictureDisplayedToJpegCallbackTime;
50    private long mAvgJpegCallbackToFirstFrameTime;
51
52    public CameraLatency() {
53        super("com.android.camera", Camera.class);
54    }
55
56    @Override
57    protected void setUp() throws Exception {
58        getActivity();
59        super.setUp();
60    }
61
62    @Override
63    protected void tearDown() throws Exception {
64        super.tearDown();
65    }
66
67    @LargeTest
68    public void testImageCapture() {
69        Instrumentation inst = getInstrumentation();
70        try {
71            for (int i = 0; i < TOTAL_NUMBER_OF_IMAGECAPTURE; i++) {
72                Thread.sleep(WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN);
73                inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_UP);
74                inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
75                Thread.sleep(WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN);
76                //skip the first measurement
77                if (i != 0) {
78                    Camera c = getActivity();
79                    mTotalAutoFocusTime += c.mAutoFocusTime;
80                    mTotalShutterLag += c.mShutterLag;
81                    mTotalShutterToPictureDisplayedTime +=
82                            c.mShutterToPictureDisplayedTime;
83                    mTotalPictureDisplayedToJpegCallbackTime +=
84                            c.mPictureDisplayedToJpegCallbackTime;
85                    mTotalJpegCallbackToFirstFrameTime += c.mJpegCallbackToFirstFrameTime;
86                }
87            }
88        } catch (Exception e) {
89            Log.v(TAG, e.toString());
90        }
91        //ToDO: yslau
92        //1) Need to get the baseline from the cupcake so that we can add the
93        //failure condition of the camera latency.
94        //2) Only count those number with succesful capture. Set the timer to invalid
95        //before capture and ignore them if the value is invalid
96        int numberofRun = TOTAL_NUMBER_OF_IMAGECAPTURE - 1;
97        mAvgAutoFocusTime = mTotalAutoFocusTime / numberofRun;
98        mAvgShutterLag = mTotalShutterLag / numberofRun;
99        mAvgShutterToPictureDisplayedTime =
100                mTotalShutterToPictureDisplayedTime / numberofRun;
101        mAvgPictureDisplayedToJpegCallbackTime =
102                mTotalPictureDisplayedToJpegCallbackTime / numberofRun;
103        mAvgJpegCallbackToFirstFrameTime =
104                mTotalJpegCallbackToFirstFrameTime / numberofRun;
105
106        try {
107            FileWriter fstream = null;
108            fstream = new FileWriter(CAMERA_TEST_OUTPUT_FILE, true);
109            BufferedWriter out = new BufferedWriter(fstream);
110            out.write("Camera Latency : \n");
111            out.write("Number of loop: " + TOTAL_NUMBER_OF_IMAGECAPTURE + "\n");
112            out.write("Avg AutoFocus = " + mAvgAutoFocusTime + "\n");
113            out.write("Avg mShutterLag = " + mAvgShutterLag + "\n");
114            out.write("Avg mShutterToPictureDisplayedTime = "
115                    + mAvgShutterToPictureDisplayedTime + "\n");
116            out.write("Avg mPictureDisplayedToJpegCallbackTime = "
117                    + mAvgPictureDisplayedToJpegCallbackTime + "\n");
118            out.write("Avg mJpegCallbackToFirstFrameTime = " +
119                    mAvgJpegCallbackToFirstFrameTime + "\n");
120            out.close();
121            fstream.close();
122        } catch (Exception e) {
123            fail("Camera Latency write output to file");
124        }
125
126        Log.v(TAG, "Avg AutoFocus = " + mAvgAutoFocusTime);
127        Log.v(TAG, "Avg mShutterLag = " + mAvgShutterLag);
128        Log.v(TAG, "Avg mShutterToPictureDisplayedTime = "
129                + mAvgShutterToPictureDisplayedTime);
130        Log.v(TAG, "Avg mPictureDisplayedToJpegCallbackTime = "
131                + mAvgPictureDisplayedToJpegCallbackTime);
132        Log.v(TAG, "Avg mJpegCallbackToFirstFrameTime = " + mAvgJpegCallbackToFirstFrameTime);
133        assertTrue("testImageCapture", true);
134    }
135}
136
137