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