extensions/cookie/test/file_testcommon.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 var gExpectedCookies;
michael@0 2 var gExpectedLoads;
michael@0 3
michael@0 4 var gPopup;
michael@0 5
michael@0 6 var gLoads = 0;
michael@0 7
michael@0 8 function setupTest(uri, cookies, loads) {
michael@0 9 SimpleTest.waitForExplicitFinish();
michael@0 10
michael@0 11 SpecialPowers.Cc["@mozilla.org/preferences-service;1"]
michael@0 12 .getService(SpecialPowers.Ci.nsIPrefBranch)
michael@0 13 .setIntPref("network.cookie.cookieBehavior", 1);
michael@0 14
michael@0 15 var cs = SpecialPowers.Cc["@mozilla.org/cookiemanager;1"]
michael@0 16 .getService(SpecialPowers.Ci.nsICookieManager2);
michael@0 17 cs.removeAll();
michael@0 18
michael@0 19 gExpectedCookies = cookies;
michael@0 20 gExpectedLoads = loads;
michael@0 21
michael@0 22 // Listen for MessageEvents.
michael@0 23 window.addEventListener("message", messageReceiver, false);
michael@0 24
michael@0 25 // load a window which contains an iframe; each will attempt to set
michael@0 26 // cookies from their respective domains.
michael@0 27 gPopup = window.open(uri, 'hai', 'width=100,height=100');
michael@0 28 }
michael@0 29
michael@0 30 function finishTest()
michael@0 31 {
michael@0 32 SpecialPowers.Cc["@mozilla.org/preferences-service;1"]
michael@0 33 .getService(SpecialPowers.Ci.nsIPrefBranch)
michael@0 34 .clearUserPref("network.cookie.cookieBehavior");
michael@0 35
michael@0 36 SimpleTest.finish();
michael@0 37 }
michael@0 38
michael@0 39 /** Receives MessageEvents to this window. */
michael@0 40 // Count and check loads.
michael@0 41 function messageReceiver(evt)
michael@0 42 {
michael@0 43 is(evt.data, "message", "message data received from popup");
michael@0 44 if (evt.data != "message") {
michael@0 45 gPopup.close();
michael@0 46 window.removeEventListener("message", messageReceiver, false);
michael@0 47
michael@0 48 finishTest();
michael@0 49 return;
michael@0 50 }
michael@0 51
michael@0 52 // only run the test when all our children are done loading & setting cookies
michael@0 53 if (++gLoads == gExpectedLoads) {
michael@0 54 gPopup.close();
michael@0 55 window.removeEventListener("message", messageReceiver, false);
michael@0 56
michael@0 57 runTest();
michael@0 58 }
michael@0 59 }
michael@0 60
michael@0 61 // runTest() is run by messageReceiver().
michael@0 62 // Count and check cookies.
michael@0 63 function runTest() {
michael@0 64 // set a cookie from a domain of "localhost"
michael@0 65 document.cookie = "oh=hai";
michael@0 66
michael@0 67 var cs = SpecialPowers.Cc["@mozilla.org/cookiemanager;1"]
michael@0 68 .getService(SpecialPowers.Ci.nsICookieManager);
michael@0 69 var count = 0;
michael@0 70 for(var list = cs.enumerator; list.hasMoreElements(); list.getNext())
michael@0 71 ++count;
michael@0 72 is(count, gExpectedCookies, "total number of cookies");
michael@0 73 cs.removeAll();
michael@0 74
michael@0 75 finishTest();
michael@0 76 }

mercurial