blob: a541412f7f8b4db3f64c2b98e37b7d16f608f1f9 [file] [log] [blame] [view]
Kent Tamura59ffb022018-11-27 05:30:561# Writing Web Tests
pwnall4ea2eb32016-11-29 02:47:252
3[TOC]
4
5## Overview
6
Kent Tamura59ffb022018-11-27 05:30:567Web tests should be used to accomplish one of the following goals:
pwnall4ea2eb32016-11-29 02:47:258
91. The entire surface of Blink that is exposed to the Web should be covered by
foolipeda32ab2017-02-16 19:21:5810 tests that we contribute to [web-platform-tests](./web_platform_tests.md)
11 (WPT). This helps us avoid regressions, and helps us identify Web Platform
12 areas where the major browsers don't have interoperable implementations.
13 Furthermore, by contributing to projects such as WPT, we share the burden of
14 writing tests with the other browser vendors, and we help all the browsers
15 get better. This is very much in line with our goal to move the Web forward.
pwnall4ea2eb32016-11-29 02:47:25162. When a Blink feature cannot be tested using the tools provided by WPT, and
17 cannot be easily covered by
Kent Tamura6943cf792018-04-09 05:24:5418 [C++ unit tests](https://cs.chromium.org/chromium/src/third_party/blink/renderer/web/tests/?q=webframetest&sq=package:chromium&type=cs),
Kent Tamura59ffb022018-11-27 05:30:5619 the feature must be covered by web tests, to avoid unexpected regressions.
pwnall4ea2eb32016-11-29 02:47:2520 These tests will use Blink-specific testing APIs that are only available in
Kent Tamura59ffb022018-11-27 05:30:5621 [content_shell](./web_tests_in_content_shell.md).
pwnall4ea2eb32016-11-29 02:47:2522
Yoshisato Yanagisawa638e2ee02021-12-09 05:52:0823Note: if you are looking for a guide for the Web Platform Test, you should read
24["Web platform tests"](./web_platform_tests.md) (WPT). This document does not
Weizhong Xiac851d2f62022-03-22 15:56:4025cover WPT specific features/behaviors. **The WPT is recommended today rather than
26test types mentioned below!**
Yoshisato Yanagisawa638e2ee02021-12-09 05:52:0827
pwnall4ea2eb32016-11-29 02:47:2528*** promo
Kent Tamura59ffb022018-11-27 05:30:5629If you know that Blink web tests are upstreamed to other projects, such as
pwnall4ea2eb32016-11-29 02:47:2530[test262](https://github.com/tc39/test262), please update this document. Most
31importantly, our guidelines should to make it easy for our tests to be
pwnall6acacd82016-12-02 01:40:1532upstreamed. The
33[blink-dev mailing list](https://groups.google.com/a/chromium.org/forum/#!forum/blink-dev)
34will be happy to help you harmonize our current guidelines with communal test
35repositories.
pwnall4ea2eb32016-11-29 02:47:2536***
37
38### Test Types
39
Kent Tamura59ffb022018-11-27 05:30:5640There are four broad types of web tests, listed in the order of preference.
pwnall4ea2eb32016-11-29 02:47:2541
Kent Tamura59ffb022018-11-27 05:30:5642* *JavaScript Tests* are the web test implementation of
pwnall4ea2eb32016-11-29 02:47:2543 [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
dpranked2b7d642017-01-15 04:00:2452 baseline image in the repository. Pixel tests are less robust than the
53 first two types, because the rendering of a page is influenced by
pwnall4ea2eb32016-11-29 02:47:2554 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 common for a pixel test to have a
dpranked2b7d642017-01-15 04:00:2457 different reference image for each platform that Blink is tested on, and
58 the reference images are
Kent Tamura59ffb022018-11-27 05:30:5659 [quite cumbersome to manage](./web_test_expectations.md). You
Xianzhu Wang6e84f0992018-10-23 23:07:5560 should only write a pixel test if you cannot use a reference test.
61* *Text Tests* output pure text which represents the DOM tree, the DOM inner
62 text, internal data structure of Blink like layout tree or graphics layer
63 tree, or any custom text that the text wants to output. The test passes if the
64 output matches a baseline text file in the repository. Text tests outputting
65 internal data structures are used as a last resort to test the internal quirks
66 of the implementation, and they should be avoided in favor of one of other
dpranked2b7d642017-01-15 04:00:2467 options.
Xianzhu Wang6e84f0992018-10-23 23:07:5568* *Audio tests* output audio results.
69
70*** aside
71A JavaScript test is actually a special kind of text test, but its text
72baseline can be often omitted.
73***
74
75*** aside
76A test can be a reference/pixel test and a text test at the same time.
77***
pwnall59aadcb2017-01-26 23:27:2178
pwnall4ea2eb32016-11-29 02:47:2579## General Principles
80
pwnall6acacd82016-12-02 01:40:1581Tests should be written under the assumption that they will be upstreamed
pwnall59aadcb2017-01-26 23:27:2182to the WPT project. To this end, tests should follow the
Philip Jägenstedt3a3d5b82018-05-31 15:25:3583[WPT guidelines](https://web-platform-tests.org/writing-tests/).
pwnall6acacd82016-12-02 01:40:1584
pwnall6acacd82016-12-02 01:40:1585
Kent Tamura59ffb022018-11-27 05:30:5686There is no style guide that applies to all web tests. However, some projects
pwnall59aadcb2017-01-26 23:27:2187have adopted style guides, such as the
88[ServiceWorker Tests Style guide](https://www.chromium.org/blink/serviceworker/testing).
pwnall6acacd82016-12-02 01:40:1589
Kent Tamura59ffb022018-11-27 05:30:5690Our [document on web tests tips](./web_tests_tips.md) summarizes the most
pwnall59aadcb2017-01-26 23:27:2191important WPT guidelines and highlights some JavaScript concepts that are worth
92paying attention to when trying to infer style rules from existing tests. If
93you're unopinionated and looking for a style guide to follow, the document also
94suggests some defaults.
pwnall6acacd82016-12-02 01:40:1595
pwnall4ea2eb32016-11-29 02:47:2596## JavaScript Tests
97
98Whenever possible, the testing criteria should be expressed in JavaScript. The
99alternatives, which will be described in future sections, result in slower and
100less reliable tests.
101
102All new JavaScript tests should be written using the
Philip Jägenstedt3a3d5b82018-05-31 15:25:35103[testharness.js](https://github.com/web-platform-tests/wpt/tree/master/resources)
mekb330e312017-05-03 19:56:22104testing framework. This framework is used by the tests in the
Philip Jägenstedt3a3d5b82018-05-31 15:25:35105[web-platform-tests](https://github.com/web-platform-tests/wpt) repository,
pwnall4ea2eb32016-11-29 02:47:25106which is shared with all the other browser vendors, so `testharness.js` tests
107are more accessible to browser developers.
108
Philip Jägenstedt3a3d5b82018-05-31 15:25:35109See the [API documentation](https://web-platform-tests.org/writing-tests/testharness-api.html)
foolipeda32ab2017-02-16 19:21:58110for a thorough introduction to `testharness.js`.
111
Kent Tamura59ffb022018-11-27 05:30:56112Web tests should follow the recommendations of the above documentation.
113Furthermore, web tests should include relevant
Philip Jägenstedt3a3d5b82018-05-31 15:25:35114[metadata](https://web-platform-tests.org/writing-tests/css-metadata.html). The
pwnall4ea2eb32016-11-29 02:47:25115specification URL (in `<link rel="help">`) is almost always relevant, and is
116incredibly helpful to a developer who needs to understand the test quickly.
117
118Below is a skeleton for a JavaScript test embedded in an HTML page. Note that,
119in order to follow the minimality guideline, the test omits the tags `<html>`,
120`<head>`, and `<body>`, as they can be inferred by the HTML parser.
121
122```html
123<!doctype html>
pwnall59aadcb2017-01-26 23:27:21124<title>JavaScript: the true literal is immutable and equal to itself</title>
pwnall4ea2eb32016-11-29 02:47:25125<link rel="help" href="https://tc39.github.io/ecma262/#sec-boolean-literals">
pwnall4ea2eb32016-11-29 02:47:25126<script src="/resources/testharness.js"></script>
127<script src="/resources/testharnessreport.js"></script>
128<script>
129'use strict';
130
131// Synchronous test example.
132test(() => {
133 const value = true;
134 assert_true(value, 'true literal');
135 assert_equals(value.toString(), 'true', 'the string representation of true');
136}, 'The literal true in a synchronous test case');
137
138// Asynchronous test example.
139async_test(t => {
140 const originallyTrue = true;
141 setTimeout(t.step_func_done(() => {
142 assert_equals(originallyTrue, true);
143 }), 0);
144}, 'The literal true in a setTimeout callback');
145
146// Promise test example.
147promise_test(() => {
148 return new Promise((resolve, reject) => {
149 resolve(true);
150 }).then(value => {
151 assert_true(value);
152 });
153}, 'The literal true used to resolve a Promise');
154
155</script>
156```
157
158Some points that are not immediately obvious from the example:
159
pwnall4ea2eb32016-11-29 02:47:25160* When calling an `assert_` function that compares two values, the first
161 argument is the actual value (produced by the functionality being tested), and
162 the second argument is the expected value (known good, golden). The order
163 is important, because the testing harness relies on it to generate expressive
164 error messages that are relied upon when debugging test failures.
165* The assertion description (the string argument to `assert_` methods) conveys
166 the way the actual value was obtained.
167 * If the expected value doesn't make it clear, the assertion description
168 should explain the desired behavior.
169 * Test cases with a single assertion should omit the assertion's description
170 when it is sufficiently clear.
171* Each test case describes the circumstance that it tests, without being
172 redundant.
173 * Do not start test case descriptions with redundant terms like "Testing"
174 or "Test for".
ktyliue0bb9882017-01-10 01:47:50175 * Test files with a single test case should omit the test case description.
176 The file's `<title>` should be sufficient to describe the scenario being
177 tested.
pwnall4ea2eb32016-11-29 02:47:25178* Asynchronous tests have a few subtleties.
179 * The `async_test` wrapper calls its function with a test case argument that
180 is used to signal when the test case is done, and to connect assertion
181 failures to the correct test.
182 * `t.done()` must be called after all the test case's assertions have
183 executed.
184 * Test case assertions (actually, any callback code that can throw
185 exceptions) must be wrapped in `t.step_func()` calls, so that
186 assertion failures and exceptions can be traced back to the correct test
187 case.
188 * `t.step_func_done()` is a shortcut that combines `t.step_func()` with a
189 `t.done()` call.
190
191*** promo
Kent Tamura59ffb022018-11-27 05:30:56192Web tests that load from `file://` origins must currently use relative paths
pwnall4ea2eb32016-11-29 02:47:25193to point to
Kent Tamura59ffb022018-11-27 05:30:56194[/resources/testharness.js](../../third_party/blink/web_tests/resources/testharness.js)
pwnall4ea2eb32016-11-29 02:47:25195and
Kent Tamura59ffb022018-11-27 05:30:56196[/resources/testharnessreport.js](../../third_party/blink/web_tests/resources/testharnessreport.js).
pwnall4ea2eb32016-11-29 02:47:25197This is contrary to the WPT guidelines, which call for absolute paths.
Kent Tamura59ffb022018-11-27 05:30:56198This limitation does not apply to the tests in `web_tests/http`, which rely on
199an HTTP server, or to the tests in `web_tests/external/wpt`, which are
Philip Jägenstedt3a3d5b82018-05-31 15:25:35200imported from the [WPT repository](https://github.com/web-platform-tests/wpt).
pwnall4ea2eb32016-11-29 02:47:25201***
202
203### WPT Supplemental Testing APIs
204
205Some tests simply cannot be expressed using the Web Platform APIs. For example,
206some tests that require a user to perform a gesture, such as a mouse click,
207cannot be implemented using Web APIs. The WPT project covers some of these cases
208via supplemental testing APIs.
209
pwnall59aadcb2017-01-26 23:27:21210When writing tests that rely on supplemental testing APIs, please consider the
211cost and benefits of having the tests
Kent Tamura59ffb022018-11-27 05:30:56212[gracefully degrade to manual tests](./web_tests_with_manual_fallback.md) in
pwnall59aadcb2017-01-26 23:27:21213the absence of the testing APIs.
214
pwnall4ea2eb32016-11-29 02:47:25215*** promo
216In many cases, the user gesture is not actually necessary. For example, many
217event handling tests can use
218[synthetic events](https://developer.mozilla.org/docs/Web/Guide/Events/Creating_and_triggering_events).
219***
220
pwnall4ea2eb32016-11-29 02:47:25221### Relying on Blink-Specific Testing APIs
222
223Tests that cannot be expressed using the Web Platform APIs or WPT's testing APIs
224use Blink-specific testing APIs. These APIs are only available in
Kent Tamura59ffb022018-11-27 05:30:56225[content_shell](./web_tests_in_content_shell.md), and should only be used as
pwnall4ea2eb32016-11-29 02:47:25226a last resort.
227
228A downside of Blink-specific APIs is that they are not as well documented as the
229Web Platform features. Learning to use a Blink-specific feature requires finding
230other tests that use it, or reading its source code.
231
232For example, the most popular Blink-specific API is `testRunner`, which is
233implemented in
kenoe339a582022-12-22 10:36:20234[content/web_test/renderer/test_runner.h](../../content/web_test/renderer/test_runner.h)
pwnall4ea2eb32016-11-29 02:47:25235and
kenoe339a582022-12-22 10:36:20236[content/web_test/renderer/test_runner.cc](../../content/web_test/renderer/test_runner.cc).
pwnall4ea2eb32016-11-29 02:47:25237By skimming the `TestRunnerBindings::Install` method, we learn that the
Xianzhu Wangaf4fa412018-05-14 21:26:52238testRunner API is presented by the `.testRunner` etc. objects. Reading the
pwnall4ea2eb32016-11-29 02:47:25239`TestRunnerBindings::GetObjectTemplateBuilder` method tells us what properties
Xianzhu Wangaf4fa412018-05-14 21:26:52240are available on the `testRunner` object.
pwnall4ea2eb32016-11-29 02:47:25241
Xianzhu Wangaf4fa412018-05-14 21:26:52242Another popular Blink-specific API 'internals' defined in
243[third_party/blink/renderer/core/testing/internals.idl](../../third_party/blink/renderer/core/testing/internals.idl)
244contains more direct access to blink internals.
245
246*** note
247If possible, a test using blink-specific testing APIs should be written not to
248depend on the APIs, so that it can also work directly in a browser. If the test
249does need the APIs to work, it should still check if the API is available before
250using the API. Note that though we omit the `window.` prefix when using the
251APIs, we should use the qualified name in the `if` statement:
252```javascript
253 if (window.testRunner)
254 testRunner.waitUntilDone();
255```
pwnall4ea2eb32016-11-29 02:47:25256***
257
258*** note
259`testRunner` is the most popular testing API because it is also used indirectly
260by tests that stick to Web Platform APIs. The `testharnessreport.js` file in
261`testharness.js` is specifically designated to hold glue code that connects
262`testharness.js` to the testing environment. Our implementation is in
Kent Tamura59ffb022018-11-27 05:30:56263[third_party/blink/web_tests/resources/testharnessreport.js](../../third_party/blink/web_tests/resources/testharnessreport.js),
pwnall4ea2eb32016-11-29 02:47:25264and uses the `testRunner` API.
265***
266
Jonathan Lee80280d22023-11-27 22:40:56267See the [content/web_test/renderer/](../../content/web_test/renderer/) directory and
pwnall4ea2eb32016-11-29 02:47:25268[WebKit's LayoutTests guide](https://trac.webkit.org/wiki/Writing%20Layout%20Tests%20for%20DumpRenderTree)
Xianzhu Wangaf4fa412018-05-14 21:26:52269for other useful APIs. For example, `eventSender`
Jonathan Lee80280d22023-11-27 22:40:56270([content/shell/renderer/web_test/event_sender.h](../../content/web_test/renderer/event_sender.h)
pwnall4ea2eb32016-11-29 02:47:25271and
Jonathan Lee80280d22023-11-27 22:40:56272[content/shell/renderer/web_test/event_sender.cc](../../content/web_test/renderer/event_sender.cc))
pwnall4ea2eb32016-11-29 02:47:25273has methods that simulate events input such as keyboard / mouse input and
274drag-and-drop.
275
276Here is a UML diagram of how the `testRunner` bindings fit into Chromium.
277
278[![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:15279
pwnall4ea2eb32016-11-29 02:47:25280### Text Test Baselines
281
282By default, all the test cases in a file that uses `testharness.js` are expected
283to pass. However, in some cases, we prefer to add failing test cases to the
284repository, so that we can be notified when the failure modes change (e.g., we
285want to know if a test starts crashing rather than returning incorrect output).
286In these situations, a test file will be accompanied by a baseline, which is an
287`-expected.txt` file that contains the test's expected output.
288
289The baselines are generated automatically when appropriate by
Kent Tamura59ffb022018-11-27 05:30:56290`run_web_tests.py`, which is described [here](./web_tests.md), and by the
291[rebaselining tools](./web_test_expectations.md).
pwnall4ea2eb32016-11-29 02:47:25292
293Text baselines for `testharness.js` should be avoided, as having a text baseline
Jonathan Leedfcfcf4d2023-03-31 01:10:30294associated with a `testharness.js` test usually indicates the presence of a bug.
295For this reason, CLs that add text baselines must include a
pwnall4ea2eb32016-11-29 02:47:25296[crbug.com](https://crbug.com) link for an issue tracking the removal of the
297text expectations.
298
299* When creating tests that will be upstreamed to WPT, and Blink's current
300 behavior does not match the specification that is being tested, a text
301 baseline is necessary. Remember to create an issue tracking the expectation's
302 removal, and to link the issue in the CL description.
Kent Tamura59ffb022018-11-27 05:30:56303* Web tests that cannot be upstreamed to WPT should use JavaScript to
pwnall4ea2eb32016-11-29 02:47:25304 document Blink's current behavior, rather than using JavaScript to document
305 desired behavior and a text file to document current behavior.
306
Jonathan Leedfcfcf4d2023-03-31 01:10:30307*** promo
308Because of [baseline fallback](./web_test_baseline_fallback.md), it may not be
309possible to [represent a platform-specific all-`PASS`
310status](https://crbug.com/1324638) by the platform baseline's absence. In such
Kaylee Lubickf7bfd2f2025-03-10 20:30:37311rare cases, `blink_tool.py rebaseline-cl` will generate a placeholder baseline
Jonathan Leedfcfcf4d2023-03-31 01:10:30312indicating to `run_web_tests.py` that all subtests are meant to pass:
313
314```
315This is a testharness.js-based test.
316All subtests passed and are omitted for brevity.
317See https://chromium.googlesource.com/chromium/src/+/HEAD/docs/testing/writing_web_tests.md#Text-Test-Baselines for details.
318Harness: the test ran to completion.
319```
320
Kaylee Lubickf7bfd2f2025-03-10 20:30:37321`blink_tool.py optimize-baselines` will automatically remove these placeholder
Jonathan Leedfcfcf4d2023-03-31 01:10:30322baselines once all platforms are all-`PASS`.
323***
324
pwnall4ea2eb32016-11-29 02:47:25325### The js-test.js Legacy Harness
326
327*** promo
328For historical reasons, older tests are written using the `js-test` harness.
329This harness is **deprecated**, and should not be used for new tests.
330***
331
332If you need to understand old tests, the best `js-test` documentation is its
333implementation at
Kent Tamura59ffb022018-11-27 05:30:56334[third_party/blink/web_tests/resources/js-test.js](../../third_party/blink/web_tests/resources/js-test.js).
pwnall4ea2eb32016-11-29 02:47:25335
336`js-test` tests lean heavily on the Blink-specific `testRunner` testing API.
337In a nutshell, the tests call `testRunner.dumpAsText()` to signal that the page
338content should be dumped and compared against a text baseline (an
339`-expected.txt` file). As a consequence, `js-test` tests are always accompanied
340by text baselines. Asynchronous tests also use `testRunner.waitUntilDone()` and
341`testRunner.notifyDone()` to tell the testing tools when they are complete.
342
343### Tests that use an HTTP Server
344
345By default, tests are loaded as if via `file:` URLs. Some web platform features
346require tests served via HTTP or HTTPS, for example absolute paths (`src=/foo`)
347or features restricted to secure protocols.
348
Kent Tamura59ffb022018-11-27 05:30:56349HTTP tests are those under `web_tests/http/tests` (or virtual variants). Use a
pwnall4ea2eb32016-11-29 02:47:25350locally running HTTP server (Apache) to run them. Tests are served off of ports
3518000 and 8080 for HTTP, and 8443 for HTTPS. If you run the tests using
Kent Tamuraa045a7f2018-04-25 05:08:11352`run_web_tests.py`, the server will be started automatically. To run the server
pwnall4ea2eb32016-11-29 02:47:25353manually to reproduce or debug a failure:
354
355```bash
Kent Tamurae81dbff2018-04-20 17:35:34356cd src/third_party/blink/tools
357./run_blink_httpd.py
pwnall4ea2eb32016-11-29 02:47:25358```
359
Kent Tamura59ffb022018-11-27 05:30:56360The web tests will be served from `http://127.0.0.1:8000`. For example, to
pwnall4ea2eb32016-11-29 02:47:25361run the test `http/tests/serviceworker/chromium/service-worker-allowed.html`,
362navigate to
363`http://127.0.0.1:8000/serviceworker/chromium/service-worker-allowed.html`. Some
364tests will behave differently if you go to 127.0.0.1 instead of localhost, so
365use 127.0.0.1.
366
Kent Tamurae81dbff2018-04-20 17:35:34367To kill the server, hit any key on the terminal where `run_blink_httpd.py` is
Hajime Hoshia6fad022017-08-01 17:57:58368running, or just use `taskkill` or the Task Manager on Windows, and `killall` or
369Activity Monitor on MacOS.
pwnall4ea2eb32016-11-29 02:47:25370
Kent Tamura59ffb022018-11-27 05:30:56371The test server sets up an alias to the `web_tests/resources` directory. In
pwnall4ea2eb32016-11-29 02:47:25372HTTP tests, you can access the testing framework at e.g.
pwnalle7819482016-12-17 00:58:40373`src="/resources/testharness.js"`.
pwnall4ea2eb32016-11-29 02:47:25374
375TODO: Document [wptserve](http://wptserve.readthedocs.io/) when we are in a
Kent Tamura59ffb022018-11-27 05:30:56376position to use it to run web tests.
pwnall4ea2eb32016-11-29 02:47:25377
378## Reference Tests (Reftests)
379
380Reference tests, also known as reftests, perform a pixel-by-pixel comparison
381between the rendered image of a test page and the rendered image of a reference
382page. Most reference tests pass if the two images match, but there are cases
383where it is useful to have a test pass when the two images do _not_ match.
384
385Reference tests are more difficult to debug than JavaScript tests, and tend to
386be slower as well. Therefore, they should only be used for functionality that
387cannot be covered by JavaScript tests.
388
389New reference tests should follow the
Philip Jägenstedt3a3d5b82018-05-31 15:25:35390[WPT reftests guidelines](https://web-platform-tests.org/writing-tests/reftests.html).
foolipeda32ab2017-02-16 19:21:58391The most important points are summarized below.
pwnall4ea2eb32016-11-29 02:47:25392
pwnall6acacd82016-12-02 01:40:15393* &#x1F6A7; The test page declares the reference page using a
394 `<link rel="match">` or `<link rel="mismatch">`, depending on whether the test
395 passes when the test image matches or does not match the reference image.
pwnall4ea2eb32016-11-29 02:47:25396* The reference page must not use the feature being tested. Otherwise, the test
397 is meaningless.
398* The reference page should be as simple as possible, and should not depend on
399 advanced features. Ideally, the reference page should render as intended even
400 on browsers with poor CSS support.
401* Reference tests should be self-describing.
402* Reference tests do _not_ include `testharness.js`.
403
pwnall6acacd82016-12-02 01:40:15404&#x1F6A7; Our testing infrastructure was designed for the
pwnall4ea2eb32016-11-29 02:47:25405[WebKit reftests](https://trac.webkit.org/wiki/Writing%20Reftests) that Blink
406has inherited. The consequences are summarized below.
407
408* Each reference page must be in the same directory as its associated test.
409 Given a test page named `foo` (e.g. `foo.html` or `foo.svg`),
410 * The reference page must be named `foo-expected` (e.g.,
411 `foo-expected.html`) if the test passes when the two images match.
412 * The reference page must be named `foo-expected-mismatch` (e.g.,
413 `foo-expected-mismatch.svg`) if the test passes when the two images do
414 _not_ match.
415* Multiple references and chained references are not supported.
416
417The following example demonstrates a reference test for
418[`<ol>`'s reversed attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ol).
419The example assumes that the test page is named `ol-reversed.html`.
420
421```html
422<!doctype html>
pwnall4ea2eb32016-11-29 02:47:25423<link rel="match" href="ol-reversed-expected.html">
424
425<ol reversed>
426 <li>A</li>
427 <li>B</li>
428 <li>C</li>
429</ol>
430```
431
432The reference page, which must be named `ol-reversed-expected.html`, is below.
433
434```html
435<!doctype html>
pwnall4ea2eb32016-11-29 02:47:25436
437<ol>
438 <li value="3">A</li>
439 <li value="2">B</li>
440 <li value="1">C</li>
441</ol>
442```
443
pwnall6acacd82016-12-02 01:40:15444*** promo
445The method for pointing out a test's reference page is still in flux, and is
446being discussed on
447[blink-dev](https://groups.google.com/a/chromium.org/d/topic/blink-dev/XsR6PKRrS1E/discussion).
448***
449
pwnall4ea2eb32016-11-29 02:47:25450## Pixel Tests
451
Xianzhu Wang6e84f0992018-10-23 23:07:55452A test creates an image result by default unless some `testRunner` API is
453called (e.g. `testRunner.dumpAsText()`, `testRunner.dumpAsLayout()`, see
454[text tests](#text-tests)) to suppress the image result. A test is a
455**pixel test** if it creates an image result but is not a reference test.
456The image result is compared against an image baseline, which is an
457`-expected.png` file associated with the test, and the test passes if the
pwnall4ea2eb32016-11-29 02:47:25458image result is identical to the baseline, according to a pixel-by-pixel
Xianzhu Wang6e84f0992018-10-23 23:07:55459comparison.
pwnall4ea2eb32016-11-29 02:47:25460
461Pixel tests should still follow the principles laid out above. Pixel tests pose
462unique challenges to the desire to have *self-describing* and *cross-platform*
463tests. The
Philip Jägenstedt3a3d5b82018-05-31 15:25:35464[WPT rendering test guidelines](https://web-platform-tests.org/writing-tests/rendering.html)
pwnall4ea2eb32016-11-29 02:47:25465contain useful guidance. The most relevant pieces of advice are below.
466
467* Whenever possible, use a green paragraph / page / square to indicate success.
468 If that is not possible, make the test self-describing by including a textual
469 description of the desired (passing) outcome.
470* Only use the red color or the word `FAIL` to highlight errors. This does not
471 apply when testing the color red.
pwnall6acacd82016-12-02 01:40:15472* &#x1F6A7; Use the
473 [Ahem font](https://www.w3.org/Style/CSS/Test/Fonts/Ahem/README) to reduce the
474 variance introduced by the platform's text rendering system. This does not
475 apply when testing text, text flow, font selection, font fallback, font
476 features, or other typographic information.
pwnall4ea2eb32016-11-29 02:47:25477
478*** promo
Xianzhu Wang6e84f0992018-10-23 23:07:55479The default size of the image result of a pixel test is 800x600px, because test
480pages are rendered in an 800x600px viewport by default. Normally pixel tests
481that do not specifically cover scrolling should fit in an 800x600px viewport
482without creating scrollbars.
pwnall4ea2eb32016-11-29 02:47:25483***
484
pwnall6acacd82016-12-02 01:40:15485*** promo
486The recommendation of using Ahem in pixel tests is being discussed on
487[blink-dev](https://groups.google.com/a/chromium.org/d/topic/blink-dev/XsR6PKRrS1E/discussion).
488***
489
Kent Tamura59ffb022018-11-27 05:30:56490The following snippet includes the Ahem font in a web test.
pwnall4ea2eb32016-11-29 02:47:25491
492```html
493<style>
494body {
495 font: 10px Ahem;
496}
497</style>
498<script src="/resources/ahem.js"></script>
499```
500
501*** promo
Kent Tamura59ffb022018-11-27 05:30:56502Tests outside `web_tests/http` and `web_tests/external/wpt` currently need
pwnall4ea2eb32016-11-29 02:47:25503to use a relative path to
Kent Tamura59ffb022018-11-27 05:30:56504[/third_party/blink/web_tests/resources/ahem.js](../../third_party/blink/web_tests/resources/ahem.js)
pwnall4ea2eb32016-11-29 02:47:25505***
506
507### Tests that need to paint, raster, or draw a frame of intermediate output
508
Kent Tamura59ffb022018-11-27 05:30:56509A web test does not actually draw frames of output until the test exits.
Xianzhu Wangaf4fa412018-05-14 21:26:52510Tests that need to generate a painted frame can use `runAfterLayoutAndPaint()`
Kent Tamura59ffb022018-11-27 05:30:56511defined in [third_party/blink/web_tests/resources/run-after-layout-and-paint.js](../../third_party/blink/web_tests/resources/run-after-layout-and-paint.js)
Xianzhu Wangaf4fa412018-05-14 21:26:52512which will run the machinery to put up a frame, then call the passed callback.
513There is also a library at
Kent Tamura59ffb022018-11-27 05:30:56514[third_party/blink/web_tests/paint/invalidation/resources/text-based-repaint.js](../../third_party/blink/web_tests/paint/invalidation/resources/text-based-repaint.js)
Xianzhu Wangaf4fa412018-05-14 21:26:52515to help with writing paint invalidation and repaint tests.
pwnall4ea2eb32016-11-29 02:47:25516
Mason Freedb0855622018-10-02 17:49:47517### Tests for scrolling animations
518
Kent Tamura59ffb022018-11-27 05:30:56519Some web tests need to ensure animations such as middle-click auto-scroll,
Mason Freedb0855622018-10-02 17:49:47520fling, etc. get performed properly. When testing in display compositor pixel
521dump mode (now the standard), the standard behavior for tests is to
522synchronously composite without rastering (to save time). However, animations
523run upon surface activation, which only happens once rasterization is performed.
524Therefore, for these tests, an additional setting needs to be set. Near the
Mason Freedc41b2d082018-10-26 17:20:21525beginning of these tests, call `setAnimationRequiresRaster()` defined in
Kent Tamura59ffb022018-11-27 05:30:56526[third_party/blink/web_tests/resources/compositor-controls.js](../../third_party/blink/web_tests/resources/compositor-controls.js)
Mason Freedb0855622018-10-02 17:49:47527which will enable full rasterization during the test.
528
Xianzhu Wang6e84f0992018-10-23 23:07:55529## Text tests
pwnall4ea2eb32016-11-29 02:47:25530
Xianzhu Wang6e84f0992018-10-23 23:07:55531A **text test** outputs text result. The result is compared against a text
532baseline which is an `-expected.txt` file associated with the test, and the
533test passes if the text result is identical to the baseline. A test isn't a
534text test by default until it calls some `testRunner` API to instruct the
535test runner to output text. A text test can be categorized based on what kind of
536information that the text result represents.
pwnall4ea2eb32016-11-29 02:47:25537
Xianzhu Wang6e84f0992018-10-23 23:07:55538### Layout tree test
pwnall4ea2eb32016-11-29 02:47:25539
Xianzhu Wang6e84f0992018-10-23 23:07:55540If a test calls `testRunner.dumpAsLayout()` or
541`testRunner.dumpAsLayoutWithPixelResults()`, The text result will be a
542textual representation of Blink's
543[layout tree](https://developers.google.com/web/fundamentals/performance/critical-rendering-path/render-tree-construction)
544(called the render tree on that page) of the main frame of the test page.
545With `testRunner.dumpChildFrames()` the text result will also include layout
546tree of child frames.
pwnall4ea2eb32016-11-29 02:47:25547
Xianzhu Wang6e84f0992018-10-23 23:07:55548Like pixel tests, the output of layout tree tests may depend on
549platform-specific details, so layout tree tests often require per-platform
550baselines. Furthermore, since the tests obviously depend on the layout tree
551structure, that means that if we change the layout tree you have to rebaseline
552each layout tree test to see if the results are still correct and whether the
553test is still meaningful. There are actually many cases where the layout tree
dpranked2b7d642017-01-15 04:00:24554output is misstated (i.e., wrong), because people didn't want to have to update
555existing baselines and tests. This is really unfortunate and confusing.
556
557For these reasons, layout tree tests should **only** be used to cover aspects
558of the layout code that can only be tested by looking at the layout tree. Any
559combination of the other test types is preferable to a layout tree test.
560Layout tree tests are
pwnall4ea2eb32016-11-29 02:47:25561[inherited from WebKit](https://webkit.org/blog/1456/layout-tests-practice/), so
dpranked2b7d642017-01-15 04:00:24562the repository may have some unfortunate examples of layout tree tests.
pwnall4ea2eb32016-11-29 02:47:25563
dpranked2b7d642017-01-15 04:00:24564The following page is an example of a layout tree test.
pwnall4ea2eb32016-11-29 02:47:25565
566```html
567<!doctype html>
pwnall4ea2eb32016-11-29 02:47:25568<style>
569body { font: 10px Ahem; }
570span::after {
571 content: "pass";
572 color: green;
573}
574</style>
575<script src="/resources/ahem.js"></script>
Xianzhu Wang6e84f0992018-10-23 23:07:55576<script>
577 if (window.testRunner)
578 testRunner.dumpAsLayout();
579</script>
pwnall4ea2eb32016-11-29 02:47:25580<p><span>Pass if a green PASS appears to the right: </span></p>
581```
582
pwnall4ea2eb32016-11-29 02:47:25583The test page produces the text result below.
584
585```
586layer at (0,0) size 800x600
587 LayoutView at (0,0) size 800x600
588layer at (0,0) size 800x30
589 LayoutBlockFlow {HTML} at (0,0) size 800x30
590 LayoutBlockFlow {BODY} at (8,10) size 784x10
591 LayoutBlockFlow {P} at (0,0) size 784x10
592 LayoutInline {SPAN} at (0,0) size 470x10
593 LayoutText {#text} at (0,0) size 430x10
594 text run at (0,0) width 430: "Pass if a green PASS appears to the right: "
595 LayoutInline {<pseudo:after>} at (0,0) size 40x10 [color=#008000]
596 LayoutTextFragment (anonymous) at (430,0) size 40x10
597 text run at (430,0) width 40: "pass"
598```
599
600Notice that the test result above depends on the size of the `<p>` text. The
601test page uses the Ahem font (introduced above), whose main design goal is
602consistent cross-platform rendering. Had the test used another font, its text
603baseline would have depended on the fonts installed on the testing computer, and
604on the platform's font rendering system. Please follow the pixel tests
dpranked2b7d642017-01-15 04:00:24605guidelines and write reliable layout tree tests!
pwnall4ea2eb32016-11-29 02:47:25606
dpranked2b7d642017-01-15 04:00:24607WebKit's layout tree is described in
pwnall4ea2eb32016-11-29 02:47:25608[a series of posts](https://webkit.org/blog/114/webcore-rendering-i-the-basics/)
dpranked2b7d642017-01-15 04:00:24609on WebKit's blog. Some of the concepts there still apply to Blink's layout tree.
pwnall4ea2eb32016-11-29 02:47:25610
Xianzhu Wang6e84f0992018-10-23 23:07:55611### Text dump test
612
613If `testRunner.dumpAsText()` or `testRunner.dumpAsTextWithPixelResults()`
614is called from a test, the test will dump the text contents of the main frame
615of the tested page. With `testRunner.dumpChildFrames()` the text
616result will also include text contents of child frames. Actually a JavaScript
617test is a special kind of text dump test which can often omit the text baseline.
618
619A test can override the default text dump by calling
620`testRunner.setCustomTextOutput(string)`. The string parameter can be any
621text that the test wants to output. The [`internals` API](../../third_party/blink/renderer/core/testing/internals.idl]
622provides methods to get textual representations of internal data structures that
623can be used as the parameter of `testRunner.setCustomTextOutput()`.
624
625### Markup dump test
626
627If a test calls `testRunner.dumpAsMarkup()`, the text result will be the DOM
628of the main frame of the test. With `testRunner.dumpChildFrames()` the text
629result will also include DOM of child frames.
630
631## Audio tests
632
633If a test calls `testRunner.setAudioData(array_buffer)`, the test will
634create an audio result. The result will be compared against an audio baseline
635which is an `-expected.wav` file associated with the test, and the test passes
636if the audio result is identical to the baseline.
637
638## Tests that are both pixel/reference tests and text tests
639
640If a test calls `testRunner.dumpAsTextWithPixelResults()` or
641`testRunner.dumpAsLayoutWithPixelResults()`, the test is both a
642pixel/reference test and a text test. It will output both pixel result and text
643result.
644
Xianzhu Wang4bbcebe2018-10-31 02:21:46645For a test that is both a pixel/reference test and a text test, both pixel and
646text results will be compared to baselines, and the test passes if each result
647matches the corresponding baseline.
Xianzhu Wang6e84f0992018-10-23 23:07:55648
Kent Tamura59ffb022018-11-27 05:30:56649Many of the [paint invalidation tests](../../third_party/blink/web_tests/paint/invalidation)
Xianzhu Wang6e84f0992018-10-23 23:07:55650are of this type. The pixel results (compared against `-expected.png` or
651`-expected.html`) ensure correct rendering, and the text results (compared
652against `-expected.txt`) ensure correct compositing and raster invalidation
653(without unexpected over and under invalidations).
654
655For a layout tree test, whether you want a pixel test and/or a text test depends
656on whether you care about the visual image, the details of how that image was
657constructed, or both. It is possible for multiple layout trees to produce
658the same pixel output, so it is important to make it clear in the test
659which outputs you really care about.
660
pwnall4ea2eb32016-11-29 02:47:25661## Directory Structure
662
Kent Tamura59ffb022018-11-27 05:30:56663The [web_tests directory](../../third_party/blink/web_tests) currently
pwnall4ea2eb32016-11-29 02:47:25664lacks a strict, formal structure. The following directories have special
665meaning:
666
667* The `http/` directory hosts tests that require an HTTP server (see above).
668* The `resources/` subdirectory in every directory contains binary files, such
669 as media files, and code that is shared by multiple test files.
670
671*** note
Kent Tamura59ffb022018-11-27 05:30:56672Some web tests consist of a minimal HTML page that references a JavaScript
pwnall4ea2eb32016-11-29 02:47:25673file in `resources/`. Please do not use this pattern for new tests, as it goes
674against the minimality principle. JavaScript and CSS files should only live in
675`resources/` if they are shared by at least two test files.
676***