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 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 | |
| 16 tests that we contribute to the | |
| 17 [Web Platform Tests Project](https://github.com/w3c/web-platform-tests) | |
| 18 (WPT). This helps us avoid regressions, and helps us identify Web Platform | |
| 19 areas where the major browsers don't have interoperable implementations. | |
| 20 2. When a Blink feature cannot be tested using the Web Platform, and cannot be | |
| 21 easily covered by | |
| 22 [C++ unit tests](https://cs.chromium.org/chromium/src/third_party/WebKit/Sour ce/web/tests/?q=webframetest&sq=package:chromium&type=cs), | |
| 23 the feature must be covered by layout tests, to avoid unexpected regressions. | |
| 24 These tests will use Blink-specific testing APIs that are only available in | |
| 25 [content_shell](./layout_tests_in_content_shell.md). | |
| 26 | |
| 27 ### Test Types | |
| 28 | |
| 29 There are three broad types of layout tests, listed in the order of preference. | |
| 30 | |
| 31 * *JavaScript Tests* are the layout test implementation of | |
| 32 [xUnit tests](https://en.wikipedia.org/wiki/XUnit). These tests contain | |
| 33 assertions written in JavaScript, and pass if the assertions evaluate to | |
| 34 true. | |
| 35 * *Reference Tests* render a test page and a reference page, and pass if the two | |
| 36 renderings are identical, according to a pixel-by-pixel comparison. These | |
| 37 tests are less robust, harder to debug, and significantly slower than | |
| 38 JavaScript tests, and are only used when JavaScript tests are insufficient, | |
| 39 such as when testing layout code. | |
| 40 * *Pixel Tests* render a test page and compare the result against a pre-rendered | |
| 41 image in the repository. Pixel tests are less robust than JavaScript tests and | |
| 42 reference tests, because the rendering of a page is influenced by many factors | |
| 43 such as the host computer's graphics card and driver, the platform's text | |
| 44 rendering system, and various user-configurable operating system settings. | |
| 45 For this reason, it is not uncommon for a pixel test to have a different | |
| 46 reference image for each platform that Blink is tested on. Pixel tests are | |
| 47 least preferred, because the reference images are | |
| 48 [quite cumbersome to manage](./layout_test_expectations.md). | |
| 49 | |
| 50 ## General Principles | |
| 51 | |
| 52 The principles below are adapted from | |
| 53 [Test the Web Forward's Test Format Guidelines](http://testthewebforward.org/doc s/test-format-guidelines.html) | |
| 54 and | |
| 55 [WebKit's Wiki page on Writing good test cases](https://trac.webkit.org/wiki/Wri ting%20Layout%20Tests%20for%20DumpRenderTree). | |
| 56 | |
| 57 * Tests should be as **short** as possible. The page should only include | |
| 58 elements that are necessary and relevant to what is being tested. | |
| 59 | |
| 60 * Tests should be as **fast** as possible. Blink has several thousand layout | |
| 61 tests that are run in parallel, and avoiding unnecessary delays is crucial to | |
| 62 keeping our Commit Queue in good shape. | |
| 63 * Avoid [window.setTimeout](https://developer.mozilla.org/en-US/docs/Web/API /WindowTimers/setTimeout), | |
| 64 as it wastes time on the testing infrastructure. Instead, use specific | |
| 65 event handlers, such as | |
| 66 [window.onload](https://developer.mozilla.org/en-US/docs/Web/API/GlobalEve ntHandlers/onload), | |
| 67 to decide when to proceed with a test. | |
| 68 | |
| 69 * Tests should be **reliable** and yield consistent results for a given | |
| 70 implementation. Flaky tests slow down fellow developers' debugging efforts and | |
| 71 the Commit Queue. | |
| 72 * `window.setTimeout` is again a primary offender here. Asides from wasting | |
| 73 time on a fast system, tests that rely on fixed timeouts can fail when run | |
| 74 on systems that are slower than expected. | |
| 75 * Follow the guidelines in this | |
| 76 [PSA on writing reliable layout tests](https://docs.google.com/document/d/ 1Yl4SnTLBWmY1O99_BTtQvuoffP8YM9HZx2YPkEsaduQ/edit). | |
| 77 | |
| 78 * Tests should be **self-describing**, so that a project member can recognize | |
| 79 whether a test passes or fails without having to read the specification of the | |
| 80 feature being tested. `testharness.js` makes a test self-describing when used | |
| 81 correctly, but tests that degrade to manual tests | |
| 82 [must be carefully designed](http://testthewebforward.org/docs/test-style-guid elines.html) | |
| 83 to be self-describing. | |
| 84 | |
| 85 * Tests should use the **minimal** set of platform features needed to express | |
| 86 the test scenario efficiently. | |
| 87 * Avoid depending on edge case behavior of features that aren't explicitly | |
| 88 covered by the test. For example, except where testing parsing, tests | |
| 89 should contain valid markup (no parsing errors). | |
| 90 * Prefer JavaScript's | |
| 91 [===](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operator s/Comparison_Operators#Identity_strict_equality_()) | |
| 92 operator to | |
| 93 [==](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators /Comparison_Operators#Equality_()) | |
| 94 so that readers don't have to reason about | |
| 95 [type conversion](http://www.ecma-international.org/ecma-262/6.0/#sec-abst ract-equality-comparison). | |
| 96 | |
| 97 * Tests should be as **cross-platform** as reasonably possible. Avoid | |
| 98 assumptions about device type, screen resolution, etc. Unavoidable assumptions | |
| 99 should be documented. | |
| 100 * When possible, tests should only use Web platform features, as specified | |
| 101 in the relevant standards. | |
| 102 * Test pages should use the HTML5 doctype (`<!doctype html>`) unless they | |
| 103 are testing the quirks mode. | |
| 104 * Tests should be written under the assumption that they will be upstreamed | |
| 105 to the WPT project. For example, tests should follow the | |
| 106 [WPT guidelines](http://testthewebforward.org/docs/writing-tests.html). | |
| 107 * Tests that use Blink-specific testing APIs should feature-test for the | |
| 108 presence of the testing APIs and degrade to | |
| 109 [manual tests](http://testthewebforward.org/docs/manual-test.html) | |
| 110 when the testing APIs are not present. | |
| 111 | |
| 112 * Tests must be **self-contained** and not depend on external network resources. | |
| 113 Unless used by multiple test files, CSS and JavaScript should be inlined using | |
| 114 `<style>` and `<script>` tags. Content shared by multiple tests should be | |
| 115 placed in a `resources/` directory near the tests that share it. See below for | |
| 116 using multiple origins in a test. | |
| 117 | |
| 118 * Test **file names** should describe what is being tested. File names should | |
| 119 use `snake-case`, but preserve the case of any embedded API names. For | |
| 120 example, prefer `document-createElement.html` to | |
| 121 `document-create-element.html`. | |
| 122 | |
| 123 * Tests should prefer **modern features** in JavaScript and in the Web Platform. | |
| 124 * JavaScript code should prefer | |
| 125 [const](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statem ents/const) | |
| 126 and | |
| 127 [let](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statemen ts/let) | |
| 128 over `var`, prefer | |
| 129 [classes](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Clas ses) | |
| 130 over other OOP constructs, and prefer | |
| 131 [Promises](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Glo bal_Objects/Promise) | |
| 132 over other mechanisms for structuring asynchronous code. | |
| 133 * The desire to use modern features must be balanced with the desire for | |
| 134 cross-platform tests. Avoid using features that haven't shipped by other | |
| 135 current major rendering engines (WebKit, Gecko, Edge). When unsure, check | |
| 136 [caniuse.com](http://caniuse.com/). | |
| 137 | |
| 138 * Tests must use the UTF-8 **character encoding**, which should be declared by | |
| 139 `<meta charset=utf-8>`. This does not apply when specifically testing | |
| 140 encodings. | |
| 141 | |
| 142 * Tests must aim to have a **coding style** that is consistent with | |
| 143 [Google's JavaScript Style Guide](https://google.github.io/styleguide/javascri ptguide.xml), | |
| 144 and | |
| 145 [Google's HTML/CSS Style Guide](https://google.github.io/styleguide/htmlcssgui de.xml), | |
| 146 with the following exceptions. | |
| 147 * Rules related to Google Closure and JSDoc do not apply. | |
| 148 * Modern Web Platform and JavaScript features should be preferred to legacy | |
| 149 constructs that target old browsers. For example, prefer `const` and `let` | |
| 150 to `var`, and prefer `class` over other OOP constructs. This should be | |
| 151 balanced with the desire to have cross-platform tests. | |
| 152 * Concerns regarding buggy behavior in legacy browsers do not apply. For | |
| 153 example, the garbage collection cycle note in the _Closures_ section does | |
| 154 not apply. | |
| 155 * Per the JavaScript guide, new tests should also follow any per-project | |
| 156 style guide, such as the | |
| 157 [ServiceWorker Tests Style guide](http://www.chromium.org/blink/servicewor ker/testing). | |
| 158 | |
| 159 ## JavaScript Tests | |
| 160 | |
| 161 Whenever possible, the testing criteria should be expressed in JavaScript. The | |
| 162 alternatives, which will be described in future sections, result in slower and | |
| 163 less robust tests. | |
| 164 | |
| 165 All new JavaScript tests should be written using the | |
| 166 [testharness.js](https://github.com/w3c/testharness.js/) testing framework. This | |
| 167 framework is used by the tests in the | |
| 168 [web-platform-tests](https://github.com/w3c/web-platform-tests) repository, | |
| 169 which is shared with all the other browser vendors, so `testharness.js` tests | |
| 170 are more accessible to browser developers. | |
| 171 | |
| 172 As a shared framework, `testharness.js` enjoys high-quality documentation, such | |
| 173 as [a tutorial](http://testthewebforward.org/docs/testharness-tutorial.html) and | |
| 174 [API documentation](https://github.com/w3c/testharness.js/blob/master/docs/api.m d). | |
| 175 Layout tests should follow the recommendations of the above documents. | |
| 176 Furthermore, layout tests should include relevant | |
| 177 [metadata](http://testthewebforward.org/docs/css-metadata.html). The | |
| 178 specification URL (in `<link rel="help">`) is almost always relevant, and is | |
| 179 incredibly helpful to a developer who needs to understand the test quickly. | |
| 180 | |
| 181 Below is a skeleton for a JavaScript test embedded in an HTML page. Note that, | |
| 182 in order to follow the minimality guideline, the test omits the tags `<html>`, | |
| 183 `<head>` and `<body>`, as they can be inferred by the HTML parser. | |
| 184 | |
| 185 ```html | |
| 186 <!doctype html> | |
| 187 <meta charset="utf-8"> | |
| 188 <title>JavaScript: the true literal</title> | |
| 189 <link rel="help" href="https://tc39.github.io/ecma262/#sec-boolean-literals"> | |
| 190 <meta name="assert" value="The true literal is equal to itself and immutable"> | |
| 191 <script src="/resources/testharness.js"></script> | |
| 192 <script src="/resources/testharnessreport.js"></script> | |
| 193 <script> | |
| 194 | |
| 195 // Synchronous test example. | |
| 196 test(() => { | |
| 197 const value = true; | |
| 198 assert_true(value, 'true literal'); | |
| 199 assert_equal(!value, false, 'the logical negtion of true'); | |
|
foolip
2016/11/18 11:30:40
I guess assert_false here, since you'll have a cha
pwnall
2016/11/22 20:32:55
I changed the assertion to assert_equals(value.toS
foolip
2016/11/23 09:23:59
Thanks, that looks good!
| |
| 200 }, 'The literal true in a synchronous test case'); | |
| 201 | |
| 202 // Asynchronous test example. | |
| 203 async_test(t => { | |
| 204 const originallyTrue = true; | |
| 205 setTimeout(t.step_func_done(() => { | |
| 206 const value = true; | |
| 207 assert_true(originallyTrue); | |
|
foolip
2016/11/18 11:30:40
assert_equals(value, orginallyTrue), or maybe the
pwnall
2016/11/22 20:32:55
Thanks for noticing! I remove the value variable a
| |
| 208 }), 0); | |
| 209 }, 'The literal true in a setTimeout callback'); | |
| 210 | |
| 211 // Promise test example. | |
| 212 promise_test(() => { | |
| 213 return new Promise((resolve, reject) => { | |
| 214 resolve(true); | |
| 215 }).then(value => { | |
| 216 assert_true(value); | |
| 217 }); | |
| 218 }, 'The literal true used to resolve a Promise'); | |
| 219 | |
| 220 </script> | |
| 221 ``` | |
| 222 | |
| 223 Some points that are not immediately obvious from the example: | |
| 224 | |
| 225 * The `<meta name="assert">` describes the purpose of the entire file, and | |
| 226 is not redundant to `<title>`. Don't add a `<meta name="assert">` when the | |
| 227 information in the `<title>` is sufficient. | |
| 228 * When calling an `assert_` function that compares two values, the first | |
| 229 argument is the actual value (produced by the functionality being tested), and | |
| 230 the second argument is the expected value (known good, golden). The order | |
| 231 is important, because the testing harness relies on it to generate expressive | |
| 232 error messages that are relied upon when debugging test failures. | |
| 233 * The assertion description (the string argument to `assert_` methods) conveys | |
| 234 the way the actual value was obtained. | |
| 235 * If the expected value doesn't make it clear, the assertion description | |
| 236 should explain the desired behavior. | |
| 237 * Test cases with a single assertion should omit the assertion's description | |
| 238 when it is sufficiently clear. | |
| 239 * Each test case describes the circumstance that it tests, without being | |
| 240 redundant. | |
| 241 * Do not start test case descriptions with redundant terms like "Testing " | |
|
foolip
2016/11/18 11:30:40
This whole block is great! Nit here is extra space
pwnall
2016/11/22 20:32:55
Done.
Thank you for catching this!
Totally my faul
| |
| 242 or "Test for". | |
| 243 * Test files with a single test case should omit the test case description. | |
| 244 The file's `<title>` should be sufficient to describe the scenario being | |
| 245 tested. | |
| 246 * Asynchronous tests have a few subtleties. | |
| 247 * The `async_test` wrapper calls its function with a test case argument that | |
| 248 is used to signal when the test case is done, and to connect assertion | |
| 249 failures to the correct test. | |
| 250 * `t.done()` must be called after all the test case's assertions have | |
| 251 executed. | |
| 252 * Test case assertions must be wrapped in `t.step_func()` calls, so that | |
|
foolip
2016/11/18 11:30:40
Callbacks that don't have assertions but could pla
pwnall
2016/11/22 20:32:55
Good point! I amended the description.
| |
| 253 assertion failures can be connected to the correct test case. | |
| 254 * `t.step_func_done()` is a shortcut that combines `t.step_func()` with a | |
| 255 `t.done()` call. | |
| 256 | |
| 257 *** promo | |
| 258 Layout tests that load from `file://` origins must currently use relative paths | |
| 259 to point to | |
| 260 [/resources/testharness.js](../../third_party/WebKit/LayoutTests/resources/testh arness.js) | |
| 261 and | |
| 262 [/resources/testharnessreport.js](../../third_party/WebKit/LayoutTests/resources /testharnessreport.js). | |
| 263 This is contrary to the WPT guidelines, which call for absolute paths. | |
| 264 This limitation does not apply to the tests in `LayoutTests/http`, which rely on | |
| 265 an HTTP server, or to the tests in `LayoutTests/imported/wpt`, which are | |
| 266 imported from the [WPT repository](https://github.com/w3c/web-platform-tests). | |
| 267 *** | |
| 268 | |
| 269 | |
| 270 ### Relying on Blink-Specific Testing APIs | |
| 271 | |
| 272 Tests that cannot be expressed using the Web Platform APIs rely on | |
| 273 Blink-specific testing APIs. These APIs are only available in | |
| 274 [content_shell](./layout_tests_in_content_shell.md). | |
| 275 | |
| 276 ### Manual Tests | |
| 277 | |
| 278 Whenever possible, tests that rely on Blink-specific testing APIs should also be | |
| 279 usable as [manual tests](http://testthewebforward.org/docs/manual-test.html). | |
| 280 This makes it easy to debug the test, and to check whether our behavior matches | |
| 281 other browsers. | |
| 282 | |
| 283 Manual tests should minimize the chance of user error. This implies keeping the | |
| 284 manual steps to a minimum, and having simple and clear instructions that | |
| 285 describe all the configuration changes and user gestures that match the effect | |
| 286 of the Blink-specific APIs used by the test. | |
| 287 | |
| 288 Below is an example of a fairly minimal test that uses a Blink-Specific API | |
| 289 (`window.eventSender`), and gracefully degrades to a manual test. | |
| 290 | |
| 291 ```html | |
| 292 <!doctype html> | |
| 293 <meta charset="utf-8"> | |
| 294 <title>DOM: Event.isTrusted for UI events</title> | |
| 295 <link rel="help" href="https://dom.spec.whatwg.org/#dom-event-istrusted"> | |
| 296 <link rel="help" href="https://dom.spec.whatwg.org/#constructing-events"> | |
| 297 <meta name="assert" | |
| 298 content="Event.isTrusted is true for events generated by user interaction"> | |
| 299 <script src="../../resources/testharness.js"></script> | |
| 300 <script src="../../resources/testharnessreport.js"></script> | |
| 301 | |
| 302 <p>Please click on the button below.</p> | |
| 303 <button>Click Me!</button> | |
| 304 | |
| 305 <script> | |
| 306 | |
| 307 setup({ explicit_timeout: true }); | |
| 308 | |
| 309 promise_test(() => { | |
| 310 const button = document.querySelector('button'); | |
| 311 return new Promise((resolve, reject) => { | |
| 312 const button = document.querySelector('button'); | |
| 313 button.addEventListener('click', (event) => { | |
| 314 clickEvent = event; | |
| 315 resolve(event); | |
| 316 }); | |
| 317 | |
| 318 if (window.eventSender) { | |
| 319 eventSender.mouseMoveTo(button.offsetLeft, button.offsetTop); | |
| 320 eventSender.mouseDown(); | |
| 321 eventSender.mouseUp(); | |
| 322 } | |
| 323 }).then((clickEvent) => { | |
| 324 assert_true(clickEvent.isTrusted); | |
| 325 }); | |
| 326 | |
| 327 }, 'Click generated by user interaction'); | |
| 328 | |
| 329 </script> | |
| 330 ``` | |
| 331 | |
| 332 The test exhibits the following desirable features: | |
| 333 | |
| 334 * It has a second specification URL (`<link rel="help">`), because the paragraph | |
| 335 that documents the tested feature (referenced by the primary URL) is not very | |
| 336 informative on its own. | |
| 337 * It links to the | |
| 338 [WHATWG Living Standard](https://wiki.whatwg.org/wiki/FAQ#What_does_.22Living_ Standard.22_mean.3F), | |
| 339 rather than to a frozen version of the specification. | |
| 340 * It documents its assertions clearly. | |
| 341 * The `<meta name="assert">` describes the purpose of the entire file. | |
| 342 However, don't add a `<meta>` when the information in the `<title>` is | |
| 343 sufficient. | |
| 344 * The `assert_equals` string describes the way the actual value was | |
|
foolip
2016/11/18 11:30:40
The above test doesn't have assert_equals any more
pwnall
2016/11/22 20:32:54
Done.
This block was duplicated (and then edited)
| |
| 345 obtained. If the expected value doesn't make it clear, the assertion | |
| 346 description should explain the desired behavior. | |
| 347 * Each test case describes the circumstance that it tests. | |
| 348 * It contains clear instructions for manually triggering the test conditions. | |
| 349 The test starts with a paragraph (`<p>`) that tells the tester exactly what to | |
| 350 do, and the `<button>` that needs to be clicked is clearly labeled. | |
| 351 * It disables the timeout mechanism built into `testharness.js` by calling | |
| 352 `setup({ explicit_timeout: true });` | |
| 353 * It checks for the presence of the Blink-specific testing APIs | |
| 354 (`window.eventSender`) before invoking them. The test does not automatically | |
| 355 fail when the APIs are not present. | |
| 356 * It uses [Promises](https://developer.mozilla.org/docs/Web/JavaScript/Reference /Global_Objects/Promise) | |
| 357 to separate the test setup from the assertions. This is particularly helpful | |
| 358 for manual tests that depend on a sequence of events to occur, as Promises | |
| 359 offer a composable way to express waiting for asynchronous events that avoids | |
| 360 [callback hell](http://stackabuse.com/avoiding-callback-hell-in-node-js/). | |
| 361 | |
| 362 Notice that the test is pretty heavy compared to a minimal JavaScript test that | |
| 363 does not rely on testing APIs. Only use Blink-specific testing APIs when the | |
| 364 desired testing conditions cannot be set up using Web Platform APIs. | |
| 365 | |
| 366 #### Using Blink-Specific Testing APIs | |
| 367 | |
| 368 A downside of Blink-specific APIs is that they are not as well documented as the | |
| 369 Web Platform features. Learning to use a Blink-specific feature requires finding | |
| 370 other tests that use it, or reading its source code. | |
| 371 | |
| 372 For example, the most popular Blink-specific API is `testRunner`, which is | |
| 373 implemented in | |
| 374 [components/test_runner/test_runner.h](../../components/test_runner/test_runner. h) | |
| 375 and | |
| 376 [components/test_runner/test_runner.cpp](../../components/test_runner/test_runne r.cpp). | |
| 377 By skimming the `TestRunnerBindings::Install` method, we learn that the | |
| 378 testRunner API is presented by the `window.testRunner` and | |
| 379 `window.layoutTestsController` objects, which are synonyms. Reading the | |
| 380 `TestRunnerBindings::GetObjectTemplateBuilder` method tells us what properties | |
| 381 are available on the `window.testRunner` object. | |
| 382 | |
| 383 *** aside | |
| 384 `window.testRunner` is the preferred way to access the `testRunner` APIs. | |
| 385 `window.layoutTestsController` is still supported because it is used by | |
| 386 3rd-party tests. | |
| 387 *** | |
| 388 | |
| 389 *** note | |
| 390 `testRunner` is the most popular testing API because it is also used indirectly | |
| 391 by tests that stick to Web Platform APIs. The `testharnessreport.js` file in | |
| 392 `testharness.js` is specifically designated to hold glue code that connects | |
| 393 `testharness.js` to the testing environment. Our implementation is in | |
| 394 [third_party/WebKit/LayoutTests/resources/testharnessreport.js](../../third_part y/WebKit/LayoutTests/resources/testharnessreport.js), | |
| 395 and uses the `testRunner` API. | |
| 396 *** | |
| 397 | |
| 398 See the [components/test_runner/](../../components/test_runner/) directory and | |
| 399 [WebKit's LayoutTests guide](https://trac.webkit.org/wiki/Writing%20Layout%20Tes ts%20for%20DumpRenderTree) | |
| 400 for other useful APIs. For example, `window.eventSender` | |
| 401 ([components/test_runner/event_sender.h](../../components/test_runner/event_send er.h) | |
| 402 and | |
| 403 [components/test_runner/event_sender.cpp](../../components/test_runner/event_sen der.cpp)) | |
| 404 has methods that simulate events input such as keyboard / mouse input and | |
| 405 drag-and-drop. | |
| 406 | |
| 407 Here is a UML diagram of how the `testRunner` bindings fit into Chromium. | |
| 408 | |
| 409 [](https://docs.google .com/drawings/d/1KNRNjlxK0Q3Tp8rKxuuM5mpWf4OJQZmvm9_kpwu_Wwg/edit) | |
| 410 | |
| 411 ### Text Test Baselines | |
| 412 | |
| 413 By default, all the test cases in a file that uses `testharness.js` are expected | |
| 414 to pass. However, in some cases, we prefer to add failing test cases to the | |
| 415 repository, so that we can be notified when the failure modes change (e.g., we | |
| 416 want to know if a test starts crashing rather than returning incorrect output). | |
| 417 In these situations, a test file will be accompanied by a baseline, which is an | |
| 418 `-expected.txt` file that contains the test's expected output. | |
| 419 | |
| 420 The baselines are generated automatically when appropriate by | |
| 421 `run-webkit-tests`, which is described [here](./layout_tests.md), and by the | |
| 422 [rebaselining tools](./layout_test_expectations.md). | |
| 423 | |
| 424 Text baselines for `testharness.js` should be avoided, as having a text baseline | |
| 425 associated with a `testharness.js` indicates the presence of a bug. For this | |
| 426 reason, CLs that add text baselines must include a | |
| 427 [crbug.com](https://crbug.com) link for an issue tracking the removal of the | |
| 428 text expectations. | |
| 429 | |
| 430 * When creating tests that will be upstreamed to WPT, and Blink's current | |
| 431 behavior does not match the specification that is being tested, a text | |
| 432 baseline is necessary. Remember to create an issue tracking the expectation's | |
| 433 removal, and to link the issue in the CL description. | |
| 434 * Layout tests that cannot be upstreamed to WPT should use JavaScript to | |
| 435 document Blink's current behavior, rather than using JavaScript to document | |
| 436 desired behavior and a text file to document current behavior. | |
| 437 | |
| 438 | |
| 439 ### The js-test.js Legacy Harness | |
| 440 | |
| 441 *** promo | |
| 442 For historical reasons, older tests are written using the `js-test` harness. | |
| 443 This harness is **deprecated**, and should not be used for new tests. | |
| 444 *** | |
| 445 | |
| 446 If you need to understand old tests, the best `js-test` documentation is its | |
| 447 implementation at | |
| 448 [third_party/WebKit/LayoutTests/resources/js-test.js](../../third_party/WebKit/L ayoutTests/resources/js-test.js). | |
| 449 | |
| 450 `js-test` tests lean heavily on the Blink-specific `testRunner` testing API. | |
| 451 In a nutshell, the tests call `testRunner.dumpAsText()` to signal that the page | |
| 452 content should be dumped and compared against a text baseline (an | |
| 453 `-expected.txt` file). As a consequence, `js-test` tests are always accompanied | |
| 454 by text baselines. Asynchronous tests also use `testRunner.waitUntilDone()` and | |
| 455 `testRunner.notifyDone()` to tell the testing tools when they are complete. | |
| 456 | |
| 457 ### Tests that use an HTTP Server | |
| 458 | |
| 459 By default, tests are loaded as if via `file:` URLs. Some web platform features | |
| 460 require tests served via HTTP or HTTPS, for example absolute paths (`src=/foo`) | |
| 461 or features restricted to secure protocols. | |
| 462 | |
| 463 HTTP tests are those tests that are under `LayoutTests/http/tests` (or virtual | |
| 464 variants). Use a locally running HTTP server (Apache) to run them. Tests are | |
| 465 served off of ports 8000 and 8080 for HTTP, and 8443 for HTTPS. If you run the | |
| 466 tests using `run-webkit-tests`, the server will be started automatically. To run | |
| 467 the server manually to reproduce or debug a failure: | |
| 468 | |
| 469 ```bash | |
| 470 cd src/third_party/WebKit/Tools/Scripts | |
| 471 run-blink-httpd start | |
| 472 ``` | |
| 473 | |
| 474 The layout tests will be served from `http://127.0.0.1:8000`. For example, to | |
| 475 run the test `http/tests/serviceworker/chromium/service-worker-allowed.html`, | |
| 476 navigate to | |
| 477 `http://127.0.0.1:8000/serviceworker/chromium/service-worker-allowed.html`. Some | |
| 478 tests will behave differently if you go to 127.0.0.1 instead of localhost, so | |
| 479 use 127.0.0.1. | |
| 480 | |
| 481 To kill the server, run `run-blink-httpd --server stop`, or just use `taskkill` | |
| 482 or the Task Manager on Windows, and `killall` or Activity Monitor on MacOS. | |
| 483 | |
| 484 The test server sets up an alias to `LayoutTests/resources` directory. In HTTP | |
| 485 tests, you can access the testing framework at e.g. | |
| 486 `src="/js-test-resources/js-test.js"`. | |
| 487 | |
| 488 TODO: Document [wptserve](http://wptserve.readthedocs.io/) when we are in a | |
| 489 position to use it to run layout tests. | |
| 490 | |
| 491 ## Reference Tests | |
| 492 | |
| 493 *** promo | |
| 494 In the long term, we intend to express reference tests as | |
| 495 [WPT reftests](http://testthewebforward.org/docs/reftests.html). Currently, | |
| 496 Chromium's testing infrastructure does not support WPT reftests. In the | |
|
foolip
2016/11/18 11:31:42
qyearsley@, I think this maybe doesn't quite captu
qyearsley
2016/11/18 18:02:08
Yeah, rather than saying that we "don't support WP
foolip
2016/11/21 09:45:02
Keeping -expected.html outside of WPT sounds good
pwnall
2016/11/22 20:32:55
In the spirit of writing tests that can be upstrea
foolip
2016/11/23 09:23:59
I think using just one model at a time is preferab
| |
| 497 meantime, please use the legacy reference tests format described below. | |
| 498 *** | |
| 499 | |
| 500 *** note | |
| 501 TODO: Summarize best practices for reftests and give examples. | |
| 502 *** | |
| 503 | |
| 504 ### Legacy Reference Tests | |
| 505 | |
| 506 Blink also has inherited a sizable amount of | |
| 507 [reftests](https://trac.webkit.org/wiki/Writing%20Reftests) from WebKit. In | |
| 508 these tests, the reference page file name is based on the test page's file name | |
| 509 and an `-expected.html` suffix. | |
|
qyearsley
2016/11/18 18:02:08
Not sure if it's worth mentioning that it's also p
foolip
2016/11/21 09:45:02
+1, this can be powerful, and the first time I wan
pwnall
2016/11/22 20:32:54
Thank you very much for explaining all this! It is
| |
| 510 | |
| 511 ## Pixel Tests | |
| 512 | |
| 513 `testRunner` APIs such as `window.testRunner.dumpAsTextWithPixelResults()` and | |
| 514 `window.testRunner.dumpDragImage()` create an image result that is associated | |
| 515 with the test. The image result is compared against an image baseline, which is | |
| 516 an `-expected.png` file associated with the test, and the test passes if the | |
| 517 image result is identical to the baseline, according to a pixel-by-pixel | |
| 518 comparison. Tests that have image results (and baselines) are called **pixel | |
| 519 tests**. | |
| 520 | |
| 521 Pixel tests should still follow the principles laid out above. Pixel tests pose | |
| 522 unique challenges to the desire to have *self-describing* and *cross-platform* | |
| 523 tests. The | |
| 524 [WPT test style guidelines](http://testthewebforward.org/docs/test-style-guideli nes.html) | |
| 525 contain useful guidance. The most relevant pieces of advice are below. | |
| 526 | |
| 527 * use a green paragraph / page / square to indicate success | |
| 528 * use the red color or the word `FAIL` to highlight errors | |
| 529 * use the [Ahem font](https://www.w3.org/Style/CSS/Test/Fonts/Ahem/README) to | |
| 530 minimize the variance introduced by the platform's text rendering system | |
| 531 | |
| 532 The following snippet includes the Ahem font in a layout test. | |
| 533 | |
| 534 ```html | |
| 535 <style> | |
| 536 body { | |
| 537 font: 10px Ahem; | |
| 538 } | |
| 539 </style> | |
| 540 <script src="/resources/ahem.js"></script> | |
| 541 ``` | |
| 542 | |
| 543 *** promo | |
| 544 Tests outside `LayoutTests/http` and `LayoutTests/imported/wpt` currently need | |
| 545 to use a relative path to | |
| 546 [/third_party/WebKit/LayoutTests/resources/ahem.js](../../third_party/WebKit/Lay outTests/resources/ahem.js) | |
| 547 *** | |
| 548 | |
| 549 ### Tests that need to paint, raster, or draw a frame of intermediate output | |
| 550 | |
| 551 A layout test does not actually draw frames of output until the test exits. If | |
| 552 it is required to generate a painted frame, then use | |
| 553 `window.testRunner.displayAsyncThen`, which will run the machinery to put up a | |
| 554 frame, then call the passed callback. There is also a library at | |
| 555 `fast/repaint/resources/text-based-repaint.js` to help with writing paint | |
| 556 invalidation and repaint tests. | |
| 557 | |
| 558 ## Directory Structure | |
| 559 | |
| 560 The [LayoutTests directory](../../third_party/WebKit/LayoutTests) currently | |
| 561 lacks a strict, formal structure. The following directories have special | |
| 562 meaning: | |
| 563 | |
| 564 * The `http/` directory hosts tests that require a HTTP server (see above). | |
| 565 * The `resources/` subdirectory in every directory contains binary files, such | |
| 566 as media files, and code that is shared by multiple test files. | |
| 567 | |
| 568 *** note | |
| 569 Some layout tests consist of a minimal HTML page that references a JavaScript | |
| 570 file in `resources/`. Please do not use this pattern for new tests, as it goes | |
| 571 against the minimality principle. JavaScript and CSS files should only live in | |
| 572 `resources/` if they are shared by at least two test files. | |
| 573 *** | |
| OLD | NEW |