pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 1 | # Writing Layout Tests |
| 2 | |
| 3 | _Layout tests_ is a bit of a misnomer. This term is |
| 4 | [a part of our WebKit heritage](https://webkit.org/blog/1452/layout-tests-theory/), |
| 5 | and we use it to refer to every test that is written as a Web page (HTML, SVG, |
| 6 | or XHTML) and lives in |
| 7 | [third_party/WebKit/LayoutTests/](../../third_party/WebKit/LayoutTests). |
| 8 | |
| 9 | [TOC] |
| 10 | |
| 11 | ## Overview |
| 12 | |
| 13 | Layout tests should be used to accomplish one of the following goals: |
| 14 | |
| 15 | 1. The entire surface of Blink that is exposed to the Web should be covered by |
foolip | eda32ab | 2017-02-16 19:21:58 | [diff] [blame] | 16 | tests that we contribute to [web-platform-tests](./web_platform_tests.md) |
| 17 | (WPT). This helps us avoid regressions, and helps us identify Web Platform |
| 18 | areas where the major browsers don't have interoperable implementations. |
| 19 | Furthermore, by contributing to projects such as WPT, we share the burden of |
| 20 | writing tests with the other browser vendors, and we help all the browsers |
| 21 | get better. This is very much in line with our goal to move the Web forward. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 22 | 2. When a Blink feature cannot be tested using the tools provided by WPT, and |
| 23 | cannot be easily covered by |
Kent Tamura | 6943cf79 | 2018-04-09 05:24:54 | [diff] [blame] | 24 | [C++ unit tests](https://cs.chromium.org/chromium/src/third_party/blink/renderer/web/tests/?q=webframetest&sq=package:chromium&type=cs), |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 25 | the feature must be covered by layout tests, to avoid unexpected regressions. |
| 26 | These tests will use Blink-specific testing APIs that are only available in |
| 27 | [content_shell](./layout_tests_in_content_shell.md). |
| 28 | |
| 29 | *** promo |
| 30 | If you know that Blink layout tests are upstreamed to other projects, such as |
| 31 | [test262](https://github.com/tc39/test262), please update this document. Most |
| 32 | importantly, our guidelines should to make it easy for our tests to be |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 33 | upstreamed. The |
| 34 | [blink-dev mailing list](https://groups.google.com/a/chromium.org/forum/#!forum/blink-dev) |
| 35 | will be happy to help you harmonize our current guidelines with communal test |
| 36 | repositories. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 37 | *** |
| 38 | |
| 39 | ### Test Types |
| 40 | |
| 41 | There are four broad types of layout tests, listed in the order of preference. |
| 42 | |
| 43 | * *JavaScript Tests* are the layout test implementation of |
| 44 | [xUnit tests](https://en.wikipedia.org/wiki/XUnit). These tests contain |
| 45 | assertions written in JavaScript, and pass if the assertions evaluate to |
| 46 | true. |
| 47 | * *Reference Tests* render a test page and a reference page, and pass if the two |
| 48 | renderings are identical, according to a pixel-by-pixel comparison. These |
| 49 | tests are less robust, harder to debug, and significantly slower than |
| 50 | JavaScript tests, and are only used when JavaScript tests are insufficient, |
| 51 | such as when testing paint code. |
| 52 | * *Pixel Tests* render a test page and compare the result against a pre-rendered |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 53 | baseline image in the repository. Pixel tests are less robust than the |
| 54 | first two types, because the rendering of a page is influenced by |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 55 | many factors such as the host computer's graphics card and driver, the |
| 56 | platform's text rendering system, and various user-configurable operating |
| 57 | system settings. For this reason, it is common for a pixel test to have a |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 58 | different reference image for each platform that Blink is tested on, and |
| 59 | the reference images are |
| 60 | [quite cumbersome to manage](./layout_test_expectations.md). You |
| 61 | should only write a pixel test if you cannot use a reference test. By default |
| 62 | a pixel test will also dump the layout tree as text output, so they are |
| 63 | similar to ... |
| 64 | * *Layout tree tests*, which output a textual representation of the layout |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 65 | tree, which is the key data structure in Blink's page rendering system. The |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 66 | test passes if the output matches a baseline text file in the repository. |
| 67 | Layout tree tests are used as a last resort to test the internal quirks of |
| 68 | the implementation, and they should be avoided in favor of one of the earlier |
| 69 | options. |
pwnall | 59aadcb | 2017-01-26 23:27:21 | [diff] [blame] | 70 | |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 71 | ## General Principles |
| 72 | |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 73 | |
| 74 | Tests should be written under the assumption that they will be upstreamed |
pwnall | 59aadcb | 2017-01-26 23:27:21 | [diff] [blame] | 75 | to the WPT project. To this end, tests should follow the |
foolip | eda32ab | 2017-02-16 19:21:58 | [diff] [blame] | 76 | [WPT guidelines](http://web-platform-tests.org/writing-tests/). |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 77 | |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 78 | |
pwnall | 59aadcb | 2017-01-26 23:27:21 | [diff] [blame] | 79 | There is no style guide that applies to all layout tests. However, some projects |
| 80 | have adopted style guides, such as the |
| 81 | [ServiceWorker Tests Style guide](https://www.chromium.org/blink/serviceworker/testing). |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 82 | |
pwnall | 59aadcb | 2017-01-26 23:27:21 | [diff] [blame] | 83 | Our [document on layout tests tips](./layout_tests_tips.md) summarizes the most |
| 84 | important WPT guidelines and highlights some JavaScript concepts that are worth |
| 85 | paying attention to when trying to infer style rules from existing tests. If |
| 86 | you're unopinionated and looking for a style guide to follow, the document also |
| 87 | suggests some defaults. |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 88 | |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 89 | ## JavaScript Tests |
| 90 | |
| 91 | Whenever possible, the testing criteria should be expressed in JavaScript. The |
| 92 | alternatives, which will be described in future sections, result in slower and |
| 93 | less reliable tests. |
| 94 | |
| 95 | All new JavaScript tests should be written using the |
mek | b330e31 | 2017-05-03 19:56:22 | [diff] [blame] | 96 | [testharness.js](https://github.com/w3c/web-platform-tests/tree/master/resources) |
| 97 | testing framework. This framework is used by the tests in the |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 98 | [web-platform-tests](https://github.com/w3c/web-platform-tests) repository, |
| 99 | which is shared with all the other browser vendors, so `testharness.js` tests |
| 100 | are more accessible to browser developers. |
| 101 | |
mek | b330e31 | 2017-05-03 19:56:22 | [diff] [blame] | 102 | See the [API documentation](http://web-platform-tests.org/writing-tests/testharness-api.html) |
foolip | eda32ab | 2017-02-16 19:21:58 | [diff] [blame] | 103 | for a thorough introduction to `testharness.js`. |
| 104 | |
| 105 | Layout tests should follow the recommendations of the above documentation. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 106 | Furthermore, layout tests should include relevant |
foolip | eda32ab | 2017-02-16 19:21:58 | [diff] [blame] | 107 | [metadata](http://web-platform-tests.org/writing-tests/css-metadata.html). The |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 108 | specification URL (in `<link rel="help">`) is almost always relevant, and is |
| 109 | incredibly helpful to a developer who needs to understand the test quickly. |
| 110 | |
| 111 | Below is a skeleton for a JavaScript test embedded in an HTML page. Note that, |
| 112 | in order to follow the minimality guideline, the test omits the tags `<html>`, |
| 113 | `<head>`, and `<body>`, as they can be inferred by the HTML parser. |
| 114 | |
| 115 | ```html |
| 116 | <!doctype html> |
pwnall | 59aadcb | 2017-01-26 23:27:21 | [diff] [blame] | 117 | <title>JavaScript: the true literal is immutable and equal to itself</title> |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 118 | <link rel="help" href="https://tc39.github.io/ecma262/#sec-boolean-literals"> |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 119 | <script src="/resources/testharness.js"></script> |
| 120 | <script src="/resources/testharnessreport.js"></script> |
| 121 | <script> |
| 122 | 'use strict'; |
| 123 | |
| 124 | // Synchronous test example. |
| 125 | test(() => { |
| 126 | const value = true; |
| 127 | assert_true(value, 'true literal'); |
| 128 | assert_equals(value.toString(), 'true', 'the string representation of true'); |
| 129 | }, 'The literal true in a synchronous test case'); |
| 130 | |
| 131 | // Asynchronous test example. |
| 132 | async_test(t => { |
| 133 | const originallyTrue = true; |
| 134 | setTimeout(t.step_func_done(() => { |
| 135 | assert_equals(originallyTrue, true); |
| 136 | }), 0); |
| 137 | }, 'The literal true in a setTimeout callback'); |
| 138 | |
| 139 | // Promise test example. |
| 140 | promise_test(() => { |
| 141 | return new Promise((resolve, reject) => { |
| 142 | resolve(true); |
| 143 | }).then(value => { |
| 144 | assert_true(value); |
| 145 | }); |
| 146 | }, 'The literal true used to resolve a Promise'); |
| 147 | |
| 148 | </script> |
| 149 | ``` |
| 150 | |
| 151 | Some points that are not immediately obvious from the example: |
| 152 | |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 153 | * When calling an `assert_` function that compares two values, the first |
| 154 | argument is the actual value (produced by the functionality being tested), and |
| 155 | the second argument is the expected value (known good, golden). The order |
| 156 | is important, because the testing harness relies on it to generate expressive |
| 157 | error messages that are relied upon when debugging test failures. |
| 158 | * The assertion description (the string argument to `assert_` methods) conveys |
| 159 | the way the actual value was obtained. |
| 160 | * If the expected value doesn't make it clear, the assertion description |
| 161 | should explain the desired behavior. |
| 162 | * Test cases with a single assertion should omit the assertion's description |
| 163 | when it is sufficiently clear. |
| 164 | * Each test case describes the circumstance that it tests, without being |
| 165 | redundant. |
| 166 | * Do not start test case descriptions with redundant terms like "Testing" |
| 167 | or "Test for". |
ktyliu | e0bb988 | 2017-01-10 01:47:50 | [diff] [blame] | 168 | * Test files with a single test case should omit the test case description. |
| 169 | The file's `<title>` should be sufficient to describe the scenario being |
| 170 | tested. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 171 | * Asynchronous tests have a few subtleties. |
| 172 | * The `async_test` wrapper calls its function with a test case argument that |
| 173 | is used to signal when the test case is done, and to connect assertion |
| 174 | failures to the correct test. |
| 175 | * `t.done()` must be called after all the test case's assertions have |
| 176 | executed. |
| 177 | * Test case assertions (actually, any callback code that can throw |
| 178 | exceptions) must be wrapped in `t.step_func()` calls, so that |
| 179 | assertion failures and exceptions can be traced back to the correct test |
| 180 | case. |
| 181 | * `t.step_func_done()` is a shortcut that combines `t.step_func()` with a |
| 182 | `t.done()` call. |
| 183 | |
| 184 | *** promo |
| 185 | Layout tests that load from `file://` origins must currently use relative paths |
| 186 | to point to |
| 187 | [/resources/testharness.js](../../third_party/WebKit/LayoutTests/resources/testharness.js) |
| 188 | and |
| 189 | [/resources/testharnessreport.js](../../third_party/WebKit/LayoutTests/resources/testharnessreport.js). |
| 190 | This is contrary to the WPT guidelines, which call for absolute paths. |
| 191 | This limitation does not apply to the tests in `LayoutTests/http`, which rely on |
foolip | 339204d | 2017-01-27 21:10:17 | [diff] [blame] | 192 | an HTTP server, or to the tests in `LayoutTests/external/wpt`, which are |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 193 | imported from the [WPT repository](https://github.com/w3c/web-platform-tests). |
| 194 | *** |
| 195 | |
| 196 | ### WPT Supplemental Testing APIs |
| 197 | |
| 198 | Some tests simply cannot be expressed using the Web Platform APIs. For example, |
| 199 | some tests that require a user to perform a gesture, such as a mouse click, |
| 200 | cannot be implemented using Web APIs. The WPT project covers some of these cases |
| 201 | via supplemental testing APIs. |
| 202 | |
pwnall | 59aadcb | 2017-01-26 23:27:21 | [diff] [blame] | 203 | When writing tests that rely on supplemental testing APIs, please consider the |
| 204 | cost and benefits of having the tests |
| 205 | [gracefully degrade to manual tests](./layout_tests_with_manual_fallback.md) in |
| 206 | the absence of the testing APIs. |
| 207 | |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 208 | *** promo |
| 209 | In many cases, the user gesture is not actually necessary. For example, many |
| 210 | event handling tests can use |
| 211 | [synthetic events](https://developer.mozilla.org/docs/Web/Guide/Events/Creating_and_triggering_events). |
| 212 | *** |
| 213 | |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 214 | ### Relying on Blink-Specific Testing APIs |
| 215 | |
| 216 | Tests that cannot be expressed using the Web Platform APIs or WPT's testing APIs |
| 217 | use Blink-specific testing APIs. These APIs are only available in |
| 218 | [content_shell](./layout_tests_in_content_shell.md), and should only be used as |
| 219 | a last resort. |
| 220 | |
| 221 | A downside of Blink-specific APIs is that they are not as well documented as the |
| 222 | Web Platform features. Learning to use a Blink-specific feature requires finding |
| 223 | other tests that use it, or reading its source code. |
| 224 | |
| 225 | For example, the most popular Blink-specific API is `testRunner`, which is |
| 226 | implemented in |
Euisang Lim | e2f2e78 | 2018-05-09 05:04:06 | [diff] [blame] | 227 | [content/shell/test_runner/test_runner.h](../../content/shell/test_runner/test_runner.h) |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 228 | and |
Euisang Lim | e2f2e78 | 2018-05-09 05:04:06 | [diff] [blame] | 229 | [content/shell/test_runner/test_runner.cc](../../content/shell/test_runner/test_runner.cc). |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 230 | By skimming the `TestRunnerBindings::Install` method, we learn that the |
Xianzhu Wang | af4fa41 | 2018-05-14 21:26:52 | [diff] [blame^] | 231 | testRunner API is presented by the `.testRunner` etc. objects. Reading the |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 232 | `TestRunnerBindings::GetObjectTemplateBuilder` method tells us what properties |
Xianzhu Wang | af4fa41 | 2018-05-14 21:26:52 | [diff] [blame^] | 233 | are available on the `testRunner` object. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 234 | |
Xianzhu Wang | af4fa41 | 2018-05-14 21:26:52 | [diff] [blame^] | 235 | Another popular Blink-specific API 'internals' defined in |
| 236 | [third_party/blink/renderer/core/testing/internals.idl](../../third_party/blink/renderer/core/testing/internals.idl) |
| 237 | contains more direct access to blink internals. |
| 238 | |
| 239 | *** note |
| 240 | If possible, a test using blink-specific testing APIs should be written not to |
| 241 | depend on the APIs, so that it can also work directly in a browser. If the test |
| 242 | does need the APIs to work, it should still check if the API is available before |
| 243 | using the API. Note that though we omit the `window.` prefix when using the |
| 244 | APIs, we should use the qualified name in the `if` statement: |
| 245 | ```javascript |
| 246 | if (window.testRunner) |
| 247 | testRunner.waitUntilDone(); |
| 248 | ``` |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 249 | *** |
| 250 | |
| 251 | *** note |
| 252 | `testRunner` is the most popular testing API because it is also used indirectly |
| 253 | by tests that stick to Web Platform APIs. The `testharnessreport.js` file in |
| 254 | `testharness.js` is specifically designated to hold glue code that connects |
| 255 | `testharness.js` to the testing environment. Our implementation is in |
| 256 | [third_party/WebKit/LayoutTests/resources/testharnessreport.js](../../third_party/WebKit/LayoutTests/resources/testharnessreport.js), |
| 257 | and uses the `testRunner` API. |
| 258 | *** |
| 259 | |
Euisang Lim | e2f2e78 | 2018-05-09 05:04:06 | [diff] [blame] | 260 | See the [content/shell/test_runner/](../../content/shell/test_runner/) directory and |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 261 | [WebKit's LayoutTests guide](https://trac.webkit.org/wiki/Writing%20Layout%20Tests%20for%20DumpRenderTree) |
Xianzhu Wang | af4fa41 | 2018-05-14 21:26:52 | [diff] [blame^] | 262 | for other useful APIs. For example, `eventSender` |
Euisang Lim | e2f2e78 | 2018-05-09 05:04:06 | [diff] [blame] | 263 | ([content/shell/test_runner/event_sender.h](../../content/shell/test_runner/event_sender.h) |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 264 | and |
Euisang Lim | e2f2e78 | 2018-05-09 05:04:06 | [diff] [blame] | 265 | [content/shell/test_runner/event_sender.cc](../../content/shell/test_runner/event_sender.cc)) |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 266 | has methods that simulate events input such as keyboard / mouse input and |
| 267 | drag-and-drop. |
| 268 | |
| 269 | Here is a UML diagram of how the `testRunner` bindings fit into Chromium. |
| 270 | |
| 271 | [](https://docs.google.com/drawings/d/1KNRNjlxK0Q3Tp8rKxuuM5mpWf4OJQZmvm9_kpwu_Wwg/edit) |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 272 | |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 273 | ### Text Test Baselines |
| 274 | |
| 275 | By default, all the test cases in a file that uses `testharness.js` are expected |
| 276 | to pass. However, in some cases, we prefer to add failing test cases to the |
| 277 | repository, so that we can be notified when the failure modes change (e.g., we |
| 278 | want to know if a test starts crashing rather than returning incorrect output). |
| 279 | In these situations, a test file will be accompanied by a baseline, which is an |
| 280 | `-expected.txt` file that contains the test's expected output. |
| 281 | |
| 282 | The baselines are generated automatically when appropriate by |
Kent Tamura | a045a7f | 2018-04-25 05:08:11 | [diff] [blame] | 283 | `run_web_tests.py`, which is described [here](./layout_tests.md), and by the |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 284 | [rebaselining tools](./layout_test_expectations.md). |
| 285 | |
| 286 | Text baselines for `testharness.js` should be avoided, as having a text baseline |
| 287 | associated with a `testharness.js` indicates the presence of a bug. For this |
| 288 | reason, CLs that add text baselines must include a |
| 289 | [crbug.com](https://crbug.com) link for an issue tracking the removal of the |
| 290 | text expectations. |
| 291 | |
| 292 | * When creating tests that will be upstreamed to WPT, and Blink's current |
| 293 | behavior does not match the specification that is being tested, a text |
| 294 | baseline is necessary. Remember to create an issue tracking the expectation's |
| 295 | removal, and to link the issue in the CL description. |
| 296 | * Layout tests that cannot be upstreamed to WPT should use JavaScript to |
| 297 | document Blink's current behavior, rather than using JavaScript to document |
| 298 | desired behavior and a text file to document current behavior. |
| 299 | |
| 300 | ### The js-test.js Legacy Harness |
| 301 | |
| 302 | *** promo |
| 303 | For historical reasons, older tests are written using the `js-test` harness. |
| 304 | This harness is **deprecated**, and should not be used for new tests. |
| 305 | *** |
| 306 | |
| 307 | If you need to understand old tests, the best `js-test` documentation is its |
| 308 | implementation at |
| 309 | [third_party/WebKit/LayoutTests/resources/js-test.js](../../third_party/WebKit/LayoutTests/resources/js-test.js). |
| 310 | |
| 311 | `js-test` tests lean heavily on the Blink-specific `testRunner` testing API. |
| 312 | In a nutshell, the tests call `testRunner.dumpAsText()` to signal that the page |
| 313 | content should be dumped and compared against a text baseline (an |
| 314 | `-expected.txt` file). As a consequence, `js-test` tests are always accompanied |
| 315 | by text baselines. Asynchronous tests also use `testRunner.waitUntilDone()` and |
| 316 | `testRunner.notifyDone()` to tell the testing tools when they are complete. |
| 317 | |
| 318 | ### Tests that use an HTTP Server |
| 319 | |
| 320 | By default, tests are loaded as if via `file:` URLs. Some web platform features |
| 321 | require tests served via HTTP or HTTPS, for example absolute paths (`src=/foo`) |
| 322 | or features restricted to secure protocols. |
| 323 | |
| 324 | HTTP tests are those under `LayoutTests/http/tests` (or virtual variants). Use a |
| 325 | locally running HTTP server (Apache) to run them. Tests are served off of ports |
| 326 | 8000 and 8080 for HTTP, and 8443 for HTTPS. If you run the tests using |
Kent Tamura | a045a7f | 2018-04-25 05:08:11 | [diff] [blame] | 327 | `run_web_tests.py`, the server will be started automatically. To run the server |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 328 | manually to reproduce or debug a failure: |
| 329 | |
| 330 | ```bash |
Kent Tamura | e81dbff | 2018-04-20 17:35:34 | [diff] [blame] | 331 | cd src/third_party/blink/tools |
| 332 | ./run_blink_httpd.py |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 333 | ``` |
| 334 | |
| 335 | The layout tests will be served from `http://127.0.0.1:8000`. For example, to |
| 336 | run the test `http/tests/serviceworker/chromium/service-worker-allowed.html`, |
| 337 | navigate to |
| 338 | `http://127.0.0.1:8000/serviceworker/chromium/service-worker-allowed.html`. Some |
| 339 | tests will behave differently if you go to 127.0.0.1 instead of localhost, so |
| 340 | use 127.0.0.1. |
| 341 | |
Kent Tamura | e81dbff | 2018-04-20 17:35:34 | [diff] [blame] | 342 | To kill the server, hit any key on the terminal where `run_blink_httpd.py` is |
Hajime Hoshi | a6fad02 | 2017-08-01 17:57:58 | [diff] [blame] | 343 | running, or just use `taskkill` or the Task Manager on Windows, and `killall` or |
| 344 | Activity Monitor on MacOS. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 345 | |
| 346 | The test server sets up an alias to the `LayoutTests/resources` directory. In |
| 347 | HTTP tests, you can access the testing framework at e.g. |
pwnall | e781948 | 2016-12-17 00:58:40 | [diff] [blame] | 348 | `src="/resources/testharness.js"`. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 349 | |
| 350 | TODO: Document [wptserve](http://wptserve.readthedocs.io/) when we are in a |
| 351 | position to use it to run layout tests. |
| 352 | |
| 353 | ## Reference Tests (Reftests) |
| 354 | |
| 355 | Reference tests, also known as reftests, perform a pixel-by-pixel comparison |
| 356 | between the rendered image of a test page and the rendered image of a reference |
| 357 | page. Most reference tests pass if the two images match, but there are cases |
| 358 | where it is useful to have a test pass when the two images do _not_ match. |
| 359 | |
| 360 | Reference tests are more difficult to debug than JavaScript tests, and tend to |
| 361 | be slower as well. Therefore, they should only be used for functionality that |
| 362 | cannot be covered by JavaScript tests. |
| 363 | |
| 364 | New reference tests should follow the |
foolip | eda32ab | 2017-02-16 19:21:58 | [diff] [blame] | 365 | [WPT reftests guidelines](http://web-platform-tests.org/writing-tests/reftests.html). |
| 366 | The most important points are summarized below. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 367 | |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 368 | * 🚧 The test page declares the reference page using a |
| 369 | `<link rel="match">` or `<link rel="mismatch">`, depending on whether the test |
| 370 | passes when the test image matches or does not match the reference image. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 371 | * The reference page must not use the feature being tested. Otherwise, the test |
| 372 | is meaningless. |
| 373 | * The reference page should be as simple as possible, and should not depend on |
| 374 | advanced features. Ideally, the reference page should render as intended even |
| 375 | on browsers with poor CSS support. |
| 376 | * Reference tests should be self-describing. |
| 377 | * Reference tests do _not_ include `testharness.js`. |
| 378 | |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 379 | 🚧 Our testing infrastructure was designed for the |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 380 | [WebKit reftests](https://trac.webkit.org/wiki/Writing%20Reftests) that Blink |
| 381 | has inherited. The consequences are summarized below. |
| 382 | |
| 383 | * Each reference page must be in the same directory as its associated test. |
| 384 | Given a test page named `foo` (e.g. `foo.html` or `foo.svg`), |
| 385 | * The reference page must be named `foo-expected` (e.g., |
| 386 | `foo-expected.html`) if the test passes when the two images match. |
| 387 | * The reference page must be named `foo-expected-mismatch` (e.g., |
| 388 | `foo-expected-mismatch.svg`) if the test passes when the two images do |
| 389 | _not_ match. |
| 390 | * Multiple references and chained references are not supported. |
| 391 | |
| 392 | The following example demonstrates a reference test for |
| 393 | [`<ol>`'s reversed attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ol). |
| 394 | The example assumes that the test page is named `ol-reversed.html`. |
| 395 | |
| 396 | ```html |
| 397 | <!doctype html> |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 398 | <link rel="match" href="ol-reversed-expected.html"> |
| 399 | |
| 400 | <ol reversed> |
| 401 | <li>A</li> |
| 402 | <li>B</li> |
| 403 | <li>C</li> |
| 404 | </ol> |
| 405 | ``` |
| 406 | |
| 407 | The reference page, which must be named `ol-reversed-expected.html`, is below. |
| 408 | |
| 409 | ```html |
| 410 | <!doctype html> |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 411 | |
| 412 | <ol> |
| 413 | <li value="3">A</li> |
| 414 | <li value="2">B</li> |
| 415 | <li value="1">C</li> |
| 416 | </ol> |
| 417 | ``` |
| 418 | |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 419 | *** promo |
| 420 | The method for pointing out a test's reference page is still in flux, and is |
| 421 | being discussed on |
| 422 | [blink-dev](https://groups.google.com/a/chromium.org/d/topic/blink-dev/XsR6PKRrS1E/discussion). |
| 423 | *** |
| 424 | |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 425 | ## Pixel Tests |
| 426 | |
Xianzhu Wang | af4fa41 | 2018-05-14 21:26:52 | [diff] [blame^] | 427 | `testRunner` APIs such as `testRunner.dumpAsTextWithPixelResults()` and |
| 428 | `testRunner.dumpDragImage()` create an image result that is associated |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 429 | with the test. The image result is compared against an image baseline, which is |
| 430 | an `-expected.png` file associated with the test, and the test passes if the |
| 431 | image result is identical to the baseline, according to a pixel-by-pixel |
| 432 | comparison. Tests that have image results (and baselines) are called **pixel |
| 433 | tests**. |
| 434 | |
| 435 | Pixel tests should still follow the principles laid out above. Pixel tests pose |
| 436 | unique challenges to the desire to have *self-describing* and *cross-platform* |
| 437 | tests. The |
foolip | eda32ab | 2017-02-16 19:21:58 | [diff] [blame] | 438 | [WPT rendering test guidelines](http://web-platform-tests.org/writing-tests/rendering.html) |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 439 | contain useful guidance. The most relevant pieces of advice are below. |
| 440 | |
| 441 | * Whenever possible, use a green paragraph / page / square to indicate success. |
| 442 | If that is not possible, make the test self-describing by including a textual |
| 443 | description of the desired (passing) outcome. |
| 444 | * Only use the red color or the word `FAIL` to highlight errors. This does not |
| 445 | apply when testing the color red. |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 446 | * 🚧 Use the |
| 447 | [Ahem font](https://www.w3.org/Style/CSS/Test/Fonts/Ahem/README) to reduce the |
| 448 | variance introduced by the platform's text rendering system. This does not |
| 449 | apply when testing text, text flow, font selection, font fallback, font |
| 450 | features, or other typographic information. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 451 | |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 452 | TODO: Document how to opt out of generating a layout tree when generating |
| 453 | pixel results. |
| 454 | |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 455 | *** promo |
Xianzhu Wang | af4fa41 | 2018-05-14 21:26:52 | [diff] [blame^] | 456 | When using `testRunner.dumpAsTextWithPixelResults()`, the image result |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 457 | will always be 800x600px, because test pages are rendered in an 800x600px |
| 458 | viewport. Pixel tests that do not specifically cover scrolling should fit in an |
| 459 | 800x600px viewport without creating scrollbars. |
| 460 | *** |
| 461 | |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 462 | *** promo |
| 463 | The recommendation of using Ahem in pixel tests is being discussed on |
| 464 | [blink-dev](https://groups.google.com/a/chromium.org/d/topic/blink-dev/XsR6PKRrS1E/discussion). |
| 465 | *** |
| 466 | |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 467 | The following snippet includes the Ahem font in a layout test. |
| 468 | |
| 469 | ```html |
| 470 | <style> |
| 471 | body { |
| 472 | font: 10px Ahem; |
| 473 | } |
| 474 | </style> |
| 475 | <script src="/resources/ahem.js"></script> |
| 476 | ``` |
| 477 | |
| 478 | *** promo |
foolip | 339204d | 2017-01-27 21:10:17 | [diff] [blame] | 479 | Tests outside `LayoutTests/http` and `LayoutTests/external/wpt` currently need |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 480 | to use a relative path to |
| 481 | [/third_party/WebKit/LayoutTests/resources/ahem.js](../../third_party/WebKit/LayoutTests/resources/ahem.js) |
| 482 | *** |
| 483 | |
| 484 | ### Tests that need to paint, raster, or draw a frame of intermediate output |
| 485 | |
| 486 | A layout test does not actually draw frames of output until the test exits. |
Xianzhu Wang | af4fa41 | 2018-05-14 21:26:52 | [diff] [blame^] | 487 | Tests that need to generate a painted frame can use `runAfterLayoutAndPaint()` |
| 488 | defined in [third_party/WebKit/LayoutTests/resources/run-after-layout-and-paint.js](../../third_party/WebKit/LayoutTests/resources/run-after-layout-and-paint.js) |
| 489 | which will run the machinery to put up a frame, then call the passed callback. |
| 490 | There is also a library at |
| 491 | [third_party/WebKit/LayoutTests/paint/invalidation/resources/text-based-repaint.js](../../third_party/WebKit/LayoutTests/paint/invalidation/resources/text-based-repaint.js) |
| 492 | to help with writing paint invalidation and repaint tests. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 493 | |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 494 | ## Layout tree tests |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 495 | |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 496 | A layout tree test renders a web page and produces up to two results, which |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 497 | are compared against baseline files: |
| 498 | |
| 499 | * All tests output a textual representation of Blink's |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 500 | [layout tree](https://developers.google.com/web/fundamentals/performance/critical-rendering-path/render-tree-construction) (called the render tree on that page), |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 501 | which is compared against an `-expected.txt` text baseline. |
| 502 | * Some tests also output the image of the rendered page, which is compared |
| 503 | against an `-expected.png` image baseline, using the same method as pixel |
| 504 | tests. |
| 505 | |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 506 | Whether you want a pixel test or a layout tree test depends on whether |
| 507 | you care about the visual image, the details of how that image was |
| 508 | constructed, or both. It is possible for multiple layout trees to produce |
| 509 | the same pixel output, so it is important to make it clear in the test |
| 510 | which outputs you really care about. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 511 | |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 512 | TODO: Document the API used by layout tree tests to opt out of producing image |
| 513 | results. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 514 | |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 515 | A layout tree test passes if _all_ of its results match their baselines. Like pixel |
| 516 | tests, the output of layout tree tests depends on platform-specific details, |
| 517 | so layout tree tests often require per-platform baselines. Furthermore, |
| 518 | since the tests obviously depend on the layout tree structure, |
| 519 | that means that if we change the layout tree you have to rebaseline each |
| 520 | layout tree test to see if the results are still correct and whether the test |
| 521 | is still meaningful. There are actually many cases where the layout tree |
| 522 | output is misstated (i.e., wrong), because people didn't want to have to update |
| 523 | existing baselines and tests. This is really unfortunate and confusing. |
| 524 | |
| 525 | For these reasons, layout tree tests should **only** be used to cover aspects |
| 526 | of the layout code that can only be tested by looking at the layout tree. Any |
| 527 | combination of the other test types is preferable to a layout tree test. |
| 528 | Layout tree tests are |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 529 | [inherited from WebKit](https://webkit.org/blog/1456/layout-tests-practice/), so |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 530 | the repository may have some unfortunate examples of layout tree tests. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 531 | |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 532 | |
| 533 | The following page is an example of a layout tree test. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 534 | |
| 535 | ```html |
| 536 | <!doctype html> |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 537 | <style> |
| 538 | body { font: 10px Ahem; } |
| 539 | span::after { |
| 540 | content: "pass"; |
| 541 | color: green; |
| 542 | } |
| 543 | </style> |
| 544 | <script src="/resources/ahem.js"></script> |
| 545 | |
| 546 | <p><span>Pass if a green PASS appears to the right: </span></p> |
| 547 | ``` |
| 548 | |
| 549 | The most important aspects of the example are that the test page does not |
| 550 | include a testing framework, and that it follows the guidelines for pixel tests. |
| 551 | The test page produces the text result below. |
| 552 | |
| 553 | ``` |
| 554 | layer at (0,0) size 800x600 |
| 555 | LayoutView at (0,0) size 800x600 |
| 556 | layer at (0,0) size 800x30 |
| 557 | LayoutBlockFlow {HTML} at (0,0) size 800x30 |
| 558 | LayoutBlockFlow {BODY} at (8,10) size 784x10 |
| 559 | LayoutBlockFlow {P} at (0,0) size 784x10 |
| 560 | LayoutInline {SPAN} at (0,0) size 470x10 |
| 561 | LayoutText {#text} at (0,0) size 430x10 |
| 562 | text run at (0,0) width 430: "Pass if a green PASS appears to the right: " |
| 563 | LayoutInline {<pseudo:after>} at (0,0) size 40x10 [color=#008000] |
| 564 | LayoutTextFragment (anonymous) at (430,0) size 40x10 |
| 565 | text run at (430,0) width 40: "pass" |
| 566 | ``` |
| 567 | |
| 568 | Notice that the test result above depends on the size of the `<p>` text. The |
| 569 | test page uses the Ahem font (introduced above), whose main design goal is |
| 570 | consistent cross-platform rendering. Had the test used another font, its text |
| 571 | baseline would have depended on the fonts installed on the testing computer, and |
| 572 | on the platform's font rendering system. Please follow the pixel tests |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 573 | guidelines and write reliable layout tree tests! |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 574 | |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 575 | WebKit's layout tree is described in |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 576 | [a series of posts](https://webkit.org/blog/114/webcore-rendering-i-the-basics/) |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 577 | on WebKit's blog. Some of the concepts there still apply to Blink's layout tree. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 578 | |
| 579 | ## Directory Structure |
| 580 | |
| 581 | The [LayoutTests directory](../../third_party/WebKit/LayoutTests) currently |
| 582 | lacks a strict, formal structure. The following directories have special |
| 583 | meaning: |
| 584 | |
| 585 | * The `http/` directory hosts tests that require an HTTP server (see above). |
| 586 | * The `resources/` subdirectory in every directory contains binary files, such |
| 587 | as media files, and code that is shared by multiple test files. |
| 588 | |
| 589 | *** note |
| 590 | Some layout tests consist of a minimal HTML page that references a JavaScript |
| 591 | file in `resources/`. Please do not use this pattern for new tests, as it goes |
| 592 | against the minimality principle. JavaScript and CSS files should only live in |
| 593 | `resources/` if they are shared by at least two test files. |
| 594 | *** |