1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/sessionstore/test/browser_586068-reload.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,140 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +const PREF_RESTORE_ON_DEMAND = "browser.sessionstore.restore_on_demand"; 1.9 + 1.10 +function test() { 1.11 + TestRunner.run(); 1.12 +} 1.13 + 1.14 +function runTests() { 1.15 + // Request a longer timeout because the test takes quite a while 1.16 + // to complete on slow Windows debug machines and we would otherwise 1.17 + // see a lot of (not so) intermittent test failures. 1.18 + requestLongerTimeout(2); 1.19 + 1.20 + Services.prefs.setBoolPref(PREF_RESTORE_ON_DEMAND, true); 1.21 + registerCleanupFunction(function () { 1.22 + Services.prefs.clearUserPref(PREF_RESTORE_ON_DEMAND); 1.23 + }); 1.24 + 1.25 + let state = { windows: [{ tabs: [ 1.26 + { entries: [{ url: "http://example.org/#1" }], extData: { "uniq": r() } }, 1.27 + { entries: [{ url: "http://example.org/#2" }], extData: { "uniq": r() } }, 1.28 + { entries: [{ url: "http://example.org/#3" }], extData: { "uniq": r() } }, 1.29 + { entries: [{ url: "http://example.org/#4" }], extData: { "uniq": r() } }, 1.30 + { entries: [{ url: "http://example.org/#5" }], extData: { "uniq": r() } }, 1.31 + { entries: [{ url: "http://example.org/#6" }], extData: { "uniq": r() } }, 1.32 + { entries: [{ url: "http://example.org/#7" }], extData: { "uniq": r() } }, 1.33 + { entries: [{ url: "http://example.org/#8" }], extData: { "uniq": r() } }, 1.34 + { entries: [{ url: "http://example.org/#9" }], extData: { "uniq": r() } }, 1.35 + { entries: [{ url: "http://example.org/#10" }], extData: { "uniq": r() } }, 1.36 + { entries: [{ url: "http://example.org/#11" }], extData: { "uniq": r() } }, 1.37 + { entries: [{ url: "http://example.org/#12" }], extData: { "uniq": r() } }, 1.38 + { entries: [{ url: "http://example.org/#13" }], extData: { "uniq": r() } }, 1.39 + { entries: [{ url: "http://example.org/#14" }], extData: { "uniq": r() } }, 1.40 + { entries: [{ url: "http://example.org/#15" }], extData: { "uniq": r() } }, 1.41 + { entries: [{ url: "http://example.org/#16" }], extData: { "uniq": r() } }, 1.42 + { entries: [{ url: "http://example.org/#17" }], extData: { "uniq": r() } }, 1.43 + { entries: [{ url: "http://example.org/#18" }], extData: { "uniq": r() } } 1.44 + ], selected: 1 }] }; 1.45 + 1.46 + let loadCount = 0; 1.47 + gProgressListener.setCallback(function (aBrowser, aNeedRestore, aRestoring, aRestored) { 1.48 + loadCount++; 1.49 + is(aBrowser.currentURI.spec, state.windows[0].tabs[loadCount - 1].entries[0].url, 1.50 + "load " + loadCount + " - browser loaded correct url"); 1.51 + 1.52 + if (loadCount <= state.windows[0].tabs.length) { 1.53 + // double check that this tab was the right one 1.54 + let expectedData = state.windows[0].tabs[loadCount - 1].extData.uniq; 1.55 + let tab; 1.56 + for (let i = 0; i < window.gBrowser.tabs.length; i++) { 1.57 + if (!tab && window.gBrowser.tabs[i].linkedBrowser == aBrowser) 1.58 + tab = window.gBrowser.tabs[i]; 1.59 + } 1.60 + is(ss.getTabValue(tab, "uniq"), expectedData, 1.61 + "load " + loadCount + " - correct tab was restored"); 1.62 + 1.63 + if (loadCount == state.windows[0].tabs.length) { 1.64 + gProgressListener.unsetCallback(); 1.65 + executeSoon(function () { 1.66 + reloadAllTabs(state, function () { 1.67 + waitForBrowserState(TestRunner.backupState, testCascade); 1.68 + }); 1.69 + }); 1.70 + } else { 1.71 + // reload the next tab 1.72 + window.gBrowser.reloadTab(window.gBrowser.tabs[loadCount]); 1.73 + } 1.74 + } 1.75 + }); 1.76 + 1.77 + yield ss.setBrowserState(JSON.stringify(state)); 1.78 +} 1.79 + 1.80 +function testCascade() { 1.81 + Services.prefs.setBoolPref(PREF_RESTORE_ON_DEMAND, false); 1.82 + 1.83 + let state = { windows: [{ tabs: [ 1.84 + { entries: [{ url: "http://example.com/#1" }], extData: { "uniq": r() } }, 1.85 + { entries: [{ url: "http://example.com/#2" }], extData: { "uniq": r() } }, 1.86 + { entries: [{ url: "http://example.com/#3" }], extData: { "uniq": r() } }, 1.87 + { entries: [{ url: "http://example.com/#4" }], extData: { "uniq": r() } }, 1.88 + { entries: [{ url: "http://example.com/#5" }], extData: { "uniq": r() } }, 1.89 + { entries: [{ url: "http://example.com/#6" }], extData: { "uniq": r() } } 1.90 + ] }] }; 1.91 + 1.92 + let loadCount = 0; 1.93 + gProgressListener.setCallback(function (aBrowser, aNeedRestore, aRestoring, aRestored) { 1.94 + if (++loadCount < state.windows[0].tabs.length) { 1.95 + return; 1.96 + } 1.97 + 1.98 + gProgressListener.unsetCallback(); 1.99 + executeSoon(function () { 1.100 + reloadAllTabs(state, next); 1.101 + }); 1.102 + }); 1.103 + 1.104 + ss.setBrowserState(JSON.stringify(state)); 1.105 +} 1.106 + 1.107 +function reloadAllTabs(aState, aCallback) { 1.108 + // Simulate a left mouse button click with no modifiers, which is what 1.109 + // Command-R, or clicking reload does. 1.110 + let fakeEvent = { 1.111 + button: 0, 1.112 + metaKey: false, 1.113 + altKey: false, 1.114 + ctrlKey: false, 1.115 + shiftKey: false 1.116 + }; 1.117 + 1.118 + let loadCount = 0; 1.119 + gWebProgressListener.setCallback(function (aBrowser) { 1.120 + if (++loadCount <= aState.windows[0].tabs.length) { 1.121 + // double check that this tab was the right one 1.122 + let expectedData = aState.windows[0].tabs[loadCount - 1].extData.uniq; 1.123 + let tab; 1.124 + for (let i = 0; i < window.gBrowser.tabs.length; i++) { 1.125 + if (!tab && window.gBrowser.tabs[i].linkedBrowser == aBrowser) 1.126 + tab = window.gBrowser.tabs[i]; 1.127 + } 1.128 + is(ss.getTabValue(tab, "uniq"), expectedData, 1.129 + "load " + loadCount + " - correct tab was reloaded"); 1.130 + 1.131 + if (loadCount == aState.windows[0].tabs.length) { 1.132 + gWebProgressListener.unsetCallback(); 1.133 + executeSoon(aCallback); 1.134 + } else { 1.135 + // reload the next tab 1.136 + window.gBrowser.selectTabAtIndex(loadCount); 1.137 + BrowserReloadOrDuplicate(fakeEvent); 1.138 + } 1.139 + } 1.140 + }); 1.141 + 1.142 + BrowserReloadOrDuplicate(fakeEvent); 1.143 +}