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