| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.webview_shell; | 5 package org.chromium.webview_shell; |
| 6 | 6 |
| 7 import android.os.Environment; |
| 7 import android.test.ActivityInstrumentationTestCase2; | 8 import android.test.ActivityInstrumentationTestCase2; |
| 8 | 9 |
| 9 import org.chromium.base.Log; | 10 import org.chromium.base.Log; |
| 10 | 11 |
| 11 import java.io.BufferedReader; | 12 import java.io.BufferedReader; |
| 12 import java.io.File; | 13 import java.io.File; |
| 13 import java.io.FileInputStream; | 14 import java.io.FileInputStream; |
| 14 import java.io.FileNotFoundException; | 15 import java.io.FileNotFoundException; |
| 15 import java.io.FileOutputStream; | 16 import java.io.FileOutputStream; |
| 16 import java.io.IOException; | 17 import java.io.IOException; |
| 17 import java.io.InputStreamReader; | 18 import java.io.InputStreamReader; |
| 18 import java.util.concurrent.TimeUnit; | 19 import java.util.concurrent.TimeUnit; |
| 19 import java.util.concurrent.TimeoutException; | 20 import java.util.concurrent.TimeoutException; |
| 20 | 21 |
| 21 /** | 22 /** |
| 22 * Tests running end-to-end layout tests. | 23 * Tests running end-to-end layout tests. |
| 23 */ | 24 */ |
| 24 public class WebViewLayoutTest | 25 public class WebViewLayoutTest |
| 25 extends ActivityInstrumentationTestCase2<WebViewLayoutTestActivity> { | 26 extends ActivityInstrumentationTestCase2<WebViewLayoutTestActivity> { |
| 26 | 27 |
| 27 private static final String TAG = "WebViewLayoutTest"; | 28 private static final String TAG = "WebViewLayoutTest"; |
| 28 private static final String PATH_PREFIX = "/data/local/tmp/webview_test/"; | 29 |
| 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/Layou
tTests/"; |
| 34 private static final String PATH_WEBVIEW_PREFIX = EXTERNAL_PREFIX + BASE_WEB
VIEW_TEST_PATH; |
| 35 private static final String PATH_BLINK_PREFIX = EXTERNAL_PREFIX + BASE_BLINK
_TEST_PATH; |
| 36 |
| 29 private static final long TIMEOUT_SECONDS = 20; | 37 private static final long TIMEOUT_SECONDS = 20; |
| 38 |
| 30 private WebViewLayoutTestActivity mTestActivity; | 39 private WebViewLayoutTestActivity mTestActivity; |
| 31 | 40 |
| 32 public WebViewLayoutTest() { | 41 public WebViewLayoutTest() { |
| 33 super(WebViewLayoutTestActivity.class); | 42 super(WebViewLayoutTestActivity.class); |
| 34 } | 43 } |
| 35 | 44 |
| 36 @Override | 45 @Override |
| 37 protected void setUp() throws Exception { | 46 protected void setUp() throws Exception { |
| 38 super.setUp(); | 47 super.setUp(); |
| 39 mTestActivity = (WebViewLayoutTestActivity) getActivity(); | 48 mTestActivity = (WebViewLayoutTestActivity) getActivity(); |
| 40 } | 49 } |
| 41 | 50 |
| 42 @Override | 51 @Override |
| 43 protected void tearDown() throws Exception { | 52 protected void tearDown() throws Exception { |
| 44 mTestActivity.finish(); | 53 mTestActivity.finish(); |
| 45 super.tearDown(); | 54 super.tearDown(); |
| 46 } | 55 } |
| 47 | 56 |
| 48 @Override | 57 @Override |
| 49 public WebViewLayoutTestRunner getInstrumentation() { | 58 public WebViewLayoutTestRunner getInstrumentation() { |
| 50 return (WebViewLayoutTestRunner) super.getInstrumentation(); | 59 return (WebViewLayoutTestRunner) super.getInstrumentation(); |
| 51 } | 60 } |
| 52 | 61 |
| 53 public void testSimple() throws Exception { | 62 public void testSimple() throws Exception { |
| 54 runTest("experimental/basic-logging.html", "experimental/basic-logging-e
xpected.txt"); | 63 runWebViewLayoutTest("experimental/basic-logging.html", |
| 64 "experimental/basic-logging-expected.txt"); |
| 55 } | 65 } |
| 56 | 66 |
| 57 public void testGlobalInterface() throws Exception { | 67 public void testGlobalInterface() throws Exception { |
| 58 runTest("webexposed/global-interface-listing.html", | 68 runBlinkLayoutTest("webexposed/global-interface-listing.html", |
| 59 "webexposed/global-interface-listing-expected.txt"); | 69 "webexposed/global-interface-listing-expected.txt"); |
| 60 } | 70 } |
| 61 | 71 |
| 62 // test helper methods | 72 // test helper methods |
| 73 |
| 74 private void runWebViewLayoutTest(final String fileName, final String fileNa
meExpected) |
| 75 throws Exception { |
| 76 runTest(PATH_WEBVIEW_PREFIX + fileName, PATH_WEBVIEW_PREFIX + fileNameEx
pected); |
| 77 } |
| 78 |
| 79 private void runBlinkLayoutTest(final String fileName, final String fileName
Expected) |
| 80 throws Exception { |
| 81 ensureJsTestCopied(); |
| 82 runTest(PATH_BLINK_PREFIX + fileName, PATH_WEBVIEW_PREFIX + fileNameExpe
cted); |
| 83 } |
| 84 |
| 63 private void runTest(final String fileName, final String fileNameExpected) | 85 private void runTest(final String fileName, final String fileNameExpected) |
| 64 throws FileNotFoundException, IOException, InterruptedException, Tim
eoutException { | 86 throws FileNotFoundException, IOException, InterruptedException, Tim
eoutException { |
| 65 loadUrlWebViewAsync("file://" + PATH_PREFIX + fileName, mTestActivity); | 87 loadUrlWebViewAsync("file://" + fileName, mTestActivity); |
| 66 | 88 |
| 67 if (getInstrumentation().isRebaseline()) { | 89 if (getInstrumentation().isRebaseline()) { |
| 68 // this is the rebase line process; | 90 // this is the rebaseline process |
| 69 mTestActivity.waitForFinish(TIMEOUT_SECONDS, TimeUnit.SECONDS); | 91 mTestActivity.waitForFinish(TIMEOUT_SECONDS, TimeUnit.SECONDS); |
| 70 String result = mTestActivity.getTestResult(); | 92 String result = mTestActivity.getTestResult(); |
| 71 writeFile(fileNameExpected, result, mTestActivity.getFilesDir()); | 93 writeFile(fileNameExpected, result, true); |
| 72 Log.i(TAG, "file: " + fileNameExpected + " --> rebaselined, length="
+ result.length()); | 94 Log.i(TAG, "file: " + fileNameExpected + " --> rebaselined, length="
+ result.length()); |
| 73 } else { | 95 } else { |
| 74 String expected = readFile(PATH_PREFIX + fileNameExpected); | 96 String expected = readFile(fileNameExpected); |
| 75 mTestActivity.waitForFinish(TIMEOUT_SECONDS, TimeUnit.SECONDS); | 97 mTestActivity.waitForFinish(TIMEOUT_SECONDS, TimeUnit.SECONDS); |
| 76 String result = mTestActivity.getTestResult(); | 98 String result = mTestActivity.getTestResult(); |
| 77 assertEquals(expected, result); | 99 assertEquals(expected, result); |
| 78 } | 100 } |
| 79 } | 101 } |
| 80 | 102 |
| 81 private void loadUrlWebViewAsync(final String fileUrl, | 103 private void loadUrlWebViewAsync(final String fileUrl, |
| 82 final WebViewLayoutTestActivity activity) { | 104 final WebViewLayoutTestActivity activity) { |
| 83 getInstrumentation().runOnMainSync(new Runnable() { | 105 getInstrumentation().runOnMainSync(new Runnable() { |
| 84 @Override | 106 @Override |
| 85 public void run() { | 107 public void run() { |
| 86 activity.loadUrl(fileUrl); | 108 activity.loadUrl(fileUrl); |
| 87 } | 109 } |
| 88 }); | 110 }); |
| 89 } | 111 } |
| 90 | 112 |
| 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); |
| 118 } |
| 119 |
| 120 /** |
| 121 * Reads a file and returns it's contents as string. |
| 122 */ |
| 91 private static String readFile(String fileName) throws IOException { | 123 private static String readFile(String fileName) throws IOException { |
| 92 FileInputStream inputStream = new FileInputStream(new File(fileName)); | 124 FileInputStream inputStream = new FileInputStream(new File(fileName)); |
| 93 try { | 125 try { |
| 94 BufferedReader reader = new BufferedReader(new InputStreamReader(inp
utStream)); | 126 BufferedReader reader = new BufferedReader(new InputStreamReader(inp
utStream)); |
| 95 try { | 127 try { |
| 96 StringBuilder contents = new StringBuilder(); | 128 StringBuilder contents = new StringBuilder(); |
| 97 String line; | 129 String line; |
| 98 | 130 |
| 99 while ((line = reader.readLine()) != null) { | 131 while ((line = reader.readLine()) != null) { |
| 100 contents.append(line); | 132 contents.append(line); |
| 101 contents.append("\n"); | 133 contents.append("\n"); |
| 102 } | 134 } |
| 103 return contents.toString(); | 135 return contents.toString(); |
| 104 } finally { | 136 } finally { |
| 105 reader.close(); | 137 reader.close(); |
| 106 } | 138 } |
| 107 } finally { | 139 } finally { |
| 108 inputStream.close(); | 140 inputStream.close(); |
| 109 } | 141 } |
| 110 } | 142 } |
| 111 | 143 |
| 112 private static void writeFile(final String fileName, final String contents, | 144 /** |
| 113 final File internalFilesDir) throws FileNotFoundException, IOExcepti
on { | 145 * Writes a file with the given fileName and contents. If overwrite is true
overwrites any |
| 114 File fileOut = new File(internalFilesDir, fileName); | 146 * exisiting file with the same file name. If the file does not exist any in
termediate |
| 147 * required directories are created. |
| 148 */ |
| 149 private static void writeFile(final String fileName, final String contents,
boolean overwrite) |
| 150 throws FileNotFoundException, IOException { |
| 151 File fileOut = new File(fileName); |
| 152 |
| 153 if (fileOut.exists() && !overwrite) { |
| 154 return; |
| 155 } |
| 156 |
| 115 String absolutePath = fileOut.getAbsolutePath(); | 157 String absolutePath = fileOut.getAbsolutePath(); |
| 116 String path = absolutePath.substring(0, absolutePath.lastIndexOf("/")); | 158 File filePath = new File(absolutePath.substring(0, absolutePath.lastInde
xOf("/"))); |
| 117 boolean mkdirsSuccess = new File(path).mkdirs(); | 159 |
| 118 if (!mkdirsSuccess) | 160 if (!filePath.exists()) { |
| 119 throw new IOException("failed to create directories: " + path); | 161 if (!filePath.mkdirs()) |
| 162 throw new IOException("failed to create directories: " + filePat
h); |
| 163 } |
| 120 | 164 |
| 121 FileOutputStream outputStream = new FileOutputStream(fileOut); | 165 FileOutputStream outputStream = new FileOutputStream(fileOut); |
| 122 try { | 166 try { |
| 123 outputStream.write(contents.getBytes()); | 167 outputStream.write(contents.getBytes()); |
| 124 } finally { | 168 } finally { |
| 125 outputStream.close(); | 169 outputStream.close(); |
| 126 } | 170 } |
| 127 } | 171 } |
| 128 } | 172 } |
| OLD | NEW |