Bug 450450 - mochitest-plain: test_loadflags.html intermittently fails; (Bv1a) Make it more serial, explicit and documented; r=dwitte

This commit is contained in:
Serge Gautherie 2009-08-07 01:43:53 +02:00
parent c5699560da
commit dd75783c9f
4 changed files with 59 additions and 54 deletions

View File

@ -7,7 +7,7 @@
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>

View File

@ -1,12 +1,13 @@
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"]
@ -20,36 +21,39 @@ function setupTest(uri, cookies, loads) {
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";
@ -58,15 +62,11 @@ function runTest() {
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();
}

View File

@ -1,16 +1,17 @@
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 gLoads = 0;
var gHeaders = 0;
var gLoads = 0;
var o = null;
// setupTest() is run from 'onload='.
function setupTest(uri, domain, cookies, loads, headers) {
SimpleTest.waitForExplicitFinish();
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
Components.classes["@mozilla.org/preferences-service;1"]
@ -21,26 +22,29 @@ function setupTest(uri, domain, cookies, loads, headers) {
.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 = {
@ -49,11 +53,11 @@ obs.prototype = {
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()
@ -66,52 +70,48 @@ obs.prototype = {
}
}
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();
}

View File

@ -6,6 +6,11 @@
<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">