Layout Tests 分析3- 百度手機瀏覽器T5內核

按包名分析

com.android.dumprendertree2.scriptsupport 

可以和 python 腳本交互的包,用來啓動 layouttest,待運行結束之後回掉  OnEverythingFinishedCallback

下面這一段註釋及代碼交代的也非常清楚了:
    /**
     * This method is called from adb to start executing the tests. It doesn't return
     * until everything is finished so that the script can wait for the end if it needs
     * to.
     */
    public void startLayoutTests() {
        ScriptTestRunner runner = (ScriptTestRunner)getInstrumentation();
        String relativePath = runner.getTestsRelativePath();

        ForwarderManager.getForwarderManager().start();

        Intent intent = new Intent();
        intent.setClassName("com.android.dumprendertree2", "TestsListActivity");
        intent.setAction(Intent.ACTION_RUN);
        intent.putExtra(TestsListActivity.EXTRA_TEST_PATH, relativePath);
        setActivityIntent(intent);
        getActivity().registerOnEverythingFinishedCallback(new OnEverythingFinishedCallback() {
            /** This method is safe to call on any thread */
            @Override
            public void onFinished() {
                synchronized (Starter.this) {
                    mEverythingFinished = true;
                    Starter.this.notifyAll();
                }
            }
        });

        synchronized (this) {
            while (!mEverythingFinished) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    Log.e(LOG_TAG, "startLayoutTests()", e);
                }
            }
        }

        ForwarderManager.getForwarderManager().stop();
    }

com.android.dumprendertree2.forwarder

/**

 * A port forwarding server. Listens on localhost on specified port and forwards thetcp

 * communications to external socket via adb networking proxy.

 */

public class Forwarderextends Thread 


/**

 * Worker class for {@link Forwarder}. A ConnectionHandler will be created once the Forwarder

 * accepts an incoming connection, and it will then forward the incoming/outgoing streams to a

 * connection already proxied by adb networking (see also {@link AdbUtils}).

 */

public class ConnectionHandler 


/**

 * A simple class to start and stop Forwarders running on some ports.

 *

 * It uses a singleton pattern and is thread safe.

 */

public class ForwarderManager 

/**

 * The utility class that can setup a socket allowing the device to communicate with remote

 * machines through the machine that the device is connected to via adb.

 */

public class AdbUtils



com.android.dumprendertre2

從 UML 圖上面可以看出這是 Layouttest 的核心。核心類前面有講述,但這個圖中描述的更加全面。
依據這個圖來閱讀源碼效率會更高:


需要注意的一點,LayoutTestExecutor 通過下面的接口註冊了 JavaScriptInterface,讓 JavaScript 可以調用 Java 程序,這一點非常有用。

    private static class WebViewWithJavascriptInterfaces extends WebView {

        public WebViewWithJavascriptInterfaces(

                Context context, Map<String, Object> javascriptInterfaces) {

            super(context,

                  null, // attribute set

                  0, // default style resource ID

                  javascriptInterfaces,

                  false); // is private browsing

        }

    }


接下來就可以看一看 LayoutTest 的流程是如何的:

LayoutTest 的流程

TestsListActivity 啓動到第一個測試用例開始執行的流程:



LayoutTestExecutor 在接收到測試執行指令之後的動作:

(右鍵-》在新標籤中打開,會看得比較清楚)




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章