1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/sessionstore/test/browser_590268.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,136 @@ 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 NUM_TABS = 12; 1.9 + 1.10 +let stateBackup = ss.getBrowserState(); 1.11 + 1.12 +function test() { 1.13 + /** Test for Bug 590268 - Provide access to sessionstore tab data sooner **/ 1.14 + waitForExplicitFinish(); 1.15 + 1.16 + let startedTest = false; 1.17 + 1.18 + // wasLoaded will be used to keep track of tabs that have already had SSTabRestoring 1.19 + // fired for them. 1.20 + let wasLoaded = { }; 1.21 + let restoringTabsCount = 0; 1.22 + let restoredTabsCount = 0; 1.23 + let uniq2 = { }; 1.24 + let uniq2Count = 0; 1.25 + let state = { windows: [{ tabs: [] }] }; 1.26 + // We're going to put a bunch of tabs into this state 1.27 + for (let i = 0; i < NUM_TABS; i++) { 1.28 + let uniq = r(); 1.29 + let tabData = { 1.30 + entries: [{ url: "http://example.com/#" + i }], 1.31 + extData: { "uniq": uniq, "baz": "qux" } 1.32 + }; 1.33 + state.windows[0].tabs.push(tabData); 1.34 + wasLoaded[uniq] = false; 1.35 + } 1.36 + 1.37 + 1.38 + function onSSTabRestoring(aEvent) { 1.39 + restoringTabsCount++; 1.40 + let uniq = ss.getTabValue(aEvent.originalTarget, "uniq"); 1.41 + wasLoaded[uniq] = true; 1.42 + 1.43 + is(ss.getTabValue(aEvent.originalTarget, "foo"), "", 1.44 + "There is no value for 'foo'"); 1.45 + 1.46 + // On the first SSTabRestoring we're going to run the the real test. 1.47 + // We'll keep this listener around so we can keep marking tabs as restored. 1.48 + if (restoringTabsCount == 1) 1.49 + onFirstSSTabRestoring(); 1.50 + else if (restoringTabsCount == NUM_TABS) 1.51 + onLastSSTabRestoring(); 1.52 + } 1.53 + 1.54 + function onSSTabRestored(aEvent) { 1.55 + if (++restoredTabsCount < NUM_TABS) 1.56 + return; 1.57 + cleanup(); 1.58 + } 1.59 + 1.60 + function onTabOpen(aEvent) { 1.61 + // To test bug 614708, we'll just set a value on the tab here. This value 1.62 + // would previously cause us to not recognize the values in extData until 1.63 + // much later. So testing "uniq" failed. 1.64 + ss.setTabValue(aEvent.originalTarget, "foo", "bar"); 1.65 + } 1.66 + 1.67 + // This does the actual testing. SSTabRestoring should be firing on tabs from 1.68 + // left to right, so we're going to start with the rightmost tab. 1.69 + function onFirstSSTabRestoring() { 1.70 + info("onFirstSSTabRestoring..."); 1.71 + for (let i = gBrowser.tabs.length - 1; i >= 0; i--) { 1.72 + let tab = gBrowser.tabs[i]; 1.73 + let actualUniq = ss.getTabValue(tab, "uniq"); 1.74 + let expectedUniq = state.windows[0].tabs[i].extData["uniq"]; 1.75 + 1.76 + if (wasLoaded[actualUniq]) { 1.77 + info("tab " + i + ": already restored"); 1.78 + continue; 1.79 + } 1.80 + is(actualUniq, expectedUniq, "tab " + i + ": extData was correct"); 1.81 + 1.82 + // Now we're going to set a piece of data back on the tab so it can be read 1.83 + // to test setting a value "early". 1.84 + uniq2[actualUniq] = r(); 1.85 + ss.setTabValue(tab, "uniq2", uniq2[actualUniq]); 1.86 + 1.87 + // Delete the value we have for "baz". This tests that deleteTabValue 1.88 + // will delete "early access" values (c.f. bug 617175). If this doesn't throw 1.89 + // then the test is successful. 1.90 + try { 1.91 + ss.deleteTabValue(tab, "baz"); 1.92 + } 1.93 + catch (e) { 1.94 + ok(false, "no error calling deleteTabValue - " + e); 1.95 + } 1.96 + 1.97 + // This will be used in the final comparison to make sure we checked the 1.98 + // same number as we set. 1.99 + uniq2Count++; 1.100 + } 1.101 + } 1.102 + 1.103 + function onLastSSTabRestoring() { 1.104 + let checked = 0; 1.105 + for (let i = 0; i < gBrowser.tabs.length; i++) { 1.106 + let tab = gBrowser.tabs[i]; 1.107 + let uniq = ss.getTabValue(tab, "uniq"); 1.108 + 1.109 + // Look to see if we set a uniq2 value for this uniq value 1.110 + if (uniq in uniq2) { 1.111 + is(ss.getTabValue(tab, "uniq2"), uniq2[uniq], "tab " + i + " has correct uniq2 value"); 1.112 + checked++; 1.113 + } 1.114 + } 1.115 + ok(uniq2Count > 0, "at least 1 tab properly checked 'early access'"); 1.116 + is(checked, uniq2Count, "checked the same number of uniq2 as we set"); 1.117 + } 1.118 + 1.119 + function cleanup() { 1.120 + // remove the event listener and clean up before finishing 1.121 + gBrowser.tabContainer.removeEventListener("SSTabRestoring", onSSTabRestoring, false); 1.122 + gBrowser.tabContainer.removeEventListener("SSTabRestored", onSSTabRestored, true); 1.123 + gBrowser.tabContainer.removeEventListener("TabOpen", onTabOpen, false); 1.124 + // Put this in an executeSoon because we still haven't called restoreNextTab 1.125 + // in sessionstore for the last tab (we'll call it after this). We end up 1.126 + // trying to restore the tab (since we then add a closed tab to the array). 1.127 + executeSoon(function() { 1.128 + ss.setBrowserState(stateBackup); 1.129 + executeSoon(finish); 1.130 + }); 1.131 + } 1.132 + 1.133 + // Add the event listeners 1.134 + gBrowser.tabContainer.addEventListener("SSTabRestoring", onSSTabRestoring, false); 1.135 + gBrowser.tabContainer.addEventListener("SSTabRestored", onSSTabRestored, true); 1.136 + gBrowser.tabContainer.addEventListener("TabOpen", onTabOpen, false); 1.137 + // Restore state 1.138 + ss.setBrowserState(JSON.stringify(state)); 1.139 +}