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