summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/web-locks/resources/helpers.js
blob: 4b3311eee6290e62cfb5f504f0319ad7a64d28b9 (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
76
77
78
79
80
81
82
83
// Test helpers used by multiple Web Locks API tests.
(() => {

  // Generate a unique resource identifier, using the script path and
  // test case name. This is useful to avoid lock interference between
  // test cases.
  let res_num = 0;
  self.uniqueName = (testCase, prefix) => {
    return `${self.location.pathname}-${prefix}-${testCase.name}-${++res_num}`;
  };
  self.uniqueNameByQuery = () => {
    const prefix = new URL(location.href).searchParams.get('prefix');
    return `${prefix}-${++res_num}`;
  }

  // Inject an iframe showing the given url into the page, and resolve
  // the returned promise when the frame is loaded.
  self.iframe = url => new Promise(resolve => {
    const element = document.createElement('iframe');
    element.addEventListener(
      'load', () => { resolve(element); }, { once: true });
    element.src = url;
    document.documentElement.appendChild(element);
  });

  // Post a message to the target frame, and resolve the returned
  // promise when a response comes back. The posted data is annotated
  // with unique id to track the response. This assumes the use of
  // 'iframe.html' as the frame, which implements this protocol.
  let next_request_id = 0;
  self.postToFrameAndWait = (frame, data) => {
    const iframe_window = frame.contentWindow;
    data.rqid = next_request_id++;
    iframe_window.postMessage(data, '*');
    return new Promise(resolve => {
      const listener = event => {
        if (event.source !== iframe_window || event.data.rqid !== data.rqid)
          return;
        self.removeEventListener('message', listener);
        resolve(event.data);
      };
      self.addEventListener('message', listener);
    });
  };

  // Post a message to the target worker, and resolve the returned
  // promise when a response comes back. The posted data is annotated
  // with unique id to track the response. This assumes the use of
  // 'worker.js' as the worker, which implements this protocol.
  self.postToWorkerAndWait = (worker, data) => {
    return new Promise(resolve => {
      data.rqid = next_request_id++;
      worker.postMessage(data);
      const listener = event => {
        if (event.data.rqid !== data.rqid)
          return;
        worker.removeEventListener('message', listener);
        resolve(event.data);
      };
      worker.addEventListener('message', listener);
    });
  };

  /**
   * Request a lock and hold it until the subtest ends.
   * @param {*} t test runner object
   * @param {string} name lock name
   * @param {LockOptions=} options lock options
   * @returns
   */
  self.requestLockAndHold = (t, name, options = {}) => {
    return navigator.locks.request(name, options, () => {
      return new Promise(resolve => t.add_cleanup(resolve));
    });
  };

  self.makePromiseAndResolveFunc = () => {
    let resolve;
    const promise = new Promise(r => { resolve = r; });
    return [promise, resolve];
  };

})();