1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/sessionstore/test/browser_491577.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,121 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +function test() { 1.9 + /** Test for Bug 491577 **/ 1.10 + 1.11 + // test setup 1.12 + waitForExplicitFinish(); 1.13 + 1.14 + const REMEMBER = Date.now(), FORGET = Math.random(); 1.15 + let test_state = { 1.16 + windows: [ { tabs: [{ entries: [{ url: "http://example.com/" }] }], selected: 1 } ], 1.17 + _closedWindows : [ 1.18 + // _closedWindows[0] 1.19 + { 1.20 + tabs: [ 1.21 + { entries: [{ url: "http://example.com/", title: "title" }] }, 1.22 + { entries: [{ url: "http://mozilla.org/", title: "title" }] } 1.23 + ], 1.24 + selected: 2, 1.25 + title: FORGET, 1.26 + _closedTabs: [] 1.27 + }, 1.28 + // _closedWindows[1] 1.29 + { 1.30 + tabs: [ 1.31 + { entries: [{ url: "http://mozilla.org/", title: "title" }] }, 1.32 + { entries: [{ url: "http://example.com/", title: "title" }] }, 1.33 + { entries: [{ url: "http://mozilla.org/", title: "title" }] }, 1.34 + ], 1.35 + selected: 3, 1.36 + title: REMEMBER, 1.37 + _closedTabs: [] 1.38 + }, 1.39 + // _closedWindows[2] 1.40 + { 1.41 + tabs: [ 1.42 + { entries: [{ url: "http://example.com/", title: "title" }] } 1.43 + ], 1.44 + selected: 1, 1.45 + title: FORGET, 1.46 + _closedTabs: [ 1.47 + { 1.48 + state: { 1.49 + entries: [ 1.50 + { url: "http://mozilla.org/", title: "title" }, 1.51 + { url: "http://mozilla.org/again", title: "title" } 1.52 + ] 1.53 + }, 1.54 + pos: 1, 1.55 + title: "title" 1.56 + }, 1.57 + { 1.58 + state: { 1.59 + entries: [ 1.60 + { url: "http://example.com", title: "title" } 1.61 + ] 1.62 + }, 1.63 + title: "title" 1.64 + } 1.65 + ] 1.66 + } 1.67 + ] 1.68 + }; 1.69 + let remember_count = 1; 1.70 + 1.71 + function countByTitle(aClosedWindowList, aTitle) 1.72 + aClosedWindowList.filter(function(aData) aData.title == aTitle).length; 1.73 + 1.74 + function testForError(aFunction) { 1.75 + try { 1.76 + aFunction(); 1.77 + return false; 1.78 + } 1.79 + catch (ex) { 1.80 + return ex.name == "NS_ERROR_ILLEGAL_VALUE"; 1.81 + } 1.82 + } 1.83 + 1.84 + // open a window and add the above closed window list 1.85 + let newWin = openDialog(location, "_blank", "chrome,all,dialog=no"); 1.86 + newWin.addEventListener("load", function(aEvent) { 1.87 + this.removeEventListener("load", arguments.callee, false); 1.88 + gPrefService.setIntPref("browser.sessionstore.max_windows_undo", 1.89 + test_state._closedWindows.length); 1.90 + ss.setWindowState(newWin, JSON.stringify(test_state), true); 1.91 + 1.92 + let closedWindows = JSON.parse(ss.getClosedWindowData()); 1.93 + is(closedWindows.length, test_state._closedWindows.length, 1.94 + "Closed window list has the expected length"); 1.95 + is(countByTitle(closedWindows, FORGET), 1.96 + test_state._closedWindows.length - remember_count, 1.97 + "The correct amount of windows are to be forgotten"); 1.98 + is(countByTitle(closedWindows, REMEMBER), remember_count, 1.99 + "Everything is set up."); 1.100 + 1.101 + // all of the following calls with illegal arguments should throw NS_ERROR_ILLEGAL_VALUE 1.102 + ok(testForError(function() ss.forgetClosedWindow(-1)), 1.103 + "Invalid window for forgetClosedWindow throws"); 1.104 + ok(testForError(function() ss.forgetClosedWindow(test_state._closedWindows.length + 1)), 1.105 + "Invalid window for forgetClosedWindow throws"); 1.106 + 1.107 + // Remove third window, then first window 1.108 + ss.forgetClosedWindow(2); 1.109 + ss.forgetClosedWindow(null); 1.110 + 1.111 + closedWindows = JSON.parse(ss.getClosedWindowData()); 1.112 + is(closedWindows.length, remember_count, 1.113 + "The correct amount of windows were removed"); 1.114 + is(countByTitle(closedWindows, FORGET), 0, 1.115 + "All windows specifically forgotten were indeed removed"); 1.116 + is(countByTitle(closedWindows, REMEMBER), remember_count, 1.117 + "... and windows not specifically forgetten weren't."); 1.118 + 1.119 + // clean up 1.120 + newWin.close(); 1.121 + gPrefService.clearUserPref("browser.sessionstore.max_windows_undo"); 1.122 + finish(); 1.123 + }, false); 1.124 +}