Fix on the failure of the 3 tests in com.android.cts.monkey.CategoryTest class.

**This is a duplicate of ag/17623755, which is on the master branch, and this one is for tm-dev branch.**

**Reason of Failure**

This failure is caused by the missing of assigning appropriate categories to different activities in 'development/cmds/monkey/src/com/android/commands/monkey/MonkeyActivityEvent.java' file, which is reflected in the update.

The `BaboonActivity` is invoked by the Monkey test program, and therefore should be categorized as `android.intent.category.MONKEY`, which is reflected in 'cts/hostsidetests/monkey/test-apps/CtsMonkeyApp/AndroidManifest.xml' file.

However, in the `MonkeyActivityEvent` class (in 'development/cmds/monkey/src/com/android/commands/monkey/MonkeyActivityEvent.java' file), all the `Intent` (equivalent to an event) created by the Monkey test program is still categorized as `android.intent.category.LAUNCHER`. This wrong categorization will raise an error with a message in the log stating that the access to `BaboonActivity` is blocked, and some more messages after will show that the `BaboonActivity` is not actually executed.

The categorizations are defined in 'frameworks/base/core/java/android/content/Intent.java' file.

**Debugging Notes**
1. In `getMainApps()` of 'Monkey.java', the activities (including `BaboonActivity`) are added by their categories. There can be activities under different categories. However, in 'MonkeyActivityEvent.java', all the activities are started under `LAUNCHER` category, despite their original category, which is the point causing the failure.

2. This failure is detected by the intent filter in ag/15419750 and ag/15315839 (using CL Range search between builds 7618377 and 7618385), for more info about intent filter: go for <go/intents-match-intent-filters-guide>.

Fix: 196047981
Test: Use `atest CtsMonkeyTestCases` command, all 10 tests should pass.
Change-Id: Iea4bf2cac5924a193902dd337275d753e0577fa1
This commit is contained in:
Fangqiu Su
2022-04-06 23:05:50 +00:00
parent cac84dd712
commit 7742e2ea0a
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);
}