<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                上一篇文章《[UiAutomator源碼分析之UiAutomatorBridge框架](http://blog.csdn.net/zhubaitian/article/details/40539103)》中我們把UiAutomatorBridge以及它相關的類進行的描述,往下我們會嘗試根據兩個實例將這些類給串聯起來,我準備做的是用如下兩個很有代表性的實例: - 注入事件 - 獲取控件 這一篇文章我們會通過分析UiDevice的pressHome這個方法來分析UiAutomator是如何注入事件的,下一篇文章會描述如何獲取控件,敬請期待。 # 1. UiObject.pressHome順序圖 首先我們看一下我手畫的非規范的順序圖,從中我們可以看到pressHome這個動作究竟需要和多少個類進行交互,以及它們是怎么交互的。 ![](https://box.kancloud.cn/2016-08-12_57ad6e29eb28c.jpg) # 2.這些類是什么時候初始化的 在我們編寫測試用例腳本的時候我們不會對以上所有的類進行初始化,包括UiObject對象都是通過直接在腳本中調用父類UiAutomationTestCase的getUiDevice()這個方法來獲得的。其實這些都是在uiautomator運行時由RunTestCommand類的start()這個方法進行初始化的,具體請看《[UIAutomator源碼分析之啟動和運行](http://blog.csdn.net/zhubaitian/article/details/40535579)》的 3.6章節“初始化UiDevice和UiAutomationBridge“,這里就不做累述。我們這里會看下在初始化UiAutomatorBridge的時候是如何把QuneryControoler和InteractionController一并初始化了的,具體請看UiAutomatorBridge的構造函數: ~~~ /* */ UiAutomatorBridge(UiAutomation uiAutomation) /* */ { /* 48 */ this.mUiAutomation = uiAutomation; /* 49 */ this.mInteractionController = new InteractionController(this); /* 50 */ this.mQueryController = new QueryController(this); /* */ } ~~~ # 3. 代碼跟蹤 首先看UiDevice的pressHome方法: ~~~ public boolean pressHome() { 218 Tracer.trace(); 219 waitForIdle(); 220 return getAutomatorBridge().getInteractionController().sendKeyAndWaitForEvent( 221 KeyEvent.KEYCODE_HOME, 0, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED, 222 KEY_PRESS_EVENT_TIMEOUT); 223 } ~~~ 220行: - 獲得UiDevice對象保存的UiAutomatorBridge對象。著兩個對象都是在運行時初始化的,不清楚的話請翻看上面提到的文章 - 通過UiAutomatorBridge對象獲得上面章節初始化的InteractionController對象 - 調用InteractionController對象的sendKeyAndWaitForEvent方法,里面參數關鍵是第一個keycode和第二個eventType - keycode:代表我們要注入的是按下哪個按鍵的事件,比如這里我們是KEYCODE_HOME - eventType:代表我們注射了該事件后預期會獲得窗口返回來的哪種AccessibilityEvent類型,比如我們這里是TYPE_WINDOW_CONTENT_CHANGE 進入InteractionController類的sendKeyAndWaitForEvent: ~~~ /* */ public boolean sendKeyAndWaitForEvent(final int keyCode, final int metaState, int eventType, long timeout) /* */ { /* 188 */ Runnable command = new Runnable() /* */ { /* */ public void run() { /* 191 */ long eventTime = SystemClock.uptimeMillis(); /* 192 */ KeyEvent downEvent = new KeyEvent(eventTime, eventTime, 0, keyCode, 0, metaState, -1, 0, 0, 257); /* */ /* */ /* 195 */ if (InteractionController.this.injectEventSync(downEvent)) { /* 196 */ KeyEvent upEvent = new KeyEvent(eventTime, eventTime, 1, keyCode, 0, metaState, -1, 0, 0, 257); /* */ /* */ /* 199 */ InteractionController.this.injectEventSync(upEvent); /* */ } /* */ /* */ } /* 203 */ }; /* 204 */ return runAndWaitForEvents(command, new WaitForAnyEventPredicate(eventType), timeout) != null; /* */ } ~~~ 代碼中創建了一個Runnable的線程,線程里面run重寫方法要做的事情就是去做注入事件的事情,那么為什么我們不直接去調用事件而需要創建一個線程了,這是因為我們在注入完事件之后還要去等待我們上面定義的預期的eventType是否有出現來判斷我們的事件注入究竟是否成功,這個就是204行runAndWaitForEvents做的事情。但我們這里還是先看下線程中是如何注入事件的: ~~~ /* */ private boolean injectEventSync(InputEvent event) { /* 655 */ return this.mUiAutomatorBridge.injectInputEvent(event, true); /* */ } ~~~ 再跟蹤到UiAutomatorBridge對象: ~~~ /* */ public boolean injectInputEvent(InputEvent event, boolean sync) { /* 70 */ return this.mUiAutomation.injectInputEvent(event, sync); /* */ } ~~~ 可以看到最終還是通過UiAutomation來注入事件的,和我們的預期是一致的。 我們繼續看InteractionController中真正執行注入事件線程的runAndWaitForEvents方法: ~~~ /* */ private AccessibilityEvent runAndWaitForEvents(Runnable command, UiAutomation.AccessibilityEventFilter filter, long timeout) /* */ { /* */ try /* */ { /* 161 */ return this.mUiAutomatorBridge.executeCommandAndWaitForAccessibilityEvent(command, filter, timeout); /* */ } /* */ catch (TimeoutException e) { /* 164 */ Log.w(LOG_TAG, "runAndwaitForEvent timedout waiting for events"); /* 165 */ return null; /* */ } catch (Exception e) { /* 167 */ Log.e(LOG_TAG, "exception from executeCommandAndWaitForAccessibilityEvent", e); } /* 168 */ return null; /* */ } ~~~ 代碼又跳到了UiAutomatorBridge這個類 ~~~ /* */ public AccessibilityEvent executeCommandAndWaitForAccessibilityEvent(Runnable command, UiAutomation.AccessibilityEventFilter filter, long timeoutMillis) throws TimeoutException /* */ { /* 104 */ return this.mUiAutomation.executeAndWaitForEvent(command, filter, timeoutMillis); /* */ } ~~~ 最終把要執行的runnable執行注入事件的線程command和我們預期事件發生后返回來的窗口事件filter以及超時timeoutMillis傳進去,UiAutomation就會和AccessibilityService進行交互以注入事件并且等待預期AccessibilityEvent發生或者超時返回。至于UiAutomation是如何和AccessibilityService交互的,這就超出了這個系列文章的范疇了。也許今后有充裕的時間的話我們再來深入去了解分析它。 <table cellspacing="0" cellpadding="0" width="539" class=" " style="margin:0px 0px 10px; padding:0px; border-collapse:collapse; width:668px; max-width:100%; word-wrap:break-word!important"><tbody style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><tr style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><td valign="top" width="112" height="39" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important">?</td></tr><tr style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><td valign="top" width="111" height="13" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important; background-color:rgb(190,192,191)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">作者</span></p></td><td valign="top" width="112" height="13" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important; background-color:rgb(190,192,191)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">自主博客</span></p></td><td valign="top" width="111" height="13" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important; background-color:rgb(190,192,191)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">微信</span></p></td><td valign="top" width="112" height="13" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important; background-color:rgb(190,192,191)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; font-family:Helvetica; letter-spacing:0px; word-wrap:break-word!important">CSDN</span></p></td></tr><tr style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><td valign="top" width="111" height="39" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important; background-color:rgb(227,228,228)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">天地會珠海分舵</span></p></td><td valign="top" width="112" height="39" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; font-size:11px; font-family:Helvetica; letter-spacing:0px; word-wrap:break-word!important"><a target="_blank" href="http://techgogogo.com/">http://techgogogo.com</a></span><span style="margin:0px; padding:0px; max-width:100%; font-family:Helvetica; font-size:11px; letter-spacing:0px; word-wrap:break-word!important"/></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:14px; white-space:pre-wrap; font-family:Helvetica; word-wrap:break-word!important"><br style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"/></p></td><td valign="top" width="111" height="39" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">服務號</span><span style="margin:0px; padding:0px; max-width:100%; font-size:10px; font-family:Helvetica; letter-spacing:0px; word-wrap:break-word!important">:TechGoGoGo</span></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">掃描碼</span><span style="margin:0px; padding:0px; max-width:100%; font-size:10px; font-family:Helvetica; letter-spacing:0px; word-wrap:break-word!important">:</span></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:14px; white-space:pre-wrap; font-family:Helvetica; word-wrap:break-word!important"><img src="image/47cf4f9ec59b0ef1f807a6c33ab5ce5f.jpg" alt="" style="max-width:100%; margin:0px; padding:0px; height:auto!important; word-wrap:break-word!important; width:auto!important; visibility:visible!important"/></p></td><td valign="top" width="112" height="39" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; color:rgb(62,62,62); font-family:'Helvetica Neue',Helvetica,'Hiragino Sans GB','Microsoft YaHei',?¢èí??oú,Arial,sans-serif; font-size:18px; line-height:28.7999992370605px; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; color:rgb(0,0,0); font-size:11px; font-family:Helvetica; letter-spacing:0px; word-wrap:break-word!important"><a target="_blank" href="http://blog.csdn.net/zhubaitian">http://blog.csdn.net/zhubaitian</a></span><span style="margin:0px; padding:0px; max-width:100%; color:rgb(0,0,0); font-family:Helvetica; font-size:11px; letter-spacing:0px; line-height:28.7999992370605px; word-wrap:break-word!important"/></p><div><span style="margin:0px; padding:0px; max-width:100%; color:rgb(0,0,0); font-family:Helvetica; font-size:11px; letter-spacing:0px; line-height:28.7999992370605px; word-wrap:break-word!important"><br/></span></div></td></tr></tbody></table>
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看