extensions/cookie/test/unit/test_cookies_privatebrowsing.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/extensions/cookie/test/unit/test_cookies_privatebrowsing.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,116 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +// Test private browsing mode.
     1.8 +
     1.9 +let test_generator = do_run_test();
    1.10 +
    1.11 +function run_test() {
    1.12 +  do_test_pending();
    1.13 +  do_run_generator(test_generator);
    1.14 +}
    1.15 +
    1.16 +function finish_test() {
    1.17 +  do_execute_soon(function() {
    1.18 +    test_generator.close();
    1.19 +    do_test_finished();
    1.20 +  });
    1.21 +}
    1.22 +
    1.23 +function make_channel(url) {
    1.24 +  var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
    1.25 +  var chan = ios.newChannel(url, null, null).QueryInterface(Ci.nsIHttpChannel);
    1.26 +  return chan;
    1.27 +}
    1.28 +
    1.29 +function do_run_test() {
    1.30 +  // Set up a profile.
    1.31 +  let profile = do_get_profile();
    1.32 +
    1.33 +  // Test with cookies enabled.
    1.34 +  Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
    1.35 +
    1.36 +  // Create URIs pointing to foo.com and bar.com.
    1.37 +  let uri1 = NetUtil.newURI("http://foo.com/foo.html");
    1.38 +  let uri2 = NetUtil.newURI("http://bar.com/bar.html");
    1.39 +
    1.40 +  // Set a cookie for host 1.
    1.41 +  Services.cookies.setCookieString(uri1, null, "oh=hai; max-age=1000", null);
    1.42 +  do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1);
    1.43 +
    1.44 +  // Enter private browsing mode, set a cookie for host 2, and check the counts.
    1.45 +  var chan1 = make_channel(uri1.spec);
    1.46 +  chan1.QueryInterface(Ci.nsIPrivateBrowsingChannel);
    1.47 +  chan1.setPrivate(true);
    1.48 +
    1.49 +  var chan2 = make_channel(uri2.spec);
    1.50 +  chan2.QueryInterface(Ci.nsIPrivateBrowsingChannel);
    1.51 +  chan2.setPrivate(true);
    1.52 +
    1.53 +  Services.cookies.setCookieString(uri2, null, "oh=hai; max-age=1000", chan2);
    1.54 +  do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null);
    1.55 +  do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), "oh=hai");
    1.56 +
    1.57 +  // Remove cookies and check counts.
    1.58 +  Services.obs.notifyObservers(null, "last-pb-context-exited", null);
    1.59 +  do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null);
    1.60 +  do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), null);
    1.61 +
    1.62 +  Services.cookies.setCookieString(uri2, null, "oh=hai; max-age=1000", chan2);
    1.63 +  do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), "oh=hai");
    1.64 +
    1.65 +  // Leave private browsing mode and check counts.
    1.66 +  Services.obs.notifyObservers(null, "last-pb-context-exited", null);
    1.67 +  do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1);
    1.68 +  do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0);
    1.69 +
    1.70 +  // Fake a profile change.
    1.71 +  do_close_profile(test_generator);
    1.72 +  yield;
    1.73 +  do_load_profile();
    1.74 +
    1.75 +  // Check that the right cookie persisted.
    1.76 +  do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1);
    1.77 +  do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0);
    1.78 +
    1.79 +  // Enter private browsing mode, set a cookie for host 2, and check the counts.
    1.80 +  do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null);
    1.81 +  do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), null);
    1.82 +  Services.cookies.setCookieString(uri2, null, "oh=hai; max-age=1000", chan2);
    1.83 +  do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), "oh=hai");
    1.84 +
    1.85 +  // Fake a profile change.
    1.86 +  do_close_profile(test_generator);
    1.87 +  yield;
    1.88 +  do_load_profile();
    1.89 +
    1.90 +  // We're still in private browsing mode, but should have a new session.
    1.91 +  // Check counts.
    1.92 +  do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null);
    1.93 +  do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), null);
    1.94 +
    1.95 +  // Leave private browsing mode and check counts.
    1.96 +  Services.obs.notifyObservers(null, "last-pb-context-exited", null);
    1.97 +  do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1);
    1.98 +  do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0);
    1.99 +
   1.100 +  // Enter private browsing mode.
   1.101 +
   1.102 +  // Fake a profile change, but wait for async read completion.
   1.103 +  do_close_profile(test_generator);
   1.104 +  yield;
   1.105 +  do_load_profile(test_generator);
   1.106 +  yield;
   1.107 +
   1.108 +  // We're still in private browsing mode, but should have a new session.
   1.109 +  // Check counts.
   1.110 +  do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null);
   1.111 +  do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), null);
   1.112 +
   1.113 +  // Leave private browsing mode and check counts.
   1.114 +  Services.obs.notifyObservers(null, "last-pb-context-exited", null);
   1.115 +  do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1);
   1.116 +  do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0);
   1.117 +
   1.118 +  finish_test();
   1.119 +}

mercurial