michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: "use strict"; michael@0: michael@0: const RAND = Math.random(); michael@0: const URL = "http://mochi.test:8888/browser/" + michael@0: "browser/components/sessionstore/test/browser_sessionStorage.html" + michael@0: "?" + RAND; michael@0: michael@0: const OUTER_VALUE = "outer-value-" + RAND; michael@0: const INNER_VALUE = "inner-value-" + RAND; michael@0: michael@0: /** michael@0: * This test ensures that setting, modifying and restoring sessionStorage data michael@0: * works as expected. michael@0: */ michael@0: add_task(function session_storage() { michael@0: let tab = gBrowser.addTab(URL); michael@0: let browser = tab.linkedBrowser; michael@0: yield promiseBrowserLoaded(browser); michael@0: michael@0: // Flush to make sure chrome received all data. michael@0: SyncHandlers.get(browser).flush(); michael@0: michael@0: let {storage} = JSON.parse(ss.getTabState(tab)); michael@0: is(storage["http://example.com"].test, INNER_VALUE, michael@0: "sessionStorage data for example.com has been serialized correctly"); michael@0: is(storage["http://mochi.test:8888"].test, OUTER_VALUE, michael@0: "sessionStorage data for mochi.test has been serialized correctly"); michael@0: michael@0: // Ensure that modifying sessionStore values works. michael@0: yield modifySessionStorage(browser, {test: "modified"}); michael@0: yield modifySessionStorage2(browser, {test: "modified2"}); michael@0: SyncHandlers.get(browser).flush(); michael@0: michael@0: let {storage} = JSON.parse(ss.getTabState(tab)); michael@0: is(storage["http://example.com"].test, "modified2", michael@0: "sessionStorage data for example.com has been serialized correctly"); michael@0: is(storage["http://mochi.test:8888"].test, "modified", michael@0: "sessionStorage data for mochi.test has been serialized correctly"); michael@0: michael@0: // Test that duplicating a tab works. michael@0: let tab2 = gBrowser.duplicateTab(tab); michael@0: let browser2 = tab2.linkedBrowser; michael@0: yield promiseTabRestored(tab2); michael@0: michael@0: // Flush to make sure chrome received all data. michael@0: SyncHandlers.get(browser2).flush(); michael@0: michael@0: let {storage} = JSON.parse(ss.getTabState(tab2)); michael@0: is(storage["http://example.com"].test, "modified2", michael@0: "sessionStorage data for example.com has been duplicated correctly"); michael@0: is(storage["http://mochi.test:8888"].test, "modified", michael@0: "sessionStorage data for mochi.test has been duplicated correctly"); michael@0: michael@0: // Ensure that the content script retains restored data michael@0: // (by e.g. duplicateTab) and sends it along with new data. michael@0: yield modifySessionStorage(browser2, {test: "modified3"}); michael@0: SyncHandlers.get(browser2).flush(); michael@0: michael@0: let {storage} = JSON.parse(ss.getTabState(tab2)); michael@0: is(storage["http://example.com"].test, "modified2", michael@0: "sessionStorage data for example.com has been duplicated correctly"); michael@0: is(storage["http://mochi.test:8888"].test, "modified3", michael@0: "sessionStorage data for mochi.test has been duplicated correctly"); michael@0: michael@0: // Check that loading a new URL discards data. michael@0: browser2.loadURI("http://mochi.test:8888/"); michael@0: yield promiseBrowserLoaded(browser2); michael@0: SyncHandlers.get(browser2).flush(); michael@0: michael@0: let {storage} = JSON.parse(ss.getTabState(tab2)); michael@0: is(storage["http://mochi.test:8888"].test, "modified3", michael@0: "navigating retains correct storage data"); michael@0: ok(!storage["http://example.com"], "storage data was discarded"); michael@0: michael@0: // Check that loading a new URL discards data. michael@0: browser2.loadURI("about:mozilla"); michael@0: yield promiseBrowserLoaded(browser2); michael@0: SyncHandlers.get(browser2).flush(); michael@0: michael@0: let state = JSON.parse(ss.getTabState(tab2)); michael@0: ok(!state.hasOwnProperty("storage"), "storage data was discarded"); michael@0: michael@0: // Clean up. michael@0: gBrowser.removeTab(tab); michael@0: gBrowser.removeTab(tab2); michael@0: }); michael@0: michael@0: /** michael@0: * This test ensures that purging domain data also purges data from the michael@0: * sessionStorage data collected for tabs. michael@0: */ michael@0: add_task(function purge_domain() { michael@0: let tab = gBrowser.addTab(URL); michael@0: let browser = tab.linkedBrowser; michael@0: yield promiseBrowserLoaded(browser); michael@0: michael@0: // Purge data for "mochi.test". michael@0: yield purgeDomainData(browser, "mochi.test"); michael@0: michael@0: // Flush to make sure chrome received all data. michael@0: SyncHandlers.get(browser).flush(); michael@0: michael@0: let {storage} = JSON.parse(ss.getTabState(tab)); michael@0: ok(!storage["http://mochi.test:8888"], michael@0: "sessionStorage data for mochi.test has been purged"); michael@0: is(storage["http://example.com"].test, INNER_VALUE, michael@0: "sessionStorage data for example.com has been preserved"); michael@0: michael@0: gBrowser.removeTab(tab); michael@0: }); michael@0: michael@0: /** michael@0: * This test ensures that collecting sessionStorage data respects the privacy michael@0: * levels as set by the user. michael@0: */ michael@0: add_task(function respect_privacy_level() { michael@0: let tab = gBrowser.addTab(URL + "&secure"); michael@0: yield promiseBrowserLoaded(tab.linkedBrowser); michael@0: gBrowser.removeTab(tab); michael@0: michael@0: let [{state: {storage}}] = JSON.parse(ss.getClosedTabData(window)); michael@0: is(storage["http://mochi.test:8888"].test, OUTER_VALUE, michael@0: "http sessionStorage data has been saved"); michael@0: is(storage["https://example.com"].test, INNER_VALUE, michael@0: "https sessionStorage data has been saved"); michael@0: michael@0: // Disable saving data for encrypted sites. michael@0: Services.prefs.setIntPref("browser.sessionstore.privacy_level", 1); michael@0: michael@0: let tab = gBrowser.addTab(URL + "&secure"); michael@0: yield promiseBrowserLoaded(tab.linkedBrowser); michael@0: gBrowser.removeTab(tab); michael@0: michael@0: let [{state: {storage}}] = JSON.parse(ss.getClosedTabData(window)); michael@0: is(storage["http://mochi.test:8888"].test, OUTER_VALUE, michael@0: "http sessionStorage data has been saved"); michael@0: ok(!storage["https://example.com"], michael@0: "https sessionStorage data has *not* been saved"); michael@0: michael@0: // Disable saving data for any site. michael@0: Services.prefs.setIntPref("browser.sessionstore.privacy_level", 2); michael@0: michael@0: // Check that duplicating a tab copies all private data. michael@0: let tab = gBrowser.addTab(URL + "&secure"); michael@0: yield promiseBrowserLoaded(tab.linkedBrowser); michael@0: let tab2 = gBrowser.duplicateTab(tab); michael@0: yield promiseTabRestored(tab2); michael@0: gBrowser.removeTab(tab); michael@0: michael@0: // With privacy_level=2 the |tab| shouldn't have any sessionStorage data. michael@0: let [{state: {storage}}] = JSON.parse(ss.getClosedTabData(window)); michael@0: ok(!storage, "sessionStorage data has *not* been saved"); michael@0: michael@0: // Restore the default privacy level and close the duplicated tab. michael@0: Services.prefs.clearUserPref("browser.sessionstore.privacy_level"); michael@0: gBrowser.removeTab(tab2); michael@0: michael@0: // With privacy_level=0 the duplicated |tab2| should persist all data. michael@0: let [{state: {storage}}] = JSON.parse(ss.getClosedTabData(window)); michael@0: is(storage["http://mochi.test:8888"].test, OUTER_VALUE, michael@0: "http sessionStorage data has been saved"); michael@0: is(storage["https://example.com"].test, INNER_VALUE, michael@0: "https sessionStorage data has been saved"); michael@0: }); michael@0: michael@0: function waitForStorageEvent(browser) { michael@0: return promiseContentMessage(browser, "ss-test:MozStorageChanged"); michael@0: } michael@0: michael@0: function modifySessionStorage(browser, data) { michael@0: browser.messageManager.sendAsyncMessage("ss-test:modifySessionStorage", data); michael@0: return waitForStorageEvent(browser); michael@0: } michael@0: michael@0: function modifySessionStorage2(browser, data) { michael@0: browser.messageManager.sendAsyncMessage("ss-test:modifySessionStorage2", data); michael@0: return waitForStorageEvent(browser); michael@0: } michael@0: michael@0: function purgeDomainData(browser, domain) { michael@0: return sendMessage(browser, "ss-test:purgeDomainData", domain); michael@0: }