browser/components/sessionstore/test/browser_privatetabs.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 /* Any copyright is dedicated to the Public Domain.
michael@0 2 * http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 let Imports = {};
michael@0 5 Cu.import("resource:///modules/sessionstore/SessionSaver.jsm", Imports);
michael@0 6 let {SessionSaver} = Imports;
michael@0 7
michael@0 8 add_task(function cleanup() {
michael@0 9 info("Forgetting closed tabs");
michael@0 10 while (ss.getClosedTabCount(window)) {
michael@0 11 ss.forgetClosedTab(window, 0);
michael@0 12 }
michael@0 13 });
michael@0 14
michael@0 15 add_task(function() {
michael@0 16 let URL_PUBLIC = "http://example.com/public/" + Math.random();
michael@0 17 let URL_PRIVATE = "http://example.com/private/" + Math.random();
michael@0 18 let tab1, tab2;
michael@0 19 try {
michael@0 20 // Setup a public tab and a private tab
michael@0 21 info("Setting up public tab");
michael@0 22 tab1 = gBrowser.addTab(URL_PUBLIC);
michael@0 23 yield promiseBrowserLoaded(tab1.linkedBrowser);
michael@0 24
michael@0 25 info("Setting up private tab");
michael@0 26 tab2 = gBrowser.addTab();
michael@0 27 yield promiseBrowserLoaded(tab2.linkedBrowser);
michael@0 28 yield setUsePrivateBrowsing(tab2.linkedBrowser, true);
michael@0 29 tab2.linkedBrowser.loadURI(URL_PRIVATE);
michael@0 30 yield promiseBrowserLoaded(tab2.linkedBrowser);
michael@0 31
michael@0 32 info("Flush to make sure chrome received all data.");
michael@0 33 SyncHandlers.get(tab1.linkedBrowser).flush();
michael@0 34 SyncHandlers.get(tab2.linkedBrowser).flush();
michael@0 35
michael@0 36 info("Checking out state");
michael@0 37 yield SessionSaver.run();
michael@0 38 let path = OS.Path.join(OS.Constants.Path.profileDir, "sessionstore.js");
michael@0 39 let data = yield OS.File.read(path);
michael@0 40 let state = new TextDecoder().decode(data);
michael@0 41 info("State: " + state);
michael@0 42 // Ensure that sessionstore.js only knows about the public tab
michael@0 43 ok(state.indexOf(URL_PUBLIC) != -1, "State contains public tab");
michael@0 44 ok(state.indexOf(URL_PRIVATE) == -1, "State does not contain private tab");
michael@0 45
michael@0 46 // Ensure that we can close and undo close the public tab but not the private tab
michael@0 47 gBrowser.removeTab(tab2);
michael@0 48 tab2 = null;
michael@0 49
michael@0 50 gBrowser.removeTab(tab1);
michael@0 51 tab1 = null;
michael@0 52
michael@0 53 tab1 = ss.undoCloseTab(window, 0);
michael@0 54 ok(true, "Public tab supports undo close");
michael@0 55
michael@0 56 is(ss.getClosedTabCount(window), 0, "Private tab does not support undo close");
michael@0 57
michael@0 58 } finally {
michael@0 59 if (tab1) {
michael@0 60 gBrowser.removeTab(tab1);
michael@0 61 }
michael@0 62 if (tab2) {
michael@0 63 gBrowser.removeTab(tab2);
michael@0 64 }
michael@0 65 }
michael@0 66 });
michael@0 67
michael@0 68 add_task(function () {
michael@0 69 const FRAME_SCRIPT = "data:," +
michael@0 70 "docShell.QueryInterface%28Ci.nsILoadContext%29.usePrivateBrowsing%3Dtrue";
michael@0 71
michael@0 72 // Clear the list of closed windows.
michael@0 73 while (ss.getClosedWindowCount()) {
michael@0 74 ss.forgetClosedWindow(0);
michael@0 75 }
michael@0 76
michael@0 77 // Create a new window to attach our frame script to.
michael@0 78 let win = yield promiseNewWindowLoaded();
michael@0 79 win.messageManager.loadFrameScript(FRAME_SCRIPT, true);
michael@0 80
michael@0 81 // Create a new tab in the new window that will load the frame script.
michael@0 82 let tab = win.gBrowser.addTab("about:mozilla");
michael@0 83 let browser = tab.linkedBrowser;
michael@0 84 yield promiseBrowserLoaded(browser);
michael@0 85 SyncHandlers.get(browser).flush();
michael@0 86
michael@0 87 // Check that we consider the tab as private.
michael@0 88 let state = JSON.parse(ss.getTabState(tab));
michael@0 89 ok(state.isPrivate, "tab considered private");
michael@0 90
michael@0 91 // Ensure we don't allow restoring closed private tabs in non-private windows.
michael@0 92 win.gBrowser.removeTab(tab);
michael@0 93 is(ss.getClosedTabCount(win), 0, "no tabs to restore");
michael@0 94
michael@0 95 // Create a new tab in the new window that will load the frame script.
michael@0 96 let tab = win.gBrowser.addTab("about:mozilla");
michael@0 97 let browser = tab.linkedBrowser;
michael@0 98 yield promiseBrowserLoaded(browser);
michael@0 99 SyncHandlers.get(browser).flush();
michael@0 100
michael@0 101 // Check that we consider the tab as private.
michael@0 102 let state = JSON.parse(ss.getTabState(tab));
michael@0 103 ok(state.isPrivate, "tab considered private");
michael@0 104
michael@0 105 // Check that all private tabs are removed when the non-private
michael@0 106 // window is closed and we don't save windows without any tabs.
michael@0 107 yield promiseWindowClosed(win);
michael@0 108 is(ss.getClosedWindowCount(), 0, "no windows to restore");
michael@0 109 });
michael@0 110
michael@0 111 add_task(function () {
michael@0 112 // Clear the list of closed windows.
michael@0 113 while (ss.getClosedWindowCount()) {
michael@0 114 ss.forgetClosedWindow(0);
michael@0 115 }
michael@0 116
michael@0 117 // Create a new window to attach our frame script to.
michael@0 118 let win = yield promiseNewWindowLoaded({private: true});
michael@0 119
michael@0 120 // Create a new tab in the new window that will load the frame script.
michael@0 121 let tab = win.gBrowser.addTab("about:mozilla");
michael@0 122 let browser = tab.linkedBrowser;
michael@0 123 yield promiseBrowserLoaded(browser);
michael@0 124 SyncHandlers.get(browser).flush();
michael@0 125
michael@0 126 // Check that we consider the tab as private.
michael@0 127 let state = JSON.parse(ss.getTabState(tab));
michael@0 128 ok(state.isPrivate, "tab considered private");
michael@0 129
michael@0 130 // Ensure that closed tabs in a private windows can be restored.
michael@0 131 win.gBrowser.removeTab(tab);
michael@0 132 is(ss.getClosedTabCount(win), 1, "there is a single tab to restore");
michael@0 133
michael@0 134 // Ensure that closed private windows can never be restored.
michael@0 135 yield promiseWindowClosed(win);
michael@0 136 is(ss.getClosedWindowCount(), 0, "no windows to restore");
michael@0 137 });
michael@0 138
michael@0 139 function setUsePrivateBrowsing(browser, val) {
michael@0 140 return sendMessage(browser, "ss-test:setUsePrivateBrowsing", val);
michael@0 141 }
michael@0 142

mercurial