Fix on the failure of the 3 tests in com.android.cts.monkey.CategoryTest class. am: 7742e2ea0a

Original change: https://googleplex-android-review.googlesource.com/c/platform/development/+/17650269

Change-Id: Ifa6d9378aca38ef6586ced8519d9a70c93df3d78
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Fangqiu Su
2022-04-09 00:28:15 +00:00
committed by Automerger Merge Worker
3 changed files with 28 additions and 11 deletions

View File

@@ -48,6 +48,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
@@ -187,8 +188,8 @@ public class Monkey {
/** Categories we are allowed to launch **/
private ArrayList<String> mMainCategories = new ArrayList<String>();
/** Applications we can switch to. */
private ArrayList<ComponentName> mMainApps = new ArrayList<ComponentName>();
/** Applications we can switch to, as well as their corresponding categories. */
private HashMap<ComponentName, String> mMainApps = new HashMap<>();
/** The delay between event inputs **/
long mThrottle = 0;
@@ -1073,7 +1074,8 @@ public class Monkey {
Logger.out.println("// + Using main activity " + r.activityInfo.name
+ " (from package " + packageName + ")");
}
mMainApps.add(new ComponentName(packageName, r.activityInfo.name));
mMainApps.put(
new ComponentName(packageName, r.activityInfo.name), category);
} else {
if (mVerbose >= 3) { // very very verbose
Logger.out.println("// - NOT USING main activity "

View File

@@ -27,12 +27,15 @@ import android.os.RemoteException;
import android.os.ServiceManager;
import android.view.IWindowManager;
import java.util.HashMap;
/**
* monkey activity event
*/
public class MonkeyActivityEvent extends MonkeyEvent {
private ComponentName mApp;
long mAlarmTime = 0;
private HashMap<ComponentName, String> mMainApps = new HashMap<>();
public MonkeyActivityEvent(ComponentName app) {
super(EVENT_TYPE_ACTIVITY);
@@ -45,12 +48,23 @@ public class MonkeyActivityEvent extends MonkeyEvent {
mAlarmTime = arg;
}
public MonkeyActivityEvent(ComponentName app,
HashMap<ComponentName, String> MainApps) {
super(EVENT_TYPE_ACTIVITY);
mApp = app;
mMainApps = MainApps;
}
/**
* @return Intent for the new activity
*/
private Intent getEvent() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
if (mMainApps.containsKey(mApp)) {
intent.addCategory(mMainApps.get(mApp));
} else {
intent.addCategory(Intent.CATEGORY_LAUNCHER);
}
intent.setComponent(mApp);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
return intent;

View File

@@ -26,7 +26,8 @@ import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Surface;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
/**
@@ -94,7 +95,7 @@ public class MonkeySourceRandom implements MonkeyEventSource {
* values after we read any optional values.
**/
private float[] mFactors = new float[FACTORZ_COUNT];
private List<ComponentName> mMainApps;
private HashMap<ComponentName, String> mMainApps;
private int mEventCount = 0; //total number of events generated so far
private MonkeyEventQueue mQ;
private Random mRandom;
@@ -119,7 +120,7 @@ public class MonkeySourceRandom implements MonkeyEventSource {
return KeyEvent.keyCodeFromString(keyName);
}
public MonkeySourceRandom(Random random, List<ComponentName> MainApps,
public MonkeySourceRandom(Random random, HashMap<ComponentName, String> MainApps,
long throttle, boolean randomizeThrottle, boolean permissionTargetSystem) {
// default values for random distributions
// note, these are straight percentages, to match user input (cmd line args)
@@ -430,8 +431,8 @@ public class MonkeySourceRandom implements MonkeyEventSource {
} else if (cls < mFactors[FACTOR_SYSOPS]) {
lastKey = SYS_KEYS[mRandom.nextInt(SYS_KEYS.length)];
} else if (cls < mFactors[FACTOR_APPSWITCH]) {
MonkeyActivityEvent e = new MonkeyActivityEvent(mMainApps.get(
mRandom.nextInt(mMainApps.size())));
MonkeyActivityEvent e = new MonkeyActivityEvent(new ArrayList<ComponentName>(mMainApps.keySet()).get(
mRandom.nextInt(mMainApps.size())), mMainApps);
mQ.addLast(e);
return;
} else if (cls < mFactors[FACTOR_FLIP]) {
@@ -479,8 +480,8 @@ public class MonkeySourceRandom implements MonkeyEventSource {
* generate an activity event
*/
public void generateActivity() {
MonkeyActivityEvent e = new MonkeyActivityEvent(mMainApps.get(
mRandom.nextInt(mMainApps.size())));
MonkeyActivityEvent e = new MonkeyActivityEvent(new ArrayList<ComponentName>(mMainApps.keySet()).get(
mRandom.nextInt(mMainApps.size())), mMainApps);
mQ.addLast(e);
}