Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | // Test private browsing mode. |
michael@0 | 5 | |
michael@0 | 6 | let test_generator = do_run_test(); |
michael@0 | 7 | |
michael@0 | 8 | function run_test() { |
michael@0 | 9 | do_test_pending(); |
michael@0 | 10 | do_run_generator(test_generator); |
michael@0 | 11 | } |
michael@0 | 12 | |
michael@0 | 13 | function finish_test() { |
michael@0 | 14 | do_execute_soon(function() { |
michael@0 | 15 | test_generator.close(); |
michael@0 | 16 | do_test_finished(); |
michael@0 | 17 | }); |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | function make_channel(url) { |
michael@0 | 21 | var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); |
michael@0 | 22 | var chan = ios.newChannel(url, null, null).QueryInterface(Ci.nsIHttpChannel); |
michael@0 | 23 | return chan; |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | function do_run_test() { |
michael@0 | 27 | // Set up a profile. |
michael@0 | 28 | let profile = do_get_profile(); |
michael@0 | 29 | |
michael@0 | 30 | // Test with cookies enabled. |
michael@0 | 31 | Services.prefs.setIntPref("network.cookie.cookieBehavior", 0); |
michael@0 | 32 | |
michael@0 | 33 | // Create URIs pointing to foo.com and bar.com. |
michael@0 | 34 | let uri1 = NetUtil.newURI("http://foo.com/foo.html"); |
michael@0 | 35 | let uri2 = NetUtil.newURI("http://bar.com/bar.html"); |
michael@0 | 36 | |
michael@0 | 37 | // Set a cookie for host 1. |
michael@0 | 38 | Services.cookies.setCookieString(uri1, null, "oh=hai; max-age=1000", null); |
michael@0 | 39 | do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1); |
michael@0 | 40 | |
michael@0 | 41 | // Enter private browsing mode, set a cookie for host 2, and check the counts. |
michael@0 | 42 | var chan1 = make_channel(uri1.spec); |
michael@0 | 43 | chan1.QueryInterface(Ci.nsIPrivateBrowsingChannel); |
michael@0 | 44 | chan1.setPrivate(true); |
michael@0 | 45 | |
michael@0 | 46 | var chan2 = make_channel(uri2.spec); |
michael@0 | 47 | chan2.QueryInterface(Ci.nsIPrivateBrowsingChannel); |
michael@0 | 48 | chan2.setPrivate(true); |
michael@0 | 49 | |
michael@0 | 50 | Services.cookies.setCookieString(uri2, null, "oh=hai; max-age=1000", chan2); |
michael@0 | 51 | do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null); |
michael@0 | 52 | do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), "oh=hai"); |
michael@0 | 53 | |
michael@0 | 54 | // Remove cookies and check counts. |
michael@0 | 55 | Services.obs.notifyObservers(null, "last-pb-context-exited", null); |
michael@0 | 56 | do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null); |
michael@0 | 57 | do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), null); |
michael@0 | 58 | |
michael@0 | 59 | Services.cookies.setCookieString(uri2, null, "oh=hai; max-age=1000", chan2); |
michael@0 | 60 | do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), "oh=hai"); |
michael@0 | 61 | |
michael@0 | 62 | // Leave private browsing mode and check counts. |
michael@0 | 63 | Services.obs.notifyObservers(null, "last-pb-context-exited", null); |
michael@0 | 64 | do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1); |
michael@0 | 65 | do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0); |
michael@0 | 66 | |
michael@0 | 67 | // Fake a profile change. |
michael@0 | 68 | do_close_profile(test_generator); |
michael@0 | 69 | yield; |
michael@0 | 70 | do_load_profile(); |
michael@0 | 71 | |
michael@0 | 72 | // Check that the right cookie persisted. |
michael@0 | 73 | do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1); |
michael@0 | 74 | do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0); |
michael@0 | 75 | |
michael@0 | 76 | // Enter private browsing mode, set a cookie for host 2, and check the counts. |
michael@0 | 77 | do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null); |
michael@0 | 78 | do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), null); |
michael@0 | 79 | Services.cookies.setCookieString(uri2, null, "oh=hai; max-age=1000", chan2); |
michael@0 | 80 | do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), "oh=hai"); |
michael@0 | 81 | |
michael@0 | 82 | // Fake a profile change. |
michael@0 | 83 | do_close_profile(test_generator); |
michael@0 | 84 | yield; |
michael@0 | 85 | do_load_profile(); |
michael@0 | 86 | |
michael@0 | 87 | // We're still in private browsing mode, but should have a new session. |
michael@0 | 88 | // Check counts. |
michael@0 | 89 | do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null); |
michael@0 | 90 | do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), null); |
michael@0 | 91 | |
michael@0 | 92 | // Leave private browsing mode and check counts. |
michael@0 | 93 | Services.obs.notifyObservers(null, "last-pb-context-exited", null); |
michael@0 | 94 | do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1); |
michael@0 | 95 | do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0); |
michael@0 | 96 | |
michael@0 | 97 | // Enter private browsing mode. |
michael@0 | 98 | |
michael@0 | 99 | // Fake a profile change, but wait for async read completion. |
michael@0 | 100 | do_close_profile(test_generator); |
michael@0 | 101 | yield; |
michael@0 | 102 | do_load_profile(test_generator); |
michael@0 | 103 | yield; |
michael@0 | 104 | |
michael@0 | 105 | // We're still in private browsing mode, but should have a new session. |
michael@0 | 106 | // Check counts. |
michael@0 | 107 | do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null); |
michael@0 | 108 | do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), null); |
michael@0 | 109 | |
michael@0 | 110 | // Leave private browsing mode and check counts. |
michael@0 | 111 | Services.obs.notifyObservers(null, "last-pb-context-exited", null); |
michael@0 | 112 | do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1); |
michael@0 | 113 | do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0); |
michael@0 | 114 | |
michael@0 | 115 | finish_test(); |
michael@0 | 116 | } |