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.CameraActivity;
20
21import android.app.Instrumentation;
22import android.os.Environment;
23import android.test.ActivityInstrumentationTestCase2;
24import android.test.suitebuilder.annotation.LargeTest;
25import android.util.Log;
26import android.view.KeyEvent;
27
28import java.io.BufferedWriter;
29import java.io.FileWriter;
30
31/**
32 * Junit / Instrumentation test case for camera test
33 *
34 */
35
36public class CameraLatency extends ActivityInstrumentationTestCase2 <CameraActivity> {
37    private String TAG = "CameraLatency";
38    private static final int TOTAL_NUMBER_OF_IMAGECAPTURE = 20;
39    private static final long WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN = 4000;
40    private static final String CAMERA_TEST_OUTPUT_FILE =
41            Environment.getExternalStorageDirectory().toString() + "/mediaStressOut.txt";
42
43    private long mTotalAutoFocusTime;
44    private long mTotalShutterLag;
45    private long mTotalShutterToPictureDisplayedTime;
46    private long mTotalPictureDisplayedToJpegCallbackTime;
47    private long mTotalJpegCallbackFinishTime;
48    private long mAvgAutoFocusTime;
49    private long mAvgShutterLag = mTotalShutterLag;
50    private long mAvgShutterToPictureDisplayedTime;
51    private long mAvgPictureDisplayedToJpegCallbackTime;
52    private long mAvgJpegCallbackFinishTime;
53
54    public CameraLatency() {
55        super(CameraActivity.class);
56    }
57
58    @Override
59    protected void setUp() throws Exception {
60        getActivity();
61        super.setUp();
62    }
63
64    @Override
65    protected void tearDown() throws Exception {
66        super.tearDown();
67    }
68
69    @LargeTest
70    public void testImageCapture() {
71        Log.v(TAG, "start testImageCapture test");
72        Instrumentation inst = getInstrumentation();
73        inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
74        try {
75            for (int i = 0; i < TOTAL_NUMBER_OF_IMAGECAPTURE; i++) {
76                Thread.sleep(WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN);
77                inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
78                Thread.sleep(WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN);
79                //skip the first measurement
80                if (i != 0) {
81                    CameraActivity c = getActivity();
82
83                    // if any of the latency var accessor methods return -1 then the
84                    // camera is set to a different module other than PhotoModule so
85                    // skip the shot and try again
86                    if (c.getAutoFocusTime() != -1) {
87                        mTotalAutoFocusTime += c.getAutoFocusTime();
88                        mTotalShutterLag += c.getShutterLag();
89                        mTotalShutterToPictureDisplayedTime +=
90                                c.getShutterToPictureDisplayedTime();
91                        mTotalPictureDisplayedToJpegCallbackTime +=
92                                c.getPictureDisplayedToJpegCallbackTime();
93                        mTotalJpegCallbackFinishTime += c.getJpegCallbackFinishTime();
94                    }
95                    else {
96                        i--;
97                        continue;
98                    }
99                }
100            }
101        } catch (Exception e) {
102            Log.v(TAG, "Got exception", e);
103        }
104        //ToDO: yslau
105        //1) Need to get the baseline from the cupcake so that we can add the
106        //failure condition of the camera latency.
107        //2) Only count those number with succesful capture. Set the timer to invalid
108        //before capture and ignore them if the value is invalid
109        int numberofRun = TOTAL_NUMBER_OF_IMAGECAPTURE - 1;
110        mAvgAutoFocusTime = mTotalAutoFocusTime / numberofRun;
111        mAvgShutterLag = mTotalShutterLag / numberofRun;
112        mAvgShutterToPictureDisplayedTime =
113                mTotalShutterToPictureDisplayedTime / numberofRun;
114        mAvgPictureDisplayedToJpegCallbackTime =
115                mTotalPictureDisplayedToJpegCallbackTime / numberofRun;
116        mAvgJpegCallbackFinishTime =
117                mTotalJpegCallbackFinishTime / numberofRun;
118
119        try {
120            FileWriter fstream = null;
121            fstream = new FileWriter(CAMERA_TEST_OUTPUT_FILE, true);
122            BufferedWriter out = new BufferedWriter(fstream);
123            out.write("Camera Latency : \n");
124            out.write("Number of loop: " + TOTAL_NUMBER_OF_IMAGECAPTURE + "\n");
125            out.write("Avg AutoFocus = " + mAvgAutoFocusTime + "\n");
126            out.write("Avg mShutterLag = " + mAvgShutterLag + "\n");
127            out.write("Avg mShutterToPictureDisplayedTime = "
128                    + mAvgShutterToPictureDisplayedTime + "\n");
129            out.write("Avg mPictureDisplayedToJpegCallbackTime = "
130                    + mAvgPictureDisplayedToJpegCallbackTime + "\n");
131            out.write("Avg mJpegCallbackFinishTime = " +
132                    mAvgJpegCallbackFinishTime + "\n");
133            out.close();
134            fstream.close();
135        } catch (Exception e) {
136            fail("Camera Latency write output to file");
137        }
138        Log.v(TAG, "The Image capture wait time = " +
139            WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN);
140        Log.v(TAG, "Avg AutoFocus = " + mAvgAutoFocusTime);
141        Log.v(TAG, "Avg mShutterLag = " + mAvgShutterLag);
142        Log.v(TAG, "Avg mShutterToPictureDisplayedTime = "
143                + mAvgShutterToPictureDisplayedTime);
144        Log.v(TAG, "Avg mPictureDisplayedToJpegCallbackTime = "
145                + mAvgPictureDisplayedToJpegCallbackTime);
146        Log.v(TAG, "Avg mJpegCallbackFinishTime = " + mAvgJpegCallbackFinishTime);
147    }
148}
149
150