browser_491577.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  * Michael Kraft <morac99-firefox@yahoo.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  // test setup
42  let ss = Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
44 
45  const REMEMBER = Date.now(), FORGET = Math.random();
46  let test_state = {
47  windows: [ { tabs: [{ entries: [{ url: "http://example.com/" }] }], selected: 1 } ],
48  _closedWindows : [
49  // _closedWindows[0]
50  {
51  tabs: [
52  { entries: [{ url: "http://example.com/", title: "title" }] },
53  { entries: [{ url: "http://mozilla.org/", title: "title" }] }
54  ],
55  selected: 2,
56  title: FORGET,
57  _closedTabs: []
58  },
59  // _closedWindows[1]
60  {
61  tabs: [
62  { entries: [{ url: "http://mozilla.org/", title: "title" }] },
63  { entries: [{ url: "http://example.com/", title: "title" }] },
64  { entries: [{ url: "http://mozilla.org/", title: "title" }] },
65  ],
66  selected: 3,
67  title: REMEMBER,
68  _closedTabs: []
69  },
70  // _closedWindows[2]
71  {
72  tabs: [
73  { entries: [{ url: "http://example.com/", title: "title" }] }
74  ],
75  selected: 1,
76  title: FORGET,
77  _closedTabs: [
78  {
79  state: {
80  entries: [
81  { url: "http://mozilla.org/", title: "title" },
82  { url: "http://mozilla.org/again", title: "title" }
83  ]
84  },
85  pos: 1,
86  title: "title"
87  },
88  {
89  state: {
90  entries: [
91  { url: "http://example.com", title: "title" }
92  ]
93  },
94  title: "title"
95  }
96  ]
97  }
98  ]
99  };
100  let remember_count = 1;
101 
102  function countByTitle(aClosedWindowList, aTitle)
103  aClosedWindowList.filter(function(aData) aData.title == aTitle).length;
104 
105  function testForError(aFunction) {
106  try {
107  aFunction();
108  return false;
109  }
110  catch (ex) {
111  return ex.name == "NS_ERROR_ILLEGAL_VALUE";
112  }
113  }
114 
115  // open a window and add the above closed window list
116  let newWin = openDialog(location, "_blank", "chrome,all,dialog=no");
117  newWin.addEventListener("load", function(aEvent) {
118  gPrefService.setIntPref("browser.sessionstore.max_windows_undo",
119  test_state._closedWindows.length);
120  ss.setWindowState(newWin, JSON.stringify(test_state), true);
121 
122  let closedWindows = JSON.parse(ss.getClosedWindowData());
123  is(closedWindows.length, test_state._closedWindows.length,
124  "Closed window list has the expected length");
125  is(countByTitle(closedWindows, FORGET),
126  test_state._closedWindows.length - remember_count,
127  "The correct amount of windows are to be forgotten");
128  is(countByTitle(closedWindows, REMEMBER), remember_count,
129  "Everything is set up.");
130 
131  // all of the following calls with illegal arguments should throw NS_ERROR_ILLEGAL_VALUE
132  ok(testForError(function() ss.forgetClosedWindow(-1)),
133  "Invalid window for forgetClosedWindow throws");
134  ok(testForError(function() ss.forgetClosedWindow(test_state._closedWindows.length + 1)),
135  "Invalid window for forgetClosedWindow throws");
136 
137  // Remove third window, then first window
138  ss.forgetClosedWindow(2);
139  ss.forgetClosedWindow(null);
140 
141  closedWindows = JSON.parse(ss.getClosedWindowData());
142  is(closedWindows.length, remember_count,
143  "The correct amount of windows were removed");
144  is(countByTitle(closedWindows, FORGET), 0,
145  "All windows specifically forgotten were indeed removed");
146  is(countByTitle(closedWindows, REMEMBER), remember_count,
147  "... and windows not specifically forgetten weren't.");
148 
149  // clean up
150  newWin.close();
151  if (gPrefService.prefHasUserValue("browser.sessionstore.max_windows_undo"))
152  gPrefService.clearUserPref("browser.sessionstore.max_windows_undo");
153  finish();
154  }, false);
155 }
const Cc
_dialogDatepicker pos
var windows
var tabs
waitForExplicitFinish()
return null
Definition: FeedWriter.js:1143
var gPrefService
Definition: overlay.js:34
function url(spec)
const Ci
function test()
var JSON
_updateTextAndScrollDataForFrame aData