extensions/cookie/test/unit/test_cookies_privatebrowsing.js

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

mercurial