Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(285)

Side by Side Diff: android_webview/tools/WebViewShell/src/org/chromium/webview_shell/WebViewLayoutTest.java

Issue 1364593002: Refactor android_webview_shell and its tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.os.Environment;
8 import android.test.ActivityInstrumentationTestCase2;
9 import android.test.suitebuilder.annotation.MediumTest;
10
11 import junit.framework.ComparisonFailure;
12
13 import org.chromium.base.Log;
14
15 import java.io.BufferedReader;
16 import java.io.File;
17 import java.io.FileInputStream;
18 import java.io.FileNotFoundException;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.io.InputStreamReader;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.TimeoutException;
26
27 /**
28 * Tests running end-to-end layout tests.
29 */
30 public class WebViewLayoutTest
31 extends ActivityInstrumentationTestCase2<WebViewLayoutTestActivity> {
32
33 private static final String TAG = "WebViewLayoutTest";
34
35 private static final String EXTERNAL_PREFIX =
36 Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
37 private static final String BASE_WEBVIEW_TEST_PATH = "android_webview/tools/ WebViewShell/test/";
38 private static final String BASE_BLINK_TEST_PATH = "third_party/WebKit/Layou tTests/";
39 private static final String BASE_BLINK_STABLE_TEST_PATH =
40 BASE_BLINK_TEST_PATH + "virtual/stable/";
41 private static final String PATH_WEBVIEW_PREFIX = EXTERNAL_PREFIX + BASE_WEB VIEW_TEST_PATH;
42 private static final String PATH_BLINK_PREFIX = EXTERNAL_PREFIX + BASE_BLINK _TEST_PATH;
43 private static final String PATH_BLINK_STABLE_PREFIX =
44 EXTERNAL_PREFIX + BASE_BLINK_STABLE_TEST_PATH;
45
46 private static final long TIMEOUT_SECONDS = 20;
47
48 private WebViewLayoutTestActivity mTestActivity;
49
50 public WebViewLayoutTest() {
51 super(WebViewLayoutTestActivity.class);
52 }
53
54 @Override
55 protected void setUp() throws Exception {
56 super.setUp();
57 mTestActivity = (WebViewLayoutTestActivity) getActivity();
58 }
59
60 @Override
61 protected void tearDown() throws Exception {
62 mTestActivity.finish();
63 super.tearDown();
64 }
65
66 @Override
67 public WebViewLayoutTestRunner getInstrumentation() {
68 return (WebViewLayoutTestRunner) super.getInstrumentation();
69 }
70
71 @MediumTest
72 public void testSimple() throws Exception {
73 runWebViewLayoutTest("experimental/basic-logging.html",
74 "experimental/basic-logging-expected.txt");
75 }
76
77 // This is a non-failing test because it tends to require frequent rebaselin es.
78 @MediumTest
79 public void testGlobalInterfaceNoFail() throws Exception {
80 runBlinkLayoutTest("webexposed/global-interface-listing.html",
81 "webexposed/global-interface-listing-expected.txt", t rue);
82 }
83
84 @MediumTest
85 public void testNoUnexpectedInterfaces() throws Exception {
86 ensureJsTestCopied();
87 loadUrlWebViewAsync("file://" + PATH_BLINK_PREFIX
88 + "webexposed/global-interface-listing.html", mTestActivity);
89 String webviewExpected = readFile(PATH_WEBVIEW_PREFIX
90 + "webexposed/global-interface-listing-expected.txt");
91 mTestActivity.waitForFinish(TIMEOUT_SECONDS, TimeUnit.SECONDS);
92 String result = mTestActivity.getTestResult();
93
94 HashMap<String, HashSet<String>> webviewInterfacesMap = buildHashMap(res ult);
95 HashMap<String, HashSet<String>> webviewExpectedInterfacesMap =
96 buildHashMap(webviewExpected);
97 StringBuilder newInterfaces = new StringBuilder();
98
99 // Check that each current webview interface is one of webview expected interfaces.
100 for (String interfaceS : webviewInterfacesMap.keySet()) {
101 if (webviewExpectedInterfacesMap.get(interfaceS) == null) {
102 newInterfaces.append(interfaceS + "\n");
103 }
104 }
105 assertEquals("Unexpected new webview interfaces found", "", newInterface s.toString());
106 }
107
108 @MediumTest
109 public void testWebViewExcludedInterfaces() throws Exception {
110 ensureJsTestCopied();
111 loadUrlWebViewAsync("file://" + PATH_BLINK_PREFIX
112 + "webexposed/global-interface-listing.html", mTestActivity);
113 String blinkExpected = readFile(PATH_BLINK_PREFIX
114 + "webexposed/global-interface-listing-expected.txt");
115 String webviewExcluded = readFile(PATH_WEBVIEW_PREFIX
116 + "webexposed/not-webview-exposed.txt");
117 mTestActivity.waitForFinish(TIMEOUT_SECONDS, TimeUnit.SECONDS);
118 String result = mTestActivity.getTestResult();
119
120 HashMap<String, HashSet<String>> webviewExcludedInterfacesMap =
121 buildHashMap(webviewExcluded);
122 HashMap<String, HashSet<String>> webviewInterfacesMap = buildHashMap(res ult);
123 HashMap<String, HashSet<String>> blinkInterfacesMap = buildHashMap(blink Expected);
124 StringBuilder unexpected = new StringBuilder();
125
126 // Check that each excluded interface is present in blinkInterfacesMap b ut not
127 // in webviewInterfacesMap.
128 for (String interfaceS : webviewExcludedInterfacesMap.keySet()) {
129 assertNotNull("Interface " + interfaceS + " not exposed in blink",
130 blinkInterfacesMap.get(interfaceS));
131 if (webviewInterfacesMap.get(interfaceS) != null) {
132 unexpected.append(interfaceS + "\n");
133 }
134 }
135 assertEquals("Unexpected webview interfaces found", "", unexpected.toStr ing());
136 }
137
138 @MediumTest
139 public void testWebViewIncludedStableInterfaces() throws Exception {
140 ensureJsTestCopied();
141 loadUrlWebViewAsync("file://" + PATH_BLINK_PREFIX
142 + "webexposed/global-interface-listing.html", mTestActivity);
143 String blinkStableExpected = readFile(PATH_BLINK_STABLE_PREFIX
144 + "webexposed/global-interface-listing-expected.txt");
145 String webviewExcluded = readFile(PATH_WEBVIEW_PREFIX
146 + "webexposed/not-webview-exposed.txt");
147 mTestActivity.waitForFinish(TIMEOUT_SECONDS, TimeUnit.SECONDS);
148 String result = mTestActivity.getTestResult();
149
150 HashMap<String, HashSet<String>> webviewExcludedInterfacesMap =
151 buildHashMap(webviewExcluded);
152 HashMap<String, HashSet<String>> webviewInterfacesMap = buildHashMap(res ult);
153 HashMap<String, HashSet<String>> blinkStableInterfacesMap =
154 buildHashMap(blinkStableExpected);
155 StringBuilder missing = new StringBuilder();
156
157 // Check that each stable blink interface is present in webview except t he excluded
158 // interfaces.
159 for (String interfaceS : blinkStableInterfacesMap.keySet()) {
160 // TODO(timvolodine): consider implementing matching of subsets as w ell.
161 HashSet<String> subsetExcluded = webviewExcludedInterfacesMap.get(in terfaceS);
162 if (subsetExcluded != null && subsetExcluded.isEmpty()) continue;
163
164 if (webviewInterfacesMap.get(interfaceS) == null) {
165 missing.append(interfaceS + "\n");
166 }
167 }
168 assertEquals("Missing webview interfaces found", "", missing.toString()) ;
169 }
170
171 // Blink platform API tests
172
173 @MediumTest
174 public void testGeolocationCallbacks() throws Exception {
175 runWebViewLayoutTest("blink-apis/geolocation/geolocation-permission-call backs.html",
176 "blink-apis/geolocation/geolocation-permission-callbacks-expecte d.txt");
177 }
178
179 @MediumTest
180 public void testMediaStreamApiDenyPermission() throws Exception {
181 runWebViewLayoutTest("blink-apis/webrtc/mediastream-permission-denied-ca llbacks.html",
182 "blink-apis/webrtc/mediastream-permission-denied-callbacks-expec ted.txt");
183 }
184
185 @MediumTest
186 public void testMediaStreamApi() throws Exception {
187 mTestActivity.setGrantPermission(true);
188 runWebViewLayoutTest("blink-apis/webrtc/mediastream-callbacks.html",
189 "blink-apis/webrtc/mediastream-callbacks-expected.txt");
190 mTestActivity.setGrantPermission(false);
191 }
192
193 public void testBatteryApi() throws Exception {
194 runWebViewLayoutTest("blink-apis/battery-status/battery-callback.html",
195 "blink-apis/battery-status/battery-callback-expected.txt");
196 }
197
198 // test helper methods
199
200 private void runWebViewLayoutTest(final String fileName, final String fileNa meExpected)
201 throws Exception {
202 runTest(PATH_WEBVIEW_PREFIX + fileName, PATH_WEBVIEW_PREFIX + fileNameEx pected, false);
203 }
204
205 private void runBlinkLayoutTest(final String fileName, final String fileName Expected,
206 boolean noFail) throws Exception {
207 ensureJsTestCopied();
208 runTest(PATH_BLINK_PREFIX + fileName, PATH_WEBVIEW_PREFIX + fileNameExpe cted, noFail);
209 }
210
211 private void runTest(final String fileName, final String fileNameExpected, b oolean noFail)
212 throws FileNotFoundException, IOException, InterruptedException, Tim eoutException {
213 loadUrlWebViewAsync("file://" + fileName, mTestActivity);
214
215 if (getInstrumentation().isRebaseline()) {
216 // this is the rebaseline process
217 mTestActivity.waitForFinish(TIMEOUT_SECONDS, TimeUnit.SECONDS);
218 String result = mTestActivity.getTestResult();
219 writeFile(fileNameExpected, result, true);
220 Log.i(TAG, "file: " + fileNameExpected + " --> rebaselined, length=" + result.length());
221 } else {
222 String expected = readFile(fileNameExpected);
223 mTestActivity.waitForFinish(TIMEOUT_SECONDS, TimeUnit.SECONDS);
224 String result = mTestActivity.getTestResult();
225 if (noFail && !expected.equals(result)) {
226 ComparisonFailure cf = new ComparisonFailure("Unexpected result" , expected, result);
227 Log.e(TAG, cf.toString());
228 } else {
229 assertEquals(expected, result);
230 }
231 }
232 }
233
234 private void loadUrlWebViewAsync(final String fileUrl,
235 final WebViewLayoutTestActivity activity) {
236 getInstrumentation().runOnMainSync(new Runnable() {
237 @Override
238 public void run() {
239 activity.loadUrl(fileUrl);
240 }
241 });
242 }
243
244 private static void ensureJsTestCopied() throws IOException {
245 File jsTestFile = new File(PATH_BLINK_PREFIX + "resources/js-test.js");
246 if (jsTestFile.exists()) return;
247 String original = readFile(PATH_WEBVIEW_PREFIX + "resources/js-test.js") ;
248 writeFile(PATH_BLINK_PREFIX + "resources/js-test.js", original, false);
249 }
250
251 /**
252 * Reads a file and returns it's contents as string.
253 */
254 private static String readFile(String fileName) throws IOException {
255 FileInputStream inputStream = new FileInputStream(new File(fileName));
256 try {
257 BufferedReader reader = new BufferedReader(new InputStreamReader(inp utStream));
258 try {
259 StringBuilder contents = new StringBuilder();
260 String line;
261
262 while ((line = reader.readLine()) != null) {
263 contents.append(line);
264 contents.append("\n");
265 }
266 return contents.toString();
267 } finally {
268 reader.close();
269 }
270 } finally {
271 inputStream.close();
272 }
273 }
274
275 /**
276 * Writes a file with the given fileName and contents. If overwrite is true overwrites any
277 * exisiting file with the same file name. If the file does not exist any in termediate
278 * required directories are created.
279 */
280 private static void writeFile(final String fileName, final String contents, boolean overwrite)
281 throws FileNotFoundException, IOException {
282 File fileOut = new File(fileName);
283
284 if (fileOut.exists() && !overwrite) {
285 return;
286 }
287
288 String absolutePath = fileOut.getAbsolutePath();
289 File filePath = new File(absolutePath.substring(0, absolutePath.lastInde xOf("/")));
290
291 if (!filePath.exists()) {
292 if (!filePath.mkdirs())
293 throw new IOException("failed to create directories: " + filePat h);
294 }
295
296 FileOutputStream outputStream = new FileOutputStream(fileOut);
297 try {
298 outputStream.write(contents.getBytes());
299 } finally {
300 outputStream.close();
301 }
302 }
303
304 private HashMap<String, HashSet<String>> buildHashMap(String contents) {
305 String[] lineByLine = contents.split("\\n");
306
307 HashSet subset = null;
308 HashMap<String, HashSet<String>> interfaces = new HashMap<String, HashSe t<String>>();
309 for (String line : lineByLine) {
310 String s = trimAndRemoveComments(line);
311 if (s.startsWith("interface")) {
312 subset = new HashSet();
313 interfaces.put(s, subset);
314 } else if (interfaceProperty(s) && subset != null) {
315 subset.add(s);
316 }
317 }
318 return interfaces;
319 }
320
321 private String trimAndRemoveComments(String line) {
322 String s = line.trim();
323 int commentIndex = s.indexOf("#"); // remove any potential comments
324 return (commentIndex >= 0) ? s.substring(0, commentIndex).trim() : s;
325 }
326
327 private boolean interfaceProperty(String s) {
328 return s.startsWith("getter") || s.startsWith("setter")
329 || s.startsWith("method") || s.startsWith("attribute");
330 }
331
332 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698