monkey.java::run()第四部分源码——三个方法:
//方法一
if (!checkInternalConfiguration()) {
return -2;
}
//方法二
if (!getSystemInterfaces()) {
return -3;
}
//方法三
if (!getMainApps()) {
return -4;
}
1.方法一:检查内部配置
//检查内部配置是否正确,而checkInternalConfiguration()返回值永远是true??有啥用?!
if (!checkInternalConfiguration()) {
return -2;
}
……
private boolean checkInternalConfiguration() {
return true;
}
2.方法二:获取系统接口
获取三个对象:ActivityManager,IWindowManager, IPackageManager,如果获取失败则报错返回false。这三个使用来干什么的?是monkey通信需要的3个系统服务:
- ActivityManager:通过调用setActivityController()对整个测试周期进行控制
- IWindowManager:通过IWindowManager提供的方法是的monkey将点击、按键、翻转等事件加入应用中
- IPackageManager:通过IPackageManager获取应用列表,以便在测试时可在多个应用中随机切换
if (!getSystemInterfaces()) {
return -3;
}
……
private boolean getSystemInterfaces() {
mAm = ActivityManager.getService();
if (mAm == null) {
Logger.err.println("** Error: Unable to connect to activity manager; is the system "
+ "running?");
return false;
}
mWm = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
if (mWm == null) {
Logger.err.println("** Error: Unable to connect to window manager; is the system "
+ "running?");
return false;
}
mPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
if (mPm == null) {
Logger.err.println("** Error: Unable to connect to package manager; is the system "
+ "running?");
return false;
}
//注册ActivityManager对象
try {
mAm.setActivityController(new ActivityController(), true);
mNetworkMonitor.register(mAm);
} catch (RemoteException e) {
Logger.err.println("** Failed talking with activity manager!");
return false;
}
return true;
}
3.方法三:获取package列表
if (!getMainApps()) {
return -4;
}
……
//将合适的package添加到mMainApps列表中
private boolean getMainApps() {
try {
final int N = mMainCategories.size();
for (int i = 0; i < N; i++) {
Intent intent = new Intent(Intent.ACTION_MAIN);
String category = mMainCategories.get(i);
if (category.length() > 0) {
intent.addCategory(category);
}
//检查该app是否已经成功注册了Intent.ACTION_MAIN
List<ResolveInfo> mainApps = mPm.queryIntentActivities(intent, null, 0,
UserHandle.myUserId()).getList();
if (mainApps == null || mainApps.size() == 0) {
Logger.err.println("// Warning: no activities found for category " + category);
continue;
}
//要两个“-v”才会显示这各打印信息
if (mVerbose >= 2) { // very verbose
Logger.out.println("// Selecting main activities from category " + category);
}
final int NA = mainApps.size();
for (int a = 0; a < NA; a++) {
ResolveInfo r = mainApps.get(a);
String packageName = r.activityInfo.applicationInfo.packageName;
//检查这个package是否合法
if (MonkeyUtils.getPackageFilter().checkEnteringPackage(packageName)) {
if (mVerbose >= 2) { // very verbose
Logger.out.println("// + Using main activity " + r.activityInfo.name
+ " (from package " + packageName + ")");
}
//将合法的package添加到mMainApps列表中
mMainApps.add(new ComponentName(packageName, r.activityInfo.name));
} else {
if (mVerbose >= 3) { // very very verbose
Logger.out.println("// - NOT USING main activity "
+ r.activityInfo.name + " (from package " + packageName + ")");
}
}
}
}
} catch (RemoteException e) {
Logger.err.println("** Failed talking with package manager!");
return false;
}
if (mMainApps.size() == 0) {
Logger.out.println("** No activities found to run, monkey aborted.");
return false;
}
return true;
}
1)检查该package是否合法,checkEnteringPackage(),只有当该包不在mInvalidPackages、在mValidPackages才能返回true,进行下一步操作。
MonkeyUtils::checkEnteringPackage()
public boolean checkEnteringPackage(String pkg) {
//检查该package是否在黑名单中,在的话返回false,否则返回true
if (mInvalidPackages.size() > 0) {
if (mInvalidPackages.contains(pkg)) {
return false;
}
//检查该package是否在白名单中,不在的话返回false,否则返回true
} else if (mValidPackages.size() > 0) {
if (!mValidPackages.contains(pkg)) {
return false;
}
}
return true;
}
2)将组件加入mMainApps列表中,mMainApps列表会在monkey运行过程中使用到