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 17 package com.android.helper; 18 19 import android.os.Bundle; 20 import android.os.Environment; 21 import android.os.SystemClock; 22 import android.test.InstrumentationTestRunner; 23 24 import androidx.test.uiautomator.UiAutomatorTestCase; 25 26 import java.io.File; 27 import java.io.FileInputStream; 28 import java.io.FileNotFoundException; 29 import java.io.FileWriter; 30 import java.io.IOException; 31 import java.util.Properties; 32 33 public class PowerTestHelper extends UiAutomatorTestCase { 34 private final static String PARAM_CONFIG = "conf"; 35 private final static String SD_CARD_PATH = 36 Environment.getExternalStorageDirectory().getAbsolutePath() + "/"; 37 private final static String POWER_OUTPUT = SD_CARD_PATH + "autotester.log"; 38 private final static String PROPERTY_FILE_NAME = SD_CARD_PATH + "PowerTests.conf"; 39 private final static long SYNC_DELAY = 10 * 1000; // 10 secs 40 private final static String TAG = "PowerTestHelper"; 41 42 private Bundle mParams; 43 44 @Override getParams()45 public Bundle getParams() { 46 if (mParams == null) { 47 mParams = ((InstrumentationTestRunner) getInstrumentation()).getArguments(); 48 } 49 return mParams; 50 } 51 52 @Override setUp()53 protected void setUp() throws Exception { 54 super.setUp(); 55 mParams = getParams(); 56 assertNotNull("mParams is null", mParams); 57 58 // Wait for USB to be disconnected by the test harness 59 SystemClock.sleep(SYNC_DELAY); 60 } 61 62 /** 63 * Expects a file from the command line via conf param or default following 64 * format each on its own line. <code> 65 * key=Value 66 * Browser_URL1=cnn.com 67 * Browser_URL2=google.com 68 * Camera_ShutterDelay=1000 69 * etc... 70 * </code> 71 * @param Bundle params 72 * @param key 73 * @return the value of the property else defaultValue 74 * @throws FileNotFoundException 75 * @throws IOException 76 */ getPropertyString(String key)77 protected String getPropertyString(String key) 78 throws FileNotFoundException, IOException { 79 String value = getProperty(key); 80 if (value != null && !value.isEmpty()) 81 return value; 82 return null; 83 } 84 85 /** 86 * Expects a file from the command line via conf param or default following 87 * format each on its own line. <code> 88 * key=Value 89 * Browser_URL1=cnn.com 90 * Browser_URL2=google.com 91 * Camera_ShutterDelay=1000 92 * etc... 93 * </code> 94 * @param Bundle params 95 * @param key 96 * @return the value of the property else defaultValue 97 * @throws FileNotFoundException 98 * @throws IOException 99 */ getPropertyLong(String key)100 protected long getPropertyLong(String key) 101 throws FileNotFoundException, IOException { 102 String value = getProperty(key); 103 if (value != null && !value.trim().isEmpty()) 104 return Long.valueOf(value.trim()); 105 return 0; 106 } 107 getProperty(String key)108 private String getProperty(String key) 109 throws FileNotFoundException, IOException { 110 String value; 111 112 Properties prop = new Properties(); 113 FileInputStream inputStream = new FileInputStream(mParams.getString(PARAM_CONFIG, 114 PROPERTY_FILE_NAME)); 115 prop.load(inputStream); 116 value = prop.getProperty(key); 117 inputStream.close(); 118 119 return value; 120 } 121 122 /** 123 * The power log capture when the measuremnt start and end. It will be 124 * merged with the monsoon raw power data to get the average power usage in 125 * that particular time frame. 126 * @param logType 127 * @param testCase 128 * @param delay 129 * @throws IOException 130 */ writePowerLog(String logType, String testCase, long delay)131 protected void writePowerLog(String logType, String testCase, long delay) 132 throws IOException { 133 writePowerLog(logType, testCase, System.currentTimeMillis(), delay); 134 } 135 136 /** 137 * Power log capture the time when the measurement start and end. It will be 138 * merged with the monsoon raw power data to get the average power usage in 139 * that particular time frame. 140 * @param logType 141 * @param testCase : Test case name 142 * @param time : Specific time stamp 143 * @param delay : Delay for the actual log time. 144 * @throws IOException 145 */ writePowerLog(String logType, String testCase, long time, long delay)146 protected void writePowerLog(String logType, String testCase, long time, 147 long delay) throws IOException { 148 FileWriter outputWriter = new FileWriter(new File(POWER_OUTPUT), true); 149 outputWriter.write(String.format("%d %s %s\n", (time + delay), 150 logType, testCase)); 151 outputWriter.close(); 152 } 153 writePowerLogStart(String testCase)154 protected void writePowerLogStart(String testCase) throws IOException { 155 writePowerLog("AUTOTEST_TEST_BEGIN", testCase, 5 * 1000); 156 } 157 writePowerLogEnd(String testCase)158 protected void writePowerLogEnd(String testCase) throws IOException { 159 writePowerLog("AUTOTEST_TEST_SUCCESS", testCase, 0); 160 } 161 writePowerLogIdleStart(String testCase, long delay)162 protected void writePowerLogIdleStart(String testCase, long delay) throws IOException { 163 writePowerLog("AUTOTEST_TEST_BEGIN", testCase, delay); 164 } 165 writePowerLogIdleEnd(String testCase, long delay)166 protected void writePowerLogIdleEnd(String testCase, long delay) throws IOException { 167 writePowerLog("AUTOTEST_TEST_SUCCESS", testCase, delay); 168 } 169 } 170