|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 const PREF_RESTORE_ON_DEMAND = "browser.sessionstore.restore_on_demand"; |
|
6 |
|
7 function test() { |
|
8 TestRunner.run(); |
|
9 } |
|
10 |
|
11 function runTests() { |
|
12 // Request a longer timeout because the test takes quite a while |
|
13 // to complete on slow Windows debug machines and we would otherwise |
|
14 // see a lot of (not so) intermittent test failures. |
|
15 requestLongerTimeout(2); |
|
16 |
|
17 Services.prefs.setBoolPref(PREF_RESTORE_ON_DEMAND, true); |
|
18 registerCleanupFunction(function () { |
|
19 Services.prefs.clearUserPref(PREF_RESTORE_ON_DEMAND); |
|
20 }); |
|
21 |
|
22 let state = { windows: [{ tabs: [ |
|
23 { entries: [{ url: "http://example.org/#1" }], extData: { "uniq": r() } }, |
|
24 { entries: [{ url: "http://example.org/#2" }], extData: { "uniq": r() } }, |
|
25 { entries: [{ url: "http://example.org/#3" }], extData: { "uniq": r() } }, |
|
26 { entries: [{ url: "http://example.org/#4" }], extData: { "uniq": r() } }, |
|
27 { entries: [{ url: "http://example.org/#5" }], extData: { "uniq": r() } }, |
|
28 { entries: [{ url: "http://example.org/#6" }], extData: { "uniq": r() } }, |
|
29 { entries: [{ url: "http://example.org/#7" }], extData: { "uniq": r() } }, |
|
30 { entries: [{ url: "http://example.org/#8" }], extData: { "uniq": r() } }, |
|
31 { entries: [{ url: "http://example.org/#9" }], extData: { "uniq": r() } }, |
|
32 { entries: [{ url: "http://example.org/#10" }], extData: { "uniq": r() } }, |
|
33 { entries: [{ url: "http://example.org/#11" }], extData: { "uniq": r() } }, |
|
34 { entries: [{ url: "http://example.org/#12" }], extData: { "uniq": r() } }, |
|
35 { entries: [{ url: "http://example.org/#13" }], extData: { "uniq": r() } }, |
|
36 { entries: [{ url: "http://example.org/#14" }], extData: { "uniq": r() } }, |
|
37 { entries: [{ url: "http://example.org/#15" }], extData: { "uniq": r() } }, |
|
38 { entries: [{ url: "http://example.org/#16" }], extData: { "uniq": r() } }, |
|
39 { entries: [{ url: "http://example.org/#17" }], extData: { "uniq": r() } }, |
|
40 { entries: [{ url: "http://example.org/#18" }], extData: { "uniq": r() } } |
|
41 ], selected: 1 }] }; |
|
42 |
|
43 let loadCount = 0; |
|
44 gProgressListener.setCallback(function (aBrowser, aNeedRestore, aRestoring, aRestored) { |
|
45 loadCount++; |
|
46 is(aBrowser.currentURI.spec, state.windows[0].tabs[loadCount - 1].entries[0].url, |
|
47 "load " + loadCount + " - browser loaded correct url"); |
|
48 |
|
49 if (loadCount <= state.windows[0].tabs.length) { |
|
50 // double check that this tab was the right one |
|
51 let expectedData = state.windows[0].tabs[loadCount - 1].extData.uniq; |
|
52 let tab; |
|
53 for (let i = 0; i < window.gBrowser.tabs.length; i++) { |
|
54 if (!tab && window.gBrowser.tabs[i].linkedBrowser == aBrowser) |
|
55 tab = window.gBrowser.tabs[i]; |
|
56 } |
|
57 is(ss.getTabValue(tab, "uniq"), expectedData, |
|
58 "load " + loadCount + " - correct tab was restored"); |
|
59 |
|
60 if (loadCount == state.windows[0].tabs.length) { |
|
61 gProgressListener.unsetCallback(); |
|
62 executeSoon(function () { |
|
63 reloadAllTabs(state, function () { |
|
64 waitForBrowserState(TestRunner.backupState, testCascade); |
|
65 }); |
|
66 }); |
|
67 } else { |
|
68 // reload the next tab |
|
69 window.gBrowser.reloadTab(window.gBrowser.tabs[loadCount]); |
|
70 } |
|
71 } |
|
72 }); |
|
73 |
|
74 yield ss.setBrowserState(JSON.stringify(state)); |
|
75 } |
|
76 |
|
77 function testCascade() { |
|
78 Services.prefs.setBoolPref(PREF_RESTORE_ON_DEMAND, false); |
|
79 |
|
80 let state = { windows: [{ tabs: [ |
|
81 { entries: [{ url: "http://example.com/#1" }], extData: { "uniq": r() } }, |
|
82 { entries: [{ url: "http://example.com/#2" }], extData: { "uniq": r() } }, |
|
83 { entries: [{ url: "http://example.com/#3" }], extData: { "uniq": r() } }, |
|
84 { entries: [{ url: "http://example.com/#4" }], extData: { "uniq": r() } }, |
|
85 { entries: [{ url: "http://example.com/#5" }], extData: { "uniq": r() } }, |
|
86 { entries: [{ url: "http://example.com/#6" }], extData: { "uniq": r() } } |
|
87 ] }] }; |
|
88 |
|
89 let loadCount = 0; |
|
90 gProgressListener.setCallback(function (aBrowser, aNeedRestore, aRestoring, aRestored) { |
|
91 if (++loadCount < state.windows[0].tabs.length) { |
|
92 return; |
|
93 } |
|
94 |
|
95 gProgressListener.unsetCallback(); |
|
96 executeSoon(function () { |
|
97 reloadAllTabs(state, next); |
|
98 }); |
|
99 }); |
|
100 |
|
101 ss.setBrowserState(JSON.stringify(state)); |
|
102 } |
|
103 |
|
104 function reloadAllTabs(aState, aCallback) { |
|
105 // Simulate a left mouse button click with no modifiers, which is what |
|
106 // Command-R, or clicking reload does. |
|
107 let fakeEvent = { |
|
108 button: 0, |
|
109 metaKey: false, |
|
110 altKey: false, |
|
111 ctrlKey: false, |
|
112 shiftKey: false |
|
113 }; |
|
114 |
|
115 let loadCount = 0; |
|
116 gWebProgressListener.setCallback(function (aBrowser) { |
|
117 if (++loadCount <= aState.windows[0].tabs.length) { |
|
118 // double check that this tab was the right one |
|
119 let expectedData = aState.windows[0].tabs[loadCount - 1].extData.uniq; |
|
120 let tab; |
|
121 for (let i = 0; i < window.gBrowser.tabs.length; i++) { |
|
122 if (!tab && window.gBrowser.tabs[i].linkedBrowser == aBrowser) |
|
123 tab = window.gBrowser.tabs[i]; |
|
124 } |
|
125 is(ss.getTabValue(tab, "uniq"), expectedData, |
|
126 "load " + loadCount + " - correct tab was reloaded"); |
|
127 |
|
128 if (loadCount == aState.windows[0].tabs.length) { |
|
129 gWebProgressListener.unsetCallback(); |
|
130 executeSoon(aCallback); |
|
131 } else { |
|
132 // reload the next tab |
|
133 window.gBrowser.selectTabAtIndex(loadCount); |
|
134 BrowserReloadOrDuplicate(fakeEvent); |
|
135 } |
|
136 } |
|
137 }); |
|
138 |
|
139 BrowserReloadOrDuplicate(fakeEvent); |
|
140 } |