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