summaryrefslogtreecommitdiffstats
path: root/docshell/test/chrome/bug321671_window.xhtml
blob: 60ab5e80d07b5649ac888b5bf82e40e0ae7d4a6f (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
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>

<window id="321671Test"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        width="600"
        height="600"
        onload="setTimeout(runTest, 0);"
        title="bug 321671 test">

  <script type="application/javascript" src="chrome://mochikit/content/chrome-harness.js" />
  <script type="application/javascript" src="docshell_helpers.js" />
  <script type="application/javascript"><![CDATA[
    Services.prefs.setBoolPref("browser.navigation.requireUserInteraction", false);
  
    // Maximum number of entries in the bfcache for this session history.
    // This number is hardcoded in docshell code.  In the test, we'll
    // navigate through enough pages so that we hit one that's been
    // evicted from the bfcache because it's farther from the current
    // page than this number.
    const MAX_BFCACHE_PAGES = 3;

    ////
    // Bug 321671:  Scroll position should be retained when moving backwards and
    // forwards through pages when bfcache is enabled.
    //
    async function runTest()
    {
      // Variable to hold the scroll positions of the test pages.
      var scrollPositions = [];
      
      // Make sure bfcache is on.
      enableBFCache(true);
      
      // Load enough test pages that so the first one is evicted from the
      // bfcache, scroll down on each page, and save the
      // current scroll position before continuing.  Verify that each
      // page we're navigating away from is initially put into the bfcache.
      for (var i = 0; i <= MAX_BFCACHE_PAGES + 1; i++) {
        let eventsToListenFor = ["pageshow"];
        let expectedEvents = [ { type: "pageshow",
                                 title: "bug321671 page" + (i + 1) } ];
        if (i > 0) {
          eventsToListenFor.push("pagehide");
          expectedEvents.unshift({ type: "pagehide",
                                   persisted: true,
                                   title: "bug321671 page" + i });
        }
        await promisePageNavigation( {
           uri: "data:text/html,<html><head><title>bug321671 page" + (i + 1) +
                "</title></head>" +
                "<body><table border='1' width='300' height='1000'>" +
                "<tbody><tr><td>" +
                " page " + (i + 1) + ": foobar foobar foobar foobar " +
                "</td></tr></tbody></table> " +
                "</body></html>",
           eventsToListenFor,
           expectedEvents,
          } );

        let { initialScrollY, scrollY } = await SpecialPowers.spawn(TestWindow.getBrowser(), [i], (i) => {
          let initialScrollY = content.scrollY;
          content.scrollByLines(10 + (2 * i));
          return { initialScrollY, scrollY: content.scrollY };
        });
        is(initialScrollY, 0,
          "Page initially has non-zero scrollY position");
        ok(scrollY > 0,
          "Page has zero scrollY position after scrolling");
        scrollPositions[i] = scrollY;
      }
      
      // Go back to the first page, one page at a time.  For each 'back' 
      // action, verify that its vertical scroll position is restored 
      // correctly.  Verify that the last page in the sequence
      // does not come from the bfcache.  Again verify that all pages 
      // that we navigate away from are initially
      // stored in the bfcache.
      for (i = MAX_BFCACHE_PAGES + 1; i > 0; i--) {
        await promisePageNavigation( {
          back: true,
          eventsToListenFor: ["pageshow", "pagehide"],
          expectedEvents: [ { type: "pagehide",
                              title: "bug321671 page" + (i+1),
                              persisted: true }, 
                            { type: "pageshow", 
                              title: "bug321671 page" + i,
                              persisted: i > 1 } ],
        } );

        is(await SpecialPowers.spawn(TestWindow.getBrowser(), [], () => {
             return content.scrollY;
           }), scrollPositions[i-1],
           "Scroll position not restored while going back!");
      }
      
      // Traverse history forward now, and verify scroll position is still 
      // restored.  Similar to the backward traversal, verify that all
      // but the last page in the sequence comes from the bfcache.  Also 
      // verify that all of the pages get stored in the bfcache when we 
      // navigate away from them.
      for (i = 1; i <= MAX_BFCACHE_PAGES + 1; i++) {
        await promisePageNavigation( {
          forward: true,
          eventsToListenFor: ["pageshow", "pagehide"],
          expectedEvents: [ { type: "pagehide", 
                              persisted: true,
                              title: "bug321671 page" + i },
                            { type: "pageshow", 
                              persisted: i < MAX_BFCACHE_PAGES + 1,
                              title: "bug321671 page" + (i + 1) } ],
        } );

        is(await SpecialPowers.spawn(TestWindow.getBrowser(), [], () => {
             return content.scrollY;
           }), scrollPositions[i],
           "Scroll position not restored while going forward!");
      }

      Services.prefs.clearUserPref("browser.navigation.requireUserInteraction");
      // Tell the framework the test is finished.
      finish();
    }

  ]]></script>

  <browser type="content" primary="true" flex="1" id="content" remote="true" maychangeremoteness="true" />
</window>