summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/speculation-rules/prefetch/initiators-window-open.sub.https.html
blob: f786df077dbfe1c3856aeb576ec005484e505c63 (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
<!DOCTYPE html>
<meta name="variant" content="?cross-site">
<meta name="variant" content="?same-site">
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/dispatcher/dispatcher.js"></script>
<script src="/common/utils.js"></script>
<script src="resources/utils.sub.js"></script>
<script>
  // In https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate,
  // `sourceDocument` (instead of `navigable`'s active document) should be
  // used as the referring document for prefetch.
  promise_test(async t => {
    assert_implements(HTMLScriptElement.supports('speculationrules'), "Speculation Rules not supported");

    const win = await spawnWindow(t, { protocol: 'https' });

    const hostname =
      location.search === '?cross-site' ? '{{hosts[alt][www]}}' : undefined;
    const nextUrl = win.getExecutorURL({ protocol: 'https', hostname, page: 2 });

    await win.forceSinglePrefetch(nextUrl);

    await win.execute_script((url) => {
      window.executor.suspend(() => {
        window.open(url, "_blank");
      });
    }, [nextUrl]);

    // Below, the scripts given to `win.execute_script()` are executed on the
    // `nextUrl` page in the new window, because `window.executor.suspend()`
    // above made `win`'s original page stop processing `execute_script()`,
    // while the new page of `nextUrl` in the new window starts processing
    // `execute_script()` for the same ID. Same for below.
    assert_equals(
        await win.execute_script(() => location.href),
        nextUrl.toString(),
        "expected navigation to reach destination URL");

    assert_prefetched(await win.getRequestHeaders());
  }, `window.open()`);

  promise_test(async t => {
    assert_implements(HTMLScriptElement.supports('speculationrules'), "Speculation Rules not supported");

    const win = await spawnWindow(t, { protocol: 'https' });

    const hostname =
      location.search === '?cross-site' ? '{{hosts[alt][www]}}' : undefined;
    const nextUrl = win.getExecutorURL({ protocol: 'https', hostname, page: 2 });

    await win.forceSinglePrefetch(nextUrl);

    await win.execute_script((url) => {
      window.executor.suspend(() => {
        window.open(url, "_blank", "noopener");
      });
    }, [nextUrl]);

    assert_equals(
        await win.execute_script(() => location.href),
        nextUrl.toString(),
        "expected navigation to reach destination URL");

    assert_prefetched(await win.getRequestHeaders());
  }, `window.open(noopener)`);
</script>