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 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | /** |
michael@0 | 5 | * Make sure that the debugger attaches to the right tab when multiple windows |
michael@0 | 6 | * are open. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | const TAB1_URL = EXAMPLE_URL + "doc_script-switching-01.html"; |
michael@0 | 10 | const TAB2_URL = EXAMPLE_URL + "doc_script-switching-02.html"; |
michael@0 | 11 | |
michael@0 | 12 | let gNewTab, gNewWindow; |
michael@0 | 13 | let gClient; |
michael@0 | 14 | |
michael@0 | 15 | function test() { |
michael@0 | 16 | if (!DebuggerServer.initialized) { |
michael@0 | 17 | DebuggerServer.init(() => true); |
michael@0 | 18 | DebuggerServer.addBrowserActors(); |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | let transport = DebuggerServer.connectPipe(); |
michael@0 | 22 | gClient = new DebuggerClient(transport); |
michael@0 | 23 | gClient.connect((aType, aTraits) => { |
michael@0 | 24 | is(aType, "browser", |
michael@0 | 25 | "Root actor should identify itself as a browser."); |
michael@0 | 26 | |
michael@0 | 27 | promise.resolve(null) |
michael@0 | 28 | .then(() => addTab(TAB1_URL)) |
michael@0 | 29 | .then(testFirstTab) |
michael@0 | 30 | .then(() => addWindow(TAB2_URL)) |
michael@0 | 31 | .then(testNewWindow) |
michael@0 | 32 | .then(testFocusFirst) |
michael@0 | 33 | .then(testRemoveTab) |
michael@0 | 34 | .then(closeConnection) |
michael@0 | 35 | .then(finish) |
michael@0 | 36 | .then(null, aError => { |
michael@0 | 37 | ok(false, "Got an error: " + aError.message + "\n" + aError.stack); |
michael@0 | 38 | }); |
michael@0 | 39 | }); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | function testFirstTab(aTab) { |
michael@0 | 43 | let deferred = promise.defer(); |
michael@0 | 44 | |
michael@0 | 45 | gNewTab = aTab; |
michael@0 | 46 | ok(!!gNewTab, "Second tab created."); |
michael@0 | 47 | |
michael@0 | 48 | gClient.listTabs(aResponse => { |
michael@0 | 49 | let tabActor = aResponse.tabs.filter(aGrip => aGrip.url == TAB1_URL).pop(); |
michael@0 | 50 | ok(tabActor, |
michael@0 | 51 | "Should find a tab actor for the first tab."); |
michael@0 | 52 | |
michael@0 | 53 | is(aResponse.selected, 1, |
michael@0 | 54 | "The first tab is selected."); |
michael@0 | 55 | |
michael@0 | 56 | deferred.resolve(); |
michael@0 | 57 | }); |
michael@0 | 58 | |
michael@0 | 59 | return deferred.promise; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | function testNewWindow(aWindow) { |
michael@0 | 63 | let deferred = promise.defer(); |
michael@0 | 64 | |
michael@0 | 65 | gNewWindow = aWindow; |
michael@0 | 66 | ok(!!gNewWindow, "Second window created."); |
michael@0 | 67 | |
michael@0 | 68 | gNewWindow.focus(); |
michael@0 | 69 | |
michael@0 | 70 | let topWindow = Services.wm.getMostRecentWindow("navigator:browser"); |
michael@0 | 71 | is(topWindow, gNewWindow, |
michael@0 | 72 | "The second window is on top."); |
michael@0 | 73 | |
michael@0 | 74 | let isActive = promise.defer(); |
michael@0 | 75 | let isLoaded = promise.defer(); |
michael@0 | 76 | |
michael@0 | 77 | promise.all([isActive.promise, isLoaded.promise]).then(() => { |
michael@0 | 78 | gNewWindow.BrowserChromeTest.runWhenReady(() => { |
michael@0 | 79 | gClient.listTabs(aResponse => { |
michael@0 | 80 | is(aResponse.selected, 2, |
michael@0 | 81 | "The second tab is selected."); |
michael@0 | 82 | |
michael@0 | 83 | deferred.resolve(); |
michael@0 | 84 | }); |
michael@0 | 85 | }); |
michael@0 | 86 | }); |
michael@0 | 87 | |
michael@0 | 88 | let focusManager = Cc["@mozilla.org/focus-manager;1"].getService(Ci.nsIFocusManager); |
michael@0 | 89 | if (focusManager.activeWindow != gNewWindow) { |
michael@0 | 90 | gNewWindow.addEventListener("activate", function onActivate(aEvent) { |
michael@0 | 91 | if (aEvent.target != gNewWindow) { |
michael@0 | 92 | return; |
michael@0 | 93 | } |
michael@0 | 94 | gNewWindow.removeEventListener("activate", onActivate, true); |
michael@0 | 95 | isActive.resolve(); |
michael@0 | 96 | }, true); |
michael@0 | 97 | } else { |
michael@0 | 98 | isActive.resolve(); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | let contentLocation = gNewWindow.content.location.href; |
michael@0 | 102 | if (contentLocation != TAB2_URL) { |
michael@0 | 103 | gNewWindow.document.addEventListener("load", function onLoad(aEvent) { |
michael@0 | 104 | if (aEvent.target.documentURI != TAB2_URL) { |
michael@0 | 105 | return; |
michael@0 | 106 | } |
michael@0 | 107 | gNewWindow.document.removeEventListener("load", onLoad, true); |
michael@0 | 108 | isLoaded.resolve(); |
michael@0 | 109 | }, true); |
michael@0 | 110 | } else { |
michael@0 | 111 | isLoaded.resolve(); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | return deferred.promise; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | function testFocusFirst() { |
michael@0 | 118 | let deferred = promise.defer(); |
michael@0 | 119 | |
michael@0 | 120 | once(window.content, "focus").then(() => { |
michael@0 | 121 | let topWindow = Services.wm.getMostRecentWindow("navigator:browser"); |
michael@0 | 122 | is(top, getDOMWindow(window), |
michael@0 | 123 | "The first window is on top."); |
michael@0 | 124 | |
michael@0 | 125 | gClient.listTabs(aResponse => { |
michael@0 | 126 | is(aResponse.selected, 1, |
michael@0 | 127 | "The first tab is selected after focusing on it."); |
michael@0 | 128 | |
michael@0 | 129 | deferred.resolve(); |
michael@0 | 130 | }); |
michael@0 | 131 | }); |
michael@0 | 132 | |
michael@0 | 133 | window.content.focus(); |
michael@0 | 134 | |
michael@0 | 135 | return deferred.promise; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | function testRemoveTab() { |
michael@0 | 139 | let deferred = promise.defer(); |
michael@0 | 140 | |
michael@0 | 141 | gNewWindow.close(); |
michael@0 | 142 | |
michael@0 | 143 | // give it time to close |
michael@0 | 144 | executeSoon(function() { continue_remove_tab(deferred) }); |
michael@0 | 145 | return deferred.promise; |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | function continue_remove_tab(deferred) |
michael@0 | 149 | { |
michael@0 | 150 | removeTab(gNewTab); |
michael@0 | 151 | |
michael@0 | 152 | gClient.listTabs(aResponse => { |
michael@0 | 153 | // Verify that tabs are no longer included in listTabs. |
michael@0 | 154 | let foundTab1 = aResponse.tabs.some(aGrip => aGrip.url == TAB1_URL); |
michael@0 | 155 | let foundTab2 = aResponse.tabs.some(aGrip => aGrip.url == TAB2_URL); |
michael@0 | 156 | ok(!foundTab1, "Tab1 should be gone."); |
michael@0 | 157 | ok(!foundTab2, "Tab2 should be gone."); |
michael@0 | 158 | |
michael@0 | 159 | is(aResponse.selected, 0, |
michael@0 | 160 | "The original tab is selected."); |
michael@0 | 161 | |
michael@0 | 162 | deferred.resolve(); |
michael@0 | 163 | }); |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | function closeConnection() { |
michael@0 | 167 | let deferred = promise.defer(); |
michael@0 | 168 | gClient.close(deferred.resolve); |
michael@0 | 169 | return deferred.promise; |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | registerCleanupFunction(function() { |
michael@0 | 173 | gNewTab = null; |
michael@0 | 174 | gNewWindow = null; |
michael@0 | 175 | gClient = null; |
michael@0 | 176 | }); |