summaryrefslogtreecommitdiffstats
path: root/docshell/test/navigation/test_session_history_on_redirect.html
blob: a303f8153638257beef9de2060030d41c07b85a0 (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Session history on redirect</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
  <script>
    /*
     * The test opens a new window and loads a page there. Then another document
     * is loaded to the window. The initial load of that second page doesn't do
     * a redirect. Now another non-redirecting page is loaded. Then
     * history.go(-2) and history.forward() are called. The second time
     * the second page is loaded, it does a redirect. history.back() and
     * history.forward() are called again. The page which did the redirect
     * shouldn't be accessed, but the page which it redirected to.
     * Finally history.forward() is called again and the third page should be
     * loaded and history.length should have the same value as it had when the
     * third page was loaded the first time.
     */

    SimpleTest.waitForExplicitFinish();
    var win;
    var finalHistoryLength = 0;

    function run() {
      win = window.open("file_session_history_on_redirect.html");
    }

    var pageshowCounter = 0;
    async function pageshow() {
      // Need to trigger new loads asynchronously after page load, otherwise
      // new loads are treated as replace loads.
      await new Promise((r) => setTimeout(r));
      ++pageshowCounter;
      info("Page load: " + win.location.href);
      switch (pageshowCounter) {
        case 1:
          ok(win.location.href.includes("file_session_history_on_redirect.html"));
          win.location.href = "redirect_handlers.sjs";
          break;
        case 2:
          ok(win.location.href.includes("redirect_handlers.sjs"));
          // Put the initial page also as the last entry in the session history.
          win.location.href = "file_session_history_on_redirect.html";
          break;
        case 3:
          ok(win.location.href.includes("file_session_history_on_redirect.html"));
          finalHistoryLength = win.history.length;
          win.history.go(-2);
          break;
        case 4:
          ok(win.location.href.includes("file_session_history_on_redirect.html"));
          win.history.forward();
          break;
        case 5:
          ok(win.location.href.includes("file_session_history_on_redirect_2.html"));
          win.history.back();
          break;
        case 6:
          ok(win.location.href.includes("file_session_history_on_redirect.html"));
          win.history.forward();
          break;
        case 7:
          ok(win.location.href.includes("file_session_history_on_redirect_2.html"));
          is(win.history.length, finalHistoryLength, "Shouldn't have changed the history length.");
          win.history.forward();
          break;
        case 8:
          ok(win.location.href.includes("file_session_history_on_redirect.html"));
          is(win.history.length, finalHistoryLength, "Shouldn't have changed the history length.");
          win.onpagehide = null;
          finishTest();
          break;
        default:
            ok(false, "unexpected pageshow");
      }
    }

    function finishTest() {
      win.close()
      SimpleTest.finish();
    }

  </script>
</head>
<body onload="run()">
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
</body>
</html>