blob: f2767c206dedb34307ae5b5972ed16a7271b5d22 [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
pwnalle7819482016-12-17 00:58:40273&#x1F6A7; Tests should use the UTF-8 **character encoding**, which should be
274declared by `<meta charset=utf-8>`. This does not apply when specifically
275testing encodings.
pwnall6acacd82016-12-02 01:40:15276
277The `<meta>` tag must be the first child of the document's `<head>` element. In
278documents that do not have an explicit `<head>`, the `<meta>` tag must follow
279the doctype.
280
281When HTML pages do not explicitly declare a character encoding, browsers
282determine the encoding using an
283[encoding sniffing algorithm](https://html.spec.whatwg.org/multipage/syntax.html#determining-the-character-encoding)
284that will surprise most modern Web developers. Highlights include a default
285encoding that depends on the user's locale, and non-standardized
286browser-specific heuristics.
287
288*** promo
289The WPT guidelines state that test files that only contain ASCII characters may
290omit the `<meta>` tag. This exception is currently discussed on
291[blink-dev](https://groups.google.com/a/chromium.org/d/topic/blink-dev/XsR6PKRrS1E/discussion).
292If taking that route, please keep in mind that Firefox currently issues a
293[development tools](https://developer.mozilla.org/en-US/docs/Tools) warning for
294pages without a declared encoding.
295***
296
297### Coding Style
298
299Tests should aim to have a **coding style** that is consistent with
300[Google's JavaScript Style Guide](https://google.github.io/styleguide/jsguide.html),
301and
302[Google's HTML/CSS Style Guide](https://google.github.io/styleguide/htmlcssguide.xml),
303with the following exceptions.
304
305* Rules related to Google Closure and JSDoc do not apply.
306* Modern Web Platform and JavaScript features should be preferred to legacy
307 constructs that target old browsers.
308* Per the JavaScript guide, new tests should also follow any per-project
309 style guide, such as the
xiaoyin.l1003c0b2016-12-06 02:51:17310 [ServiceWorker Tests Style guide](https://www.chromium.org/blink/serviceworker/testing).
pwnall6acacd82016-12-02 01:40:15311
pwnall4ea2eb32016-11-29 02:47:25312## JavaScript Tests
313
314Whenever possible, the testing criteria should be expressed in JavaScript. The
315alternatives, which will be described in future sections, result in slower and
316less reliable tests.
317
318All new JavaScript tests should be written using the
319[testharness.js](https://github.com/w3c/testharness.js/) testing framework. This
320framework is used by the tests in the
321[web-platform-tests](https://github.com/w3c/web-platform-tests) repository,
322which is shared with all the other browser vendors, so `testharness.js` tests
323are more accessible to browser developers.
324
325As a shared framework, `testharness.js` enjoys high-quality documentation, such
326as [a tutorial](http://testthewebforward.org/docs/testharness-tutorial.html) and
327[API documentation](https://github.com/w3c/testharness.js/blob/master/docs/api.md).
328Layout tests should follow the recommendations of the above documents.
329Furthermore, layout tests should include relevant
330[metadata](http://testthewebforward.org/docs/css-metadata.html). The
331specification URL (in `<link rel="help">`) is almost always relevant, and is
332incredibly helpful to a developer who needs to understand the test quickly.
333
334Below is a skeleton for a JavaScript test embedded in an HTML page. Note that,
335in order to follow the minimality guideline, the test omits the tags `<html>`,
336`<head>`, and `<body>`, as they can be inferred by the HTML parser.
337
338```html
339<!doctype html>
340<meta charset="utf-8">
ktyliue0bb9882017-01-10 01:47:50341<title>JavaScript: the true literal</title>
pwnall4ea2eb32016-11-29 02:47:25342<link rel="help" href="https://tc39.github.io/ecma262/#sec-boolean-literals">
343<meta name="assert" value="The true literal is equal to itself and immutable">
344<script src="/resources/testharness.js"></script>
345<script src="/resources/testharnessreport.js"></script>
346<script>
347'use strict';
348
349// Synchronous test example.
350test(() => {
351 const value = true;
352 assert_true(value, 'true literal');
353 assert_equals(value.toString(), 'true', 'the string representation of true');
354}, 'The literal true in a synchronous test case');
355
356// Asynchronous test example.
357async_test(t => {
358 const originallyTrue = true;
359 setTimeout(t.step_func_done(() => {
360 assert_equals(originallyTrue, true);
361 }), 0);
362}, 'The literal true in a setTimeout callback');
363
364// Promise test example.
365promise_test(() => {
366 return new Promise((resolve, reject) => {
367 resolve(true);
368 }).then(value => {
369 assert_true(value);
370 });
371}, 'The literal true used to resolve a Promise');
372
373</script>
374```
375
376Some points that are not immediately obvious from the example:
377
ktyliue0bb9882017-01-10 01:47:50378* The `<meta name="assert">` describes the purpose of the entire file, and
379 is not redundant to `<title>`. Don't add a `<meta name="assert">` when the
380 information in the `<title>` is sufficient.
pwnall4ea2eb32016-11-29 02:47:25381* When calling an `assert_` function that compares two values, the first
382 argument is the actual value (produced by the functionality being tested), and
383 the second argument is the expected value (known good, golden). The order
384 is important, because the testing harness relies on it to generate expressive
385 error messages that are relied upon when debugging test failures.
386* The assertion description (the string argument to `assert_` methods) conveys
387 the way the actual value was obtained.
388 * If the expected value doesn't make it clear, the assertion description
389 should explain the desired behavior.
390 * Test cases with a single assertion should omit the assertion's description
391 when it is sufficiently clear.
392* Each test case describes the circumstance that it tests, without being
393 redundant.
394 * Do not start test case descriptions with redundant terms like "Testing"
395 or "Test for".
ktyliue0bb9882017-01-10 01:47:50396 * Test files with a single test case should omit the test case description.
397 The file's `<title>` should be sufficient to describe the scenario being
398 tested.
pwnall4ea2eb32016-11-29 02:47:25399* Asynchronous tests have a few subtleties.
400 * The `async_test` wrapper calls its function with a test case argument that
401 is used to signal when the test case is done, and to connect assertion
402 failures to the correct test.
403 * `t.done()` must be called after all the test case's assertions have
404 executed.
405 * Test case assertions (actually, any callback code that can throw
406 exceptions) must be wrapped in `t.step_func()` calls, so that
407 assertion failures and exceptions can be traced back to the correct test
408 case.
409 * `t.step_func_done()` is a shortcut that combines `t.step_func()` with a
410 `t.done()` call.
411
412*** promo
413Layout tests that load from `file://` origins must currently use relative paths
414to point to
415[/resources/testharness.js](../../third_party/WebKit/LayoutTests/resources/testharness.js)
416and
417[/resources/testharnessreport.js](../../third_party/WebKit/LayoutTests/resources/testharnessreport.js).
418This is contrary to the WPT guidelines, which call for absolute paths.
419This limitation does not apply to the tests in `LayoutTests/http`, which rely on
420an HTTP server, or to the tests in `LayoutTests/imported/wpt`, which are
421imported from the [WPT repository](https://github.com/w3c/web-platform-tests).
422***
423
424### WPT Supplemental Testing APIs
425
426Some tests simply cannot be expressed using the Web Platform APIs. For example,
427some tests that require a user to perform a gesture, such as a mouse click,
428cannot be implemented using Web APIs. The WPT project covers some of these cases
429via supplemental testing APIs.
430
431*** promo
432In many cases, the user gesture is not actually necessary. For example, many
433event handling tests can use
434[synthetic events](https://developer.mozilla.org/docs/Web/Guide/Events/Creating_and_triggering_events).
435***
436
437*** note
438TODO: document wpt_automation. Manual tests might end up moving here.
439***
440
441### Relying on Blink-Specific Testing APIs
442
443Tests that cannot be expressed using the Web Platform APIs or WPT's testing APIs
444use Blink-specific testing APIs. These APIs are only available in
445[content_shell](./layout_tests_in_content_shell.md), and should only be used as
446a last resort.
447
448A downside of Blink-specific APIs is that they are not as well documented as the
449Web Platform features. Learning to use a Blink-specific feature requires finding
450other tests that use it, or reading its source code.
451
452For example, the most popular Blink-specific API is `testRunner`, which is
453implemented in
454[components/test_runner/test_runner.h](../../components/test_runner/test_runner.h)
455and
456[components/test_runner/test_runner.cpp](../../components/test_runner/test_runner.cpp).
457By skimming the `TestRunnerBindings::Install` method, we learn that the
458testRunner API is presented by the `window.testRunner` and
459`window.layoutTestsController` objects, which are synonyms. Reading the
460`TestRunnerBindings::GetObjectTemplateBuilder` method tells us what properties
461are available on the `window.testRunner` object.
462
463*** aside
464`window.testRunner` is the preferred way to access the `testRunner` APIs.
465`window.layoutTestsController` is still supported because it is used by
4663rd-party tests.
467***
468
469*** note
470`testRunner` is the most popular testing API because it is also used indirectly
471by tests that stick to Web Platform APIs. The `testharnessreport.js` file in
472`testharness.js` is specifically designated to hold glue code that connects
473`testharness.js` to the testing environment. Our implementation is in
474[third_party/WebKit/LayoutTests/resources/testharnessreport.js](../../third_party/WebKit/LayoutTests/resources/testharnessreport.js),
475and uses the `testRunner` API.
476***
477
478See the [components/test_runner/](../../components/test_runner/) directory and
479[WebKit's LayoutTests guide](https://trac.webkit.org/wiki/Writing%20Layout%20Tests%20for%20DumpRenderTree)
480for other useful APIs. For example, `window.eventSender`
481([components/test_runner/event_sender.h](../../components/test_runner/event_sender.h)
482and
483[components/test_runner/event_sender.cpp](../../components/test_runner/event_sender.cpp))
484has methods that simulate events input such as keyboard / mouse input and
485drag-and-drop.
486
487Here is a UML diagram of how the `testRunner` bindings fit into Chromium.
488
489[![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:15490
pwnall4ea2eb32016-11-29 02:47:25491### Manual Tests
492
pwnall6acacd82016-12-02 01:40:15493&#x1F6A7; Whenever possible, tests that rely on (WPT's or Blink's) testing APIs
494should also be usable as
pwnall4ea2eb32016-11-29 02:47:25495[manual tests](http://testthewebforward.org/docs/manual-test.html). This makes
496it easy to debug the test, and to check whether our behavior matches other
497browsers.
498
pwnall6acacd82016-12-02 01:40:15499*** promo
500The recommendation to degrade to manual tests is still being discussed on
501[blink-dev](https://groups.google.com/a/chromium.org/d/topic/blink-dev/XsR6PKRrS1E/discussion).
502However, please keep in mind that a manual test can be debugged in the browser,
503whereas a test that does not degrade gracefully can only be debugged in the test
504runner. Fellow project members and future you will thank you for having your
505test work as a manual test.
pwnall4ea2eb32016-11-29 02:47:25506***
507
508Manual tests should minimize the chance of user error. This implies keeping the
509manual steps to a minimum, and having simple and clear instructions that
510describe all the configuration changes and user gestures that match the effect
511of the Blink-specific APIs used by the test.
512
513Below is an example of a fairly minimal test that uses a Blink-Specific API
514(`window.eventSender`), and gracefully degrades to a manual test.
515
516```html
517<!doctype html>
518<meta charset="utf-8">
ktyliue0bb9882017-01-10 01:47:50519<title>DOM: Event.isTrusted for UI events</title>
pwnall4ea2eb32016-11-29 02:47:25520<link rel="help" href="https://dom.spec.whatwg.org/#dom-event-istrusted">
521<link rel="help" href="https://dom.spec.whatwg.org/#constructing-events">
522<meta name="assert"
523 content="Event.isTrusted is true for events generated by user interaction">
524<script src="../../resources/testharness.js"></script>
525<script src="../../resources/testharnessreport.js"></script>
526
527<p>Please click on the button below.</p>
528<button>Click Me!</button>
529
530<script>
531'use strict';
532
533setup({ explicit_timeout: true });
534
535promise_test(() => {
536 const button = document.querySelector('button');
537 return new Promise((resolve, reject) => {
538 const button = document.querySelector('button');
539 button.addEventListener('click', (event) => {
540 resolve(event);
541 });
542
543 if (window.eventSender) {
544 eventSender.mouseMoveTo(button.offsetLeft, button.offsetTop);
545 eventSender.mouseDown();
546 eventSender.mouseUp();
547 }
548 }).then((clickEvent) => {
549 assert_true(clickEvent.isTrusted);
550 });
551
552}, 'Click generated by user interaction');
553
554</script>
555```
556
557The test exhibits the following desirable features:
558
559* It has a second specification URL (`<link rel="help">`), because the paragraph
560 that documents the tested feature (referenced by the primary URL) is not very
561 informative on its own.
562* It links to the
563 [WHATWG Living Standard](https://wiki.whatwg.org/wiki/FAQ#What_does_.22Living_Standard.22_mean.3F),
564 rather than to a frozen version of the specification.
565* It contains clear instructions for manually triggering the test conditions.
566 The test starts with a paragraph (`<p>`) that tells the tester exactly what to
567 do, and the `<button>` that needs to be clicked is clearly labeled.
568* It disables the timeout mechanism built into `testharness.js` by calling
569 `setup({ explicit_timeout: true });`
570* It checks for the presence of the Blink-specific testing APIs
571 (`window.eventSender`) before invoking them. The test does not automatically
572 fail when the APIs are not present.
573* It uses [Promises](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)
574 to separate the test setup from the assertions. This is particularly helpful
575 for manual tests that depend on a sequence of events to occur, as Promises
576 offer a composable way to express waiting for asynchronous events that avoids
577 [callback hell](http://stackabuse.com/avoiding-callback-hell-in-node-js/).
578
579Notice that the test is pretty heavy compared to a minimal JavaScript test that
580does not rely on testing APIs. Only use testing APIs when the desired testing
581conditions cannot be set up using Web Platform APIs.
582
583### Text Test Baselines
584
585By default, all the test cases in a file that uses `testharness.js` are expected
586to pass. However, in some cases, we prefer to add failing test cases to the
587repository, so that we can be notified when the failure modes change (e.g., we
588want to know if a test starts crashing rather than returning incorrect output).
589In these situations, a test file will be accompanied by a baseline, which is an
590`-expected.txt` file that contains the test's expected output.
591
592The baselines are generated automatically when appropriate by
593`run-webkit-tests`, which is described [here](./layout_tests.md), and by the
594[rebaselining tools](./layout_test_expectations.md).
595
596Text baselines for `testharness.js` should be avoided, as having a text baseline
597associated with a `testharness.js` indicates the presence of a bug. For this
598reason, CLs that add text baselines must include a
599[crbug.com](https://crbug.com) link for an issue tracking the removal of the
600text expectations.
601
602* When creating tests that will be upstreamed to WPT, and Blink's current
603 behavior does not match the specification that is being tested, a text
604 baseline is necessary. Remember to create an issue tracking the expectation's
605 removal, and to link the issue in the CL description.
606* Layout tests that cannot be upstreamed to WPT should use JavaScript to
607 document Blink's current behavior, rather than using JavaScript to document
608 desired behavior and a text file to document current behavior.
609
610### The js-test.js Legacy Harness
611
612*** promo
613For historical reasons, older tests are written using the `js-test` harness.
614This harness is **deprecated**, and should not be used for new tests.
615***
616
617If you need to understand old tests, the best `js-test` documentation is its
618implementation at
619[third_party/WebKit/LayoutTests/resources/js-test.js](../../third_party/WebKit/LayoutTests/resources/js-test.js).
620
621`js-test` tests lean heavily on the Blink-specific `testRunner` testing API.
622In a nutshell, the tests call `testRunner.dumpAsText()` to signal that the page
623content should be dumped and compared against a text baseline (an
624`-expected.txt` file). As a consequence, `js-test` tests are always accompanied
625by text baselines. Asynchronous tests also use `testRunner.waitUntilDone()` and
626`testRunner.notifyDone()` to tell the testing tools when they are complete.
627
628### Tests that use an HTTP Server
629
630By default, tests are loaded as if via `file:` URLs. Some web platform features
631require tests served via HTTP or HTTPS, for example absolute paths (`src=/foo`)
632or features restricted to secure protocols.
633
634HTTP tests are those under `LayoutTests/http/tests` (or virtual variants). Use a
635locally running HTTP server (Apache) to run them. Tests are served off of ports
6368000 and 8080 for HTTP, and 8443 for HTTPS. If you run the tests using
637`run-webkit-tests`, the server will be started automatically. To run the server
638manually to reproduce or debug a failure:
639
640```bash
641cd src/third_party/WebKit/Tools/Scripts
642run-blink-httpd start
643```
644
645The layout tests will be served from `http://127.0.0.1:8000`. For example, to
646run the test `http/tests/serviceworker/chromium/service-worker-allowed.html`,
647navigate to
648`http://127.0.0.1:8000/serviceworker/chromium/service-worker-allowed.html`. Some
649tests will behave differently if you go to 127.0.0.1 instead of localhost, so
650use 127.0.0.1.
651
652To kill the server, run `run-blink-httpd --server stop`, or just use `taskkill`
653or the Task Manager on Windows, and `killall` or Activity Monitor on MacOS.
654
655The test server sets up an alias to the `LayoutTests/resources` directory. In
656HTTP tests, you can access the testing framework at e.g.
pwnalle7819482016-12-17 00:58:40657`src="/resources/testharness.js"`.
pwnall4ea2eb32016-11-29 02:47:25658
659TODO: Document [wptserve](http://wptserve.readthedocs.io/) when we are in a
660position to use it to run layout tests.
661
662## Reference Tests (Reftests)
663
664Reference tests, also known as reftests, perform a pixel-by-pixel comparison
665between the rendered image of a test page and the rendered image of a reference
666page. Most reference tests pass if the two images match, but there are cases
667where it is useful to have a test pass when the two images do _not_ match.
668
669Reference tests are more difficult to debug than JavaScript tests, and tend to
670be slower as well. Therefore, they should only be used for functionality that
671cannot be covered by JavaScript tests.
672
673New reference tests should follow the
674[WPT reftests guidelines](http://testthewebforward.org/docs/reftests.html). The
675most important points are summarized below.
676
pwnall6acacd82016-12-02 01:40:15677* &#x1F6A7; The test page declares the reference page using a
678 `<link rel="match">` or `<link rel="mismatch">`, depending on whether the test
679 passes when the test image matches or does not match the reference image.
pwnall4ea2eb32016-11-29 02:47:25680* The reference page must not use the feature being tested. Otherwise, the test
681 is meaningless.
682* The reference page should be as simple as possible, and should not depend on
683 advanced features. Ideally, the reference page should render as intended even
684 on browsers with poor CSS support.
685* Reference tests should be self-describing.
686* Reference tests do _not_ include `testharness.js`.
687
pwnall6acacd82016-12-02 01:40:15688&#x1F6A7; Our testing infrastructure was designed for the
pwnall4ea2eb32016-11-29 02:47:25689[WebKit reftests](https://trac.webkit.org/wiki/Writing%20Reftests) that Blink
690has inherited. The consequences are summarized below.
691
692* Each reference page must be in the same directory as its associated test.
693 Given a test page named `foo` (e.g. `foo.html` or `foo.svg`),
694 * The reference page must be named `foo-expected` (e.g.,
695 `foo-expected.html`) if the test passes when the two images match.
696 * The reference page must be named `foo-expected-mismatch` (e.g.,
697 `foo-expected-mismatch.svg`) if the test passes when the two images do
698 _not_ match.
699* Multiple references and chained references are not supported.
700
701The following example demonstrates a reference test for
702[`<ol>`'s reversed attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ol).
703The example assumes that the test page is named `ol-reversed.html`.
704
705```html
706<!doctype html>
707<meta charset="utf-8">
708<link rel="match" href="ol-reversed-expected.html">
709
710<ol reversed>
711 <li>A</li>
712 <li>B</li>
713 <li>C</li>
714</ol>
715```
716
717The reference page, which must be named `ol-reversed-expected.html`, is below.
718
719```html
720<!doctype html>
721<meta charset="utf-8">
722
723<ol>
724 <li value="3">A</li>
725 <li value="2">B</li>
726 <li value="1">C</li>
727</ol>
728```
729
pwnall6acacd82016-12-02 01:40:15730*** promo
731The method for pointing out a test's reference page is still in flux, and is
732being discussed on
733[blink-dev](https://groups.google.com/a/chromium.org/d/topic/blink-dev/XsR6PKRrS1E/discussion).
734***
735
pwnall4ea2eb32016-11-29 02:47:25736## Pixel Tests
737
738`testRunner` APIs such as `window.testRunner.dumpAsTextWithPixelResults()` and
739`window.testRunner.dumpDragImage()` create an image result that is associated
740with the test. The image result is compared against an image baseline, which is
741an `-expected.png` file associated with the test, and the test passes if the
742image result is identical to the baseline, according to a pixel-by-pixel
743comparison. Tests that have image results (and baselines) are called **pixel
744tests**.
745
746Pixel tests should still follow the principles laid out above. Pixel tests pose
747unique challenges to the desire to have *self-describing* and *cross-platform*
748tests. The
749[WPT test style guidelines](http://testthewebforward.org/docs/test-style-guidelines.html)
750contain useful guidance. The most relevant pieces of advice are below.
751
752* Whenever possible, use a green paragraph / page / square to indicate success.
753 If that is not possible, make the test self-describing by including a textual
754 description of the desired (passing) outcome.
755* Only use the red color or the word `FAIL` to highlight errors. This does not
756 apply when testing the color red.
pwnall6acacd82016-12-02 01:40:15757* &#x1F6A7; Use the
758 [Ahem font](https://www.w3.org/Style/CSS/Test/Fonts/Ahem/README) to reduce the
759 variance introduced by the platform's text rendering system. This does not
760 apply when testing text, text flow, font selection, font fallback, font
761 features, or other typographic information.
pwnall4ea2eb32016-11-29 02:47:25762
763*** promo
764When using `window.testRunner.dumpAsTextWithPixelResults()`, the image result
765will always be 800x600px, because test pages are rendered in an 800x600px
766viewport. Pixel tests that do not specifically cover scrolling should fit in an
767800x600px viewport without creating scrollbars.
768***
769
pwnall6acacd82016-12-02 01:40:15770*** promo
771The recommendation of using Ahem in pixel tests is being discussed on
772[blink-dev](https://groups.google.com/a/chromium.org/d/topic/blink-dev/XsR6PKRrS1E/discussion).
773***
774
pwnall4ea2eb32016-11-29 02:47:25775The following snippet includes the Ahem font in a layout test.
776
777```html
778<style>
779body {
780 font: 10px Ahem;
781}
782</style>
783<script src="/resources/ahem.js"></script>
784```
785
786*** promo
787Tests outside `LayoutTests/http` and `LayoutTests/imported/wpt` currently need
788to use a relative path to
789[/third_party/WebKit/LayoutTests/resources/ahem.js](../../third_party/WebKit/LayoutTests/resources/ahem.js)
790***
791
792### Tests that need to paint, raster, or draw a frame of intermediate output
793
794A layout test does not actually draw frames of output until the test exits.
795Tests that need to generate a painted frame can use
796`window.testRunner.displayAsyncThen`, which will run the machinery to put up a
797frame, then call the passed callback. There is also a library at
798`fast/repaint/resources/text-based-repaint.js` to help with writing paint
799invalidation and repaint tests.
800
801## Dump Render Tree (DRT) Tests
802
803A Dump Render Tree test renders a web page and produces up to two results, which
804are compared against baseline files:
805
806* All tests output a textual representation of Blink's
807 [render tree](https://developers.google.com/web/fundamentals/performance/critical-rendering-path/render-tree-construction),
808 which is compared against an `-expected.txt` text baseline.
809* Some tests also output the image of the rendered page, which is compared
810 against an `-expected.png` image baseline, using the same method as pixel
811 tests.
812
813TODO: Document the API used by DRT tests to opt out of producing image results.
814
815A DRT test passes if _all_ of its results match their baselines. Like pixel
816tests, the output of DRT tests depends on platform-specific mechanisms, so DRT
817tests often require per-platform baselines. Furthermore, DRT tests depend on the
818render tree data structure, which means that if we replace the render tree data
819structure, we will need to look at each DRT test and consider whether it is
820still meaningful.
821
822For these reasons, DRT tests should **only** be used to cover aspects of the
823layout code that can only be tested by looking at the render tree. Any
824combination of the other test types is preferable to a DRT test. DRT tests are
825[inherited from WebKit](https://webkit.org/blog/1456/layout-tests-practice/), so
826the repository may have some unfortunate examples of DRT tests.
827
828The following page is an example of a DRT test.
829
830```html
831<!doctype html>
832<meta charset="utf-8">
833<style>
834body { font: 10px Ahem; }
835span::after {
836 content: "pass";
837 color: green;
838}
839</style>
840<script src="/resources/ahem.js"></script>
841
842<p><span>Pass if a green PASS appears to the right: </span></p>
843```
844
845The most important aspects of the example are that the test page does not
846include a testing framework, and that it follows the guidelines for pixel tests.
847The test page produces the text result below.
848
849```
850layer at (0,0) size 800x600
851 LayoutView at (0,0) size 800x600
852layer at (0,0) size 800x30
853 LayoutBlockFlow {HTML} at (0,0) size 800x30
854 LayoutBlockFlow {BODY} at (8,10) size 784x10
855 LayoutBlockFlow {P} at (0,0) size 784x10
856 LayoutInline {SPAN} at (0,0) size 470x10
857 LayoutText {#text} at (0,0) size 430x10
858 text run at (0,0) width 430: "Pass if a green PASS appears to the right: "
859 LayoutInline {<pseudo:after>} at (0,0) size 40x10 [color=#008000]
860 LayoutTextFragment (anonymous) at (430,0) size 40x10
861 text run at (430,0) width 40: "pass"
862```
863
864Notice that the test result above depends on the size of the `<p>` text. The
865test page uses the Ahem font (introduced above), whose main design goal is
866consistent cross-platform rendering. Had the test used another font, its text
867baseline would have depended on the fonts installed on the testing computer, and
868on the platform's font rendering system. Please follow the pixel tests
869guidelines and write reliable DRT tests!
870
871WebKit's render tree is described in
872[a series of posts](https://webkit.org/blog/114/webcore-rendering-i-the-basics/)
873on WebKit's blog. Some of the concepts there still apply to Blink's render tree.
874
875## Directory Structure
876
877The [LayoutTests directory](../../third_party/WebKit/LayoutTests) currently
878lacks a strict, formal structure. The following directories have special
879meaning:
880
881* The `http/` directory hosts tests that require an HTTP server (see above).
882* The `resources/` subdirectory in every directory contains binary files, such
883 as media files, and code that is shared by multiple test files.
884
885*** note
886Some layout tests consist of a minimal HTML page that references a JavaScript
887file in `resources/`. Please do not use this pattern for new tests, as it goes
888against the minimality principle. JavaScript and CSS files should only live in
889`resources/` if they are shared by at least two test files.
890***