Bug 736688 - Part 1: Fix up browser frame tests. r=smaug
authorJustin Lebar <justin.lebar@gmail.com>
Wed, 28 Mar 2012 11:36:01 -0700
changeset 90549 8e1f2165d52ec92e8d350ed66867c7fdff395a3f
parent 90548 a313f75c607c1c15c743038ff18caa6c2393a90a
child 90550 e69f1c7562aa9b5eea20b976dad20e7abafdba3e
push idunknown
push userunknown
push dateunknown
reviewerssmaug
bugs736688
milestone14.0a1
Bug 736688 - Part 1: Fix up browser frame tests. r=smaug
dom/tests/mochitest/general/test_browserFrame4.html
dom/tests/mochitest/general/test_browserFrame5.html
dom/tests/mochitest/general/test_browserFrame6.html
dom/tests/mochitest/general/test_browserFrame7.html
--- a/dom/tests/mochitest/general/test_browserFrame4.html
+++ b/dom/tests/mochitest/general/test_browserFrame4.html
@@ -16,86 +16,141 @@ https://bugzilla.mozilla.org/show_bug.cg
   Test that an iframe with the |mozbrowser| attribute emits
   mozbrowserX events when this page is in the whitelist.
 -->
 
 <script type="application/javascript;version=1.7">
 
 SimpleTest.waitForExplicitFinish();
 
