summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/background-fetch/resources/utils.js
blob: 10895c3ccfc012c0e0ecd1d9ffa820a86c3ef142 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';

let nextBackgroundFetchId = 0;

function loadScript(path) {
  let script = document.createElement('script');
  let promise = new Promise(resolve => script.onload = resolve);
  script.src = path;
  script.async = false;
  document.head.appendChild(script);
  return promise;
}

// Waits for a single message received from a registered Service Worker.
async function getMessageFromServiceWorker() {
  return new Promise(resolve => {
    function listener(event) {
      navigator.serviceWorker.removeEventListener('message', listener);
      resolve(event.data);
    }

    navigator.serviceWorker.addEventListener('message', listener);
  });
}

// Registers the |name| instrumentation Service Worker located at "service_workers/"
// with a scope unique to the test page that's running, and waits for it to be
// activated. The Service Worker will be unregistered automatically.
//
// Depends on /service-workers/service-worker/resources/test-helpers.sub.js
async function registerAndActivateServiceWorker(test, name) {
  const script = `service_workers/${name}`;
  const scope = 'service_workers/scope' + location.pathname;

  let serviceWorkerRegistration =
      await service_worker_unregister_and_register(test, script, scope);

  add_completion_callback(() => serviceWorkerRegistration.unregister());

  await wait_for_state(test, serviceWorkerRegistration.installing, 'activated');
  return serviceWorkerRegistration;
}

// Creates a Promise test for |func| given the |description|. The |func| will be
// executed with the `backgroundFetch` object of an activated Service Worker
// Registration.
// |workerName| is the name of the service worker file in the service_workers
// directory to register.
function backgroundFetchTest(func, description, workerName = 'sw.js') {
  promise_test(async t => {
    if (typeof test_driver === 'undefined') {
      await loadScript('/resources/testdriver.js');
      await loadScript('/resources/testdriver-vendor.js');
    }

    await test_driver.set_permission({name: 'background-fetch'}, 'granted');

    const serviceWorkerRegistration =
        await registerAndActivateServiceWorker(t, workerName);
    serviceWorkerRegistration.active.postMessage(null);

    assert_equals(await getMessageFromServiceWorker(), 'ready');

    return func(t, serviceWorkerRegistration.backgroundFetch);
  }, description);
}

// Returns a Background Fetch ID that's unique for the current page.
function uniqueId() {
  return 'id' + nextBackgroundFetchId++;
}