michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: "use strict"; michael@0: michael@0: /** michael@0: * This test ensures that the preferences (added in bug 943339) that control how michael@0: * many back and forward button session history entries we store work correctly. michael@0: * michael@0: * It adds a number of entries to the session history, restores them and checks michael@0: * that the restored state matches the preferences. michael@0: */ michael@0: michael@0: add_task(function *test_history_cap() { michael@0: const baseURL = "http://example.com/browser_history_cap#" michael@0: const maxEntries = 9; // The number of generated session history entries. michael@0: const middleEntry = 4; // The zero-based index of the middle entry. michael@0: michael@0: const maxBack1 = 2; // The history cap settings used for the first test, michael@0: const maxFwd1 = 3; // where maxBack1 + 1 + maxFwd1 < maxEntries. michael@0: michael@0: const maxBack2 = 5; // The history cap settings used for the other tests, michael@0: const maxFwd2 = 5; // where maxBack2 + 1 + maxFwd2 > maxEntries. michael@0: michael@0: // Set the relevant preferences for the first test. michael@0: gPrefService.setIntPref("browser.sessionhistory.max_entries", maxEntries); michael@0: gPrefService.setIntPref("browser.sessionstore.max_serialize_back", maxBack1); michael@0: gPrefService.setIntPref("browser.sessionstore.max_serialize_forward", maxFwd1); michael@0: michael@0: // Make sure the settings we modify are reset afterward. michael@0: registerCleanupFunction(() => { michael@0: gPrefService.clearUserPref("browser.sessionhistory.max_entries"); michael@0: gPrefService.clearUserPref("browser.sessionstore.max_serialize_back"); michael@0: gPrefService.clearUserPref("browser.sessionstore.max_serialize_forward"); michael@0: }); michael@0: michael@0: let tab = gBrowser.addTab(); michael@0: let browser = tab.linkedBrowser; michael@0: yield promiseBrowserLoaded(browser); michael@0: michael@0: // Generate the tab state entries and set the one-based michael@0: // tab-state index to the middle session history entry. michael@0: let tabState = {entries: [], index: middleEntry + 1}; michael@0: for (let i = 0; i < maxEntries; i++) { michael@0: tabState.entries.push({url: baseURL + i}); michael@0: } michael@0: michael@0: info("Testing situation where only a subset of session history entries should be restored."); michael@0: michael@0: ss.setTabState(tab, JSON.stringify(tabState)); michael@0: yield promiseTabRestored(tab); michael@0: SyncHandlers.get(tab.linkedBrowser).flush(); michael@0: michael@0: let restoredTabState = JSON.parse(ss.getTabState(tab)); michael@0: is(restoredTabState.entries.length, maxBack1 + 1 + maxFwd1, michael@0: "The expected number of session history entries was restored."); michael@0: is(restoredTabState.index, maxBack1 + 1, "The restored tab-state index is correct"); michael@0: michael@0: let indexURLOffset = middleEntry - (restoredTabState.index - 1); michael@0: for (let i = 0; i < restoredTabState.entries.length; i++) { michael@0: is(restoredTabState.entries[i].url, baseURL + (i + indexURLOffset), michael@0: "URL of restored entry matches the expected URL."); michael@0: } michael@0: michael@0: // Set the relevant preferences for the other tests. michael@0: gPrefService.setIntPref("browser.sessionstore.max_serialize_back", maxBack2); michael@0: gPrefService.setIntPref("browser.sessionstore.max_serialize_forward", maxFwd2); michael@0: michael@0: info("Testing situation where all of the entries in the session history should be restored."); michael@0: michael@0: ss.setTabState(tab, JSON.stringify(tabState)); michael@0: yield promiseTabRestored(tab); michael@0: SyncHandlers.get(tab.linkedBrowser).flush(); michael@0: michael@0: restoredTabState = JSON.parse(ss.getTabState(tab)); michael@0: is(restoredTabState.entries.length, maxEntries, michael@0: "The expected number of session history entries was restored."); michael@0: is(restoredTabState.index, middleEntry + 1, "The restored tab-state index is correct"); michael@0: michael@0: for (let i = middleEntry - 2; i <= middleEntry + 2; i++) { michael@0: is(restoredTabState.entries[i].url, baseURL + i, michael@0: "URL of restored entry matches the expected URL."); michael@0: } michael@0: michael@0: info("Testing situation where only the 1 + maxFwd2 oldest entries should be restored."); michael@0: michael@0: // Set the one-based tab-state index to the oldest session history entry. michael@0: tabState.index = 1; michael@0: michael@0: ss.setTabState(tab, JSON.stringify(tabState)); michael@0: yield promiseTabRestored(tab); michael@0: SyncHandlers.get(tab.linkedBrowser).flush(); michael@0: michael@0: restoredTabState = JSON.parse(ss.getTabState(tab)); michael@0: is(restoredTabState.entries.length, 1 + maxFwd2, michael@0: "The expected number of session history entries was restored."); michael@0: is(restoredTabState.index, 1, "The restored tab-state index is correct"); michael@0: michael@0: for (let i = 0; i <= 2; i++) { michael@0: is(restoredTabState.entries[i].url, baseURL + i, michael@0: "URL of restored entry matches the expected URL."); michael@0: } michael@0: michael@0: info("Testing situation where only the maxBack2 + 1 newest entries should be restored."); michael@0: michael@0: // Set the one-based tab-state index to the newest session history entry. michael@0: tabState.index = maxEntries; michael@0: michael@0: ss.setTabState(tab, JSON.stringify(tabState)); michael@0: yield promiseTabRestored(tab); michael@0: SyncHandlers.get(tab.linkedBrowser).flush(); michael@0: michael@0: restoredTabState = JSON.parse(ss.getTabState(tab)); michael@0: is(restoredTabState.entries.length, maxBack2 + 1, michael@0: "The expected number of session history entries was restored."); michael@0: is(restoredTabState.index, maxBack2 + 1, "The restored tab-state index is correct"); michael@0: michael@0: indexURLOffset = (maxEntries - 1) - maxBack2; michael@0: for (let i = maxBack2 - 2; i <= maxBack2; i++) { michael@0: is(restoredTabState.entries[i].url, baseURL + (i + indexURLOffset), michael@0: "URL of restored entry matches the expected URL."); michael@0: } michael@0: michael@0: gBrowser.removeTab(tab); michael@0: });