1 /* 2 * Copyright (C) 2014 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 package com.android.sensor.tests; 17 18 import com.android.tradefed.config.Option; 19 import com.android.tradefed.device.DeviceNotAvailableException; 20 import com.android.tradefed.result.FileInputStreamSource; 21 import com.android.tradefed.result.ITestInvocationListener; 22 import com.android.tradefed.result.InputStreamSource; 23 import com.android.tradefed.result.LogDataType; 24 import com.android.tradefed.testtype.InstrumentationTest; 25 import com.android.tradefed.util.FileUtil; 26 import com.android.tradefed.util.StreamUtil; 27 28 import org.junit.Assert; 29 30 import java.io.File; 31 32 /** 33 * Run the sensor test instrumentation. 34 */ 35 public class SingleSensorTests extends InstrumentationTest { 36 37 @Option(name = "output-file-prefix", description = "The files that the test generates. These" 38 + "files will be pulled, uploaded, and deleted from the device.") 39 private String mOutputPrefix = "single_sensor_"; 40 41 @Option(name = "screen-off", description = "Whether to run the tests with the screen off") 42 private boolean mScreenOff = false; 43 44 /** 45 * Run the instrumentation tests, pull the test generated files, and clean up. 46 */ 47 @Override 48 public void run(ITestInvocationListener listener) throws DeviceNotAvailableException { 49 Assert.assertNotNull(getDevice()); 50 51 try { 52 if (mScreenOff) { 53 turnScreenOff(); 54 } 55 super.run(listener); 56 pullFiles(listener); 57 } finally { 58 cleanUp(); 59 } 60 } 61 62 /** 63 * Helper method to turn screen off. 64 */ 65 private void turnScreenOff() throws DeviceNotAvailableException { 66 getDevice().executeShellCommand("svc power stayon false"); 67 getDevice().executeShellCommand("input keyevent 26"); 68 } 69 70 /** 71 * Helper method to pull the test files. 72 */ 73 private void pullFiles(ITestInvocationListener listener) throws DeviceNotAvailableException { 74 String rawFileList = getDevice().executeShellCommand( 75 String.format("ls ${EXTERNAL_STORAGE}/%s*", mOutputPrefix)); 76 if (rawFileList != null && !rawFileList.contains("No such file or directory")) { 77 String[] filePaths = rawFileList.split("\r?\n"); 78 for (String filePath : filePaths) { 79 pullFile(listener, filePath); 80 } 81 } 82 } 83 84 /** 85 * Helper method to pull a single file. 86 */ 87 private void pullFile(ITestInvocationListener listener, String filePath) 88 throws DeviceNotAvailableException { 89 String fileName = new File(filePath).getName(); 90 String report_key = FileUtil.getBaseName(fileName); 91 92 File outputFile = null; 93 InputStreamSource outputSource = null; 94 try { 95 outputFile = getDevice().pullFile(filePath); 96 if (outputFile != null) { 97 outputSource = new FileInputStreamSource(outputFile); 98 listener.testLog(report_key, LogDataType.TEXT, outputSource); 99 } 100 } finally { 101 FileUtil.deleteFile(outputFile); 102 StreamUtil.cancel(outputSource); 103 } 104 } 105 106 /** 107 * Helper method to clean up. 108 */ 109 private void cleanUp() throws DeviceNotAvailableException { 110 getDevice().executeShellCommand(String.format("rm ${EXTERNAL_STORAGE}/%s*", mOutputPrefix)); 111 } 112 } 113