1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/sessionstore/test/browser_merge_closed_tabs.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,71 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +/** 1.8 + * This test ensures that closed tabs are merged when restoring 1.9 + * a window state without overwriting tabs. 1.10 + */ 1.11 +add_task(function () { 1.12 + const initialState = { 1.13 + windows: [{ 1.14 + tabs: [ 1.15 + { entries: [{ url: "about:blank" }] } 1.16 + ], 1.17 + _closedTabs: [ 1.18 + { state: { entries: [{ ID: 1000, url: "about:blank" }]} }, 1.19 + { state: { entries: [{ ID: 1001, url: "about:blank" }]} } 1.20 + ] 1.21 + }] 1.22 + } 1.23 + 1.24 + const restoreState = { 1.25 + windows: [{ 1.26 + tabs: [ 1.27 + { entries: [{ url: "about:robots" }] } 1.28 + ], 1.29 + _closedTabs: [ 1.30 + { state: { entries: [{ ID: 1002, url: "about:robots" }]} }, 1.31 + { state: { entries: [{ ID: 1003, url: "about:robots" }]} }, 1.32 + { state: { entries: [{ ID: 1004, url: "about:robots" }]} } 1.33 + ] 1.34 + }] 1.35 + } 1.36 + 1.37 + const maxTabsUndo = 4; 1.38 + gPrefService.setIntPref("browser.sessionstore.max_tabs_undo", maxTabsUndo); 1.39 + 1.40 + // Open a new window and restore it to an initial state. 1.41 + let win = yield promiseNewWindowLoaded({private: false}); 1.42 + SessionStore.setWindowState(win, JSON.stringify(initialState), true); 1.43 + is(SessionStore.getClosedTabCount(win), 2, "2 closed tabs after restoring initial state"); 1.44 + 1.45 + // Restore the new state but do not overwrite existing tabs (this should 1.46 + // cause the closed tabs to be merged). 1.47 + SessionStore.setWindowState(win, JSON.stringify(restoreState), false); 1.48 + 1.49 + // Verify the windows closed tab data is correct. 1.50 + let iClosed = initialState.windows[0]._closedTabs; 1.51 + let rClosed = restoreState.windows[0]._closedTabs; 1.52 + let cData = JSON.parse(SessionStore.getClosedTabData(win)); 1.53 + 1.54 + is(cData.length, Math.min(iClosed.length + rClosed.length, maxTabsUndo), 1.55 + "Number of closed tabs is correct"); 1.56 + 1.57 + // When the closed tabs are merged the restored tabs are considered to be 1.58 + // closed more recently. 1.59 + for (let i = 0; i < cData.length; i++) { 1.60 + if (i < rClosed.length) { 1.61 + is(cData[i].state.entries[0].ID, rClosed[i].state.entries[0].ID, 1.62 + "Closed tab entry matches"); 1.63 + } else { 1.64 + is(cData[i].state.entries[0].ID, iClosed[i - rClosed.length].state.entries[0].ID, 1.65 + "Closed tab entry matches"); 1.66 + } 1.67 + } 1.68 + 1.69 + // Clean up. 1.70 + gPrefService.clearUserPref("browser.sessionstore.max_tabs_undo"); 1.71 + win.close(); 1.72 +}); 1.73 + 1.74 +