FileDocCategorySizeDatePackage
JavaBridgeTestBase.javaAPI DocAndroid 5.1 API4435Thu Mar 12 22:22:44 GMT 2015com.android.webviewtests

JavaBridgeTestBase

public class JavaBridgeTestBase extends android.test.ActivityInstrumentationTestCase2
Common functionality for testing the WebView's Java Bridge.

Fields Summary
protected TestWebViewClient
mWebViewClient
Constructors Summary
public JavaBridgeTestBase()

        super(WebViewStubActivity.class);
    
Methods Summary
protected voidexecuteJavaScript(java.lang.String script)

        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                // When a JavaScript URL is executed, if the value of the last
                // expression evaluated is not 'undefined', this value is
                // converted to a string and used as the new document for the
                // frame. We don't want this behaviour, so wrap the script in
                // an anonymous function.
                getWebView().loadUrl("javascript:(function() { " + script + " })()");
            }
        });
    
protected android.webkit.WebViewgetWebView()

        return getActivity().getWebView();
    
protected voidsetUpWebView(java.lang.Object object, java.lang.String name)

        mWebViewClient = new TestWebViewClient();
        // This starts the activity, so must be called on the test thread.
        final WebViewStubActivity activity = getActivity();
        // On the UI thread, load an empty page and wait for it to finish
        // loading so that the Java object is injected.
        try {
            runTestOnUiThread(new Runnable() {
                @Override
                public void run() {
                    WebView webView = activity.getWebView();
                    webView.addJavascriptInterface(object, name);
                    webView.getSettings().setJavaScriptEnabled(true);
                    webView.setWebViewClient(mWebViewClient);
                    webView.loadData("<!DOCTYPE html><title></title>", "text/html", null);
                }
            });
            mWebViewClient.waitForOnPageFinished();
        } catch (Throwable e) {
            throw new RuntimeException("Failed to set up WebView: " + Log.getStackTraceString(e));
        }