monkeyrunner点击控件之源码分析

monkeyrunner 简单测试脚本

device = mr.waitForConnection()  #连接设备,等待 
easy_device = EasyMonkeyDevice(device)
easy_device.touch(By.id('id/main_widget1'),md.DOWN_AND_UP) 

1、waitForConnection就不说了,看下EasyMonkeyDevice源码

@MonkeyRunnerExported(doc = "MonkeyDevice with easier methods to refer to objects.")
public class EasyMonkeyDevice extends PyObject implements ClassDictInit {
    public static void classDictInit(PyObject dict) {
        JythonUtils.convertDocAnnotationsForClass(EasyMonkeyDevice.class, dict);
    }

    private MonkeyDevice mDevice;
    private HierarchyViewer mHierarchyViewer;

    private static final Set<String> EXPORTED_METHODS =
        JythonUtils.getMethodNames(EasyMonkeyDevice.class);

    @MonkeyRunnerExported(doc = "Creates EasyMonkeyDevice with an underlying MonkeyDevice.",
            args = { "device" },
            argDocs = { "MonkeyDevice to extend." })
    public EasyMonkeyDevice(MonkeyDevice device) {
        this.mDevice = device;
        this.mHierarchyViewer = device.getImpl().getHierarchyViewer();
    }
EasyMonkeyDevice 构造函数是传入device对象并获得 HierarchyViewer实例
注意这里device.getImp()获得的是IMonkeyDevice实例,即它的实现类AdbMonkeyDevice,所以这里最终是调用AdbMonkeyDevice的getHierarchyViewer()方法

    @Override
    public HierarchyViewer getHierarchyViewer() {
        return new HierarchyViewer(device);
    }
2、看EasyMonkeyDevice的touch方法

    public void touch(By selector, TouchPressType type) {
        Point p = getElementCenter(selector);
        mDevice.getImpl().touch(p.x, p.y, type);
    }
这里传入的是By类型的参数和事件Type

先看下getElementCenter这个方法

    /**
     * Get the coordinates of the element's center.
     *
     * @param selector the element selector
     * @return the (x,y) coordinates of the center
     */
    private Point getElementCenter(By selector) {
        ViewNode node = mHierarchyViewer.findView(selector);
        if (node == null) {
            throw new PyException(Py.ValueError,
                    String.format("View not found: %s", selector));
        }

        Point p = HierarchyViewer.getAbsoluteCenterOfView(node);
        return p;
    }

}

实际上是使用了HierarchyViewer类来获取ViewNode视图节点

这里看下它的实现方法

    /**
     * Finds a view using a selector. Currently only supports selectors which
     * specify an id.
     *
     * @param selector selector for the view.
     * @return view with the specified ID, or {@code null} if no view found.
     */
    public ViewNode findView(By selector) {
        ViewNode rootNode = DeviceBridge.loadWindowData(
                new Window(mDevice, "", 0xffffffff));
        if (rootNode == null) {
            throw new RuntimeException("Could not dump view");
        }
        return selector.find(rootNode);
    }
这里使用了device的桥接器加载窗口组件,是许多view的组合
    /**
     * Find the selected view from the root view node.
     */
    ViewNode find(ViewNode rootNode) {
        if (rootNode.id.equals(id)) {
            return rootNode;
        }
        for (ViewNode child : rootNode.children) {
            ViewNode found = find(child);
            if (found != null) {
                return found;
            }
        }
        return null;
    }
}
这里先是做个判断如果找的刚好只有一个view并且和要找的id一致则直接返回

如果是许多view的话就遍历查找

3、回到下面的方法

 Point p = HierarchyViewer.getAbsoluteCenterOfView(node);
找到相应的view后就要找到对应的中心点,看源码实现

    /**
     * Gets the absolute x/y center of the specified view node.
     *
     * @param node view node to find position of.
     * @return absolute x/y center of the specified view node.
     */
    public static Point getAbsoluteCenterOfView(ViewNode node) {
        Point point = getAbsolutePositionOfView(node);
        return new Point(
                point.x + (node.width / 2), point.y + (node.height / 2));
    }
上面应该是先找到xy起点然后再加上view视图的长宽的一半,就是view的中点了,然后把这个画笔point点返回给touch函数
    /**
     * Gets the absolute x/y position of the view node.
     *
     * @param node view node to find position of.
     * @return point specifying the x/y position of the node.
     */
    public static Point getAbsolutePositionOfView(ViewNode node) {
        int x = node.left;
        int y = node.top;
        ViewNode p = node.parent;
        while (p != null) {
            x += p.left - p.scrollX;
            y += p.top - p.scrollY;
            p = p.parent;
        }
        return new Point(x, y);
    }

4、点击

private MonkeyDevice mDevice;

 mDevice.getImpl().touch(p.x, p.y, type);

    private IMonkeyDevice impl;

    public MonkeyDevice(IMonkeyDevice impl) {
        this.impl = impl;
    }

    public IMonkeyDevice getImpl() {
        return impl;
    }
MonkeyDevice类获得IMonkeyDevice实例,即AdbMonkeyDevice的touch方法

    @Override
    public void touch(int x, int y, TouchPressType type) {
        try {
            switch (type) {
                case DOWN:
                    manager.touchDown(x, y);
                    break;
                case UP:
                    manager.touchUp(x, y);
                    break;
                case DOWN_AND_UP:
                    manager.tap(x, y);
                    break;
            }
        } catch (IOException e) {
            LOG.log(Level.SEVERE, "Error sending touch event: " + x + " " + y + " " + type, e);
        }
    }

到这里又回到了上一篇的内容了,不再阐述

monkeyrunner点击坐标之源码分析



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值