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: /**
michael@0: * This test ensures that form data collection respects the privacy level as
michael@0: * set by the user.
michael@0: */
michael@0: add_task(function test_formdata() {
michael@0: const URL = "http://mochi.test:8888/browser/browser/components/" +
michael@0: "sessionstore/test/browser_formdata_sample.html";
michael@0:
michael@0: const OUTER_VALUE = "browser_formdata_" + Math.random();
michael@0: const INNER_VALUE = "browser_formdata_" + Math.random();
michael@0:
michael@0: // Creates a tab, loads a page with some form fields,
michael@0: // modifies their values and closes the tab.
michael@0: function createAndRemoveTab() {
michael@0: return Task.spawn(function () {
michael@0: // Create a new tab.
michael@0: let tab = gBrowser.addTab(URL);
michael@0: let browser = tab.linkedBrowser;
michael@0: yield promiseBrowserLoaded(browser);
michael@0:
michael@0: // Modify form data.
michael@0: yield setInputValue(browser, {id: "txt", value: OUTER_VALUE});
michael@0: yield setInputValue(browser, {id: "txt", value: INNER_VALUE, frame: 0});
michael@0:
michael@0: // Remove the tab.
michael@0: gBrowser.removeTab(tab);
michael@0: });
michael@0: }
michael@0:
michael@0: yield createAndRemoveTab();
michael@0: let [{state: {formdata}}] = JSON.parse(ss.getClosedTabData(window));
michael@0: is(formdata.id.txt, OUTER_VALUE, "outer value is correct");
michael@0: is(formdata.children[0].id.txt, INNER_VALUE, "inner value is correct");
michael@0:
michael@0: // Disable saving data for encrypted sites.
michael@0: Services.prefs.setIntPref("browser.sessionstore.privacy_level", 1);
michael@0:
michael@0: yield createAndRemoveTab();
michael@0: let [{state: {formdata}}] = JSON.parse(ss.getClosedTabData(window));
michael@0: is(formdata.id.txt, OUTER_VALUE, "outer value is correct");
michael@0: ok(!formdata.children, "inner value was *not* stored");
michael@0:
michael@0: // Disable saving data for any site.
michael@0: Services.prefs.setIntPref("browser.sessionstore.privacy_level", 2);
michael@0:
michael@0: yield createAndRemoveTab();
michael@0: let [{state: {formdata}}] = JSON.parse(ss.getClosedTabData(window));
michael@0: ok(!formdata, "form data has *not* been stored");
michael@0:
michael@0: // Restore the default privacy level.
michael@0: Services.prefs.clearUserPref("browser.sessionstore.privacy_level");
michael@0: });
michael@0:
michael@0: /**
michael@0: * This test ensures that we maintain backwards compatibility with the form
michael@0: * data format used pre Fx 29.
michael@0: */
michael@0: add_task(function test_old_format() {
michael@0: const URL = "data:text/html;charset=utf-8,";
michael@0: const VALUE = "value-" + Math.random();
michael@0:
michael@0: // Create a tab with an iframe containing an input field.
michael@0: let tab = gBrowser.addTab(URL);
michael@0: let browser = tab.linkedBrowser;
michael@0: yield promiseBrowserLoaded(browser);
michael@0:
michael@0: // Check that the form value is restored.
michael@0: let state = {entries: [{url: URL, formdata: {id: {input: VALUE}}}]};
michael@0: ss.setTabState(tab, JSON.stringify(state));
michael@0: yield promiseTabRestored(tab);
michael@0: is((yield getInputValue(browser, "input")), VALUE, "form data restored");
michael@0:
michael@0: // Cleanup.
michael@0: gBrowser.removeTab(tab);
michael@0: });
michael@0:
michael@0: /**
michael@0: * This test ensures that we maintain backwards compatibility with the form
michael@0: * data form used pre Fx 29, esp. the .innerHTML property for editable docs.
michael@0: */
michael@0: add_task(function test_old_format_inner_html() {
michael@0: const URL = "data:text/html;charset=utf-8,
mozilla
" +
michael@0: "";
michael@0: const VALUE = "
value-" + Math.random() + "
";
michael@0:
michael@0: // Create a tab with an iframe containing an input field.
michael@0: let tab = gBrowser.addTab(URL);
michael@0: let browser = tab.linkedBrowser;
michael@0: yield promiseBrowserLoaded(browser);
michael@0:
michael@0: // Restore the tab state.
michael@0: let state = {entries: [{url: URL, innerHTML: VALUE}]};
michael@0: ss.setTabState(tab, JSON.stringify(state));
michael@0: yield promiseTabRestored(tab);
michael@0:
michael@0: // Check that the innerHTML value was restored.
michael@0: let html = yield getInnerHTML(browser);
michael@0: is(html, VALUE, "editable document has been restored correctly");
michael@0:
michael@0: // Cleanup.
michael@0: gBrowser.removeTab(tab);
michael@0: });
michael@0:
michael@0: /**
michael@0: * This test ensures that a malicious website can't trick us into restoring
michael@0: * form data into a wrong website and that we always check the stored URL
michael@0: * before doing so.
michael@0: */
michael@0: add_task(function test_url_check() {
michael@0: const URL = "data:text/html;charset=utf-8,";
michael@0: const VALUE = "value-" + Math.random();
michael@0:
michael@0: // Create a tab with an iframe containing an input field.
michael@0: let tab = gBrowser.addTab(URL);
michael@0: let browser = tab.linkedBrowser;
michael@0: yield promiseBrowserLoaded(browser);
michael@0:
michael@0: // Restore a tab state with a given form data url.
michael@0: function restoreStateWithURL(url) {
michael@0: let state = {entries: [{url: URL}], formdata: {id: {input: VALUE}}};
michael@0:
michael@0: if (url) {
michael@0: state.formdata.url = url;
michael@0: }
michael@0:
michael@0: ss.setTabState(tab, JSON.stringify(state));
michael@0: return promiseTabRestored(tab).then(() => getInputValue(browser, "input"));
michael@0: }
michael@0:
michael@0: // Check that the form value is restored with the correct URL.
michael@0: is((yield restoreStateWithURL(URL)), VALUE, "form data restored");
michael@0:
michael@0: // Check that the form value is *not* restored with the wrong URL.
michael@0: is((yield restoreStateWithURL(URL + "?")), "", "form data not restored");
michael@0: is((yield restoreStateWithURL()), "", "form data not restored");
michael@0:
michael@0: // Cleanup.
michael@0: gBrowser.removeTab(tab);
michael@0: });
michael@0:
michael@0: /**
michael@0: * This test ensures that collecting form data works as expected when having
michael@0: * nested frame sets.
michael@0: */
michael@0: add_task(function test_nested() {
michael@0: const URL = "data:text/html;charset=utf-8," +
michael@0: "";
michael@0:
michael@0: const FORM_DATA = {
michael@0: children: [{
michael@0: xpath: {"/xhtml:html/xhtml:body/xhtml:input": "M"},
michael@0: url: "data:text/html;charset=utf-8,"
michael@0: }]
michael@0: };
michael@0:
michael@0: // Create a tab with an iframe containing an input field.
michael@0: let tab = gBrowser.selectedTab = gBrowser.addTab(URL);
michael@0: let browser = tab.linkedBrowser;
michael@0: yield promiseBrowserLoaded(browser);
michael@0:
michael@0: // Modify the input field's value.
michael@0: yield sendMessage(browser, "ss-test:sendKeyEvent", {key: "m", frame: 0});
michael@0:
michael@0: // Remove the tab and check that we stored form data correctly.
michael@0: gBrowser.removeTab(tab);
michael@0: let [{state: {formdata}}] = JSON.parse(ss.getClosedTabData(window));
michael@0: is(JSON.stringify(formdata), JSON.stringify(FORM_DATA),
michael@0: "formdata for iframe stored correctly");
michael@0:
michael@0: // Restore the closed tab.
michael@0: let tab = ss.undoCloseTab(window, 0);
michael@0: let browser = tab.linkedBrowser;
michael@0: yield promiseTabRestored(tab);
michael@0:
michael@0: // Check that the input field has the right value.
michael@0: SyncHandlers.get(browser).flush();
michael@0: let {formdata} = JSON.parse(ss.getTabState(tab));
michael@0: is(JSON.stringify(formdata), JSON.stringify(FORM_DATA),
michael@0: "formdata for iframe restored correctly");
michael@0:
michael@0: // Cleanup.
michael@0: gBrowser.removeTab(tab);
michael@0: });
michael@0:
michael@0: /**
michael@0: * This test ensures that collecting form data for documents with
michael@0: * designMode=on works as expected.
michael@0: */
michael@0: add_task(function test_design_mode() {
michael@0: const URL = "data:text/html;charset=utf-8,
mozilla
" +
michael@0: "";
michael@0:
michael@0: // Load a tab with an editable document.
michael@0: let tab = gBrowser.selectedTab = gBrowser.addTab(URL);
michael@0: let browser = tab.linkedBrowser;
michael@0: yield promiseBrowserLoaded(browser);
michael@0:
michael@0: // Modify the document content.
michael@0: yield sendMessage(browser, "ss-test:sendKeyEvent", {key: "m"});
michael@0:
michael@0: // Duplicate the modified tab.
michael@0: let tab2 = gBrowser.duplicateTab(tab);
michael@0: yield promiseTabRestored(tab2);
michael@0:
michael@0: // Check that the innerHTML value was restored.
michael@0: let html = yield getInnerHTML(browser);
michael@0: let expected = "