summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/cookie-store/resources/helpers.js
blob: 8d5dddef6577fc534a68c9e8415854e4c64901fd (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
/**
 * Promise based helper function who's return promise will resolve
 * once the iframe src has been loaded
 * @param {string} url the url to set the iframe src
 * @param {test} t a test object to add a cleanup function to
 * @return {Promise} when resolved, will return the iframe
 */
self.createIframe = (url, t) => new Promise(resolve => {
  const iframe = document.createElement('iframe');
  iframe.addEventListener('load', () => {resolve(iframe);}, {once: true});
  iframe.src = url;
  document.documentElement.appendChild(iframe);
  t.add_cleanup(() => iframe.remove());
});

/**
 * @description - Function unregisters any service workers in this scope
 *                and then creates a new registration. The function returns
 *                a promise that resolves when the registered service worker
 *                becomes activated. The resolved promise yields the
 *                service worker registration
 * @param {testCase} t - test case to add cleanup functions to
 */
self.createServiceWorker = async (t, sw_registration_name, scope_url) => {
  let registration = await navigator.serviceWorker.getRegistration(scope_url);
  if (registration)
    await registration.unregister();

  registration = await navigator.serviceWorker.register(sw_registration_name,
      {scope_url});
  t.add_cleanup(() => registration.unregister());

  return new Promise(resolve => {
    const serviceWorker = registration.installing || registration.active ||
        registration.waiting;
    serviceWorker.addEventListener('statechange', event => {
      if (event.target.state === 'activated') {
        resolve(serviceWorker);
      }
    });
  })
}

/**
 * Function that will return a promise that resolves when a message event
 * is fired. Returns a promise that resolves to the message that was received
 */
self.waitForMessage = () => new Promise(resolve => {
  window.addEventListener('message', event => {
    resolve(event.data);
  }, {once: true});
});

/**
 * Sends a message via MessageChannel and waits for the response
 * @param {*} message
 * @returns {Promise} resolves with the response payload
 */
self.sendMessageOverChannel = (message, target) => {
  return new Promise(function(resolve, reject) {
    const messageChannel = new MessageChannel();
    messageChannel.port1.onmessage = event => {
      if (event.data.error) {
        reject(event.data.error);
      } else {
        resolve(event.data);
      }
    };

    target.postMessage(message, [messageChannel.port2]);
  })
};