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.Activity;
20 import android.app.Instrumentation;
21 import android.content.Intent;
22 import android.os.Environment;
23 import android.provider.MediaStore;
24 import android.test.InstrumentationTestCase;
25 import android.util.Log;
26 
27 import com.android.camera.CameraActivity;
28 
29 import java.io.BufferedWriter;
30 import java.io.FileWriter;
31 
32 /**
33  * Test cases to measure the camera and video recorder startup time.
34  */
35 public class CameraStartUp extends InstrumentationTestCase {
36 
37     private static final int TOTAL_NUMBER_OF_STARTUP = 20;
38 
39     private String TAG = "CameraStartUp";
40     private static final String CAMERA_TEST_OUTPUT_FILE =
41             Environment.getExternalStorageDirectory().toString() + "/mediaStressOut.txt";
42     private static int WAIT_TIME_FOR_PREVIEW = 1500; //1.5 second
43 
launchCamera()44     private long launchCamera() {
45         long startupTime = 0;
46         try {
47             Intent intent = new Intent(Intent.ACTION_MAIN);
48             intent.setClass(getInstrumentation().getTargetContext(), CameraActivity.class);
49             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
50             long beforeStart = System.currentTimeMillis();
51             Instrumentation inst = getInstrumentation();
52             Activity cameraActivity = inst.startActivitySync(intent);
53             long cameraStarted = System.currentTimeMillis();
54             Thread.sleep(WAIT_TIME_FOR_PREVIEW);
55             cameraActivity.finish();
56             startupTime = cameraStarted - beforeStart;
57             Thread.sleep(1000);
58             Log.v(TAG, "camera startup time: " + startupTime);
59         } catch (Exception e) {
60             Log.v(TAG, "Got exception", e);
61             fail("Fails to get the output file");
62         }
63         return startupTime;
64     }
65 
launchVideo()66     private long launchVideo() {
67         long startupTime = 0;
68 
69         try {
70             Intent intent = new Intent(MediaStore.INTENT_ACTION_VIDEO_CAMERA);
71             intent.setClass(getInstrumentation().getTargetContext(), CameraActivity.class);
72             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
73             long beforeStart = System.currentTimeMillis();
74             Instrumentation inst = getInstrumentation();
75             Activity recorderActivity = inst.startActivitySync(intent);
76             long cameraStarted = System.currentTimeMillis();
77             recorderActivity.finish();
78             startupTime = cameraStarted - beforeStart;
79             Log.v(TAG, "Video Startup Time = " + startupTime);
80             // wait for 1s to make sure it reach a clean stage
81             Thread.sleep(WAIT_TIME_FOR_PREVIEW);
82             Log.v(TAG, "video startup time: " + startupTime);
83         } catch (Exception e) {
84             Log.v(TAG, "Got exception", e);
85             fail("Fails to launch video output file");
86         }
87         return startupTime;
88     }
89 
writeToOutputFile(long totalStartupTime, String individualStartupTime, boolean firstStartUp, String Type)90     private void writeToOutputFile(long totalStartupTime,
91             String individualStartupTime, boolean firstStartUp, String Type) throws Exception {
92         // TODO (yslau) : Need to integrate the output data with central
93         // dashboard
94         try {
95             FileWriter fstream = null;
96             fstream = new FileWriter(CAMERA_TEST_OUTPUT_FILE, true);
97             BufferedWriter out = new BufferedWriter(fstream);
98             if (firstStartUp) {
99                 out.write("First " + Type + " Startup: " + totalStartupTime + "\n");
100             } else {
101                 long averageStartupTime = totalStartupTime / (TOTAL_NUMBER_OF_STARTUP -1);
102                 out.write(Type + "startup time: " + "\n");
103                 out.write("Number of loop: " + (TOTAL_NUMBER_OF_STARTUP -1)  + "\n");
104                 out.write(individualStartupTime + "\n\n");
105                 out.write(Type + " average startup time: " + averageStartupTime + " ms\n\n");
106             }
107             out.close();
108             fstream.close();
109         } catch (Exception e) {
110             fail("Camera write output to file");
111         }
112     }
113 
testLaunchVideo()114     public void testLaunchVideo() throws Exception {
115         String individualStartupTime;
116         individualStartupTime = "Individual Video Startup Time = ";
117         long totalStartupTime = 0;
118         long startupTime = 0;
119         for (int i = 0; i < TOTAL_NUMBER_OF_STARTUP; i++) {
120             if (i == 0) {
121                 // Capture the first startup time individually
122                 long firstStartUpTime = launchVideo();
123                 writeToOutputFile(firstStartUpTime, "na", true, "Video");
124             } else {
125                 startupTime = launchVideo();
126                 totalStartupTime += startupTime;
127                 individualStartupTime += startupTime + " ,";
128             }
129         }
130         Log.v(TAG, "totalStartupTime =" + totalStartupTime);
131         writeToOutputFile(totalStartupTime, individualStartupTime, false, "Video");
132     }
133 
testLaunchCamera()134     public void testLaunchCamera() throws Exception {
135         String individualStartupTime;
136         individualStartupTime = "Individual Camera Startup Time = ";
137         long totalStartupTime = 0;
138         long startupTime = 0;
139         for (int i = 0; i < TOTAL_NUMBER_OF_STARTUP; i++) {
140             if (i == 0) {
141                 // Capture the first startup time individually
142                 long firstStartUpTime = launchCamera();
143                 writeToOutputFile(firstStartUpTime, "na", true, "Camera");
144             } else {
145                 startupTime = launchCamera();
146                 totalStartupTime += startupTime;
147                 individualStartupTime += startupTime + " ,";
148             }
149         }
150         Log.v(TAG, "totalStartupTime =" + totalStartupTime);
151         writeToOutputFile(totalStartupTime,
152                 individualStartupTime, false, "Camera");
153     }
154 }
155