author Mitchell Field <>
Sat, 05 Jun 2010 21:18:12 -0400
changeset 43144 2d90590dabe63ad9d6323376e6a1138add81e1cb
parent 37368 c922c6402f7631b27cabb2ba769da5c880c1a43a
child 58340 50eb584944aa3fcf02d17726623f29db67833f26
permissions -rw-r--r--
Bug 542222 - Reduce recursion in netwerk makefiles.

  <title>Test for Bug 385434</title>
  <script type="application/javascript" src="/MochiKit/packed.js"></script>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<a target="_blank" href="">Mozilla Bug 385434</a>
<p id="display"></p>
<div id="content">
  <iframe id="frame" style="height:100px; width:100px; border:0"></iframe>
  <div id="status" style="display: none"></div>
<pre id="test">
<script type="application/javascript;version=1.7">

/** Test for Bug 385434 **/

var gNumHashchanges = 0;
var gCallbackOnIframeLoad = false;
var gCallbackOnHashchange = false;

function statusMsg(msg) {
  var msgElem = document.createElement("p");


function longWait() {
  setTimeout(function() { }, 1000);

// onIframeHashchange, onIframeLoad, and onIframeScroll are all called by the
// content we load into our iframe in order to notify the parent frame of an
// event which was fired.
function onIframeHashchange() {
  if (gCallbackOnHashchange) {
    gCallbackOnHashchange = false;;

function enableHashchangeCallback() {
  gCallbackOnHashchange = true;

function onIframeLoad() {
  if (gCallbackOnIframeLoad) {
    gCallbackOnIframeLoad = false;;

function onIframeScroll() {
  is(gNumHashchanges, 0, "onscroll should fire before onhashchange.");

function enableIframeLoadCallback() {
  gCallbackOnIframeLoad = true;

function noEventExpected(msg) {
  is(gNumHashchanges, 0, msg);

  // Even if there's an error, set gNumHashchanges to 0 so other tests don't
  // fail.
  gNumHashchanges = 0;

function eventExpected(msg) {
  is(gNumHashchanges, 1, msg);

  // Eat up this event, whether the test above was true or not
  gNumHashchanges = 0;

 * The hashchange event is dispatched asynchronously, so if we want to observe
 * it, we have to yield within run_test(), transferring control back to the
 * event loop.
 * When we're expecting our iframe to observe a hashchange event after we poke
 * it, we just yield and wait for onIframeHashchange() to call and
 * wake us up.
 * When we're testing to ensure that the iframe doesn't dispatch a hashchange
 * event, we try to hook onto the iframe's load event.  We call
 * enableIframeLoadCallback(), which causes onIframeLoad() to call
 * upon the next observed load.  After we get our callback, we check that a
 * hashchange didn't occur.
 * We can't always just wait for page load in order to observe that a
 * hashchange didn't happen.  In these cases, we call longWait() and yield
 * until either a hashchange occurs or longWait's callback is scheduled.  This
 * is something of a hack; it's entirely possible that longWait won't wait long
 * enough, and we won't observe what should have been a failure of the test.
 * But it shouldn't happen that good code will randomly *fail* this test.
function run_test() {
   * TEST 1 tests that:
   *    <body onhashchange = ... >  works,
   *    the event is (not) fired at the correct times
  var frame = document.getElementById("frame");
  var frameCw = frame.contentWindow;

  frameCw.document.location = "file_bug385434_1.html";
  // Wait for the iframe to load and for our callback to fire

  noEventExpected("No hashchange expected initially.");

  sendMouseEvent({type: "click"}, "link1", frameCw);
  eventExpected("Clicking link1 should trigger a hashchange.");

  sendMouseEvent({type: "click"}, "link1", frameCw);
  // succeed if a hashchange event wasn't triggered while we were waiting
  noEventExpected("Clicking link1 again should not trigger a hashchange.");

  sendMouseEvent({type: "click"}, "link2", frameCw);
  eventExpected("Clicking link2 should trigger a hashchange.");

  eventExpected("Going back should trigger a hashchange.");

  eventExpected("Going forward should trigger a hashchange.");

  frameCw.window.location.hash = "";
  eventExpected("Changing window.location.hash should trigger a hashchange.");

  // window.location has a trailing '#' right now, so we append "link1", not
  // "#link1".
  frameCw.window.location = frameCw.window.location + "link1";
  eventExpected("Assigning to window.location should trigger a hashchange.");

  // Set up history in the iframe which looks like:
  //   file_bug385434_1.html#link1
  //   file_bug385434_2.html
  //   file_bug385434_1.html#foo      <-- current page
  frameCw.window.location = "file_bug385434_2.html";

  frameCw.window.location = "file_bug385434_1.html#foo";

  // Now when we do history.go(-2) on the frame, it *shouldn't* fire a
  // hashchange.  Although the URIs differ only by their hashes, they belong to
  // two different Documents.
  noEventExpected("Moving between different Documents shouldn't " +
                  "trigger a hashchange.");

   * TEST 2 tests that:
   *     <frameset onhashchange = ... > works,
   *     the event is targeted at the window object
   *     the event's cancelable, bubbles settings are correct
  frameCw.document.location = "file_bug385434_2.html";

  frameCw.document.location = "file_bug385434_2.html#foo";
  eventExpected("frame onhashchange should fire events.");

  // iframe should set gSampleEvent
  is(, frameCw,
     "The hashchange event should be targeted to the window.");
  is(gSampleEvent.type, "hashchange",
     "Event type should be 'hashchange'.");
  is(gSampleEvent.cancelable, false,
     "The hashchange event shouldn't be cancelable.");
  is(gSampleEvent.bubbles, false,
     "The hashchange event shouldn't bubble.");

   * TEST 3 tests that:
   *     hashchange is dispatched if the current document readyState is
   *     not "complete" (bug 504837).
  frameCw.document.location = "file_bug385434_3.html";
  eventExpected("Hashchange should fire even if the document " +
                "hasn't finished loading.");


var gGen = run_test();;