Uiautomator 源码分析之点击事件

uiautomator一个简单的脚本

public class UiautomatorTest extends UiAutomatorTestCase{

    public void testDemo() throws UiObjectNotFoundException {
        
    //   模拟 HOME 键点击事件
        getUiDevice().pressHome();

}

1、uiautomatorTest继承于UiAutomatorTestCase,通过父类获得Uidevice类实例,并调用父类方法pressHome();

2、查看pressHome方法源码

    /**
     * Simulates a short press on the HOME button.
     * @return true if successful, else return false
     * @since API Level 16
     */
    public boolean pressHome() {
        Tracer.trace();
        waitForIdle();
        return getAutomatorBridge().getInteractionController().sendKeyAndWaitForEvent(
                KeyEvent.KEYCODE_HOME, 0, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED,
                KEY_PRESS_EVENT_TIMEOUT);
    }

getAutomatorBridge()方法获得InstrumentationUiAutomatorBridge实例,UiDevice类保存了

UiAutomatorBridge对象
    InstrumentationUiAutomatorBridge getAutomatorBridge() {
        if (mUiAutomationBridge == null) {
            throw new RuntimeException("UiDevice not initialized");
        }
        return mUiAutomationBridge;
    }
  • 通过UiAutomatorBridge对象获得InteractionController对象
  • 调用InteractionController对象的sendKeyAndWaitForEvent方法,里面参数关键是第一个keycode和第二个eventType

3、先来看下UiAutomatorBridge类何时初始化的还有InteractionController对象又是什么?

    /**
     * @hide
     */
    public void initialize(InstrumentationUiAutomatorBridge uiAutomatorBridge) {
        mUiAutomationBridge = uiAutomatorBridge;
    }

这个是隐藏的方法,初始化UiDevice类时直接传入UiAutomatorBridge对象,那么改函数何时执行尼?

查看源码发现这个函数是在UiDevice类里面调用的

    /**
     * Initializes this test case.
     *
     * @param params Instrumentation arguments.
     */
    void initialize(Bundle params) {
        mParams = params;

        // check if this is a monkey test mode
        String monkeyVal = mParams.getString("monkey");
        if (monkeyVal != null) {
            // only if the monkey key is specified, 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值