Bug 450450 - mochitest-plain: test_loadflags.html intermittently fails; (Bv1a) Make it more serial, explicit and documented; r=dwitte
authorSerge Gautherie <sgautherie.bz@free.fr>
Fri, 07 Aug 2009 01:43:53 +0200
changeset 31200 9281246909f0227f281b357f21b248d2ae62a9d2
parent 31199 fa557041abb01eb3d97fa3e693b0b963c9d71772
child 31201 631bd1a59dfb9b7e5931df08f6fdee94d35fb21a
push idunknown
push userunknown
push dateunknown
reviewersdwitte
bugs450450
milestone1.9.2a1pre
Bug 450450 - mochitest-plain: test_loadflags.html intermittently fails; (Bv1a) Make it more serial, explicit and documented; r=dwitte
extensions/cookie/test/file_loadflags_inner.html
extensions/cookie/test/file_testcommon.js
extensions/cookie/test/file_testloadflags.js
extensions/cookie/test/test_loadflags.html
--- a/extensions/cookie/test/file_loadflags_inner.html
+++ b/extensions/cookie/test/file_loadflags_inner.html
@@ -2,16 +2,16 @@
 <html>
 <head>
   <META HTTP-EQUIV="Set-Cookie" CONTENT="meta=tag">
   <script type="text/javascript">
     function runTest() {
       document.cookie = "can=has";
 
       // send a message to our test document, to say we're done loading
-      window.opener.postMessage("message", "http://localhost:8888");
+      window.opener.postMessage("f_lf_i msg data", "http://localhost:8888");
     }
   </script>
 </head>
 <body>
 <img src="http://example.org/tests/extensions/cookie/test/image1.png" onload="runTest()" />
 </body>
 </html>
--- a/extensions/cookie/test/file_testcommon.js
+++ b/extensions/cookie/test/file_testcommon.js
@@ -1,72 +1,72 @@
-SimpleTest.waitForExplicitFinish();
+var gExpectedCookies;
+var gExpectedLoads;
 
-var gPopup = null;
+var gPopup;
 
-var gExpectedCookies = 0;
-var gExpectedLoads = 0;
 var gLoads = 0;
 
 function setupTest(uri, cookies, loads) {
+  SimpleTest.waitForExplicitFinish();
+
   netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
 
   Components.classes["@mozilla.org/preferences-service;1"]
             .getService(Components.interfaces.nsIPrefBranch)
             .setIntPref("network.cookie.cookieBehavior", 1);
 
   var cs = Components.classes["@mozilla.org/cookiemanager;1"]
                      .getService(Components.interfaces.nsICookieManager2);
   cs.removeAll();
 
   gExpectedCookies = cookies;
   gExpectedLoads = loads;
 
+  // Listen for MessageEvents.
+  window.addEventListener("message", messageReceiver, false);
+
   // load a window which contains an iframe; each will attempt to set
   // cookies from their respective domains.
   gPopup = window.open(uri, 'hai', 'width=100,height=100');
 }
 
-window.addEventListener("message", messageReceiver, false);
 
 /** Receives MessageEvents to this window. */
+// Count and check loads.
 function messageReceiver(evt)
 {
-  ok(evt instanceof MessageEvent, "event type", evt);
-
+  is(evt.data, "message", "message data received from popup");
   if (evt.data != "message") {
+    gPopup.close();
     window.removeEventListener("message", messageReceiver, false);
 
-    ok(false, "message", evt.data);
-
-    gPopup.close();
     SimpleTest.finish();
     return;
   }
 
   // only run the test when all our children are done loading & setting cookies
   if (++gLoads == gExpectedLoads) {
+    gPopup.close();
     window.removeEventListener("message", messageReceiver, false);
 
     runTest();
   }
 }
 
+// runTest() is run by messageReceiver().
+// Count and check cookies.
 function runTest() {
   // set a cookie from a domain of "localhost"
   document.cookie = "oh=hai";
 
   netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
 
   var cs = Components.classes["@mozilla.org/cookiemanager;1"]
                      .getService(Components.interfaces.nsICookieManager);
-  var list = cs.enumerator;
   var count = 0;
-  while (list.hasMoreElements()) {
-    count++;
-    list.getNext();
-  }
-  is(count, gExpectedCookies, "number of cookies");
+  for(var list = cs.enumerator; list.hasMoreElements(); list.getNext())
+    ++count;
+  is(count, gExpectedCookies, "total number of cookies");
   cs.removeAll();
 
-  gPopup.close();
   SimpleTest.finish();
 }
--- a/extensions/cookie/test/file_testloadflags.js
+++ b/extensions/cookie/test/file_testloadflags.js
@@ -1,117 +1,117 @@
-SimpleTest.waitForExplicitFinish();
+var gExpectedCookies;
+var gExpectedHeaders;
+var gExpectedLoads;
 
-var gPopup = null;
+var gObs;
+var gPopup;
 
-var gExpectedCookies = 0;
-var gExpectedLoads = 0;
-var gExpectedHeaders = 0;
+var gHeaders = 0;
 var gLoads = 0;
-var gHeaders = 0;
 
