browser/components/sessionstore/test/browser_history_cap.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 * http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 "use strict";
michael@0 5
michael@0 6 /**
michael@0 7 * This test ensures that the preferences (added in bug 943339) that control how
michael@0 8 * many back and forward button session history entries we store work correctly.
michael@0 9 *
michael@0 10 * It adds a number of entries to the session history, restores them and checks
michael@0 11 * that the restored state matches the preferences.
michael@0 12 */
michael@0 13
michael@0 14 add_task(function *test_history_cap() {
michael@0 15 const baseURL = "http://example.com/browser_history_cap#"
michael@0 16 const maxEntries = 9; // The number of generated session history entries.
michael@0 17 const middleEntry = 4; // The zero-based index of the middle entry.
michael@0 18
michael@0 19 const maxBack1 = 2; // The history cap settings used for the first test,
michael@0 20 const maxFwd1 = 3; // where maxBack1 + 1 + maxFwd1 < maxEntries.
michael@0 21
michael@0 22 const maxBack2 = 5; // The history cap settings used for the other tests,
michael@0 23 const maxFwd2 = 5; // where maxBack2 + 1 + maxFwd2 > maxEntries.
michael@0 24
michael@0 25 // Set the relevant preferences for the first test.
michael@0 26 gPrefService.setIntPref("browser.sessionhistory.max_entries", maxEntries);
michael@0 27 gPrefService.setIntPref("browser.sessionstore.max_serialize_back", maxBack1);
michael@0 28 gPrefService.setIntPref("browser.sessionstore.max_serialize_forward", maxFwd1);
michael@0 29
michael@0 30 // Make sure the settings we modify are reset afterward.
michael@0 31 registerCleanupFunction(() => {
michael@0 32 gPrefService.clearUserPref("browser.sessionhistory.max_entries");
michael@0 33 gPrefService.clearUserPref("browser.sessionstore.max_serialize_back");
michael@0 34 gPrefService.clearUserPref("browser.sessionstore.max_serialize_forward");
michael@0 35 });
michael@0 36
michael@0 37 let tab = gBrowser.addTab();
michael@0 38 let browser = tab.linkedBrowser;
michael@0 39 yield promiseBrowserLoaded(browser);
michael@0 40
michael@0 41 // Generate the tab state entries and set the one-based
michael@0 42 // tab-state index to the middle session history entry.
michael@0 43 let tabState = {entries: [], index: middleEntry + 1};
michael@0 44 for (let i = 0; i < maxEntries; i++) {
michael@0 45 tabState.entries.push({url: baseURL + i});
michael@0 46 }
michael@0 47
michael@0 48 info("Testing situation where only a subset of session history entries should be restored.");
michael@0 49
michael@0 50 ss.setTabState(tab, JSON.stringify(tabState));
michael@0 51 yield promiseTabRestored(tab);
michael@0 52 SyncHandlers.get(tab.linkedBrowser).flush();
michael@0 53
michael@0 54 let restoredTabState = JSON.parse(ss.getTabState(tab));
michael@0 55 is(restoredTabState.entries.length, maxBack1 + 1 + maxFwd1,
michael@0 56 "The expected number of session history entries was restored.");
michael@0 57 is(restoredTabState.index, maxBack1 + 1, "The restored tab-state index is correct");
michael@0 58
michael@0 59 let indexURLOffset = middleEntry - (restoredTabState.index - 1);
michael@0 60 for (let i = 0; i < restoredTabState.entries.length; i++) {
michael@0 61 is(restoredTabState.entries[i].url, baseURL + (i + indexURLOffset),
michael@0 62 "URL of restored entry matches the expected URL.");
michael@0 63 }
michael@0 64
michael@0 65 // Set the relevant preferences for the other tests.
michael@0 66 gPrefService.setIntPref("browser.sessionstore.max_serialize_back", maxBack2);
michael@0 67 gPrefService.setIntPref("browser.sessionstore.max_serialize_forward", maxFwd2);
michael@0 68
michael@0 69 info("Testing situation where all of the entries in the session history should be restored.");
michael@0 70
michael@0 71 ss.setTabState(tab, JSON.stringify(tabState));
michael@0 72 yield promiseTabRestored(tab);
michael@0 73 SyncHandlers.get(tab.linkedBrowser).flush();
michael@0 74
michael@0 75 restoredTabState = JSON.parse(ss.getTabState(tab));
michael@0 76 is(restoredTabState.entries.length, maxEntries,
michael@0 77 "The expected number of session history entries was restored.");
michael@0 78 is(restoredTabState.index, middleEntry + 1, "The restored tab-state index is correct");
michael@0 79
michael@0 80 for (let i = middleEntry - 2; i <= middleEntry + 2; i++) {
michael@0 81 is(restoredTabState.entries[i].url, baseURL + i,
michael@0 82 "URL of restored entry matches the expected URL.");
michael@0 83 }
michael@0 84
michael@0 85 info("Testing situation where only the 1 + maxFwd2 oldest entries should be restored.");
michael@0 86
michael@0 87 // Set the one-based tab-state index to the oldest session history entry.
michael@0 88 tabState.index = 1;
michael@0 89
michael@0 90 ss.setTabState(tab, JSON.stringify(tabState));
michael@0 91 yield promiseTabRestored(tab);
michael@0 92 SyncHandlers.get(tab.linkedBrowser).flush();
michael@0 93
michael@0 94 restoredTabState = JSON.parse(ss.getTabState(tab));
michael@0 95 is(restoredTabState.entries.length, 1 + maxFwd2,
michael@0 96 "The expected number of session history entries was restored.");
michael@0 97 is(restoredTabState.index, 1, "The restored tab-state index is correct");
michael@0 98
michael@0 99 for (let i = 0; i <= 2; i++) {
michael@0 100 is(restoredTabState.entries[i].url, baseURL + i,
michael@0 101 "URL of restored entry matches the expected URL.");
michael@0 102 }
michael@0 103
michael@0 104 info("Testing situation where only the maxBack2 + 1 newest entries should be restored.");
michael@0 105
michael@0 106 // Set the one-based tab-state index to the newest session history entry.
michael@0 107 tabState.index = maxEntries;
michael@0 108
michael@0 109 ss.setTabState(tab, JSON.stringify(tabState));
michael@0 110 yield promiseTabRestored(tab);
michael@0 111 SyncHandlers.get(tab.linkedBrowser).flush();
michael@0 112
michael@0 113 restoredTabState = JSON.parse(ss.getTabState(tab));
michael@0 114 is(restoredTabState.entries.length, maxBack2 + 1,
michael@0 115 "The expected number of session history entries was restored.");
michael@0 116 is(restoredTabState.index, maxBack2 + 1, "The restored tab-state index is correct");
michael@0 117
michael@0 118 indexURLOffset = (maxEntries - 1) - maxBack2;
michael@0 119 for (let i = maxBack2 - 2; i <= maxBack2; i++) {
michael@0 120 is(restoredTabState.entries[i].url, baseURL + (i + indexURLOffset),
michael@0 121 "URL of restored entry matches the expected URL.");
michael@0 122 }
michael@0 123
michael@0 124 gBrowser.removeTab(tab);
michael@0 125 });

mercurial