• uiautomator 原理 (UiAutomation、UiAutomatorBridge、QueryController)


    UiAutomation

    UiAutomation主要和AccessibilityService进行连接然后获取界面空间信息和注入事件,截图

    获取界面空间信息:

    public AccessibilityNodeInfo getRootInActiveWindow() {
         return this.mUiAutomation.getRootInActiveWindow();
       }

    注入事件:

    public boolean injectInputEvent(InputEvent event, boolean sync) {
         return this.mUiAutomation.injectInputEvent(event, sync);
      }

    截图:

    Bitmap screenshot = this.mUiAutomation.takeScreenshot();

    UiAutomatorBridge

    UiAutomatorBridge相当于UiAutomation的代理,进行获取界面空间信息和注入事件,截图都需要经过UiAutomatorBridge

    private boolean injectEventSync(InputEvent event) {
        return this.mUiAutomatorBridge.injectInputEvent(event, true);
      }
    this.mUiAutomatorBridge.getRootInActiveWindow(); 

    QueryController

    QueryController做的所有事情就是去把UiSelector这个UI控件选择子翻译成真实的适合我们使用的Android.view.accessibility.AccessibilityNodeInfo

    protected AccessibilityNodeInfo findAccessibilityNodeInfo(long timeout)
      {
        AccessibilityNodeInfo node = null;
      long startMills = SystemClock.uptimeMillis();
       long currentMills = 0L;
       while (currentMills <= timeout) {
         node = getQueryController().findAccessibilityNodeInfo(getSelector());
         if (node != null) {
            break;
         }
        
         UiDevice.getInstance().runWatchers();
         
         currentMills = SystemClock.uptimeMillis() - startMills;
          if (timeout > 0L) {
            SystemClock.sleep(1000L);
         }
       }
        return node;
    }

    UiDevice的实例化

    private UiDevice(Instrumentation instrumentation) {
        this.mInstrumentation = instrumentation;
        UiAutomation uiAutomation = instrumentation.getUiAutomation();
        this.mUiAutomationBridge = new InstrumentationUiAutomatorBridge(instrumentation.getContext(), uiAutomation);
  • 相关阅读:
    IntelliJ IDEA 修改内存大小,使得idea运行更流畅。(转发)
    QueryRunner使用总结
    C#中static修饰符的作用
    Design Support库中的控件
    关于tomcat部署项目的问题
    让后台服务一直运行
    活动和服务的通信
    服务的用法
    数组右移问题
    素数对猜想
  • 原文地址:https://www.cnblogs.com/season-xie/p/6337687.html
Copyright © 2020-2023  润新知