docshell/test/test_bug529119-1.html
author Boris Zbarsky <bzbarsky@mit.edu>
Mon, 04 Aug 2014 01:25:30 -0400
changeset 217382 48788ecea2c6fd740386b5413f8a308e14f7b8a5
parent 107892 fb0d1d0a683763cbce3fad6a312684cefbf6f058
child 246964 fa8bd3194aca6a90360994c17869845f7b7da5ac
permissions -rw-r--r--
Bug 1045096 - Make sure initial about:blank in iframes have a nsDOMNavigationTiming object, so we don't end up with window.performance throwing when accessed on their window. r=smaug, a=sledru

<!DOCTYPE HTML>
<html>
<head>
<title>Test bug 529119</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>        
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />

<script class="testbody" type="text/javascript">

SimpleTest.waitForExplicitFinish();

var workingURL = "http://mochi.test:8888/tests/docshell/test/bug529119-window.html";
var faultyURL = "http://some-nonexistent-domain-27489274c892748217cn2384.com/";

var w = null;
var phase = 0;
var gotWrongPageOnTryAgainClick = false;

function pollForPage(f, w)
{
  // Start with polling after a delay, we might mistakenly take the current page
  // as an expected one.
  window.setTimeout(function() {
    var iterationsLeft = 200;
    var int = window.setInterval(function() {
      iterationsLeft--;

      var haveErrorPage = false;
      try {
        var title = w.document.title;
      }
      catch (ex) {
        haveErrorPage = true;
      }
  
      if (iterationsLeft == 0 || haveErrorPage) {
        window.clearInterval(int);
        f(iterationsLeft > 0);
      }
    }, 100);
  }, 1000);
}

function windowLoaded()
{
  switch (phase)
  {
    case 0:
      /* 2. We have succeededfully loaded a page, now go to a faulty URL */
      window.setTimeout(function() {
        w.location.href = faultyURL;
      }, 0);
    
      phase = 1;

      pollForPage(function(succeeded) {
        ok(succeeded, "Waiting for error page succeeded");
        
        /* 3. now, while we are on the error page, try to reload it, actually 
           click the "Try Again" button */
        SpecialPowers.wrap(w).location.reload();

        pollForPage(function(succeeded) {
          ok(succeeded, "Waiting for error page succeeded");
          
          /* 4-finish, check we are still on the error page */
          is(SpecialPowers.wrap(w).location.href, faultyURL, "Is on an error page");
          isnot(SpecialPowers.wrap(w).location.href, workingURL, "Is not on the previous page");
          is(gotWrongPageOnTryAgainClick, false, 
            "Must not get www.example.com page on reload of an error page");
          w.close();
          SimpleTest.finish();
        }, w);
      }, w);
      break;
      
    case 1:
      /* 4-check, we must not get here! */
      gotWrongPageOnTryAgainClick = true;
      break;
  }             
}

function startTest()
{
  /* 1. load a URL that leads to an error page */
  w = window.open(workingURL);
}

</script>
</head>
<body onload="startTest();">
</body>
</html>