Home | History | Annotate | Download | only in monkey
      1 /*
      2  * Copyright (C) 2010 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.commands.monkey;
     18 
     19 import java.io.FileWriter;
     20 import java.io.IOException;
     21 import java.util.ArrayList;
     22 import android.app.IActivityManager;
     23 import android.content.ContentValues;
     24 import android.util.Log;
     25 import android.view.IWindowManager;
     26 import android.os.Build;
     27 
     28 
     29 /**
     30  * Special events for power measurement.
     31  */
     32 public class MonkeyPowerEvent extends MonkeyEvent {
     33 
     34     //Parameter for the power test runner
     35     private static final String TAG = "PowerTester";
     36     private static final String LOG_FILE = "/sdcard/autotester.log";
     37     private static ArrayList<ContentValues> mLogEvents = new ArrayList<ContentValues>();
     38     private static final String TEST_SEQ_BEGIN = "AUTOTEST_SEQUENCE_BEGIN";
     39     private static final String TEST_STARTED = "AUTOTEST_TEST_BEGIN";
     40     private static final String TEST_DELAY_STARTED = "AUTOTEST_TEST_BEGIN_DELAY";
     41     private static final String TEST_ENDED = "AUTOTEST_TEST_SUCCESS";
     42     private static final String TEST_IDLE_ENDED = "AUTOTEST_IDLE_SUCCESS";
     43     private static long mTestStartTime;
     44 
     45     private String mPowerLogTag;
     46     private String mTestResult;
     47 
     48     //10 secs for the screen to trun off after the usb notification
     49     private static final long USB_DELAY_TIME = 10000;
     50 
     51     public MonkeyPowerEvent(String powerLogTag, String powerTestResult) {
     52         super(EVENT_TYPE_ACTIVITY);
     53         mPowerLogTag = powerLogTag;
     54         mTestResult = powerTestResult;
     55     }
     56 
     57     public MonkeyPowerEvent(String powerLogTag) {
     58         super(EVENT_TYPE_ACTIVITY);
     59         mPowerLogTag = powerLogTag;
     60         mTestResult = null;
     61     }
     62 
     63     public MonkeyPowerEvent() {
     64         super(EVENT_TYPE_ACTIVITY);
     65         mPowerLogTag = null;
     66         mTestResult = null;
     67     }
     68 
     69     /**
     70      * Buffer an event to be logged later.
     71      */
     72     private void bufferLogEvent(String tag, String value) {
     73         long tagTime = System.currentTimeMillis();
     74         // Record the test start time
     75         if (tag.compareTo(TEST_STARTED) == 0) {
     76             mTestStartTime = tagTime;
     77         } else if (tag.compareTo(TEST_IDLE_ENDED) == 0) {
     78             long lagTime = Long.parseLong(value);
     79             tagTime = mTestStartTime + lagTime;
     80             tag = TEST_ENDED;
     81         } else if (tag.compareTo(TEST_DELAY_STARTED) == 0) {
     82             mTestStartTime = tagTime + USB_DELAY_TIME;
     83             tagTime = mTestStartTime;
     84             tag = TEST_STARTED;
     85         }
     86 
     87         ContentValues event = new ContentValues();
     88         event.put("date", tagTime);
     89         event.put("tag", tag);
     90 
     91         if (value != null) {
     92             event.put("value", value);
     93         }
     94         mLogEvents.add(event);
     95     }
     96 
     97     /**
     98      * Write the accumulated log events to a file on the SD card.
     99      */
    100     private void writeLogEvents() {
    101         ContentValues[] events;
    102 
    103         events = mLogEvents.toArray(new ContentValues[0]);
    104         mLogEvents.clear();
    105         FileWriter writer = null;
    106         try {
    107             StringBuffer buffer = new StringBuffer();
    108             for (int i = 0; i < events.length; ++i) {
    109                 ContentValues event = events[i];
    110                 buffer.append(MonkeyUtils.toCalendarTime(event.getAsLong("date")));
    111                 buffer.append(event.getAsString("tag"));
    112                 if (event.containsKey("value")) {
    113                     String value = event.getAsString("value");
    114                     buffer.append(" ");
    115                     buffer.append(value.replace('\n', '/'));
    116                 }
    117                 buffer.append("\n");
    118             }
    119             writer = new FileWriter(LOG_FILE, true); // true = append
    120             writer.write(buffer.toString());
    121         } catch (IOException e) {
    122             Log.w(TAG, "Can't write sdcard log file", e);
    123         } finally {
    124             try {
    125                 if (writer != null) writer.close();
    126             } catch (IOException e) {
    127             }
    128         }
    129     }
    130 
    131     @Override
    132     public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) {
    133         if (mPowerLogTag != null) {
    134             if (mPowerLogTag.compareTo(TEST_SEQ_BEGIN) == 0) {
    135                 bufferLogEvent(mPowerLogTag, Build.FINGERPRINT);
    136             } else if (mTestResult != null) {
    137                 bufferLogEvent(mPowerLogTag, mTestResult);
    138             }
    139         } else {
    140             writeLogEvents();
    141         }
    142         return MonkeyEvent.INJECT_SUCCESS;
    143     }
    144 }
    145