Merge "Added the instrumentation and power log monkey events fro the new power framework." into froyo

This commit is contained in:
Yu Shan Emily Lau
2010-05-28 15:38:48 -07:00
committed by Android (Google) Code Review
4 changed files with 232 additions and 6 deletions

View File

@@ -0,0 +1,56 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.commands.monkey;
import android.app.IActivityManager;
import android.content.ComponentName;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.AndroidException;
import android.view.IWindowManager;
/**
* monkey instrumentation event
*/
public class MonkeyInstrumentationEvent extends MonkeyEvent {
String mRunnerName;
String mTestCaseName;
public MonkeyInstrumentationEvent(String testCaseName, String runnerName) {
super(EVENT_TYPE_ACTIVITY);
mTestCaseName = testCaseName;
mRunnerName = runnerName;
}
@Override
public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) {
ComponentName cn = ComponentName.unflattenFromString(mRunnerName);
if (cn == null || mTestCaseName == null)
throw new IllegalArgumentException("Bad component name");
Bundle args = new Bundle();
args.putString("class", mTestCaseName);
try {
iam.startInstrumentation(cn, null, 0, args, null);
} catch (RemoteException e) {
System.err.println("** Failed talking with activity manager!");
return MonkeyEvent.INJECT_ERROR_REMOTE_EXCEPTION;
}
return MonkeyEvent.INJECT_SUCCESS;
}
}

View File

@@ -0,0 +1,136 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.commands.monkey;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import android.app.IActivityManager;
import android.content.ContentValues;
import android.util.Log;
import android.view.IWindowManager;
import android.os.Build;
/**
* Special events for power measurement.
*/
public class MonkeyPowerEvent extends MonkeyEvent {
//Parameter for the power test runner
private static final String TAG = "PowerTester";
private static final String LOG_FILE = "/sdcard/autotester.log";
private static ArrayList<ContentValues> mLogEvents = new ArrayList<ContentValues>();
private static final String TEST_SEQ_BEGIN = "AUTOTEST_SEQUENCE_BEGIN";
private static final String TEST_STARTED = "AUTOTEST_TEST_BEGIN";
private static final String TEST_ENDED = "AUTOTEST_TEST_SUCCESS";
private static final String TEST_IDLE_ENDED = "AUTOTEST_IDLE_SUCCESS";
private static long mTestStartTime;
private String mPowerLogTag;
private String mTestResult;
public MonkeyPowerEvent(String powerLogTag, String powerTestResult) {
super(EVENT_TYPE_ACTIVITY);
mPowerLogTag = powerLogTag;
mTestResult = powerTestResult;
}
public MonkeyPowerEvent(String powerLogTag) {
super(EVENT_TYPE_ACTIVITY);
mPowerLogTag = powerLogTag;
mTestResult = null;
}
public MonkeyPowerEvent() {
super(EVENT_TYPE_ACTIVITY);
mPowerLogTag = null;
mTestResult = null;
}
/**
* Buffer an event to be logged later.
*/
private void bufferLogEvent(String tag, String value) {
long tagTime = System.currentTimeMillis();
// Record the test start time
if (tag.compareTo(TEST_STARTED) == 0) {
mTestStartTime = tagTime;
} else if (tag.compareTo(TEST_IDLE_ENDED) == 0) {
long lagTime = Long.parseLong(value);
tagTime = mTestStartTime + lagTime;
tag = TEST_ENDED;
}
ContentValues event = new ContentValues();
event.put("date", tagTime);
event.put("tag", tag);
if (value != null) {
event.put("value", value);
}
mLogEvents.add(event);
}
/**
* Write the accumulated log events to a file on the SD card.
*/
private void writeLogEvents() {
ContentValues[] events;
events = mLogEvents.toArray(new ContentValues[0]);
mLogEvents.clear();
FileWriter writer = null;
try {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < events.length; ++i) {
ContentValues event = events[i];
buffer.append(MonkeyUtils.toCalendarTime(event.getAsLong("date")));
buffer.append(event.getAsString("tag"));
if (event.containsKey("value")) {
String value = event.getAsString("value");
buffer.append(" ");
buffer.append(value.replace('\n', '/'));
}
buffer.append("\n");
}
writer = new FileWriter(LOG_FILE, true); // true = append
writer.write(buffer.toString());
} catch (IOException e) {
Log.w(TAG, "Can't write sdcard log file", e);
} finally {
try {
if (writer != null) writer.close();
} catch (IOException e) {
}
}
}
@Override
public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) {
if (mPowerLogTag != null) {
if (mPowerLogTag.compareTo(TEST_SEQ_BEGIN) == 0) {
bufferLogEvent(mPowerLogTag, Build.FINGERPRINT);
} else if (mTestResult != null) {
bufferLogEvent(mPowerLogTag, mTestResult);
}
} else {
writeLogEvents();
}
return MonkeyEvent.INJECT_SUCCESS;
}
}

