summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/resources/windowclient-navigate-worker.js
blob: 383f66631d3a0d3b6f9b02f6847c01e1b19259c7 (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
72
73
74
75
importScripts('/resources/testharness.js');

function matchQuery(queryString) {
  return self.location.search.substr(1) === queryString;
}

async function navigateTest(t, e) {
  const port = e.data.port;
  const url = e.data.url;
  const expected = e.data.expected;

  let p = clients.matchAll({ includeUncontrolled : true })
    .then(function(clients) {
      for (const client of clients) {
        if (client.url === e.data.clientUrl) {
          assert_equals(client.frameType, e.data.frameType);
          return client.navigate(url);
        }
      }
      throw 'Could not locate window client.';
    }).then(function(newClient) {
      // If we didn't reject, we better get resolved with the right thing.
      if (newClient === null) {
        assert_equals(newClient, expected);
      } else {
        assert_equals(newClient.url, expected);
      }
    });

  if (typeof self[expected] === "function") {
    // It's a JS error type name.  We are expecting our promise to be rejected
    // with that error.
    p = promise_rejects_js(t, self[expected], p);
  }

  // Let our caller know we are done.
  return p.finally(() => port.postMessage(null));
}

function getTestClient() {
  return clients.matchAll({ includeUncontrolled: true })
    .then(function(clients) {
      for (const client of clients) {
        if (client.url.includes('windowclient-navigate.https.html')) {
          return client;
        }
      }

      throw new Error('Service worker was unable to locate test client.');
    });
}

function waitForMessage(client) {
  const channel = new MessageChannel();
  client.postMessage({ port: channel.port2 }, [channel.port2]);

  return new Promise(function(resolve) {
    channel.port1.onmessage = resolve;
  });
}

// The worker must remain in the "installing" state for the duration of some
// sub-tests. In order to achieve this coordination without relying on global
// state, the worker must create a message channel with the client from within
// the "install" event handler.
if (matchQuery('installing')) {
  self.addEventListener('install', function(e) {
    e.waitUntil(getTestClient().then(waitForMessage));
  });
}

self.addEventListener('message', function(e) {
  e.waitUntil(promise_test(t => navigateTest(t, e),
                           e.data.description + " worker side"));
});