browser_461634.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 = { windows: [{ "tabs": [{ "entries": [] }], _closedTabs: [
47  { state: { entries: [{ url: "http://www.example.net/" }] }, title: FORGET },
48  { state: { entries: [{ url: "http://www.example.net/" }] }, title: REMEMBER },
49  { state: { entries: [{ url: "http://www.example.net/" }] }, title: FORGET },
50  { state: { entries: [{ url: "http://www.example.net/" }] }, title: REMEMBER },
51  ] }] };
52  let remember_count = 2;
53 
54  function countByTitle(aClosedTabList, aTitle)
55  aClosedTabList.filter(function(aData) aData.title == aTitle).length;
56 
57  function testForError(aFunction) {
58  try {
59  aFunction();
60  return false;
61  }
62  catch (ex) {
63  return ex.name == "NS_ERROR_ILLEGAL_VALUE";
64  }
65  }
66 
67  // open a window and add the above closed tab list
68  let newWin = openDialog(location, "_blank", "chrome,all,dialog=no");
69  newWin.addEventListener("load", function(aEvent) {
70  gPrefService.setIntPref("browser.sessionstore.max_tabs_undo",
71  test_state.windows[0]._closedTabs.length);
72  ss.setWindowState(newWin, JSON.stringify(test_state), true);
73 
74  let closedTabs = eval("(" + ss.getClosedTabData(newWin) + ")");
75  is(closedTabs.length, test_state.windows[0]._closedTabs.length,
76  "Closed tab list has the expected length");
77  is(countByTitle(closedTabs, FORGET),
78  test_state.windows[0]._closedTabs.length - remember_count,
79  "The correct amout of tabs are to be forgotten");
80  is(countByTitle(closedTabs, REMEMBER), remember_count,
81  "Everything is set up.");
82 
83  // all of the following calls with illegal arguments should throw NS_ERROR_ILLEGAL_VALUE
84  ok(testForError(function() ss.forgetClosedTab({}, 0)),
85  "Invalid window for forgetClosedTab throws");
86  ok(testForError(function() ss.forgetClosedTab(newWin, -1)),
87  "Invalid tab for forgetClosedTab throws");
88  ok(testForError(function() ss.forgetClosedTab(newWin, test_state.windows[0]._closedTabs.length + 1)),
89  "Invalid tab for forgetClosedTab throws");
90 
91  // Remove third tab, then first tab
92  ss.forgetClosedTab(newWin, 2);
93  ss.forgetClosedTab(newWin, null);
94 
95  closedTabs = eval("(" + ss.getClosedTabData(newWin) + ")");
96  is(closedTabs.length, remember_count,
97  "The correct amout of tabs was removed");
98  is(countByTitle(closedTabs, FORGET), 0,
99  "All tabs specifically forgotten were indeed removed");
100  is(countByTitle(closedTabs, REMEMBER), remember_count,
101  "... and tabs not specifically forgetten weren't.");
102 
103  // clean up
104  newWin.close();
105  gPrefService.clearUserPref("browser.sessionstore.max_tabs_undo");
106  finish();
107  }, false);
108 }
const Cc
var windows
waitForExplicitFinish()
return null
Definition: FeedWriter.js:1143
var gPrefService
Definition: overlay.js:34
function url(spec)
function test()
const Ci
var JSON
_updateTextAndScrollDataForFrame aData