Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / android_webview / tools / WebViewShell / src / org / chromium / webview_shell / WebViewLayoutTest.java
blob9224260317d272fad1494f0f3641557a6ab713b5
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.os.Environment;
8 import android.test.ActivityInstrumentationTestCase2;
10 import org.chromium.base.Log;
12 import java.io.BufferedReader;
13 import java.io.File;
14 import java.io.FileInputStream;
15 import java.io.FileNotFoundException;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18 import java.io.InputStreamReader;
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.TimeoutException;
22 /**
23 * Tests running end-to-end layout tests.
25 public class WebViewLayoutTest
26 extends ActivityInstrumentationTestCase2<WebViewLayoutTestActivity> {
28 private static final String TAG = "WebViewLayoutTest";
30 private static final String EXTERNAL_PREFIX =
31 Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
32 private static final String BASE_WEBVIEW_TEST_PATH = "android_webview/tools/WebViewShell/test/";
33 private static final String BASE_BLINK_TEST_PATH = "third_party/WebKit/LayoutTests/";
34 private static final String PATH_WEBVIEW_PREFIX = EXTERNAL_PREFIX + BASE_WEBVIEW_TEST_PATH;
35 private static final String PATH_BLINK_PREFIX = EXTERNAL_PREFIX + BASE_BLINK_TEST_PATH;
37 private static final long TIMEOUT_SECONDS = 20;
39 private WebViewLayoutTestActivity mTestActivity;
41 public WebViewLayoutTest() {
42 super(WebViewLayoutTestActivity.class);
45 @Override
46 protected void setUp() throws Exception {
47 super.setUp();
48 mTestActivity = (WebViewLayoutTestActivity) getActivity();
51 @Override
52 protected void tearDown() throws Exception {
53 mTestActivity.finish();
54 super.tearDown();
57 @Override
58 public WebViewLayoutTestRunner getInstrumentation() {
59 return (WebViewLayoutTestRunner) super.getInstrumentation();
62 public void testSimple() throws Exception {
63 runWebViewLayoutTest("experimental/basic-logging.html",
64 "experimental/basic-logging-expected.txt");
67 public void testGlobalInterface() throws Exception {
68 runBlinkLayoutTest("webexposed/global-interface-listing.html",
69 "webexposed/global-interface-listing-expected.txt");
72 // test helper methods
74 private void runWebViewLayoutTest(final String fileName, final String fileNameExpected)
75 throws Exception {
76 runTest(PATH_WEBVIEW_PREFIX + fileName, PATH_WEBVIEW_PREFIX + fileNameExpected);
79 private void runBlinkLayoutTest(final String fileName, final String fileNameExpected)
80 throws Exception {
81 ensureJsTestCopied();
82 runTest(PATH_BLINK_PREFIX + fileName, PATH_WEBVIEW_PREFIX + fileNameExpected);
85 private void runTest(final String fileName, final String fileNameExpected)
86 throws FileNotFoundException, IOException, InterruptedException, TimeoutException {
87 loadUrlWebViewAsync("file://" + fileName, mTestActivity);
89 if (getInstrumentation().isRebaseline()) {
90 // this is the rebaseline process
91 mTestActivity.waitForFinish(TIMEOUT_SECONDS, TimeUnit.SECONDS);
92 String result = mTestActivity.getTestResult();
93 writeFile(fileNameExpected, result, true);
94 Log.i(TAG, "file: " + fileNameExpected + " --> rebaselined, length=" + result.length());
95 } else {
96 String expected = readFile(fileNameExpected);
97 mTestActivity.waitForFinish(TIMEOUT_SECONDS, TimeUnit.SECONDS);
98 String result = mTestActivity.getTestResult();
99 assertEquals(expected, result);
103 private void loadUrlWebViewAsync(final String fileUrl,
104 final WebViewLayoutTestActivity activity) {
105 getInstrumentation().runOnMainSync(new Runnable() {
106 @Override
107 public void run() {
108 activity.loadUrl(fileUrl);
113 private static void ensureJsTestCopied() throws IOException {
114 File jsTestFile = new File(PATH_BLINK_PREFIX + "resources/js-test.js");
115 if (jsTestFile.exists()) return;
116 String original = readFile(PATH_WEBVIEW_PREFIX + "resources/js-test.js");
117 writeFile(PATH_BLINK_PREFIX + "resources/js-test.js", original, false);
121 * Reads a file and returns it's contents as string.
123 private static String readFile(String fileName) throws IOException {
124 FileInputStream inputStream = new FileInputStream(new File(fileName));
125 try {
126 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
127 try {
128 StringBuilder contents = new StringBuilder();
129 String line;
131 while ((line = reader.readLine()) != null) {
132 contents.append(line);
133 contents.append("\n");
135 return contents.toString();
136 } finally {
137 reader.close();
139 } finally {
140 inputStream.close();
145 * Writes a file with the given fileName and contents. If overwrite is true overwrites any
146 * exisiting file with the same file name. If the file does not exist any intermediate
147 * required directories are created.
149 private static void writeFile(final String fileName, final String contents, boolean overwrite)
150 throws FileNotFoundException, IOException {
151 File fileOut = new File(fileName);
153 if (fileOut.exists() && !overwrite) {
154 return;
157 String absolutePath = fileOut.getAbsolutePath();
158 File filePath = new File(absolutePath.substring(0, absolutePath.lastIndexOf("/")));
160 if (!filePath.exists()) {
161 if (!filePath.mkdirs())
162 throw new IOException("failed to create directories: " + filePath);
165 FileOutputStream outputStream = new FileOutputStream(fileOut);
166 try {
167 outputStream.write(contents.getBytes());
168 } finally {
169 outputStream.close();