Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(217)

Side by Side Diff: docs/testing/writing_layout_tests.md

Issue 2492733003: New documentation on writing LayoutTests. (Closed)
Patch Set: More tweaks in response to eae's feedback. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « docs/testing/layout_tests.md ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 Furthermore, by contributing to projects such as WPT, we share the burden of
21 writing tests with the other browser vendors, and we help all the browsers
22 get better. This is very much in line with our goal to move the Web forward.
23 2. When a Blink feature cannot be tested using the tools provided by WPT, and
24 cannot be easily covered by
25 [C++ unit tests](https://cs.chromium.org/chromium/src/third_party/WebKit/Sour ce/web/tests/?q=webframetest&sq=package:chromium&type=cs),
26 the feature must be covered by layout tests, to avoid unexpected regressions.
27 These tests will use Blink-specific testing APIs that are only available in
28 [content_shell](./layout_tests_in_content_shell.md).
29
30 *** promo
31 If you know that Blink layout tests are upstreamed to other projects, such as
32 [test262](https://github.com/tc39/test262), please update this document. Most
33 importantly, our guidelines should to make it easy for our tests to be
34 upstreamed. The `blink-dev` mailing list will be happy to help you harmonize our
35 current guidelines with communal test repositories.
36 ***
37
38 ### Test Types
39
40 There are four broad types of layout tests, listed in the order of preference.
41
42 * *JavaScript Tests* are the layout test implementation of
43 [xUnit tests](https://en.wikipedia.org/wiki/XUnit). These tests contain
44 assertions written in JavaScript, and pass if the assertions evaluate to
45 true.
46 * *Reference Tests* render a test page and a reference page, and pass if the two
47 renderings are identical, according to a pixel-by-pixel comparison. These
48 tests are less robust, harder to debug, and significantly slower than
49 JavaScript tests, and are only used when JavaScript tests are insufficient,
50 such as when testing paint code.
51 * *Pixel Tests* render a test page and compare the result against a pre-rendered
52 baseline image in the repository. Pixel tests are less robust than all
53 alternatives listed above, because the rendering of a page is influenced by
54 many factors such as the host computer's graphics card and driver, the
55 platform's text rendering system, and various user-configurable operating
56 system settings. For this reason, it is not uncommon for a pixel test to have
spark 2016/11/29 00:57:48 If it still carries the intended meaning, I'd try
pwnall 2016/11/29 01:19:23 Done.
57 a different reference image for each platform that Blink is tested on. Pixel
58 tests are least preferred, because the reference images are
59 [quite cumbersome to manage](./layout_test_expectations.md).
60 * *Dump Render Tree (DRT) Tests* output a textual representation of the render
61 tree, which is the key data structure in Blink's page rendering system. The
62 test passes if the output matches a baseline text file in the repository. In
63 addition to their text result, DRT tests can also produce an image result
64 which is compared to an image baseline, similarly to pixel tests (described
65 above). A DRT test with two results (text and image) passes if _both_ results
66 match the baselines in the repository. DRT tests are less desirable than all
67 the alternatives, because they depend on a browser implementation detail.
68
69 ## General Principles
70
71 The principles below are adapted from
72 [Test the Web Forward's Test Format Guidelines](http://testthewebforward.org/doc s/test-format-guidelines.html)
73 and
74 [WebKit's Wiki page on Writing good test cases](https://trac.webkit.org/wiki/Wri ting%20Layout%20Tests%20for%20DumpRenderTree).
75
76 * Tests should be **concise**, without compromising on the principles below.
77 Every element and piece of code on the page should be necessary and relevant
78 to what is being tested. For example, don't build a fully functional signup
79 form if you only need a text field or a button.
80 * Content needed to satisfy the principles below is considered necessary.
81 For example, it is acceptable and desirable to add elements that make
82 the test self-describing (see below), and to add code that makes the
83 test more reliable (see below).
84 * Content that makes test failures easier to debug is considered necessary
85 (to maintaining a good development speed), and is both acceptable and
86 desirable.
87 * Conciseness is particularly important for reference tests and pixel
88 tests, as the test pages are rendered in an 800x600px viewport. Having
89 content outside the viewport is undesirable because the outside content
90 does not get compared, and because the resulting scrollbars are
91 platform-specific UI widgets, making the test results less reliable.
92
93 * Tests should be as **fast** as possible, without compromising on the
94 principles below. Blink has several thousand layout tests that are run in
95 parallel, and avoiding unnecessary delays is crucial to keeping our Commit
96 Queue in good shape.
97 * Avoid [window.setTimeout](https://developer.mozilla.org/en-US/docs/Web/API /WindowTimers/setTimeout),
98 as it wastes time on the testing infrastructure. Instead, use specific
99 event handlers, such as
100 [window.onload](https://developer.mozilla.org/en-US/docs/Web/API/GlobalEve ntHandlers/onload),
101 to decide when to advance to the next step in a test.
102
103 * Tests should be **reliable** and yield consistent results for a given
104 implementation. Flaky tests slow down your fellow developers' debugging
105 efforts and the Commit Queue.
106 * `window.setTimeout` is again a primary offender here. Asides from wasting
107 time on a fast system, tests that rely on fixed timeouts can fail when run
108 on systems that are slower than expected.
109 * Follow the guidelines in this
110 [PSA on writing reliable layout tests](https://docs.google.com/document/d/ 1Yl4SnTLBWmY1O99_BTtQvuoffP8YM9HZx2YPkEsaduQ/edit).
111
112 * Tests should be **self-describing**, so that a project member can recognize
113 whether a test passes or fails without having to read the specification of the
114 feature being tested. `testharness.js` makes a test self-describing when used
115 correctly, but tests that degrade to manual tests
116 [must be carefully designed](http://testthewebforward.org/docs/test-style-guid elines.html)
117 to be self-describing.
118
119 * Tests should require a **minimal** amount of cognitive effort to read and
120 maintain.
121 * Avoid depending on edge case behavior of features that aren't explicitly
122 covered by the test. For example, except where testing parsing, tests
123 should contain valid markup (no parsing errors).
124 * Tests should provide as much relevant information as possible when
125 failing. `testharness.js` tests should prefer
126 [rich assert_ functions](https://github.com/w3c/testharness.js/blob/master /docs/api.md#list-of-assertions)
127 to combining `assert_true()` with a boolean operator. Using appropriate
128 `assert_` functions results in better diagnostic output when the assertion
129 fails.
130 * Prefer JavaScript's
131 [===](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operator s/Comparison_Operators#Identity_strict_equality_())
132 operator to
133 [==](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators /Comparison_Operators#Equality_())
134 so that readers don't have to reason about
135 [type conversion](http://www.ecma-international.org/ecma-262/6.0/#sec-abst ract-equality-comparison).
136
137 * Tests should be as **cross-platform** as reasonably possible. Avoid
138 assumptions about device type, screen resolution, etc. Unavoidable assumptions
139 should be documented.
140 * When possible, tests should only use Web platform features, as specified
141 in the relevant standards. When the Web platform's APIs are insufficient,
142 tests should prefer to use WPT extended testing APIs, such as
143 `wpt_automation`.
144 * Test pages should use the HTML5 doctype (`<!doctype html>`) unless they
145 specifically cover
146 [quirks mode](https://developer.mozilla.org/docs/Quirks_Mode_and_Standards _Mode)
147 behavior.
148 * Tests should be written under the assumption that they will be upstreamed
149 to the WPT project. For example, tests should follow the
150 [WPT guidelines](http://testthewebforward.org/docs/writing-tests.html).
151 * Tests that use Blink-specific testing APIs should feature-test for the
152 presence of the testing APIs and degrade to
153 [manual tests](http://testthewebforward.org/docs/manual-test.html) when
154 the testing APIs are not present. _This is not currently enforced in code
155 review. However, please keep in mind that a manual test can be debugged in
156 the browser, whereas a test that does not degrade gracefully can only be
157 debugged in the test runner._
158
159 * Tests must be **self-contained** and not depend on external network resources.
160 Unless used by multiple test files, CSS and JavaScript should be inlined using
161 `<style>` and `<script>` tags. Content shared by multiple tests should be
162 placed in a `resources/` directory near the tests that share it. See below for
163 using multiple origins in a test.
164
165 * Test **file names** should describe what is being tested. File names should
166 use `snake-case`, but preserve the case of any embedded API names. For
167 example, prefer `document-createElement.html` to
168 `document-create-element.html`.
169
170 * Tests should prefer **modern features** in JavaScript and in the Web Platform.
171 * Tests should use
172 [strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe rence/Strict_mode)
173 for all JavaScript, except when specifically testing sloppy mode behavior.
174 Strict mode flags deprecated features and helps catch some errors, such as
175 forgetting to declare variables.
176 * JavaScript code should prefer
177 [const](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statem ents/const)
178 and
179 [let](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statemen ts/let)
180 over `var`, prefer
spark 2016/11/29 00:57:48 I think you can omit this "prefer", and the one in
pwnall 2016/11/29 01:19:22 Done.
181 [classes](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Clas ses)
182 over other OOP constructs, and prefer
183 [Promises](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Glo bal_Objects/Promise)
184 over other mechanisms for structuring asynchronous code.
185 * The desire to use modern features must be balanced with the desire for
186 cross-platform tests. Avoid using features that haven't shipped by other
spark 2016/11/29 00:57:48 "haven't been shipped by"
pwnall 2016/11/29 01:19:22 Done.
187 current major rendering engines (WebKit, Gecko, Edge). When unsure, check
188 [caniuse.com](http://caniuse.com/).
189
190 * Tests should use the UTF-8 **character encoding**, which should be declared by
191 `<meta charset=utf-8>`. This does not apply when specifically testing
192 encodings.
193 * At this time, code reviewers may choose to accept layout tests that do
194 not have a `<meta charset>`, as long as the file contents is pure ASCII.
spark 2016/11/29 00:57:48 "contents are" or "content is"
pwnall 2016/11/29 01:19:22 Done.
195 If going that route, please keep in mind that Firefox currently issues a
196 dev tools warning for pages without a declared charset.
197
198 * Tests should aim to have a **coding style** that is consistent with
199 [Google's JavaScript Style Guide](https://google.github.io/styleguide/jsguide. html),
200 and
201 [Google's HTML/CSS Style Guide](https://google.github.io/styleguide/htmlcssgui de.xml),
202 with the following exceptions.
203 * Rules related to Google Closure and JSDoc do not apply.
204 * Modern Web Platform and JavaScript features should be preferred to legacy
205 constructs that target old browsers. For example, prefer `const` and `let`
206 to `var`, and prefer `class` over other OOP constructs. This should be
207 balanced with the desire to have cross-platform tests.
208 * Concerns regarding buggy behavior in legacy browsers do not apply. For
209 example, the garbage collection cycle note in the _Closures_ section does
210 not apply.
211 * Per the JavaScript guide, new tests should also follow any per-project
212 style guide, such as the
213 [ServiceWorker Tests Style guide](http://www.chromium.org/blink/servicewor ker/testing).
214
215 *** note
216 This document intentionally uses _should_ a lot more than _must_, as defined in
217 [RFC 2119](https://www.ietf.org/rfc/rfc2119.txt). Writing layout tests is a
218 careful act of balancing many concerns, and this humble document cannot possibly
219 capture the context that rests in the head of an experienced Blink engineer.
220 ***
221
222 ## JavaScript Tests
223
224 Whenever possible, the testing criteria should be expressed in JavaScript. The
225 alternatives, which will be described in future sections, result in slower and
226 less reliable tests.
227
228 All new JavaScript tests should be written using the
229 [testharness.js](https://github.com/w3c/testharness.js/) testing framework. This
230 framework is used by the tests in the
231 [web-platform-tests](https://github.com/w3c/web-platform-tests) repository,
232 which is shared with all the other browser vendors, so `testharness.js` tests
233 are more accessible to browser developers.
234
235 As a shared framework, `testharness.js` enjoys high-quality documentation, such
236 as [a tutorial](http://testthewebforward.org/docs/testharness-tutorial.html) and
237 [API documentation](https://github.com/w3c/testharness.js/blob/master/docs/api.m d).
238 Layout tests should follow the recommendations of the above documents.
239 Furthermore, layout tests should include relevant
240 [metadata](http://testthewebforward.org/docs/css-metadata.html). The
241 specification URL (in `<link rel="help">`) is almost always relevant, and is
242 incredibly helpful to a developer who needs to understand the test quickly.
243
244 Below is a skeleton for a JavaScript test embedded in an HTML page. Note that,
245 in order to follow the minimality guideline, the test omits the tags `<html>`,
246 `<head>` and `<body>`, as they can be inferred by the HTML parser.
spark 2016/11/29 00:57:48 comma after '`<head>`'
pwnall 2016/11/29 01:19:22 Done.
247
248 ```html
249 <!doctype html>
250 <meta charset="utf-8">
251 <title>JavaScript: the true literal</title>
252 <link rel="help" href="https://tc39.github.io/ecma262/#sec-boolean-literals">
253 <meta name="assert" value="The true literal is equal to itself and immutable">
254 <script src="/resources/testharness.js"></script>
255 <script src="/resources/testharnessreport.js"></script>
256 <script>
257 'use strict';
258
259 // Synchronous test example.
260 test(() => {
261 const value = true;
262 assert_true(value, 'true literal');
263 assert_equals(value.toString(), 'true', 'the string representation of true');
264 }, 'The literal true in a synchronous test case');
265
266 // Asynchronous test example.
267 async_test(t => {
268 const originallyTrue = true;
269 setTimeout(t.step_func_done(() => {
270 assert_equals(originallyTrue, true);
271 }), 0);
272 }, 'The literal true in a setTimeout callback');
273
274 // Promise test example.
275 promise_test(() => {
276 return new Promise((resolve, reject) => {
277 resolve(true);
278 }).then(value => {
279 assert_true(value);
280 });
281 }, 'The literal true used to resolve a Promise');
282
283 </script>
284 ```
285
286 Some points that are not immediately obvious from the example:
287
288 * The `<meta name="assert">` describes the purpose of the entire file, and
289 is not redundant to `<title>`. Don't add a `<meta name="assert">` when the
290 information in the `<title>` is sufficient.
291 * When calling an `assert_` function that compares two values, the first
292 argument is the actual value (produced by the functionality being tested), and
293 the second argument is the expected value (known good, golden). The order
294 is important, because the testing harness relies on it to generate expressive
295 error messages that are relied upon when debugging test failures.
296 * The assertion description (the string argument to `assert_` methods) conveys
297 the way the actual value was obtained.
298 * If the expected value doesn't make it clear, the assertion description
299 should explain the desired behavior.
300 * Test cases with a single assertion should omit the assertion's description
301 when it is sufficiently clear.
302 * Each test case describes the circumstance that it tests, without being
303 redundant.
304 * Do not start test case descriptions with redundant terms like "Testing"
305 or "Test for".
306 * Test files with a single test case should omit the test case description.
307 The file's `<title>` should be sufficient to describe the scenario being
308 tested.
309 * Asynchronous tests have a few subtleties.
310 * The `async_test` wrapper calls its function with a test case argument that
311 is used to signal when the test case is done, and to connect assertion
312 failures to the correct test.
313 * `t.done()` must be called after all the test case's assertions have
314 executed.
315 * Test case assertions (actually, any callback code that can throw
316 exceptions) must be wrapped in `t.step_func()` calls, so that
317 assertion failures and exceptions can be traced back to the correct test
318 case.
319 * `t.step_func_done()` is a shortcut that combines `t.step_func()` with a
320 `t.done()` call.
321
322 *** promo
323 Layout tests that load from `file://` origins must currently use relative paths
324 to point to
325 [/resources/testharness.js](../../third_party/WebKit/LayoutTests/resources/testh arness.js)
326 and
327 [/resources/testharnessreport.js](../../third_party/WebKit/LayoutTests/resources /testharnessreport.js).
328 This is contrary to the WPT guidelines, which call for absolute paths.
329 This limitation does not apply to the tests in `LayoutTests/http`, which rely on
330 an HTTP server, or to the tests in `LayoutTests/imported/wpt`, which are
331 imported from the [WPT repository](https://github.com/w3c/web-platform-tests).
332 ***
333
334 ### WPT Supplemental Testing APIs
335
336 Some tests simply cannot be expressed using the Web Platform APIs. For example,
337 some tests that require a user to perform a gesture, such as a mouse click,
338 cannot be implemented using Web APIs. The WPT project covers some of these cases
339 via supplemental testing APIs.
340
341 *** promo
342 In many cases, the user gesture is not actually necessary. For example, many
343 event handling tests can use
344 [synthetic events](https://developer.mozilla.org/docs/Web/Guide/Events/Creating_ and_triggering_events).
345 ***
346
347 *** note
348 TODO: document wpt_automation. Manual tests might end up moving here.
349 ***
350
351 ### Relying on Blink-Specific Testing APIs
352
353 Tests that cannot be expressed using the Web Platform APIs or WPT's testing APIs
354 use Blink-specific testing APIs. These APIs are only available in
355 [content_shell](./layout_tests_in_content_shell.md), and should only be used as
356 a last resort.
357
358 A downside of Blink-specific APIs is that they are not as well documented as the
359 Web Platform features. Learning to use a Blink-specific feature requires finding
360 other tests that use it, or reading its source code.
361
362 For example, the most popular Blink-specific API is `testRunner`, which is
363 implemented in
364 [components/test_runner/test_runner.h](../../components/test_runner/test_runner. h)
365 and
366 [components/test_runner/test_runner.cpp](../../components/test_runner/test_runne r.cpp).
367 By skimming the `TestRunnerBindings::Install` method, we learn that the
368 testRunner API is presented by the `window.testRunner` and
369 `window.layoutTestsController` objects, which are synonyms. Reading the
370 `TestRunnerBindings::GetObjectTemplateBuilder` method tells us what properties
371 are available on the `window.testRunner` object.
372
373 *** aside
374 `window.testRunner` is the preferred way to access the `testRunner` APIs.
375 `window.layoutTestsController` is still supported because it is used by
376 3rd-party tests.
377 ***
378
379 *** note
380 `testRunner` is the most popular testing API because it is also used indirectly
381 by tests that stick to Web Platform APIs. The `testharnessreport.js` file in
382 `testharness.js` is specifically designated to hold glue code that connects
383 `testharness.js` to the testing environment. Our implementation is in
384 [third_party/WebKit/LayoutTests/resources/testharnessreport.js](../../third_part y/WebKit/LayoutTests/resources/testharnessreport.js),
385 and uses the `testRunner` API.
386 ***
387
388 See the [components/test_runner/](../../components/test_runner/) directory and
389 [WebKit's LayoutTests guide](https://trac.webkit.org/wiki/Writing%20Layout%20Tes ts%20for%20DumpRenderTree)
390 for other useful APIs. For example, `window.eventSender`
391 ([components/test_runner/event_sender.h](../../components/test_runner/event_send er.h)
392 and
393 [components/test_runner/event_sender.cpp](../../components/test_runner/event_sen der.cpp))
394 has methods that simulate events input such as keyboard / mouse input and
395 drag-and-drop.
396
397 Here is a UML diagram of how the `testRunner` bindings fit into Chromium.
398
399 [![UML of testRunner bindings configuring platform implementation](https://docs. google.com/drawings/u/1/d/1KNRNjlxK0Q3Tp8rKxuuM5mpWf4OJQZmvm9_kpwu_Wwg/export/sv g?id=1KNRNjlxK0Q3Tp8rKxuuM5mpWf4OJQZmvm9_kpwu_Wwg&pageid=p)](https://docs.google .com/drawings/d/1KNRNjlxK0Q3Tp8rKxuuM5mpWf4OJQZmvm9_kpwu_Wwg/edit)
400 ### Manual Tests
401
402 Whenever possible, tests that rely on (WPT's or Blink's) testing APIs should =
spark 2016/11/29 00:57:48 er, I think this "=" is a typo?
pwnall 2016/11/29 01:19:22 Done.
403 also be usable as
404 [manual tests](http://testthewebforward.org/docs/manual-test.html). This makes
405 it easy to debug the test, and to check whether our behavior matches other
406 browsers.
407
408 *** note
409 The recommendation to have tests that depend on Blink-only testing APIs
410 gracefully degrade to manual tests is not currently enforced in code review.
411 When considering skipping this recommendation, please keep in mind that a manual
412 test can be debugged in the browser, whereas a test that does not degrade
413 gracefully can only be debugged in the test runner. Fellow project members and
414 future you will thank you for having your test work as a manual test.
415 ***
416
417 Manual tests should minimize the chance of user error. This implies keeping the
418 manual steps to a minimum, and having simple and clear instructions that
419 describe all the configuration changes and user gestures that match the effect
420 of the Blink-specific APIs used by the test.
421
422 Below is an example of a fairly minimal test that uses a Blink-Specific API
423 (`window.eventSender`), and gracefully degrades to a manual test.
spark 2016/11/29 00:57:48 extra space between after 'degrades'
pwnall 2016/11/29 01:19:22 Done.
424
425 ```html
426 <!doctype html>
427 <meta charset="utf-8">
428 <title>DOM: Event.isTrusted for UI events</title>
429 <link rel="help" href="https://dom.spec.whatwg.org/#dom-event-istrusted">
430 <link rel="help" href="https://dom.spec.whatwg.org/#constructing-events">
431 <meta name="assert"
432 content="Event.isTrusted is true for events generated by user interaction">
433 <script src="../../resources/testharness.js"></script>
434 <script src="../../resources/testharnessreport.js"></script>
435
436 <p>Please click on the button below.</p>
437 <button>Click Me!</button>
438
439 <script>
440 'use strict';
441
442 setup({ explicit_timeout: true });
443
444 promise_test(() => {
445 const button = document.querySelector('button');
446 return new Promise((resolve, reject) => {
447 const button = document.querySelector('button');
448 button.addEventListener('click', (event) => {
449 resolve(event);
450 });
451
452 if (window.eventSender) {
453 eventSender.mouseMoveTo(button.offsetLeft, button.offsetTop);
454 eventSender.mouseDown();
455 eventSender.mouseUp();
456 }
457 }).then((clickEvent) => {
458 assert_true(clickEvent.isTrusted);
459 });
460
461 }, 'Click generated by user interaction');
462
463 </script>
464 ```
465
466 The test exhibits the following desirable features:
467
468 * It has a second specification URL (`<link rel="help">`), because the paragraph
469 that documents the tested feature (referenced by the primary URL) is not very
470 informative on its own.
471 * It links to the
472 [WHATWG Living Standard](https://wiki.whatwg.org/wiki/FAQ#What_does_.22Living_ Standard.22_mean.3F),
473 rather than to a frozen version of the specification.
474 * It contains clear instructions for manually triggering the test conditions.
475 The test starts with a paragraph (`<p>`) that tells the tester exactly what to
476 do, and the `<button>` that needs to be clicked is clearly labeled.
477 * It disables the timeout mechanism built into `testharness.js` by calling
478 `setup({ explicit_timeout: true });`
479 * It checks for the presence of the Blink-specific testing APIs
480 (`window.eventSender`) before invoking them. The test does not automatically
481 fail when the APIs are not present.
482 * It uses [Promises](https://developer.mozilla.org/docs/Web/JavaScript/Reference /Global_Objects/Promise)
483 to separate the test setup from the assertions. This is particularly helpful
484 for manual tests that depend on a sequence of events to occur, as Promises
485 offer a composable way to express waiting for asynchronous events that avoids
486 [callback hell](http://stackabuse.com/avoiding-callback-hell-in-node-js/).
487
488 Notice that the test is pretty heavy compared to a minimal JavaScript test that
489 does not rely on testing APIs. Only use testing APIs when the desired testing
490 conditions cannot be set up using Web Platform APIs.
491
492 ### Text Test Baselines
493
494 By default, all the test cases in a file that uses `testharness.js` are expected
495 to pass. However, in some cases, we prefer to add failing test cases to the
496 repository, so that we can be notified when the failure modes change (e.g., we
497 want to know if a test starts crashing rather than returning incorrect output).
498 In these situations, a test file will be accompanied by a baseline, which is an
499 `-expected.txt` file that contains the test's expected output.
500
501 The baselines are generated automatically when appropriate by
502 `run-webkit-tests`, which is described [here](./layout_tests.md), and by the
503 [rebaselining tools](./layout_test_expectations.md).
504
505 Text baselines for `testharness.js` should be avoided, as having a text baseline
506 associated with a `testharness.js` indicates the presence of a bug. For this
507 reason, CLs that add text baselines must include a
508 [crbug.com](https://crbug.com) link for an issue tracking the removal of the
509 text expectations.
510
511 * When creating tests that will be upstreamed to WPT, and Blink's current
512 behavior does not match the specification that is being tested, a text
513 baseline is necessary. Remember to create an issue tracking the expectation's
514 removal, and to link the issue in the CL description.
515 * Layout tests that cannot be upstreamed to WPT should use JavaScript to
516 document Blink's current behavior, rather than using JavaScript to document
517 desired behavior and a text file to document current behavior.
518
519 ### The js-test.js Legacy Harness
520
521 *** promo
522 For historical reasons, older tests are written using the `js-test` harness.
523 This harness is **deprecated**, and should not be used for new tests.
524 ***
525
526 If you need to understand old tests, the best `js-test` documentation is its
527 implementation at
528 [third_party/WebKit/LayoutTests/resources/js-test.js](../../third_party/WebKit/L ayoutTests/resources/js-test.js).
529
530 `js-test` tests lean heavily on the Blink-specific `testRunner` testing API.
531 In a nutshell, the tests call `testRunner.dumpAsText()` to signal that the page
532 content should be dumped and compared against a text baseline (an
533 `-expected.txt` file). As a consequence, `js-test` tests are always accompanied
534 by text baselines. Asynchronous tests also use `testRunner.waitUntilDone()` and
535 `testRunner.notifyDone()` to tell the testing tools when they are complete.
536
537 ### Tests that use an HTTP Server
538
539 By default, tests are loaded as if via `file:` URLs. Some web platform features
540 require tests served via HTTP or HTTPS, for example absolute paths (`src=/foo`)
541 or features restricted to secure protocols.
542
543 HTTP tests are those tests that are under `LayoutTests/http/tests` (or virtual
spark 2016/11/29 00:57:48 "are those under"
pwnall 2016/11/29 01:19:22 Done. FWIW, this section was carried over from a p
544 variants). Use a locally running HTTP server (Apache) to run them. Tests are
545 served off of ports 8000 and 8080 for HTTP, and 8443 for HTTPS. If you run the
546 tests using `run-webkit-tests`, the server will be started automatically. To run
547 the server manually to reproduce or debug a failure:
548
549 ```bash
550 cd src/third_party/WebKit/Tools/Scripts
551 run-blink-httpd start
552 ```
553
554 The layout tests will be served from `http://127.0.0.1:8000`. For example, to
555 run the test `http/tests/serviceworker/chromium/service-worker-allowed.html`,
556 navigate to
557 `http://127.0.0.1:8000/serviceworker/chromium/service-worker-allowed.html`. Some
558 tests will behave differently if you go to 127.0.0.1 instead of localhost, so
559 use 127.0.0.1.
560
561 To kill the server, run `run-blink-httpd --server stop`, or just use `taskkill`
562 or the Task Manager on Windows, and `killall` or Activity Monitor on MacOS.
563
564 The test server sets up an alias to `LayoutTests/resources` directory. In HTTP
spark 2016/11/29 00:57:48 "to the `LayoutTests/resources`'
pwnall 2016/11/29 01:19:22 Done.
565 tests, you can access the testing framework at e.g.
566 `src="/js-test-resources/js-test.js"`.
567
568 TODO: Document [wptserve](http://wptserve.readthedocs.io/) when we are in a
569 position to use it to run layout tests.
570
571 ## Reference Tests (Reftests)
572
573 Reference tests, also known as reftests, perform a pixel-by-pixel comparison
574 between the rendered image of a test page and the rendered image of a reference
575 page. Most reference tests pass if the two images match, but there are cases
576 where it is useful to have a test pass when the two images do _not_ match.
577
578 Reference tests are more difficult to debug than JavaScript tests, and tend to
579 be slower as well. Therefore, they should only be used for functionality that
580 cannot be covered by JavaScript tests.
581
582 New reference tests should follow the
583 [WPT reftests guidelines](http://testthewebforward.org/docs/reftests.html). The
584 most important points are summarized below.
585
586 * The test page declares the reference page using a `<link rel="match">` or
587 `<link rel="mismatch">`, depending on whether the test passes when the test
588 image matches or does not match the reference image.
589 * The reference page must not use the feature being tested. Otherwise, the test
590 is meaningless.
591 * The reference page should be as simple as possible, and should not depend on
592 advanced features. Ideally, the reference page should render as intended even
593 on browsers with poor CSS support.
594 * Reference tests should be self-describing.
595 * Reference tests do _not_ include `testharness.js`.
596
597 Our testing infrastructure was designed for the
598 [WebKit reftests](https://trac.webkit.org/wiki/Writing%20Reftests) that Blink
599 has inherited. The consequences are summarized below.
600
601 * Each reference page must be in the same directory as its associated test.
602 Given a test page named `foo` (e.g. `foo.html` or `foo.svg`),
603 * The reference page must be named `foo-expected` (e.g.,
604 `foo-expected.html`) if the test passes when the two images match.
605 * The reference page must be named `foo-expected-mismatch` (e.g.,
606 `foo-expected-mismatch.svg`) if the test passes when the two images do
607 _not_ match.
608 * Multiple references and chained references are not supported.
609
610 The following example demonstrates a reference test for
611 [`<ol>`'s reversed attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/ Element/ol).
612 The example assumes that the test page is named `ol-reversed.html`.
613
614 ```html
615 <!doctype html>
616 <meta charset="utf-8">
617 <link rel="match" href="ol-reversed-expected.html">
618
619 <ol reversed>
620 <li>A</li>
621 <li>B</li>
622 <li>C</li>
623 </ol>
624 ```
625
626 The reference page, which must be named `ol-reversed-expected.html`, is below.
627
628 ```html
629 <!doctype html>
630 <meta charset="utf-8">
631
632 <ol>
633 <li value="3">A</li>
634 <li value="2">B</li>
635 <li value="1">C</li>
636 </ol>
637 ```
638
639 ## Pixel Tests
640
641 `testRunner` APIs such as `window.testRunner.dumpAsTextWithPixelResults()` and
642 `window.testRunner.dumpDragImage()` create an image result that is associated
643 with the test. The image result is compared against an image baseline, which is
644 an `-expected.png` file associated with the test, and the test passes if the
645 image result is identical to the baseline, according to a pixel-by-pixel
646 comparison. Tests that have image results (and baselines) are called **pixel
647 tests**.
648
649 Pixel tests should still follow the principles laid out above. Pixel tests pose
650 unique challenges to the desire to have *self-describing* and *cross-platform*
651 tests. The
652 [WPT test style guidelines](http://testthewebforward.org/docs/test-style-guideli nes.html)
653 contain useful guidance. The most relevant pieces of advice are below.
654
655 * Whenever possible, use a green paragraph / page / square to indicate success.
656 If that is not possible, make the test self-describing by including a textual
657 description of the desired (passing) outcome.
658 * Only use the red color or the word `FAIL` to highlight errors. This does not
659 apply when testing the color red.
660 * Use the [Ahem font](https://www.w3.org/Style/CSS/Test/Fonts/Ahem/README) to
661 reduce the variance introduced by the platform's text rendering system. This
662 does not apply when testing text, text flow, font selection, font fallback,
663 font features or other typographic information.
spark 2016/11/29 00:57:48 comma after "font features"
pwnall 2016/11/29 01:19:22 Done.
664
665 *** promo
666 When using `window.testRunner.dumpAsTextWithPixelResults()`, the image result
667 will always be 800x600px, because test pages are rendered in an 800x600px
668 viewport. Pixel tests that do not specifically cover scrolling should fit in an
669 800x600px viewport without creating scrollbars.
670 ***
671
672 The following snippet includes the Ahem font in a layout test.
673
674 ```html
675 <style>
676 body {
677 font: 10px Ahem;
678 }
679 </style>
680 <script src="/resources/ahem.js"></script>
681 ```
682
683 *** promo
684 Tests outside `LayoutTests/http` and `LayoutTests/imported/wpt` currently need
685 to use a relative path to
686 [/third_party/WebKit/LayoutTests/resources/ahem.js](../../third_party/WebKit/Lay outTests/resources/ahem.js)
687 ***
688
689 ### Tests that need to paint, raster, or draw a frame of intermediate output
690
691 A layout test does not actually draw frames of output until the test exits. If
692 it is required to generate a painted frame, then use
spark 2016/11/29 00:57:48 it's unclear what "it" is referring to; perhaps "I
pwnall 2016/11/29 01:19:22 Done. I rephrased the sentence, thank you. This i
693 `window.testRunner.displayAsyncThen`, which will run the machinery to put up a
694 frame, then call the passed callback. There is also a library at
695 `fast/repaint/resources/text-based-repaint.js` to help with writing paint
696 invalidation and repaint tests.
697
698 ## Dump Render Tree (DRT) Tests
699
700 A Dump Render Tree test renders a web page and produces up to two results, which
701 are compared against baseline files:
702
703 * All tests output a textual representation of Blink's
704 [render tree](https://developers.google.com/web/fundamentals/performance/criti cal-rendering-path/render-tree-construction),
705 which is compared against an `-expected.txt` text baseline.
706 * Some tests also output the image of the rendered page, which is compared
707 against an `-expected.png` image baseline, using the same method as pixel
708 tests.
709
710 TODO: Document the API used by DRT tests to opt out of producing image results.
711
712 A DRT test passes if _all_ of its results match their baselines. Like pixel
713 tests, the output of DRT tests depends on platform-specific mechanisms, so DRT
714 tests often require per-platform baselines. Furthermore, DRT tests depend on the
715 render tree data structure, which means that if we replace the render tree data
716 structure, we will need to look at each DRT test and consider whether it is
717 still meaningful.
718
719 For these reasons, DRT tests should **only** be used to cover aspects of the
720 layout code that can only be tested by looking at the render tree. Any
721 combination of the other test types is preferable to a DRT test. DRT tests are
722 [inherited from WebKit](https://webkit.org/blog/1456/layout-tests-practice/), so
723 the repository may have some unfortunate examples of DRT tests.
724
725 The following page is an example of a DRT test.
726
727 ```html
728 <!doctype html>
729 <meta charset="utf-8">
730 <style>
731 body { font: 10px Ahem; }
732 span::after {
733 content: "pass";
734 color: green;
735 }
736 </style>
737 <script src="/resources/ahem.js"></script>
738
739 <p><span>Pass if a green PASS appears to the right: </span></p>
740 ```
741
742 The most important aspects of the example are that the test page does not
743 include a testing framework, and that it follows the guidelines for pixel tests.
744 The test page produces the text result below.
745
746 ```
747 layer at (0,0) size 800x600
748 LayoutView at (0,0) size 800x600
749 layer at (0,0) size 800x30
750 LayoutBlockFlow {HTML} at (0,0) size 800x30
751 LayoutBlockFlow {BODY} at (8,10) size 784x10
752 LayoutBlockFlow {P} at (0,0) size 784x10
753 LayoutInline {SPAN} at (0,0) size 470x10
754 LayoutText {#text} at (0,0) size 430x10
755 text run at (0,0) width 430: "Pass if a green PASS appears to the ri ght: "
756 LayoutInline {<pseudo:after>} at (0,0) size 40x10 [color=#008000]
757 LayoutTextFragment (anonymous) at (430,0) size 40x10
758 text run at (430,0) width 40: "pass"
759 ```
760
761 Notice that the test result above depends on the size of the `<p>` text. The
762 test page uses the Ahem font (introduced above), whose main design goal is
763 consistent cross-platform rendering. Had the test used another font, its text
764 baseline would have depended on the fonts installed on the testing computer, and
765 on the platform's font rendering system. Please follow the pixel tests
766 guidelines and write reliable DRT tests!
767
768 WebKit's render tree is described in
769 [a series of posts](https://webkit.org/blog/114/webcore-rendering-i-the-basics/)
770 on WebKit's blog. Some of the concepts there still apply to Blink's render tree.
771
772 ## Directory Structure
773
774 The [LayoutTests directory](../../third_party/WebKit/LayoutTests) currently
775 lacks a strict, formal structure. The following directories have special
776 meaning:
777
778 * The `http/` directory hosts tests that require a HTTP server (see above).
spark 2016/11/29 00:57:48 "an HTTP server"
pwnall 2016/11/29 01:19:23 Done.
779 * The `resources/` subdirectory in every directory contains binary files, such
780 as media files, and code that is shared by multiple test files.
781
782 *** note
783 Some layout tests consist of a minimal HTML page that references a JavaScript
784 file in `resources/`. Please do not use this pattern for new tests, as it goes
785 against the minimality principle. JavaScript and CSS files should only live in
786 `resources/` if they are shared by at least two test files.
787 ***
OLDNEW
« no previous file with comments | « docs/testing/layout_tests.md ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698