summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/fetch-later/new-window.tentative.https.window.js
blob: 93705418f21d1c5d4536a689852b1c252bfdbbb3 (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
// META: script=/common/utils.js
// META: script=/common/get-host-info.sub.js
// META: script=/pending-beacon/resources/pending_beacon-helper.js

'use strict';

const {
  HTTPS_ORIGIN,
  HTTPS_NOTSAMESITE_ORIGIN,
} = get_host_info();

function fetchLaterPopupUrl(host, targetUrl) {
  return `${host}/fetch/fetch-later/resources/fetch-later.html?url=${
      encodeURIComponent(targetUrl)}`;
}

for (const target of ['', '_blank']) {
  for (const features in ['', 'popup', 'popup,noopener']) {
    parallelPromiseTest(
        async t => {
          const uuid = token();
          const url =
              generateSetBeaconURL(uuid, {host: HTTPS_NOTSAMESITE_ORIGIN});

          // Opens a blank popup window that fires a fetchLater request.
          const w = window.open(
              `javascript: fetchLater("${url}", {activateAfter: 0})`, target,
              features);
          await new Promise(resolve => w.addEventListener('load', resolve));

          // The popup should have sent the request.
          await expectBeacon(uuid, {count: 1});
          w.close();
        },
        `A blank window[target='${target}'][features='${
            features}'] can trigger fetchLater.`);

    parallelPromiseTest(
        async t => {
          const uuid = token();
          const popupUrl =
              fetchLaterPopupUrl(HTTPS_ORIGIN, generateSetBeaconURL(uuid));

          // Opens a same-origin popup that fires a fetchLater request.
          const w = window.open(popupUrl, target, features);
          await new Promise(resolve => w.addEventListener('load', resolve));

          // The popup should have sent the request.
          await expectBeacon(uuid, {count: 1});
          w.close();
        },
        `A same-origin window[target='${target}'][features='${
            features}'] can trigger fetchLater.`);

    parallelPromiseTest(
        async t => {
          const uuid = token();
          const popupUrl = fetchLaterPopupUrl(
              HTTPS_NOTSAMESITE_ORIGIN, generateSetBeaconURL(uuid));

          // Opens a cross-origin popup that fires a fetchLater request.
          const w = window.open(popupUrl, target, features);
          // As events from cross-origin window is not accessible, waiting for
          // its message instead.
          await new Promise(
              resolve => window.addEventListener('message', resolve));

          // The popup should have sent the request.
          await expectBeacon(uuid, {count: 1});
          w.close();
        },
        `A cross-origin window[target='${target}'][features='${
            features}'] can trigger fetchLater.`);
  }
}