michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: michael@0: let tmp = {}; michael@0: Cu.import("resource:///modules/sessionstore/SessionFile.jsm", tmp); michael@0: let {SessionFile} = tmp; michael@0: michael@0: // Shortcuts for histogram names michael@0: let Keys = {}; michael@0: for (let k of ["HISTORY", "FORMDATA", "OPEN_WINDOWS", "CLOSED_WINDOWS", "CLOSED_TABS_IN_OPEN_WINDOWS", "DOM_STORAGE"]) { michael@0: Keys[k] = "FX_SESSION_RESTORE_TOTAL_" + k + "_SIZE_BYTES"; michael@0: } michael@0: michael@0: function lt(a, b, message) { michael@0: isnot(a, undefined, message + " (sanity check)"); michael@0: isnot(b, undefined, message + " (sanity check)"); michael@0: ok(a < b, message + " ( " + a + " < " + b + ")"); michael@0: } michael@0: function gt(a, b, message) { michael@0: isnot(a, undefined, message + " (sanity check)"); michael@0: isnot(b, undefined, message + " (sanity check)"); michael@0: ok(a > b, message + " ( " + a + " > " + b + ")"); michael@0: } michael@0: michael@0: add_task(function init() { michael@0: for (let i = ss.getClosedWindowCount() - 1; i >= 0; --i) { michael@0: ss.forgetClosedWindow(i); michael@0: } michael@0: for (let i = ss.getClosedTabCount(window) - 1; i >= 0; --i) { michael@0: ss.forgetClosedTab(window, i); michael@0: } michael@0: }); michael@0: michael@0: /** michael@0: * Test that Telemetry collection doesn't cause any error. michael@0: */ michael@0: add_task(function() { michael@0: info("Checking a little bit of consistency"); michael@0: let statistics = yield promiseStats(); michael@0: michael@0: for (let k of Object.keys(statistics)) { michael@0: let data = statistics[k]; michael@0: info("Data for " + k + ": " + data); michael@0: if (Array.isArray(data)) { michael@0: ok(data.every(x => x >= 0), "Data for " + k + " is >= 0"); michael@0: } else { michael@0: ok(data >= 0, "Data for " + k + " is >= 0"); michael@0: } michael@0: } michael@0: }); michael@0: michael@0: /** michael@0: * Test HISTORY key. michael@0: */ michael@0: add_task(function history() { michael@0: let KEY = Keys.HISTORY; michael@0: let tab = gBrowser.addTab("http://example.org:80/?"); michael@0: yield promiseBrowserLoaded(tab.linkedBrowser); michael@0: try { michael@0: SyncHandlers.get(tab.linkedBrowser).flush(); michael@0: let statistics = yield promiseStats(); michael@0: michael@0: info("Now changing history"); michael@0: tab.linkedBrowser.loadURI("http://example.org:80/1"); michael@0: yield promiseBrowserLoaded(tab.linkedBrowser); michael@0: SyncHandlers.get(tab.linkedBrowser).flush(); michael@0: let statistics2 = yield promiseStats(); michael@0: michael@0: // We have changed history, so it must have increased michael@0: isnot(statistics[KEY], undefined, "Key was defined"); michael@0: isnot(statistics2[KEY], undefined, "Key is still defined"); michael@0: gt(statistics2[KEY], statistics[KEY], "The total size of HISTORY has increased"); michael@0: michael@0: // Almost nothing else should michael@0: for (let k of ["FORMDATA", "DOM_STORAGE", "CLOSED_WINDOWS", "CLOSED_TABS_IN_OPEN_WINDOWS"]) { michael@0: is(statistics2[Keys[k]], statistics[Keys[k]], "The total size of " + k + " has not increased"); michael@0: } michael@0: } finally { michael@0: if (tab) { michael@0: gBrowser.removeTab(tab); michael@0: } michael@0: } michael@0: }); michael@0: michael@0: /** michael@0: * Test CLOSED_TABS_IN_OPEN_WINDOWS key. michael@0: */ michael@0: add_task(function close_tab() { michael@0: let KEY = Keys.CLOSED_TABS_IN_OPEN_WINDOWS; michael@0: let tab = gBrowser.addTab("http://example.org:80/?close_tab"); michael@0: yield promiseBrowserLoaded(tab.linkedBrowser); michael@0: try { michael@0: SyncHandlers.get(tab.linkedBrowser).flush(); michael@0: let statistics = yield promiseStats(); michael@0: michael@0: info("Now closing a tab"); michael@0: gBrowser.removeTab(tab); michael@0: tab = null; michael@0: let statistics2 = yield promiseStats(); michael@0: michael@0: isnot(statistics[KEY], undefined, "Key was defined"); michael@0: isnot(statistics2[KEY], undefined, "Key is still defined"); michael@0: gt(statistics2[KEY], statistics[KEY], "The total size of CLOSED_TABS_IN_OPEN_WINDOWS has increased"); michael@0: michael@0: // Almost nothing else should change michael@0: for (let k of ["FORMDATA", "DOM_STORAGE", "CLOSED_WINDOWS"]) { michael@0: is(statistics2[Keys[k]], statistics[Keys[k]], "The total size of " + k + " has not increased"); michael@0: } michael@0: michael@0: } finally { michael@0: if (tab) { michael@0: gBrowser.removeTab(tab); michael@0: } michael@0: } michael@0: }); michael@0: michael@0: /** michael@0: * Test OPEN_WINDOWS key. michael@0: */ michael@0: add_task(function open_window() { michael@0: let KEY = Keys.OPEN_WINDOWS; michael@0: let win; michael@0: try { michael@0: let statistics = yield promiseStats(); michael@0: win = yield promiseNewWindowLoaded("http://example.org:80/?open_window"); michael@0: let statistics2 = yield promiseStats(); michael@0: michael@0: isnot(statistics[KEY], undefined, "Key was defined"); michael@0: isnot(statistics2[KEY], undefined, "Key is still defined"); michael@0: gt(statistics2[KEY], statistics[KEY], "The total size of OPEN_WINDOWS has increased"); michael@0: michael@0: // Almost nothing else should change michael@0: for (let k of ["FORMDATA", "DOM_STORAGE", "CLOSED_WINDOWS", "CLOSED_TABS_IN_OPEN_WINDOWS"]) { michael@0: is(statistics2[Keys[k]], statistics[Keys[k]], "The total size of " + k + " has not increased"); michael@0: } michael@0: michael@0: } finally { michael@0: if (win) { michael@0: yield promiseWindowClosed(win); michael@0: } michael@0: } michael@0: }); michael@0: michael@0: /** michael@0: * Test CLOSED_WINDOWS key. michael@0: */ michael@0: add_task(function close_window() { michael@0: let KEY = Keys.CLOSED_WINDOWS; michael@0: let win = yield promiseNewWindowLoaded("http://example.org:80/?close_window"); michael@0: michael@0: // We need to add something to the window, otherwise it won't be saved michael@0: let tab = win.gBrowser.addTab("http://example.org:80/?close_tab"); michael@0: yield promiseBrowserLoaded(tab.linkedBrowser); michael@0: try { michael@0: let statistics = yield promiseStats(); michael@0: yield promiseWindowClosed(win); michael@0: win = null; michael@0: let statistics2 = yield promiseStats(); michael@0: michael@0: isnot(statistics[KEY], undefined, "Key was defined"); michael@0: isnot(statistics2[KEY], undefined, "Key is still defined"); michael@0: gt(statistics2[KEY], statistics[KEY], "The total size of CLOSED_WINDOWS has increased"); michael@0: lt(statistics2[Keys.OPEN_WINDOWS], statistics[Keys.OPEN_WINDOWS], "The total size of OPEN_WINDOWS has decreased"); michael@0: michael@0: // Almost nothing else should change michael@0: for (let k of ["FORMDATA", "DOM_STORAGE", "CLOSED_TABS_IN_OPEN_WINDOWS"]) { michael@0: is(statistics2[Keys[k]], statistics[Keys[k]], "The total size of " + k + " has not increased"); michael@0: } michael@0: michael@0: } finally { michael@0: if (win) { michael@0: yield promiseWindowClosed(win); michael@0: } michael@0: } michael@0: }); michael@0: michael@0: michael@0: /** michael@0: * Test DOM_STORAGE key. michael@0: */ michael@0: add_task(function dom_storage() { michael@0: let KEY = Keys.DOM_STORAGE; michael@0: let tab = gBrowser.addTab("http://example.org:80/?dom_storage"); michael@0: yield promiseBrowserLoaded(tab.linkedBrowser); michael@0: try { michael@0: SyncHandlers.get(tab.linkedBrowser).flush(); michael@0: let statistics = yield promiseStats(); michael@0: michael@0: info("Now adding some storage"); michael@0: yield modifySessionStorage(tab.linkedBrowser, {foo: "bar"}); michael@0: SyncHandlers.get(tab.linkedBrowser).flush(); michael@0: michael@0: let statistics2 = yield promiseStats(); michael@0: michael@0: isnot(statistics[KEY], undefined, "Key was defined"); michael@0: isnot(statistics2[KEY], undefined, "Key is still defined"); michael@0: gt(statistics2[KEY], statistics[KEY], "The total size of DOM_STORAGE has increased"); michael@0: michael@0: // Almost nothing else should change michael@0: for (let k of ["CLOSED_TABS_IN_OPEN_WINDOWS", "FORMDATA", "CLOSED_WINDOWS"]) { michael@0: is(statistics2[Keys[k]], statistics[Keys[k]], "The total size of " + k + " has not increased"); michael@0: } michael@0: michael@0: } finally { michael@0: if (tab) { michael@0: gBrowser.removeTab(tab); michael@0: } michael@0: } michael@0: }); michael@0: michael@0: /** michael@0: * Test FORMDATA key. michael@0: */ michael@0: add_task(function formdata() { michael@0: let KEY = Keys.FORMDATA; michael@0: let tab = gBrowser.addTab("data:text/html;charset=utf-8,"); michael@0: yield promiseBrowserLoaded(tab.linkedBrowser); michael@0: try { michael@0: SyncHandlers.get(tab.linkedBrowser).flush(); michael@0: let statistics = yield promiseStats(); michael@0: michael@0: info("Now changing form data"); michael@0: michael@0: yield setInputValue(tab.linkedBrowser, {id: "input", value: "This is some form data"}); michael@0: SyncHandlers.get(tab.linkedBrowser).flush(); michael@0: michael@0: let statistics2 = yield promiseStats(); michael@0: michael@0: isnot(statistics[KEY], undefined, "Key was defined"); michael@0: isnot(statistics2[KEY], undefined, "Key is still defined"); michael@0: gt(statistics2[KEY], statistics[KEY], "The total size of FORMDATA has increased"); michael@0: michael@0: // Almost nothing else should michael@0: for (let k of ["DOM_STORAGE", "CLOSED_WINDOWS", "CLOSED_TABS_IN_OPEN_WINDOWS"]) { michael@0: is(statistics2[Keys[k]], statistics[Keys[k]], "The total size of " + k + " has not increased"); michael@0: } michael@0: } finally { michael@0: if (tab) { michael@0: gBrowser.removeTab(tab); michael@0: } michael@0: } michael@0: }); michael@0: michael@0: add_task(function* test_sessionRestoreInit() { michael@0: let info = Cc["@mozilla.org/toolkit/app-startup;1"]. michael@0: getService(Ci.nsIAppStartup). michael@0: getStartupInfo(); michael@0: ok(info.sessionRestoreInit > info.process, "sessionRestoreInit is after process creation"); michael@0: ok(info.sessionRestoreInit < michael@0: Date.now() + 10000 /* Date.now() is non-monotonic, let's play it paranoid*/, michael@0: "sessionRestoreInit is before now"); michael@0: }); michael@0: michael@0: /** michael@0: * Get the latest statistics. michael@0: */ michael@0: function promiseStats() { michael@0: let state = ss.getBrowserState(); michael@0: info("Stats: " + state); michael@0: return SessionFile.gatherTelemetry(state); michael@0: } michael@0: michael@0: michael@0: function modifySessionStorage(browser, data) { michael@0: browser.messageManager.sendAsyncMessage("ss-test:modifySessionStorage", data); michael@0: return promiseContentMessage(browser, "ss-test:MozStorageChanged"); michael@0: } michael@0: michael@0: function setInputValue(browser, data) { michael@0: return sendMessage(browser, "ss-test:setInputValue", data); michael@0: }