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