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 在接收到测试执行指令之后的动作:

(右键-》在新标签中打开,会看得比较清楚)




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