michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: // Test private browsing mode. michael@0: michael@0: let test_generator = do_run_test(); michael@0: michael@0: function run_test() { michael@0: do_test_pending(); michael@0: do_run_generator(test_generator); michael@0: } michael@0: michael@0: function finish_test() { michael@0: do_execute_soon(function() { michael@0: test_generator.close(); michael@0: do_test_finished(); michael@0: }); michael@0: } michael@0: michael@0: function make_channel(url) { michael@0: var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); michael@0: var chan = ios.newChannel(url, null, null).QueryInterface(Ci.nsIHttpChannel); michael@0: return chan; michael@0: } michael@0: michael@0: function do_run_test() { michael@0: // Set up a profile. michael@0: let profile = do_get_profile(); michael@0: michael@0: // Test with cookies enabled. michael@0: Services.prefs.setIntPref("network.cookie.cookieBehavior", 0); michael@0: michael@0: // Create URIs pointing to foo.com and bar.com. michael@0: let uri1 = NetUtil.newURI("http://foo.com/foo.html"); michael@0: let uri2 = NetUtil.newURI("http://bar.com/bar.html"); michael@0: michael@0: // Set a cookie for host 1. michael@0: Services.cookies.setCookieString(uri1, null, "oh=hai; max-age=1000", null); michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1); michael@0: michael@0: // Enter private browsing mode, set a cookie for host 2, and check the counts. michael@0: var chan1 = make_channel(uri1.spec); michael@0: chan1.QueryInterface(Ci.nsIPrivateBrowsingChannel); michael@0: chan1.setPrivate(true); michael@0: michael@0: var chan2 = make_channel(uri2.spec); michael@0: chan2.QueryInterface(Ci.nsIPrivateBrowsingChannel); michael@0: chan2.setPrivate(true); michael@0: michael@0: Services.cookies.setCookieString(uri2, null, "oh=hai; max-age=1000", chan2); michael@0: do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null); michael@0: do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), "oh=hai"); michael@0: michael@0: // Remove cookies and check counts. michael@0: Services.obs.notifyObservers(null, "last-pb-context-exited", null); michael@0: do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null); michael@0: do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), null); michael@0: michael@0: Services.cookies.setCookieString(uri2, null, "oh=hai; max-age=1000", chan2); michael@0: do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), "oh=hai"); michael@0: michael@0: // Leave private browsing mode and check counts. michael@0: Services.obs.notifyObservers(null, "last-pb-context-exited", null); michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1); michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0); michael@0: michael@0: // Fake a profile change. michael@0: do_close_profile(test_generator); michael@0: yield; michael@0: do_load_profile(); michael@0: michael@0: // Check that the right cookie persisted. michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1); michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0); michael@0: michael@0: // Enter private browsing mode, set a cookie for host 2, and check the counts. michael@0: do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null); michael@0: do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), null); michael@0: Services.cookies.setCookieString(uri2, null, "oh=hai; max-age=1000", chan2); michael@0: do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), "oh=hai"); michael@0: michael@0: // Fake a profile change. michael@0: do_close_profile(test_generator); michael@0: yield; michael@0: do_load_profile(); michael@0: michael@0: // We're still in private browsing mode, but should have a new session. michael@0: // Check counts. michael@0: do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null); michael@0: do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), null); michael@0: michael@0: // Leave private browsing mode and check counts. michael@0: Services.obs.notifyObservers(null, "last-pb-context-exited", null); michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1); michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0); michael@0: michael@0: // Enter private browsing mode. michael@0: michael@0: // Fake a profile change, but wait for async read completion. michael@0: do_close_profile(test_generator); michael@0: yield; michael@0: do_load_profile(test_generator); michael@0: yield; michael@0: michael@0: // We're still in private browsing mode, but should have a new session. michael@0: // Check counts. michael@0: do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null); michael@0: do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), null); michael@0: michael@0: // Leave private browsing mode and check counts. michael@0: Services.obs.notifyObservers(null, "last-pb-context-exited", null); michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1); michael@0: do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0); michael@0: michael@0: finish_test(); michael@0: }