现在的位置: 首页 > 综合 > 正文

Android ActivityManager isUserAMonkey() 方法探究 与 monkey 测试

2014年02月10日 ⁄ 综合 ⁄ 共 1498字 ⁄ 字号 评论关闭

这个方法不论是从表面还是从系统源码代分析来看他都是google开的一个玩笑!

直到你百度 monkey 测试,哦你才恍然大悟,原来是 猴子测试

何为monkey 测试?

顾名思义,让猴子帮你测试。听起来是不是很扯?

其实当你需要对你的应用进行暴力测试时,显然一只猴子会比你做的更专业。方法很简单,把手机扔给猴子,10分钟后拿回,如果你的应用没有FC,手机没死机,那么恭喜你,你的技术很牛X,连猴子都找不到BUG!

哈哈,开玩笑啦,你真以为猴子也会切水果么?他只是看中了香蕉而已!

正题:

adb shell

monkey -p com.mrpoid.shequ2 500

执行这条命令后,你的手机已经被猴子抓了500次了,快看看成什么样了!

------------------------------------------------------------------------------------------------------------

isUserAMonkey()

Returns "true" if the user interface is currently being messed with by a monkey.

字面意思为 返回 true 如果此时用户界面被一直猴子给搞乱了

api 文档看不出什么究竟,我们来看下源码吧

    public boolean isUserAMonkey() {
        // For now the fact that there is a controller implies
        // we have a monkey.
        synchronized (this) {
            return mController != null;
        }
    }

在看一个方法

    public void setActivityController(IActivityController controller) {
        enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER,
                "setActivityController()");
        synchronized (this) {
            mController = controller;
        }
    }

再看下这个 mController 在什么地方用到了吧

    public final boolean finishActivity(IBinder token, int resultCode, Intent resultData) {
        // Refuse possible leaked file descriptors
        if (resultData != null && resultData.hasFileDescriptors() == true) {
            throw new IllegalArgumentException("File descriptors passed in Intent");
        }

        synchronized(this) {
            if (mController != null) {
                // Find the first activity that is not finishing.
                ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0);
                if (next != null) {
                    // ask watcher if this is allowed
                    boolean resumeOK = true;
                    try {
                        resumeOK = mController.activityResuming(next.packageName);
                    } catch (RemoteException e) {
                        mController = null;
                    }
    
                    if (!resumeOK) {
                        return false;
                    }
                }
            }
【上篇】
【下篇】

抱歉!评论已关闭.