blob: 33ae3978db168c4e3c57ff780d50d286d9e7b8a1 [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
23*** promo
Kent Tamura59ffb022018-11-27 05:30:5624If you know that Blink web tests are upstreamed to other projects, such as
pwnall4ea2eb32016-11-29 02:47:2525[test262](https://github.com/tc39/test262), please update this document. Most
26importantly, our guidelines should to make it easy for our tests to be
pwnall6acacd82016-12-02 01:40:1527upstreamed. The
28[blink-dev mailing list](https://groups.google.com/a/chromium.org/forum/#!forum/blink-dev)
29will be happy to help you harmonize our current guidelines with communal test
30repositories.
pwnall4ea2eb32016-11-29 02:47:2531***
32
33### Test Types
34
Kent Tamura59ffb022018-11-27 05:30:5635There are four broad types of web tests, listed in the order of preference.
pwnall4ea2eb32016-11-29 02:47:2536
Kent Tamura59ffb022018-11-27 05:30:5637* *JavaScript Tests* are the web test implementation of
pwnall4ea2eb32016-11-29 02:47:2538 [xUnit tests](https://en.wikipedia.org/wiki/XUnit). These tests contain
39 assertions written in JavaScript, and pass if the assertions evaluate to
40 true.
41* *Reference Tests* render a test page and a reference page, and pass if the two
42 renderings are identical, according to a pixel-by-pixel comparison. These
43 tests are less robust, harder to debug, and significantly slower than
44 JavaScript tests, and are only used when JavaScript tests are insufficient,
45 such as when testing paint code.
46* *Pixel Tests* render a test page and compare the result against a pre-rendered
dpranked2b7d642017-01-15 04:00:2447 baseline image in the repository. Pixel tests are less robust than the
48 first two types, because the rendering of a page is influenced by
pwnall4ea2eb32016-11-29 02:47:2549 many factors such as the host computer's graphics card and driver, the
50 platform's text rendering system, and various user-configurable operating
51 system settings. For this reason, it is common for a pixel test to have a
dpranked2b7d642017-01-15 04:00:2452 different reference image for each platform that Blink is tested on, and
53 the reference images are
Kent Tamura59ffb022018-11-27 05:30:5654 [quite cumbersome to manage](./web_test_expectations.md). You
Xianzhu Wang6e84f0992018-10-23 23:07:5555 should only write a pixel test if you cannot use a reference test.
56* *Text Tests* output pure text which represents the DOM tree, the DOM inner
57 text, internal data structure of Blink like layout tree or graphics layer
58 tree, or any custom text that the text wants to output. The test passes if the
59 output matches a baseline text file in the repository. Text tests outputting
60 internal data structures are used as a last resort to test the internal quirks
61 of the implementation, and they should be avoided in favor of one of other
dpranked2b7d642017-01-15 04:00:2462 options.
Xianzhu Wang6e84f0992018-10-23 23:07:5563* *Audio tests* output audio results.
64
65*** aside
66A JavaScript test is actually a special kind of text test, but its text
67baseline can be often omitted.
68***
69
70*** aside
71A test can be a reference/pixel test and a text test at the same time.
72***
pwnall59aadcb2017-01-26 23:27:2173
pwnall4ea2eb32016-11-29 02:47:2574## General Principles
75
pwnall6acacd82016-12-02 01:40:1576Tests should be written under the assumption that they will be upstreamed
pwnall59aadcb2017-01-26 23:27:2177to the WPT project. To this end, tests should follow the
Philip Jägenstedt3a3d5b82018-05-31 15:25:3578[WPT guidelines](https://web-platform-tests.org/writing-tests/).
pwnall6acacd82016-12-02 01:40:1579
pwnall6acacd82016-12-02 01:40:1580
Kent Tamura59ffb022018-11-27 05:30:5681There is no style guide that applies to all web tests. However, some projects
pwnall59aadcb2017-01-26 23:27:2182have adopted style guides, such as the
83[ServiceWorker Tests Style guide](https://www.chromium.org/blink/serviceworker/testing).
pwnall6acacd82016-12-02 01:40:1584
Kent Tamura59ffb022018-11-27 05:30:5685Our [document on web tests tips](./web_tests_tips.md) summarizes the most
pwnall59aadcb2017-01-26 23:27:2186important WPT guidelines and highlights some JavaScript concepts that are worth
87paying attention to when trying to infer style rules from existing tests. If
88you're unopinionated and looking for a style guide to follow, the document also
89suggests some defaults.
pwnall6acacd82016-12-02 01:40:1590
pwnall4ea2eb32016-11-29 02:47:2591## JavaScript Tests
92
93Whenever possible, the testing criteria should be expressed in JavaScript. The
94alternatives, which will be described in future sections, result in slower and
95less reliable tests.
96
97All new JavaScript tests should be written using the
Philip Jägenstedt3a3d5b82018-05-31 15:25:3598[testharness.js](https://github.com/web-platform-tests/wpt/tree/master/resources)
mekb330e312017-05-03 19:56:2299testing framework. This framework is used by the tests in the
Philip Jägenstedt3a3d5b82018-05-31 15:25:35100[web-platform-tests](https://github.com/web-platform-tests/wpt) repository,
pwnall4ea2eb32016-11-29 02:47:25101which is shared with all the other browser vendors, so `testharness.js` tests
102are more accessible to browser developers.
103
Philip Jägenstedt3a3d5b82018-05-31 15:25:35104See the [API documentation](https://web-platform-tests.org/writing-tests/testharness-api.html)
foolipeda32ab2017-02-16 19:21:58105for a thorough introduction to `testharness.js`.
106
Kent Tamura59ffb022018-11-27 05:30:56107Web tests should follow the recommendations of the above documentation.
108Furthermore, web tests should include relevant
Philip Jägenstedt3a3d5b82018-05-31 15:25:35109[metadata](https://web-platform-tests.org/writing-tests/css-metadata.html). The
pwnall4ea2eb32016-11-29 02:47:25110specification URL (in `<link rel="help">`) is almost always relevant, and is
111incredibly helpful to a developer who needs to understand the test quickly.
112
113Below is a skeleton for a JavaScript test embedded in an HTML page. Note that,
114in order to follow the minimality guideline, the test omits the tags `<html>`,
115`<head>`, and `<body>`, as they can be inferred by the HTML parser.
116
117```html
118<!doctype html>
pwnall59aadcb2017-01-26 23:27:21119<title>JavaScript: the true literal is immutable and equal to itself</title>
pwnall4ea2eb32016-11-29 02:47:25120<link rel="help" href="https://tc39.github.io/ecma262/#sec-boolean-literals">
pwnall4ea2eb32016-11-29 02:47:25121<script src="/resources/testharness.js"></script>
122<script src="/resources/testharnessreport.js"></script>
123<script>
124'use strict';
125
126// Synchronous test example.
127test(() => {
128 const value = true;
129 assert_true(value, 'true literal');
130 assert_equals(value.toString(), 'true', 'the string representation of true');
131}, 'The literal true in a synchronous test case');
132
133// Asynchronous test example.
134async_test(t => {
135 const originallyTrue = true;
136 setTimeout(t.step_func_done(() => {
137 assert_equals(originallyTrue, true);
138 }), 0);
139}, 'The literal true in a setTimeout callback');
140
141// Promise test example.
142promise_test(() => {
143 return new Promise((resolve, reject) => {
144 resolve(true);
145 }).then(value => {
146 assert_true(value);
147 });
148}, 'The literal true used to resolve a Promise');
149
150</script>
151```
152
153Some points that are not immediately obvious from the example:
154
pwnall4ea2eb32016-11-29 02:47:25155* When calling an `assert_` function that compares two values, the first
156 argument is the actual value (produced by the functionality being tested), and
157 the second argument is the expected value (known good, golden). The order
158 is important, because the testing harness relies on it to generate expressive
159 error messages that are relied upon when debugging test failures.
160* The assertion description (the string argument to `assert_` methods) conveys
161 the way the actual value was obtained.
162 * If the expected value doesn't make it clear, the assertion description
163 should explain the desired behavior.
164 * Test cases with a single assertion should omit the assertion's description
165 when it is sufficiently clear.
166* Each test case describes the circumstance that it tests, without being
167 redundant.
168 * Do not start test case descriptions with redundant terms like "Testing"
169 or "Test for".
ktyliue0bb9882017-01-10 01:47:50170 * Test files with a single test case should omit the test case description.
171 The file's `<title>` should be sufficient to describe the scenario being
172 tested.
pwnall4ea2eb32016-11-29 02:47:25173* Asynchronous tests have a few subtleties.
174 * The `async_test` wrapper calls its function with a test case argument that
175 is used to signal when the test case is done, and to connect assertion
176 failures to the correct test.
177 * `t.done()` must be called after all the test case's assertions have
178 executed.
179 * Test case assertions (actually, any callback code that can throw
180 exceptions) must be wrapped in `t.step_func()` calls, so that
181 assertion failures and exceptions can be traced back to the correct test
182 case.
183 * `t.step_func_done()` is a shortcut that combines `t.step_func()` with a
184 `t.done()` call.
185
186*** promo
Kent Tamura59ffb022018-11-27 05:30:56187Web tests that load from `file://` origins must currently use relative paths
pwnall4ea2eb32016-11-29 02:47:25188to point to
Kent Tamura59ffb022018-11-27 05:30:56189[/resources/testharness.js](../../third_party/blink/web_tests/resources/testharness.js)
pwnall4ea2eb32016-11-29 02:47:25190and
Kent Tamura59ffb022018-11-27 05:30:56191[/resources/testharnessreport.js](../../third_party/blink/web_tests/resources/testharnessreport.js).
pwnall4ea2eb32016-11-29 02:47:25192This is contrary to the WPT guidelines, which call for absolute paths.
Kent Tamura59ffb022018-11-27 05:30:56193This limitation does not apply to the tests in `web_tests/http`, which rely on
194an HTTP server, or to the tests in `web_tests/external/wpt`, which are
Philip Jägenstedt3a3d5b82018-05-31 15:25:35195imported from the [WPT repository](https://github.com/web-platform-tests/wpt).
pwnall4ea2eb32016-11-29 02:47:25196***
197
198### WPT Supplemental Testing APIs
199
200Some tests simply cannot be expressed using the Web Platform APIs. For example,
201some tests that require a user to perform a gesture, such as a mouse click,
202cannot be implemented using Web APIs. The WPT project covers some of these cases
203via supplemental testing APIs.
204
pwnall59aadcb2017-01-26 23:27:21205When writing tests that rely on supplemental testing APIs, please consider the
206cost and benefits of having the tests
Kent Tamura59ffb022018-11-27 05:30:56207[gracefully degrade to manual tests](./web_tests_with_manual_fallback.md) in
pwnall59aadcb2017-01-26 23:27:21208the absence of the testing APIs.
209
pwnall4ea2eb32016-11-29 02:47:25210*** promo
211In many cases, the user gesture is not actually necessary. For example, many
212event handling tests can use
213[synthetic events](https://developer.mozilla.org/docs/Web/Guide/Events/Creating_and_triggering_events).
214***
215
pwnall4ea2eb32016-11-29 02:47:25216### Relying on Blink-Specific Testing APIs
217
218Tests that cannot be expressed using the Web Platform APIs or WPT's testing APIs
219use Blink-specific testing APIs. These APIs are only available in
Kent Tamura59ffb022018-11-27 05:30:56220[content_shell](./web_tests_in_content_shell.md), and should only be used as
pwnall4ea2eb32016-11-29 02:47:25221a last resort.
222
223A downside of Blink-specific APIs is that they are not as well documented as the
224Web Platform features. Learning to use a Blink-specific feature requires finding
225other tests that use it, or reading its source code.
226
227For example, the most popular Blink-specific API is `testRunner`, which is
228implemented in
Wolfgang Beyeraf543d72020-07-28 06:30:46229[content/shell/renderer/web_test/test_runner.h](../../content/shell/renderer/web_test/test_runner.h)
pwnall4ea2eb32016-11-29 02:47:25230and
Wolfgang Beyeraf543d72020-07-28 06:30:46231[content/shell/renderer/web_test/test_runner.cc](../../content/shell/renderer/web_test/test_runner.cc).
pwnall4ea2eb32016-11-29 02:47:25232By skimming the `TestRunnerBindings::Install` method, we learn that the
Xianzhu Wangaf4fa412018-05-14 21:26:52233testRunner API is presented by the `.testRunner` etc. objects. Reading the
pwnall4ea2eb32016-11-29 02:47:25234`TestRunnerBindings::GetObjectTemplateBuilder` method tells us what properties
Xianzhu Wangaf4fa412018-05-14 21:26:52235are available on the `testRunner` object.
pwnall4ea2eb32016-11-29 02:47:25236
Xianzhu Wangaf4fa412018-05-14 21:26:52237Another popular Blink-specific API 'internals' defined in
238[third_party/blink/renderer/core/testing/internals.idl](../../third_party/blink/renderer/core/testing/internals.idl)
239contains more direct access to blink internals.
240
241*** note
242If possible, a test using blink-specific testing APIs should be written not to
243depend on the APIs, so that it can also work directly in a browser. If the test
244does need the APIs to work, it should still check if the API is available before
245using the API. Note that though we omit the `window.` prefix when using the
246APIs, we should use the qualified name in the `if` statement:
247```javascript
248 if (window.testRunner)
249 testRunner.waitUntilDone();
250```
pwnall4ea2eb32016-11-29 02:47:25251***
252
253*** note
254`testRunner` is the most popular testing API because it is also used indirectly
255by tests that stick to Web Platform APIs. The `testharnessreport.js` file in
256`testharness.js` is specifically designated to hold glue code that connects
257`testharness.js` to the testing environment. Our implementation is in
Kent Tamura59ffb022018-11-27 05:30:56258[third_party/blink/web_tests/resources/testharnessreport.js](../../third_party/blink/web_tests/resources/testharnessreport.js),
pwnall4ea2eb32016-11-29 02:47:25259and uses the `testRunner` API.
260***
261
Wolfgang Beyeraf543d72020-07-28 06:30:46262See the [content/shell/renderer/web_test/](../../content/shell/renderer/web_test/) directory and
pwnall4ea2eb32016-11-29 02:47:25263[WebKit's LayoutTests guide](https://trac.webkit.org/wiki/Writing%20Layout%20Tests%20for%20DumpRenderTree)
Xianzhu Wangaf4fa412018-05-14 21:26:52264for other useful APIs. For example, `eventSender`
Wolfgang Beyeraf543d72020-07-28 06:30:46265([content/shell/renderer/web_test/event_sender.h](../../content/shell/renderer/web_test/event_sender.h)
pwnall4ea2eb32016-11-29 02:47:25266and
Wolfgang Beyeraf543d72020-07-28 06:30:46267[content/shell/renderer/web_test/event_sender.cc](../../content/shell/renderer/web_test/event_sender.cc))
pwnall4ea2eb32016-11-29 02:47:25268has methods that simulate events input such as keyboard / mouse input and
269drag-and-drop.
270
271Here is a UML diagram of how the `testRunner` bindings fit into Chromium.
272
273[![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:15274
pwnall4ea2eb32016-11-29 02:47:25275### Text Test Baselines
276
277By default, all the test cases in a file that uses `testharness.js` are expected
278to pass. However, in some cases, we prefer to add failing test cases to the
279repository, so that we can be notified when the failure modes change (e.g., we
280want to know if a test starts crashing rather than returning incorrect output).
281In these situations, a test file will be accompanied by a baseline, which is an
282`-expected.txt` file that contains the test's expected output.
283
284The baselines are generated automatically when appropriate by
Kent Tamura59ffb022018-11-27 05:30:56285`run_web_tests.py`, which is described [here](./web_tests.md), and by the
286[rebaselining tools](./web_test_expectations.md).
pwnall4ea2eb32016-11-29 02:47:25287
288Text baselines for `testharness.js` should be avoided, as having a text baseline
289associated with a `testharness.js` indicates the presence of a bug. For this
290reason, CLs that add text baselines must include a
291[crbug.com](https://crbug.com) link for an issue tracking the removal of the
292text expectations.
293
294* When creating tests that will be upstreamed to WPT, and Blink's current
295 behavior does not match the specification that is being tested, a text
296 baseline is necessary. Remember to create an issue tracking the expectation's
297 removal, and to link the issue in the CL description.
Kent Tamura59ffb022018-11-27 05:30:56298* Web tests that cannot be upstreamed to WPT should use JavaScript to
pwnall4ea2eb32016-11-29 02:47:25299 document Blink's current behavior, rather than using JavaScript to document
300 desired behavior and a text file to document current behavior.
301
302### The js-test.js Legacy Harness
303
304*** promo
305For historical reasons, older tests are written using the `js-test` harness.
306This harness is **deprecated**, and should not be used for new tests.
307***
308
309If you need to understand old tests, the best `js-test` documentation is its
310implementation at
Kent Tamura59ffb022018-11-27 05:30:56311[third_party/blink/web_tests/resources/js-test.js](../../third_party/blink/web_tests/resources/js-test.js).
pwnall4ea2eb32016-11-29 02:47:25312
313`js-test` tests lean heavily on the Blink-specific `testRunner` testing API.
314In a nutshell, the tests call `testRunner.dumpAsText()` to signal that the page
315content should be dumped and compared against a text baseline (an
316`-expected.txt` file). As a consequence, `js-test` tests are always accompanied
317by text baselines. Asynchronous tests also use `testRunner.waitUntilDone()` and
318`testRunner.notifyDone()` to tell the testing tools when they are complete.
319
320### Tests that use an HTTP Server
321
322By default, tests are loaded as if via `file:` URLs. Some web platform features
323require tests served via HTTP or HTTPS, for example absolute paths (`src=/foo`)
324or features restricted to secure protocols.
325
Kent Tamura59ffb022018-11-27 05:30:56326HTTP tests are those under `web_tests/http/tests` (or virtual variants). Use a
pwnall4ea2eb32016-11-29 02:47:25327locally running HTTP server (Apache) to run them. Tests are served off of ports
3288000 and 8080 for HTTP, and 8443 for HTTPS. If you run the tests using
Kent Tamuraa045a7f2018-04-25 05:08:11329`run_web_tests.py`, the server will be started automatically. To run the server
pwnall4ea2eb32016-11-29 02:47:25330manually to reproduce or debug a failure:
331
332```bash
Kent Tamurae81dbff2018-04-20 17:35:34333cd src/third_party/blink/tools
334./run_blink_httpd.py
pwnall4ea2eb32016-11-29 02:47:25335```
336
Kent Tamura59ffb022018-11-27 05:30:56337The web tests will be served from `http://127.0.0.1:8000`. For example, to
pwnall4ea2eb32016-11-29 02:47:25338run the test `http/tests/serviceworker/chromium/service-worker-allowed.html`,
339navigate to
340`http://127.0.0.1:8000/serviceworker/chromium/service-worker-allowed.html`. Some
341tests will behave differently if you go to 127.0.0.1 instead of localhost, so
342use 127.0.0.1.
343
Kent Tamurae81dbff2018-04-20 17:35:34344To kill the server, hit any key on the terminal where `run_blink_httpd.py` is
Hajime Hoshia6fad022017-08-01 17:57:58345running, or just use `taskkill` or the Task Manager on Windows, and `killall` or
346Activity Monitor on MacOS.
pwnall4ea2eb32016-11-29 02:47:25347
Kent Tamura59ffb022018-11-27 05:30:56348The test server sets up an alias to the `web_tests/resources` directory. In
pwnall4ea2eb32016-11-29 02:47:25349HTTP tests, you can access the testing framework at e.g.
pwnalle7819482016-12-17 00:58:40350`src="/resources/testharness.js"`.
pwnall4ea2eb32016-11-29 02:47:25351
352TODO: Document [wptserve](http://wptserve.readthedocs.io/) when we are in a
Kent Tamura59ffb022018-11-27 05:30:56353position to use it to run web tests.
pwnall4ea2eb32016-11-29 02:47:25354
355## Reference Tests (Reftests)
356
357Reference tests, also known as reftests, perform a pixel-by-pixel comparison
358between the rendered image of a test page and the rendered image of a reference
359page. Most reference tests pass if the two images match, but there are cases
360where it is useful to have a test pass when the two images do _not_ match.
361
362Reference tests are more difficult to debug than JavaScript tests, and tend to
363be slower as well. Therefore, they should only be used for functionality that
364cannot be covered by JavaScript tests.
365
366New reference tests should follow the
Philip Jägenstedt3a3d5b82018-05-31 15:25:35367[WPT reftests guidelines](https://web-platform-tests.org/writing-tests/reftests.html).
foolipeda32ab2017-02-16 19:21:58368The most important points are summarized below.
pwnall4ea2eb32016-11-29 02:47:25369
pwnall6acacd82016-12-02 01:40:15370* &#x1F6A7; The test page declares the reference page using a
371 `<link rel="match">` or `<link rel="mismatch">`, depending on whether the test
372 passes when the test image matches or does not match the reference image.
pwnall4ea2eb32016-11-29 02:47:25373* The reference page must not use the feature being tested. Otherwise, the test
374 is meaningless.
375* The reference page should be as simple as possible, and should not depend on
376 advanced features. Ideally, the reference page should render as intended even
377 on browsers with poor CSS support.
378* Reference tests should be self-describing.
379* Reference tests do _not_ include `testharness.js`.
380
pwnall6acacd82016-12-02 01:40:15381&#x1F6A7; Our testing infrastructure was designed for the
pwnall4ea2eb32016-11-29 02:47:25382[WebKit reftests](https://trac.webkit.org/wiki/Writing%20Reftests) that Blink
383has inherited. The consequences are summarized below.
384
385* Each reference page must be in the same directory as its associated test.
386 Given a test page named `foo` (e.g. `foo.html` or `foo.svg`),
387 * The reference page must be named `foo-expected` (e.g.,
388 `foo-expected.html`) if the test passes when the two images match.
389 * The reference page must be named `foo-expected-mismatch` (e.g.,
390 `foo-expected-mismatch.svg`) if the test passes when the two images do
391 _not_ match.
392* Multiple references and chained references are not supported.
393
394The following example demonstrates a reference test for
395[`<ol>`'s reversed attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ol).
396The example assumes that the test page is named `ol-reversed.html`.
397
398```html
399<!doctype html>
pwnall4ea2eb32016-11-29 02:47:25400<link rel="match" href="ol-reversed-expected.html">
401
402<ol reversed>
403 <li>A</li>
404 <li>B</li>
405 <li>C</li>
406</ol>
407```
408
409The reference page, which must be named `ol-reversed-expected.html`, is below.
410
411```html
412<!doctype html>
pwnall4ea2eb32016-11-29 02:47:25413
414<ol>
415 <li value="3">A</li>
416 <li value="2">B</li>
417 <li value="1">C</li>
418</ol>
419```
420
pwnall6acacd82016-12-02 01:40:15421*** promo
422The method for pointing out a test's reference page is still in flux, and is
423being discussed on
424[blink-dev](https://groups.google.com/a/chromium.org/d/topic/blink-dev/XsR6PKRrS1E/discussion).
425***
426
pwnall4ea2eb32016-11-29 02:47:25427## Pixel Tests
428
Xianzhu Wang6e84f0992018-10-23 23:07:55429A test creates an image result by default unless some `testRunner` API is
430called (e.g. `testRunner.dumpAsText()`, `testRunner.dumpAsLayout()`, see
431[text tests](#text-tests)) to suppress the image result. A test is a
432**pixel test** if it creates an image result but is not a reference test.
433The image result is compared against an image baseline, which is an
434`-expected.png` file associated with the test, and the test passes if the
pwnall4ea2eb32016-11-29 02:47:25435image result is identical to the baseline, according to a pixel-by-pixel
Xianzhu Wang6e84f0992018-10-23 23:07:55436comparison.
pwnall4ea2eb32016-11-29 02:47:25437
438Pixel tests should still follow the principles laid out above. Pixel tests pose
439unique challenges to the desire to have *self-describing* and *cross-platform*
440tests. The
Philip Jägenstedt3a3d5b82018-05-31 15:25:35441[WPT rendering test guidelines](https://web-platform-tests.org/writing-tests/rendering.html)
pwnall4ea2eb32016-11-29 02:47:25442contain useful guidance. The most relevant pieces of advice are below.
443
444* Whenever possible, use a green paragraph / page / square to indicate success.
445 If that is not possible, make the test self-describing by including a textual
446 description of the desired (passing) outcome.
447* Only use the red color or the word `FAIL` to highlight errors. This does not
448 apply when testing the color red.
pwnall6acacd82016-12-02 01:40:15449* &#x1F6A7; Use the
450 [Ahem font](https://www.w3.org/Style/CSS/Test/Fonts/Ahem/README) to reduce the
451 variance introduced by the platform's text rendering system. This does not
452 apply when testing text, text flow, font selection, font fallback, font
453 features, or other typographic information.
pwnall4ea2eb32016-11-29 02:47:25454
455*** promo
Xianzhu Wang6e84f0992018-10-23 23:07:55456The default size of the image result of a pixel test is 800x600px, because test
457pages are rendered in an 800x600px viewport by default. Normally pixel tests
458that do not specifically cover scrolling should fit in an 800x600px viewport
459without creating scrollbars.
pwnall4ea2eb32016-11-29 02:47:25460***
461
pwnall6acacd82016-12-02 01:40:15462*** promo
463The recommendation of using Ahem in pixel tests is being discussed on
464[blink-dev](https://groups.google.com/a/chromium.org/d/topic/blink-dev/XsR6PKRrS1E/discussion).
465***
466
Kent Tamura59ffb022018-11-27 05:30:56467The following snippet includes the Ahem font in a web test.
pwnall4ea2eb32016-11-29 02:47:25468
469```html
470<style>
471body {
472 font: 10px Ahem;
473}
474</style>
475<script src="/resources/ahem.js"></script>
476```
477
478*** promo
Kent Tamura59ffb022018-11-27 05:30:56479Tests outside `web_tests/http` and `web_tests/external/wpt` currently need
pwnall4ea2eb32016-11-29 02:47:25480to use a relative path to
Kent Tamura59ffb022018-11-27 05:30:56481[/third_party/blink/web_tests/resources/ahem.js](../../third_party/blink/web_tests/resources/ahem.js)
pwnall4ea2eb32016-11-29 02:47:25482***
483
484### Tests that need to paint, raster, or draw a frame of intermediate output
485
Kent Tamura59ffb022018-11-27 05:30:56486A web test does not actually draw frames of output until the test exits.
Xianzhu Wangaf4fa412018-05-14 21:26:52487Tests that need to generate a painted frame can use `runAfterLayoutAndPaint()`
Kent Tamura59ffb022018-11-27 05:30:56488defined 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:52489which will run the machinery to put up a frame, then call the passed callback.
490There is also a library at
Kent Tamura59ffb022018-11-27 05:30:56491[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:52492to help with writing paint invalidation and repaint tests.
pwnall4ea2eb32016-11-29 02:47:25493
Mason Freedb0855622018-10-02 17:49:47494### Tests for scrolling animations
495
Kent Tamura59ffb022018-11-27 05:30:56496Some web tests need to ensure animations such as middle-click auto-scroll,
Mason Freedb0855622018-10-02 17:49:47497fling, etc. get performed properly. When testing in display compositor pixel
498dump mode (now the standard), the standard behavior for tests is to
499synchronously composite without rastering (to save time). However, animations
500run upon surface activation, which only happens once rasterization is performed.
501Therefore, for these tests, an additional setting needs to be set. Near the
Mason Freedc41b2d082018-10-26 17:20:21502beginning of these tests, call `setAnimationRequiresRaster()` defined in
Kent Tamura59ffb022018-11-27 05:30:56503[third_party/blink/web_tests/resources/compositor-controls.js](../../third_party/blink/web_tests/resources/compositor-controls.js)
Mason Freedb0855622018-10-02 17:49:47504which will enable full rasterization during the test.
505
Xianzhu Wang6e84f0992018-10-23 23:07:55506## Text tests
pwnall4ea2eb32016-11-29 02:47:25507
Xianzhu Wang6e84f0992018-10-23 23:07:55508A **text test** outputs text result. The result is compared against a text
509baseline which is an `-expected.txt` file associated with the test, and the
510test passes if the text result is identical to the baseline. A test isn't a
511text test by default until it calls some `testRunner` API to instruct the
512test runner to output text. A text test can be categorized based on what kind of
513information that the text result represents.
pwnall4ea2eb32016-11-29 02:47:25514
Xianzhu Wang6e84f0992018-10-23 23:07:55515### Layout tree test
pwnall4ea2eb32016-11-29 02:47:25516
Xianzhu Wang6e84f0992018-10-23 23:07:55517If a test calls `testRunner.dumpAsLayout()` or
518`testRunner.dumpAsLayoutWithPixelResults()`, The text result will be a
519textual representation of Blink's
520[layout tree](https://developers.google.com/web/fundamentals/performance/critical-rendering-path/render-tree-construction)
521(called the render tree on that page) of the main frame of the test page.
522With `testRunner.dumpChildFrames()` the text result will also include layout
523tree of child frames.
pwnall4ea2eb32016-11-29 02:47:25524
Xianzhu Wang6e84f0992018-10-23 23:07:55525Like pixel tests, the output of layout tree tests may depend on
526platform-specific details, so layout tree tests often require per-platform
527baselines. Furthermore, since the tests obviously depend on the layout tree
528structure, that means that if we change the layout tree you have to rebaseline
529each layout tree test to see if the results are still correct and whether the
530test is still meaningful. There are actually many cases where the layout tree
dpranked2b7d642017-01-15 04:00:24531output is misstated (i.e., wrong), because people didn't want to have to update
532existing baselines and tests. This is really unfortunate and confusing.
533
534For these reasons, layout tree tests should **only** be used to cover aspects
535of the layout code that can only be tested by looking at the layout tree. Any
536combination of the other test types is preferable to a layout tree test.
537Layout tree tests are
pwnall4ea2eb32016-11-29 02:47:25538[inherited from WebKit](https://webkit.org/blog/1456/layout-tests-practice/), so
dpranked2b7d642017-01-15 04:00:24539the repository may have some unfortunate examples of layout tree tests.
pwnall4ea2eb32016-11-29 02:47:25540
dpranked2b7d642017-01-15 04:00:24541The following page is an example of a layout tree test.
pwnall4ea2eb32016-11-29 02:47:25542
543```html
544<!doctype html>
pwnall4ea2eb32016-11-29 02:47:25545<style>
546body { font: 10px Ahem; }
547span::after {
548 content: "pass";
549 color: green;
550}
551</style>
552<script src="/resources/ahem.js"></script>
Xianzhu Wang6e84f0992018-10-23 23:07:55553<script>
554 if (window.testRunner)
555 testRunner.dumpAsLayout();
556</script>
pwnall4ea2eb32016-11-29 02:47:25557<p><span>Pass if a green PASS appears to the right: </span></p>
558```
559
pwnall4ea2eb32016-11-29 02:47:25560The test page produces the text result below.
561
562```
563layer at (0,0) size 800x600
564 LayoutView at (0,0) size 800x600
565layer at (0,0) size 800x30
566 LayoutBlockFlow {HTML} at (0,0) size 800x30
567 LayoutBlockFlow {BODY} at (8,10) size 784x10
568 LayoutBlockFlow {P} at (0,0) size 784x10
569 LayoutInline {SPAN} at (0,0) size 470x10
570 LayoutText {#text} at (0,0) size 430x10
571 text run at (0,0) width 430: "Pass if a green PASS appears to the right: "
572 LayoutInline {<pseudo:after>} at (0,0) size 40x10 [color=#008000]
573 LayoutTextFragment (anonymous) at (430,0) size 40x10
574 text run at (430,0) width 40: "pass"
575```
576
577Notice that the test result above depends on the size of the `<p>` text. The
578test page uses the Ahem font (introduced above), whose main design goal is
579consistent cross-platform rendering. Had the test used another font, its text
580baseline would have depended on the fonts installed on the testing computer, and
581on the platform's font rendering system. Please follow the pixel tests
dpranked2b7d642017-01-15 04:00:24582guidelines and write reliable layout tree tests!
pwnall4ea2eb32016-11-29 02:47:25583
dpranked2b7d642017-01-15 04:00:24584WebKit's layout tree is described in
pwnall4ea2eb32016-11-29 02:47:25585[a series of posts](https://webkit.org/blog/114/webcore-rendering-i-the-basics/)
dpranked2b7d642017-01-15 04:00:24586on WebKit's blog. Some of the concepts there still apply to Blink's layout tree.
pwnall4ea2eb32016-11-29 02:47:25587
Xianzhu Wang6e84f0992018-10-23 23:07:55588### Text dump test
589
590If `testRunner.dumpAsText()` or `testRunner.dumpAsTextWithPixelResults()`
591is called from a test, the test will dump the text contents of the main frame
592of the tested page. With `testRunner.dumpChildFrames()` the text
593result will also include text contents of child frames. Actually a JavaScript
594test is a special kind of text dump test which can often omit the text baseline.
595
596A test can override the default text dump by calling
597`testRunner.setCustomTextOutput(string)`. The string parameter can be any
598text that the test wants to output. The [`internals` API](../../third_party/blink/renderer/core/testing/internals.idl]
599provides methods to get textual representations of internal data structures that
600can be used as the parameter of `testRunner.setCustomTextOutput()`.
601
602### Markup dump test
603
604If a test calls `testRunner.dumpAsMarkup()`, the text result will be the DOM
605of the main frame of the test. With `testRunner.dumpChildFrames()` the text
606result will also include DOM of child frames.
607
608## Audio tests
609
610If a test calls `testRunner.setAudioData(array_buffer)`, the test will
611create an audio result. The result will be compared against an audio baseline
612which is an `-expected.wav` file associated with the test, and the test passes
613if the audio result is identical to the baseline.
614
615## Tests that are both pixel/reference tests and text tests
616
617If a test calls `testRunner.dumpAsTextWithPixelResults()` or
618`testRunner.dumpAsLayoutWithPixelResults()`, the test is both a
619pixel/reference test and a text test. It will output both pixel result and text
620result.
621
Xianzhu Wang4bbcebe2018-10-31 02:21:46622For a test that is both a pixel/reference test and a text test, both pixel and
623text results will be compared to baselines, and the test passes if each result
624matches the corresponding baseline.
Xianzhu Wang6e84f0992018-10-23 23:07:55625
Kent Tamura59ffb022018-11-27 05:30:56626Many of the [paint invalidation tests](../../third_party/blink/web_tests/paint/invalidation)
Xianzhu Wang6e84f0992018-10-23 23:07:55627are of this type. The pixel results (compared against `-expected.png` or
628`-expected.html`) ensure correct rendering, and the text results (compared
629against `-expected.txt`) ensure correct compositing and raster invalidation
630(without unexpected over and under invalidations).
631
632For a layout tree test, whether you want a pixel test and/or a text test depends
633on whether you care about the visual image, the details of how that image was
634constructed, or both. It is possible for multiple layout trees to produce
635the same pixel output, so it is important to make it clear in the test
636which outputs you really care about.
637
pwnall4ea2eb32016-11-29 02:47:25638## Directory Structure
639
Kent Tamura59ffb022018-11-27 05:30:56640The [web_tests directory](../../third_party/blink/web_tests) currently
pwnall4ea2eb32016-11-29 02:47:25641lacks a strict, formal structure. The following directories have special
642meaning:
643
644* The `http/` directory hosts tests that require an HTTP server (see above).
645* The `resources/` subdirectory in every directory contains binary files, such
646 as media files, and code that is shared by multiple test files.
647
648*** note
Kent Tamura59ffb022018-11-27 05:30:56649Some web tests consist of a minimal HTML page that references a JavaScript
pwnall4ea2eb32016-11-29 02:47:25650file in `resources/`. Please do not use this pattern for new tests, as it goes
651against the minimality principle. JavaScript and CSS files should only live in
652`resources/` if they are shared by at least two test files.
653***