AI 145122: am: CL 144927 Fix throttling in scripted monkeys (bug #1739843).

Original author: emillar

Automated import of CL 145122
This commit is contained in:
Evan Millar
2009-04-08 13:28:35 -07:00
committed by The Android Open Source Project
parent d4e48c3aa4
commit fb7c34758a
8 changed files with 74 additions and 22 deletions

View File

@@ -362,7 +362,7 @@ public class Monkey {
if (mScriptFileName != null) { if (mScriptFileName != null) {
// script mode, ignore other options // script mode, ignore other options
mEventSource = new MonkeySourceScript(mScriptFileName); mEventSource = new MonkeySourceScript(mScriptFileName, mThrottle);
mEventSource.setVerbose(mVerbose); mEventSource.setVerbose(mVerbose);
} else { } else {
// random source by default // random source by default

View File

@@ -50,6 +50,14 @@ public abstract class MonkeyEvent {
return eventType; return eventType;
} }
/**
* @return true if it is safe to throttle after this event, and false otherwise.
*/
public boolean isThrottlable() {
return true;
}
/** /**
* a method for injecting event * a method for injecting event
* @param iwm wires to current window manager * @param iwm wires to current window manager

View File

@@ -0,0 +1,41 @@
/*
* Copyright (C) 2008 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.util.LinkedList;
/**
* class for keeping a monkey event queue
*/
@SuppressWarnings("serial")
public class MonkeyEventQueue extends LinkedList<MonkeyEvent> {
private long mThrottle;
public MonkeyEventQueue(long throttle) {
super();
mThrottle = throttle;
}
@Override
public void addLast(MonkeyEvent e) {
super.add(e);
if (e.isThrottlable()) {
super.add(new MonkeyThrottleEvent(mThrottle));
}
}
}

View File

@@ -91,6 +91,11 @@ public class MonkeyKeyEvent extends MonkeyEvent {
mKeyCode, mRepeatCount, mMetaState, mDeviceId, mScancode); mKeyCode, mRepeatCount, mMetaState, mDeviceId, mScancode);
} }
@Override
public boolean isThrottlable() {
return (getAction() == KeyEvent.ACTION_UP);
}
@Override @Override
public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) { public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) {
if (verbose > 1) { if (verbose > 1) {

View File

@@ -20,6 +20,7 @@ import android.app.IActivityManager;
import android.os.RemoteException; import android.os.RemoteException;
import android.os.SystemClock; import android.os.SystemClock;
import android.view.IWindowManager; import android.view.IWindowManager;
import android.view.KeyEvent;
import android.view.MotionEvent; import android.view.MotionEvent;
@@ -123,6 +124,11 @@ public class MonkeyMotionEvent extends MonkeyEvent {
mXPrecision, mYPrecision, mDeviceId, mEdgeFlags); mXPrecision, mYPrecision, mDeviceId, mEdgeFlags);
} }
@Override
public boolean isThrottlable() {
return (getAction() == KeyEvent.ACTION_UP);
}
@Override @Override
public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) { public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) {

View File

@@ -168,7 +168,7 @@ public class MonkeySourceRandom implements MonkeyEventSource{
private float[] mFactors = new float[FACTORZ_COUNT]; private float[] mFactors = new float[FACTORZ_COUNT];
private ArrayList<ComponentName> mMainApps; private ArrayList<ComponentName> mMainApps;
private int mEventCount = 0; //total number of events generated so far private int mEventCount = 0; //total number of events generated so far
private LinkedList<MonkeyEvent> mQ = new LinkedList<MonkeyEvent>(); private MonkeyEventQueue mQ;
private Random mRandom; private Random mRandom;
private int mVerbose = 0; private int mVerbose = 0;
private long mThrottle = 0; private long mThrottle = 0;
@@ -203,7 +203,7 @@ public class MonkeySourceRandom implements MonkeyEventSource{
mRandom = new SecureRandom(); mRandom = new SecureRandom();
mRandom.setSeed((seed == 0) ? -1 : seed); mRandom.setSeed((seed == 0) ? -1 : seed);
mMainApps = MainApps; mMainApps = MainApps;
mThrottle = throttle; mQ = new MonkeyEventQueue(throttle);
} }
/** /**
@@ -336,7 +336,6 @@ public class MonkeySourceRandom implements MonkeyEventSource{
downAt, MotionEvent.ACTION_UP, x, y, 0); downAt, MotionEvent.ACTION_UP, x, y, 0);
e.setIntermediateNote(false); e.setIntermediateNote(false);
mQ.addLast(e); mQ.addLast(e);
addThrottle();
} }
/** /**
@@ -387,7 +386,6 @@ public class MonkeySourceRandom implements MonkeyEventSource{
e.setIntermediateNote(false); e.setIntermediateNote(false);
mQ.addLast(e); mQ.addLast(e);
} }
addThrottle();
} }
/** /**
@@ -420,13 +418,11 @@ public class MonkeySourceRandom implements MonkeyEventSource{
MonkeyActivityEvent e = new MonkeyActivityEvent(mMainApps.get( MonkeyActivityEvent e = new MonkeyActivityEvent(mMainApps.get(
mRandom.nextInt(mMainApps.size()))); mRandom.nextInt(mMainApps.size())));
mQ.addLast(e); mQ.addLast(e);
addThrottle();
return; return;
} else if (cls < mFactors[FACTOR_FLIP]) { } else if (cls < mFactors[FACTOR_FLIP]) {
MonkeyFlipEvent e = new MonkeyFlipEvent(mKeyboardOpen); MonkeyFlipEvent e = new MonkeyFlipEvent(mKeyboardOpen);
mKeyboardOpen = !mKeyboardOpen; mKeyboardOpen = !mKeyboardOpen;
mQ.addLast(e); mQ.addLast(e);
addThrottle();
return; return;
} else { } else {
lastKey = 1 + mRandom.nextInt(KeyEvent.getMaxKeyCode() - 1); lastKey = 1 + mRandom.nextInt(KeyEvent.getMaxKeyCode() - 1);
@@ -437,8 +433,6 @@ public class MonkeySourceRandom implements MonkeyEventSource{
e = new MonkeyKeyEvent(KeyEvent.ACTION_UP, lastKey); e = new MonkeyKeyEvent(KeyEvent.ACTION_UP, lastKey);
mQ.addLast(e); mQ.addLast(e);
addThrottle();
} }
public boolean validate() { public boolean validate() {
@@ -472,8 +466,4 @@ public class MonkeySourceRandom implements MonkeyEventSource{
mQ.removeFirst(); mQ.removeFirst();
return e; return e;
} }
private void addThrottle() {
mQ.addLast(new MonkeyThrottleEvent(MonkeyEvent.EVENT_TYPE_THROTTLE, mThrottle));
}
} }

View File

@@ -35,6 +35,7 @@ import java.util.StringTokenizer;
* type= raw events * type= raw events
* count= 10 * count= 10
* speed= 1.0 * speed= 1.0
* start data >>
* captureDispatchPointer(5109520,5109520,0,230.75429,458.1814,0.20784314, * captureDispatchPointer(5109520,5109520,0,230.75429,458.1814,0.20784314,
* 0.06666667,0,0.0,0.0,65539,0) * 0.06666667,0,0.0,0.0,65539,0)
* captureDispatchKey(5113146,5113146,0,20,0,0,0,0) * captureDispatchKey(5113146,5113146,0,20,0,0,0,0)
@@ -46,7 +47,7 @@ public class MonkeySourceScript implements MonkeyEventSource{
private int mVerbose = 0; private int mVerbose = 0;
private double mSpeed = 1.0; private double mSpeed = 1.0;
private String mScriptFileName; private String mScriptFileName;
private LinkedList<MonkeyEvent> mQ = new LinkedList<MonkeyEvent>(); private MonkeyEventQueue mQ;
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=";
@@ -80,12 +81,14 @@ public class MonkeySourceScript implements MonkeyEventSource{
// 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 >>";
private boolean mFileOpened = false; private boolean mFileOpened = false;
FileInputStream mFStream; FileInputStream mFStream;
DataInputStream mInputStream; DataInputStream mInputStream;
BufferedReader mBufferReader; BufferedReader mBufferReader;
public MonkeySourceScript(String filename) { public MonkeySourceScript(String filename, long throttle) {
mScriptFileName = filename; mScriptFileName = filename;
mQ = new MonkeyEventQueue(throttle);
} }
/** /**

View File

@@ -16,11 +16,10 @@
package com.android.commands.monkey; package com.android.commands.monkey;
import java.util.List;
import android.app.IActivityManager; import android.app.IActivityManager;
import android.os.RemoteException;
import android.os.SystemClock;
import android.view.IWindowManager; import android.view.IWindowManager;
import android.view.MotionEvent;
/** /**
@@ -29,8 +28,8 @@ import android.view.MotionEvent;
public class MonkeyThrottleEvent extends MonkeyEvent { public class MonkeyThrottleEvent extends MonkeyEvent {
private long mThrottle; private long mThrottle;
public MonkeyThrottleEvent(int type, long throttle) { public MonkeyThrottleEvent(long throttle) {
super(type); super(MonkeyEvent.EVENT_TYPE_THROTTLE);
mThrottle = throttle; mThrottle = throttle;
} }