summaryrefslogtreecommitdiffstats
path: root/docshell/test/chrome/bug215405_window.xhtml
blob: ddbe2630b92f056c788decc289f114e664eb8352 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?xml version="1.0"?>

<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<?xml-stylesheet href="chrome://global/skin" type="text/css"?>

<window id="215405Test"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        width="600"
        height="600"
        onload="onLoad();"
        title="215405 test">

  <script type="application/javascript"><![CDATA[
    const {BrowserTestUtils} = ChromeUtils.importESModule(
      "resource://testing-common/BrowserTestUtils.sys.mjs"
    );
    Services.prefs.setBoolPref("browser.navigation.requireUserInteraction", false);

    /* globals SimpleTest, is, isnot, ok */
    var imports = [ "SimpleTest", "is", "isnot", "ok"];
    for (var name of imports) {
      window[name] = window.arguments[0][name];
    }

    const text="MOZILLA";
    const nostoreURI = "http://mochi.test:8888/tests/docshell/test/chrome/" +
                       "215405_nostore.html";
    const nocacheURI = "https://example.com:443/tests/docshell/test/chrome/" +
                       "215405_nocache.html";

    var gBrowser;
    var gTestsIterator;
    var currScrollX = 0;
    var currScrollY = 0;

    function finish() {
      gBrowser.removeEventListener("pageshow", eventListener, true);
      // Work around bug 467960
      let historyPurged;
      if (SpecialPowers.Services.appinfo.sessionHistoryInParent) {
        let history = gBrowser.browsingContext.sessionHistory;
        history.purgeHistory(history.count);
        historyPurged = Promise.resolve();
      } else {
        historyPurged = SpecialPowers.spawn(gBrowser, [], () => {
          let history = docShell.QueryInterface(Ci.nsIWebNavigation).sessionHistory
            .legacySHistory;
          history.purgeHistory(history.count);
        });
      }

      historyPurged.then(_ => {
        window.close();
        window.arguments[0].SimpleTest.finish();
      });
    }

    function onLoad(e) {
      gBrowser = document.getElementById("content");
      gBrowser.addEventListener("pageshow", eventListener, true);

      gTestsIterator = testsIterator();
      nextTest();
    }

    function eventListener(event) {
      setTimeout(nextTest, 0);
    }

    function nextTest() {
      gTestsIterator.next();
    }

    function* testsIterator() {
      // No-store tests
      var testName = "[nostore]";

      // Load a page with a no-store header
      BrowserTestUtils.startLoadingURIString(gBrowser, nostoreURI);
      yield undefined;


      // Now that the page has loaded, amend the form contents
      var form = gBrowser.contentDocument.getElementById("inp");
      form.value = text;

      // Attempt to scroll the page
      var originalXPosition = gBrowser.contentWindow.scrollX;
      var originalYPosition = gBrowser.contentWindow.scrollY;
      var scrollToX = gBrowser.contentWindow.scrollMaxX;
      var scrollToY = gBrowser.contentWindow.scrollMaxY;
      gBrowser.contentWindow.scrollBy(scrollToX, scrollToY);

      // Save the scroll position for future comparison
      currScrollX = gBrowser.contentWindow.scrollX;
      currScrollY = gBrowser.contentWindow.scrollY;
      isnot(currScrollX, originalXPosition,
            testName + " failed to scroll window horizontally");
      isnot(currScrollY, originalYPosition,
            testName + " failed to scroll window vertically");

      // Load a new document into the browser
      var simple = "data:text/html,<html><head><title>test2</title></head>" +
                     "<body>test2</body></html>";
      BrowserTestUtils.startLoadingURIString(gBrowser, simple);
      yield undefined;


      // Now go back in history. First page should not have been cached.
      gBrowser.goBack();
      yield undefined;


      // First uncacheable page will now be reloaded. Check scroll position
      // restored, and form contents not
      is(gBrowser.contentWindow.scrollX, currScrollX, testName +
         " horizontal axis scroll position not correctly restored");
      is(gBrowser.contentWindow.scrollY, currScrollY, testName +
         " vertical axis scroll position not correctly restored");
      var formValue = gBrowser.contentDocument.getElementById("inp").value;
      isnot(formValue, text, testName + " form value incorrectly restored");


      // https no-cache
      testName = "[nocache]";

      // Load a page with a no-cache header. This should not be
      // restricted like no-store (bug 567365)
      BrowserTestUtils.startLoadingURIString(gBrowser, nocacheURI);
      yield undefined;


      // Now that the page has loaded, amend the form contents
      form = gBrowser.contentDocument.getElementById("inp");
      form.value = text;

      // Attempt to scroll the page
      originalXPosition = gBrowser.contentWindow.scrollX;
      originalYPosition = gBrowser.contentWindow.scrollY;
      scrollToX = gBrowser.contentWindow.scrollMaxX;
      scrollToY = gBrowser.contentWindow.scrollMaxY;
      gBrowser.contentWindow.scrollBy(scrollToX, scrollToY);

      // Save the scroll position for future comparison
      currScrollX = gBrowser.contentWindow.scrollX;
      currScrollY = gBrowser.contentWindow.scrollY;
      isnot(currScrollX, originalXPosition,
            testName + " failed to scroll window horizontally");
      isnot(currScrollY, originalYPosition,
            testName + " failed to scroll window vertically");

      BrowserTestUtils.startLoadingURIString(gBrowser, simple);
      yield undefined;


      // Now go back in history to the cached page.
      gBrowser.goBack();
      yield undefined;


      // First page will now be reloaded. Check scroll position
      // and form contents are restored
      is(gBrowser.contentWindow.scrollX, currScrollX, testName +
         " horizontal axis scroll position not correctly restored");
      is(gBrowser.contentWindow.scrollY, currScrollY, testName +
         " vertical axis scroll position not correctly restored");
      formValue = gBrowser.contentDocument.getElementById("inp").value;
      is(formValue, text, testName + " form value not correctly restored");

      Services.prefs.clearUserPref("browser.navigation.requireUserInteraction");
      finish();
    }
  ]]></script>

  <browser type="content" primary="true" flex="1" id="content" src="about:blank"/>
</window>