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 gives us confidence that we match | |
|
foolip
2016/11/16 13:42:38
To be picky, merely being in WPT doesn't imply muc
pwnall
2016/11/18 05:40:43
Done.
I tried to rephrase. Does this work for you?
foolip
2016/11/18 11:30:40
Wonderful!
| |
| 19 other browsers' behavior. | |
| 20 2. When a Blink feature cannot be tested using the Web Platform, and cannot be | |
|
foolip
2016/11/16 13:42:39
s/Web Platform/WPT/? There will be many things tha
pwnall
2016/11/18 05:40:43
Wouldn't you say these are cases where the Web Pla
foolip
2016/11/18 11:30:40
Yes, that's precisely where I think we must go, to
pwnall
2016/11/22 20:32:54
I was completely unaware of wpt_automation. I adde
| |
| 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. | |
|
foolip
2016/11/16 13:42:38
There's also the dump render tree kind of test. No
pwnall
2016/11/18 05:40:43
I have no idea what I'm talking about, because I h
foolip
2016/11/18 11:30:39
Not entirely I think, DRT also includes pseudo ele
pwnall
2016/11/22 20:32:54
Done.
I did some reading, and wrote a section on D
| |
| 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/W indowTimers/setTimeout), | |
| 64 as it wastes testing time and can introduce flakiness. Instead, use specific | |
| 65 event handlers, such as | |
| 66 [window.onload](https://developer.mozilla.org/en-US/docs/Web/API/GlobalEvent Handlers/onload). | |
|
foolip
2016/11/16 13:42:39
FWIW, when setTimeout is used it's often because t
pwnall
2016/11/18 05:40:43
Done.
Ideally, the document should be turned into
foolip
2016/11/18 11:30:40
Yep, that'd be great, some day.
| |
| 67 | |
| 68 * Tests should be **self-describing**, so that a project member can recognize | |
| 69 whether a test passes or fails without having to read the specification of the | |
| 70 feature being tested. `testharness.js` makes a test self-describing when used | |
| 71 correctly, but tests that degrade to manual tests | |
| 72 [must be carefully designed](http://testthewebforward.org/docs/test-style-guid elines.html) | |
| 73 to be self-describing. | |
| 74 | |
| 75 * Tests should use the **minimal** set of platform features needed to express | |
| 76 the test scenario efficiently. | |
| 77 * Avoid depending on edge case behavior of features that aren't explicitly | |
| 78 covered by the test. For example, except where testing parsing, tests | |
| 79 should contain valid markup (no parsing errors). | |
| 80 * Prefer JavaScript's `===` operator to `==`, so that readers don't have to | |
| 81 reason about type conversion. | |
| 82 | |
| 83 * Tests should be as **cross-platform** as reasonably possible. Avoid | |
| 84 assumptions about device type, screen resolution, etc. Unavoidable assumptions | |
| 85 should be documented. | |
| 86 * When possible, tests should only use Web platform features, as specified | |
| 87 in the relevant standards. | |
| 88 * Test pages should use the HTML5 doctype (`<!doctype html>`) unless they | |
| 89 are testing the quirks mode. | |
| 90 * Tests should be written under the assumption that they will be upstreamed | |
|
foolip
2016/11/16 13:42:39
\o/
pwnall
2016/11/18 05:40:44
Agreed!
| |
| 91 to the WPT project. For example, tests should follow the | |
| 92 [WPT guidelines](http://testthewebforward.org/docs/writing-tests.html). | |
| 93 * Tests that use Blink-specific testing APIs should feature-test for the | |
|
foolip
2016/11/16 13:42:38
This isn't something I've ever asked for in code r
pwnall
2016/11/18 05:40:44
I was definitely asked to do this in (perhaps too
foolip
2016/11/18 11:30:40
I guess consult blink-dev on this question? Leavin
| |
| 94 presence of the testing APIs and degrade to | |
| 95 [manual tests](http://testthewebforward.org/docs/manual-test.html) | |
| 96 when the testing APIs are not present. | |
| 97 | |
| 98 * Tests must be **self-contained** and not depend on external network resources. | |
| 99 Unless used by multiple test files, CSS and JavaScript should be inlined using | |
| 100 `<style>` and `<script>` tags. Content shared by multiple tests should be | |
| 101 placed in a `resources/` directory near the tests that share it. See below for | |
| 102 using multiple origins in a test. | |
| 103 | |
| 104 * Test **file names** should describe what is being tested. File names should | |
| 105 use `snake-case`, but preserve the case of any embedded Web Platform names. | |
|
foolip
2016/11/16 13:42:39
s/Web Platform/API/ perhaps.
pwnall
2016/11/18 05:40:44
Done.
| |
| 106 For example, prefer `document-createElement.html` to | |
| 107 `document-create-element.html`. | |
| 108 | |
| 109 * Tests must use the UTF-8 **character encoding**, which should be declared by | |
|
foolip
2016/11/16 13:42:38
Most tests are in ASCII, so I actually ask people
pwnall
2016/11/18 05:40:43
Firefox Nightly now issues a warning when a page d
foolip
2016/11/18 11:30:40
I think I'd still like the exception, but again pe
pwnall
2016/11/22 20:32:54
Done.
I put the exception in for now, while I seek
| |
| 110 `<meta charset=utf-8>`. This does not apply when specifically testing | |
| 111 encodings. | |
| 112 | |
| 113 * Tests must aim to have a **coding style** that is consistent with | |
| 114 [Google's JavaScript Style Guide](https://google.github.io/styleguide/javascri ptguide.xml), | |
|
foolip
2016/11/16 13:42:38
Is there a lint or format tool for this so that pe
pwnall
2016/11/18 05:40:44
AFAIK that is not yet the case, but I hear that cl
foolip
2016/11/18 11:30:40
Acknowledged.
| |
| 115 and | |
| 116 [Google's HTML/CSS Style Guide](https://google.github.io/styleguide/htmlcssgui de.xml), | |
| 117 with the following exceptions. | |
| 118 * Rules related to Google Closure and JSDoc do not apply. | |
| 119 * Modern Web Platform and JavaScript features should be preferred to legacy | |
|
foolip
2016/11/16 13:42:39
Note that this is can be slightly at odds with ups
pwnall
2016/11/18 05:40:43
Done.
Thank you for reminding me about this balanc
foolip
2016/11/18 11:30:40
Great, these and the above changes get the message
| |
| 120 constructs that target old browsers. For example, prefer `const` and `let` | |
| 121 to `var`, and prefer `class` over other OOP constructs. | |
| 122 * Concerns regarding buggy behavior in legacy browsers do not apply. For | |
| 123 example, the garbage collection cycle note in the _Closures_ section does | |
| 124 not apply. | |
| 125 * Per the JavaScript guide, new tests should also follow any per-project | |
| 126 style guide, such as the | |
| 127 [ServiceWorker Tests Style guide](http://www.chromium.org/blink/servicewor ker/testing). | |
| 128 | |
| 129 ## JavaScript Tests | |
| 130 | |
| 131 Whenever possible, the testing criteria should be expressed in JavaScript. The | |
| 132 alternatives, which will be described in future sections, result in slower and | |
| 133 less robust tests. | |
| 134 | |
| 135 All new JavaScript tests should be written using the | |
| 136 [testharness.js](https://github.com/w3c/testharness.js/) testing framework. This | |
| 137 framework is used by the tests in the | |
| 138 [web-platform-tests](https://github.com/w3c/web-platform-tests) repository, | |
| 139 which is shared with all the other browser vendors, so `testharness.js` tests | |
| 140 are more accessible to browser developers. | |
| 141 | |
| 142 As a shared framework, `testharness.js` enjoys high-quality documentation, such | |
| 143 as [a tutorial](http://testthewebforward.org/docs/testharness-tutorial.html) and | |
| 144 [API documentation](https://github.com/w3c/testharness.js/blob/master/docs/api.m d). | |
| 145 Layout tests should follow the recommendations of the above documents. | |
| 146 Furthermore, layout tests should include relevant | |
| 147 [metadata](http://testthewebforward.org/docs/css-metadata.html). The | |
| 148 specification URL (in `<link rel="help">`) is almost always relevant, and is | |
| 149 incredibly helpful to a developer who needs to understand the test quickly. | |
| 150 | |
| 151 Below is a skeleton for an HTML5 JavaScript test. Note that, in order to follow | |
|
foolip
2016/11/16 13:42:39
Drop "HTML5", just "JavaScript test" seems OK. (ht
pwnall
2016/11/18 05:40:44
Done.
I rephrased to JavaScript test embedded in H
| |
| 152 the minimality guideline, the test omits the tags `<html>`, `<head>` and | |
| 153 `<body>`, as they can be inferred by the HTML5 parser. | |
| 154 | |
| 155 ```html | |
| 156 <!doctype html> | |
| 157 <meta charset="utf-8"> | |
| 158 <title>JavaScript: the true literal</title> | |
| 159 <link rel="help" | |
| 160 href="http://www.ecma-international.org/ecma-262/6.0/#sec-terms-and-defini tions-boolean-value"> | |
|
foolip
2016/11/16 13:42:39
https://tc39.github.io/ecma262/#sec-boolean-litera
pwnall
2016/11/18 05:40:43
Done.
Thank you very much for the shorter link!
I
foolip
2016/11/18 11:30:40
Let's stick with this, presumably literals are alr
| |
| 161 <script src="/resources/testharness.js"></script> | |
| 162 <script src="/resources/testharnessreport.js"></script> | |
| 163 <script> | |
| 164 | |
| 165 // Synchronous test example. | |
| 166 test(t => { | |
|
foolip
2016/11/16 13:42:39
Suggest `() => { }` as the Test instance isn't gen
pwnall
2016/11/18 05:40:43
Done.
| |
| 167 const truthy = true; | |
| 168 assert_true(truthy, 'true should be truthy'); | |
|
foolip
2016/11/16 13:42:39
If it failed, this would say "FAIL JavaScript: the
pwnall
2016/11/18 05:40:43
Done.
I changed the tests to demonstrate the rule
| |
| 169 }, 'The literal true in a synchronous test case'); | |
|
foolip
2016/11/16 13:42:39
This isn't spelled out above, but I think this is
pwnall
2016/11/18 05:40:43
I created a bulleted list below this block of code
| |
| 170 | |
| 171 // Asynchronous test example. | |
| 172 async_test(t => { | |
| 173 setTimeout(t.step_func(() => { | |
| 174 const truthy = true; | |
|
foolip
2016/11/16 13:42:38
Testing a true literal like this becomes rather st
pwnall
2016/11/18 05:40:43
Done.
This does look better. Thank you!
| |
| 175 assert_true(truthy, 'true should be truthy'); | |
| 176 }), 0); | |
| 177 t.done(); | |
|
foolip
2016/11/16 13:42:39
This test will always pass because this is run bef
pwnall
2016/11/18 05:40:43
Done. (pun not intended)
Wow, that error was prett
foolip
2016/11/18 11:30:40
I totally believe that your promise resolves to tr
| |
| 178 }, 'The literal true in a setTimeout callback'); | |
| 179 | |
| 180 // Promise test example. | |
| 181 promise_test(t => { | |
|
foolip
2016/11/16 13:42:39
() here too since t isn't used.
pwnall
2016/11/18 05:40:44
Done.
| |
| 182 return new Promise((resolve, reject) => { | |
| 183 resolve(true); | |
| 184 }).then(truthy => { | |
|
foolip
2016/11/16 13:42:39
s/truthy/value/
pwnall
2016/11/18 05:40:44
Done.
| |
| 185 assert_true(truthy, 'true should be truthy'); | |
| 186 }); | |
| 187 }, 'The literal true in a Promise'); | |
| 188 | |
| 189 </script> | |
| 190 ``` | |
| 191 | |
| 192 ### Relying on Blink-Specific Testing APIs | |
| 193 | |
| 194 Tests that cannot be expressed using the Web Platform APIs rely on | |
| 195 Blink-specific testing APIs. These APIs are only available in | |
| 196 [content_shell](./layout_tests_in_content_shell.md). | |
| 197 | |
| 198 ### Manual Tests | |
| 199 | |
| 200 Whenever possible, tests that rely on Blink-specific testing APIs should also be | |
|
foolip
2016/11/16 13:42:38
Maybe drop the similar bit above and have only thi
pwnall
2016/11/18 05:40:43
Ack.
I will watch out for more opinions on the man
| |
| 201 usable as [manual tests](http://testthewebforward.org/docs/manual-test.html). | |
| 202 This makes it easy to debug the test, and to check whether our behavior matches | |
| 203 other browsers. | |
| 204 | |
| 205 Manual tests should minimize the chance of user error. This implies keeping the | |
| 206 manual steps to a minimum, and having simple and clear instructions that | |
| 207 describe all the configuration changes and user gestures that match the effect | |
| 208 of the Blink-specific APIs used by the test. | |
| 209 | |
| 210 Below is an example of a fairly minimal test that uses a Blink-Specific API | |
| 211 (`window.eventSender`), and gracefully degrades to a manual test. | |
| 212 | |
| 213 ```html | |
| 214 <!doctype html> | |
| 215 <meta charset="utf-8"> | |
| 216 <title>DOM Events: Event.isTrusted for UI events</title> | |
|
foolip
2016/11/16 13:42:39
s/DOM Events/DOM/
pwnall
2016/11/18 05:40:44
Done.
| |
| 217 <link rel="help" href="https://dom.spec.whatwg.org/#dom-event-istrusted"> | |
| 218 <link rel="help" href="https://dom.spec.whatwg.org/#constructing-events"> | |
| 219 <meta name="assert" content="Event.isTrusted value under certain situations"> | |
| 220 <script src="../../resources/testharness.js"></script> | |
| 221 <script src="../../resources/testharnessreport.js"></script> | |
| 222 | |
| 223 <p>Please click on the button below.</p> | |
| 224 <button id="click-me" type="button">Click Me!</button> | |
|
foolip
2016/11/16 13:42:39
id="click-me" and type="button" is not needed, can
pwnall
2016/11/18 05:40:43
Done.
FWIW, I'm a bit wary of using querySelector,
foolip
2016/11/18 11:30:39
Well, I have that bias too, I even use querySelect
| |
| 225 | |
| 226 <script> | |
| 227 | |
| 228 setup({ explicit_timeout: true }); | |
| 229 | |
| 230 promise_test(() => { | |
| 231 return new Promise((resolve, reject) => { | |
| 232 const button = document.getElementById('click-me'); | |
| 233 button.addEventListener('click', (event) => { | |
| 234 clickEvent = event; | |
| 235 resolve(event); | |
| 236 }); | |
| 237 | |
| 238 if (window.eventSender) { | |
| 239 eventSender.mouseMoveTo(button.offsetLeft, button.offsetTop); | |
| 240 eventSender.mouseDown(); | |
| 241 eventSender.mouseUp(); | |
| 242 } | |
| 243 }).then((clickEvent) => { | |
| 244 assert_equals(true, clickEvent.isTrusted, | |
|
foolip
2016/11/16 13:42:39
Expected and actual are reversed here, but just as
pwnall
2016/11/18 05:40:43
Done.
Wow, I failed at a point that I was trying t
foolip
2016/11/18 11:30:40
It's very good to have clear documentation on this
| |
| 245 'User interaction events should have isTrusted set to true'); | |
| 246 }); | |
| 247 | |
| 248 }, 'Click generated by user interaction'); | |
|
foolip
2016/11/16 13:42:39
Omit test description in single-test files with a
pwnall
2016/11/18 05:40:43
I also made this explicit in a new bulleted list a
| |
| 249 | |
| 250 </script> | |
| 251 ``` | |
| 252 | |
| 253 The test exhibits the following desirable features: | |
| 254 | |
| 255 * It has a second specification URL (`<link rel="help">`), because the paragraph | |
| 256 that documents the tested feature (referenced by the primary URL) is not very | |
| 257 informative on its own. | |
| 258 * It links to the | |
| 259 [WHATWG Living Standard](https://wiki.whatwg.org/wiki/FAQ#What_does_.22Living_ Standard.22_mean.3F), | |
| 260 rather than to a frozen version of the specification. | |
| 261 * It uses relative paths to point to | |
|
foolip
2016/11/16 13:42:39
It's not possible to get this wrong outside of Lay
pwnall
2016/11/18 05:40:43
Done.
I described relative paths as a limitation o
foolip
2016/11/18 11:30:40
:)
| |
| 262 [/references/testharness.js](../../third_party/WebKit/LayoutTests/references/t estharness.js) | |
| 263 and | |
| 264 [/references/testharnessreport.js](../../third_party/WebKit/LayoutTests/refere nces/testharnessreport.js), | |
| 265 so a developer can iterate on the test simply by opening its file in a | |
| 266 browser, without having to start a HTTP server. This is only relevant for | |
| 267 tests that work from `file://` origins, and tests that require a HTTP server | |
| 268 should use absolute paths. | |
| 269 * It documents its assertions clearly. | |
| 270 * The `<meta name="assert">` describes the purpose of the entire file. | |
|
foolip
2016/11/16 13:42:39
Can you throw on something like "Only use when it
pwnall
2016/11/18 05:40:44
Done.
| |
| 271 * The `assert_equals` string describes the expected behavior, not the error. | |
|
foolip
2016/11/16 13:42:39
The expected value (second argument) describes the
pwnall
2016/11/18 05:40:43
Done.
| |
| 272 * Each test case describes the circumstance that it tests. | |
| 273 * It contains clear instructions for manually triggering the test conditions. | |
| 274 The test starts with a paragraph (`<p>`) that tells the tester exactly what to | |
| 275 do, and the `<button>` that needs to be clicked is clearly labeled. | |
| 276 * It disables the timeout mechanism built into `testharness.js` by calling | |
| 277 `setup({ explicit_timeout: true });` | |
| 278 * It checks for the presence of the Blink-specific testing APIs | |
| 279 (`window.eventSender`) before invoking them. The test does not automatically | |
| 280 fail when the APIs are not present. | |
| 281 * It uses [Promises](https://developer.mozilla.org/docs/Web/JavaScript/Reference /Global_Objects/Promise) | |
|
foolip
2016/11/16 13:42:39
This test I would argue would be smaller and clear
pwnall
2016/11/18 05:40:44
I agree. I still think that it's more valuable to
foolip
2016/11/18 11:30:40
OK, I'll take both styles! One benefit of promise_
| |
| 282 to separate the test setup from the assertions. This is particularly helpful | |
| 283 for manual tests that depend on a sequence of events to occur, as Promises | |
| 284 offer a composable way to express waiting for asynchronous events that avoids | |
| 285 [callback hell](http://stackabuse.com/avoiding-callback-hell-in-node-js/). | |
| 286 | |
| 287 Notice that the test is pretty heavy compared to a minimal JavaScript test that | |
| 288 does not rely on testing APIs. Only use Blink-specific testing APIs when the | |
| 289 desired testing conditions cannot be set up using Web Platform APIs. | |
| 290 | |
| 291 #### Using Blink-Specific Testing APIs | |
| 292 | |
| 293 A downside of Blink-specific APIs is that they are not as well documented as the | |
| 294 Web Platform features. Learning to use a Blink-specific feature requires finding | |
| 295 other tests that use it, or reading its source code. | |
| 296 | |
| 297 For example, the most popular Blink-specific API is `testRunner`, which is | |
| 298 implemented in | |
| 299 [components/test_runner/test_runner.h](../../components/test_runner/test_runner. h) | |
| 300 and | |
| 301 [components/test_runner/test_runner.cpp](../../components/test_runner/test_runne r.cpp). | |
| 302 By skimming the `TestRunnerBindings::Install` method, we learn that the | |
| 303 testRunner API is presented by the `window.testRunner` and | |
| 304 `window.layoutTestsController` objects, which are synonyms. Reading the | |
| 305 `TestRunnerBindings::GetObjectTemplateBuilder` method tells us what properties | |
| 306 are available on the `window.testRunner` object. | |
| 307 | |
| 308 *** aside | |
| 309 `window.testRunner` is the preferred way to access the `testRunner` APIs. | |
| 310 `window.layoutTestsController` is still supported because it is used by | |
| 311 3rd-party tests. | |
| 312 *** | |
| 313 | |
| 314 *** note | |
| 315 `testRunner` is the most popular testing API because it is also used indirectly | |
| 316 by tests that stick to Web Platform APIs. The `testharnessreport.js` file in | |
| 317 `testharness.js` is specifically designated to hold glue code that connects | |
| 318 `testharness.js` to the testing environment. Our implementation is in | |
| 319 [third_party/WebKit/LayoutTests/resources/testharnessreport.js](../../third_part y/WebKit/LayoutTests/references/testharnessreport.js), | |
| 320 and uses the `testRunner` API. | |
| 321 *** | |
| 322 | |
| 323 See the [components/test_runner/](../../components/test_runner/) directory and | |
| 324 [WebKit's LayoutTests guide](https://trac.webkit.org/wiki/Writing%20Layout%20Tes ts%20for%20DumpRenderTree) | |
| 325 for other useful APIs. For example, `window.eventSender` | |
| 326 ([components/test_runner/event_sender.h](../../components/test_runner/event_send er.h) | |
| 327 and | |
| 328 [components/test_runner/event_sender.cpp](../../components/test_runner/event_sen der.cpp)) | |
| 329 has methods that simulate events input such as keyboard / mouse input and | |
| 330 drag-and-drop. | |
| 331 | |
| 332 Here is a UML diagram of how the `testRunner` bindings fit into Chromium. | |
| 333 | |
| 334 [](https://docs.google .com/drawings/d/1KNRNjlxK0Q3Tp8rKxuuM5mpWf4OJQZmvm9_kpwu_Wwg/edit) | |
| 335 | |
| 336 ### Text Test Baselines | |
| 337 | |
| 338 By default, all the test cases in a file that uses `testharness.js` are expected | |
| 339 to pass. However, in some cases, we prefer to add failing test cases to the | |
| 340 repository, so that we can be notified when the failure modes change (e.g., we | |
| 341 want to know if a test starts crashing rather than returning incorrect output). | |
| 342 In these situations, a test file will be accompanied by a baseline, which is an | |
| 343 `-expected.txt` file that contains the test's expected output. | |
| 344 | |
| 345 The baselines are generated automatically when appropriate by | |
|
foolip
2016/11/16 13:42:39
double space
pwnall
2016/11/18 05:40:44
Done.
| |
| 346 `run-webkit-tests`, which is described [here](./layout_tests.md), and by the | |
| 347 [rebaselining tools](./layout_test_expectations.md). | |
| 348 | |
| 349 Text baselines should be very rare. In general, layout tests should use | |
|
foolip
2016/11/16 13:42:38
As noted above, this'll be at odds with the desire
pwnall
2016/11/18 05:40:43
Done.
I rewrote this paragraph. It now advises for
foolip
2016/11/18 11:30:40
Acknowledged.
| |
| 350 JavaScript to document Blink's current behavior, rather than using JavaScript to | |
| 351 document desired behavior and a text file to document current behavior. | |
| 352 | |
| 353 ### The js-test.js Legacy Harness | |
| 354 | |
| 355 *** promo | |
| 356 For historical reasons, older tests are written using the `js-test` harness. | |
| 357 This harness is **deprecated**, and should not be used for new tests. | |
| 358 *** | |
| 359 | |
| 360 If you need to understand old tests, the best `js-test` documentation is its | |
| 361 implementation at | |
| 362 [third_party/WebKit/LayoutTests/resources/js-test.js](../../third_party/WebKit/L ayoutTests/resources/js-test.js). | |
| 363 | |
| 364 `js-test` tests lean heavily on the Blink-specific `testRunner` testing API. | |
| 365 In a nutshell, the tests call `testRunner.dumpAsText()` to signal that the page | |
| 366 content should be dumped and compared against a text baseline (an | |
| 367 `-expected.txt` file). As a consequence, `js-test` tests are always accompanied | |
| 368 by text baselines. Asynchronous tests also use `testRunner.waitUntilDone()` and | |
| 369 `testRunner.notifyDone()` to tell the testing tools when they are complete. | |
| 370 | |
| 371 ### Tests that use an HTTP Server | |
| 372 | |
| 373 By default, tests are loaded as if via `file:` URLs. Some web platform features | |
| 374 require tests served via HTTP or HTTPS, for example absolute paths (`src=/foo`) | |
| 375 or features restricted to secure protocols. | |
| 376 | |
| 377 HTTP tests are those tests that are under `LayoutTests/http/tests` (or virtual | |
| 378 variants). Use a locally running HTTP server (Apache) to run them. Tests are | |
| 379 served off of ports 8000 and 8080 for HTTP, and 8443 for HTTPS. If you run the | |
| 380 tests using `run-webkit-tests`, the server will be started automatically. To run | |
| 381 the server manually to reproduce or debug a failure: | |
| 382 | |
| 383 ```bash | |
| 384 cd src/third_party/WebKit/Tools/Scripts | |
| 385 run-blink-httpd start | |
| 386 ``` | |
| 387 | |
| 388 The layout tests will be served from `http://127.0.0.1:8000`. For example, to | |
| 389 run the test `http/tests/serviceworker/chromium/service-worker-allowed.html`, | |
| 390 navigate to | |
| 391 `http://127.0.0.1:8000/serviceworker/chromium/service-worker-allowed.html`. Some | |
| 392 tests will behave differently if you go to 127.0.0.1 instead of localhost, so | |
| 393 use 127.0.0.1. | |
| 394 | |
| 395 To kill the server, run `run-blink-httpd --server stop`, or just use `taskkill` | |
| 396 or the Task Manager on Windows, and `killall` or Activity Monitor on MacOS. | |
| 397 | |
| 398 The test server sets up an alias to `LayoutTests/resources` directory. In HTTP | |
| 399 tests, you can access the testing framework at e.g. | |
| 400 `src="/js-test-resources/js-test.js"`. | |
| 401 | |
| 402 TODO: Document [wptserve](http://wptserve.readthedocs.io/) when we are in a | |
| 403 position to use it to run layout tests. | |
| 404 | |
| 405 ## Reference Tests | |
| 406 | |
| 407 TODO: Check that we match the Web Platform's | |
| 408 [reftests](http://testthewebforward.org/docs/reftests.html), link to their | |
| 409 document and summarize it. | |
| 410 | |
| 411 ### Legacy Reference Tests | |
| 412 | |
| 413 *** promo | |
| 414 WebKit-style reftests are deprecated. Please use the WPT style for all new | |
|
foolip
2016/11/16 13:42:39
Right now this isn't possible, and the WPT style i
pwnall
2016/11/18 05:40:43
Done.
I have been trying to avoid writing this sec
| |
| 415 reference tests that you create. | |
| 416 *** | |
| 417 | |
| 418 Blink also has inherited a sizable amount of | |
| 419 [reftests](https://trac.webkit.org/wiki/Writing%20Reftests) from WebKit. In | |
| 420 these tests, the reference page file name is based on the test page's file name | |
| 421 and an `-expected.html` suffix. | |
| 422 | |
| 423 ## Pixel Tests | |
| 424 | |
| 425 `testRunner` APIs such as `window.testRunner.dumpAsTextWithPixelResults()` and | |
| 426 `window.testRunner.dumpDragImage()` create an image result that is associated | |
| 427 with the test. The image result is compared against an image baseline, which is | |
| 428 an `-expected.png` file associated with the test, and the test passes if the | |
| 429 image result is identical to the baseline, according to a pixel-by-pixel | |
| 430 comparison. Tests that have image results (and baselines) are called **pixel | |
| 431 tests**. | |
| 432 | |
| 433 Pixel tests should still follow the principles laid out above. Pixel tests pose | |
| 434 unique challenges to the desire to have *self-describing* and *cross-platform* | |
| 435 tests. The | |
| 436 [WPT test style guidelines](http://testthewebforward.org/docs/test-style-guideli nes.html) | |
| 437 contain useful guidance. The most relevant pieces of advice are below. | |
| 438 | |
| 439 * use a green paragraph / page / square to indicate success | |
| 440 * use the red color or the word `FAIL` to highlight errors | |
| 441 * use the [Ahem font](https://www.w3.org/Style/CSS/Test/Fonts/Ahem/README) to | |
| 442 minimize the variance introduced by the platform's text rendering system | |
| 443 | |
| 444 The following snippet includes the Ahem font in a layout test. | |
| 445 | |
| 446 ```html | |
| 447 <style> | |
| 448 body { | |
| 449 font: 10px Ahem; | |
| 450 } | |
| 451 </style> | |
| 452 <script src="/resources/ahem.js"></script> | |
|
foolip
2016/11/16 13:42:38
Looks like this is always included using a relativ
pwnall
2016/11/18 05:40:44
Done.
I added a note about this.
| |
| 453 ``` | |
| 454 | |
| 455 ### Tests that need to paint, raster, or draw a frame of intermediate output | |
| 456 | |
| 457 A layout test does not actually draw frames of output until the test exits. If | |
| 458 it is required to generate a painted frame, then use | |
| 459 `window.testRunner.displayAsyncThen`, which will run the machinery to put up a | |
| 460 frame, then call the passed callback. There is also a library at | |
| 461 `fast/repaint/resources/text-based-repaint.js` to help with writing paint | |
| 462 invalidation and repaint tests. | |
| 463 | |
| 464 ## Directory Structure | |
| 465 | |
| 466 The [LayoutTests directory](../../third_party/WebKit/LayoutTests) currently | |
| 467 lacks a strict, formal structure. The following directories have special | |
| 468 meaning: | |
| 469 | |
| 470 * The `http/` directory hosts tests that require a HTTP server (see above). | |
| 471 * The `resources/` subdirectory in every directory contains binary files, such | |
| 472 as media files, and code that is shared by multiple test files. | |
| 473 | |
| 474 *** note | |
| 475 Some layout tests consist of a minimal HTML page that references a JavaScript | |
| 476 file in `resources/`. Please do not use this pattern for new tests, as it goes | |
| 477 against the minimality principle. JavaScript and CSS files should only live in | |
| 478 `resources/` if they are shared by at least two test files. | |
| 479 *** | |
| 480 | |
| 481 ## See Also | |
| 482 | |
| 483 [Writing reliable layout tests](https://docs.google.com/document/d/1Yl4SnTLBWmY1 O99_BTtQvuoffP8YM9HZx2YPkEsaduQ/edit) | |
| OLD | NEW |