michael@0: var gExpectedCookies; michael@0: var gExpectedLoads; michael@0: michael@0: var gPopup; michael@0: michael@0: var gLoads = 0; michael@0: michael@0: function setupTest(uri, cookies, loads) { michael@0: SimpleTest.waitForExplicitFinish(); michael@0: michael@0: SpecialPowers.Cc["@mozilla.org/preferences-service;1"] michael@0: .getService(SpecialPowers.Ci.nsIPrefBranch) michael@0: .setIntPref("network.cookie.cookieBehavior", 1); michael@0: michael@0: var cs = SpecialPowers.Cc["@mozilla.org/cookiemanager;1"] michael@0: .getService(SpecialPowers.Ci.nsICookieManager2); michael@0: cs.removeAll(); michael@0: michael@0: gExpectedCookies = cookies; michael@0: gExpectedLoads = loads; michael@0: michael@0: // Listen for MessageEvents. michael@0: window.addEventListener("message", messageReceiver, false); michael@0: michael@0: // load a window which contains an iframe; each will attempt to set michael@0: // cookies from their respective domains. michael@0: gPopup = window.open(uri, 'hai', 'width=100,height=100'); michael@0: } michael@0: michael@0: function finishTest() michael@0: { michael@0: SpecialPowers.Cc["@mozilla.org/preferences-service;1"] michael@0: .getService(SpecialPowers.Ci.nsIPrefBranch) michael@0: .clearUserPref("network.cookie.cookieBehavior"); michael@0: michael@0: SimpleTest.finish(); michael@0: } michael@0: michael@0: /** Receives MessageEvents to this window. */ michael@0: // Count and check loads. michael@0: function messageReceiver(evt) michael@0: { michael@0: is(evt.data, "message", "message data received from popup"); michael@0: if (evt.data != "message") { michael@0: gPopup.close(); michael@0: window.removeEventListener("message", messageReceiver, false); michael@0: michael@0: finishTest(); michael@0: return; michael@0: } michael@0: michael@0: // only run the test when all our children are done loading & setting cookies michael@0: if (++gLoads == gExpectedLoads) { michael@0: gPopup.close(); michael@0: window.removeEventListener("message", messageReceiver, false); michael@0: michael@0: runTest(); michael@0: } michael@0: } michael@0: michael@0: // runTest() is run by messageReceiver(). michael@0: // Count and check cookies. michael@0: function runTest() { michael@0: // set a cookie from a domain of "localhost" michael@0: document.cookie = "oh=hai"; michael@0: michael@0: var cs = SpecialPowers.Cc["@mozilla.org/cookiemanager;1"] michael@0: .getService(SpecialPowers.Ci.nsICookieManager); michael@0: var count = 0; michael@0: for(var list = cs.enumerator; list.hasMoreElements(); list.getNext()) michael@0: ++count; michael@0: is(count, gExpectedCookies, "total number of cookies"); michael@0: cs.removeAll(); michael@0: michael@0: finishTest(); michael@0: }