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

<window id="360511Test"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        width="600"
        height="600"
        onload="setTimeout(runTest, 0);"
        title="bug 360511 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);

    function getScrollY()
    {
      return SpecialPowers.spawn(TestWindow.getBrowser(), [], () => {
          return content.scrollY;
        });
    }
    function getLocation()
    {
      return SpecialPowers.spawn(TestWindow.getBrowser(), [], () => {
          return content.location.href;
        });
    }

    ////
    // Bug 360511: Fragment uri's in session history should be restored correctly
    // upon back navigation.
    //
    async function runTest()
    {
      // Case 1: load a page containing a fragment link; the page should be 
      // stored in the bfcache.
      // Case 2: load a page containing a fragment link; the page should NOT 
      // be stored in the bfcache.
      for (var i = 1; i < 3; i++)
      {
        var url = "bug360511_case" + i + ".html";
        await promisePageNavigation( {
          uri: getHttpUrl(url),
          preventBFCache: i != 1
        } );

        // Store the original url for later comparison.
        var originalUrl = TestWindow.getBrowser().currentURI.spec;
        var originalDocLocation = await getLocation();

        // Verify we're at the top of the page.
        is(await getScrollY(), 0, "Page initially has a non-zero scrollY property");

        // Click the on the fragment link in the browser, and use setTimeout 
        // to give the event a chance to be processed.
        await SpecialPowers.spawn(TestWindow.getBrowser(), [], () => {
          var event = content.document.createEvent('MouseEvent');
          event.initMouseEvent("click", true, true, content, 0,
            0, 0, 0, 0,
            false, false, false, false, 0, null);
          content.document.getElementById("link1").dispatchEvent(event);
        });
        await promiseNextPaint();

        // Verify we're no longer at the top of the page.
        await promiseTrue(async function() {
          return await getScrollY() > 0;
        }, 20);

        // Store the fragment url for later comparison.
        var fragmentUrl = TestWindow.getBrowser().currentURI.spec;
        let fragDocLocation = await getLocation();

        // Now navigate to any other page
        var expectedPageTitle = "bug360511 case " + i;
        await promisePageNavigation( {
          uri: getHttpUrl("generic.html"),
          eventsToListenFor: ["pagehide", "pageshow"],
          expectedEvents: [ {type: "pagehide", title: expectedPageTitle,
                             persisted: i == 1},
                            {type: "pageshow"} ],
        } );

        // Go back
        await promisePageNavigation( {
          back: true,
          eventsToListenFor: ["pageshow"],
          expectedEvents: [ {type: "pageshow", title: expectedPageTitle, 
                             persisted: i == 1} ],
        } );

        // Verify the current url is the fragment url
        is(TestWindow.getBrowser().currentURI.spec, fragmentUrl, 
          "current url is not the previous fragment url");
        is(await getLocation(), fragDocLocation,
           "document.location is not the previous fragment url");
        
        // Go back again.  Since we're just going from a fragment url to 
        // parent url, no pageshow event is fired, so don't wait for any 
        // events.  Rather, just wait for the page's scrollY property to
        // change.
        var originalScrollY = await getScrollY();
        doPageNavigation( {
          back: true,
          eventsToListenFor: []
        } );
        await promiseTrue(
          async function() {
            return (await getScrollY() != originalScrollY);
          }, 20);

        // Verify the current url is the original url without fragment
        is(TestWindow.getBrowser().currentURI.spec, originalUrl,
          "current url is not the original url");
        is(await getLocation(), originalDocLocation,
          "document.location is not the original url");
      }
                      
      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" />
</window>