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 | * Tests that the break-on-dom-events request works. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | const TAB_URL = EXAMPLE_URL + "doc_event-listeners.html"; |
michael@0 | 9 | |
michael@0 | 10 | let gClient, gThreadClient, gInput, gButton; |
michael@0 | 11 | |
michael@0 | 12 | function test() { |
michael@0 | 13 | if (!DebuggerServer.initialized) { |
michael@0 | 14 | DebuggerServer.init(() => true); |
michael@0 | 15 | DebuggerServer.addBrowserActors(); |
michael@0 | 16 | } |
michael@0 | 17 | |
michael@0 | 18 | let transport = DebuggerServer.connectPipe(); |
michael@0 | 19 | gClient = new DebuggerClient(transport); |
michael@0 | 20 | gClient.connect((aType, aTraits) => { |
michael@0 | 21 | is(aType, "browser", |
michael@0 | 22 | "Root actor should identify itself as a browser."); |
michael@0 | 23 | |
michael@0 | 24 | addTab(TAB_URL) |
michael@0 | 25 | .then(() => attachThreadActorForUrl(gClient, TAB_URL)) |
michael@0 | 26 | .then(setupGlobals) |
michael@0 | 27 | .then(pauseDebuggee) |
michael@0 | 28 | .then(testBreakOnAll) |
michael@0 | 29 | .then(testBreakOnDisabled) |
michael@0 | 30 | .then(testBreakOnNone) |
michael@0 | 31 | .then(testBreakOnClick) |
michael@0 | 32 | .then(closeConnection) |
michael@0 | 33 | .then(finish) |
michael@0 | 34 | .then(null, aError => { |
michael@0 | 35 | ok(false, "Got an error: " + aError.message + "\n" + aError.stack); |
michael@0 | 36 | }); |
michael@0 | 37 | }); |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | function setupGlobals(aThreadClient) { |
michael@0 | 41 | gThreadClient = aThreadClient; |
michael@0 | 42 | gInput = content.document.querySelector("input"); |
michael@0 | 43 | gButton = content.document.querySelector("button"); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | function pauseDebuggee() { |
michael@0 | 47 | let deferred = promise.defer(); |
michael@0 | 48 | |
michael@0 | 49 | gClient.addOneTimeListener("paused", (aEvent, aPacket) => { |
michael@0 | 50 | is(aPacket.type, "paused", |
michael@0 | 51 | "We should now be paused."); |
michael@0 | 52 | is(aPacket.why.type, "debuggerStatement", |
michael@0 | 53 | "The debugger statement was hit."); |
michael@0 | 54 | |
michael@0 | 55 | deferred.resolve(); |
michael@0 | 56 | }); |
michael@0 | 57 | |
michael@0 | 58 | // Spin the event loop before causing the debuggee to pause, to allow |
michael@0 | 59 | // this function to return first. |
michael@0 | 60 | executeSoon(triggerButtonClick); |
michael@0 | 61 | |
michael@0 | 62 | return deferred.promise; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | // Test pause on all events. |
michael@0 | 66 | function testBreakOnAll() { |
michael@0 | 67 | let deferred = promise.defer(); |
michael@0 | 68 | |
michael@0 | 69 | // Test calling pauseOnDOMEvents from a paused state. |
michael@0 | 70 | gThreadClient.pauseOnDOMEvents("*", (aPacket) => { |
michael@0 | 71 | is(aPacket.error, undefined, |
michael@0 | 72 | "The pause-on-any-event request completed successfully."); |
michael@0 | 73 | |
michael@0 | 74 | gClient.addOneTimeListener("paused", (aEvent, aPacket) => { |
michael@0 | 75 | is(aPacket.why.type, "pauseOnDOMEvents", |
michael@0 | 76 | "A hidden breakpoint was hit."); |
michael@0 | 77 | is(aPacket.frame.callee.name, "keyupHandler", |
michael@0 | 78 | "The keyupHandler is entered."); |
michael@0 | 79 | |
michael@0 | 80 | gClient.addOneTimeListener("paused", (aEvent, aPacket) => { |
michael@0 | 81 | is(aPacket.why.type, "pauseOnDOMEvents", |
michael@0 | 82 | "A hidden breakpoint was hit."); |
michael@0 | 83 | is(aPacket.frame.callee.name, "clickHandler", |
michael@0 | 84 | "The clickHandler is entered."); |
michael@0 | 85 | |
michael@0 | 86 | gClient.addOneTimeListener("paused", (aEvent, aPacket) => { |
michael@0 | 87 | is(aPacket.why.type, "pauseOnDOMEvents", |
michael@0 | 88 | "A hidden breakpoint was hit."); |
michael@0 | 89 | is(aPacket.frame.callee.name, "onchange", |
michael@0 | 90 | "The onchange handler is entered."); |
michael@0 | 91 | |
michael@0 | 92 | gThreadClient.resume(deferred.resolve); |
michael@0 | 93 | }); |
michael@0 | 94 | |
michael@0 | 95 | gThreadClient.resume(triggerInputChange); |
michael@0 | 96 | }); |
michael@0 | 97 | |
michael@0 | 98 | gThreadClient.resume(triggerButtonClick); |
michael@0 | 99 | }); |
michael@0 | 100 | |
michael@0 | 101 | gThreadClient.resume(triggerInputKeyup); |
michael@0 | 102 | }); |
michael@0 | 103 | |
michael@0 | 104 | return deferred.promise; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | // Test that removing events from the array disables them. |
michael@0 | 108 | function testBreakOnDisabled() { |
michael@0 | 109 | let deferred = promise.defer(); |
michael@0 | 110 | |
michael@0 | 111 | // Test calling pauseOnDOMEvents from a running state. |
michael@0 | 112 | gThreadClient.pauseOnDOMEvents(["click"], (aPacket) => { |
michael@0 | 113 | is(aPacket.error, undefined, |
michael@0 | 114 | "The pause-on-click-only request completed successfully."); |
michael@0 | 115 | |
michael@0 | 116 | gClient.addListener("paused", unexpectedListener); |
michael@0 | 117 | |
michael@0 | 118 | // This non-capturing event listener is guaranteed to run after the page's |
michael@0 | 119 | // capturing one had a chance to execute and modify window.foobar. |
michael@0 | 120 | once(gInput, "keyup").then(() => { |
michael@0 | 121 | is(content.wrappedJSObject.foobar, "keyupHandler", |
michael@0 | 122 | "No hidden breakpoint was hit."); |
michael@0 | 123 | |
michael@0 | 124 | gClient.removeListener("paused", unexpectedListener); |
michael@0 | 125 | deferred.resolve(); |
michael@0 | 126 | }); |
michael@0 | 127 | |
michael@0 | 128 | triggerInputKeyup(); |
michael@0 | 129 | }); |
michael@0 | 130 | |
michael@0 | 131 | return deferred.promise; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | // Test that specifying an empty event array clears all hidden breakpoints. |
michael@0 | 135 | function testBreakOnNone() { |
michael@0 | 136 | let deferred = promise.defer(); |
michael@0 | 137 | |
michael@0 | 138 | // Test calling pauseOnDOMEvents from a running state. |
michael@0 | 139 | gThreadClient.pauseOnDOMEvents([], (aPacket) => { |
michael@0 | 140 | is(aPacket.error, undefined, |
michael@0 | 141 | "The pause-on-none request completed successfully."); |
michael@0 | 142 | |
michael@0 | 143 | gClient.addListener("paused", unexpectedListener); |
michael@0 | 144 | |
michael@0 | 145 | // This non-capturing event listener is guaranteed to run after the page's |
michael@0 | 146 | // capturing one had a chance to execute and modify window.foobar. |
michael@0 | 147 | once(gInput, "keyup").then(() => { |
michael@0 | 148 | is(content.wrappedJSObject.foobar, "keyupHandler", |
michael@0 | 149 | "No hidden breakpoint was hit."); |
michael@0 | 150 | |
michael@0 | 151 | gClient.removeListener("paused", unexpectedListener); |
michael@0 | 152 | deferred.resolve(); |
michael@0 | 153 | }); |
michael@0 | 154 | |
michael@0 | 155 | triggerInputKeyup(); |
michael@0 | 156 | }); |
michael@0 | 157 | |
michael@0 | 158 | return deferred.promise; |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | // Test pause on a single event. |
michael@0 | 162 | function testBreakOnClick() { |
michael@0 | 163 | let deferred = promise.defer(); |
michael@0 | 164 | |
michael@0 | 165 | // Test calling pauseOnDOMEvents from a running state. |
michael@0 | 166 | gThreadClient.pauseOnDOMEvents(["click"], (aPacket) => { |
michael@0 | 167 | is(aPacket.error, undefined, |
michael@0 | 168 | "The pause-on-click request completed successfully."); |
michael@0 | 169 | |
michael@0 | 170 | gClient.addOneTimeListener("paused", (aEvent, aPacket) => { |
michael@0 | 171 | is(aPacket.why.type, "pauseOnDOMEvents", |
michael@0 | 172 | "A hidden breakpoint was hit."); |
michael@0 | 173 | is(aPacket.frame.callee.name, "clickHandler", |
michael@0 | 174 | "The clickHandler is entered."); |
michael@0 | 175 | |
michael@0 | 176 | gThreadClient.resume(deferred.resolve); |
michael@0 | 177 | }); |
michael@0 | 178 | |
michael@0 | 179 | triggerButtonClick(); |
michael@0 | 180 | }); |
michael@0 | 181 | |
michael@0 | 182 | return deferred.promise; |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | function closeConnection() { |
michael@0 | 186 | let deferred = promise.defer(); |
michael@0 | 187 | gClient.close(deferred.resolve); |
michael@0 | 188 | return deferred.promise; |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | function unexpectedListener() { |
michael@0 | 192 | gClient.removeListener("paused", unexpectedListener); |
michael@0 | 193 | ok(false, "An unexpected hidden breakpoint was hit."); |
michael@0 | 194 | gThreadClient.resume(testBreakOnClick); |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | function triggerInputKeyup() { |
michael@0 | 198 | // Make sure that the focus is not on the input box so that a focus event |
michael@0 | 199 | // will be triggered. |
michael@0 | 200 | window.focus(); |
michael@0 | 201 | gBrowser.selectedBrowser.focus(); |
michael@0 | 202 | gButton.focus(); |
michael@0 | 203 | |
michael@0 | 204 | // Focus the element and wait for focus event. |
michael@0 | 205 | once(gInput, "focus").then(() => { |
michael@0 | 206 | executeSoon(() => { |
michael@0 | 207 | EventUtils.synthesizeKey("e", { shiftKey: 1 }, content); |
michael@0 | 208 | }); |
michael@0 | 209 | }); |
michael@0 | 210 | |
michael@0 | 211 | gInput.focus(); |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | function triggerButtonClick() { |
michael@0 | 215 | EventUtils.sendMouseEvent({ type: "click" }, gButton); |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | function triggerInputChange() { |
michael@0 | 219 | gInput.focus(); |
michael@0 | 220 | gInput.value = "foo"; |
michael@0 | 221 | gInput.blur(); |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | registerCleanupFunction(function() { |
michael@0 | 225 | removeTab(gBrowser.selectedTab); |
michael@0 | 226 | gClient = null; |
michael@0 | 227 | gThreadClient = null; |
michael@0 | 228 | gInput = null; |
michael@0 | 229 | gButton = null; |
michael@0 | 230 | }); |