michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: function test() { michael@0: /** Test for Bug 491577 **/ michael@0: michael@0: // test setup michael@0: waitForExplicitFinish(); michael@0: michael@0: const REMEMBER = Date.now(), FORGET = Math.random(); michael@0: let test_state = { michael@0: windows: [ { tabs: [{ entries: [{ url: "http://example.com/" }] }], selected: 1 } ], michael@0: _closedWindows : [ michael@0: // _closedWindows[0] michael@0: { michael@0: tabs: [ michael@0: { entries: [{ url: "http://example.com/", title: "title" }] }, michael@0: { entries: [{ url: "http://mozilla.org/", title: "title" }] } michael@0: ], michael@0: selected: 2, michael@0: title: FORGET, michael@0: _closedTabs: [] michael@0: }, michael@0: // _closedWindows[1] michael@0: { michael@0: tabs: [ michael@0: { entries: [{ url: "http://mozilla.org/", title: "title" }] }, michael@0: { entries: [{ url: "http://example.com/", title: "title" }] }, michael@0: { entries: [{ url: "http://mozilla.org/", title: "title" }] }, michael@0: ], michael@0: selected: 3, michael@0: title: REMEMBER, michael@0: _closedTabs: [] michael@0: }, michael@0: // _closedWindows[2] michael@0: { michael@0: tabs: [ michael@0: { entries: [{ url: "http://example.com/", title: "title" }] } michael@0: ], michael@0: selected: 1, michael@0: title: FORGET, michael@0: _closedTabs: [ michael@0: { michael@0: state: { michael@0: entries: [ michael@0: { url: "http://mozilla.org/", title: "title" }, michael@0: { url: "http://mozilla.org/again", title: "title" } michael@0: ] michael@0: }, michael@0: pos: 1, michael@0: title: "title" michael@0: }, michael@0: { michael@0: state: { michael@0: entries: [ michael@0: { url: "http://example.com", title: "title" } michael@0: ] michael@0: }, michael@0: title: "title" michael@0: } michael@0: ] michael@0: } michael@0: ] michael@0: }; michael@0: let remember_count = 1; michael@0: michael@0: function countByTitle(aClosedWindowList, aTitle) michael@0: aClosedWindowList.filter(function(aData) aData.title == aTitle).length; michael@0: michael@0: function testForError(aFunction) { michael@0: try { michael@0: aFunction(); michael@0: return false; michael@0: } michael@0: catch (ex) { michael@0: return ex.name == "NS_ERROR_ILLEGAL_VALUE"; michael@0: } michael@0: } michael@0: michael@0: // open a window and add the above closed window list michael@0: let newWin = openDialog(location, "_blank", "chrome,all,dialog=no"); michael@0: newWin.addEventListener("load", function(aEvent) { michael@0: this.removeEventListener("load", arguments.callee, false); michael@0: gPrefService.setIntPref("browser.sessionstore.max_windows_undo", michael@0: test_state._closedWindows.length); michael@0: ss.setWindowState(newWin, JSON.stringify(test_state), true); michael@0: michael@0: let closedWindows = JSON.parse(ss.getClosedWindowData()); michael@0: is(closedWindows.length, test_state._closedWindows.length, michael@0: "Closed window list has the expected length"); michael@0: is(countByTitle(closedWindows, FORGET), michael@0: test_state._closedWindows.length - remember_count, michael@0: "The correct amount of windows are to be forgotten"); michael@0: is(countByTitle(closedWindows, REMEMBER), remember_count, michael@0: "Everything is set up."); michael@0: michael@0: // all of the following calls with illegal arguments should throw NS_ERROR_ILLEGAL_VALUE michael@0: ok(testForError(function() ss.forgetClosedWindow(-1)), michael@0: "Invalid window for forgetClosedWindow throws"); michael@0: ok(testForError(function() ss.forgetClosedWindow(test_state._closedWindows.length + 1)), michael@0: "Invalid window for forgetClosedWindow throws"); michael@0: michael@0: // Remove third window, then first window michael@0: ss.forgetClosedWindow(2); michael@0: ss.forgetClosedWindow(null); michael@0: michael@0: closedWindows = JSON.parse(ss.getClosedWindowData()); michael@0: is(closedWindows.length, remember_count, michael@0: "The correct amount of windows were removed"); michael@0: is(countByTitle(closedWindows, FORGET), 0, michael@0: "All windows specifically forgotten were indeed removed"); michael@0: is(countByTitle(closedWindows, REMEMBER), remember_count, michael@0: "... and windows not specifically forgetten weren't."); michael@0: michael@0: // clean up michael@0: newWin.close(); michael@0: gPrefService.clearUserPref("browser.sessionstore.max_windows_undo"); michael@0: finish(); michael@0: }, false); michael@0: }