Source code
Revision control
Copy as Markdown
Other Tools
<?xml version="1.0"?>
<window id="360511Test"
        width="600"
        height="600"
        onload="setTimeout(runTest, 0);"
  <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;
        });
    }
    ////
    // 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
        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" maychangeremoteness="true" />
</window>