Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / android_webview / tools / WebViewShell / src / org / chromium / webview_shell / WebViewLayoutTestActivity.java
blobd28a5aeea1473a9b889611aad2e693422125420b
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.webview_shell;
7 import android.app.Activity;
8 import android.os.Bundle;
9 import android.text.TextUtils;
11 import android.webkit.ConsoleMessage;
12 import android.webkit.GeolocationPermissions;
13 import android.webkit.PermissionRequest;
14 import android.webkit.WebChromeClient;
15 import android.webkit.WebSettings;
16 import android.webkit.WebView;
17 import android.webkit.WebViewClient;
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.TimeoutException;
22 /**
23 * This activity is used for running layout tests using webview. The activity
24 * creates a webview instance, loads url and captures console messages from
25 * JavaScript until the test is finished.
26 * provides a blocking callback.
28 public class WebViewLayoutTestActivity extends Activity {
30 private final StringBuilder mConsoleLog = new StringBuilder();
31 private final Object mLock = new Object();
32 private static final String TEST_FINISHED_SENTINEL = "TEST FINISHED";
34 private WebView mWebView;
35 private boolean mFinished = false;
36 private boolean mGrantPermission = false;
38 private static final String[] AUTOMATICALLY_GRANT =
39 { PermissionRequest.RESOURCE_VIDEO_CAPTURE, PermissionRequest.RESOURCE_AUDIO_CAPTURE };
41 @Override
42 public void onCreate(Bundle savedInstanceState) {
43 super.onCreate(savedInstanceState);
44 setContentView(R.layout.activity_webview);
45 mWebView = (WebView) findViewById(R.id.webview);
46 WebSettings settings = mWebView.getSettings();
47 initializeSettings(settings);
49 mWebView.setWebViewClient(new WebViewClient() {
50 @Override
51 public boolean shouldOverrideUrlLoading(WebView webView, String url) {
52 return false;
55 @Override
56 public void onReceivedError(WebView view, int errorCode, String description,
57 String failingUrl) {
58 mConsoleLog.append("WebView error: " + description + ", " + failingUrl + "\n");
59 mConsoleLog.append(TEST_FINISHED_SENTINEL + "\n");
60 finishTest();
62 });
64 mWebView.setWebChromeClient(new WebChromeClient() {
65 @Override
66 public void onGeolocationPermissionsShowPrompt(String origin,
67 GeolocationPermissions.Callback callback) {
68 mConsoleLog.append("onGeolocationPermissionsShowPrompt" + "\n");
69 if (mGrantPermission) {
70 mConsoleLog.append("geolocation request granted" + "\n");
71 callback.invoke(origin, true /* allow */, false);
72 } else {
73 mConsoleLog.append("geolocation request denied" + "\n");
74 callback.invoke(origin, false /* allow */, false);
78 @Override
79 public void onPermissionRequest(PermissionRequest request) {
80 mConsoleLog.append("onPermissionRequest: "
81 + TextUtils.join(",", request.getResources()) + "\n");
82 if (mGrantPermission) {
83 mConsoleLog.append("request granted: "
84 + TextUtils.join(",", AUTOMATICALLY_GRANT) + "\n");
85 request.grant(AUTOMATICALLY_GRANT);
86 } else {
87 mConsoleLog.append("request denied" + "\n");
88 request.deny();
92 @Override
93 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
94 // TODO(timvolodine): put log and warnings in separate string builders.
95 mConsoleLog.append(consoleMessage.message() + "\n");
96 if (consoleMessage.message().equals(TEST_FINISHED_SENTINEL)) {
97 finishTest();
99 return true;
104 public void waitForFinish(long timeout, TimeUnit unit) throws InterruptedException,
105 TimeoutException {
106 synchronized (mLock) {
107 long deadline = System.currentTimeMillis() + unit.toMillis(timeout);
108 while (!mFinished && System.currentTimeMillis() < deadline) {
109 mLock.wait(deadline - System.currentTimeMillis());
111 if (!mFinished) {
112 throw new TimeoutException("timeout");
117 public String getTestResult() {
118 return mConsoleLog.toString();
121 public void loadUrl(String url) {
122 mWebView.loadUrl(url);
123 mWebView.requestFocus();
126 public void setGrantPermission(boolean allow) {
127 mGrantPermission = allow;
130 private void initializeSettings(WebSettings settings) {
131 settings.setJavaScriptEnabled(true);
132 settings.setGeolocationEnabled(true);
133 settings.setAllowFileAccess(true);
134 settings.setAllowFileAccessFromFileURLs(true);
137 private void finishTest() {
138 mFinished = true;
139 synchronized (mLock) {
140 mLock.notifyAll();