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 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 <Camera> {
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(Camera.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                    Camera c = getActivity();
82                    mTotalAutoFocusTime += c.mAutoFocusTime;
83                    mTotalShutterLag += c.mShutterLag;
84                    mTotalShutterToPictureDisplayedTime +=
85                            c.mShutterToPictureDisplayedTime;
86                    mTotalPictureDisplayedToJpegCallbackTime +=
87                            c.mPictureDisplayedToJpegCallbackTime;
88                    mTotalJpegCallbackFinishTime += c.mJpegCallbackFinishTime;
89                }
90            }
91        } catch (Exception e) {
92            Log.v(TAG, "Got exception", e);
93        }
94        //ToDO: yslau
95        //1) Need to get the baseline from the cupcake so that we can add the
96        //failure condition of the camera latency.
97        //2) Only count those number with succesful capture. Set the timer to invalid
98        //before capture and ignore them if the value is invalid
99        int numberofRun = TOTAL_NUMBER_OF_IMAGECAPTURE - 1;
100        mAvgAutoFocusTime = mTotalAutoFocusTime / numberofRun;
101        mAvgShutterLag = mTotalShutterLag / numberofRun;
102        mAvgShutterToPictureDisplayedTime =
103                mTotalShutterToPictureDisplayedTime / numberofRun;
104        mAvgPictureDisplayedToJpegCallbackTime =
105                mTotalPictureDisplayedToJpegCallbackTime / numberofRun;
106        mAvgJpegCallbackFinishTime =
107                mTotalJpegCallbackFinishTime / numberofRun;
108
109        try {
110            FileWriter fstream = null;
111            fstream = new FileWriter(CAMERA_TEST_OUTPUT_FILE, true);
112            BufferedWriter out = new BufferedWriter(fstream);
113            out.write("Camera Latency : \n");
114            out.write("Number of loop: " + TOTAL_NUMBER_OF_IMAGECAPTURE + "\n");
115            out.write("Avg AutoFocus = " + mAvgAutoFocusTime + "\n");
116            out.write("Avg mShutterLag = " + mAvgShutterLag + "\n");
117            out.write("Avg mShutterToPictureDisplayedTime = "
118                    + mAvgShutterToPictureDisplayedTime + "\n");
119            out.write("Avg mPictureDisplayedToJpegCallbackTime = "
120                    + mAvgPictureDisplayedToJpegCallbackTime + "\n");
121            out.write("Avg mJpegCallbackFinishTime = " +
122                    mAvgJpegCallbackFinishTime + "\n");
123            out.close();
124            fstream.close();
125        } catch (Exception e) {
126            fail("Camera Latency write output to file");
127        }
128        Log.v(TAG, "The Image capture wait time = " +
129            WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN);
130        Log.v(TAG, "Avg AutoFocus = " + mAvgAutoFocusTime);
131        Log.v(TAG, "Avg mShutterLag = " + mAvgShutterLag);
132        Log.v(TAG, "Avg mShutterToPictureDisplayedTime = "
133                + mAvgShutterToPictureDisplayedTime);
134        Log.v(TAG, "Avg mPictureDisplayedToJpegCallbackTime = "
135                + mAvgPictureDisplayedToJpegCallbackTime);
136        Log.v(TAG, "Avg mJpegCallbackFinishTime = " + mAvgJpegCallbackFinishTime);
137    }
138}
139
140