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));
}