browser_248970_b.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  * Aaron Train <aaron.train@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  * Ehsan Akhgari <ehsan.akhgari@gmail.com>
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37 
38 function test() {
41  function test(aLambda) {
42  try {
43  return aLambda() || true;
44  } catch(ex) { }
45  return false;
46  }
47 
48  let fieldList = {
49  "//input[@name='input']": Date.now().toString(),
50  "//input[@name='spaced 1']": Math.random().toString(),
51  "//input[3]": "three",
52  "//input[@type='checkbox']": true,
53  "//input[@name='uncheck']": false,
54  "//input[@type='radio'][1]": false,
55  "//input[@type='radio'][2]": true,
56  "//input[@type='radio'][3]": false,
57  "//select": 2,
58  "//select[@multiple]": [1, 3],
59  "//textarea[1]": "",
60  "//textarea[2]": "Some text... " + Math.random(),
61  "//textarea[3]": "Some more text\n" + new Date(),
62  "//input[@type='file']": "/dev/null"
63  };
64 
65  function getElementByXPath(aTab, aQuery) {
66  let doc = aTab.linkedBrowser.contentDocument;
67  let xptype = Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE;
68  return doc.evaluate(aQuery, doc, null, xptype, null).singleNodeValue;
69  }
70 
71  function setFormValue(aTab, aQuery, aValue) {
72  let node = getElementByXPath(aTab, aQuery);
73  if (typeof aValue == "string")
74  node.value = aValue;
75  else if (typeof aValue == "boolean")
76  node.checked = aValue;
77  else if (typeof aValue == "number")
78  node.selectedIndex = aValue;
79  else
80  Array.forEach(node.options, function(aOpt, aIx)
81  (aOpt.selected = aValue.indexOf(aIx) > -1));
82  }
83 
84  function compareFormValue(aTab, aQuery, aValue) {
85  let node = getElementByXPath(aTab, aQuery);
86  if (!node)
87  return false;
88  if (node instanceof Ci.nsIDOMHTMLInputElement)
89  return aValue == (node.type == "checkbox" || node.type == "radio" ?
90  node.checked : node.value);
91  if (node instanceof Ci.nsIDOMHTMLTextAreaElement)
92  return aValue == node.value;
93  if (!node.multiple)
94  return aValue == node.selectedIndex;
95  return Array.every(node.options, function(aOpt, aIx)
96  (aValue.indexOf(aIx) > -1) == aOpt.selected);
97  }
98 
99  // test setup
101 
102  // private browsing service
103  let pb = Cc["@mozilla.org/privatebrowsing;1"].
104  getService(Ci.nsIPrivateBrowsingService);
105  gPrefService.setBoolPref("browser.privatebrowsing.keep_current_session", true);
106 
107  // sessionstore service
108  let ss = test(function() Cc["@mozilla.org/browser/sessionstore;1"].
109  getService(Ci.nsISessionStore));
110 
112  // Test (B) : Session data restoration between modes //
114 
115  const testURL = "chrome://mochikit/content/browser/" +
116  "browser/components/sessionstore/test/browser/browser_248970_b_sample.html";
117  const testURL2 = "http://localhost:8888/browser/" +
118  "browser/components/sessionstore/test/browser/browser_248970_b_sample.html";
119 
120  // get closed tab count
121  let count = ss.getClosedTabCount(window);
122  let max_tabs_undo = gPrefService.getIntPref("browser.sessionstore.max_tabs_undo");
123  ok(0 <= count && count <= max_tabs_undo,
124  "getClosedTabCount should return zero or at most max_tabs_undo");
125 
126  // setup a state for tab (A) so we can check later that is restored
127  let key = "key";
128  let value = "Value " + Math.random();
129  let state = { entries: [{ url: testURL }], extData: { key: value } };
130 
131  // public session, add new tab: (A)
132  tab_A = gBrowser.addTab(testURL);
133  ss.setTabState(tab_A, state.toSource());
134  tab_A.linkedBrowser.addEventListener("load", function(aEvent) {
135  this.removeEventListener("load", arguments.callee, true);
136 
137  // make sure that the next closed tab will increase getClosedTabCount
138  gPrefService.setIntPref("browser.sessionstore.max_tabs_undo", max_tabs_undo + 1)
139 
140  // populate tab_A with form data
141  for (let i in fieldList)
142  setFormValue(tab_A, i, fieldList[i]);
143 
144  // public session, close tab: (A)
145  gBrowser.removeTab(tab_A);
146 
147  // verify that closedTabCount increased
148  ok(ss.getClosedTabCount(window) > count, "getClosedTabCount has increased after closing a tab");
149 
150  // verify tab: (A), in undo list
151  let tab_A_restored = test(function() ss.undoCloseTab(window, 0));
152  ok(tab_A_restored, "a tab is in undo list");
153  tab_A_restored.linkedBrowser.addEventListener("load", function(aEvent) {
154  this.removeEventListener("load", arguments.callee, true);
155 
156  is(testURL, this.currentURI.spec, "it's the same tab that we expect");
157  gBrowser.removeTab(tab_A_restored);
158 
159  // enter private browsing mode
160  pb.privateBrowsingEnabled = true;
161  ok(pb.privateBrowsingEnabled, "private browsing enabled");
162 
163  // setup a state for tab (B) so we can check that its duplicated properly
164  let key1 = "key1";
165  let value1 = "Value " + Math.random();
166  let state1 = { entries: [{ url: testURL2 }], extData: { key1: value1 } };
167 
168  // private browsing session, new tab: (B)
169  tab_B = gBrowser.addTab(testURL2);
170  ss.setTabState(tab_B, state1.toSource());
171  tab_B.linkedBrowser.addEventListener("load", function(aEvent) {
172  this.removeEventListener("load", arguments.callee, true);
173 
174  // populate tab: (B) with different form data
175  for (let item in fieldList)
176  setFormValue(tab_B, item, fieldList[item]);
177 
178  // duplicate tab: (B)
179  let tab_C = gBrowser.duplicateTab(tab_B);
180  tab_C.linkedBrowser.addEventListener("load", function(aEvent) {
181  this.removeEventListener("load", arguments.callee, true);
182 
183  // verify the correctness of the duplicated tab
184  is(ss.getTabValue(tab_C, key1), value1,
185  "tab successfully duplicated - correct state");
186 
187  for (let item in fieldList)
188  ok(compareFormValue(tab_C, item, fieldList[item]),
189  "The value for \"" + item + "\" was correctly duplicated");
190 
191  // private browsing session, close tab: (C) and (B)
192  gBrowser.removeTab(tab_C);
193  gBrowser.removeTab(tab_B);
194 
195  // exit private browsing mode
196  pb.privateBrowsingEnabled = false;
197  ok(!pb.privateBrowsingEnabled, "private browsing disabled");
198 
199  // cleanup
200  if (gPrefService.prefHasUserValue("browser.privatebrowsing.keep_current_session"))
201  gPrefService.clearUserPref("browser.privatebrowsing.keep_current_session");
202  finish();
203  }, true);
204  }, true);
205  }, true);
206  }, true);
207 }
const Cc
onPageChanged aValue
Definition: FeedWriter.js:1395
function doc() browser.contentDocument
getService(Ci.sbIFaceplateManager)
let window
var count
Definition: test_bug7406.js:32
waitForExplicitFinish()
var testURL
return null
Definition: FeedWriter.js:1143
restoreHistoryPrecursor aIx
let node
return!aWindow arguments!aWindow arguments[0]
var gPrefService
Definition: overlay.js:34
function url(spec)
countRef value
Definition: FeedWriter.js:1423
const Ci
_replaceLoadingTitle aTab
this removeEventListener("load", this.__SS_restore, true)
function test()
_getSelectedPageStyle s i