Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4ee7f4b1 authored by Maksymilian Osowski's avatar Maksymilian Osowski
Browse files

LayoutTest class with supporting classes (AbstractResult, TextResult).

These classes are responsible for actually running the tests, checking the results, generating visual diffs, etc.

Change-Id: I9b7f554409db1c97ac086c456db3aea3b993b5c6
parent 3c8ccb38
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ limitations under the License.
        </activity>
    </application>

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_SDCARD" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
 No newline at end of file
+34 −4
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

package com.android.dumprendertree2;

import android.os.Message;
import android.webkit.WebView;

/**
 * A class that represent a result of the test. It is responsible for returning the result's
 * raw data and generating its own diff in HTML format.
@@ -47,12 +50,39 @@ public abstract class AbstractResult {
    }

    /**
     * Returns result's raw data that can be written to the disk.
     * Makes the result object obtain the result of the test from the webview
     * and store it in the format that suits itself bests. This method is asynchronous.
     * The message passed as a parameter is a message that should be sent to its target
     * when the result finishes obtaining the result.
     *
     * @param webview
     * @param resultObtainedMsg
     */
    public abstract void obtainActualResult(WebView webview, Message resultObtainedMsg);

    public abstract void setExpectedImageResult(byte[] expectedResult);

    public abstract void setExpectedTextResult(String expectedResult);

    /**
     * Returns result's image data that can be written to the disk. It can be null
     * if there is an error of some sort or for example the test times out.
     *
     * <p> Some tests will not provide data (like text tests)
     *
     * @return
     *      results image data
     */
    public abstract byte[] getActualImageResult();

    /**
     * Returns result's text data. It can be null
     * if there is an error of some sort or for example the test times out.
     *
     * @return
     *      results raw data
     *      results text data
     */
    public abstract byte[] getData();
    public abstract String getActualTextResult();

    /**
     * Returns the code of this result.
@@ -60,7 +90,7 @@ public abstract class AbstractResult {
     * @return
     *      the code of this result
     */
    public abstract ResultCode getCode();
    public abstract ResultCode getResultCode();

    /**
     * Return the type of the result data.
+28 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.dumprendertree2;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
@@ -48,4 +49,30 @@ public class FsUtils {
            Log.e(LOG_TAG + "::writeDataToStorage", e.getMessage());
        }
    }

    public static byte[] readDataFromStorage(File file) {
        if (!file.exists()) {
            Log.d(LOG_TAG + "::readDataFromStorage", "File does not exist: "
                    + file.getAbsolutePath());
            return null;
        }

        byte[] bytes = null;
        try {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                bytes = new byte[(int) file.length()];
                fis.read(bytes);
            } finally {
                if (fis != null) {
                    fis.close();
                }
            }
        } catch (IOException e) {
            Log.e(LOG_TAG + "::readDataFromStorage", e.getMessage());
        }

        return bytes;
    }
}
 No newline at end of file
+133 −5
Original line number Diff line number Diff line
@@ -16,7 +16,19 @@

package com.android.dumprendertree2;

import android.app.Activity;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebStorage.QuotaUpdater;

import java.io.File;

/**
 * A class that represents a single layout test. It is responsible for running the test,
@@ -24,18 +36,134 @@ import android.os.Handler;
 */
public class LayoutTest {

    private static final String LOG_TAG = "LayoutTest";

    public static final int MSG_ACTUAL_RESULT_OBTAINED = 0;

    private String mRelativePath;
    private Handler mCallbackHandler;
    private String mTestsRootDirPath;
    private String mUrl;
    private boolean mOnTestFinishedCalled;
    private Message mTestFinishedMsg;
    private AbstractResult mResult;

