Service Worker has multiple kinds of tests: Content Browser Tests, WebKit Unit Tests, Telemetry Performance Tests, and Blink Layout Tests. Performance TestsIssue 372759 is tracking the development of Service Worker performance tests.How to run the performance tests:
How to update the performance tests:
Web TestsWithin Blink Web Tests, Service Worker has two kinds of tests:
Web Tests StyleHistorically, style was not rigorously enforced for Blink Web Tests. Service Worker has decided to implement tests in a consistent style. The style guide we have designed is more heavily influenced by the W3C's test harness than Blink's style guide because we plan to contribute most of our test suite to the W3C. Where this style guide is not explicit, follow the Google JavaScript style guide because it is at least comprehensive. Use the W3C Test Harness Script Test Template: Minimal for HTML files: <!DOCTYPE html> <meta charset="utf-8"> <title>[Title of Test]</title> <script src="/resources/testharness.js"></script> <script src="/resources/testharnessreport.js"></script> <script> // Calls to test() or async_test() </script> Write variable identifiers and function names in lowercase separated with underscores. Use PascalCase for constructor functions.
Use single quotes for string literals. Reserve double quotes for attribute values in markup. This makes it easy to be consistent in most situations where JavaScript appears in markup and vice-versa.
Always quote attributes. Use double quotes. Exception: Use defaults for brevity, for example write <button disabled> and not <button disabled="disabled">.
Do:
var html_button = '<button onclick="close();">Bye</button>'; Don't:
var htmlButton = "<button onclick=close()>Bye</button>"; Wrapping and IndentationIndent two spaces.
Do:
for (var w = window; w != window.parent; w = window.parent) { Array.prototype.forEach.call(w.frames, function(frame) { console.log( w.title, frame.contentWindow.title); }); } When wrapping function arguments, indent two spaces. Object and function literals are not exempt from this (example 1). Exceptions. These indent the arguments four spaces:
Do: // Example 1
items.forEach(function(item) { assert_true(is_expensive(item)); });
// Example 2
service_worker_unregister_and_register( t, 'resources/a-very-long-path-to-a-worker.js' 'resources/a-very-long-path-to-a-scope') .then(function(registration) { // code }); // Example 3 function dispatch_keys_for_cache( cache_id, callbacks, request, query_params) { // code } Don't: items.forEach(function(item) { assert_true(is_expensive(item)); }); Note: This is different to the Google JavaScript style guide. It is necessary to have sensible indentation with Promises, which mix functions and long function- or Promise-returning arguments. Wrap at 80 columns. Exception: Markup with long attribute values.
Break after opening curly braces. Exception: Anonymous functions that are small enough to fit within a line do not need to break after {.
Do:
clients.forEach(function(client) { client.postMessage('Happy New Year!); }); Don't:
function embiggen(element) { element.baggage = new ArrayBuffer(1000000); } When wrapping chained property accesses, break before the period.
Do:
naviga tor.serviceWorker.register('worker.js') .then( function(worker) { // party time }) .catch(poop); Don't:
navigator.serviceWorker.register('worker.js') .then( function(worker) { // party time }) . catch(poop); PromisesWhen using Promises, prefer a separate .catch to the two-argument form of .then. Exception: You need to test an expected exception, and using .catch separately would be tedious.
Do:
navigator.serviceWorker.register('worker.js') .then(function(worker) { // test code }) .then(function(worker) { // moar test code }) .catch(unexpected_exception(t)); navigator.serviceWorker.register('http://insecure.example.com/worker.js') .then( unreachable_code( t, 'registering a script with an insecure origin should throw a ' + 'SecurityError'), expected_exception(t, SecurityError)) .then(function() { // moar test code }); Don't:
navigator.serviceWorker.register('worker.js') .then(function(worker) { // test code }) .then(function() { // moar test code }, unexpected_exception(t)); Test Harness specific issuesWhen writing an async test, use the form that takes a function as the first argument. Capture the test object in the variable 't'. Implement the test in the scope of the anonymous function. This keeps variables touched by async steps of a test isolated which makes the tests more reliable and makes it easier to move tests between files.
Do:
async_test(function(t) { // test code }, '"ready" resolves to the active Service Worker of a controlled document'); Don't:
var t = async_test( '"ready" resolves to the active Service Worker of a controlled document'); // test code Assertions should have messages. Messages should describe the desired behavior and use the word 'should'; this aids clarity.Do:
assert_equals( navigator.serviceWorker.active, null, 'an uncontrolled document should not have an active Service Worker'); Don't:
assert_equals( navigator.serviceWorker.active, null, 'non-null active Service Worker'); To ensure test failures from unhandled exceptions pinpoint the cause of the failure precisely, asynchronous callbacks, such as event handlers and setTimeout callbacks, must use the |
Blink > Service workers >