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