Skip to content

Commit 39d7a74

Browse files
Yu Shan Emily LauAndroid (Google) Code Review
authored andcommitted
Merge "Added the instrumentation and power log monkey events fro the new power framework." into froyo
2 parents 99cb1d1 + 2952b49 commit 39d7a74

File tree

4 files changed

+232
-6
lines changed

4 files changed

+232
-6
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
20+
import android.app.IActivityManager;
21+
import android.content.ComponentName;
22+
import android.os.Bundle;
23+
import android.os.RemoteException;
24+
import android.util.AndroidException;
25+
import android.view.IWindowManager;
26+
27+
/**
28+
* monkey instrumentation event
29+
*/
30+
public class MonkeyInstrumentationEvent extends MonkeyEvent {
31+
String mRunnerName;
32+
String mTestCaseName;
33+
34+
public MonkeyInstrumentationEvent(String testCaseName, String runnerName) {
35+
super(EVENT_TYPE_ACTIVITY);
36+
mTestCaseName = testCaseName;
37+
mRunnerName = runnerName;
38+
}
39+
40+
@Override
41+
public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) {
42+
ComponentName cn = ComponentName.unflattenFromString(mRunnerName);
43+
if (cn == null || mTestCaseName == null)
44+
throw new IllegalArgumentException("Bad component name");
45+
46+
Bundle args = new Bundle();
47+
args.putString("class", mTestCaseName);
48+
try {
49+
iam.startInstrumentation(cn, null, 0, args, null);
50+
} catch (RemoteException e) {
51+
System.err.println("** Failed talking with activity manager!");
52+
return MonkeyEvent.INJECT_ERROR_REMOTE_EXCEPTION;
53+
}
54+
return MonkeyEvent.INJECT_SUCCESS;
55+
}
56+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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_ENDED = "AUTOTEST_TEST_SUCCESS";
41+
private static final String TEST_IDLE_ENDED = "AUTOTEST_IDLE_SUCCESS";
42+
private static long mTestStartTime;
43+
44+
private String mPowerLogTag;
45+
private String mTestResult;
46+
47+
public MonkeyPowerEvent(String powerLogTag, String powerTestResult) {
48+
super(EVENT_TYPE_ACTIVITY);
49+
mPowerLogTag = powerLogTag;
50+
mTestResult = powerTestResult;
51+
}
52+
53+
public MonkeyPowerEvent(String powerLogTag) {
54+
super(EVENT_TYPE_ACTIVITY);
55+
mPowerLogTag = powerLogTag;
56+
mTestResult = null;
57+
}
58+
59+
public MonkeyPowerEvent() {
60+
super(EVENT_TYPE_ACTIVITY);
61+
mPowerLogTag = null;
62+
mTestResult = null;
63+
}
64+
65+
/**
66+
* Buffer an event to be logged later.
67+
*/
68+
private void bufferLogEvent(String tag, String value) {
69+
long tagTime = System.currentTimeMillis();
70+
// Record the test start time
71+
if (tag.compareTo(TEST_STARTED) == 0) {
72+
mTestStartTime = tagTime;
73+
} else if (tag.compareTo(TEST_IDLE_ENDED) == 0) {
74+
long lagTime = Long.parseLong(value);
75+
tagTime = mTestStartTime + lagTime;
76+
tag = TEST_ENDED;
77+
}
78+
79+
ContentValues event = new ContentValues();
80+
event.put("date", tagTime);
81+
event.put("tag", tag);
82+
83+
if (value != null) {
84+
event.put("value", value);
85+
}
86+
mLogEvents.add(event);
87+
}
88+
89+
/**
90+
* Write the accumulated log events to a file on the SD card.
91+
*/
92+
private void writeLogEvents() {
93+
ContentValues[] events;
94+
95+
events = mLogEvents.toArray(new ContentValues[0]);
96+
mLogEvents.clear();
97+
FileWriter writer = null;
98+
try {
99+
StringBuffer buffer = new StringBuffer();
100+
for (int i = 0; i < events.length; ++i) {
101+
ContentValues event = events[i];
102+
buffer.append(MonkeyUtils.toCalendarTime(event.getAsLong("date")));
103+
buffer.append(event.getAsString("tag"));
104+
if (event.containsKey("value")) {
105+
String value = event.getAsString("value");
106+
buffer.append(" ");
107+
buffer.append(value.replace('\n', '/'));
108+
}
109+
buffer.append("\n");
110+
}
111+
writer = new FileWriter(LOG_FILE, true); // true = append
112+
writer.write(buffer.toString());
113+
} catch (IOException e) {
114+
Log.w(TAG, "Can't write sdcard log file", e);
115+
} finally {
116+
try {
117+
if (writer != null) writer.close();
118+
} catch (IOException e) {
119+
}
120+
}
121+
}
122+
123+
@Override
124+
public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) {
125+
if (mPowerLogTag != null) {
126+
if (mPowerLogTag.compareTo(TEST_SEQ_BEGIN) == 0) {
127+
bufferLogEvent(mPowerLogTag, Build.FINGERPRINT);
128+
} else if (mTestResult != null) {
129+
bufferLogEvent(mPowerLogTag, mTestResult);
130+
}
131+
} else {
132+
writeLogEvents();
133+
}
134+
return MonkeyEvent.INJECT_SUCCESS;
135+
}
136+
}

