OLD | NEW |
| (Empty) |
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. | |
4 | |
5 package org.chromium.webview_shell; | |
6 | |
7 import android.app.Activity; | |
8 import android.os.Bundle; | |
9 import android.text.TextUtils; | |
10 | |
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; | |
18 | |
19 import java.util.concurrent.TimeUnit; | |
20 import java.util.concurrent.TimeoutException; | |
21 | |
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. | |
27 */ | |
28 public class WebViewLayoutTestActivity extends Activity { | |
29 | |
30 private final StringBuilder mConsoleLog = new StringBuilder(); | |
31 private final Object mLock = new Object(); | |
32 private static final String TEST_FINISHED_SENTINEL = "TEST FINISHED"; | |
33 | |
34 private WebView mWebView; | |
35 private boolean mFinished = false; | |
36 private boolean mGrantPermission = false; | |
37 | |
38 @Override | |
39 public void onCreate(Bundle savedInstanceState) { | |
40 super.onCreate(savedInstanceState); | |
41 setContentView(R.layout.activity_webview); | |
42 mWebView = (WebView) findViewById(R.id.webview); | |
43 WebSettings settings = mWebView.getSettings(); | |
44 initializeSettings(settings); | |
45 | |
46 mWebView.setWebViewClient(new WebViewClient() { | |
47 @Override | |
48 public boolean shouldOverrideUrlLoading(WebView webView, String url)
{ | |
49 return false; | |
50 } | |
51 | |
52 @Override | |
53 public void onReceivedError(WebView view, int errorCode, String desc
ription, | |
54 String failingUrl) { | |
55 mConsoleLog.append("WebView error: " + description + ", " + fail
ingUrl + "\n"); | |
56 mConsoleLog.append(TEST_FINISHED_SENTINEL + "\n"); | |
57 finishTest(); | |
58 } | |
59 }); | |
60 | |
61 mWebView.setWebChromeClient(new WebChromeClient() { | |
62 @Override | |
63 public void onGeolocationPermissionsShowPrompt(String origin, | |
64 GeolocationPermissions.Callback callback) { | |
65 mConsoleLog.append("onGeolocationPermissionsShowPrompt" + "\n"); | |
66 if (mGrantPermission) { | |
67 mConsoleLog.append("geolocation request granted" + "\n"); | |
68 callback.invoke(origin, true /* allow */, false); | |
69 } else { | |
70 mConsoleLog.append("geolocation request denied" + "\n"); | |
71 callback.invoke(origin, false /* allow */, false); | |
72 } | |
73 } | |
74 | |
75 @Override | |
76 public void onPermissionRequest(PermissionRequest request) { | |
77 mConsoleLog.append("onPermissionRequest: " | |
78 + TextUtils.join(",", request.getResources()) + "\n"); | |
79 if (mGrantPermission) { | |
80 mConsoleLog.append("request granted: " | |
81 + TextUtils.join(",", request.getResources()) + "\n"
); | |
82 request.grant(request.getResources()); | |
83 } else { | |
84 mConsoleLog.append("request denied" + "\n"); | |
85 request.deny(); | |
86 } | |
87 } | |
88 | |
89 @Override | |
90 public boolean onConsoleMessage(ConsoleMessage consoleMessage) { | |
91 // TODO(timvolodine): put log and warnings in separate string bu
ilders. | |
92 mConsoleLog.append(consoleMessage.message() + "\n"); | |
93 if (consoleMessage.message().equals(TEST_FINISHED_SENTINEL)) { | |
94 finishTest(); | |
95 } | |
96 return true; | |
97 } | |
98 }); | |
99 } | |
100 | |
101 public void waitForFinish(long timeout, TimeUnit unit) throws InterruptedExc
eption, | |
102 TimeoutException { | |
103 synchronized (mLock) { | |
104 long deadline = System.currentTimeMillis() + unit.toMillis(timeout); | |
105 while (!mFinished && System.currentTimeMillis() < deadline) { | |
106 mLock.wait(deadline - System.currentTimeMillis()); | |
107 } | |
108 if (!mFinished) { | |
109 throw new TimeoutException("timeout"); | |
110 } | |
111 } | |
112 } | |
113 | |
114 public String getTestResult() { | |
115 return mConsoleLog.toString(); | |
116 } | |
117 | |
118 public void loadUrl(String url) { | |
119 mWebView.loadUrl(url); | |
120 mWebView.requestFocus(); | |
121 } | |
122 | |
123 public void setGrantPermission(boolean allow) { | |
124 mGrantPermission = allow; | |
125 } | |
126 | |
127 private void initializeSettings(WebSettings settings) { | |
128 settings.setJavaScriptEnabled(true); | |
129 settings.setGeolocationEnabled(true); | |
130 settings.setAllowFileAccess(true); | |
131 settings.setAllowFileAccessFromFileURLs(true); | |
132 } | |
133 | |
134 private void finishTest() { | |
135 mFinished = true; | |
136 synchronized (mLock) { | |
137 mLock.notifyAll(); | |
138 } | |
139 } | |
140 } | |
OLD | NEW |