summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/browsers/browsing-the-web/history-traversal/srcdoc/srcdoc-history-entries.html
blob: 09f4094c5f6eaa72d617f50b0743caf9bef1ad0d (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
84
85
86
87
88
89
90
91
92
93
94
95
<!DOCTYPE html>
<meta charset="utf-8">
<title>srcdoc history entries</title>
<link rel="help" href="https://github.com/whatwg/html/issues/6809">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../../resources/helpers.js"></script>

<script>
"use strict";

// Note: bfcache won't mess with any windows with openers, so it doesn't
// interfere with these tests.

promise_test(async t => {
  // Set up a window whose iframe contains
  // [normal page, srcdoc, normal page, srcdoc] entries.
  const w = await openWindow("/common/blank.html", t);
  const iframe = await addIframe("/common/blank.html?iframe", w.document);

  assert_equals(w.history.length, 1);

  await waitToAvoidReplace(t);
  iframe.srcdoc = srcdocThatPostsParentOpener("srcdoc1");
  assert_equals(w.history.length, 1, "srcdoc navigation must not be sync");

  await waitForMessage(iframe.contentWindow);
  assert_equals(w.history.length, 2);

  await waitToAvoidReplace(t);
  const middleURL = (new URL(
    "../../resources/post-top-opener-on-load.html", location.href)).href;
  iframe.contentWindow.location.href = middleURL;

  await waitForMessage(iframe.contentWindow);
  assert_equals(w.history.length, 3);

  await waitToAvoidReplace(t);
  iframe.srcdoc = srcdocThatPostsParentOpener("srcdoc2");
  assert_equals(w.history.length, 3, "srcdoc navigation must not be sync");

  await waitForMessage(iframe.contentWindow);
  assert_equals(w.history.length, 4);

  // Now test traversal.
  w.history.back();
  await waitForMessage(iframe.contentWindow);
  assert_equals(iframe.contentWindow.location.href, middleURL);

  await waitToAvoidReplace(t);

  w.history.back();
  await waitForMessage(iframe.contentWindow);
  assert_equals(iframe.contentWindow.location.href, "about:srcdoc");
  assert_equals(
    iframe.contentDocument.querySelector("p").textContent,
    "srcdoc1",
    "srcdoc contents must be restored from history, not from the current " +
    "value ('srcdoc2') of the content attribute"
  );
}, "srcdoc history entries: the iframe itself navigates");

promise_test(async t => {
  // Set up a window whose iframe contains [normal page, srcdoc] entries.
  const w = await openWindow("../../resources/has-iframe.html", t);
  const iframe = w.document.querySelector("iframe");

  assert_equals(w.history.length, 1);

  await waitToAvoidReplace(t);
  iframe.srcdoc = srcdocThatPostsParentOpener("srcdoc1");
  assert_equals(w.history.length, 1, "srcdoc navigation must not be sync");

  await waitForMessage(iframe.contentWindow);
  assert_equals(w.history.length, 2);

  // Now navigate the window itself.
  w.location.href = "../../resources/post-top-opener-on-load.html";
  await waitForMessage(w);
  assert_equals(w.history.length, 3);

  // Now test traversal.
  w.history.back();
  await waitForMessage(w);
  const iframeAgain = w.document.querySelector("iframe");

  assert_equals(iframeAgain.contentWindow.location.href, "about:srcdoc");
  assert_equals(
    iframeAgain.contentDocument?.querySelector("p")?.textContent,
    "srcdoc1",
    "srcdoc contents must be restored from history, not from the current " +
    "value of the (not-existing) content attribute"
  );
}, "srcdoc history entries: the container window navigates");
</script>