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