Wed, 31 Dec 2014 06:09:35 +0100
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 | |
michael@0 | 5 | let tmp = {}; |
michael@0 | 6 | Cu.import("resource:///modules/sessionstore/SessionFile.jsm", tmp); |
michael@0 | 7 | let {SessionFile} = tmp; |
michael@0 | 8 | |
michael@0 | 9 | // Shortcuts for histogram names |
michael@0 | 10 | let Keys = {}; |
michael@0 | 11 | for (let k of ["HISTORY", "FORMDATA", "OPEN_WINDOWS", "CLOSED_WINDOWS", "CLOSED_TABS_IN_OPEN_WINDOWS", "DOM_STORAGE"]) { |
michael@0 | 12 | Keys[k] = "FX_SESSION_RESTORE_TOTAL_" + k + "_SIZE_BYTES"; |
michael@0 | 13 | } |
michael@0 | 14 | |
michael@0 | 15 | function lt(a, b, message) { |
michael@0 | 16 | isnot(a, undefined, message + " (sanity check)"); |
michael@0 | 17 | isnot(b, undefined, message + " (sanity check)"); |
michael@0 | 18 | ok(a < b, message + " ( " + a + " < " + b + ")"); |
michael@0 | 19 | } |
michael@0 | 20 | function gt(a, b, message) { |
michael@0 | 21 | isnot(a, undefined, message + " (sanity check)"); |
michael@0 | 22 | isnot(b, undefined, message + " (sanity check)"); |
michael@0 | 23 | ok(a > b, message + " ( " + a + " > " + b + ")"); |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | add_task(function init() { |
michael@0 | 27 | for (let i = ss.getClosedWindowCount() - 1; i >= 0; --i) { |
michael@0 | 28 | ss.forgetClosedWindow(i); |
michael@0 | 29 | } |
michael@0 | 30 | for (let i = ss.getClosedTabCount(window) - 1; i >= 0; --i) { |
michael@0 | 31 | ss.forgetClosedTab(window, i); |
michael@0 | 32 | } |
michael@0 | 33 | }); |
michael@0 | 34 | |
michael@0 | 35 | /** |
michael@0 | 36 | * Test that Telemetry collection doesn't cause any error. |
michael@0 | 37 | */ |
michael@0 | 38 | add_task(function() { |
michael@0 | 39 | info("Checking a little bit of consistency"); |
michael@0 | 40 | let statistics = yield promiseStats(); |
michael@0 | 41 | |
michael@0 | 42 | for (let k of Object.keys(statistics)) { |
michael@0 | 43 | let data = statistics[k]; |
michael@0 | 44 | info("Data for " + k + ": " + data); |
michael@0 | 45 | if (Array.isArray(data)) { |
michael@0 | 46 | ok(data.every(x => x >= 0), "Data for " + k + " is >= 0"); |
michael@0 | 47 | } else { |
michael@0 | 48 | ok(data >= 0, "Data for " + k + " is >= 0"); |
michael@0 | 49 | } |
michael@0 | 50 | } |
michael@0 | 51 | }); |
michael@0 | 52 | |
michael@0 | 53 | /** |
michael@0 | 54 | * Test HISTORY key. |
michael@0 | 55 | */ |
michael@0 | 56 | add_task(function history() { |
michael@0 | 57 | let KEY = Keys.HISTORY; |
michael@0 | 58 | let tab = gBrowser.addTab("http://example.org:80/?"); |
michael@0 | 59 | yield promiseBrowserLoaded(tab.linkedBrowser); |
michael@0 | 60 | try { |
michael@0 | 61 | SyncHandlers.get(tab.linkedBrowser).flush(); |
michael@0 | 62 | let statistics = yield promiseStats(); |
michael@0 | 63 | |
michael@0 | 64 | info("Now changing history"); |
michael@0 | 65 | tab.linkedBrowser.loadURI("http://example.org:80/1"); |
michael@0 | 66 | yield promiseBrowserLoaded(tab.linkedBrowser); |
michael@0 | 67 | SyncHandlers.get(tab.linkedBrowser).flush(); |
michael@0 | 68 | let statistics2 = yield promiseStats(); |
michael@0 | 69 | |
michael@0 | 70 | // We have changed history, so it must have increased |
michael@0 | 71 | isnot(statistics[KEY], undefined, "Key was defined"); |
michael@0 | 72 | isnot(statistics2[KEY], undefined, "Key is still defined"); |
michael@0 | 73 | gt(statistics2[KEY], statistics[KEY], "The total size of HISTORY has increased"); |
michael@0 | 74 | |
michael@0 | 75 | // Almost nothing else should |
michael@0 | 76 | for (let k of ["FORMDATA", "DOM_STORAGE", "CLOSED_WINDOWS", "CLOSED_TABS_IN_OPEN_WINDOWS"]) { |
michael@0 | 77 | is(statistics2[Keys[k]], statistics[Keys[k]], "The total size of " + k + " has not increased"); |
michael@0 | 78 | } |
michael@0 | 79 | } finally { |
michael@0 | 80 | if (tab) { |
michael@0 | 81 | gBrowser.removeTab(tab); |
michael@0 | 82 | } |
michael@0 | 83 | } |
michael@0 | 84 | }); |
michael@0 | 85 | |
michael@0 | 86 | /** |
michael@0 | 87 | * Test CLOSED_TABS_IN_OPEN_WINDOWS key. |
michael@0 | 88 | */ |
michael@0 | 89 | add_task(function close_tab() { |
michael@0 | 90 | let KEY = Keys.CLOSED_TABS_IN_OPEN_WINDOWS; |
michael@0 | 91 | let tab = gBrowser.addTab("http://example.org:80/?close_tab"); |
michael@0 | 92 | yield promiseBrowserLoaded(tab.linkedBrowser); |
michael@0 | 93 | try { |
michael@0 | 94 | SyncHandlers.get(tab.linkedBrowser).flush(); |
michael@0 | 95 | let statistics = yield promiseStats(); |
michael@0 | 96 | |
michael@0 | 97 | info("Now closing a tab"); |
michael@0 | 98 | gBrowser.removeTab(tab); |
michael@0 | 99 | tab = null; |
michael@0 | 100 | let statistics2 = yield promiseStats(); |
michael@0 | 101 | |
michael@0 | 102 | isnot(statistics[KEY], undefined, "Key was defined"); |
michael@0 | 103 | isnot(statistics2[KEY], undefined, "Key is still defined"); |
michael@0 | 104 | gt(statistics2[KEY], statistics[KEY], "The total size of CLOSED_TABS_IN_OPEN_WINDOWS has increased"); |
michael@0 | 105 | |
michael@0 | 106 | // Almost nothing else should change |
michael@0 | 107 | for (let k of ["FORMDATA", "DOM_STORAGE", "CLOSED_WINDOWS"]) { |
michael@0 | 108 | is(statistics2[Keys[k]], statistics[Keys[k]], "The total size of " + k + " has not increased"); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | } finally { |
michael@0 | 112 | if (tab) { |
michael@0 | 113 | gBrowser.removeTab(tab); |
michael@0 | 114 | } |
michael@0 | 115 | } |
michael@0 | 116 | }); |
michael@0 | 117 | |
michael@0 | 118 | /** |
michael@0 | 119 | * Test OPEN_WINDOWS key. |
michael@0 | 120 | */ |
michael@0 | 121 | add_task(function open_window() { |
michael@0 | 122 | let KEY = Keys.OPEN_WINDOWS; |
michael@0 | 123 | let win; |
michael@0 | 124 | try { |
michael@0 | 125 | let statistics = yield promiseStats(); |
michael@0 | 126 | win = yield promiseNewWindowLoaded("http://example.org:80/?open_window"); |
michael@0 | 127 | let statistics2 = yield promiseStats(); |
michael@0 | 128 | |
michael@0 | 129 | isnot(statistics[KEY], undefined, "Key was defined"); |
michael@0 | 130 | isnot(statistics2[KEY], undefined, "Key is still defined"); |
michael@0 | 131 | gt(statistics2[KEY], statistics[KEY], "The total size of OPEN_WINDOWS has increased"); |
michael@0 | 132 | |
michael@0 | 133 | // Almost nothing else should change |
michael@0 | 134 | for (let k of ["FORMDATA", "DOM_STORAGE", "CLOSED_WINDOWS", "CLOSED_TABS_IN_OPEN_WINDOWS"]) { |
michael@0 | 135 | is(statistics2[Keys[k]], statistics[Keys[k]], "The total size of " + k + " has not increased"); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | } finally { |
michael@0 | 139 | if (win) { |
michael@0 | 140 | yield promiseWindowClosed(win); |
michael@0 | 141 | } |
michael@0 | 142 | } |
michael@0 | 143 | }); |
michael@0 | 144 | |
michael@0 | 145 | /** |
michael@0 | 146 | * Test CLOSED_WINDOWS key. |
michael@0 | 147 | */ |
michael@0 | 148 | add_task(function close_window() { |
michael@0 | 149 | let KEY = Keys.CLOSED_WINDOWS; |
michael@0 | 150 | let win = yield promiseNewWindowLoaded("http://example.org:80/?close_window"); |
michael@0 | 151 | |
michael@0 | 152 | // We need to add something to the window, otherwise it won't be saved |
michael@0 | 153 | let tab = win.gBrowser.addTab("http://example.org:80/?close_tab"); |
michael@0 | 154 | yield promiseBrowserLoaded(tab.linkedBrowser); |
michael@0 | 155 | try { |
michael@0 | 156 | let statistics = yield promiseStats(); |
michael@0 | 157 | yield promiseWindowClosed(win); |
michael@0 | 158 | win = null; |
michael@0 | 159 | let statistics2 = yield promiseStats(); |
michael@0 | 160 | |
michael@0 | 161 | isnot(statistics[KEY], undefined, "Key was defined"); |
michael@0 | 162 | isnot(statistics2[KEY], undefined, "Key is still defined"); |
michael@0 | 163 | gt(statistics2[KEY], statistics[KEY], "The total size of CLOSED_WINDOWS has increased"); |
michael@0 | 164 | lt(statistics2[Keys.OPEN_WINDOWS], statistics[Keys.OPEN_WINDOWS], "The total size of OPEN_WINDOWS has decreased"); |
michael@0 | 165 | |
michael@0 | 166 | // Almost nothing else should change |
michael@0 | 167 | for (let k of ["FORMDATA", "DOM_STORAGE", "CLOSED_TABS_IN_OPEN_WINDOWS"]) { |
michael@0 | 168 | is(statistics2[Keys[k]], statistics[Keys[k]], "The total size of " + k + " has not increased"); |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | } finally { |
michael@0 | 172 | if (win) { |
michael@0 | 173 | yield promiseWindowClosed(win); |
michael@0 | 174 | } |
michael@0 | 175 | } |
michael@0 | 176 | }); |
michael@0 | 177 | |
michael@0 | 178 | |
michael@0 | 179 | /** |
michael@0 | 180 | * Test DOM_STORAGE key. |
michael@0 | 181 | */ |
michael@0 | 182 | add_task(function dom_storage() { |
michael@0 | 183 | let KEY = Keys.DOM_STORAGE; |
michael@0 | 184 | let tab = gBrowser.addTab("http://example.org:80/?dom_storage"); |
michael@0 | 185 | yield promiseBrowserLoaded(tab.linkedBrowser); |
michael@0 | 186 | try { |
michael@0 | 187 | SyncHandlers.get(tab.linkedBrowser).flush(); |
michael@0 | 188 | let statistics = yield promiseStats(); |
michael@0 | 189 | |
michael@0 | 190 | info("Now adding some storage"); |
michael@0 | 191 | yield modifySessionStorage(tab.linkedBrowser, {foo: "bar"}); |
michael@0 | 192 | SyncHandlers.get(tab.linkedBrowser).flush(); |
michael@0 | 193 | |
michael@0 | 194 | let statistics2 = yield promiseStats(); |
michael@0 | 195 | |
michael@0 | 196 | isnot(statistics[KEY], undefined, "Key was defined"); |
michael@0 | 197 | isnot(statistics2[KEY], undefined, "Key is still defined"); |
michael@0 | 198 | gt(statistics2[KEY], statistics[KEY], "The total size of DOM_STORAGE has increased"); |
michael@0 | 199 | |
michael@0 | 200 | // Almost nothing else should change |
michael@0 | 201 | for (let k of ["CLOSED_TABS_IN_OPEN_WINDOWS", "FORMDATA", "CLOSED_WINDOWS"]) { |
michael@0 | 202 | is(statistics2[Keys[k]], statistics[Keys[k]], "The total size of " + k + " has not increased"); |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | } finally { |
michael@0 | 206 | if (tab) { |
michael@0 | 207 | gBrowser.removeTab(tab); |
michael@0 | 208 | } |
michael@0 | 209 | } |
michael@0 | 210 | }); |
michael@0 | 211 | |
michael@0 | 212 | /** |
michael@0 | 213 | * Test FORMDATA key. |
michael@0 | 214 | */ |
michael@0 | 215 | add_task(function formdata() { |
michael@0 | 216 | let KEY = Keys.FORMDATA; |
michael@0 | 217 | let tab = gBrowser.addTab("data:text/html;charset=utf-8,<input%20id='input'>"); |
michael@0 | 218 | yield promiseBrowserLoaded(tab.linkedBrowser); |
michael@0 | 219 | try { |
michael@0 | 220 | SyncHandlers.get(tab.linkedBrowser).flush(); |
michael@0 | 221 | let statistics = yield promiseStats(); |
michael@0 | 222 | |
michael@0 | 223 | info("Now changing form data"); |
michael@0 | 224 | |
michael@0 | 225 | yield setInputValue(tab.linkedBrowser, {id: "input", value: "This is some form data"}); |
michael@0 | 226 | SyncHandlers.get(tab.linkedBrowser).flush(); |
michael@0 | 227 | |
michael@0 | 228 | let statistics2 = yield promiseStats(); |
michael@0 | 229 | |
michael@0 | 230 | isnot(statistics[KEY], undefined, "Key was defined"); |
michael@0 | 231 | isnot(statistics2[KEY], undefined, "Key is still defined"); |
michael@0 | 232 | gt(statistics2[KEY], statistics[KEY], "The total size of FORMDATA has increased"); |
michael@0 | 233 | |
michael@0 | 234 | // Almost nothing else should |
michael@0 | 235 | for (let k of ["DOM_STORAGE", "CLOSED_WINDOWS", "CLOSED_TABS_IN_OPEN_WINDOWS"]) { |
michael@0 | 236 | is(statistics2[Keys[k]], statistics[Keys[k]], "The total size of " + k + " has not increased"); |
michael@0 | 237 | } |
michael@0 | 238 | } finally { |
michael@0 | 239 | if (tab) { |
michael@0 | 240 | gBrowser.removeTab(tab); |
michael@0 | 241 | } |
michael@0 | 242 | } |
michael@0 | 243 | }); |
michael@0 | 244 | |
michael@0 | 245 | add_task(function* test_sessionRestoreInit() { |
michael@0 | 246 | let info = Cc["@mozilla.org/toolkit/app-startup;1"]. |
michael@0 | 247 | getService(Ci.nsIAppStartup). |
michael@0 | 248 | getStartupInfo(); |
michael@0 | 249 | ok(info.sessionRestoreInit > info.process, "sessionRestoreInit is after process creation"); |
michael@0 | 250 | ok(info.sessionRestoreInit < |
michael@0 | 251 | Date.now() + 10000 /* Date.now() is non-monotonic, let's play it paranoid*/, |
michael@0 | 252 | "sessionRestoreInit is before now"); |
michael@0 | 253 | }); |
michael@0 | 254 | |
michael@0 | 255 | /** |
michael@0 | 256 | * Get the latest statistics. |
michael@0 | 257 | */ |
michael@0 | 258 | function promiseStats() { |
michael@0 | 259 | let state = ss.getBrowserState(); |
michael@0 | 260 | info("Stats: " + state); |
michael@0 | 261 | return SessionFile.gatherTelemetry(state); |
michael@0 | 262 | } |
michael@0 | 263 | |
michael@0 | 264 | |
michael@0 | 265 | function modifySessionStorage(browser, data) { |
michael@0 | 266 | browser.messageManager.sendAsyncMessage("ss-test:modifySessionStorage", data); |
michael@0 | 267 | return promiseContentMessage(browser, "ss-test:MozStorageChanged"); |
michael@0 | 268 | } |
michael@0 | 269 | |
michael@0 | 270 | function setInputValue(browser, data) { |
michael@0 | 271 | return sendMessage(browser, "ss-test:setInputValue", data); |
michael@0 | 272 | } |