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 androidx.test.filters.LargeTest;
26 
27 import com.android.camera.Camera;
28 
29 import java.io.BufferedWriter;
30 import java.io.FileWriter;
31 
32 /**
33  * Junit / Instrumentation test case for camera test
34  *
35  */
36 
37 public class CameraLatency extends ActivityInstrumentationTestCase2 <Camera> {
38     private String TAG = "CameraLatency";
39     private static final int TOTAL_NUMBER_OF_IMAGECAPTURE = 20;
40     private static final long WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN = 4000;
41     private static final String CAMERA_TEST_OUTPUT_FILE =
42             Environment.getExternalStorageDirectory().toString() + "/mediaStressOut.txt";
43 
44     private long mTotalAutoFocusTime;
45     private long mTotalShutterLag;
46     private long mTotalShutterToPictureDisplayedTime;
47     private long mTotalPictureDisplayedToJpegCallbackTime;
48     private long mTotalJpegCallbackFinishTime;
49     private long mAvgAutoFocusTime;
50     private long mAvgShutterLag = mTotalShutterLag;
51     private long mAvgShutterToPictureDisplayedTime;
52     private long mAvgPictureDisplayedToJpegCallbackTime;
53     private long mAvgJpegCallbackFinishTime;
54 
CameraLatency()55     public CameraLatency() {
56         super(Camera.class);
57     }
58 
59     @Override
setUp()60     protected void setUp() throws Exception {
61         getActivity();
62         super.setUp();
63     }
64 
65     @Override
tearDown()66     protected void tearDown() throws Exception {
67         super.tearDown();
68     }
69 
70     @LargeTest
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                     Camera c = getActivity();
83                     mTotalAutoFocusTime += c.mAutoFocusTime;
84                     mTotalShutterLag += c.mShutterLag;
85                     mTotalShutterToPictureDisplayedTime +=
86                             c.mShutterToPictureDisplayedTime;
87                     mTotalPictureDisplayedToJpegCallbackTime +=
88                             c.mPictureDisplayedToJpegCallbackTime;
89                     mTotalJpegCallbackFinishTime += c.mJpegCallbackFinishTime;
90                 }
91             }
92         } catch (Exception e) {
93             Log.v(TAG, "Got exception", e);
94         }
95         //ToDO: yslau
96         //1) Need to get the baseline from the cupcake so that we can add the
97         //failure condition of the camera latency.
98         //2) Only count those number with succesful capture. Set the timer to invalid
99         //before capture and ignore them if the value is invalid
100         int numberofRun = TOTAL_NUMBER_OF_IMAGECAPTURE - 1;
101         mAvgAutoFocusTime = mTotalAutoFocusTime / numberofRun;
102         mAvgShutterLag = mTotalShutterLag / numberofRun;
103         mAvgShutterToPictureDisplayedTime =
104                 mTotalShutterToPictureDisplayedTime / numberofRun;
105         mAvgPictureDisplayedToJpegCallbackTime =
106                 mTotalPictureDisplayedToJpegCallbackTime / numberofRun;
107         mAvgJpegCallbackFinishTime =
108                 mTotalJpegCallbackFinishTime / numberofRun;
109 
110         try {
111             FileWriter fstream = null;
112             fstream = new FileWriter(CAMERA_TEST_OUTPUT_FILE, true);
113             BufferedWriter out = new BufferedWriter(fstream);
114             out.write("Camera Latency : \n");
115             out.write("Number of loop: " + TOTAL_NUMBER_OF_IMAGECAPTURE + "\n");
116             out.write("Avg AutoFocus = " + mAvgAutoFocusTime + "\n");
117             out.write("Avg mShutterLag = " + mAvgShutterLag + "\n");
118             out.write("Avg mShutterToPictureDisplayedTime = "
119                     + mAvgShutterToPictureDisplayedTime + "\n");
120             out.write("Avg mPictureDisplayedToJpegCallbackTime = "
121                     + mAvgPictureDisplayedToJpegCallbackTime + "\n");
122             out.write("Avg mJpegCallbackFinishTime = " +
123                     mAvgJpegCallbackFinishTime + "\n");
124             out.close();
125             fstream.close();
126         } catch (Exception e) {
127             fail("Camera Latency write output to file");
128         }
129         Log.v(TAG, "The Image capture wait time = " +
130             WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN);
131         Log.v(TAG, "Avg AutoFocus = " + mAvgAutoFocusTime);
132         Log.v(TAG, "Avg mShutterLag = " + mAvgShutterLag);
133         Log.v(TAG, "Avg mShutterToPictureDisplayedTime = "
134                 + mAvgShutterToPictureDisplayedTime);
135         Log.v(TAG, "Avg mPictureDisplayedToJpegCallbackTime = "
136                 + mAvgPictureDisplayedToJpegCallbackTime);
137         Log.v(TAG, "Avg mJpegCallbackFinishTime = " + mAvgJpegCallbackFinishTime);
138     }
139 }
140 
141