diff --git a/devtools/client/debugger/test/mochitest/head.js b/devtools/client/debugger/test/mochitest/head.js index 3107dd573dc4..8e27f28c6a55 100644 --- a/devtools/client/debugger/test/mochitest/head.js +++ b/devtools/client/debugger/test/mochitest/head.js @@ -152,30 +152,6 @@ function removeAddon(aAddon) { return deferred.promise; } -function getTargetActorForUrl(aClient, aUrl) { - let deferred = promise.defer(); - - aClient.listTabs().then(aResponse => { - let targetActor = aResponse.tabs.filter(aGrip => aGrip.url == aUrl).pop(); - deferred.resolve(targetActor); - }); - - return deferred.promise; -} - -async function attachTargetActorForUrl(aClient, aUrl) { - let grip = await getTargetActorForUrl(aClient, aUrl); - let [ response, front ] = await aClient.attachTarget(grip.actor); - return [grip, response, front]; -} - -async function attachThreadActorForUrl(aClient, aUrl) { - let [grip, response] = await attachTargetActorForUrl(aClient, aUrl); - let [response2, threadClient] = await aClient.attachThread(response.threadActor); - await threadClient.resume(); - return threadClient; -} - // Override once from shared-head, as some tests depend on trying native DOM listeners // before EventEmitter. Since this directory is deprecated, there's little value in // resolving the descrepency here. diff --git a/devtools/client/framework/test/browser_toolbox_tool_remote_reopen.js b/devtools/client/framework/test/browser_toolbox_tool_remote_reopen.js index 2ac413195acb..a59ca298390f 100644 --- a/devtools/client/framework/test/browser_toolbox_tool_remote_reopen.js +++ b/devtools/client/framework/test/browser_toolbox_tool_remote_reopen.js @@ -6,7 +6,6 @@ "use strict"; const { DebuggerServer } = require("devtools/server/main"); -const { DebuggerClient } = require("devtools/shared/client/debugger-client"); // Bug 1277805: Too slow for debug runs requestLongerTimeout(2); @@ -60,36 +59,13 @@ function runTools(target) { })(); } -function getClient() { - DebuggerServer.init(); - DebuggerServer.registerAllActors(); - - const transport = DebuggerServer.connectPipe(); - const client = new DebuggerClient(transport); - - return client.connect().then(() => client); -} - -function getTarget(client) { - return new Promise(resolve => { - client.listTabs().then(tabList => { - const target = TargetFactory.forRemoteTab({ - client: client, - form: tabList.tabs[tabList.selected], - chrome: false, - }); - resolve(target); - }); - }); -} - function test() { (async function() { toggleAllTools(true); - await addTab("about:blank"); + const tab = await addTab("about:blank"); - const client = await getClient(); - const target = await getTarget(client); + const target = await TargetFactory.forTab(tab); + const { client } = target; await runTools(target); const rootFronts = [...client.mainRoot.fronts.values()]; diff --git a/devtools/client/shared/test/browser_dbg_WorkerTargetActor.attach.js b/devtools/client/shared/test/browser_dbg_WorkerTargetActor.attach.js index 9a60d7f28e8a..936fbcdddf91 100644 --- a/devtools/client/shared/test/browser_dbg_WorkerTargetActor.attach.js +++ b/devtools/client/shared/test/browser_dbg_WorkerTargetActor.attach.js @@ -26,15 +26,10 @@ function test() { const oldMaxTotalViewers = SpecialPowers.getIntPref(MAX_TOTAL_VIEWERS); SpecialPowers.setIntPref(MAX_TOTAL_VIEWERS, 10); - DebuggerServer.init(); - DebuggerServer.registerAllActors(); - - const client = new DebuggerClient(DebuggerServer.connectPipe()); - yield connect(client); - const tab = yield addTab(TAB1_URL); - const { tabs } = yield listTabs(client); - const [, targetFront] = yield attachTarget(client, findTab(tabs, TAB1_URL)); + const target = yield TargetFactory.forTab(tab); + yield target.attach(); + const targetFront = target.activeTab; yield listWorkers(targetFront); // If a page still has pending network requests, it will not be moved into @@ -71,7 +66,7 @@ function test() { yield workerTargetFront1.attach(); is(workerTargetFront1.isClosed, false, "worker in tab 1 should not be closed"); - yield close(client); + yield target.destroy(); SpecialPowers.setIntPref(MAX_TOTAL_VIEWERS, oldMaxTotalViewers); finish(); }); diff --git a/devtools/client/shared/test/browser_dbg_debugger-statement.js b/devtools/client/shared/test/browser_dbg_debugger-statement.js index ad83ef980aea..49fb6c97c550 100644 --- a/devtools/client/shared/test/browser_dbg_debugger-statement.js +++ b/devtools/client/shared/test/browser_dbg_debugger-statement.js @@ -15,82 +15,53 @@ Services.scriptloader.loadSubScript( "chrome://mochitests/content/browser/devtools/client/shared/test/helper_workers.js", this); -var { DebuggerServer } = require("devtools/server/main"); -var { DebuggerClient } = require("devtools/shared/client/debugger-client"); - const TAB_URL = TEST_URI_ROOT + "doc_inline-debugger-statement.html"; -var gClient; -var gTab; +add_task(async () => { + const tab = await addTab(TAB_URL); + const target = await TargetFactory.forTab(tab); + await target.attach(); + const { client } = target; + const targetFront = target.activeTab; -function test() { - DebuggerServer.init(); - DebuggerServer.registerAllActors(); + const threadClient = await testEarlyDebuggerStatement(client, tab, targetFront); + await testDebuggerStatement(client, tab, threadClient); - const transport = DebuggerServer.connectPipe(); - gClient = new DebuggerClient(transport); - gClient.connect().then(([aType, aTraits]) => { - is(aType, "browser", - "Root actor should identify itself as a browser."); - - addTab(TAB_URL) - .then(tab => { - gTab = tab; - return attachTargetActorForUrl(gClient, TAB_URL); - }) - .then(testEarlyDebuggerStatement) - .then(testDebuggerStatement) - .then(() => gClient.close()) - .then(finish) - .catch(error => { - ok(false, "Got an error: " + error.message + "\n" + error.stack); - }); - }); -} - -function testEarlyDebuggerStatement([aGrip, aResponse]) { - const deferred = getDeferredPromise().defer(); + await target.destroy(); +}); +async function testEarlyDebuggerStatement(client, tab, targetFront) { const onPaused = function(event, packet) { ok(false, "Pause shouldn't be called before we've attached!"); - deferred.reject(); }; - gClient.addListener("paused", onPaused); + client.addListener("paused", onPaused); // This should continue without nesting an event loop and calling // the onPaused hook, because we haven't attached yet. - callInTab(gTab, "runDebuggerStatement"); + callInTab(tab, "runDebuggerStatement"); - gClient.removeListener("paused", onPaused); + client.removeListener("paused", onPaused); // Now attach and resume... - gClient.request({ to: aResponse.threadActor, type: "attach" }, () => { - gClient.request({ to: aResponse.threadActor, type: "resume" }, () => { - ok(true, "Pause wasn't called before we've attached."); - deferred.resolve([aGrip, aResponse]); - }); - }); + const [, threadClient] = await targetFront.attachThread(); + await threadClient.resume(); + ok(true, "Pause wasn't called before we've attached."); - return deferred.promise; + return threadClient; } -function testDebuggerStatement([aGrip, aResponse]) { - const deferred = getDeferredPromise().defer(); - - gClient.addListener("paused", (event, packet) => { - gClient.request({ to: aResponse.threadActor, type: "resume" }, () => { +async function testDebuggerStatement(client, tab, threadClient) { + const onPaused = new Promise(resolve => { + client.addListener("paused", async (event, packet) => { + await threadClient.resume(); ok(true, "The pause handler was triggered on a debugger statement."); - deferred.resolve(); + resolve(); }); }); // Reach around the debugging protocol and execute the debugger statement. - callInTab(gTab, "runDebuggerStatement"); + callInTab(tab, "runDebuggerStatement"); - return deferred.promise; + return onPaused; } - -registerCleanupFunction(function() { - gClient = null; -}); diff --git a/devtools/client/shared/test/browser_dbg_event-listeners-01.js b/devtools/client/shared/test/browser_dbg_event-listeners-01.js index 761b0bb0a63d..df8bb71a7e2c 100644 --- a/devtools/client/shared/test/browser_dbg_event-listeners-01.js +++ b/devtools/client/shared/test/browser_dbg_event-listeners-01.js @@ -15,28 +15,14 @@ Services.scriptloader.loadSubScript( "chrome://mochitests/content/browser/devtools/client/shared/test/helper_workers.js", this); -var { DebuggerServer } = require("devtools/server/main"); -var { DebuggerClient } = require("devtools/shared/client/debugger-client"); - const TAB_URL = TEST_URI_ROOT + "doc_event-listeners-01.html"; add_task(async function() { - DebuggerServer.init(); - DebuggerServer.registerAllActors(); - - const transport = DebuggerServer.connectPipe(); - const client = new DebuggerClient(transport); - const [type] = await client.connect(); - - Assert.equal(type, "browser", - "Root actor should identify itself as a browser."); - const tab = await addTab(TAB_URL); - const threadClient = await attachThreadActorForUrl(client, TAB_URL); + const { client, threadClient } = await attachThreadActorForTab(tab); + await pauseDebuggee(tab, client, threadClient); await testEventListeners(client, threadClient); - - await client.close(); }); function pauseDebuggee(tab, client, threadClient) { diff --git a/devtools/client/shared/test/browser_dbg_event-listeners-02.js b/devtools/client/shared/test/browser_dbg_event-listeners-02.js index 0edaf7cdd061..de3fb19d0458 100644 --- a/devtools/client/shared/test/browser_dbg_event-listeners-02.js +++ b/devtools/client/shared/test/browser_dbg_event-listeners-02.js @@ -16,27 +16,14 @@ Services.scriptloader.loadSubScript( "chrome://mochitests/content/browser/devtools/client/shared/test/helper_workers.js", this); -var { DebuggerServer } = require("devtools/server/main"); -var { DebuggerClient } = require("devtools/shared/client/debugger-client"); - const TAB_URL = TEST_URI_ROOT + "doc_event-listeners-03.html"; add_task(async function() { - DebuggerServer.init(); - DebuggerServer.registerAllActors(); - - const transport = DebuggerServer.connectPipe(); - const client = new DebuggerClient(transport); - const [type] = await client.connect(); - - Assert.equal(type, "browser", - "Root actor should identify itself as a browser."); - const tab = await addTab(TAB_URL); - const threadClient = await attachThreadActorForUrl(client, TAB_URL); + const { client, threadClient } = await attachThreadActorForTab(tab); + await pauseDebuggee(tab, client, threadClient); await testEventListeners(client, threadClient); - await client.close(); }); function pauseDebuggee(tab, client, threadClient) { diff --git a/devtools/client/shared/test/browser_dbg_event-listeners-03.js b/devtools/client/shared/test/browser_dbg_event-listeners-03.js index fad9958a03f0..e227ddfc90cc 100644 --- a/devtools/client/shared/test/browser_dbg_event-listeners-03.js +++ b/devtools/client/shared/test/browser_dbg_event-listeners-03.js @@ -16,43 +16,20 @@ Services.scriptloader.loadSubScript( "chrome://mochitests/content/browser/devtools/client/shared/test/helper_workers.js", this); -var { DebuggerServer } = require("devtools/server/main"); -var { DebuggerClient } = require("devtools/shared/client/debugger-client"); - const TAB_URL = TEST_URI_ROOT + "doc_native-event-handler.html"; -var gClient; -var gTab; +add_task(async () => { + const tab = await addTab(TAB_URL); + const { client, threadClient } = await attachThreadActorForTab(tab); -function test() { - DebuggerServer.init(); - DebuggerServer.registerAllActors(); + await pauseDebuggee(client, tab, threadClient); + await testEventListeners(threadClient); +}); - const transport = DebuggerServer.connectPipe(); - gClient = new DebuggerClient(transport); - gClient.connect().then(([aType, aTraits]) => { - is(aType, "browser", - "Root actor should identify itself as a browser."); - - addTab(TAB_URL) - .then((tab) => { - gTab = tab; - return attachThreadActorForUrl(gClient, TAB_URL); - }) - .then(pauseDebuggee) - .then(testEventListeners) - .then(() => gClient.close()) - .then(finish) - .catch(error => { - ok(false, "Got an error: " + error.message + "\n" + error.stack); - }); - }); -} - -function pauseDebuggee(threadClient) { +function pauseDebuggee(client, tab, threadClient) { const deferred = getDeferredPromise().defer(); - gClient.addOneTimeListener("paused", (event, packet) => { + client.addOneTimeListener("paused", (event, packet) => { is(packet.type, "paused", "We should now be paused."); is(packet.why.type, "debuggerStatement", @@ -61,7 +38,7 @@ function pauseDebuggee(threadClient) { deferred.resolve(threadClient); }); - generateMouseClickInTab(gTab, "content.document.querySelector('button')"); + generateMouseClickInTab(tab, "content.document.querySelector('button')"); return deferred.promise; } @@ -86,7 +63,3 @@ function testEventListeners(threadClient) { return deferred.promise; } - -registerCleanupFunction(function() { - gClient = null; -}); diff --git a/devtools/client/shared/test/browser_dbg_listtabs-01.js b/devtools/client/shared/test/browser_dbg_listtabs-01.js index 0e1d9a778541..61dc8aa251c9 100644 --- a/devtools/client/shared/test/browser_dbg_listtabs-01.js +++ b/devtools/client/shared/test/browser_dbg_listtabs-01.js @@ -44,9 +44,9 @@ function testFirstTab() { return addTab(TAB1_URL).then(tab => { gTab1 = tab; - return getTargetActorForUrl(gClient, TAB1_URL).then(grip => { - ok(grip, "Should find a target actor for the first tab."); - gTab1Actor = grip.actor; + return getTargetActorForUrl(gClient, TAB1_URL).then(form => { + ok(form, "Should find a target actor for the first tab."); + gTab1Actor = form.actor; }); }); } @@ -67,8 +67,8 @@ function testSecondTab() { function testRemoveTab() { return removeTab(gTab1).then(() => { - return getTargetActorForUrl(gClient, TAB1_URL).then(grip => { - ok(!grip, "Shouldn't find a target actor for the first tab anymore."); + return getTargetActorForUrl(gClient, TAB1_URL).then(form => { + ok(!form, "Shouldn't find a target actor for the first tab anymore."); }); }); } @@ -100,13 +100,8 @@ registerCleanupFunction(function() { gClient = null; }); -function getTargetActorForUrl(client, url) { - const deferred = promise.defer(); - - client.listTabs().then(response => { - const targetActor = response.tabs.filter(grip => grip.url == url).pop(); - deferred.resolve(targetActor); - }); - - return deferred.promise; +async function getTargetActorForUrl(client, url) { + const { tabs } = await client.listTabs(); + const targetActor = tabs.filter(form => form.url == url).pop(); + return targetActor; } diff --git a/devtools/client/shared/test/browser_dbg_listworkers.js b/devtools/client/shared/test/browser_dbg_listworkers.js index 679fcef2ceb5..3277aeb4be61 100644 --- a/devtools/client/shared/test/browser_dbg_listworkers.js +++ b/devtools/client/shared/test/browser_dbg_listworkers.js @@ -17,23 +17,15 @@ Services.scriptloader.loadSubScript( "chrome://mochitests/content/browser/devtools/client/shared/test/helper_workers.js", this); -var { DebuggerServer } = require("devtools/server/main"); -var { DebuggerClient } = require("devtools/shared/client/debugger-client"); - var TAB_URL = EXAMPLE_URL + "doc_listworkers-tab.html"; var WORKER1_URL = "code_listworkers-worker1.js"; var WORKER2_URL = "code_listworkers-worker2.js"; add_task(async function test() { - DebuggerServer.init(); - DebuggerServer.registerAllActors(); - - const client = new DebuggerClient(DebuggerServer.connectPipe()); - await connect(client); - const tab = await addTab(TAB_URL); - const { tabs } = await listTabs(client); - const [, targetFront] = await attachTarget(client, findTab(tabs, TAB_URL)); + const target = await TargetFactory.forTab(tab); + await target.attach(); + const targetFront = target.activeTab; let { workers } = await listWorkers(targetFront); is(workers.length, 0); @@ -74,6 +66,6 @@ add_task(async function test() { ({ workers } = await listWorkers(targetFront)); is(workers.length, 0); - await close(client); + await target.destroy(); finish(); }); diff --git a/devtools/client/shared/test/browser_dbg_navigation.js b/devtools/client/shared/test/browser_dbg_navigation.js index b39ed1e70f90..bbb28dc832ef 100644 --- a/devtools/client/shared/test/browser_dbg_navigation.js +++ b/devtools/client/shared/test/browser_dbg_navigation.js @@ -9,35 +9,19 @@ * Check tab attach/navigation. */ -var { DebuggerServer } = require("devtools/server/main"); -var { DebuggerClient } = require("devtools/shared/client/debugger-client"); - const TAB1_URL = EXAMPLE_URL + "doc_empty-tab-01.html"; const TAB2_FILE = "doc_empty-tab-02.html"; const TAB2_URL = EXAMPLE_URL + TAB2_FILE; -var gClient; +add_task(async () => { + const tab = await addTab(TAB1_URL); + const target = await TargetFactory.forTab(tab); + await target.attach(); + const targetFront = target.activeTab; -function test() { - DebuggerServer.init(); - DebuggerServer.registerAllActors(); - - const transport = DebuggerServer.connectPipe(); - gClient = new DebuggerClient(transport); - gClient.connect().then(([aType, aTraits]) => { - is(aType, "browser", - "Root actor should identify itself as a browser."); - - addTab(TAB1_URL) - .then(() => attachTargetActorForUrl(gClient, TAB1_URL)) - .then(testNavigate) - .then(testDetach) - .then(finish) - .catch(error => { - ok(false, "Got an error: " + error.message + "\n" + error.stack); - }); - }); -} + await testNavigate(targetFront); + await testDetach(target); +}); function testNavigate(targetFront) { const outstanding = [promise.defer(), promise.defer()]; @@ -59,40 +43,16 @@ function testNavigate(targetFront) { }); BrowserTestUtils.loadURI(gBrowser.selectedBrowser, TAB2_URL); - return promise.all(outstanding.map(e => e.promise)) - .then(() => targetFront); + return promise.all(outstanding.map(e => e.promise)); } -async function testDetach(targetFront) { - const onDetached = targetFront.once("tabDetached"); +async function testDetach(target) { + // We can't listen for tabDetached at it is received by Target first + // and target is destroyed + const onDetached = target.once("close"); removeTab(gBrowser.selectedTab); - const packet = await onDetached; + await onDetached; ok(true, "Got a tab detach notification."); - is(packet.from, targetFront.actorID, - "tab detach message comes from the expected actor"); - - return gClient.close(); -} - -registerCleanupFunction(function() { - gClient = null; -}); - -async function attachTargetActorForUrl(client, url) { - const grip = await getTargetActorForUrl(client, url); - const [, targetFront] = await client.attachTarget(grip.actor); - return targetFront; -} - -function getTargetActorForUrl(client, url) { - const deferred = promise.defer(); - - client.listTabs().then(response => { - const targetActor = response.tabs.filter(grip => grip.url == url).pop(); - deferred.resolve(targetActor); - }); - - return deferred.promise; } diff --git a/devtools/client/shared/test/browser_dbg_target-scoped-actor-01.js b/devtools/client/shared/test/browser_dbg_target-scoped-actor-01.js index 63f1470a25fb..8d9e14394a44 100644 --- a/devtools/client/shared/test/browser_dbg_target-scoped-actor-01.js +++ b/devtools/client/shared/test/browser_dbg_target-scoped-actor-01.js @@ -9,17 +9,11 @@ * Check target-scoped actor lifetimes. */ -var { DebuggerServer } = require("devtools/server/main"); -var { DebuggerClient } = require("devtools/shared/client/debugger-client"); - const ACTORS_URL = EXAMPLE_URL + "testactors.js"; const TAB_URL = EXAMPLE_URL + "doc_empty-tab-01.html"; add_task(async function test() { - await addTab(TAB_URL); - - DebuggerServer.init(); - DebuggerServer.registerAllActors(); + const tab = await addTab(TAB_URL); await registerActorInContentProcess(ACTORS_URL, { prefix: "testOne", @@ -27,40 +21,23 @@ add_task(async function test() { type: { target: true }, }); - const transport = DebuggerServer.connectPipe(); - const client = new DebuggerClient(transport); - const [ type ] = await client.connect(); - is(type, "browser", "Root actor should identify itself as a browser."); + const target = await TargetFactory.forTab(tab); + await target.attach(); + const { client } = target; + const targetFront = target.activeTab; + const form = targetFront.targetForm; - const [ grip ] = await attachTargetActorForUrl(client, TAB_URL); - await testTargetScopedActor(client, grip); + await testTargetScopedActor(client, form); await removeTab(gBrowser.selectedTab); - await client.close(); + await target.destroy(); }); -async function testTargetScopedActor(client, grip) { - ok(grip.testOneActor, +async function testTargetScopedActor(client, form) { + ok(form.testOneActor, "Found the test target-scoped actor."); - ok(grip.testOneActor.includes("testOne"), + ok(form.testOneActor.includes("testOne"), "testOneActor's actorPrefix should be used."); - const response = await client.request({ to: grip.testOneActor, type: "ping" }); + const response = await client.request({ to: form.testOneActor, type: "ping" }); is(response.pong, "pong", "Actor should respond to requests."); } - -async function attachTargetActorForUrl(client, url) { - const grip = await getTargetActorForUrl(client, url); - const [ response ] = await client.attachTarget(grip.actor); - return [grip, response]; -} - -function getTargetActorForUrl(client, url) { - const deferred = promise.defer(); - - client.listTabs().then(response => { - const targetActor = response.tabs.filter(grip => grip.url == url).pop(); - deferred.resolve(targetActor); - }); - - return deferred.promise; -} diff --git a/devtools/client/shared/test/browser_dbg_target-scoped-actor-02.js b/devtools/client/shared/test/browser_dbg_target-scoped-actor-02.js index 9fb9cfb60eab..87a1ac5e5cf2 100644 --- a/devtools/client/shared/test/browser_dbg_target-scoped-actor-02.js +++ b/devtools/client/shared/test/browser_dbg_target-scoped-actor-02.js @@ -9,17 +9,11 @@ * Check target-scoped actor lifetimes. */ -var { DebuggerServer } = require("devtools/server/main"); -var { DebuggerClient } = require("devtools/shared/client/debugger-client"); - const ACTORS_URL = EXAMPLE_URL + "testactors.js"; const TAB_URL = EXAMPLE_URL + "doc_empty-tab-01.html"; add_task(async function() { - await addTab(TAB_URL); - - DebuggerServer.init(); - DebuggerServer.registerAllActors(); + const tab = await addTab(TAB_URL); await registerActorInContentProcess(ACTORS_URL, { prefix: "testOne", @@ -27,51 +21,33 @@ add_task(async function() { type: { target: true }, }); - const transport = DebuggerServer.connectPipe(); - const client = new DebuggerClient(transport); - const [type] = await client.connect(); - is(type, "browser", - "Root actor should identify itself as a browser."); + const target = await TargetFactory.forTab(tab); + await target.attach(); + const { client } = target; + const targetFront = target.activeTab; + const form = targetFront.targetForm; - const [grip] = await attachTargetActorForUrl(client, TAB_URL); - await testTargetScopedActor(client, grip); - await closeTab(client, grip); - await client.close(); + await testTargetScopedActor(client, form); + await closeTab(client, form); + await target.destroy(); }); -async function testTargetScopedActor(client, grip) { - ok(grip.testOneActor, +async function testTargetScopedActor(client, form) { + ok(form.testOneActor, "Found the test target-scoped actor."); - ok(grip.testOneActor.includes("testOne"), + ok(form.testOneActor.includes("testOne"), "testOneActor's actorPrefix should be used."); - const response = await client.request({ to: grip.testOneActor, type: "ping" }); + const response = await client.request({ to: form.testOneActor, type: "ping" }); is(response.pong, "pong", "Actor should respond to requests."); } -async function closeTab(client, grip) { +async function closeTab(client, form) { await removeTab(gBrowser.selectedTab); await Assert.rejects( - client.request({ to: grip.testOneActor, type: "ping" }), - err => err.message === `'ping' active request packet to '${grip.testOneActor}' ` + + client.request({ to: form.testOneActor, type: "ping" }), + err => err.message === `'ping' active request packet to '${form.testOneActor}' ` + `can't be sent as the connection just closed.`, "testOneActor went away."); } - -async function attachTargetActorForUrl(client, url) { - const grip = await getTargetActorForUrl(client, url); - const [ response ] = await client.attachTarget(grip.actor); - return [grip, response]; -} - -function getTargetActorForUrl(client, url) { - const deferred = promise.defer(); - - client.listTabs().then(response => { - const targetActor = response.tabs.filter(grip => grip.url == url).pop(); - deferred.resolve(targetActor); - }); - - return deferred.promise; -} diff --git a/devtools/client/shared/test/browser_dbg_worker-window.js b/devtools/client/shared/test/browser_dbg_worker-window.js index 51c8e79b0e2c..44f759686680 100644 --- a/devtools/client/shared/test/browser_dbg_worker-window.js +++ b/devtools/client/shared/test/browser_dbg_worker-window.js @@ -15,15 +15,10 @@ const WORKER_URL = "code_WorkerTargetActor.attachThread-worker.js"; add_task(async function() { await pushPrefs(["devtools.scratchpad.enabled", true]); - DebuggerServer.init(); - DebuggerServer.registerAllActors(); - - const client = new DebuggerClient(DebuggerServer.connectPipe()); - await connect(client); - const tab = await addTab(TAB_URL); - const { tabs } = await listTabs(client); - const [, targetFront] = await attachTarget(client, findTab(tabs, TAB_URL)); + const target = await TargetFactory.forTab(tab); + await target.attach(); + const targetFront = target.activeTab; await listWorkers(targetFront); await createWorkerInTab(tab, WORKER_URL); @@ -56,7 +51,7 @@ add_task(async function() { terminateWorkerInTab(tab, WORKER_URL); await waitForWorkerClose(workerTargetFront); - await close(client); + await target.destroy(); await toolbox.destroy(); }); diff --git a/devtools/client/shared/test/helper_workers.js b/devtools/client/shared/test/helper_workers.js index 0931f90c2850..3ef998582f11 100644 --- a/devtools/client/shared/test/helper_workers.js +++ b/devtools/client/shared/test/helper_workers.js @@ -172,15 +172,11 @@ function getSplitConsole(toolbox, win) { } async function initWorkerDebugger(TAB_URL, WORKER_URL) { - DebuggerServer.init(); - DebuggerServer.registerAllActors(); - - const client = new DebuggerClient(DebuggerServer.connectPipe()); - await connect(client); - const tab = await addTab(TAB_URL); - const { tabs } = await listTabs(client); - const [, targetFront] = await attachTarget(client, findTab(tabs, TAB_URL)); + const target = await TargetFactory.forTab(tab); + await target.attach(); + const { client } = target; + const targetFront = target.activeTab; await createWorkerInTab(tab, WORKER_URL); @@ -242,28 +238,13 @@ this.removeTab = function removeTab(tab, win) { return deferred.promise; }; -async function attachTargetActorForUrl(client, url) { - const grip = await getTargetActorForUrl(client, url); - const [ response, front ] = await client.attachTarget(grip.actor); - return [grip, response, front]; -} - -async function attachThreadActorForUrl(client, url) { - const [, response] = await attachTargetActorForUrl(client, url); - const [, threadClient] = await client.attachThread(response.threadActor); +async function attachThreadActorForTab(tab) { + const target = await TargetFactory.forTab(tab); + await target.attach(); + const targetFront = target.activeTab; + const [, threadClient] = await targetFront.attachThread(); await threadClient.resume(); - return threadClient; -} - -function getTargetActorForUrl(client, url) { - const deferred = getDeferredPromise().defer(); - - client.listTabs().then(response => { - const targetActor = response.tabs.filter(grip => grip.url == url).pop(); - deferred.resolve(targetActor); - }); - - return deferred.promise; + return { client: target.client, threadClient }; } function pushPrefs(...aPrefs) { diff --git a/devtools/server/tests/browser/browser_navigateEvents.js b/devtools/server/tests/browser/browser_navigateEvents.js index cb787241a4f1..d432c171b176 100644 --- a/devtools/server/tests/browser/browser_navigateEvents.js +++ b/devtools/server/tests/browser/browser_navigateEvents.js @@ -95,20 +95,15 @@ function onMessage({ data }) { assertEvent(data.event, data.data); } -async function connectAndAttachTab() { - // Ensure having a minimal server - initDebuggerServer(); - - // Connect to this tab - const transport = DebuggerServer.connectPipe(); - const client = new DebuggerClient(transport); - const form = await connectDebuggerClient(client); - const actorID = form.actor; - const [, targetFront ] = await client.attachTarget(actorID); +async function connectAndAttachTab(tab) { + const target = await TargetFactory.forTab(tab); + await target.attach(); + const targetFront = target.activeTab; + const actorID = targetFront.targetForm.actor; targetFront.on("tabNavigated", function(packet) { assertEvent("tabNavigated", packet); }); - return { client, actorID }; + return { target, actorID }; } add_task(async function() { @@ -125,7 +120,8 @@ add_task(async function() { // Listen for messages sent by the content task browser.messageManager.addMessageListener("devtools-test:event", onMessage); - const { client, actorID } = await connectAndAttachTab(); + const tab = gBrowser.getTabForBrowser(browser); + const { target, actorID } = await connectAndAttachTab(tab); await ContentTask.spawn(browser, [actorID], async function(actorId) { const { require } = ChromeUtils.import("resource://devtools/shared/Loader.jsm", {}); const { DebuggerServer } = require("devtools/server/main"); @@ -170,7 +166,7 @@ add_task(async function() { // Cleanup browser.messageManager.removeMessageListener("devtools-test:event", onMessage); - await client.close(); + await target.destroy(); Services.obs.addObserver(httpObserver, "http-on-modify-request"); DebuggerServer.destroy(); }); diff --git a/devtools/server/tests/browser/browser_spawn_actor_in_parent.js b/devtools/server/tests/browser/browser_spawn_actor_in_parent.js index 749d8aa3c3c3..dbedd5775dc7 100644 --- a/devtools/server/tests/browser/browser_spawn_actor_in_parent.js +++ b/devtools/server/tests/browser/browser_spawn_actor_in_parent.js @@ -13,7 +13,7 @@ const ACTOR_URL = "chrome://mochitests/content/browser/devtools/server/tests/bro const { InContentFront, InParentFront } = require(ACTOR_URL); add_task(async function() { - await addTab("data:text/html;charset=utf-8,foo"); + const browser = await addTab("data:text/html;charset=utf-8,foo"); info("Register target-scoped actor in the content process"); await registerActorInContentProcess(ACTOR_URL, { @@ -22,9 +22,13 @@ add_task(async function() { type: { target: true }, }); - initDebuggerServer(); - const client = new DebuggerClient(DebuggerServer.connectPipe()); - const form = await connectDebuggerClient(client); + const tab = gBrowser.getTabForBrowser(browser); + const target = await TargetFactory.forTab(tab); + await target.attach(); + const targetFront = target.activeTab; + const { client } = target; + const form = targetFront.targetForm; + const inContentFront = InContentFront(client, form); const isInContent = await inContentFront.isInContent(); ok(isInContent, "ContentActor really runs in the content process"); @@ -43,6 +47,6 @@ add_task(async function() { ok(conn, "`conn`, first contructor argument is a DebuggerServerConnection instance"); is(mm, "ChromeMessageSender", "`mm`, last constructor argument is a message manager"); - await client.close(); + await target.destroy(); gBrowser.removeCurrentTab(); }); diff --git a/devtools/server/tests/browser/head.js b/devtools/server/tests/browser/head.js index 2329348d4696..fa1de2f7e58f 100644 --- a/devtools/server/tests/browser/head.js +++ b/devtools/server/tests/browser/head.js @@ -136,21 +136,6 @@ function waitUntilClientConnected(client) { }); } -/** - * Connect a debugger client. - * - * @param {DebuggerClient} - * @return {Promise} Resolves to the targetActor form for the selected tab when the client - * is connected. - */ -function connectDebuggerClient(client) { - return client.connect() - .then(() => client.listTabs()) - .then(tabs => { - return tabs.tabs[tabs.selected]; - }); -} - /** * Wait for eventName on target. * @param {Object} target An observable object that either supports on/off or diff --git a/devtools/server/tests/mochitest/webconsole-helpers.js b/devtools/server/tests/mochitest/webconsole-helpers.js index 327e4c958dfe..5521d22be614 100644 --- a/devtools/server/tests/mochitest/webconsole-helpers.js +++ b/devtools/server/tests/mochitest/webconsole-helpers.js @@ -2,8 +2,8 @@ "use strict"; const {require} = ChromeUtils.import("resource://devtools/shared/Loader.jsm", {}); -const {DebuggerClient} = require("devtools/shared/client/debugger-client"); const {DebuggerServer} = require("devtools/server/main"); +const {TargetFactory} = require("devtools/client/framework/target"); const Services = require("Services"); @@ -35,37 +35,24 @@ if (!DebuggerServer.initialized) { * the opened tab and disconnect its debugger client. */ async function attachURL(url) { - let win = window.open(url, "_blank"); - let client = null; - - const cleanup = async function() { - if (client) { - await client.close(); - client = null; - } - if (win) { - win.close(); - win = null; - } - }; - SimpleTest.registerCleanupFunction(cleanup); - - client = new DebuggerClient(DebuggerServer.connectPipe()); - await client.connect(); - const {tabs} = await client.listTabs(); - const attachedTab = tabs.find(tab => tab.url === url); - - if (!attachedTab) { - throw new Error(`Could not find a tab matching URL ${url}`); - } - - const [, targetFront] = await client.attachTarget(attachedTab.actor); - const [, consoleClient] = await client.attachConsole(attachedTab.consoleActor, []); + const tab = await addTab(url); + const target = await TargetFactory.forTab(tab); + const { client } = target; + const { consoleActor } = target.form; + const [, consoleClient] = await client.attachConsole(consoleActor, []); return { - tab: attachedTab, - targetFront, consoleClient, - cleanup, }; } + +/** + * Naive implementaion of addTab working from a mochitest-chrome test. + */ +async function addTab(url) { + const { gBrowser } = Services.wm.getMostRecentWindow("navigator:browser"); + const {BrowserTestUtils} = require("resource://testing-common/BrowserTestUtils.jsm"); + const tab = gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser, url); + await BrowserTestUtils.browserLoaded(tab.linkedBrowser); + return tab; +}