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