michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const NUM_TABS = 12; michael@0: michael@0: let stateBackup = ss.getBrowserState(); michael@0: michael@0: function test() { michael@0: /** Test for Bug 590268 - Provide access to sessionstore tab data sooner **/ michael@0: waitForExplicitFinish(); michael@0: michael@0: let startedTest = false; michael@0: michael@0: // wasLoaded will be used to keep track of tabs that have already had SSTabRestoring michael@0: // fired for them. michael@0: let wasLoaded = { }; michael@0: let restoringTabsCount = 0; michael@0: let restoredTabsCount = 0; michael@0: let uniq2 = { }; michael@0: let uniq2Count = 0; michael@0: let state = { windows: [{ tabs: [] }] }; michael@0: // We're going to put a bunch of tabs into this state michael@0: for (let i = 0; i < NUM_TABS; i++) { michael@0: let uniq = r(); michael@0: let tabData = { michael@0: entries: [{ url: "http://example.com/#" + i }], michael@0: extData: { "uniq": uniq, "baz": "qux" } michael@0: }; michael@0: state.windows[0].tabs.push(tabData); michael@0: wasLoaded[uniq] = false; michael@0: } michael@0: michael@0: michael@0: function onSSTabRestoring(aEvent) { michael@0: restoringTabsCount++; michael@0: let uniq = ss.getTabValue(aEvent.originalTarget, "uniq"); michael@0: wasLoaded[uniq] = true; michael@0: michael@0: is(ss.getTabValue(aEvent.originalTarget, "foo"), "", michael@0: "There is no value for 'foo'"); michael@0: michael@0: // On the first SSTabRestoring we're going to run the the real test. michael@0: // We'll keep this listener around so we can keep marking tabs as restored. michael@0: if (restoringTabsCount == 1) michael@0: onFirstSSTabRestoring(); michael@0: else if (restoringTabsCount == NUM_TABS) michael@0: onLastSSTabRestoring(); michael@0: } michael@0: michael@0: function onSSTabRestored(aEvent) { michael@0: if (++restoredTabsCount < NUM_TABS) michael@0: return; michael@0: cleanup(); michael@0: } michael@0: michael@0: function onTabOpen(aEvent) { michael@0: // To test bug 614708, we'll just set a value on the tab here. This value michael@0: // would previously cause us to not recognize the values in extData until michael@0: // much later. So testing "uniq" failed. michael@0: ss.setTabValue(aEvent.originalTarget, "foo", "bar"); michael@0: } michael@0: michael@0: // This does the actual testing. SSTabRestoring should be firing on tabs from michael@0: // left to right, so we're going to start with the rightmost tab. michael@0: function onFirstSSTabRestoring() { michael@0: info("onFirstSSTabRestoring..."); michael@0: for (let i = gBrowser.tabs.length - 1; i >= 0; i--) { michael@0: let tab = gBrowser.tabs[i]; michael@0: let actualUniq = ss.getTabValue(tab, "uniq"); michael@0: let expectedUniq = state.windows[0].tabs[i].extData["uniq"]; michael@0: michael@0: if (wasLoaded[actualUniq]) { michael@0: info("tab " + i + ": already restored"); michael@0: continue; michael@0: } michael@0: is(actualUniq, expectedUniq, "tab " + i + ": extData was correct"); michael@0: michael@0: // Now we're going to set a piece of data back on the tab so it can be read michael@0: // to test setting a value "early". michael@0: uniq2[actualUniq] = r(); michael@0: ss.setTabValue(tab, "uniq2", uniq2[actualUniq]); michael@0: michael@0: // Delete the value we have for "baz". This tests that deleteTabValue michael@0: // will delete "early access" values (c.f. bug 617175). If this doesn't throw michael@0: // then the test is successful. michael@0: try { michael@0: ss.deleteTabValue(tab, "baz"); michael@0: } michael@0: catch (e) { michael@0: ok(false, "no error calling deleteTabValue - " + e); michael@0: } michael@0: michael@0: // This will be used in the final comparison to make sure we checked the michael@0: // same number as we set. michael@0: uniq2Count++; michael@0: } michael@0: } michael@0: michael@0: function onLastSSTabRestoring() { michael@0: let checked = 0; michael@0: for (let i = 0; i < gBrowser.tabs.length; i++) { michael@0: let tab = gBrowser.tabs[i]; michael@0: let uniq = ss.getTabValue(tab, "uniq"); michael@0: michael@0: // Look to see if we set a uniq2 value for this uniq value michael@0: if (uniq in uniq2) { michael@0: is(ss.getTabValue(tab, "uniq2"), uniq2[uniq], "tab " + i + " has correct uniq2 value"); michael@0: checked++; michael@0: } michael@0: } michael@0: ok(uniq2Count > 0, "at least 1 tab properly checked 'early access'"); michael@0: is(checked, uniq2Count, "checked the same number of uniq2 as we set"); michael@0: } michael@0: michael@0: function cleanup() { michael@0: // remove the event listener and clean up before finishing michael@0: gBrowser.tabContainer.removeEventListener("SSTabRestoring", onSSTabRestoring, false); michael@0: gBrowser.tabContainer.removeEventListener("SSTabRestored", onSSTabRestored, true); michael@0: gBrowser.tabContainer.removeEventListener("TabOpen", onTabOpen, false); michael@0: // Put this in an executeSoon because we still haven't called restoreNextTab michael@0: // in sessionstore for the last tab (we'll call it after this). We end up michael@0: // trying to restore the tab (since we then add a closed tab to the array). michael@0: executeSoon(function() { michael@0: ss.setBrowserState(stateBackup); michael@0: executeSoon(finish); michael@0: }); michael@0: } michael@0: michael@0: // Add the event listeners michael@0: gBrowser.tabContainer.addEventListener("SSTabRestoring", onSSTabRestoring, false); michael@0: gBrowser.tabContainer.addEventListener("SSTabRestored", onSSTabRestored, true); michael@0: gBrowser.tabContainer.addEventListener("TabOpen", onTabOpen, false); michael@0: // Restore state michael@0: ss.setBrowserState(JSON.stringify(state)); michael@0: }