diff --git a/cmds/monkey/src/com/android/commands/monkey/Monkey.java b/cmds/monkey/src/com/android/commands/monkey/Monkey.java index 27004e111..b1c75f13e 100644 --- a/cmds/monkey/src/com/android/commands/monkey/Monkey.java +++ b/cmds/monkey/src/com/android/commands/monkey/Monkey.java @@ -39,10 +39,12 @@ import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.security.SecureRandom; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Random; /** * Application that injects random key events and other actions into the system. @@ -140,12 +142,18 @@ public class Monkey { /** The delay between event inputs **/ long mThrottle = 0; + /** Whether to randomize each throttle (0-mThrottle ms) inserted between events. */ + boolean mRandomizeThrottle = false; + /** The number of iterations **/ int mCount = 1000; /** The random number seed **/ long mSeed = 0; + /** The random number generator **/ + Random mRandom = null; + /** Dropped-event statistics **/ long mDroppedKeyEvents = 0; @@ -421,19 +429,24 @@ public class Monkey { return -4; } + mRandom = new SecureRandom(); + mRandom.setSeed((mSeed == 0) ? -1 : mSeed); + if (mScriptFileNames != null && mScriptFileNames.size() == 1) { // script mode, ignore other options - mEventSource = new MonkeySourceScript(mScriptFileNames.get(0), mThrottle); + mEventSource = new MonkeySourceScript(mRandom, mScriptFileNames.get(0), mThrottle, + mRandomizeThrottle); mEventSource.setVerbose(mVerbose); mCountEvents = false; } else if (mScriptFileNames != null && mScriptFileNames.size() > 1) { if (mSetupFileName != null) { mEventSource = new MonkeySourceRandomScript(mSetupFileName, mScriptFileNames, - mThrottle, mSeed); + mThrottle, mRandomizeThrottle, mRandom); mCount++; } else { - mEventSource = new MonkeySourceRandomScript(mScriptFileNames, mThrottle, mSeed); + mEventSource = new MonkeySourceRandomScript(mScriptFileNames, mThrottle, + mRandomizeThrottle, mRandom); } mEventSource.setVerbose(mVerbose); mCountEvents = false; @@ -450,7 +463,7 @@ public class Monkey { if (mVerbose >= 2) { // check seeding performance System.out.println("// Seeded: " + mSeed); } - mEventSource = new MonkeySourceRandom(mSeed, mMainApps, mThrottle); + mEventSource = new MonkeySourceRandom(mRandom, mMainApps, mThrottle, mRandomizeThrottle); mEventSource.setVerbose(mVerbose); // set any of the factors that has been set for (int i = 0; i < MonkeySourceRandom.FACTORZ_COUNT; i++) { @@ -599,6 +612,8 @@ public class Monkey { mPkgWhitelistFile = nextOptionData(); } else if (opt.equals("--throttle")) { mThrottle = nextOptionLong("delay (in milliseconds) to wait between events"); + } else if (opt.equals("--randomize-throttle")) { + mRandomizeThrottle = true; } else if (opt.equals("--wait-dbg")) { // do nothing - it's caught at the very start of run() } else if (opt.equals("--dbg-no-events")) { @@ -1068,7 +1083,8 @@ public class Monkey { usage.append(" [--wait-dbg] [--dbg-no-events]\n"); usage.append(" [--setup scriptfile] [-f scriptfile [-f scriptfile] ...]\n"); usage.append(" [--port port]\n"); - usage.append(" [-s SEED] [-v [-v] ...] [--throttle MILLISEC]\n"); + usage.append(" [-s SEED] [-v [-v] ...]\n"); + usage.append(" [--throttle MILLISEC] [--randomize-throttle]\n"); usage.append(" COUNT"); System.err.println(usage.toString()); } diff --git a/cmds/monkey/src/com/android/commands/monkey/MonkeyEventQueue.java b/cmds/monkey/src/com/android/commands/monkey/MonkeyEventQueue.java index dfe389a7a..b57b540ec 100644 --- a/cmds/monkey/src/com/android/commands/monkey/MonkeyEventQueue.java +++ b/cmds/monkey/src/com/android/commands/monkey/MonkeyEventQueue.java @@ -17,24 +17,38 @@ package com.android.commands.monkey; import java.util.LinkedList; +import java.util.Random; /** * class for keeping a monkey event queue */ @SuppressWarnings("serial") -public class MonkeyEventQueue extends LinkedList { +public class MonkeyEventQueue extends LinkedList { + private Random mRandom; private long mThrottle; - - public MonkeyEventQueue(long throttle) { + private boolean mRandomizeThrottle; + + public MonkeyEventQueue(Random random, long throttle, boolean randomizeThrottle) { super(); + mRandom = random; mThrottle = throttle; + mRandomizeThrottle = randomizeThrottle; } - + @Override public void addLast(MonkeyEvent e) { super.add(e); if (e.isThrottlable()) { + long throttle = mThrottle; + if (mRandomizeThrottle && (mThrottle > 0)) { + throttle = mRandom.nextLong(); + if (throttle < 0) { + throttle = -throttle; + } + throttle %= mThrottle; + ++throttle; + } super.add(new MonkeyThrottleEvent(mThrottle)); } } diff --git a/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandom.java b/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandom.java index 5c7fdbc0e..6fa029e22 100644 --- a/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandom.java +++ b/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandom.java @@ -23,9 +23,7 @@ import android.view.KeyEvent; import android.view.MotionEvent; import android.view.WindowManagerImpl; -import java.security.SecureRandom; import java.util.ArrayList; -import java.util.LinkedList; import java.util.Random; /** @@ -202,7 +200,8 @@ public class MonkeySourceRandom implements MonkeyEventSource { return -1; } - public MonkeySourceRandom(long seed, ArrayList MainApps, long throttle) { + public MonkeySourceRandom(Random random, ArrayList MainApps, + long throttle, boolean randomizeThrottle) { // default values for random distributions // note, these are straight percentages, to match user input (cmd line args) // but they will be converted to 0..1 values before the main loop runs. @@ -216,10 +215,9 @@ public class MonkeySourceRandom implements MonkeyEventSource { mFactors[FACTOR_FLIP] = 1.0f; mFactors[FACTOR_ANYTHING] = 15.0f; - mRandom = new SecureRandom(); - mRandom.setSeed((seed == 0) ? -1 : seed); + mRandom = random; mMainApps = MainApps; - mQ = new MonkeyEventQueue(throttle); + mQ = new MonkeyEventQueue(random, throttle, randomizeThrottle); } /** diff --git a/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandomScript.java b/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandomScript.java index a937398b2..fb60c9372 100644 --- a/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandomScript.java +++ b/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandomScript.java @@ -16,8 +16,8 @@ package com.android.commands.monkey; -import java.security.SecureRandom; import java.util.ArrayList; +import java.util.Random; /** * Class for generating MonkeyEvents from multiple scripts. @@ -36,7 +36,7 @@ public class MonkeySourceRandomScript implements MonkeyEventSource { private MonkeySourceScript mCurrentSource = null; /** The random number generator */ - private SecureRandom mRandom; + private Random mRandom; /** * Creates a MonkeySourceRandomScript instance with an additional setup script. @@ -44,21 +44,23 @@ public class MonkeySourceRandomScript implements MonkeyEventSource { * @param setupFileName The name of the setup script file on the device. * @param scriptFileNames An ArrayList of the names of the script files to be run randomly. * @param throttle The amount of time to sleep in ms between events. - * @param seed The seed of the random number generator. + * @param randomizeThrottle Whether to randomize throttle. + * @param random The random number generator. */ public MonkeySourceRandomScript(String setupFileName, ArrayList scriptFileNames, - long throttle, long seed) { + long throttle, boolean randomizeThrottle, Random random) { if (setupFileName != null) { - mSetupSource = new MonkeySourceScript(setupFileName, throttle); + mSetupSource = new MonkeySourceScript(random, setupFileName, throttle, + randomizeThrottle); mCurrentSource = mSetupSource; } for (String fileName: scriptFileNames) { - mScriptSources.add(new MonkeySourceScript(fileName, throttle)); + mScriptSources.add(new MonkeySourceScript(random, fileName, throttle, + randomizeThrottle)); } - mRandom = new SecureRandom(); - mRandom.setSeed((seed == 0) ? -1 : seed); + mRandom = random; } /** @@ -66,10 +68,12 @@ public class MonkeySourceRandomScript implements MonkeyEventSource { * * @param scriptFileNames An ArrayList of the names of the script files to be run randomly. * @param throttle The amount of time to sleep in ms between events. - * @param seed The seed of the random number generator. + * @param randomizeThrottle Whether to randomize throttle. + * @param random The random number generator. */ - public MonkeySourceRandomScript(ArrayList scriptFileNames, long throttle, long seed) { - this(null, scriptFileNames, throttle, seed); + public MonkeySourceRandomScript(ArrayList scriptFileNames, long throttle, + boolean randomizeThrottle, Random random) { + this(null, scriptFileNames, throttle, randomizeThrottle, random); } /** diff --git a/cmds/monkey/src/com/android/commands/monkey/MonkeySourceScript.java b/cmds/monkey/src/com/android/commands/monkey/MonkeySourceScript.java index e37238597..731233ccf 100644 --- a/cmds/monkey/src/com/android/commands/monkey/MonkeySourceScript.java +++ b/cmds/monkey/src/com/android/commands/monkey/MonkeySourceScript.java @@ -26,6 +26,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.NoSuchElementException; +import java.util.Random; /** * monkey event queue. It takes a script to produce events sample script format: @@ -115,9 +116,10 @@ public class MonkeySourceScript implements MonkeyEventSource { * @param filename The filename of the script (on the device). * @param throttle The amount of time in ms to sleep between events. */ - public MonkeySourceScript(String filename, long throttle) { + public MonkeySourceScript(Random random, String filename, long throttle, + boolean randomizeThrottle) { mScriptFileName = filename; - mQ = new MonkeyEventQueue(throttle); + mQ = new MonkeyEventQueue(random, throttle, randomizeThrottle); } /**