cmds/monkey/src/com/android/commands/monkey/MonkeySourceScript.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,23 @@ public class MonkeySourceScript implements MonkeyEventSource {
9191

9292
private static final String EVENT_KEYWORD_ACTIVITY = "LaunchActivity";
9393

94+
private static final String EVENT_KEYWORD_INSTRUMENTATION = "LaunchInstrumentation";
95+
9496
private static final String EVENT_KEYWORD_WAIT = "UserWait";
9597

9698
private static final String EVENT_KEYWORD_LONGPRESS = "LongPress";
9799

100+
private static final String EVENT_KEYWORD_POWERLOG = "PowerLog";
101+
102+
private static final String EVENT_KEYWORD_WRITEPOWERLOG = "WriteLog";
103+
98104
// a line at the end of the header
99105
private static final String STARTING_DATA_LINE = "start data >>";
100106

101107
private boolean mFileOpened = false;
102108

103109
private static int LONGPRESS_WAIT_TIME = 2000; // wait time for the long
104110

105-
// press
106-
107111
FileInputStream mFStream;
108112

109113
DataInputStream mInputStream;
@@ -195,6 +199,9 @@ private int readLines() throws IOException {
195199
return MAX_ONE_TIME_READS;
196200
}
197201

202+
203+
204+
198205
/**
199206
* Creates an event and adds it to the event queue. If the parameters are
200207
* not understood, they are ignored and no events are added.
@@ -273,6 +280,15 @@ private void handleEvent(String s, String[] args) {
273280
return;
274281
}
275282

283+
// Handle launch instrumentation events
284+
if (s.indexOf(EVENT_KEYWORD_INSTRUMENTATION) >= 0 && args.length == 2) {
285+
String test_name = args[0];
286+
String runner_name = args[1];
287+
MonkeyInstrumentationEvent e = new MonkeyInstrumentationEvent(test_name, runner_name);
288+
mQ.addLast(e);
289+
return;
290+
}
291+
276292
// Handle wait events
277293
if (s.indexOf(EVENT_KEYWORD_WAIT) >= 0 && args.length == 1) {
278294
try {
@@ -305,6 +321,27 @@ private void handleEvent(String s, String[] args) {
305321
e = new MonkeyKeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER);
306322
mQ.addLast(e);
307323
}
324+
325+
//The power log event is mainly for the automated power framework
326+
if (s.indexOf(EVENT_KEYWORD_POWERLOG) >= 0 && args.length > 0) {
327+
String power_log_type = args[0];
328+
String test_case_status;
329+
330+
if (args.length == 1){
331+
MonkeyPowerEvent e = new MonkeyPowerEvent(power_log_type);
332+
mQ.addLast(e);
333+
} else if (args.length == 2){
334+
test_case_status = args[1];
335+
MonkeyPowerEvent e = new MonkeyPowerEvent(power_log_type, test_case_status);
336+
mQ.addLast(e);
337+
}
338+
}
339+
340+
//Write power log to sdcard
341+
if (s.indexOf(EVENT_KEYWORD_WRITEPOWERLOG) >= 0) {
342+
MonkeyPowerEvent e = new MonkeyPowerEvent();
343+
mQ.addLast(e);
344+
}
308345
}
309346

310347
/**

cmds/monkey/src/com/android/commands/monkey/MonkeyUtils.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616

1717
package com.android.commands.monkey;
1818

19-
import java.io.BufferedReader;
20-
import java.io.FileReader;
2119
import java.text.SimpleDateFormat;
22-
import java.util.Date;
2320

2421
/**
2522
* Misc utilities.
@@ -28,7 +25,7 @@ public abstract class MonkeyUtils {
2825

2926
private static final java.util.Date DATE = new java.util.Date();
3027
private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat(
31-
"yyyy/MM/dd HH:mm:ss.SSS");
28+
"yyyy-MM-dd HH:mm:ss.SSS ");
3229

3330
private MonkeyUtils() {
3431
}

0 commit comments

Comments
 (0)