-var seenLoadStart = false;
-var seenLoad = false;
-var seenLoadEnd = false;
-var seenLocationChange = false;
-
 function runTest() {
   browserFrameHelpers.setEnabledPref(true);
   browserFrameHelpers.addToWhitelist();
 
-  // Load example.org into the iframe, wait for that to load, then call
-  // runTest2.  This would *almost* work if we just had a <iframe mozbrowser>
-  // in the HTML, except that we have to set the prefs before we create the
-  // iframe!
+  // Load example.org into the iframe, wait for that to finish loading, then
+  // call runTest2.
+  //
+  // This should trigger loadstart, load, and loadend events, but not a
+  // locationchange, because this is the initial load into the iframe.
+
+  var seenLoad = false;
+  var seenLoadEnd = false;
+  var seenLoadStart = false;
 
   var iframe = document.createElement('iframe');
+  iframe.mozbrowser = true;
   iframe.id = 'iframe';
+  iframe.src = 'http://example.org';
+
+  function loadstart(e) {
+    ok(e.isTrusted, 'Event should be trusted.');
+    ok(!seenLoad, 'loadstart before load.');
+    ok(!seenLoadEnd, 'loadstart before loadend.');
+    ok(!seenLoadStart, 'Just one loadstart event.');
+    seenLoadStart = true;
+  }
+
+  function locationchange(e) {
+    ok(e.isTrusted, 'Event should be trusted.');
+    ok(false, 'Unexpected locationchange.');
+  }
+
+  function loadend(e) {
+    ok(e.isTrusted, 'Event should be trusted.');
+    ok(seenLoadStart, 'loadend after loadstart.');
+    ok(seenLoad, 'loadend after load.');
+    ok(!seenLoadEnd, 'Just one loadend event.');
+    seenLoadEnd = true;
+  }
+
+  function load(e) {
+    ok(e.isTrusted, 'Event should be trusted.');
+    ok(seenLoadStart, 'load after loadstart.');
+    ok(!seenLoad, 'Just one load event.');
+    ok(!seenLoadEnd, 'load before loadend.');
+    seenLoad = true;
+  }
+
+  iframe.addEventListener('mozbrowserloadstart', loadstart);
+  iframe.addEventListener('mozbrowserlocationchange', locationchange);
+  iframe.addEventListener('mozbrowserloadend', loadend);
+  iframe.addEventListener('load', load);
+
+  function waitForAllCallbacks() {
+    if (!seenLoadStart || !seenLoad || !seenLoadEnd) {
+      SimpleTest.executeSoon(waitForAllCallbacks);
+      return;
+    }
+
+    iframe.removeEventListener('mozbrowserloadstart', loadstart);
+    iframe.removeEventListener('mozbrowserlocationchange', locationchange);
+    iframe.removeEventListener('mozbrowserloadend', loadend);
+    iframe.removeEventListener('load', load);
+    runTest2();
+  }
+
   document.body.appendChild(iframe);
-  iframe.src = 'data:text/html,1';
-  iframe.addEventListener('load', function() {
-    iframe.removeEventListener('load', arguments.callee);
-    SimpleTest.executeSoon(runTest2);
-  });
+  waitForAllCallbacks();
 }
 
 function runTest2() {
+  var seenLoadStart = false;
+  var seenLoad = false;
+  var seenLoadEnd = false;
+  var seenLocationChange = false;
+
   var iframe = document.getElementById('iframe');
-  iframe.mozbrowser = true;
-  iframe.addEventListener('mozbrowserloadstart', function() {
+  iframe.addEventListener('mozbrowserloadstart', function(e) {
+    ok(e.isTrusted, 'Event should be trusted.');
     ok(!seenLoadStart, 'Just one loadstart event.');
     seenLoadStart = true;
     ok(!seenLoad, 'Got mozbrowserloadstart event before load.');
     ok(!seenLoadEnd, 'Got mozbrowserloadstart before loadend.');
     ok(!seenLocationChange, 'Got mozbrowserloadstart before locationchange.');
   });
 
   iframe.addEventListener('mozbrowserlocationchange', function(e) {
+    ok(e.isTrusted, 'Event should be trusted.');
     ok(!seenLocationChange, 'Just one locationchange event.');
     seenLocationChange = true;
     ok(seenLoadStart, 'Location change after load start.');
     ok(!seenLoad, 'Location change before load.');
     ok(!seenLoadEnd, 'Location change before load end.');
     ok(e.detail, 'http://example.com', "event's reported location");
   });
 
-  iframe.addEventListener('load', function() {
+  iframe.addEventListener('load', function(e) {
+    ok(e.isTrusted, 'Event should be trusted.');
     ok(!seenLoad, 'Just one load event.');
     seenLoad = true;
     ok(seenLoadStart, 'Load after loadstart.');
     ok(seenLocationChange, 'Load after locationchange.');
     ok(!seenLoadEnd, 'Load before loadend.');
   });
 
-  iframe.addEventListener('mozbrowserloadend', function() {
+  iframe.addEventListener('mozbrowserloadend', function(e) {
+    ok(e.isTrusted, 'Event should be trusted.');
     ok(!seenLoadEnd, 'Just one load end event.');
     seenLoadEnd = true;
     ok(seenLoadStart, 'Load end after load start.');
     ok(seenLocationChange, 'Load end after location change.');
   });
 
   iframe.src = 'http://example.com';
-  waitForAllCallbacks();
-}
 
-function waitForAllCallbacks() {
-  if (!seenLoadStart || !seenLoad || !seenLoadEnd || !seenLocationChange) {
-    SimpleTest.executeSoon(waitForAllCallbacks);
-    return;
+  function waitForAllCallbacks() {
+    if (!seenLoadStart || !seenLoad || !seenLoadEnd || !seenLocationChange) {
+      SimpleTest.executeSoon(waitForAllCallbacks);
+      return;
+    }
+
+    SimpleTest.finish();
   }
 
-  SimpleTest.finish();
+  waitForAllCallbacks();
 }
 
 addEventListener('load', function() { SimpleTest.executeSoon(runTest); });
 
 </script>
 
 </body>
 </html>
--- a/dom/tests/mochitest/general/test_browserFrame5.html
+++ b/dom/tests/mochitest/general/test_browserFrame5.html
@@ -20,35 +20,35 @@ https://bugzilla.mozilla.org/show_bug.cg
 
 SimpleTest.waitForExplicitFinish();
 
 function runTest() {
   browserFrameHelpers.setEnabledPref(true);
   browserFrameHelpers.addToWhitelist();
 
   var iframe1 = document.createElement('iframe');
-  document.body.appendChild(iframe1);
+  iframe1.mozbrowser = true;
   iframe1.id = 'iframe1';
   iframe1.addEventListener('load', function() {
     iframe1.removeEventListener('load', arguments.callee);
     SimpleTest.executeSoon(runTest2);
   });
   iframe1.src = 'http://example.org';
+  document.body.appendChild(iframe1);
 }
   
 function runTest2() {
   var iframe1 = document.getElementById('iframe1');
-  iframe1.mozbrowser = true;
-
   var iframe2 = document.getElementById('iframe2');
 
   var sawLoad = false;
   var sawLocationChange = false;
 
   iframe1.addEventListener('mozbrowserlocationchange', function(e) {
+    ok(e.isTrusted, 'Event should be trusted.');
     ok(!sawLocationChange, 'Just one locationchange event.');
     ok(!sawLoad, 'locationchange before load.');
     is(e.detail, 'data:text/html,1', "event's reported location");
     sawLocationChange = true;
   });
 
   iframe1.addEventListener('load', function() {
     ok(sawLocationChange, 'Load after locationchange.');
--- a/dom/tests/mochitest/general/test_browserFrame6.html
+++ b/dom/tests/mochitest/general/test_browserFrame6.html
@@ -31,16 +31,21 @@ function runTest() {
 
   // iframe2 is a red herring; we modify its title but don't listen for
   // titlechanges; we want to make sure that its titlechange events aren't
   // picked up by the listener on iframe1.
   var iframe2 = document.createElement('iframe');
   iframe2.mozbrowser = true;
   document.body.appendChild(iframe2);
 
+  // iframe3 is another red herring.  It's not a mozbrowser, so we shouldn't
+  // get any titlechange events on it.
+  var iframe3 = document.createElement('iframe');
+  document.body.appendChild(iframe3);
+
   var numTitleChanges = 0;
 
   iframe1.addEventListener('mozbrowsertitlechange', function(e) {
     numTitleChanges++;
 
     if (numTitleChanges == 1) {
       is(e.detail, 'Title');
       iframe1.contentDocument.title = 'New title';
@@ -54,18 +59,23 @@ function runTest() {
       is(e.detail, 'Title 3');
       SimpleTest.finish();
     }
     else {
       ok(false, 'Too many titlechange events.');
     }
   });
 
+  iframe3.addEventListener('mozbrowsertitlechange', function(e) {
+    ok(false, 'Should not get a titlechange event for iframe3.');
+  });
+
   iframe1.src = 'data:text/html,<html><head><title>Title</title></head><body></body></html>';
   iframe2.src = 'data:text/html,<html><head><title>BAD TITLE</title></head><body></body></html>';
+  iframe3.src = 'data:text/html,<html><head><title>SHOULD NOT GET EVENT</title></head><body></body></html>';
 }
 
 addEventListener('load', function() { SimpleTest.executeSoon(runTest); });
 
 </script>
 
 </body>
 </html>
--- a/dom/tests/mochitest/general/test_browserFrame7.html
+++ b/dom/tests/mochitest/general/test_browserFrame7.html
@@ -21,28 +21,37 @@ https://bugzilla.mozilla.org/show_bug.cg
 
 <script type="application/javascript;version=1.7">
 "use strict";
 
 browserFrameHelpers.setEnabledPref(true);
 browserFrameHelpers.addToWhitelist();
 
 var iframe = document.createElement('iframe');
+iframe.addEventListener('load', function() {
+  outerIframeLoaded();
+});
 iframe.mozbrowser = true;
+iframe.src = 'data:text/html,Outer iframe';
 document.body.appendChild(iframe);
 
-var innerIframe = document.createElement('iframe');
-iframe.contentDocument.body.appendChild(innerIframe);
+SimpleTest.waitForExplicitFinish();
 
-var iframeCw = iframe.contentWindow;
-var innerCw = innerIframe.contentWindow;
+function outerIframeLoaded() {
+  var innerIframe = iframe.contentDocument.createElement('iframe');
+  iframe.contentDocument.body.appendChild(innerIframe);
+
+  var iframeCw = iframe.contentWindow;
+  var innerCw = innerIframe.contentWindow;
 
-is(iframeCw.top, iframeCw, 'iframe top');
-is(iframeCw.parent, iframeCw, 'iframe parent');
-is(iframeCw.frameElement, null, 'iframe frameElement');
+  is(iframeCw.top, iframeCw, 'iframe top');
+  is(iframeCw.parent, iframeCw, 'iframe parent');
+  is(iframeCw.frameElement, null, 'iframe frameElement');
 
-is(innerCw.top, iframeCw, 'inner iframe top');
-is(innerCw.parent, iframeCw, 'inner iframe parent');
-is(innerCw.frameElement, innerIframe, 'inner iframe frameElement');
+  is(innerCw.top, iframeCw, 'inner iframe top');
+  is(innerCw.parent, iframeCw, 'inner iframe parent');
+  is(innerCw.frameElement, innerIframe, 'inner iframe frameElement');
+  SimpleTest.finish();
+}
 
 </script>
 </body>
 </html>