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

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

Powered by Google App Engine
This is Rietveld 408576698