|
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 NUM_TABS = 12; |
|
6 |
|
7 let stateBackup = ss.getBrowserState(); |
|
8 |
|
9 function test() { |
|
10 /** Test for Bug 590268 - Provide access to sessionstore tab data sooner **/ |
|
11 waitForExplicitFinish(); |
|
12 |
|
13 let startedTest = false; |
|
14 |
|
15 // wasLoaded will be used to keep track of tabs that have already had SSTabRestoring |
|
16 // fired for them. |
|
17 let wasLoaded = { }; |
|
18 let restoringTabsCount = 0; |
|
19 let restoredTabsCount = 0; |
|
20 let uniq2 = { }; |
|
21 let uniq2Count = 0; |
|
22 let state = { windows: [{ tabs: [] }] }; |
|
23 // We're going to put a bunch of tabs into this state |
|
24 for (let i = 0; i < NUM_TABS; i++) { |
|
25 let uniq = r(); |
|
26 let tabData = { |
|
27 entries: [{ url: "http://example.com/#" + i }], |
|
28 extData: { "uniq": uniq, "baz": "qux" } |
|
29 }; |
|
30 state.windows[0].tabs.push(tabData); |
|
31 wasLoaded[uniq] = false; |
|
32 } |
|
33 |
|
34 |
|
35 function onSSTabRestoring(aEvent) { |
|
36 restoringTabsCount++; |
|
37 let uniq = ss.getTabValue(aEvent.originalTarget, "uniq"); |
|
38 wasLoaded[uniq] = true; |
|
39 |
|
40 is(ss.getTabValue(aEvent.originalTarget, "foo"), "", |
|
41 "There is no value for 'foo'"); |
|
42 |
|
43 // On the first SSTabRestoring we're going to run the the real test. |
|
44 // We'll keep this listener around so we can keep marking tabs as restored. |
|
45 if (restoringTabsCount == 1) |
|
46 onFirstSSTabRestoring(); |
|
47 else if (restoringTabsCount == NUM_TABS) |
|
48 onLastSSTabRestoring(); |
|
49 } |
|
50 |
|
51 function onSSTabRestored(aEvent) { |
|
52 if (++restoredTabsCount < NUM_TABS) |
|
53 return; |
|
54 cleanup(); |
|
55 } |
|
56 |
|
57 function onTabOpen(aEvent) { |
|
58 // To test bug 614708, we'll just set a value on the tab here. This value |
|
59 // would previously cause us to not recognize the values in extData until |
|
60 // much later. So testing "uniq" failed. |
|
61 ss.setTabValue(aEvent.originalTarget, "foo", "bar"); |
|
62 } |
|
63 |
|
64 // This does the actual testing. SSTabRestoring should be firing on tabs from |
|
65 // left to right, so we're going to start with the rightmost tab. |
|
66 function onFirstSSTabRestoring() { |
|
67 info("onFirstSSTabRestoring..."); |
|
68 for (let i = gBrowser.tabs.length - 1; i >= 0; i--) { |
|
69 let tab = gBrowser.tabs[i]; |
|
70 let actualUniq = ss.getTabValue(tab, "uniq"); |
|
71 let expectedUniq = state.windows[0].tabs[i].extData["uniq"]; |
|
72 |
|
73 if (wasLoaded[actualUniq]) { |
|
74 info("tab " + i + ": already restored"); |
|
75 continue; |
|
76 } |
|
77 is(actualUniq, expectedUniq, "tab " + i + ": extData was correct"); |
|
78 |
|
79 // Now we're going to set a piece of data back on the tab so it can be read |
|
80 // to test setting a value "early". |
|
81 uniq2[actualUniq] = r(); |
|
82 ss.setTabValue(tab, "uniq2", uniq2[actualUniq]); |
|
83 |
|
84 // Delete the value we have for "baz". This tests that deleteTabValue |
|
85 // will delete "early access" values (c.f. bug 617175). If this doesn't throw |
|
86 // then the test is successful. |
|
87 try { |
|
88 ss.deleteTabValue(tab, "baz"); |
|
89 } |
|
90 catch (e) { |
|
91 ok(false, "no error calling deleteTabValue - " + e); |
|
92 } |
|
93 |
|
94 // This will be used in the final comparison to make sure we checked the |
|
95 // same number as we set. |
|
96 uniq2Count++; |
|
97 } |
|
98 } |
|
99 |
|
100 function onLastSSTabRestoring() { |
|
101 let checked = 0; |
|
102 for (let i = 0; i < gBrowser.tabs.length; i++) { |
|
103 let tab = gBrowser.tabs[i]; |
|
104 let uniq = ss.getTabValue(tab, "uniq"); |
|
105 |
|
106 // Look to see if we set a uniq2 value for this uniq value |
|
107 if (uniq in uniq2) { |
|
108 is(ss.getTabValue(tab, "uniq2"), uniq2[uniq], "tab " + i + " has correct uniq2 value"); |
|
109 checked++; |
|
110 } |
|
111 } |
|
112 ok(uniq2Count > 0, "at least 1 tab properly checked 'early access'"); |
|
113 is(checked, uniq2Count, "checked the same number of uniq2 as we set"); |
|
114 } |
|
115 |
|
116 function cleanup() { |
|
117 // remove the event listener and clean up before finishing |
|
118 gBrowser.tabContainer.removeEventListener("SSTabRestoring", onSSTabRestoring, false); |
|
119 gBrowser.tabContainer.removeEventListener("SSTabRestored", onSSTabRestored, true); |
|
120 gBrowser.tabContainer.removeEventListener("TabOpen", onTabOpen, false); |
|
121 // Put this in an executeSoon because we still haven't called restoreNextTab |
|
122 // in sessionstore for the last tab (we'll call it after this). We end up |
|
123 // trying to restore the tab (since we then add a closed tab to the array). |
|
124 executeSoon(function() { |
|
125 ss.setBrowserState(stateBackup); |
|
126 executeSoon(finish); |
|
127 }); |
|
128 } |
|
129 |
|
130 // Add the event listeners |
|
131 gBrowser.tabContainer.addEventListener("SSTabRestoring", onSSTabRestoring, false); |
|
132 gBrowser.tabContainer.addEventListener("SSTabRestored", onSSTabRestored, true); |
|
133 gBrowser.tabContainer.addEventListener("TabOpen", onTabOpen, false); |
|
134 // Restore state |
|
135 ss.setBrowserState(JSON.stringify(state)); |
|
136 } |