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