browser/components/sessionstore/test/browser_formdata_xpath.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 URL = ROOT + "browser_formdata_xpath_sample.html";
michael@0 7
michael@0 8 /**
michael@0 9 * Bug 346337 - Generic form data restoration tests.
michael@0 10 */
michael@0 11 add_task(function setup() {
michael@0 12 // make sure we don't save form data at all (except for tab duplication)
michael@0 13 Services.prefs.setIntPref("browser.sessionstore.privacy_level", 2);
michael@0 14
michael@0 15 registerCleanupFunction(() => {
michael@0 16 Services.prefs.clearUserPref("browser.sessionstore.privacy_level");
michael@0 17 });
michael@0 18 });
michael@0 19
michael@0 20 const FILE1 = createFilePath("346337_test1.file");
michael@0 21 const FILE2 = createFilePath("346337_test2.file");
michael@0 22
michael@0 23 const FIELDS = {
michael@0 24 "//input[@name='input']": Date.now().toString(),
michael@0 25 "//input[@name='spaced 1']": Math.random().toString(),
michael@0 26 "//input[3]": "three",
michael@0 27 "//input[@type='checkbox']": true,
michael@0 28 "//input[@name='uncheck']": false,
michael@0 29 "//input[@type='radio'][1]": false,
michael@0 30 "//input[@type='radio'][2]": true,
michael@0 31 "//input[@type='radio'][3]": false,
michael@0 32 "//select": 2,
michael@0 33 "//select[@multiple]": [1, 3],
michael@0 34 "//textarea[1]": "",
michael@0 35 "//textarea[2]": "Some text... " + Math.random(),
michael@0 36 "//textarea[3]": "Some more text\n" + new Date(),
michael@0 37 "//input[@type='file'][1]": [FILE1],
michael@0 38 "//input[@type='file'][2]": [FILE1, FILE2]
michael@0 39 };
michael@0 40
michael@0 41 add_task(function test_form_data_restoration() {
michael@0 42 // Load page with some input fields.
michael@0 43 let tab = gBrowser.addTab(URL);
michael@0 44 let browser = tab.linkedBrowser;
michael@0 45 yield promiseBrowserLoaded(browser);
michael@0 46
michael@0 47 // Fill in some values.
michael@0 48 for (let xpath of Object.keys(FIELDS)) {
michael@0 49 yield setFormValue(browser, xpath);
michael@0 50 }
michael@0 51
michael@0 52 // Duplicate the tab.
michael@0 53 let tab2 = gBrowser.duplicateTab(tab);
michael@0 54 let browser2 = tab2.linkedBrowser;
michael@0 55 yield promiseTabRestored(tab2);
michael@0 56
michael@0 57 // Check that all form values have been duplicated.
michael@0 58 for (let xpath of Object.keys(FIELDS)) {
michael@0 59 let expected = JSON.stringify(FIELDS[xpath]);
michael@0 60 let actual = JSON.stringify(yield getFormValue(browser2, xpath));
michael@0 61 is(actual, expected, "The value for \"" + xpath + "\" was correctly restored");
michael@0 62 }
michael@0 63
michael@0 64 // Remove all tabs.
michael@0 65 gBrowser.removeTab(tab2);
michael@0 66 gBrowser.removeTab(tab);
michael@0 67
michael@0 68 // Restore one of the tabs again.
michael@0 69 tab = ss.undoCloseTab(window, 0);
michael@0 70 browser = tab.linkedBrowser;
michael@0 71 yield promiseTabRestored(tab);
michael@0 72
michael@0 73 // Check that none of the form values have been restored due to the privacy
michael@0 74 // level settings.
michael@0 75 for (let xpath of Object.keys(FIELDS)) {
michael@0 76 let expected = FIELDS[xpath];
michael@0 77 if (expected) {
michael@0 78 let actual = yield getFormValue(browser, xpath, expected);
michael@0 79 isnot(actual, expected, "The value for \"" + xpath + "\" was correctly discarded");
michael@0 80 }
michael@0 81 }
michael@0 82
michael@0 83 // Cleanup.
michael@0 84 gBrowser.removeTab(tab);
michael@0 85 });
michael@0 86
michael@0 87 function createFilePath(leaf) {
michael@0 88 let file = Services.dirsvc.get("TmpD", Ci.nsIFile);
michael@0 89 file.append(leaf);
michael@0 90 return file.path;
michael@0 91 }
michael@0 92
michael@0 93 function isArrayOfNumbers(value) {
michael@0 94 return Array.isArray(value) && value.every(n => typeof(n) === "number");
michael@0 95 }
michael@0 96
michael@0 97 function isArrayOfStrings(value) {
michael@0 98 return Array.isArray(value) && value.every(n => typeof(n) === "string");
michael@0 99 }
michael@0 100
michael@0 101 function getFormValue(browser, xpath) {
michael@0 102 let value = FIELDS[xpath];
michael@0 103
michael@0 104 if (typeof value == "string") {
michael@0 105 return getInputValue(browser, {xpath: xpath});
michael@0 106 }
michael@0 107
michael@0 108 if (typeof value == "boolean") {
michael@0 109 return getInputChecked(browser, {xpath: xpath});
michael@0 110 }
michael@0 111
michael@0 112 if (typeof value == "number") {
michael@0 113 return getSelectedIndex(browser, {xpath: xpath});
michael@0 114 }
michael@0 115
michael@0 116 if (isArrayOfNumbers(value)) {
michael@0 117 return getMultipleSelected(browser, {xpath: xpath});
michael@0 118 }
michael@0 119
michael@0 120 if (isArrayOfStrings(value)) {
michael@0 121 return getFileNameArray(browser, {xpath: xpath});
michael@0 122 }
michael@0 123
michael@0 124 throw new Error("unknown input type");
michael@0 125 }
michael@0 126
michael@0 127 function setFormValue(browser, xpath) {
michael@0 128 let value = FIELDS[xpath];
michael@0 129
michael@0 130 if (typeof value == "string") {
michael@0 131 return setInputValue(browser, {xpath: xpath, value: value});
michael@0 132 }
michael@0 133
michael@0 134 if (typeof value == "boolean") {
michael@0 135 return setInputChecked(browser, {xpath: xpath, checked: value});
michael@0 136 }
michael@0 137
michael@0 138 if (typeof value == "number") {
michael@0 139 return setSelectedIndex(browser, {xpath: xpath, index: value});
michael@0 140 }
michael@0 141
michael@0 142 if (isArrayOfNumbers(value)) {
michael@0 143 return setMultipleSelected(browser, {xpath: xpath, indices: value});
michael@0 144 }
michael@0 145
michael@0 146 if (isArrayOfStrings(value)) {
michael@0 147 return setFileNameArray(browser, {xpath: xpath, names: value});
michael@0 148 }
michael@0 149
michael@0 150 throw new Error("unknown input type");
michael@0 151 }

mercurial