View File

@@ -91,10 +91,16 @@ public class MonkeySourceScript implements MonkeyEventSource {
private static final String EVENT_KEYWORD_ACTIVITY = "LaunchActivity";
private static final String EVENT_KEYWORD_INSTRUMENTATION = "LaunchInstrumentation";
private static final String EVENT_KEYWORD_WAIT = "UserWait";
private static final String EVENT_KEYWORD_LONGPRESS = "LongPress";
private static final String EVENT_KEYWORD_POWERLOG = "PowerLog";
private static final String EVENT_KEYWORD_WRITEPOWERLOG = "WriteLog";
// a line at the end of the header
private static final String STARTING_DATA_LINE = "start data >>";
@@ -102,8 +108,6 @@ public class MonkeySourceScript implements MonkeyEventSource {
private static int LONGPRESS_WAIT_TIME = 2000; // wait time for the long
// press
FileInputStream mFStream;
DataInputStream mInputStream;
@@ -195,6 +199,9 @@ public class MonkeySourceScript implements MonkeyEventSource {
return MAX_ONE_TIME_READS;
}
/**
* Creates an event and adds it to the event queue. If the parameters are
* not understood, they are ignored and no events are added.
@@ -273,6 +280,15 @@ public class MonkeySourceScript implements MonkeyEventSource {
return;
}
// Handle launch instrumentation events
if (s.indexOf(EVENT_KEYWORD_INSTRUMENTATION) >= 0 && args.length == 2) {
String test_name = args[0];
String runner_name = args[1];
MonkeyInstrumentationEvent e = new MonkeyInstrumentationEvent(test_name, runner_name);
mQ.addLast(e);
return;
}
// Handle wait events
if (s.indexOf(EVENT_KEYWORD_WAIT) >= 0 && args.length == 1) {
try {
@@ -305,6 +321,27 @@ public class MonkeySourceScript implements MonkeyEventSource {
e = new MonkeyKeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER);
mQ.addLast(e);
}
//The power log event is mainly for the automated power framework
if (s.indexOf(EVENT_KEYWORD_POWERLOG) >= 0 && args.length > 0) {
String power_log_type = args[0];
String test_case_status;
if (args.length == 1){
MonkeyPowerEvent e = new MonkeyPowerEvent(power_log_type);
mQ.addLast(e);
} else if (args.length == 2){
test_case_status = args[1];
MonkeyPowerEvent e = new MonkeyPowerEvent(power_log_type, test_case_status);
mQ.addLast(e);
}
}
//Write power log to sdcard
if (s.indexOf(EVENT_KEYWORD_WRITEPOWERLOG) >= 0) {
MonkeyPowerEvent e = new MonkeyPowerEvent();
mQ.addLast(e);
}
}
/**

View File

@@ -16,10 +16,7 @@
package com.android.commands.monkey;
import java.io.BufferedReader;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Misc utilities.
@@ -28,7 +25,7 @@ public abstract class MonkeyUtils {
private static final java.util.Date DATE = new java.util.Date();
private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat(
"yyyy/MM/dd HH:mm:ss.SSS");
"yyyy-MM-dd HH:mm:ss.SSS ");
private MonkeyUtils() {
}