browser/components/sessionstore/test/browser_broadcast.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 "use strict";
michael@0 5
michael@0 6 const INITIAL_VALUE = "browser_broadcast.js-initial-value-" + Date.now();
michael@0 7
michael@0 8 /**
michael@0 9 * This test ensures we won't lose tab data queued in the content script when
michael@0 10 * closing a tab.
michael@0 11 */
michael@0 12 add_task(function flush_on_tabclose() {
michael@0 13 let tab = yield createTabWithStorageData(["http://example.com"]);
michael@0 14 let browser = tab.linkedBrowser;
michael@0 15
michael@0 16 yield modifySessionStorage(browser, {test: "on-tab-close"});
michael@0 17 gBrowser.removeTab(tab);
michael@0 18
michael@0 19 let [{state: {storage}}] = JSON.parse(ss.getClosedTabData(window));
michael@0 20 is(storage["http://example.com"].test, "on-tab-close",
michael@0 21 "sessionStorage data has been flushed on TabClose");
michael@0 22 });
michael@0 23
michael@0 24 /**
michael@0 25 * This test ensures we won't lose tab data queued in the content script when
michael@0 26 * the application tries to quit.
michael@0 27 */
michael@0 28 add_task(function flush_on_quit_requested() {
michael@0 29 let tab = yield createTabWithStorageData(["http://example.com"]);
michael@0 30 let browser = tab.linkedBrowser;
michael@0 31
michael@0 32 yield modifySessionStorage(browser, {test: "on-quit-requested"});
michael@0 33
michael@0 34 // Note that sending quit-application-requested should not interfere with
michael@0 35 // other tests and code. We're just notifying about a shutdown request but
michael@0 36 // we will not send quit-application-granted. Observers will thus assume
michael@0 37 // that some other observer has canceled the request.
michael@0 38 sendQuitApplicationRequested();
michael@0 39
michael@0 40 let {storage} = JSON.parse(ss.getTabState(tab));
michael@0 41 is(storage["http://example.com"].test, "on-quit-requested",
michael@0 42 "sessionStorage data has been flushed when a quit is requested");
michael@0 43
michael@0 44 gBrowser.removeTab(tab);
michael@0 45 });
michael@0 46
michael@0 47 /**
michael@0 48 * This test ensures we won't lose tab data queued in the content script when
michael@0 49 * duplicating a tab.
michael@0 50 */
michael@0 51 add_task(function flush_on_duplicate() {
michael@0 52 let tab = yield createTabWithStorageData(["http://example.com"]);
michael@0 53 let browser = tab.linkedBrowser;
michael@0 54
michael@0 55 yield modifySessionStorage(browser, {test: "on-duplicate"});
michael@0 56 let tab2 = ss.duplicateTab(window, tab);
michael@0 57 let {storage} = JSON.parse(ss.getTabState(tab2));
michael@0 58 is(storage["http://example.com"].test, "on-duplicate",
michael@0 59 "sessionStorage data has been flushed when duplicating tabs");
michael@0 60
michael@0 61 yield promiseTabRestored(tab2);
michael@0 62 gBrowser.removeTab(tab2)
michael@0 63 let [{state: {storage}}] = JSON.parse(ss.getClosedTabData(window));
michael@0 64 is(storage["http://example.com"].test, "on-duplicate",
michael@0 65 "sessionStorage data has been flushed when duplicating tabs");
michael@0 66
michael@0 67 gBrowser.removeTab(tab);
michael@0 68 });
michael@0 69
michael@0 70 /**
michael@0 71 * This test ensures we won't lose tab data queued in the content script when
michael@0 72 * a window is closed.
michael@0 73 */
michael@0 74 add_task(function flush_on_windowclose() {
michael@0 75 let win = yield promiseNewWindow();
michael@0 76 let tab = yield createTabWithStorageData(["http://example.com"], win);
michael@0 77 let browser = tab.linkedBrowser;
michael@0 78
michael@0 79 yield modifySessionStorage(browser, {test: "on-window-close"});
michael@0 80 yield closeWindow(win);
michael@0 81
michael@0 82 let [{tabs: [_, {storage}]}] = JSON.parse(ss.getClosedWindowData());
michael@0 83 is(storage["http://example.com"].test, "on-window-close",
michael@0 84 "sessionStorage data has been flushed when closing a window");
michael@0 85 });
michael@0 86
michael@0 87 /**
michael@0 88 * This test ensures that stale tab data is ignored when reusing a tab
michael@0 89 * (via e.g. setTabState) and does not overwrite the new data.
michael@0 90 */
michael@0 91 add_task(function flush_on_settabstate() {
michael@0 92 let tab = yield createTabWithStorageData(["http://example.com"]);
michael@0 93 let browser = tab.linkedBrowser;
michael@0 94
michael@0 95 // Flush to make sure our tab state is up-to-date.
michael@0 96 SyncHandlers.get(browser).flush();
michael@0 97
michael@0 98 let state = ss.getTabState(tab);
michael@0 99 yield modifySessionStorage(browser, {test: "on-set-tab-state"});
michael@0 100
michael@0 101 // Flush all data contained in the content script but send it using
michael@0 102 // asynchronous messages.
michael@0 103 SyncHandlers.get(browser).flushAsync();
michael@0 104
michael@0 105 ss.setTabState(tab, state);
michael@0 106 yield promiseTabRestored(tab);
michael@0 107
michael@0 108 let {storage} = JSON.parse(ss.getTabState(tab));
michael@0 109 is(storage["http://example.com"].test, INITIAL_VALUE,
michael@0 110 "sessionStorage data has not been overwritten");
michael@0 111
michael@0 112 gBrowser.removeTab(tab);
michael@0 113 });
michael@0 114
michael@0 115 /**
michael@0 116 * This test ensures that we won't lose tab data that has been sent
michael@0 117 * asynchronously just before closing a tab. Flushing must re-send all data
michael@0 118 * that hasn't been received by chrome, yet.
michael@0 119 */
michael@0 120 add_task(function flush_on_tabclose_racy() {
michael@0 121 let tab = yield createTabWithStorageData(["http://example.com"]);
michael@0 122 let browser = tab.linkedBrowser;
michael@0 123
michael@0 124 // Flush to make sure we start with an empty queue.
michael@0 125 SyncHandlers.get(browser).flush();
michael@0 126
michael@0 127 yield modifySessionStorage(browser, {test: "on-tab-close-racy"});
michael@0 128
michael@0 129 // Flush all data contained in the content script but send it using
michael@0 130 // asynchronous messages.
michael@0 131 SyncHandlers.get(browser).flushAsync();
michael@0 132 gBrowser.removeTab(tab);
michael@0 133
michael@0 134 let [{state: {storage}}] = JSON.parse(ss.getClosedTabData(window));
michael@0 135 is(storage["http://example.com"].test, "on-tab-close-racy",
michael@0 136 "sessionStorage data has been merged correctly to prevent data loss");
michael@0 137 });
michael@0 138
michael@0 139 function promiseNewWindow() {
michael@0 140 let deferred = Promise.defer();
michael@0 141 whenNewWindowLoaded({private: false}, deferred.resolve);
michael@0 142 return deferred.promise;
michael@0 143 }
michael@0 144
michael@0 145 function closeWindow(win) {
michael@0 146 let deferred = Promise.defer();
michael@0 147 let outerID = win.QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 148 .getInterface(Ci.nsIDOMWindowUtils)
michael@0 149 .outerWindowID;
michael@0 150
michael@0 151 Services.obs.addObserver(function obs(subject, topic) {
michael@0 152 let id = subject.QueryInterface(Ci.nsISupportsPRUint64).data;
michael@0 153 if (id == outerID) {
michael@0 154 Services.obs.removeObserver(obs, topic);
michael@0 155 deferred.resolve();
michael@0 156 }
michael@0 157 }, "outer-window-destroyed", false);
michael@0 158
michael@0 159 win.close();
michael@0 160 return deferred.promise;
michael@0 161 }
michael@0 162
michael@0 163 function createTabWithStorageData(urls, win = window) {
michael@0 164 return Task.spawn(function task() {
michael@0 165 let tab = win.gBrowser.addTab();
michael@0 166 let browser = tab.linkedBrowser;
michael@0 167
michael@0 168 for (let url of urls) {
michael@0 169 browser.loadURI(url);
michael@0 170 yield promiseBrowserLoaded(browser);
michael@0 171 yield modifySessionStorage(browser, {test: INITIAL_VALUE});
michael@0 172 }
michael@0 173
michael@0 174 throw new Task.Result(tab);
michael@0 175 });
michael@0 176 }
michael@0 177
michael@0 178 function waitForStorageEvent(browser) {
michael@0 179 return promiseContentMessage(browser, "ss-test:MozStorageChanged");
michael@0 180 }
michael@0 181
michael@0 182 function sendQuitApplicationRequested() {
michael@0 183 let cancelQuit = Cc["@mozilla.org/supports-PRBool;1"]
michael@0 184 .createInstance(Ci.nsISupportsPRBool);
michael@0 185 Services.obs.notifyObservers(cancelQuit, "quit-application-requested", null);
michael@0 186 }
michael@0 187
michael@0 188 function modifySessionStorage(browser, data) {
michael@0 189 browser.messageManager.sendAsyncMessage("ss-test:modifySessionStorage", data);
michael@0 190 return waitForStorageEvent(browser);
michael@0 191 }

mercurial