    public LayoutTest(String relativePath, Handler callbackHandler) {
    private WebView mWebView;
    private Activity mActivity;

    private final Handler mResultHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == MSG_ACTUAL_RESULT_OBTAINED) {
                mResult.setExpectedTextResult(LayoutTestsRunnerThread.getExpectedTextResult(mRelativePath));
                mResult.setExpectedImageResult(LayoutTestsRunnerThread.getExpectedImageResult(mRelativePath));
                mTestFinishedMsg.sendToTarget();
            }
        }
    };

    private WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            /** Some tests fire up many page loads, we don't want to detect them */
            if (!url.equals(mUrl)) {
                return;
            }

            onTestFinished();
        }
    };

    private WebChromeClient mWebChromeClient = new WebChromeClient() {
        @Override
        public void onExceededDatabaseQuota(String url, String databaseIdentifier,
                long currentQuota, long estimatedSize, long totalUsedQuota,
                QuotaUpdater quotaUpdater) {
            /** TODO: This should be recorded as part of the text result */
            quotaUpdater.updateQuota(currentQuota + 5 * 1024 * 1024);
        }

        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            /** TODO: Alerts should be recorded as part of text result */
            result.confirm();
            return true;
        }

        @Override
        public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
            /** TODO: Alerts should be recorded as part of text result */
            result.confirm();
            return true;
        }

        @Override
        public boolean onJsPrompt(WebView view, String url, String message, String defaultValue,
                JsPromptResult result) {
            /** TODO: Alerts should be recorded as part of text result */
            result.confirm();
            return true;
        }

    };

    public LayoutTest(String relativePath, String testsRootDirPath, Message testFinishedMsg,
            LayoutTestsRunner activity) {
        mRelativePath = relativePath;
        mCallbackHandler = callbackHandler;
        mTestsRootDirPath = testsRootDirPath;
        mTestFinishedMsg = testFinishedMsg;
        mActivity = activity;
    }

    public void run() {
        /** TODO: This is just a stub! */
        mCallbackHandler.obtainMessage(LayoutTestsRunnerThread.MSG_TEST_FINISHED).sendToTarget();
        mWebView = new WebView(mActivity);
        mActivity.setContentView(mWebView);

        setupWebView();

        /** TODO: Add timeout msg */
        mUrl = Uri.fromFile(new File(mTestsRootDirPath, mRelativePath)).toString();
        mWebView.loadUrl(mUrl);
    }

    private void onTestFinished() {
        if (mOnTestFinishedCalled) {
            return;
        }

        mOnTestFinishedCalled = true;

        /**
         * If the result has not been set by the time the test finishes we create
         * a default type of result.
         */
        if (mResult == null) {
            /** TODO: Default type should be RenderTreeResult. We don't support it now. */
            mResult = new TextResult(mRelativePath);
        }

        /** TODO: Implement waitUntilDone */

        mResult.obtainActualResult(mWebView,
                mResultHandler.obtainMessage(MSG_ACTUAL_RESULT_OBTAINED));
    }

    private void setupWebView() {
        WebSettings webViewSettings = mWebView.getSettings();
        webViewSettings.setAppCacheEnabled(true);
        webViewSettings.setAppCachePath(mActivity.getApplicationContext().getCacheDir().getPath());
        webViewSettings.setAppCacheMaxSize(Long.MAX_VALUE);
        webViewSettings.setJavaScriptEnabled(true);
        webViewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webViewSettings.setSupportMultipleWindows(true);
        webViewSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        webViewSettings.setDatabaseEnabled(true);
        webViewSettings.setDatabasePath(mActivity.getDir("databases", 0).getAbsolutePath());
        webViewSettings.setDomStorageEnabled(true);
        webViewSettings.setWorkersEnabled(false);
        webViewSettings.setXSSAuditorEnabled(false);

        mWebView.setWebViewClient(mWebViewClient);
        mWebView.setWebChromeClient(mWebChromeClient);
    }

    public AbstractResult getResult() {
+5 −1
Original line number Diff line number Diff line
@@ -85,6 +85,10 @@ public class LayoutTestsRunner extends Activity {
        }
        String path = intent.getStringExtra(EXTRA_TEST_PATH);

        new LayoutTestsRunnerThread(path, mHandler).start();
        new LayoutTestsRunnerThread(path, this).start();
    }

    public Handler getHandler() {
        return mHandler;
    }
}
 No newline at end of file
Loading