michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: /** michael@0: * This test ensures that closed tabs are merged when restoring michael@0: * a window state without overwriting tabs. michael@0: */ michael@0: add_task(function () { michael@0: const initialState = { michael@0: windows: [{ michael@0: tabs: [ michael@0: { entries: [{ url: "about:blank" }] } michael@0: ], michael@0: _closedTabs: [ michael@0: { state: { entries: [{ ID: 1000, url: "about:blank" }]} }, michael@0: { state: { entries: [{ ID: 1001, url: "about:blank" }]} } michael@0: ] michael@0: }] michael@0: } michael@0: michael@0: const restoreState = { michael@0: windows: [{ michael@0: tabs: [ michael@0: { entries: [{ url: "about:robots" }] } michael@0: ], michael@0: _closedTabs: [ michael@0: { state: { entries: [{ ID: 1002, url: "about:robots" }]} }, michael@0: { state: { entries: [{ ID: 1003, url: "about:robots" }]} }, michael@0: { state: { entries: [{ ID: 1004, url: "about:robots" }]} } michael@0: ] michael@0: }] michael@0: } michael@0: michael@0: const maxTabsUndo = 4; michael@0: gPrefService.setIntPref("browser.sessionstore.max_tabs_undo", maxTabsUndo); michael@0: michael@0: // Open a new window and restore it to an initial state. michael@0: let win = yield promiseNewWindowLoaded({private: false}); michael@0: SessionStore.setWindowState(win, JSON.stringify(initialState), true); michael@0: is(SessionStore.getClosedTabCount(win), 2, "2 closed tabs after restoring initial state"); michael@0: michael@0: // Restore the new state but do not overwrite existing tabs (this should michael@0: // cause the closed tabs to be merged). michael@0: SessionStore.setWindowState(win, JSON.stringify(restoreState), false); michael@0: michael@0: // Verify the windows closed tab data is correct. michael@0: let iClosed = initialState.windows[0]._closedTabs; michael@0: let rClosed = restoreState.windows[0]._closedTabs; michael@0: let cData = JSON.parse(SessionStore.getClosedTabData(win)); michael@0: michael@0: is(cData.length, Math.min(iClosed.length + rClosed.length, maxTabsUndo), michael@0: "Number of closed tabs is correct"); michael@0: michael@0: // When the closed tabs are merged the restored tabs are considered to be michael@0: // closed more recently. michael@0: for (let i = 0; i < cData.length; i++) { michael@0: if (i < rClosed.length) { michael@0: is(cData[i].state.entries[0].ID, rClosed[i].state.entries[0].ID, michael@0: "Closed tab entry matches"); michael@0: } else { michael@0: is(cData[i].state.entries[0].ID, iClosed[i - rClosed.length].state.entries[0].ID, michael@0: "Closed tab entry matches"); michael@0: } michael@0: } michael@0: michael@0: // Clean up. michael@0: gPrefService.clearUserPref("browser.sessionstore.max_tabs_undo"); michael@0: win.close(); michael@0: }); michael@0: michael@0: