summaryrefslogtreecommitdiffstats
path: root/docshell/test/mochitest/test_bug1742865.html
blob: c8f9a4eca31dae00e30c51284d1f350d551792d0 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Auto refreshing pages shouldn't add an entry to session history</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
  <script>
    const REFRESH_REDIRECT_TIMER = 15;

    // 2 tests (same and cross origin) consisting of 2 refreshes of maximum 1 seconds
    // 2 tests (same and cross origin) consisting of 2 refreshes of REFRESH_REDIRECT_TIMER seconds
    // => We need (2 * 1) + (2 * 15) seconds
    SimpleTest.requestLongerTimeout(3);
    SimpleTest.waitForExplicitFinish();

    const SJS = new URL("file_bug1742865.sjs", location.href);
    const SJS_OUTER = new URL("file_bug1742865_outer.sjs", location.href);
    const SCROLL = 500;

    let tolerance;
    function setup() {
      return SpecialPowers.spawn(window.top, [], () => {
        return SpecialPowers.getDOMWindowUtils(content.window).getResolution();
      }).then(resolution => {
        // Allow a half pixel difference if the top document's resolution is lower
        // than 1.0 because the scroll position is aligned with screen pixels
        // instead of CSS pixels.
        tolerance = resolution < 1.0 ? 0.5 : 0.0;
      });
    }

    function checkScrollPosition(scrollPosition, shouldKeepScrollPosition) {
      isfuzzy(scrollPosition, shouldKeepScrollPosition ? SCROLL : 0, tolerance,
              `Scroll position ${shouldKeepScrollPosition ? "should" : "shouldn't"} be maintained for meta refresh`);
    }

    function openWindowAndCheckRefresh(url, params, shouldAddToHistory, shouldKeepScrollPosition) {
      info(`Running test for ${JSON.stringify(params)}`);

      url = new URL(url);
      Object.entries(params).forEach(([k, v]) => { url.searchParams.append(k, v) });
      url.searchParams.append("scrollTo", SCROLL);

      let resetURL = new URL(SJS);
      resetURL.search = "?reset";
      return fetch(resetURL).then(() => {
        return new Promise((resolve) => {
          let count = 0;
          window.addEventListener("message", function listener({ data: { commandType, commandData = {} } }) {
            if (commandType == "onChangedInputValue") {
              let { historyLength, inputValue } = commandData;

              if (shouldAddToHistory) {
                is(historyLength, count, "Auto-refresh should add entries to session history");
              } else {
                is(historyLength, 1, "Auto-refresh shouldn't add entries to session history");
              }

              is(inputValue, "1234", "Input's value should have been changed");

              win.postMessage("loadNext", "*");
              return;
            }

            is(commandType, "pageShow", "Unknown command type");

            let { inputValue, scrollPosition } = commandData;

            switch (++count) {
              // file_bug1742865.sjs causes 3 loads:
              //  * first load, returns first meta refresh
              //  * second load, caused by first meta refresh, returns second meta refresh
              //  * third load, caused by second meta refresh, doesn't return a meta refresh
              case 2:
                checkScrollPosition(scrollPosition, shouldKeepScrollPosition);
                break;
              case 3:
                checkScrollPosition(scrollPosition, shouldKeepScrollPosition);
                win.postMessage("changeInputValue", "*");
                break;
              case 4:
                win.postMessage("back", "*");
                break;
              case 5:
                is(inputValue, "1234", "Entries for auto-refresh should be attached to session history");
                checkScrollPosition(scrollPosition, shouldKeepScrollPosition);
                removeEventListener("message", listener);
                win.close();
                resolve();
                break;
            }
          });
          let win = window.open(url);
        });
      });
    }

    function doTest(seconds, crossOrigin, shouldAddToHistory, shouldKeepScrollPosition) {
      let params = {
        seconds,
        crossOrigin,
      };

      return openWindowAndCheckRefresh(SJS, params, shouldAddToHistory, shouldKeepScrollPosition).then(() =>
        openWindowAndCheckRefresh(SJS_OUTER, params, shouldAddToHistory, shouldKeepScrollPosition)
      );
    }

    async function runTest() {
      const FAST = Math.min(1, REFRESH_REDIRECT_TIMER);
      const SLOW = REFRESH_REDIRECT_TIMER + 1;
      let tests = [
        // [ time, crossOrigin, shouldAddToHistory, shouldKeepScrollPosition ]
        [ FAST, false, false, true ],
        [ FAST, true, false, false ],
        [ SLOW, false, false, true ],
        [ SLOW, true, true, false ],
      ];

      await setup();

      for (let [ time, crossOrigin, shouldAddToHistory, shouldKeepScrollPosition ] of tests) {
        await doTest(time, crossOrigin, shouldAddToHistory, shouldKeepScrollPosition);
      }

      SimpleTest.finish();
    }
  </script>
</head>
<body onload="runTest();">
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test"></pre>
</body>
</html>