|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 function test() { |
|
5 TestRunner.run(); |
|
6 } |
|
7 |
|
8 /** |
|
9 * This test makes sure that we correctly preserve tab attributes when storing |
|
10 * and restoring tabs. It also ensures that we skip special attributes like |
|
11 * 'image' and 'pending' that need to be handled differently or internally. |
|
12 */ |
|
13 |
|
14 const PREF = "browser.sessionstore.restore_on_demand"; |
|
15 |
|
16 function runTests() { |
|
17 Services.prefs.setBoolPref(PREF, true) |
|
18 registerCleanupFunction(() => Services.prefs.clearUserPref(PREF)); |
|
19 |
|
20 // Add a new tab with a nice icon. |
|
21 let tab = gBrowser.addTab("about:robots"); |
|
22 yield whenBrowserLoaded(tab.linkedBrowser); |
|
23 |
|
24 // Check that the tab has an 'image' attribute. |
|
25 ok(tab.hasAttribute("image"), "tab.image exists"); |
|
26 |
|
27 // Make sure we do not persist 'image' attributes. |
|
28 ss.persistTabAttribute("image"); |
|
29 let {attributes} = JSON.parse(ss.getTabState(tab)); |
|
30 ok(!("image" in attributes), "'image' attribute not saved"); |
|
31 ok(!("custom" in attributes), "'custom' attribute not saved"); |
|
32 |
|
33 // Test persisting a custom attribute. |
|
34 tab.setAttribute("custom", "foobar"); |
|
35 ss.persistTabAttribute("custom"); |
|
36 |
|
37 let {attributes} = JSON.parse(ss.getTabState(tab)); |
|
38 is(attributes.custom, "foobar", "'custom' attribute is correct"); |
|
39 |
|
40 // Make sure we're backwards compatible and restore old 'image' attributes. |
|
41 let state = { |
|
42 entries: [{url: "about:mozilla"}], |
|
43 attributes: {custom: "foobaz", image: gBrowser.getIcon(tab)} |
|
44 }; |
|
45 |
|
46 // Prepare a pending tab waiting to be restored. |
|
47 whenTabRestoring(tab); |
|
48 yield ss.setTabState(tab, JSON.stringify(state)); |
|
49 |
|
50 ok(tab.hasAttribute("pending"), "tab is pending"); |
|
51 is(gBrowser.getIcon(tab), state.attributes.image, "tab has correct icon"); |
|
52 |
|
53 // Let the pending tab load. |
|
54 gBrowser.selectedTab = tab; |
|
55 yield whenTabRestored(tab); |
|
56 |
|
57 // Ensure no 'image' or 'pending' attributes are stored. |
|
58 let {attributes} = JSON.parse(ss.getTabState(tab)); |
|
59 ok(!("image" in attributes), "'image' attribute not saved"); |
|
60 ok(!("pending" in attributes), "'pending' attribute not saved"); |
|
61 is(attributes.custom, "foobaz", "'custom' attribute is correct"); |
|
62 |
|
63 // Clean up. |
|
64 gBrowser.removeTab(tab); |
|
65 } |
|
66 |
|
67 function whenTabRestoring(tab) { |
|
68 tab.addEventListener("SSTabRestoring", function onRestoring() { |
|
69 tab.removeEventListener("SSTabRestoring", onRestoring); |
|
70 executeSoon(next); |
|
71 }); |
|
72 } |