Merge change 24289 into eclair
* changes: modified: MonkeySourceScript.java
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package com.android.commands.monkey;
|
package com.android.commands.monkey;
|
||||||
|
|
||||||
|
import android.content.ComponentName;
|
||||||
import android.os.SystemClock;
|
import android.os.SystemClock;
|
||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
|
|
||||||
@@ -28,6 +29,7 @@ import java.io.InputStreamReader;
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.StringTokenizer;
|
import java.util.StringTokenizer;
|
||||||
|
|
||||||
|
import android.view.KeyEvent;
|
||||||
/**
|
/**
|
||||||
* monkey event queue. It takes a script to produce events
|
* monkey event queue. It takes a script to produce events
|
||||||
*
|
*
|
||||||
@@ -52,6 +54,8 @@ public class MonkeySourceScript implements MonkeyEventSource {
|
|||||||
private static final String HEADER_TYPE = "type=";
|
private static final String HEADER_TYPE = "type=";
|
||||||
private static final String HEADER_COUNT = "count=";
|
private static final String HEADER_COUNT = "count=";
|
||||||
private static final String HEADER_SPEED = "speed=";
|
private static final String HEADER_SPEED = "speed=";
|
||||||
|
// New script type
|
||||||
|
private static final String USER_EVENT_TYPE = "user";
|
||||||
|
|
||||||
private long mLastRecordedDownTimeKey = 0;
|
private long mLastRecordedDownTimeKey = 0;
|
||||||
private long mLastRecordedDownTimeMotion = 0;
|
private long mLastRecordedDownTimeMotion = 0;
|
||||||
@@ -59,6 +63,7 @@ public class MonkeySourceScript implements MonkeyEventSource {
|
|||||||
private long mLastExportDownTimeMotion = 0;
|
private long mLastExportDownTimeMotion = 0;
|
||||||
private long mLastExportEventTime = -1;
|
private long mLastExportEventTime = -1;
|
||||||
private long mLastRecordedEventTime = -1;
|
private long mLastRecordedEventTime = -1;
|
||||||
|
private String mScriptType = USER_EVENT_TYPE;
|
||||||
|
|
||||||
private static final boolean THIS_DEBUG = false;
|
private static final boolean THIS_DEBUG = false;
|
||||||
// a parameter that compensates the difference of real elapsed time and
|
// a parameter that compensates the difference of real elapsed time and
|
||||||
@@ -77,6 +82,9 @@ public class MonkeySourceScript implements MonkeyEventSource {
|
|||||||
private static final String EVENT_KEYWORD_TRACKBALL = "DispatchTrackball";
|
private static final String EVENT_KEYWORD_TRACKBALL = "DispatchTrackball";
|
||||||
private static final String EVENT_KEYWORD_KEY = "DispatchKey";
|
private static final String EVENT_KEYWORD_KEY = "DispatchKey";
|
||||||
private static final String EVENT_KEYWORD_FLIP = "DispatchFlip";
|
private static final String EVENT_KEYWORD_FLIP = "DispatchFlip";
|
||||||
|
private static final String EVENT_KEYWORD_KEYPRESS = "DispatchPress";
|
||||||
|
private static final String EVENT_KEYWORD_ACTIVITY = "LaunchActivity";
|
||||||
|
private static final String EVENT_KEYWORD_WAIT = "UserWait";
|
||||||
|
|
||||||
// a line at the end of the header
|
// a line at the end of the header
|
||||||
private static final String STARTING_DATA_LINE = "start data >>";
|
private static final String STARTING_DATA_LINE = "start data >>";
|
||||||
@@ -125,7 +133,7 @@ public class MonkeySourceScript implements MonkeyEventSource {
|
|||||||
while ((sLine = mBufferReader.readLine()) != null) {
|
while ((sLine = mBufferReader.readLine()) != null) {
|
||||||
sLine = sLine.trim();
|
sLine = sLine.trim();
|
||||||
if (sLine.indexOf(HEADER_TYPE) >= 0) {
|
if (sLine.indexOf(HEADER_TYPE) >= 0) {
|
||||||
// at this point, we only have one type of script
|
mScriptType = sLine.substring(HEADER_TYPE.length() + 1).trim();
|
||||||
} else if (sLine.indexOf(HEADER_COUNT) >= 0) {
|
} else if (sLine.indexOf(HEADER_COUNT) >= 0) {
|
||||||
try {
|
try {
|
||||||
mEventCountInScript = Integer.parseInt(sLine.substring(
|
mEventCountInScript = Integer.parseInt(sLine.substring(
|
||||||
@@ -162,20 +170,11 @@ public class MonkeySourceScript implements MonkeyEventSource {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processLine(String s) {
|
private void handleRawEvent(String s, StringTokenizer st) {
|
||||||
int index1 = s.indexOf('(');
|
|
||||||
int index2 = s.indexOf(')');
|
|
||||||
|
|
||||||
if (index1 < 0 || index2 < 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
StringTokenizer st = new StringTokenizer(
|
|
||||||
s.substring(index1 + 1, index2), ",");
|
|
||||||
|
|
||||||
if (s.indexOf(EVENT_KEYWORD_KEY) >= 0) {
|
if (s.indexOf(EVENT_KEYWORD_KEY) >= 0) {
|
||||||
// key events
|
// key events
|
||||||
try {
|
try {
|
||||||
|
System.out.println(" old key\n");
|
||||||
long downTime = Long.parseLong(st.nextToken());
|
long downTime = Long.parseLong(st.nextToken());
|
||||||
long eventTime = Long.parseLong(st.nextToken());
|
long eventTime = Long.parseLong(st.nextToken());
|
||||||
int action = Integer.parseInt(st.nextToken());
|
int action = Integer.parseInt(st.nextToken());
|
||||||
@@ -185,9 +184,13 @@ public class MonkeySourceScript implements MonkeyEventSource {
|
|||||||
int device = Integer.parseInt(st.nextToken());
|
int device = Integer.parseInt(st.nextToken());
|
||||||
int scancode = Integer.parseInt(st.nextToken());
|
int scancode = Integer.parseInt(st.nextToken());
|
||||||
|
|
||||||
MonkeyKeyEvent e = new MonkeyKeyEvent(downTime, eventTime,
|
MonkeyKeyEvent e =
|
||||||
action, code, repeat, metaState, device, scancode);
|
new MonkeyKeyEvent(downTime, eventTime, action, code, repeat, metaState,
|
||||||
|
device, scancode);
|
||||||
|
System.out.println(" Key code " + code + "\n");
|
||||||
|
|
||||||
mQ.addLast(e);
|
mQ.addLast(e);
|
||||||
|
System.out.println("Added key up \n");
|
||||||
|
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
// something wrong with this line in the script
|
// something wrong with this line in the script
|
||||||
@@ -213,9 +216,9 @@ public class MonkeySourceScript implements MonkeyEventSource {
|
|||||||
if (s.indexOf("Pointer") > 0) {
|
if (s.indexOf("Pointer") > 0) {
|
||||||
type = MonkeyEvent.EVENT_TYPE_POINTER;
|
type = MonkeyEvent.EVENT_TYPE_POINTER;
|
||||||
}
|
}
|
||||||
MonkeyMotionEvent e = new MonkeyMotionEvent(type, downTime, eventTime,
|
MonkeyMotionEvent e =
|
||||||
action, x, y, pressure, size, metaState, xPrecision, yPrecision,
|
new MonkeyMotionEvent(type, downTime, eventTime, action, x, y, pressure,
|
||||||
device, edgeFlags);
|
size, metaState, xPrecision, yPrecision, device, edgeFlags);
|
||||||
mQ.addLast(e);
|
mQ.addLast(e);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
// we ignore this event
|
// we ignore this event
|
||||||
@@ -225,6 +228,49 @@ public class MonkeySourceScript implements MonkeyEventSource {
|
|||||||
MonkeyFlipEvent e = new MonkeyFlipEvent(keyboardOpen);
|
MonkeyFlipEvent e = new MonkeyFlipEvent(keyboardOpen);
|
||||||
mQ.addLast(e);
|
mQ.addLast(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleUserEvent(String s, StringTokenizer st) {
|
||||||
|
if (s.indexOf(EVENT_KEYWORD_ACTIVITY) >= 0) {
|
||||||
|
String pkg_name = st.nextToken();
|
||||||
|
String cl_name = st.nextToken();
|
||||||
|
ComponentName mApp = new ComponentName(pkg_name, cl_name);
|
||||||
|
MonkeyActivityEvent e = new MonkeyActivityEvent(mApp);
|
||||||
|
mQ.addLast(e);
|
||||||
|
|
||||||
|
} else if (s.indexOf(EVENT_KEYWORD_WAIT) >= 0) {
|
||||||
|
long sleeptime = Integer.parseInt(st.nextToken());
|
||||||
|
MonkeyWaitEvent e = new MonkeyWaitEvent(sleeptime);
|
||||||
|
mQ.addLast(e);
|
||||||
|
|
||||||
|
} else if (s.indexOf(EVENT_KEYWORD_KEYPRESS) >= 0) {
|
||||||
|
String key_name = st.nextToken();
|
||||||
|
int keyCode = MonkeySourceRandom.getKeyCode(key_name);
|
||||||
|
MonkeyKeyEvent e = new MonkeyKeyEvent(KeyEvent.ACTION_DOWN, keyCode);
|
||||||
|
mQ.addLast(e);
|
||||||
|
e = new MonkeyKeyEvent(KeyEvent.ACTION_UP, keyCode);
|
||||||
|
mQ.addLast(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processLine(String s) {
|
||||||
|
int index1 = s.indexOf('(');
|
||||||
|
int index2 = s.indexOf(')');
|
||||||
|
|
||||||
|
if (index1 < 0 || index2 < 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringTokenizer st = new StringTokenizer(
|
||||||
|
s.substring(index1 + 1, index2), ",");
|
||||||
|
if (mScriptType.compareTo(USER_EVENT_TYPE) == 0) {
|
||||||
|
// User event type
|
||||||
|
handleUserEvent(s, st);
|
||||||
|
} else {
|
||||||
|
// Raw type
|
||||||
|
handleRawEvent(s,st);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void closeFile() {
|
private void closeFile() {
|
||||||
@@ -257,6 +303,10 @@ public class MonkeySourceScript implements MonkeyEventSource {
|
|||||||
* @return true if success
|
* @return true if success
|
||||||
*/
|
*/
|
||||||
private boolean readNextBatch() {
|
private boolean readNextBatch() {
|
||||||
|
/*
|
||||||
|
* The script should restore the original state when it run multiple
|
||||||
|
* times.
|
||||||
|
*/
|
||||||
String sLine = null;
|
String sLine = null;
|
||||||
int readCount = 0;
|
int readCount = 0;
|
||||||
|
|
||||||
@@ -270,14 +320,6 @@ public class MonkeySourceScript implements MonkeyEventSource {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
resetValue();
|
resetValue();
|
||||||
|
|
||||||
/*
|
|
||||||
* In order to allow the Monkey to replay captured events multiple times
|
|
||||||
* we need to define a default start UI, which is the home screen
|
|
||||||
* Otherwise, it won't be accurate since the captured events
|
|
||||||
* could end anywhere
|
|
||||||
*/
|
|
||||||
addHomeKeyEvent();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -418,7 +460,6 @@ public class MonkeySourceScript implements MonkeyEventSource {
|
|||||||
}
|
}
|
||||||
MonkeyEvent e = mQ.getFirst();
|
MonkeyEvent e = mQ.getFirst();
|
||||||
mQ.removeFirst();
|
mQ.removeFirst();
|
||||||
|
|
||||||
if (e.getEventType() == MonkeyEvent.EVENT_TYPE_KEY) {
|
if (e.getEventType() == MonkeyEvent.EVENT_TYPE_KEY) {
|
||||||
adjustKeyEventTime((MonkeyKeyEvent) e);
|
adjustKeyEventTime((MonkeyKeyEvent) e);
|
||||||
} else if (e.getEventType() == MonkeyEvent.EVENT_TYPE_POINTER ||
|
} else if (e.getEventType() == MonkeyEvent.EVENT_TYPE_POINTER ||
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2009 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.view.IWindowManager;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* monkey throttle event
|
||||||
|
*/
|
||||||
|
public class MonkeyWaitEvent extends MonkeyEvent {
|
||||||
|
private long mWaitTime;
|
||||||
|
|
||||||
|
public MonkeyWaitEvent(long waitTime) {
|
||||||
|
super(MonkeyEvent.EVENT_TYPE_THROTTLE);
|
||||||
|
mWaitTime = waitTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) {
|
||||||
|
if (verbose > 1) {
|
||||||
|
System.out.println("Wait Event for " + mWaitTime + " milliseconds");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Thread.sleep(mWaitTime);
|
||||||
|
} catch (InterruptedException e1) {
|
||||||
|
System.out.println("** Monkey interrupted in sleep.");
|
||||||
|
return MonkeyEvent.INJECT_FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return MonkeyEvent.INJECT_SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user