Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 an HTML page and lives | |
|
jsbell
2016/11/10 19:32:26
We have some ref tests that are SVG documents, as
pwnall
2016/11/11 20:51:07
Done.
Added mentions to XHTML and SVG. Let's deal
| |
| 6 in [third_party/WebKit/LayoutTests/](../../third_party/WebKit/LayoutTests). | |
| 7 | |
| 8 Layout tests should be used to accomplish one of the following goals: | |
| 9 | |
| 10 1. The entire surface of Blink that is exposed to the Web should be covered by | |
| 11 tests that we contribute to the | |
| 12 [Web Platform Tests Project](https://github.com/w3c/web-platform-tests) | |
| 13 (WPT). This helps us avoid regressions, and gives us confidence that we match | |
| 14 other browsers' behavior. | |
| 15 2. When a Blink feature cannot be tested using the Web Platform, and cannot be | |
| 16 easily covered by | |
| 17 [C++ unit tests](https://cs.chromium.org/chromium/src/third_party/WebKit/Sour ce/web/tests/?q=webframetest&sq=package:chromium&type=cs), | |
| 18 the feature must be covered by layout tests, to avoid unexpected regressions. | |
| 19 These tests will use Blink-specific testing APIs that are only available in | |
| 20 [content_shell](./layout_tests_in_content_shell.md). | |
| 21 | |
| 22 ## General Principles | |
| 23 | |
| 24 The principles below are adapted from | |
| 25 [Test the Web Forward's Test Format Guidelines](http://testthewebforward.org/doc s/test-format-guidelines.html) | |
| 26 and | |
| 27 [WebKit's Wiki page on Writing good test cases](https://trac.webkit.org/wiki/Wri ting%20Layout%20Tests%20for%20DumpRenderTree). | |
| 28 | |
| 29 * Tests should be as **short** as possible. The page should only include | |
| 30 elements that are necessary and relevant to what is being tested. | |
| 31 | |
| 32 * Tests should be as **fast** as possible. Blink has several thousand layout | |
| 33 tests that are run in parallel, and avoiding unnecessary delays is crucial to | |
| 34 keeping our Commit Queue in good shape. Avoid [window.setTimeout](https://deve loper.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout), | |
| 35 as it wastes testing time and can introduce flakiness. Instead, use specific | |
| 36 event handlers, such as | |
| 37 [window.onload](https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHa ndlers/onload). | |
| 38 | |
| 39 * Tests should be **self-describing**, so that a project member can recognize | |
| 40 whether a test passes or fails without having to read the specification of the | |
| 41 feature being tested. `testharness.js` makes a test self-describing when used | |
| 42 correctly, but tests that degrade to manual tests | |
| 43 [must be carefully designed](http://testthewebforward.org/docs/test-style-guid elines.html) | |
| 44 to be self-describing. | |
| 45 | |
| 46 * Tests should use the **minimal** set of platform features needed to express | |
| 47 the test scenario efficiently. Avoid depending on edge case behavior of | |
| 48 features that aren't explicitly covered by the test. For example, except where | |
| 49 testing parsing, tests should contain no parse errors. | |
| 50 | |
| 51 * Tests should be as **cross-platform** as reasonably possible. Avoid | |
| 52 assumptions about device type, screen resolution, etc. Unavoidable assumptions | |
| 53 should be documented. | |
| 54 * When possible, tests should only use Web platform features, as specified | |
| 55 in the relevant standards. | |
| 56 * Tests should be written under the assumption that they will be upstreamed | |
| 57 to the WPT project. For example, tests should follow the | |
| 58 [WPT guidelines](http://testthewebforward.org/docs/writing-tests.html). | |
| 59 * Tests that use Blink-specific testing APIs should feature-test for the | |
| 60 presence of the testing APIs and degrade to | |
| 61 [manual tests](http://testthewebforward.org/docs/manual-test.html) | |
| 62 when the testing APIs are not present. | |
| 63 | |
| 64 * Tests must be **self-contained** and not depend on external network resources. | |
| 65 Unless used by multiple test files, CSS and JavaScript should be inlined using | |
| 66 `<style>` and `<script>` tags. Content shared by multiple tests should be | |
| 67 placed in a `resources/` directory near the tests that share it. See below for | |
| 68 using multiple origins in a test. | |
| 69 | |
| 70 * Test **file names** should describe what is being tested. | |
| 71 | |
| 72 * Tests must stick to pure ASCII or use the UTF-8 **character encoding**, which | |
| 73 should be declared by `<meta charset=utf-8>`. This does not apply when | |
| 74 specifically testing encodings. | |
| 75 | |
| 76 * Tests must aim to have a **coding style** that is consistent with | |
| 77 [Google's JavaScript Style Guide](https://google.github.io/styleguide/javascri ptguide.xml), | |
| 78 with the following exceptions. | |
| 79 * Rules related to Google Closure and JSDoc do not apply. | |
| 80 * Modern Web Platform and JavaScript features should be preferred to legacy | |
| 81 constructs that target old browsers. For example, prefer `const` and `let` | |
| 82 to `var`, and prefer `class` over other OOP constructs. | |
| 83 * Concerns regarding buggy behavior in legacy browsers do not apply. For | |
| 84 example, the garbage collection cycle note in the _Closures_ section does | |
| 85 not apply. | |
| 86 | |
| 87 ## JavaScript Tests | |
| 88 | |
| 89 Whenever possible, the testing criteria should be expressed in JavaScript. The | |
| 90 alternatives, which will be described in future sections, result in slower and | |
| 91 less robust tests. | |
| 92 | |
| 93 All new JavaScript tests should be written using the | |
| 94 [testharness.js](https://github.com/w3c/testharness.js/) testing framework. This | |
| 95 framework is used by the tests in the | |
| 96 [web-platform-tests](https://github.com/w3c/web-platform-tests) repository, | |
| 97 which is shared with all the other browser vendors, so `testharness.js` tests | |
| 98 are more accessible to browser developers. | |
| 99 | |
| 100 As a shared framework, `testharness.js` enjoys high-quality documentation, such | |
| 101 as [a tutorial](http://testthewebforward.org/docs/testharness-tutorial.html) and | |
| 102 [API documentation](https://github.com/w3c/testharness.js/blob/master/docs/api.m d). | |
| 103 Layout tests should follow the recommendations of the above documents. | |
| 104 Furthermore, layout tests should include relevant | |
| 105 [metadata](http://testthewebforward.org/docs/css-metadata.html). The | |
| 106 specification URL (in `<link rel="help">`) is almost always relevant, and is | |
| 107 incredibly helpful to a developer who needs to understand the test quickly. | |
| 108 | |
| 109 Below is a skeleton for a JavaScript test. Note that, in order to follow the | |
|
jsbell
2016/11/10 19:32:25
I think we should give an async_test and promise_t
pwnall
2016/11/11 20:51:07
Done.
| |
| 110 minimality guideline, the test omits the tags `<html>`, `<head>` and `<body>`, | |
| 111 as they can be inferred by the HTML5 parser. | |
| 112 | |
| 113 ```html | |
| 114 <!DOCTYPE html> | |
| 115 <meta charset="utf-8"> | |
| 116 <title>JavaScript core: True is truthy</title> | |
| 117 <link rel="help" | |
| 118 href="http://www.ecma-international.org/ecma-262/6.0/#sec-terms-and-defini tions-boolean-value"> | |
| 119 <script src="/resources/testharness.js"></script> | |
| 120 <script src="/resources/testharnessreport.js"></script> | |
| 121 <script> | |
| 122 test(t => { | |
| 123 var x = true; | |
| 124 assert_true(x); | |
| 125 }, 'True is truthy.'); | |
| 126 </script> | |
| 127 ``` | |
| 128 | |
| 129 ### Blink-Specific Testing APIs | |
| 130 | |
| 131 Tests that cannot be expressed using the Web Platform APIs rely on | |
| 132 Blink-specific testing APIs. These APIs are only available in | |
| 133 [content_shell](./layout_tests_in_content_shell.md). | |
| 134 | |
| 135 Whenever possible, tests that rely on Blink-specific testing APIs should also be | |
| 136 usable as [manual tests](http://testthewebforward.org/docs/manual-test.html). | |
| 137 This makes it easy to debug the test, and to check whether our behavior matches | |
| 138 other browsers. | |
| 139 | |
| 140 Manual tests should minimize the chance of user error. This implies keeping the | |
|
jsbell
2016/11/10 19:32:26
We may want an inline example here of a "manual" t
pwnall
2016/11/11 20:51:07
Done.
| |
| 141 manual steps to a minimum, and having simple and clear instructions that | |
| 142 describe all the configuration changes and user gestures that match the effect | |
| 143 of the Blink-specific APIs used by the test. | |
| 144 | |
| 145 A downside of Blink-specific APIs is that they are not as well documented as the | |
| 146 Web Platform features. Learning to use a Blink-specific feature requires finding | |
| 147 other tests that use it, or reading its source code. | |
| 148 | |
| 149 For example, the most popular Blink-specific API is `testRunner`, which is | |
|
jsbell
2016/11/10 19:32:26
We maybe want to mention (here or above) that test
pwnall
2016/11/11 20:51:07
Done.
I added a note.
| |
| 150 implemented in | |
| 151 [components/test_runner/test_runner.h](../../components/test_runner/test_runner. h) | |
| 152 and | |
| 153 [components/test_runner/test_runner.cpp](../../components/test_runner/test_runne r.cpp). | |
| 154 By skimming the `TestRunnerBindings::Install` method, we learn that the | |
| 155 testRunner API is presented by the `window.testRunner` and | |
| 156 `window.layoutTestsController` objects, which are synonyms. Reading the | |
| 157 `TestRunnerBindings::GetObjectTemplateBuilder` method tells us what properties | |
| 158 are available on the `window.testRunner` object. | |
| 159 | |
| 160 *** aside | |
| 161 `window.testRunner` is the preferred way to access the `testRunner` APIs. | |
| 162 `window.layoutTestsController` is still supported because it is used by | |
| 163 3rd-party tests. | |
| 164 *** | |
| 165 | |
| 166 See the [components/test_runner/](../../components/test_runner/) directory and | |
| 167 [WebKit's LayoutTests guide](https://trac.webkit.org/wiki/Writing%20Layout%20Tes ts%20for%20DumpRenderTree) | |
| 168 for other useful APIs. For example, `window.eventSender` | |
| 169 ([components/test_runner/event_sender.h](../../components/test_runner/event_send er.h) | |
| 170 and | |
| 171 [components/test_runner/event_sender.cpp](../../components/test_runner/event_sen der.cpp)) | |
| 172 has methods that simulate events input such as keyboard / mouse input and | |
| 173 drag-and-drop. | |
| 174 | |
| 175 Here is a UML diagram of how the `testRunner` bindings fit into Chromium. | |
| 176 | |
| 177 [](https://docs.google .com/drawings/d/1KNRNjlxK0Q3Tp8rKxuuM5mpWf4OJQZmvm9_kpwu_Wwg/edit) | |
| 178 | |
| 179 ### Text Test Expectations | |
| 180 | |
| 181 By default, all the test cases in a file that uses `testharness.js` are expected | |
| 182 to pass. However, in some cases, we prefer to add failing test cases to the | |
| 183 repository, so that we can be notified when the failure modes change. In these | |
|
jsbell
2016/11/10 19:32:26
maybe: (e.g. a test starts crashing rather than re
pwnall
2016/11/11 20:51:07
Done.
| |
| 184 situations, a test file will be accompanied by an `-expected.txt` file, which | |
| 185 documents the test's expected output. | |
| 186 | |
| 187 The `-expected.txt` files are generated automatically when appropriate by | |
| 188 `run-webkit-tests`, which is described [here](./layout_tests.md), and by the | |
| 189 [rebaselining tools](./layout_test_expectations.md). | |
| 190 | |
| 191 `-expected.txt` files should be very rare. In general, layout tests should | |
| 192 use JavaScript to document Blink's current behavior, rather than using | |
| 193 JavaScript to document desired behavior and a text file to document current | |
| 194 behavior. | |
| 195 | |
| 196 ### js-test tests | |
| 197 | |
| 198 *** promo | |
| 199 For historical reasons, older tests are written using the `js-test` harness. | |
| 200 This harness is **deprecated**, and should not be used for new tests. | |
| 201 *** | |
| 202 | |
| 203 If you need to understand old tests, the best `js-test` documentation is its | |
| 204 implementation at | |
| 205 [third_party/WebKit/LayoutTests/resources/js-test.js](../../third_party/WebKit/L ayoutTests/resources/js-test.js). | |
| 206 | |
| 207 `js-test` tests lean heavily on the Blink-specific `testRunner` testing APIs, | |
| 208 which will be fully described below. In a nutshell, the tests call | |
| 209 `testRunner.dumpAsText()` to signal that the page content should be dumped and | |
| 210 compared against an `-expected.txt` file. As a consequence, `js-test` tests are | |
| 211 always accompanied by an `-expected.txt`. Asynchronous tests also use | |
| 212 `testRunner.waitUntilDone()` and `testRunner.notifyDone()` to tell the testing | |
| 213 tools when they are complete. | |
| 214 | |
| 215 ### Tests that use an HTTP Server | |
|
jsbell
2016/11/10 19:32:25
Should probably have a section about imported test
pwnall
2016/11/11 20:51:07
There is a TODO at the end about documenting wptse
jsbell
2016/11/14 19:12:22
Punting is fine, I just missed the TODO. Thanks!
| |
| 216 | |
| 217 By default, tests are loaded as if via `file:` URLs. Some web platform features | |
| 218 require tests served via HTTP or HTTPS, for example relative paths (`src=/foo`) | |
|
jsbell
2016/11/10 19:32:26
absolute paths
(relative paths work file with fil
pwnall
2016/11/11 20:51:07
Done.
| |
| 219 or features restricted to secure protocols. | |
| 220 | |
| 221 HTTP tests are those tests that are under `LayoutTests/http/tests` (or virtual | |
| 222 variants). Use a locally running HTTP server (Apache) to run them. Tests are | |
| 223 served off of ports 8000 and 8080 for HTTP, and 8443 for HTTPS. If you run the | |
| 224 tests using `run-webkit-tests`, the server will be started automatically. To run | |
| 225 the server manually to reproduce or debug a failure: | |
| 226 | |
| 227 ```bash | |
| 228 cd src/third_party/WebKit/Tools/Scripts | |
| 229 run-blink-httpd start | |
| 230 ``` | |
| 231 | |
| 232 The layout tests will be served from `http://127.0.0.1:8000`. For example, to | |
| 233 run the test `http/tests/serviceworker/chromium/service-worker-allowed.html`, | |
| 234 navigate to | |
| 235 `http://127.0.0.1:8000/serviceworker/chromium/service-worker-allowed.html`. Some | |
| 236 tests will behave differently if you go to 127.0.0.1 instead of localhost, so | |
| 237 use 127.0.0.1. | |
| 238 | |
| 239 To kill the server, run `run-blink-httpd --server stop`, or just use `taskkill` | |
| 240 or the Task Manager on Windows, and `killall` or Activity Monitor on MacOS. | |
| 241 | |
| 242 The test server sets up an alias to `LayoutTests/resources` directory. In HTTP | |
| 243 tests, you can access the testing framework at e.g. | |
| 244 `src="/js-test-resources/js-test.js"`. | |
| 245 | |
| 246 TODO: Document [wptserve](http://wptserve.readthedocs.io/) when we are in a | |
| 247 position to use it to run layout tests. | |
| 248 | |
| 249 ## Reference Tests | |
| 250 | |
| 251 TODO: Check that we match the Web Platform's | |
| 252 [reftests](http://testthewebforward.org/docs/reftests.html), link to their | |
| 253 document and summarize it. | |
| 254 | |
| 255 ## Pixel Tests | |
| 256 | |
| 257 `testRunner` APIs such as `window.testRunner.dumpAsTextWithPixelResults()` and | |
| 258 `window.testRunner.dumpDragImage()` create an image expectation, turning the | |
| 259 test into a **pixel test**. These tests have associated `-expected.png` image | |
| 260 files. | |
| 261 | |
| 262 Pixel tests are less robust than JavaScript tests and reference tests, because | |
| 263 the browser's image output can be influenced by many factors such as the | |
| 264 graphics driver, the platform's text rendering system, and various | |
| 265 user-configurable platform settings. For this reason, pixel tests should only be | |
| 266 used as a last resort. | |
| 267 | |
| 268 Pixel tests should still follow the | |
| 269 [WPT guidelines](http://testthewebforward.org/docs/test-style-guidelines.html). | |
| 270 The most relevant items are below. | |
| 271 | |
| 272 * use a green paragraph / page / square to indicate success | |
| 273 * use the red color or the word `FAIL` to highlight errors | |
| 274 * use the [Ahem font](https://www.w3.org/Style/CSS/Test/Fonts/Ahem/README) to | |
| 275 minimize the variance introduced by the platform's text rendering system | |
| 276 | |
| 277 The following snippet includes the Ahem font in a layout test. | |
| 278 | |
| 279 ```html | |
| 280 <style> | |
| 281 body { | |
| 282 font: 10px Ahem; | |
| 283 } | |
| 284 </style> | |
| 285 <script src="/resources/ahem.js"></script> | |
| 286 ``` | |
| 287 | |
| 288 ### Tests that need to paint, raster, or draw a frame of intermediate output | |
| 289 | |
| 290 A layout test does not actually draw frames of output until the test exits. If | |
| 291 it is required to generate a painted frame, then use | |
| 292 `window.testRunner.displayAsyncThen`, which will run the machinery to put up a | |
| 293 frame, then call the passed callback. There is also a library at | |
| 294 `fast/repaint/resources/text-based-repaint.js` to help with writing paint | |
| 295 invalidation and repaint tests. | |
| 296 | |
| 297 ## See Also | |
| 298 | |
| 299 [Writing reliable layout tests](https://docs.google.com/document/d/1Yl4SnTLBWmY1 O99_BTtQvuoffP8YM9HZx2YPkEsaduQ/edit) | |
| OLD | NEW |