Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 1 | # Writing Web Tests |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 2 | |
| 3 | [TOC] |
| 4 | |
| 5 | ## Overview |
| 6 | |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 7 | Web tests should be used to accomplish one of the following goals: |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 8 | |
| 9 | 1. The entire surface of Blink that is exposed to the Web should be covered by |
foolip | eda32ab | 2017-02-16 19:21:58 | [diff] [blame] | 10 | 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. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 16 | 2. When a Blink feature cannot be tested using the tools provided by WPT, and |
| 17 | cannot be easily covered by |
Kent Tamura | 6943cf79 | 2018-04-09 05:24:54 | [diff] [blame] | 18 | [C++ unit tests](https://cs.chromium.org/chromium/src/third_party/blink/renderer/web/tests/?q=webframetest&sq=package:chromium&type=cs), |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 19 | the feature must be covered by web tests, to avoid unexpected regressions. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 20 | These tests will use Blink-specific testing APIs that are only available in |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 21 | [content_shell](./web_tests_in_content_shell.md). |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 22 | |
Yoshisato Yanagisawa | 638e2ee0 | 2021-12-09 05:52:08 | [diff] [blame] | 23 | Note: 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 Xia | c851d2f6 | 2022-03-22 15:56:40 | [diff] [blame] | 25 | cover WPT specific features/behaviors. **The WPT is recommended today rather than |
| 26 | test types mentioned below!** |
Yoshisato Yanagisawa | 638e2ee0 | 2021-12-09 05:52:08 | [diff] [blame] | 27 | |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 28 | *** promo |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 29 | If you know that Blink web tests are upstreamed to other projects, such as |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 30 | [test262](https://github.com/tc39/test262), please update this document. Most |
| 31 | importantly, our guidelines should to make it easy for our tests to be |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 32 | upstreamed. The |
| 33 | [blink-dev mailing list](https://groups.google.com/a/chromium.org/forum/#!forum/blink-dev) |
| 34 | will be happy to help you harmonize our current guidelines with communal test |
| 35 | repositories. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 36 | *** |
| 37 | |
| 38 | ### Test Types |
| 39 | |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 40 | There are four broad types of web tests, listed in the order of preference. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 41 | |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 42 | * *JavaScript Tests* are the web test implementation of |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 43 | [xUnit tests](https://en.wikipedia.org/wiki/XUnit). These tests contain |
| 44 | assertions written in JavaScript, and pass if the assertions evaluate to |
| 45 | true. |
| 46 | * *Reference Tests* render a test page and a reference page, and pass if the two |
| 47 | renderings are identical, according to a pixel-by-pixel comparison. These |
| 48 | tests are less robust, harder to debug, and significantly slower than |
| 49 | JavaScript tests, and are only used when JavaScript tests are insufficient, |
| 50 | such as when testing paint code. |
| 51 | * *Pixel Tests* render a test page and compare the result against a pre-rendered |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 52 | 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 |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 54 | many factors such as the host computer's graphics card and driver, the |
| 55 | platform's text rendering system, and various user-configurable operating |
| 56 | system settings. For this reason, it is common for a pixel test to have a |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 57 | different reference image for each platform that Blink is tested on, and |
| 58 | the reference images are |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 59 | [quite cumbersome to manage](./web_test_expectations.md). You |
Xianzhu Wang | 6e84f099 | 2018-10-23 23:07:55 | [diff] [blame] | 60 | 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 |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 67 | options. |
Xianzhu Wang | 6e84f099 | 2018-10-23 23:07:55 | [diff] [blame] | 68 | * *Audio tests* output audio results. |
| 69 | |
| 70 | *** aside |
| 71 | A JavaScript test is actually a special kind of text test, but its text |
| 72 | baseline can be often omitted. |
| 73 | *** |
| 74 | |
| 75 | *** aside |
| 76 | A test can be a reference/pixel test and a text test at the same time. |
| 77 | *** |
pwnall | 59aadcb | 2017-01-26 23:27:21 | [diff] [blame] | 78 | |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 79 | ## General Principles |
| 80 | |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 81 | Tests should be written under the assumption that they will be upstreamed |
pwnall | 59aadcb | 2017-01-26 23:27:21 | [diff] [blame] | 82 | to the WPT project. To this end, tests should follow the |
Philip Jägenstedt | 3a3d5b8 | 2018-05-31 15:25:35 | [diff] [blame] | 83 | [WPT guidelines](https://web-platform-tests.org/writing-tests/). |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 84 | |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 85 | |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 86 | There is no style guide that applies to all web tests. However, some projects |
pwnall | 59aadcb | 2017-01-26 23:27:21 | [diff] [blame] | 87 | have adopted style guides, such as the |
| 88 | [ServiceWorker Tests Style guide](https://www.chromium.org/blink/serviceworker/testing). |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 89 | |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 90 | Our [document on web tests tips](./web_tests_tips.md) summarizes the most |
pwnall | 59aadcb | 2017-01-26 23:27:21 | [diff] [blame] | 91 | important WPT guidelines and highlights some JavaScript concepts that are worth |
| 92 | paying attention to when trying to infer style rules from existing tests. If |
| 93 | you're unopinionated and looking for a style guide to follow, the document also |
| 94 | suggests some defaults. |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 95 | |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 96 | ## JavaScript Tests |
| 97 | |
| 98 | Whenever possible, the testing criteria should be expressed in JavaScript. The |
| 99 | alternatives, which will be described in future sections, result in slower and |
| 100 | less reliable tests. |
| 101 | |
| 102 | All new JavaScript tests should be written using the |
Philip Jägenstedt | 3a3d5b8 | 2018-05-31 15:25:35 | [diff] [blame] | 103 | [testharness.js](https://github.com/web-platform-tests/wpt/tree/master/resources) |
mek | b330e31 | 2017-05-03 19:56:22 | [diff] [blame] | 104 | testing framework. This framework is used by the tests in the |
Philip Jägenstedt | 3a3d5b8 | 2018-05-31 15:25:35 | [diff] [blame] | 105 | [web-platform-tests](https://github.com/web-platform-tests/wpt) repository, |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 106 | which is shared with all the other browser vendors, so `testharness.js` tests |
| 107 | are more accessible to browser developers. |
| 108 | |
Philip Jägenstedt | 3a3d5b8 | 2018-05-31 15:25:35 | [diff] [blame] | 109 | See the [API documentation](https://web-platform-tests.org/writing-tests/testharness-api.html) |
foolip | eda32ab | 2017-02-16 19:21:58 | [diff] [blame] | 110 | for a thorough introduction to `testharness.js`. |
| 111 | |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 112 | Web tests should follow the recommendations of the above documentation. |
| 113 | Furthermore, web tests should include relevant |
Philip Jägenstedt | 3a3d5b8 | 2018-05-31 15:25:35 | [diff] [blame] | 114 | [metadata](https://web-platform-tests.org/writing-tests/css-metadata.html). The |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 115 | specification URL (in `<link rel="help">`) is almost always relevant, and is |
| 116 | incredibly helpful to a developer who needs to understand the test quickly. |
| 117 | |
| 118 | Below is a skeleton for a JavaScript test embedded in an HTML page. Note that, |
| 119 | in 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> |
pwnall | 59aadcb | 2017-01-26 23:27:21 | [diff] [blame] | 124 | <title>JavaScript: the true literal is immutable and equal to itself</title> |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 125 | <link rel="help" href="https://tc39.github.io/ecma262/#sec-boolean-literals"> |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 126 | <script src="/resources/testharness.js"></script> |
| 127 | <script src="/resources/testharnessreport.js"></script> |
| 128 | <script> |
| 129 | 'use strict'; |
| 130 | |
| 131 | // Synchronous test example. |
| 132 | test(() => { |
| 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. |
| 139 | async_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. |
| 147 | promise_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 | |
| 158 | Some points that are not immediately obvious from the example: |
| 159 | |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 160 | * 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". |
ktyliu | e0bb988 | 2017-01-10 01:47:50 | [diff] [blame] | 175 | * 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. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 178 | * 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 Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 192 | Web tests that load from `file://` origins must currently use relative paths |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 193 | to point to |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 194 | [/resources/testharness.js](../../third_party/blink/web_tests/resources/testharness.js) |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 195 | and |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 196 | [/resources/testharnessreport.js](../../third_party/blink/web_tests/resources/testharnessreport.js). |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 197 | This is contrary to the WPT guidelines, which call for absolute paths. |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 198 | This limitation does not apply to the tests in `web_tests/http`, which rely on |
| 199 | an HTTP server, or to the tests in `web_tests/external/wpt`, which are |
Philip Jägenstedt | 3a3d5b8 | 2018-05-31 15:25:35 | [diff] [blame] | 200 | imported from the [WPT repository](https://github.com/web-platform-tests/wpt). |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 201 | *** |
| 202 | |
| 203 | ### WPT Supplemental Testing APIs |
| 204 | |
| 205 | Some tests simply cannot be expressed using the Web Platform APIs. For example, |
| 206 | some tests that require a user to perform a gesture, such as a mouse click, |
| 207 | cannot be implemented using Web APIs. The WPT project covers some of these cases |
| 208 | via supplemental testing APIs. |
| 209 | |
pwnall | 59aadcb | 2017-01-26 23:27:21 | [diff] [blame] | 210 | When writing tests that rely on supplemental testing APIs, please consider the |
| 211 | cost and benefits of having the tests |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 212 | [gracefully degrade to manual tests](./web_tests_with_manual_fallback.md) in |
pwnall | 59aadcb | 2017-01-26 23:27:21 | [diff] [blame] | 213 | the absence of the testing APIs. |
| 214 | |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 215 | *** promo |
| 216 | In many cases, the user gesture is not actually necessary. For example, many |
| 217 | event handling tests can use |
| 218 | [synthetic events](https://developer.mozilla.org/docs/Web/Guide/Events/Creating_and_triggering_events). |
| 219 | *** |
| 220 | |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 221 | ### Relying on Blink-Specific Testing APIs |
| 222 | |
| 223 | Tests that cannot be expressed using the Web Platform APIs or WPT's testing APIs |
| 224 | use Blink-specific testing APIs. These APIs are only available in |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 225 | [content_shell](./web_tests_in_content_shell.md), and should only be used as |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 226 | a last resort. |
| 227 | |
| 228 | A downside of Blink-specific APIs is that they are not as well documented as the |
| 229 | Web Platform features. Learning to use a Blink-specific feature requires finding |
| 230 | other tests that use it, or reading its source code. |
| 231 | |
| 232 | For example, the most popular Blink-specific API is `testRunner`, which is |
| 233 | implemented in |
keno | e339a58 | 2022-12-22 10:36:20 | [diff] [blame] | 234 | [content/web_test/renderer/test_runner.h](../../content/web_test/renderer/test_runner.h) |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 235 | and |
keno | e339a58 | 2022-12-22 10:36:20 | [diff] [blame] | 236 | [content/web_test/renderer/test_runner.cc](../../content/web_test/renderer/test_runner.cc). |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 237 | By skimming the `TestRunnerBindings::Install` method, we learn that the |
Xianzhu Wang | af4fa41 | 2018-05-14 21:26:52 | [diff] [blame] | 238 | testRunner API is presented by the `.testRunner` etc. objects. Reading the |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 239 | `TestRunnerBindings::GetObjectTemplateBuilder` method tells us what properties |
Xianzhu Wang | af4fa41 | 2018-05-14 21:26:52 | [diff] [blame] | 240 | are available on the `testRunner` object. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 241 | |
Xianzhu Wang | af4fa41 | 2018-05-14 21:26:52 | [diff] [blame] | 242 | Another popular Blink-specific API 'internals' defined in |
| 243 | [third_party/blink/renderer/core/testing/internals.idl](../../third_party/blink/renderer/core/testing/internals.idl) |
| 244 | contains more direct access to blink internals. |
| 245 | |
| 246 | *** note |
| 247 | If possible, a test using blink-specific testing APIs should be written not to |
| 248 | depend on the APIs, so that it can also work directly in a browser. If the test |
| 249 | does need the APIs to work, it should still check if the API is available before |
| 250 | using the API. Note that though we omit the `window.` prefix when using the |
| 251 | APIs, we should use the qualified name in the `if` statement: |
| 252 | ```javascript |
| 253 | if (window.testRunner) |
| 254 | testRunner.waitUntilDone(); |
| 255 | ``` |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 256 | *** |
| 257 | |
| 258 | *** note |
| 259 | `testRunner` is the most popular testing API because it is also used indirectly |
| 260 | by 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 Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 263 | [third_party/blink/web_tests/resources/testharnessreport.js](../../third_party/blink/web_tests/resources/testharnessreport.js), |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 264 | and uses the `testRunner` API. |
| 265 | *** |
| 266 | |
Jonathan Lee | 80280d2 | 2023-11-27 22:40:56 | [diff] [blame] | 267 | See the [content/web_test/renderer/](../../content/web_test/renderer/) directory and |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 268 | [WebKit's LayoutTests guide](https://trac.webkit.org/wiki/Writing%20Layout%20Tests%20for%20DumpRenderTree) |
Xianzhu Wang | af4fa41 | 2018-05-14 21:26:52 | [diff] [blame] | 269 | for other useful APIs. For example, `eventSender` |
Jonathan Lee | 80280d2 | 2023-11-27 22:40:56 | [diff] [blame] | 270 | ([content/shell/renderer/web_test/event_sender.h](../../content/web_test/renderer/event_sender.h) |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 271 | and |
Jonathan Lee | 80280d2 | 2023-11-27 22:40:56 | [diff] [blame] | 272 | [content/shell/renderer/web_test/event_sender.cc](../../content/web_test/renderer/event_sender.cc)) |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 273 | has methods that simulate events input such as keyboard / mouse input and |
| 274 | drag-and-drop. |
| 275 | |
| 276 | Here is a UML diagram of how the `testRunner` bindings fit into Chromium. |
| 277 | |
| 278 | [](https://docs.google.com/drawings/d/1KNRNjlxK0Q3Tp8rKxuuM5mpWf4OJQZmvm9_kpwu_Wwg/edit) |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 279 | |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 280 | ### Text Test Baselines |
| 281 | |
| 282 | By default, all the test cases in a file that uses `testharness.js` are expected |
| 283 | to pass. However, in some cases, we prefer to add failing test cases to the |
| 284 | repository, so that we can be notified when the failure modes change (e.g., we |
| 285 | want to know if a test starts crashing rather than returning incorrect output). |
| 286 | In 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 | |
| 289 | The baselines are generated automatically when appropriate by |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 290 | `run_web_tests.py`, which is described [here](./web_tests.md), and by the |
| 291 | [rebaselining tools](./web_test_expectations.md). |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 292 | |
| 293 | Text baselines for `testharness.js` should be avoided, as having a text baseline |
Jonathan Lee | dfcfcf4d | 2023-03-31 01:10:30 | [diff] [blame] | 294 | associated with a `testharness.js` test usually indicates the presence of a bug. |
| 295 | For this reason, CLs that add text baselines must include a |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 296 | [crbug.com](https://crbug.com) link for an issue tracking the removal of the |
| 297 | text 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 Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 303 | * Web tests that cannot be upstreamed to WPT should use JavaScript to |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 304 | document Blink's current behavior, rather than using JavaScript to document |
| 305 | desired behavior and a text file to document current behavior. |
| 306 | |
Jonathan Lee | dfcfcf4d | 2023-03-31 01:10:30 | [diff] [blame] | 307 | *** promo |
| 308 | Because of [baseline fallback](./web_test_baseline_fallback.md), it may not be |
| 309 | possible to [represent a platform-specific all-`PASS` |
| 310 | status](https://crbug.com/1324638) by the platform baseline's absence. In such |
Kaylee Lubick | f7bfd2f | 2025-03-10 20:30:37 | [diff] [blame] | 311 | rare cases, `blink_tool.py rebaseline-cl` will generate a placeholder baseline |
Jonathan Lee | dfcfcf4d | 2023-03-31 01:10:30 | [diff] [blame] | 312 | indicating to `run_web_tests.py` that all subtests are meant to pass: |
| 313 | |
| 314 | ``` |
| 315 | This is a testharness.js-based test. |
| 316 | All subtests passed and are omitted for brevity. |
| 317 | See https://chromium.googlesource.com/chromium/src/+/HEAD/docs/testing/writing_web_tests.md#Text-Test-Baselines for details. |
| 318 | Harness: the test ran to completion. |
| 319 | ``` |
| 320 | |
Kaylee Lubick | f7bfd2f | 2025-03-10 20:30:37 | [diff] [blame] | 321 | `blink_tool.py optimize-baselines` will automatically remove these placeholder |
Jonathan Lee | dfcfcf4d | 2023-03-31 01:10:30 | [diff] [blame] | 322 | baselines once all platforms are all-`PASS`. |
| 323 | *** |
| 324 | |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 325 | ### The js-test.js Legacy Harness |
| 326 | |
| 327 | *** promo |
| 328 | For historical reasons, older tests are written using the `js-test` harness. |
| 329 | This harness is **deprecated**, and should not be used for new tests. |
| 330 | *** |
| 331 | |
| 332 | If you need to understand old tests, the best `js-test` documentation is its |
| 333 | implementation at |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 334 | [third_party/blink/web_tests/resources/js-test.js](../../third_party/blink/web_tests/resources/js-test.js). |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 335 | |
| 336 | `js-test` tests lean heavily on the Blink-specific `testRunner` testing API. |
| 337 | In a nutshell, the tests call `testRunner.dumpAsText()` to signal that the page |
| 338 | content should be dumped and compared against a text baseline (an |
| 339 | `-expected.txt` file). As a consequence, `js-test` tests are always accompanied |
| 340 | by 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 | |
| 345 | By default, tests are loaded as if via `file:` URLs. Some web platform features |
| 346 | require tests served via HTTP or HTTPS, for example absolute paths (`src=/foo`) |
| 347 | or features restricted to secure protocols. |
| 348 | |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 349 | HTTP tests are those under `web_tests/http/tests` (or virtual variants). Use a |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 350 | locally running HTTP server (Apache) to run them. Tests are served off of ports |
| 351 | 8000 and 8080 for HTTP, and 8443 for HTTPS. If you run the tests using |
Kent Tamura | a045a7f | 2018-04-25 05:08:11 | [diff] [blame] | 352 | `run_web_tests.py`, the server will be started automatically. To run the server |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 353 | manually to reproduce or debug a failure: |
| 354 | |
| 355 | ```bash |
Kent Tamura | e81dbff | 2018-04-20 17:35:34 | [diff] [blame] | 356 | cd src/third_party/blink/tools |
| 357 | ./run_blink_httpd.py |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 358 | ``` |
| 359 | |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 360 | The web tests will be served from `http://127.0.0.1:8000`. For example, to |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 361 | run the test `http/tests/serviceworker/chromium/service-worker-allowed.html`, |
| 362 | navigate to |
| 363 | `http://127.0.0.1:8000/serviceworker/chromium/service-worker-allowed.html`. Some |
| 364 | tests will behave differently if you go to 127.0.0.1 instead of localhost, so |
| 365 | use 127.0.0.1. |
| 366 | |
Kent Tamura | e81dbff | 2018-04-20 17:35:34 | [diff] [blame] | 367 | To kill the server, hit any key on the terminal where `run_blink_httpd.py` is |
Hajime Hoshi | a6fad02 | 2017-08-01 17:57:58 | [diff] [blame] | 368 | running, or just use `taskkill` or the Task Manager on Windows, and `killall` or |
| 369 | Activity Monitor on MacOS. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 370 | |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 371 | The test server sets up an alias to the `web_tests/resources` directory. In |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 372 | HTTP tests, you can access the testing framework at e.g. |
pwnall | e781948 | 2016-12-17 00:58:40 | [diff] [blame] | 373 | `src="/resources/testharness.js"`. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 374 | |
| 375 | TODO: Document [wptserve](http://wptserve.readthedocs.io/) when we are in a |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 376 | position to use it to run web tests. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 377 | |
| 378 | ## Reference Tests (Reftests) |
| 379 | |
| 380 | Reference tests, also known as reftests, perform a pixel-by-pixel comparison |
| 381 | between the rendered image of a test page and the rendered image of a reference |
| 382 | page. Most reference tests pass if the two images match, but there are cases |
| 383 | where it is useful to have a test pass when the two images do _not_ match. |
| 384 | |
| 385 | Reference tests are more difficult to debug than JavaScript tests, and tend to |
| 386 | be slower as well. Therefore, they should only be used for functionality that |
| 387 | cannot be covered by JavaScript tests. |
| 388 | |
| 389 | New reference tests should follow the |
Philip Jägenstedt | 3a3d5b8 | 2018-05-31 15:25:35 | [diff] [blame] | 390 | [WPT reftests guidelines](https://web-platform-tests.org/writing-tests/reftests.html). |
foolip | eda32ab | 2017-02-16 19:21:58 | [diff] [blame] | 391 | The most important points are summarized below. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 392 | |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 393 | * 🚧 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. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 396 | * 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 | |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 404 | 🚧 Our testing infrastructure was designed for the |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 405 | [WebKit reftests](https://trac.webkit.org/wiki/Writing%20Reftests) that Blink |
| 406 | has 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 | |
| 417 | The following example demonstrates a reference test for |
| 418 | [`<ol>`'s reversed attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ol). |
| 419 | The example assumes that the test page is named `ol-reversed.html`. |
| 420 | |
| 421 | ```html |
| 422 | <!doctype html> |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 423 | <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 | |
| 432 | The reference page, which must be named `ol-reversed-expected.html`, is below. |
| 433 | |
| 434 | ```html |
| 435 | <!doctype html> |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 436 | |
| 437 | <ol> |
| 438 | <li value="3">A</li> |
| 439 | <li value="2">B</li> |
| 440 | <li value="1">C</li> |
| 441 | </ol> |
| 442 | ``` |
| 443 | |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 444 | *** promo |
| 445 | The method for pointing out a test's reference page is still in flux, and is |
| 446 | being discussed on |
| 447 | [blink-dev](https://groups.google.com/a/chromium.org/d/topic/blink-dev/XsR6PKRrS1E/discussion). |
| 448 | *** |
| 449 | |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 450 | ## Pixel Tests |
| 451 | |
Xianzhu Wang | 6e84f099 | 2018-10-23 23:07:55 | [diff] [blame] | 452 | A test creates an image result by default unless some `testRunner` API is |
| 453 | called (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. |
| 456 | The 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 |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 458 | image result is identical to the baseline, according to a pixel-by-pixel |
Xianzhu Wang | 6e84f099 | 2018-10-23 23:07:55 | [diff] [blame] | 459 | comparison. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 460 | |
| 461 | Pixel tests should still follow the principles laid out above. Pixel tests pose |
| 462 | unique challenges to the desire to have *self-describing* and *cross-platform* |
| 463 | tests. The |
Philip Jägenstedt | 3a3d5b8 | 2018-05-31 15:25:35 | [diff] [blame] | 464 | [WPT rendering test guidelines](https://web-platform-tests.org/writing-tests/rendering.html) |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 465 | contain 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. |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 472 | * 🚧 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. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 477 | |
| 478 | *** promo |
Xianzhu Wang | 6e84f099 | 2018-10-23 23:07:55 | [diff] [blame] | 479 | The default size of the image result of a pixel test is 800x600px, because test |
| 480 | pages are rendered in an 800x600px viewport by default. Normally pixel tests |
| 481 | that do not specifically cover scrolling should fit in an 800x600px viewport |
| 482 | without creating scrollbars. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 483 | *** |
| 484 | |
pwnall | 6acacd8 | 2016-12-02 01:40:15 | [diff] [blame] | 485 | *** promo |
| 486 | The 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 Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 490 | The following snippet includes the Ahem font in a web test. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 491 | |
| 492 | ```html |
| 493 | <style> |
| 494 | body { |
| 495 | font: 10px Ahem; |
| 496 | } |
| 497 | </style> |
| 498 | <script src="/resources/ahem.js"></script> |
| 499 | ``` |
| 500 | |
| 501 | *** promo |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 502 | Tests outside `web_tests/http` and `web_tests/external/wpt` currently need |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 503 | to use a relative path to |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 504 | [/third_party/blink/web_tests/resources/ahem.js](../../third_party/blink/web_tests/resources/ahem.js) |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 505 | *** |
| 506 | |
| 507 | ### Tests that need to paint, raster, or draw a frame of intermediate output |
| 508 | |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 509 | A web test does not actually draw frames of output until the test exits. |
Xianzhu Wang | af4fa41 | 2018-05-14 21:26:52 | [diff] [blame] | 510 | Tests that need to generate a painted frame can use `runAfterLayoutAndPaint()` |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 511 | defined 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 Wang | af4fa41 | 2018-05-14 21:26:52 | [diff] [blame] | 512 | which will run the machinery to put up a frame, then call the passed callback. |
| 513 | There is also a library at |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 514 | [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 Wang | af4fa41 | 2018-05-14 21:26:52 | [diff] [blame] | 515 | to help with writing paint invalidation and repaint tests. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 516 | |
Mason Freed | b085562 | 2018-10-02 17:49:47 | [diff] [blame] | 517 | ### Tests for scrolling animations |
| 518 | |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 519 | Some web tests need to ensure animations such as middle-click auto-scroll, |
Mason Freed | b085562 | 2018-10-02 17:49:47 | [diff] [blame] | 520 | fling, etc. get performed properly. When testing in display compositor pixel |
| 521 | dump mode (now the standard), the standard behavior for tests is to |
| 522 | synchronously composite without rastering (to save time). However, animations |
| 523 | run upon surface activation, which only happens once rasterization is performed. |
| 524 | Therefore, for these tests, an additional setting needs to be set. Near the |
Mason Freed | c41b2d08 | 2018-10-26 17:20:21 | [diff] [blame] | 525 | beginning of these tests, call `setAnimationRequiresRaster()` defined in |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 526 | [third_party/blink/web_tests/resources/compositor-controls.js](../../third_party/blink/web_tests/resources/compositor-controls.js) |
Mason Freed | b085562 | 2018-10-02 17:49:47 | [diff] [blame] | 527 | which will enable full rasterization during the test. |
| 528 | |
Xianzhu Wang | 6e84f099 | 2018-10-23 23:07:55 | [diff] [blame] | 529 | ## Text tests |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 530 | |
Xianzhu Wang | 6e84f099 | 2018-10-23 23:07:55 | [diff] [blame] | 531 | A **text test** outputs text result. The result is compared against a text |
| 532 | baseline which is an `-expected.txt` file associated with the test, and the |
| 533 | test passes if the text result is identical to the baseline. A test isn't a |
| 534 | text test by default until it calls some `testRunner` API to instruct the |
| 535 | test runner to output text. A text test can be categorized based on what kind of |
| 536 | information that the text result represents. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 537 | |
Xianzhu Wang | 6e84f099 | 2018-10-23 23:07:55 | [diff] [blame] | 538 | ### Layout tree test |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 539 | |
Xianzhu Wang | 6e84f099 | 2018-10-23 23:07:55 | [diff] [blame] | 540 | If a test calls `testRunner.dumpAsLayout()` or |
| 541 | `testRunner.dumpAsLayoutWithPixelResults()`, The text result will be a |
| 542 | textual 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. |
| 545 | With `testRunner.dumpChildFrames()` the text result will also include layout |
| 546 | tree of child frames. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 547 | |
Xianzhu Wang | 6e84f099 | 2018-10-23 23:07:55 | [diff] [blame] | 548 | Like pixel tests, the output of layout tree tests may depend on |
| 549 | platform-specific details, so layout tree tests often require per-platform |
| 550 | baselines. Furthermore, since the tests obviously depend on the layout tree |
| 551 | structure, that means that if we change the layout tree you have to rebaseline |
| 552 | each layout tree test to see if the results are still correct and whether the |
| 553 | test is still meaningful. There are actually many cases where the layout tree |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 554 | output is misstated (i.e., wrong), because people didn't want to have to update |
| 555 | existing baselines and tests. This is really unfortunate and confusing. |
| 556 | |
| 557 | For these reasons, layout tree tests should **only** be used to cover aspects |
| 558 | of the layout code that can only be tested by looking at the layout tree. Any |
| 559 | combination of the other test types is preferable to a layout tree test. |
| 560 | Layout tree tests are |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 561 | [inherited from WebKit](https://webkit.org/blog/1456/layout-tests-practice/), so |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 562 | the repository may have some unfortunate examples of layout tree tests. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 563 | |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 564 | The following page is an example of a layout tree test. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 565 | |
| 566 | ```html |
| 567 | <!doctype html> |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 568 | <style> |
| 569 | body { font: 10px Ahem; } |
| 570 | span::after { |
| 571 | content: "pass"; |
| 572 | color: green; |
| 573 | } |
| 574 | </style> |
| 575 | <script src="/resources/ahem.js"></script> |
Xianzhu Wang | 6e84f099 | 2018-10-23 23:07:55 | [diff] [blame] | 576 | <script> |
| 577 | if (window.testRunner) |
| 578 | testRunner.dumpAsLayout(); |
| 579 | </script> |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 580 | <p><span>Pass if a green PASS appears to the right: </span></p> |
| 581 | ``` |
| 582 | |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 583 | The test page produces the text result below. |
| 584 | |
| 585 | ``` |
| 586 | layer at (0,0) size 800x600 |
| 587 | LayoutView at (0,0) size 800x600 |
| 588 | layer 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 | |
| 600 | Notice that the test result above depends on the size of the `<p>` text. The |
| 601 | test page uses the Ahem font (introduced above), whose main design goal is |
| 602 | consistent cross-platform rendering. Had the test used another font, its text |
| 603 | baseline would have depended on the fonts installed on the testing computer, and |
| 604 | on the platform's font rendering system. Please follow the pixel tests |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 605 | guidelines and write reliable layout tree tests! |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 606 | |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 607 | WebKit's layout tree is described in |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 608 | [a series of posts](https://webkit.org/blog/114/webcore-rendering-i-the-basics/) |
dpranke | d2b7d64 | 2017-01-15 04:00:24 | [diff] [blame] | 609 | on WebKit's blog. Some of the concepts there still apply to Blink's layout tree. |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 610 | |
Xianzhu Wang | 6e84f099 | 2018-10-23 23:07:55 | [diff] [blame] | 611 | ### Text dump test |
| 612 | |
| 613 | If `testRunner.dumpAsText()` or `testRunner.dumpAsTextWithPixelResults()` |
| 614 | is called from a test, the test will dump the text contents of the main frame |
| 615 | of the tested page. With `testRunner.dumpChildFrames()` the text |
| 616 | result will also include text contents of child frames. Actually a JavaScript |
| 617 | test is a special kind of text dump test which can often omit the text baseline. |
| 618 | |
| 619 | A test can override the default text dump by calling |
| 620 | `testRunner.setCustomTextOutput(string)`. The string parameter can be any |
| 621 | text that the test wants to output. The [`internals` API](../../third_party/blink/renderer/core/testing/internals.idl] |
| 622 | provides methods to get textual representations of internal data structures that |
| 623 | can be used as the parameter of `testRunner.setCustomTextOutput()`. |
| 624 | |
| 625 | ### Markup dump test |
| 626 | |
| 627 | If a test calls `testRunner.dumpAsMarkup()`, the text result will be the DOM |
| 628 | of the main frame of the test. With `testRunner.dumpChildFrames()` the text |
| 629 | result will also include DOM of child frames. |
| 630 | |
| 631 | ## Audio tests |
| 632 | |
| 633 | If a test calls `testRunner.setAudioData(array_buffer)`, the test will |
| 634 | create an audio result. The result will be compared against an audio baseline |
| 635 | which is an `-expected.wav` file associated with the test, and the test passes |
| 636 | if the audio result is identical to the baseline. |
| 637 | |
| 638 | ## Tests that are both pixel/reference tests and text tests |
| 639 | |
| 640 | If a test calls `testRunner.dumpAsTextWithPixelResults()` or |
| 641 | `testRunner.dumpAsLayoutWithPixelResults()`, the test is both a |
| 642 | pixel/reference test and a text test. It will output both pixel result and text |
| 643 | result. |
| 644 | |
Xianzhu Wang | 4bbcebe | 2018-10-31 02:21:46 | [diff] [blame] | 645 | For a test that is both a pixel/reference test and a text test, both pixel and |
| 646 | text results will be compared to baselines, and the test passes if each result |
| 647 | matches the corresponding baseline. |
Xianzhu Wang | 6e84f099 | 2018-10-23 23:07:55 | [diff] [blame] | 648 | |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 649 | Many of the [paint invalidation tests](../../third_party/blink/web_tests/paint/invalidation) |
Xianzhu Wang | 6e84f099 | 2018-10-23 23:07:55 | [diff] [blame] | 650 | are of this type. The pixel results (compared against `-expected.png` or |
| 651 | `-expected.html`) ensure correct rendering, and the text results (compared |
| 652 | against `-expected.txt`) ensure correct compositing and raster invalidation |
| 653 | (without unexpected over and under invalidations). |
| 654 | |
| 655 | For a layout tree test, whether you want a pixel test and/or a text test depends |
| 656 | on whether you care about the visual image, the details of how that image was |
| 657 | constructed, or both. It is possible for multiple layout trees to produce |
| 658 | the same pixel output, so it is important to make it clear in the test |
| 659 | which outputs you really care about. |
| 660 | |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 661 | ## Directory Structure |
| 662 | |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 663 | The [web_tests directory](../../third_party/blink/web_tests) currently |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 664 | lacks a strict, formal structure. The following directories have special |
| 665 | meaning: |
| 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 Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 672 | Some web tests consist of a minimal HTML page that references a JavaScript |
pwnall | 4ea2eb3 | 2016-11-29 02:47:25 | [diff] [blame] | 673 | file in `resources/`. Please do not use this pattern for new tests, as it goes |
| 674 | against the minimality principle. JavaScript and CSS files should only live in |
| 675 | `resources/` if they are shared by at least two test files. |
| 676 | *** |