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.Camera; 20 21 import android.app.Instrumentation; 22 import android.test.ActivityInstrumentationTestCase2; 23 import android.test.suitebuilder.annotation.LargeTest; 24 import android.util.Log; 25 import android.view.KeyEvent; 26 import java.io.FileWriter; 27 import java.io.Writer; 28 import java.io.File; 29 import java.io.FileWriter; 30 import java.io.BufferedWriter; 31 import java.io.IOException; 32 import java.io.InputStream; 33 import android.content.Intent; 34 /** 35 * Junit / Instrumentation test case for camera test 36 * 37 * Running the test suite: 38 * 39 * adb shell am instrument \ 40 * -e class com.android.camera.stress.ImageCapture \ 41 * -w com.android.camera.tests/com.android.camera.CameraStressTestRunner 42 * 43 */ 44 45 public class ImageCapture extends ActivityInstrumentationTestCase2 <Camera> { 46 private String TAG = "ImageCapture"; 47 private static final int TOTAL_NUMBER_OF_IMAGECAPTURE = 100; 48 private static final int TOTAL_NUMBER_OF_VIDEOCAPTURE = 100; 49 private static final long WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN = 1500; //1.5 sedconds 50 private static final long WAIT_FOR_VIDEO_CAPTURE_TO_BE_TAKEN = 20000; //20seconds 51 private static final long WAIT_FOR_PREVIEW = 1500; //1.5 seconds 52 private static final long WAIT_FOR_STABLE_STATE = 2000; //2 seconds 53 private static final int NO_OF_LOOPS_TAKE_MEMORY_SNAPSHOT = 10; 54 private static final String CAMERA_MEM_OUTPUTFILE = "/sdcard/ImageCaptureMemOut.txt"; 55 56 //the tolerant memory leak 57 private static final int MAX_ACCEPTED_MEMORY_LEAK_KB = 150; 58 59 private static int mStartMemory = 0; 60 private static int mEndMemory = 0; 61 private static int mStartPid = 0; 62 private static int mEndPid = 0; 63 64 private static final String CAMERA_TEST_OUTPUT_FILE = "/sdcard/mediaStressOut.txt"; 65 private BufferedWriter mOut; 66 private FileWriter mfstream; 67 ImageCapture()68 public ImageCapture() { 69 super("com.android.camera", Camera.class); 70 } 71 72 @Override setUp()73 protected void setUp() throws Exception { 74 getActivity(); 75 prepareOutputFile(); 76 super.setUp(); 77 } 78 79 @Override tearDown()80 protected void tearDown() throws Exception { 81 closeOutputFile(); 82 super.tearDown(); 83 } 84 prepareOutputFile()85 private void prepareOutputFile(){ 86 try{ 87 mfstream = new FileWriter(CAMERA_TEST_OUTPUT_FILE, true); 88 mOut = new BufferedWriter(mfstream); 89 } catch (Exception e){ 90 assertTrue("ImageCapture open output",false); 91 } 92 } 93 closeOutputFile()94 private void closeOutputFile() { 95 try { 96 mOut.write("\n"); 97 mOut.close(); 98 mfstream.close(); 99 } catch (Exception e) { 100 assertTrue("ImageCapture close output", false); 101 } 102 } 103 104 //Write the ps output to the file getMemoryWriteToLog(Writer output)105 public void getMemoryWriteToLog(Writer output) { 106 String memusage = null; 107 memusage = captureMediaserverInfo(); 108 Log.v(TAG, memusage); 109 try { 110 //Write to file output 111 output.write(memusage); 112 } catch (Exception e) { 113 e.toString(); 114 } 115 } 116 captureMediaserverInfo()117 public String captureMediaserverInfo() { 118 String cm = "ps mediaserver"; 119 String memoryUsage = null; 120 121 int ch; 122 try { 123 Process p = Runtime.getRuntime().exec(cm); 124 InputStream in = p.getInputStream(); 125 StringBuffer sb = new StringBuffer(512); 126 while ((ch = in.read()) != -1) { 127 sb.append((char) ch); 128 } 129 memoryUsage = sb.toString(); 130 } catch (IOException e) { 131 Log.v(TAG, e.toString()); 132 } 133 String[] poList = memoryUsage.split("\r|\n|\r\n"); 134 String memusage = poList[1].concat("\n"); 135 return memusage; 136 } 137 getMediaserverPid()138 public int getMediaserverPid(){ 139 String memoryUsage = null; 140 int pidvalue = 0; 141 memoryUsage = captureMediaserverInfo(); 142 String[] poList2 = memoryUsage.split("\t|\\s+"); 143 String pid = poList2[1]; 144 pidvalue = Integer.parseInt(pid); 145 Log.v(TAG, "PID = " + pidvalue); 146 return pidvalue; 147 } 148 getMediaserverVsize()149 public int getMediaserverVsize(){ 150 String memoryUsage = captureMediaserverInfo(); 151 String[] poList2 = memoryUsage.split("\t|\\s+"); 152 String vsize = poList2[3]; 153 int vsizevalue = Integer.parseInt(vsize); 154 Log.v(TAG, "VSIZE = " + vsizevalue); 155 return vsizevalue; 156 } 157 validateMemoryResult(int startPid, int startMemory, Writer output)158 public boolean validateMemoryResult (int startPid, int startMemory, Writer output) throws Exception { 159 Thread.sleep(20000); 160 mEndPid = getMediaserverPid(); 161 mEndMemory = getMediaserverVsize(); 162 output.write("Start Memory = " + startMemory + "\n"); 163 output.write("End Memory = " + mEndMemory + "\n"); 164 Log.v(TAG, "End memory :" + mEndMemory); 165 //Write the total memory different into the output file 166 output.write("The total diff = " + (mEndMemory - startMemory)); 167 output.write("\n\n"); 168 //mediaserver crash 169 if (startPid != mEndPid){ 170 output.write("mediaserver died. Test failed\n"); 171 return false; 172 } 173 //memory leak greter than the tolerant 174 if ((mEndMemory - startMemory) > MAX_ACCEPTED_MEMORY_LEAK_KB ) 175 return false; 176 return true; 177 } 178 179 @LargeTest testImageCapture()180 public void testImageCapture() { 181 //TODO(yslau): Need to integrate the outoput with the central dashboard, 182 //write to a txt file as a temp solution 183 boolean memoryResult = false; 184 Instrumentation inst = getInstrumentation(); 185 File imageCaptureMemFile = new File(CAMERA_MEM_OUTPUTFILE); 186 mStartPid = getMediaserverPid(); 187 mStartMemory = getMediaserverVsize(); 188 Log.v(TAG, "start memory : " + mStartMemory); 189 190 try { 191 Writer output = new BufferedWriter(new FileWriter(imageCaptureMemFile, true)); 192 output.write("Camera Image capture\n"); 193 output.write("No of loops : " + TOTAL_NUMBER_OF_VIDEOCAPTURE + "\n"); 194 getMemoryWriteToLog(output); 195 196 mOut.write("Camera Image Capture\n"); 197 mOut.write("No of loops :" + TOTAL_NUMBER_OF_VIDEOCAPTURE + "\n"); 198 mOut.write("loop: "); 199 200 for (int i = 0; i < TOTAL_NUMBER_OF_IMAGECAPTURE; i++) { 201 Thread.sleep(WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN); 202 inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_UP); 203 inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER); 204 Thread.sleep(WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN); 205 if (( i % NO_OF_LOOPS_TAKE_MEMORY_SNAPSHOT) == 0){ 206 Log.v(TAG, "value of i :" + i); 207 getMemoryWriteToLog(output); 208 } 209 //Check if the mediaserver died, if so, exit the test 210 if (Camera.mMediaServerDied){ 211 mOut.write("\nmedia server died\n"); 212 mOut.flush(); 213 output.close(); 214 Camera.mMediaServerDied = false; 215 assertTrue("Camera Image Capture", false); 216 } 217 mOut.write(" ," + i); 218 mOut.flush(); 219 } 220 Thread.sleep(WAIT_FOR_STABLE_STATE); 221 memoryResult = validateMemoryResult(mStartPid, mStartMemory, output); 222 Log.v(TAG, "End memory : " + getMediaserverVsize()); 223 output.close(); 224 assertTrue("Camera image capture memory test", memoryResult); 225 } catch (Exception e) { 226 Log.v(TAG, e.toString()); 227 assertTrue("testImageCapture", false); 228 } 229 assertTrue("testImageCapture", true); 230 } 231 232 @LargeTest testVideoCapture()233 public void testVideoCapture() { 234 //TODO(yslau): Need to integrate the output with the central dashboard, 235 //write to a txt file as a temp solution 236 boolean memoryResult = false; 237 Instrumentation inst = getInstrumentation(); 238 File imageCaptureMemFile = new File(CAMERA_MEM_OUTPUTFILE); 239 mStartPid = getMediaserverPid(); 240 mStartMemory = getMediaserverVsize(); 241 Log.v(TAG, "start memory : " + mStartMemory); 242 243 try { 244 Writer output = new BufferedWriter(new FileWriter(imageCaptureMemFile, true)); 245 output.write("Camera Video capture\n"); 246 output.write("No of loops : " + TOTAL_NUMBER_OF_VIDEOCAPTURE + "\n"); 247 getMemoryWriteToLog(output); 248 mOut.write("Camera Video Capture\n"); 249 mOut.write("No of loops :" + TOTAL_NUMBER_OF_VIDEOCAPTURE + "\n"); 250 mOut.write("loop: "); 251 // Switch to the video mode 252 Intent intent = new Intent(); 253 intent.setClassName("com.android.camera", 254 "com.android.camera.VideoCamera"); 255 getActivity().startActivity(intent); 256 for (int i = 0; i < TOTAL_NUMBER_OF_VIDEOCAPTURE; i++) { 257 Thread.sleep(WAIT_FOR_PREVIEW); 258 // record a video 259 inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER); 260 Thread.sleep(WAIT_FOR_VIDEO_CAPTURE_TO_BE_TAKEN); 261 inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER); 262 Thread.sleep(WAIT_FOR_PREVIEW); 263 mOut.write(" ," + i); 264 mOut.flush(); 265 } 266 Thread.sleep(WAIT_FOR_STABLE_STATE); 267 memoryResult = validateMemoryResult(mStartPid, mStartMemory, output); 268 Log.v(TAG, "End memory : " + getMediaserverVsize()); 269 output.close(); 270 assertTrue("Camera video capture memory test", memoryResult); 271 } catch (Exception e) { 272 Log.v(TAG, e.toString()); 273 fail("Fails to capture video"); 274 } 275 } 276 277 } 278 279