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) | |
|
Rick Byers
2016/11/21 22:37:15
nit: there's technically more than just WPT (eg. t
pwnall
2016/11/22 20:32:55
I would rather have specific projects and clear li
| |
| 18 (WPT). This helps us avoid regressions, and helps us identify Web Platform | |
| 19 areas where the major browsers don't have interoperable implementations. | |
|
Rick Byers
2016/11/21 22:37:14
also mention that this lowers the cost of testing
pwnall
2016/11/22 20:32:55
Done.
| |
| 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. | |
|
Rick Byers
2016/11/21 22:37:15
why "layout code"? Most of layout can be tested i
pwnall
2016/11/22 20:32:55
Done.
I replaced "layout" with paint, and will see
| |
| 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 * Tests should use | |
| 125 [strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe rence/Strict_mode) | |
| 126 for all JavaScript, except when specifically testing sloppy mode behavior. | |
| 127 Strict mode flags deprecated features and helps catch some errors, such as | |
| 128 forgetting to declare variables. | |
| 129 * JavaScript code should prefer | |
| 130 [const](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statem ents/const) | |
| 131 and | |
| 132 [let](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statemen ts/let) | |
| 133 over `var`, prefer | |
| 134 [classes](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Clas ses) | |
| 135 over other OOP constructs, and prefer | |
| 136 [Promises](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Glo bal_Objects/Promise) | |
| 137 over other mechanisms for structuring asynchronous code. | |
| 138 * The desire to use modern features must be balanced with the desire for | |
| 139 cross-platform tests. Avoid using features that haven't shipped by other | |
| 140 current major rendering engines (WebKit, Gecko, Edge). When unsure, check | |
| 141 [caniuse.com](http://caniuse.com/). | |
| 142 | |
| 143 * Tests must use the UTF-8 **character encoding**, which should be declared by | |
| 144 `<meta charset=utf-8>`. This does not apply when specifically testing | |
| 145 encodings. | |
| 146 | |
| 147 * Tests must aim to have a **coding style** that is consistent with | |
| 148 [Google's JavaScript Style Guide](https://google.github.io/styleguide/javascri ptguide.xml), | |
| 149 and | |
| 150 [Google's HTML/CSS Style Guide](https://google.github.io/styleguide/htmlcssgui de.xml), | |
| 151 with the following exceptions. | |
| 152 * Rules related to Google Closure and JSDoc do not apply. | |
| 153 * Modern Web Platform and JavaScript features should be preferred to legacy | |
| 154 constructs that target old browsers. For example, prefer `const` and `let` | |
| 155 to `var`, and prefer `class` over other OOP constructs. This should be | |
| 156 balanced with the desire to have cross-platform tests. | |
| 157 * Concerns regarding buggy behavior in legacy browsers do not apply. For | |
| 158 example, the garbage collection cycle note in the _Closures_ section does | |
| 159 not apply. | |
| 160 * Per the JavaScript guide, new tests should also follow any per-project | |
| 161 style guide, such as the | |
| 162 [ServiceWorker Tests Style guide](http://www.chromium.org/blink/servicewor ker/testing). | |
| 163 | |
| 164 ## JavaScript Tests | |
| 165 | |
| 166 Whenever possible, the testing criteria should be expressed in JavaScript. The | |
| 167 alternatives, which will be described in future sections, result in slower and | |
| 168 less robust tests. | |
| 169 | |
| 170 All new JavaScript tests should be written using the | |
| 171 [testharness.js](https://github.com/w3c/testharness.js/) testing framework. This | |
| 172 framework is used by the tests in the | |
| 173 [web-platform-tests](https://github.com/w3c/web-platform-tests) repository, | |
| 174 which is shared with all the other browser vendors, so `testharness.js` tests | |
| 175 are more accessible to browser developers. | |
| 176 | |
| 177 As a shared framework, `testharness.js` enjoys high-quality documentation, such | |
| 178 as [a tutorial](http://testthewebforward.org/docs/testharness-tutorial.html) and | |
| 179 [API documentation](https://github.com/w3c/testharness.js/blob/master/docs/api.m d). | |
| 180 Layout tests should follow the recommendations of the above documents. | |
| 181 Furthermore, layout tests should include relevant | |
| 182 [metadata](http://testthewebforward.org/docs/css-metadata.html). The | |
| 183 specification URL (in `<link rel="help">`) is almost always relevant, and is | |
| 184 incredibly helpful to a developer who needs to understand the test quickly. | |
| 185 | |
| 186 Below is a skeleton for a JavaScript test embedded in an HTML page. Note that, | |
| 187 in order to follow the minimality guideline, the test omits the tags `<html>`, | |
| 188 `<head>` and `<body>`, as they can be inferred by the HTML parser. | |
| 189 | |
| 190 ```html | |
| 191 <!doctype html> | |
| 192 <meta charset="utf-8"> | |
| 193 <title>JavaScript: the true literal</title> | |
| 194 <link rel="help" href="https://tc39.github.io/ecma262/#sec-boolean-literals"> | |
| 195 <meta name="assert" value="The true literal is equal to itself and immutable"> | |
| 196 <script src="/resources/testharness.js"></script> | |
| 197 <script src="/resources/testharnessreport.js"></script> | |
| 198 <script> | |
| 199 'use strict'; | |
| 200 | |
| 201 // Synchronous test example. | |
| 202 test(() => { | |
| 203 const value = true; | |
| 204 assert_true(value, 'true literal'); | |
| 205 assert_false(!value, 'the logical negtion of true'); | |
| 206 }, 'The literal true in a synchronous test case'); | |
| 207 | |
| 208 // Asynchronous test example. | |
| 209 async_test(t => { | |
| 210 const originallyTrue = true; | |
| 211 setTimeout(t.step_func_done(() => { | |
| 212 const value = true; | |
| 213 assert_equal(originallyTrue, true); | |
| 214 }), 0); | |
| 215 }, 'The literal true in a setTimeout callback'); | |
| 216 | |
| 217 // Promise test example. | |
| 218 promise_test(() => { | |
| 219 return new Promise((resolve, reject) => { | |
| 220 resolve(true); | |
| 221 }).then(value => { | |
| 222 assert_true(value); | |
| 223 }); | |
| 224 }, 'The literal true used to resolve a Promise'); | |
| 225 | |
| 226 </script> | |
| 227 ``` | |
| 228 | |
| 229 Some points that are not immediately obvious from the example: | |
| 230 | |
| 231 * The `<meta name="assert">` describes the purpose of the entire file, and | |
| 232 is not redundant to `<title>`. Don't add a `<meta name="assert">` when the | |
| 233 information in the `<title>` is sufficient. | |
| 234 * When calling an `assert_` function that compares two values, the first | |
| 235 argument is the actual value (produced by the functionality being tested), and | |
| 236 the second argument is the expected value (known good, golden). The order | |
| 237 is important, because the testing harness relies on it to generate expressive | |
| 238 error messages that are relied upon when debugging test failures. | |
| 239 * The assertion description (the string argument to `assert_` methods) conveys | |
| 240 the way the actual value was obtained. | |
| 241 * If the expected value doesn't make it clear, the assertion description | |
| 242 should explain the desired behavior. | |
| 243 * Test cases with a single assertion should omit the assertion's description | |
| 244 when it is sufficiently clear. | |
| 245 * Each test case describes the circumstance that it tests, without being | |
| 246 redundant. | |
| 247 * Do not start test case descriptions with redundant terms like "Testing " | |
| 248 or "Test for". | |
| 249 * Test files with a single test case should omit the test case description. | |
| 250 The file's `<title>` should be sufficient to describe the scenario being | |
| 251 tested. | |
| 252 * Asynchronous tests have a few subtleties. | |
| 253 * The `async_test` wrapper calls its function with a test case argument that | |
| 254 is used to signal when the test case is done, and to connect assertion | |
| 255 failures to the correct test. | |
| 256 * `t.done()` must be called after all the test case's assertions have | |
| 257 executed. | |
| 258 * Test case assertions must be wrapped in `t.step_func()` calls, so that | |
| 259 assertion failures can be connected to the correct test case. | |
| 260 * `t.step_func_done()` is a shortcut that combines `t.step_func()` with a | |
| 261 `t.done()` call. | |
| 262 | |
| 263 *** promo | |
| 264 Layout tests that load from `file://` origins must currently use relative paths | |
| 265 to point to | |
| 266 [/resources/testharness.js](../../third_party/WebKit/LayoutTests/resources/testh arness.js) | |
| 267 and | |
| 268 [/resources/testharnessreport.js](../../third_party/WebKit/LayoutTests/resources /testharnessreport.js). | |
| 269 This is contrary to the WPT guidelines, which call for absolute paths. | |
| 270 This limitation does not apply to the tests in `LayoutTests/http`, which rely on | |
| 271 an HTTP server, or to the tests in `LayoutTests/imported/wpt`, which are | |
| 272 imported from the [WPT repository](https://github.com/w3c/web-platform-tests). | |
| 273 *** | |
| 274 | |
| 275 | |
| 276 ### Relying on Blink-Specific Testing APIs | |
| 277 | |
| 278 Tests that cannot be expressed using the Web Platform APIs rely on | |
| 279 Blink-specific testing APIs. These APIs are only available in | |
| 280 [content_shell](./layout_tests_in_content_shell.md). | |
| 281 | |
| 282 ### Manual Tests | |
| 283 | |
| 284 Whenever possible, tests that rely on Blink-specific testing APIs should also be | |
| 285 usable as [manual tests](http://testthewebforward.org/docs/manual-test.html). | |
| 286 This makes it easy to debug the test, and to check whether our behavior matches | |
| 287 other browsers. | |
| 288 | |
| 289 Manual tests should minimize the chance of user error. This implies keeping the | |
| 290 manual steps to a minimum, and having simple and clear instructions that | |
| 291 describe all the configuration changes and user gestures that match the effect | |
| 292 of the Blink-specific APIs used by the test. | |
| 293 | |
| 294 Below is an example of a fairly minimal test that uses a Blink-Specific API | |
| 295 (`window.eventSender`), and gracefully degrades to a manual test. | |
| 296 | |
| 297 ```html | |
| 298 <!doctype html> | |
| 299 <meta charset="utf-8"> | |
| 300 <title>DOM: Event.isTrusted for UI events</title> | |
| 301 <link rel="help" href="https://dom.spec.whatwg.org/#dom-event-istrusted"> | |
| 302 <link rel="help" href="https://dom.spec.whatwg.org/#constructing-events"> | |
| 303 <meta name="assert" | |
| 304 content="Event.isTrusted is true for events generated by user interaction"> | |
| 305 <script src="../../resources/testharness.js"></script> | |
| 306 <script src="../../resources/testharnessreport.js"></script> | |
| 307 | |
| 308 <p>Please click on the button below.</p> | |
| 309 <button>Click Me!</button> | |
| 310 | |
| 311 <script> | |
| 312 'use strict'; | |
| 313 | |
| 314 setup({ explicit_timeout: true }); | |
| 315 | |
| 316 promise_test(() => { | |
| 317 const button = document.querySelector('button'); | |
| 318 return new Promise((resolve, reject) => { | |
| 319 const button = document.querySelector('button'); | |
| 320 button.addEventListener('click', (event) => { | |
| 321 resolve(event); | |
| 322 }); | |
| 323 | |
| 324 if (window.eventSender) { | |
| 325 eventSender.mouseMoveTo(button.offsetLeft, button.offsetTop); | |
| 326 eventSender.mouseDown(); | |
| 327 eventSender.mouseUp(); | |
| 328 } | |
| 329 }).then((clickEvent) => { | |
| 330 assert_true(clickEvent.isTrusted); | |
| 331 }); | |
| 332 | |
| 333 }, 'Click generated by user interaction'); | |
| 334 | |
| 335 </script> | |
| 336 ``` | |
| 337 | |
| 338 The test exhibits the following desirable features: | |
| 339 | |
| 340 * It has a second specification URL (`<link rel="help">`), because the paragraph | |
| 341 that documents the tested feature (referenced by the primary URL) is not very | |
| 342 informative on its own. | |
| 343 * It links to the | |
| 344 [WHATWG Living Standard](https://wiki.whatwg.org/wiki/FAQ#What_does_.22Living_ Standard.22_mean.3F), | |
| 345 rather than to a frozen version of the specification. | |
| 346 * It documents its assertions clearly. | |
| 347 * The `<meta name="assert">` describes the purpose of the entire file. | |
| 348 However, don't add a `<meta>` when the information in the `<title>` is | |
| 349 sufficient. | |
| 350 * The `assert_equals` string describes the way the actual value was | |
| 351 obtained. If the expected value doesn't make it clear, the assertion | |
| 352 description should explain the desired behavior. | |
| 353 * Each test case describes the circumstance that it tests. | |
| 354 * It contains clear instructions for manually triggering the test conditions. | |
| 355 The test starts with a paragraph (`<p>`) that tells the tester exactly what to | |
| 356 do, and the `<button>` that needs to be clicked is clearly labeled. | |
| 357 * It disables the timeout mechanism built into `testharness.js` by calling | |
| 358 `setup({ explicit_timeout: true });` | |
|
Rick Byers
2016/11/21 22:37:14
What's the implication of this on our bots? Eg. s
pwnall
2016/11/22 20:32:55
My experiments suggest that we have an external ti
| |
| 359 * It checks for the presence of the Blink-specific testing APIs | |
| 360 (`window.eventSender`) before invoking them. The test does not automatically | |
| 361 fail when the APIs are not present. | |
| 362 * It uses [Promises](https://developer.mozilla.org/docs/Web/JavaScript/Reference /Global_Objects/Promise) | |
| 363 to separate the test setup from the assertions. This is particularly helpful | |
| 364 for manual tests that depend on a sequence of events to occur, as Promises | |
| 365 offer a composable way to express waiting for asynchronous events that avoids | |
| 366 [callback hell](http://stackabuse.com/avoiding-callback-hell-in-node-js/). | |
| 367 | |
| 368 Notice that the test is pretty heavy compared to a minimal JavaScript test that | |
| 369 does not rely on testing APIs. Only use Blink-specific testing APIs when the | |
| 370 desired testing conditions cannot be set up using Web Platform APIs. | |
| 371 | |
| 372 #### Using Blink-Specific Testing APIs | |
| 373 | |
| 374 A downside of Blink-specific APIs is that they are not as well documented as the | |
| 375 Web Platform features. Learning to use a Blink-specific feature requires finding | |
| 376 other tests that use it, or reading its source code. | |
| 377 | |
| 378 For example, the most popular Blink-specific API is `testRunner`, which is | |
| 379 implemented in | |
| 380 [components/test_runner/test_runner.h](../../components/test_runner/test_runner. h) | |
| 381 and | |
| 382 [components/test_runner/test_runner.cpp](../../components/test_runner/test_runne r.cpp). | |
| 383 By skimming the `TestRunnerBindings::Install` method, we learn that the | |
| 384 testRunner API is presented by the `window.testRunner` and | |
| 385 `window.layoutTestsController` objects, which are synonyms. Reading the | |
| 386 `TestRunnerBindings::GetObjectTemplateBuilder` method tells us what properties | |
| 387 are available on the `window.testRunner` object. | |
| 388 | |
| 389 *** aside | |
| 390 `window.testRunner` is the preferred way to access the `testRunner` APIs. | |
| 391 `window.layoutTestsController` is still supported because it is used by | |
| 392 3rd-party tests. | |
| 393 *** | |
| 394 | |
| 395 *** note | |
| 396 `testRunner` is the most popular testing API because it is also used indirectly | |
| 397 by tests that stick to Web Platform APIs. The `testharnessreport.js` file in | |
| 398 `testharness.js` is specifically designated to hold glue code that connects | |
| 399 `testharness.js` to the testing environment. Our implementation is in | |
| 400 [third_party/WebKit/LayoutTests/resources/testharnessreport.js](../../third_part y/WebKit/LayoutTests/resources/testharnessreport.js), | |
| 401 and uses the `testRunner` API. | |
| 402 *** | |
| 403 | |
| 404 See the [components/test_runner/](../../components/test_runner/) directory and | |
| 405 [WebKit's LayoutTests guide](https://trac.webkit.org/wiki/Writing%20Layout%20Tes ts%20for%20DumpRenderTree) | |
| 406 for other useful APIs. For example, `window.eventSender` | |
| 407 ([components/test_runner/event_sender.h](../../components/test_runner/event_send er.h) | |
| 408 and | |
| 409 [components/test_runner/event_sender.cpp](../../components/test_runner/event_sen der.cpp)) | |
| 410 has methods that simulate events input such as keyboard / mouse input and | |
| 411 drag-and-drop. | |
| 412 | |
| 413 Here is a UML diagram of how the `testRunner` bindings fit into Chromium. | |
| 414 | |
| 415 [](https://docs.google .com/drawings/d/1KNRNjlxK0Q3Tp8rKxuuM5mpWf4OJQZmvm9_kpwu_Wwg/edit) | |
| 416 | |
| 417 ### Text Test Baselines | |
| 418 | |
| 419 By default, all the test cases in a file that uses `testharness.js` are expected | |
| 420 to pass. However, in some cases, we prefer to add failing test cases to the | |
| 421 repository, so that we can be notified when the failure modes change (e.g., we | |
| 422 want to know if a test starts crashing rather than returning incorrect output). | |
| 423 In these situations, a test file will be accompanied by a baseline, which is an | |
| 424 `-expected.txt` file that contains the test's expected output. | |
| 425 | |
| 426 The baselines are generated automatically when appropriate by | |
| 427 `run-webkit-tests`, which is described [here](./layout_tests.md), and by the | |
| 428 [rebaselining tools](./layout_test_expectations.md). | |
| 429 | |
| 430 Text baselines for `testharness.js` should be avoided, as having a text baseline | |
| 431 associated with a `testharness.js` indicates the presence of a bug. For this | |
| 432 reason, CLs that add text baselines must include a | |
| 433 [crbug.com](https://crbug.com) link for an issue tracking the removal of the | |
| 434 text expectations. | |
| 435 | |
| 436 * When creating tests that will be upstreamed to WPT, and Blink's current | |
| 437 behavior does not match the specification that is being tested, a text | |
| 438 baseline is necessary. Remember to create an issue tracking the expectation's | |
| 439 removal, and to link the issue in the CL description. | |
| 440 * Layout tests that cannot be upstreamed to WPT should use JavaScript to | |
| 441 document Blink's current behavior, rather than using JavaScript to document | |
| 442 desired behavior and a text file to document current behavior. | |
| 443 | |
| 444 | |
| 445 ### The js-test.js Legacy Harness | |
| 446 | |
| 447 *** promo | |
| 448 For historical reasons, older tests are written using the `js-test` harness. | |
| 449 This harness is **deprecated**, and should not be used for new tests. | |
| 450 *** | |
| 451 | |
| 452 If you need to understand old tests, the best `js-test` documentation is its | |
| 453 implementation at | |
| 454 [third_party/WebKit/LayoutTests/resources/js-test.js](../../third_party/WebKit/L ayoutTests/resources/js-test.js). | |
| 455 | |
| 456 `js-test` tests lean heavily on the Blink-specific `testRunner` testing API. | |
| 457 In a nutshell, the tests call `testRunner.dumpAsText()` to signal that the page | |
| 458 content should be dumped and compared against a text baseline (an | |
| 459 `-expected.txt` file). As a consequence, `js-test` tests are always accompanied | |
| 460 by text baselines. Asynchronous tests also use `testRunner.waitUntilDone()` and | |
| 461 `testRunner.notifyDone()` to tell the testing tools when they are complete. | |
| 462 | |
| 463 ### Tests that use an HTTP Server | |
| 464 | |
| 465 By default, tests are loaded as if via `file:` URLs. Some web platform features | |
| 466 require tests served via HTTP or HTTPS, for example absolute paths (`src=/foo`) | |
| 467 or features restricted to secure protocols. | |
| 468 | |
| 469 HTTP tests are those tests that are under `LayoutTests/http/tests` (or virtual | |
| 470 variants). Use a locally running HTTP server (Apache) to run them. Tests are | |
| 471 served off of ports 8000 and 8080 for HTTP, and 8443 for HTTPS. If you run the | |
| 472 tests using `run-webkit-tests`, the server will be started automatically. To run | |
| 473 the server manually to reproduce or debug a failure: | |
| 474 | |
| 475 ```bash | |
| 476 cd src/third_party/WebKit/Tools/Scripts | |
| 477 run-blink-httpd start | |
| 478 ``` | |
| 479 | |
| 480 The layout tests will be served from `http://127.0.0.1:8000`. For example, to | |
| 481 run the test `http/tests/serviceworker/chromium/service-worker-allowed.html`, | |
| 482 navigate to | |
| 483 `http://127.0.0.1:8000/serviceworker/chromium/service-worker-allowed.html`. Some | |
| 484 tests will behave differently if you go to 127.0.0.1 instead of localhost, so | |
| 485 use 127.0.0.1. | |
| 486 | |
| 487 To kill the server, run `run-blink-httpd --server stop`, or just use `taskkill` | |
| 488 or the Task Manager on Windows, and `killall` or Activity Monitor on MacOS. | |
| 489 | |
| 490 The test server sets up an alias to `LayoutTests/resources` directory. In HTTP | |
| 491 tests, you can access the testing framework at e.g. | |
| 492 `src="/js-test-resources/js-test.js"`. | |
| 493 | |
| 494 TODO: Document [wptserve](http://wptserve.readthedocs.io/) when we are in a | |
| 495 position to use it to run layout tests. | |
| 496 | |
| 497 ## Reference Tests | |
| 498 | |
| 499 *** promo | |
| 500 In the long term, we intend to express reference tests as | |
| 501 [WPT reftests](http://testthewebforward.org/docs/reftests.html). Currently, | |
| 502 Chromium's testing infrastructure does not support WPT reftests. In the | |
| 503 meantime, please use the legacy reference tests format described below. | |
| 504 *** | |
| 505 | |
| 506 *** note | |
| 507 TODO: Summarize best practices for reftests and give examples. | |
| 508 *** | |
| 509 | |
| 510 ### Legacy Reference Tests | |
| 511 | |
| 512 Blink also has inherited a sizable amount of | |
| 513 [reftests](https://trac.webkit.org/wiki/Writing%20Reftests) from WebKit. In | |
| 514 these tests, the reference page file name is based on the test page's file name | |
| 515 and an `-expected.html` suffix. | |
| 516 | |
| 517 ## Pixel Tests | |
| 518 | |
| 519 `testRunner` APIs such as `window.testRunner.dumpAsTextWithPixelResults()` and | |
| 520 `window.testRunner.dumpDragImage()` create an image result that is associated | |
| 521 with the test. The image result is compared against an image baseline, which is | |
| 522 an `-expected.png` file associated with the test, and the test passes if the | |
| 523 image result is identical to the baseline, according to a pixel-by-pixel | |
| 524 comparison. Tests that have image results (and baselines) are called **pixel | |
| 525 tests**. | |
| 526 | |
| 527 Pixel tests should still follow the principles laid out above. Pixel tests pose | |
| 528 unique challenges to the desire to have *self-describing* and *cross-platform* | |
| 529 tests. The | |
| 530 [WPT test style guidelines](http://testthewebforward.org/docs/test-style-guideli nes.html) | |
| 531 contain useful guidance. The most relevant pieces of advice are below. | |
| 532 | |
| 533 * use a green paragraph / page / square to indicate success | |
| 534 * use the red color or the word `FAIL` to highlight errors | |
| 535 * use the [Ahem font](https://www.w3.org/Style/CSS/Test/Fonts/Ahem/README) to | |
| 536 minimize the variance introduced by the platform's text rendering system | |
| 537 | |
| 538 The following snippet includes the Ahem font in a layout test. | |
| 539 | |
| 540 ```html | |
| 541 <style> | |
| 542 body { | |
| 543 font: 10px Ahem; | |
| 544 } | |
| 545 </style> | |
| 546 <script src="/resources/ahem.js"></script> | |
| 547 ``` | |
| 548 | |
| 549 *** promo | |
| 550 Tests outside `LayoutTests/http` and `LayoutTests/imported/wpt` currently need | |
| 551 to use a relative path to | |
| 552 [/third_party/WebKit/LayoutTests/resources/ahem.js](../../third_party/WebKit/Lay outTests/resources/ahem.js) | |
| 553 *** | |
| 554 | |
| 555 ### Tests that need to paint, raster, or draw a frame of intermediate output | |
| 556 | |
| 557 A layout test does not actually draw frames of output until the test exits. If | |
| 558 it is required to generate a painted frame, then use | |
| 559 `window.testRunner.displayAsyncThen`, which will run the machinery to put up a | |
| 560 frame, then call the passed callback. There is also a library at | |
| 561 `fast/repaint/resources/text-based-repaint.js` to help with writing paint | |
| 562 invalidation and repaint tests. | |
| 563 | |
| 564 ## Directory Structure | |
| 565 | |
| 566 The [LayoutTests directory](../../third_party/WebKit/LayoutTests) currently | |
| 567 lacks a strict, formal structure. The following directories have special | |
| 568 meaning: | |
| 569 | |
| 570 * The `http/` directory hosts tests that require a HTTP server (see above). | |
| 571 * The `resources/` subdirectory in every directory contains binary files, such | |
| 572 as media files, and code that is shared by multiple test files. | |
| 573 | |
| 574 *** note | |
| 575 Some layout tests consist of a minimal HTML page that references a JavaScript | |
| 576 file in `resources/`. Please do not use this pattern for new tests, as it goes | |
| 577 against the minimality principle. JavaScript and CSS files should only live in | |
| 578 `resources/` if they are shared by at least two test files. | |
| 579 *** | |
| OLD | NEW |