|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 function test() { |
|
6 /** Test for Bug 461634 **/ |
|
7 |
|
8 waitForExplicitFinish(); |
|
9 |
|
10 const REMEMBER = Date.now(), FORGET = Math.random(); |
|
11 let test_state = { windows: [{ "tabs": [{ "entries": [] }], _closedTabs: [ |
|
12 { state: { entries: [{ url: "http://www.example.net/" }] }, title: FORGET }, |
|
13 { state: { entries: [{ url: "http://www.example.net/" }] }, title: REMEMBER }, |
|
14 { state: { entries: [{ url: "http://www.example.net/" }] }, title: FORGET }, |
|
15 { state: { entries: [{ url: "http://www.example.net/" }] }, title: REMEMBER }, |
|
16 ] }] }; |
|
17 let remember_count = 2; |
|
18 |
|
19 function countByTitle(aClosedTabList, aTitle) |
|
20 aClosedTabList.filter(function(aData) aData.title == aTitle).length; |
|
21 |
|
22 function testForError(aFunction) { |
|
23 try { |
|
24 aFunction(); |
|
25 return false; |
|
26 } |
|
27 catch (ex) { |
|
28 return ex.name == "NS_ERROR_ILLEGAL_VALUE"; |
|
29 } |
|
30 } |
|
31 |
|
32 // open a window and add the above closed tab list |
|
33 let newWin = openDialog(location, "", "chrome,all,dialog=no"); |
|
34 newWin.addEventListener("load", function(aEvent) { |
|
35 newWin.removeEventListener("load", arguments.callee, false); |
|
36 |
|
37 gPrefService.setIntPref("browser.sessionstore.max_tabs_undo", |
|
38 test_state.windows[0]._closedTabs.length); |
|
39 ss.setWindowState(newWin, JSON.stringify(test_state), true); |
|
40 |
|
41 let closedTabs = JSON.parse(ss.getClosedTabData(newWin)); |
|
42 is(closedTabs.length, test_state.windows[0]._closedTabs.length, |
|
43 "Closed tab list has the expected length"); |
|
44 is(countByTitle(closedTabs, FORGET), |
|
45 test_state.windows[0]._closedTabs.length - remember_count, |
|
46 "The correct amout of tabs are to be forgotten"); |
|
47 is(countByTitle(closedTabs, REMEMBER), remember_count, |
|
48 "Everything is set up."); |
|
49 |
|
50 // all of the following calls with illegal arguments should throw NS_ERROR_ILLEGAL_VALUE |
|
51 ok(testForError(function() ss.forgetClosedTab({}, 0)), |
|
52 "Invalid window for forgetClosedTab throws"); |
|
53 ok(testForError(function() ss.forgetClosedTab(newWin, -1)), |
|
54 "Invalid tab for forgetClosedTab throws"); |
|
55 ok(testForError(function() ss.forgetClosedTab(newWin, test_state.windows[0]._closedTabs.length + 1)), |
|
56 "Invalid tab for forgetClosedTab throws"); |
|
57 |
|
58 // Remove third tab, then first tab |
|
59 ss.forgetClosedTab(newWin, 2); |
|
60 ss.forgetClosedTab(newWin, null); |
|
61 |
|
62 closedTabs = JSON.parse(ss.getClosedTabData(newWin)); |
|
63 is(closedTabs.length, remember_count, |
|
64 "The correct amout of tabs was removed"); |
|
65 is(countByTitle(closedTabs, FORGET), 0, |
|
66 "All tabs specifically forgotten were indeed removed"); |
|
67 is(countByTitle(closedTabs, REMEMBER), remember_count, |
|
68 "... and tabs not specifically forgetten weren't."); |
|
69 |
|
70 // clean up |
|
71 newWin.close(); |
|
72 gPrefService.clearUserPref("browser.sessionstore.max_tabs_undo"); |
|
73 finish(); |
|
74 }, false); |
|
75 } |