Android之UiAutomator测试框架源码分析(第二十五篇:Gestures的设计与基本实现分析)

(注意:本文基于UI Automator测试框架版本为2.2.0) 

    Gestures类产生的对象,由UiObject2使用,UiObject2对象在创建时,持有一个Gestrues对象,我们这篇学习Gestures类的一些设计思想

非线程安全的单例对象

    Gestures类只能产生一个对象,并且只能在单线程下使用,作者显然预知此类是在单线程下使用,此时没有必要做成支持多线程,我们学习一下创建的单例对象的设计……

1、私有的静态变量用于持有单例对象

    // Singleton instance
    private static Gestures sInstance;

Gestures类持有的静态变量sInstance,用于持有创建的单例对象

 

2、私有构造方法,防止正常情况下通过构造方法创建对象

    // Private constructor.
    private Gestures(ViewConfiguration config) {
       mViewConfig = config;
    }

 

3、提供给调用者使用的静态方法getInstance(),返回单例对象

    public static Gestures getInstance(UiDevice device) {
        if (sInstance == null) {
            Context context = device.getInstrumentation().getContext();
            sInstance = new Gestures(ViewConfiguration.get(context));
        }

        return sInstance;
    }

当调用者第一次获取Gestures对象时,Gustures的单例对象才开始创建

 

对外提供的API

    除了返回单例对象的getInstance()方法,其余的API返回的对象都与PointerGesture对象有关,Gustures对象就是创建PointerGesture对象的工厂!

 

click()方法解析

1、click()方法,需传入表示屏幕中某个点的Point对象、以及表示时长的long类型的duration,返回PointerGesture对象

    public PointerGesture click(Point point, long duration) {
        // A click is a touch down and touch up over the same point with an optional delay inbetween
        return new PointerGesture(point).pause(duration);
    }

new创建PointerGesture对象之后,又调用自己的pause()方法,pause方法()对当前对象持有的实例变量进行一个更新操作,然后pasuse()方法又返回加工完的同一个PointerGesture对象,典型的链式调用法呀,写的好!

 

2、位于PointerGesture类中的pasue()方法,需要传入表示时长的long类型变量

    public PointerGesture pause(long time) {
        if (time < 0) {
            throw new IllegalArgumentException("time cannot be negative");
        }
        mActions.addLast(new PointerPauseAction(mActions.peekLast().end, time));
        mDuration += (mActions.peekLast().duration);
        return this;
    }

pause()方法对当前对象持有的mActions、mDuration做了更新操作,最后的返回值仍是当前对象,这种链式调用法很常见

 

 Gestures总结

    Gestures对象提供的功能,并没有做任何实际的交互行为,尽管方法名称为click、pinchOpen、swipe等等动词名,它提供的PointerGesture对象或者PointerGesture[数组对象都会由其他对象使用,其他对象再去执行真正的手指操作,这个其它对象就是GestureController对象!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值