browser_346337.js
Go to the documentation of this file.
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is sessionstore test code.
15  *
16  * The Initial Developer of the Original Code is
17  * Simon Bünzli <zeniko@gmail.com>.
18  * Portions created by the Initial Developer are Copyright (C) 2008
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */
36 
37 function test() {
40  let fieldList = {
41  "//input[@name='input']": Date.now().toString(),
42  "//input[@name='spaced 1']": Math.random().toString(),
43  "//input[3]": "three",
44  "//input[@type='checkbox']": true,
45  "//input[@name='uncheck']": false,
46  "//input[@type='radio'][1]": false,
47  "//input[@type='radio'][2]": true,
48  "//input[@type='radio'][3]": false,
49  "//select": 2,
50  "//select[@multiple]": [1, 3],
51  "//textarea[1]": "",
52  "//textarea[2]": "Some text... " + Math.random(),
53  "//textarea[3]": "Some more text\n" + new Date(),
54  "//input[@type='file'][1]": ["/dev/null"],
55  "//input[@type='file'][2]": ["/dev/null", "/dev/stdin"]
56  };
57 
58  function getElementByXPath(aTab, aQuery) {
59  let doc = aTab.linkedBrowser.contentDocument;
60  let xptype = Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE;
61  return doc.evaluate(aQuery, doc, null, xptype, null).singleNodeValue;
62  }
63 
64  function setFormValue(aTab, aQuery, aValue) {
65  let node = getElementByXPath(aTab, aQuery);
66  if (typeof aValue == "string")
67  node.value = aValue;
68  else if (typeof aValue == "boolean")
69  node.checked = aValue;
70  else if (typeof aValue == "number")
71  node.selectedIndex = aValue;
72  else if (node instanceof Ci.nsIDOMHTMLInputElement && node.type == "file")
73  node.mozSetFileNameArray(aValue, aValue.length);
74  else
75  Array.forEach(node.options, function(aOpt, aIx)
76  (aOpt.selected = aValue.indexOf(aIx) > -1));
77  }
78 
79  function compareFormValue(aTab, aQuery, aValue) {
80  let node = getElementByXPath(aTab, aQuery);
81  if (!node)
82  return false;
83  if (node instanceof Ci.nsIDOMHTMLInputElement) {
84  if (node.type == "file") {
85  let fileNames = node.mozGetFileNameArray();
86  return fileNames.length == aValue.length &&
87  Array.every(fileNames, function(aFile) aValue.indexOf(aFile) >= 0);
88  }
89  return aValue == (node.type == "checkbox" || node.type == "radio" ?
90  node.checked : node.value);
91  }
92  if (node instanceof Ci.nsIDOMHTMLTextAreaElement)
93  return aValue == node.value;
94  if (!node.multiple)
95  return aValue == node.selectedIndex;
96  return Array.every(node.options, function(aOpt, aIx)
97  (aValue.indexOf(aIx) > -1) == aOpt.selected);
98  }
99 
100  // test setup
101  let tabbrowser = getBrowser();
103 
104  // make sure we don't save form data at all (except for tab duplication)
105  gPrefService.setIntPref("browser.sessionstore.privacy_level", 2);
106 
107  let testURL = "chrome://mochikit/content/browser/" +
108  "browser/components/sessionstore/test/browser/browser_346337_sample.html";
109  let tab = tabbrowser.addTab(testURL);
110  tab.linkedBrowser.addEventListener("load", function(aEvent) {
111  for (let xpath in fieldList)
112  setFormValue(tab, xpath, fieldList[xpath]);
113 
114  let tab2 = tabbrowser.duplicateTab(tab);
115  tab2.linkedBrowser.addEventListener("load", function(aEvent) {
116  for (let xpath in fieldList)
117  ok(compareFormValue(tab2, xpath, fieldList[xpath]),
118  "The value for \"" + xpath + "\" was correctly restored");
119 
120  // clean up
121  tabbrowser.removeTab(tab2);
122  tabbrowser.removeTab(tab);
123 
124  tab = undoCloseTab();
125  tab.linkedBrowser.addEventListener("load", function(aEvent) {
126  for (let xpath in fieldList)
127  if (fieldList[xpath])
128  ok(!compareFormValue(tab, xpath, fieldList[xpath]),
129  "The value for \"" + xpath + "\" was correctly discarded");
130 
131  if (gPrefService.prefHasUserValue("browser.sessionstore.privacy_level"))
132  gPrefService.clearUserPref("browser.sessionstore.privacy_level");
133  // undoCloseTab can reuse a single blank tab, so we have to
134  // make sure not to close the window when closing our last tab
135  if (tabbrowser.tabContainer.childNodes.length == 1)
136  tabbrowser.addTab();
137  tabbrowser.removeTab(tab);
138  finish();
139  }, true);
140  }, true);
141  }, true);
142 }
function test()
onPageChanged aValue
Definition: FeedWriter.js:1395
function doc() browser.contentDocument
function getBrowser() gBrowser
var tab
var tabbrowser
waitForExplicitFinish()
var testURL
return null
Definition: FeedWriter.js:1143
restoreHistoryPrecursor aIx
var tab2
let node
var gPrefService
Definition: overlay.js:34
const Ci
_replaceLoadingTitle aTab
function undoCloseTab(aIndex)
Definition: browser.js:6350