-var o = null;
+// setupTest() is run from 'onload='.
+function setupTest(uri, domain, cookies, loads, headers) {
+  SimpleTest.waitForExplicitFinish();
 
-function setupTest(uri, domain, cookies, loads, headers) {
   netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
 
   Components.classes["@mozilla.org/preferences-service;1"]
             .getService(Components.interfaces.nsIPrefBranch)
             .setIntPref("network.cookie.cookieBehavior", 1);
 
   var cs = Components.classes["@mozilla.org/cookiemanager;1"]
                      .getService(Components.interfaces.nsICookieManager2);
   cs.removeAll();
   cs.add(domain, "", "oh", "hai", false, false, true, Math.pow(2, 62));
-  is(cs.countCookiesFromHost(domain), 1, "cookie inited");
-
-  o = new obs();
+  is(cs.countCookiesFromHost(domain), 1, "number of cookies for domain " + domain);
 
   gExpectedCookies = cookies;
   gExpectedLoads = loads;
   gExpectedHeaders = headers;
 
+  gObs = new obs();
+  // Listen for MessageEvents.
+  window.addEventListener("message", messageReceiver, false);
+
   // load a window which contains an iframe; each will attempt to set
   // cookies from their respective domains.
   gPopup = window.open(uri, 'hai', 'width=100,height=100');
 }
 
+// Count headers.
 function obs () {
   netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
 
+  this.window = window;
   this.os = Components.classes["@mozilla.org/observer-service;1"]
                       .getService(Components.interfaces.nsIObserverService);
   this.os.addObserver(this, "http-on-modify-request", false);
-  this.window = window;
 }
 
 obs.prototype = {
   observe: function obs_observe (theSubject, theTopic, theData)
   {
     this.window.netscape.security
         .PrivilegeManager.enablePrivilege("UniversalXPConnect");
 
-    var cookie = theSubject.QueryInterface(this.window.Components.interfaces
-                                               .nsIHttpChannel)
-                           .getRequestHeader("Cookie");
-    this.window.isnot(cookie.indexOf("oh=hai"), -1, "cookie sent");
-    gHeaders++;
+    var channel = theSubject.QueryInterface(
+                    this.window.Components.interfaces.nsIHttpChannel);
+    this.window.isnot(channel.getRequestHeader("Cookie").indexOf("oh=hai"), -1,
+                      "cookie 'oh=hai' is in header for " + channel.URI.spec);
+    ++gHeaders;
   },
 
   remove: function obs_remove()
   {
     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
 
     this.os.removeObserver(this, "http-on-modify-request");
     this.os = null;
     this.window = null;
   }
 }
 
-window.addEventListener("message", messageReceiver, false);
-
 /** Receives MessageEvents to this window. */
+// Count and check loads.
 function messageReceiver(evt)
 {
-  ok(evt instanceof MessageEvent, "event type", evt);
-
-  if (evt.data != "message") {
+  is(evt.data, "f_lf_i msg data", "message data received from popup");
+  if (evt.data != "f_lf_i msg data") {
+    gPopup.close();
     window.removeEventListener("message", messageReceiver, false);
 
-    ok(false, "message", evt.data);
-
-    o.remove();
-    gPopup.close();
+    gObs.remove();
     SimpleTest.finish();
     return;
   }
 
   // only run the test when all our children are done loading & setting cookies
   if (++gLoads == gExpectedLoads) {
+    gPopup.close();
     window.removeEventListener("message", messageReceiver, false);
 
     runTest();
   }
 }
 
+// runTest() is run by messageReceiver().
+// Check headers, and count and check cookies.
 function runTest() {
   // set a cookie from a domain of "localhost"
   document.cookie = "o=noes";
 
+  gObs.remove();
+
+  is(gHeaders, gExpectedHeaders, "number of observed request headers");
+
   netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
 
   var cs = Components.classes["@mozilla.org/cookiemanager;1"]
                      .getService(Components.interfaces.nsICookieManager);
-  var list = cs.enumerator;
   var count = 0;
-  while (list.hasMoreElements()) {
-    count++;
-    list.getNext();
-  }
-  is(count, gExpectedCookies, "number of cookies");
+  for(var list = cs.enumerator; list.hasMoreElements(); list.getNext())
+    ++count;
+  is(count, gExpectedCookies, "total number of cookies");
   cs.removeAll();
 
-  is(gHeaders, gExpectedHeaders, "number of request headers");
-
-  o.remove();
-  gPopup.close();
   SimpleTest.finish();
 }
--- a/extensions/cookie/test/test_loadflags.html
+++ b/extensions/cookie/test/test_loadflags.html
@@ -1,16 +1,21 @@
 <!DOCTYPE HTML>
 <html>
 <head>
   <title>Test for Cross domain access to properties</title>
   <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>        
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
 </head>
+<!--
+ *5 cookies: 1+1 from file_testloadflags.js, 2 from file_loadflags_inner.html + 1 from image1.png^headers^.
+ *1 load: file_loadflags_inner.html.
+ *2 headers: 1 for file_loadflags_inner.html + 1 for image1.png.
+ -->
 <body onload="setupTest('http://example.org/tests/extensions/cookie/test/file_loadflags_inner.html', 'example.org', 5, 1, 2)">
 <p id="display"></p>
 <pre id="test">
 <script class="testbody" type="text/javascript" src="file_testloadflags.js">
 </script>
 </pre>
 </body>
 </html>