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 461634 **/ michael@0: michael@0: waitForExplicitFinish(); michael@0: michael@0: const REMEMBER = Date.now(), FORGET = Math.random(); michael@0: let test_state = { windows: [{ "tabs": [{ "entries": [] }], _closedTabs: [ michael@0: { state: { entries: [{ url: "http://www.example.net/" }] }, title: FORGET }, michael@0: { state: { entries: [{ url: "http://www.example.net/" }] }, title: REMEMBER }, michael@0: { state: { entries: [{ url: "http://www.example.net/" }] }, title: FORGET }, michael@0: { state: { entries: [{ url: "http://www.example.net/" }] }, title: REMEMBER }, michael@0: ] }] }; michael@0: let remember_count = 2; michael@0: michael@0: function countByTitle(aClosedTabList, aTitle) michael@0: aClosedTabList.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 tab list michael@0: let newWin = openDialog(location, "", "chrome,all,dialog=no"); michael@0: newWin.addEventListener("load", function(aEvent) { michael@0: newWin.removeEventListener("load", arguments.callee, false); michael@0: michael@0: gPrefService.setIntPref("browser.sessionstore.max_tabs_undo", michael@0: test_state.windows[0]._closedTabs.length); michael@0: ss.setWindowState(newWin, JSON.stringify(test_state), true); michael@0: michael@0: let closedTabs = JSON.parse(ss.getClosedTabData(newWin)); michael@0: is(closedTabs.length, test_state.windows[0]._closedTabs.length, michael@0: "Closed tab list has the expected length"); michael@0: is(countByTitle(closedTabs, FORGET), michael@0: test_state.windows[0]._closedTabs.length - remember_count, michael@0: "The correct amout of tabs are to be forgotten"); michael@0: is(countByTitle(closedTabs, 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.forgetClosedTab({}, 0)), michael@0: "Invalid window for forgetClosedTab throws"); michael@0: ok(testForError(function() ss.forgetClosedTab(newWin, -1)), michael@0: "Invalid tab for forgetClosedTab throws"); michael@0: ok(testForError(function() ss.forgetClosedTab(newWin, test_state.windows[0]._closedTabs.length + 1)), michael@0: "Invalid tab for forgetClosedTab throws"); michael@0: michael@0: // Remove third tab, then first tab michael@0: ss.forgetClosedTab(newWin, 2); michael@0: ss.forgetClosedTab(newWin, null); michael@0: michael@0: closedTabs = JSON.parse(ss.getClosedTabData(newWin)); michael@0: is(closedTabs.length, remember_count, michael@0: "The correct amout of tabs was removed"); michael@0: is(countByTitle(closedTabs, FORGET), 0, michael@0: "All tabs specifically forgotten were indeed removed"); michael@0: is(countByTitle(closedTabs, REMEMBER), remember_count, michael@0: "... and tabs not specifically forgetten weren't."); michael@0: michael@0: // clean up michael@0: newWin.close(); michael@0: gPrefService.clearUserPref("browser.sessionstore.max_tabs_undo"); michael@0: finish(); michael@0: }, false); michael@0: }