Bug 1509315 - Use TargetFactory.forTab for creating targets in tests. r=yulia

There is 4 patterns here:
* Tests using attachThreadActorForUrl like event-listeners ones. I augmented this helper method to call TargetFactory.
* Tests using attachTargetActorForUrl like debugger-statement, dbg-navigation and target-scoped-actor. Uses TargetFactory directly.
* Tests using connectDebuggerClient like navigateEvents and spaw_actor_in_parent. Uses TargetFactory directly.
* All the others, where creating DebuggerClient on their own, and calling listTabs+attachTarget. They now use TargetFactory directly.

A note about webconsole-helpers, only consoleClient attribute was used from tests using this helper.
Also, in various tests I remove the call to client.destroy, that's because shared-head will
close all the tabs/toolboxes and it should lead to target/client destruction.
Finally, debugger-statement now request the thread actor via the client instead of manual request,
it should help the refactoring to a front!

MozReview-Commit-ID: 2ah4UmSjuoi

Depends on D12730

Differential Revision: https://phabricator.services.mozilla.com/D12731

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Alexandre Poirot 2018-11-27 21:47:59 +00:00
Родитель 90639c1f6b
Коммит ae8986ee28
18 изменённых файлов: 149 добавлений и 437 удалений

Просмотреть файл

@ -152,30 +152,6 @@ function removeAddon(aAddon) {
return deferred.promise; 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 // 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 // before EventEmitter. Since this directory is deprecated, there's little value in
// resolving the descrepency here. // resolving the descrepency here.

Просмотреть файл

@ -6,7 +6,6 @@
"use strict"; "use strict";
const { DebuggerServer } = require("devtools/server/main"); const { DebuggerServer } = require("devtools/server/main");
const { DebuggerClient } = require("devtools/shared/client/debugger-client");
// Bug 1277805: Too slow for debug runs // Bug 1277805: Too slow for debug runs
requestLongerTimeout(2); 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() { function test() {
(async function() { (async function() {
toggleAllTools(true); toggleAllTools(true);
await addTab("about:blank"); const tab = await addTab("about:blank");
const client = await getClient(); const target = await TargetFactory.forTab(tab);
const target = await getTarget(client); const { client } = target;
await runTools(target); await runTools(target);
const rootFronts = [...client.mainRoot.fronts.values()]; const rootFronts = [...client.mainRoot.fronts.values()];

Просмотреть файл

@ -26,15 +26,10 @@ function test() {
const oldMaxTotalViewers = SpecialPowers.getIntPref(MAX_TOTAL_VIEWERS); const oldMaxTotalViewers = SpecialPowers.getIntPref(MAX_TOTAL_VIEWERS);
SpecialPowers.setIntPref(MAX_TOTAL_VIEWERS, 10); 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 tab = yield addTab(TAB1_URL);
const { tabs } = yield listTabs(client); const target = yield TargetFactory.forTab(tab);
const [, targetFront] = yield attachTarget(client, findTab(tabs, TAB1_URL)); yield target.attach();
const targetFront = target.activeTab;
yield listWorkers(targetFront); yield listWorkers(targetFront);
// If a page still has pending network requests, it will not be moved into // If a page still has pending network requests, it will not be moved into
@ -71,7 +66,7 @@ function test() {
yield workerTargetFront1.attach(); yield workerTargetFront1.attach();
is(workerTargetFront1.isClosed, false, "worker in tab 1 should not be closed"); is(workerTargetFront1.isClosed, false, "worker in tab 1 should not be closed");
yield close(client); yield target.destroy();
SpecialPowers.setIntPref(MAX_TOTAL_VIEWERS, oldMaxTotalViewers); SpecialPowers.setIntPref(MAX_TOTAL_VIEWERS, oldMaxTotalViewers);
finish(); finish();
}); });

Просмотреть файл

@ -15,82 +15,53 @@ Services.scriptloader.loadSubScript(
"chrome://mochitests/content/browser/devtools/client/shared/test/helper_workers.js", "chrome://mochitests/content/browser/devtools/client/shared/test/helper_workers.js",
this); 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"; const TAB_URL = TEST_URI_ROOT + "doc_inline-debugger-statement.html";
var gClient; add_task(async () => {
var gTab; const tab = await addTab(TAB_URL);
const target = await TargetFactory.forTab(tab);
await target.attach();
const { client } = target;
const targetFront = target.activeTab;
function test() { const threadClient = await testEarlyDebuggerStatement(client, tab, targetFront);
DebuggerServer.init(); await testDebuggerStatement(client, tab, threadClient);
DebuggerServer.registerAllActors();
const transport = DebuggerServer.connectPipe(); await target.destroy();
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();
async function testEarlyDebuggerStatement(client, tab, targetFront) {
const onPaused = function(event, packet) { const onPaused = function(event, packet) {
ok(false, "Pause shouldn't be called before we've attached!"); 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 // This should continue without nesting an event loop and calling
// the onPaused hook, because we haven't attached yet. // 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... // Now attach and resume...
gClient.request({ to: aResponse.threadActor, type: "attach" }, () => { const [, threadClient] = await targetFront.attachThread();
gClient.request({ to: aResponse.threadActor, type: "resume" }, () => { await threadClient.resume();
ok(true, "Pause wasn't called before we've attached."); ok(true, "Pause wasn't called before we've attached.");
deferred.resolve([aGrip, aResponse]);
});
});
return deferred.promise; return threadClient;
} }
function testDebuggerStatement([aGrip, aResponse]) { async function testDebuggerStatement(client, tab, threadClient) {
const deferred = getDeferredPromise().defer(); const onPaused = new Promise(resolve => {
client.addListener("paused", async (event, packet) => {
gClient.addListener("paused", (event, packet) => { await threadClient.resume();
gClient.request({ to: aResponse.threadActor, type: "resume" }, () => {
ok(true, "The pause handler was triggered on a debugger statement."); ok(true, "The pause handler was triggered on a debugger statement.");
deferred.resolve(); resolve();
}); });
}); });
// Reach around the debugging protocol and execute the debugger statement. // 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;
});

Просмотреть файл

@ -15,28 +15,14 @@ Services.scriptloader.loadSubScript(
"chrome://mochitests/content/browser/devtools/client/shared/test/helper_workers.js", "chrome://mochitests/content/browser/devtools/client/shared/test/helper_workers.js",
this); 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"; const TAB_URL = TEST_URI_ROOT + "doc_event-listeners-01.html";
add_task(async function() { 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 tab = await addTab(TAB_URL);
const threadClient = await attachThreadActorForUrl(client, TAB_URL); const { client, threadClient } = await attachThreadActorForTab(tab);
await pauseDebuggee(tab, client, threadClient); await pauseDebuggee(tab, client, threadClient);
await testEventListeners(client, threadClient); await testEventListeners(client, threadClient);
await client.close();
}); });
function pauseDebuggee(tab, client, threadClient) { function pauseDebuggee(tab, client, threadClient) {

Просмотреть файл

@ -16,27 +16,14 @@ Services.scriptloader.loadSubScript(
"chrome://mochitests/content/browser/devtools/client/shared/test/helper_workers.js", "chrome://mochitests/content/browser/devtools/client/shared/test/helper_workers.js",
this); 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"; const TAB_URL = TEST_URI_ROOT + "doc_event-listeners-03.html";
add_task(async function() { 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 tab = await addTab(TAB_URL);
const threadClient = await attachThreadActorForUrl(client, TAB_URL); const { client, threadClient } = await attachThreadActorForTab(tab);
await pauseDebuggee(tab, client, threadClient); await pauseDebuggee(tab, client, threadClient);
await testEventListeners(client, threadClient); await testEventListeners(client, threadClient);
await client.close();
}); });
function pauseDebuggee(tab, client, threadClient) { function pauseDebuggee(tab, client, threadClient) {

Просмотреть файл

@ -16,43 +16,20 @@ Services.scriptloader.loadSubScript(
"chrome://mochitests/content/browser/devtools/client/shared/test/helper_workers.js", "chrome://mochitests/content/browser/devtools/client/shared/test/helper_workers.js",
this); 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"; const TAB_URL = TEST_URI_ROOT + "doc_native-event-handler.html";
var gClient; add_task(async () => {
var gTab; const tab = await addTab(TAB_URL);
const { client, threadClient } = await attachThreadActorForTab(tab);
function test() { await pauseDebuggee(client, tab, threadClient);
DebuggerServer.init(); await testEventListeners(threadClient);
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(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(); const deferred = getDeferredPromise().defer();
gClient.addOneTimeListener("paused", (event, packet) => { client.addOneTimeListener("paused", (event, packet) => {
is(packet.type, "paused", is(packet.type, "paused",
"We should now be paused."); "We should now be paused.");
is(packet.why.type, "debuggerStatement", is(packet.why.type, "debuggerStatement",
@ -61,7 +38,7 @@ function pauseDebuggee(threadClient) {
deferred.resolve(threadClient); deferred.resolve(threadClient);
}); });
generateMouseClickInTab(gTab, "content.document.querySelector('button')"); generateMouseClickInTab(tab, "content.document.querySelector('button')");
return deferred.promise; return deferred.promise;
} }
@ -86,7 +63,3 @@ function testEventListeners(threadClient) {
return deferred.promise; return deferred.promise;
} }
registerCleanupFunction(function() {
gClient = null;
});

Просмотреть файл

@ -44,9 +44,9 @@ function testFirstTab() {
return addTab(TAB1_URL).then(tab => { return addTab(TAB1_URL).then(tab => {
gTab1 = tab; gTab1 = tab;
return getTargetActorForUrl(gClient, TAB1_URL).then(grip => { return getTargetActorForUrl(gClient, TAB1_URL).then(form => {
ok(grip, "Should find a target actor for the first tab."); ok(form, "Should find a target actor for the first tab.");
gTab1Actor = grip.actor; gTab1Actor = form.actor;
}); });
}); });
} }
@ -67,8 +67,8 @@ function testSecondTab() {
function testRemoveTab() { function testRemoveTab() {
return removeTab(gTab1).then(() => { return removeTab(gTab1).then(() => {
return getTargetActorForUrl(gClient, TAB1_URL).then(grip => { return getTargetActorForUrl(gClient, TAB1_URL).then(form => {
ok(!grip, "Shouldn't find a target actor for the first tab anymore."); ok(!form, "Shouldn't find a target actor for the first tab anymore.");
}); });
}); });
} }
@ -100,13 +100,8 @@ registerCleanupFunction(function() {
gClient = null; gClient = null;
}); });
function getTargetActorForUrl(client, url) { async function getTargetActorForUrl(client, url) {
const deferred = promise.defer(); const { tabs } = await client.listTabs();
const targetActor = tabs.filter(form => form.url == url).pop();
client.listTabs().then(response => { return targetActor;
const targetActor = response.tabs.filter(grip => grip.url == url).pop();
deferred.resolve(targetActor);
});
return deferred.promise;
} }

Просмотреть файл

@ -17,23 +17,15 @@ Services.scriptloader.loadSubScript(
"chrome://mochitests/content/browser/devtools/client/shared/test/helper_workers.js", "chrome://mochitests/content/browser/devtools/client/shared/test/helper_workers.js",
this); 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 TAB_URL = EXAMPLE_URL + "doc_listworkers-tab.html";
var WORKER1_URL = "code_listworkers-worker1.js"; var WORKER1_URL = "code_listworkers-worker1.js";
var WORKER2_URL = "code_listworkers-worker2.js"; var WORKER2_URL = "code_listworkers-worker2.js";
add_task(async function test() { 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 tab = await addTab(TAB_URL);
const { tabs } = await listTabs(client); const target = await TargetFactory.forTab(tab);
const [, targetFront] = await attachTarget(client, findTab(tabs, TAB_URL)); await target.attach();
const targetFront = target.activeTab;
let { workers } = await listWorkers(targetFront); let { workers } = await listWorkers(targetFront);
is(workers.length, 0); is(workers.length, 0);
@ -74,6 +66,6 @@ add_task(async function test() {
({ workers } = await listWorkers(targetFront)); ({ workers } = await listWorkers(targetFront));
is(workers.length, 0); is(workers.length, 0);
await close(client); await target.destroy();
finish(); finish();
}); });

Просмотреть файл

@ -9,35 +9,19 @@
* Check tab attach/navigation. * 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 TAB1_URL = EXAMPLE_URL + "doc_empty-tab-01.html";
const TAB2_FILE = "doc_empty-tab-02.html"; const TAB2_FILE = "doc_empty-tab-02.html";
const TAB2_URL = EXAMPLE_URL + TAB2_FILE; 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() { await testNavigate(targetFront);
DebuggerServer.init(); await testDetach(target);
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);
}); });
});
}
function testNavigate(targetFront) { function testNavigate(targetFront) {
const outstanding = [promise.defer(), promise.defer()]; const outstanding = [promise.defer(), promise.defer()];
@ -59,40 +43,16 @@ function testNavigate(targetFront) {
}); });
BrowserTestUtils.loadURI(gBrowser.selectedBrowser, TAB2_URL); BrowserTestUtils.loadURI(gBrowser.selectedBrowser, TAB2_URL);
return promise.all(outstanding.map(e => e.promise)) return promise.all(outstanding.map(e => e.promise));
.then(() => targetFront);
} }
async function testDetach(targetFront) { async function testDetach(target) {
const onDetached = targetFront.once("tabDetached"); // 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); removeTab(gBrowser.selectedTab);
const packet = await onDetached; await onDetached;
ok(true, "Got a tab detach notification."); 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;
} }

Просмотреть файл

@ -9,17 +9,11 @@
* Check target-scoped actor lifetimes. * 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 ACTORS_URL = EXAMPLE_URL + "testactors.js";
const TAB_URL = EXAMPLE_URL + "doc_empty-tab-01.html"; const TAB_URL = EXAMPLE_URL + "doc_empty-tab-01.html";
add_task(async function test() { add_task(async function test() {
await addTab(TAB_URL); const tab = await addTab(TAB_URL);
DebuggerServer.init();
DebuggerServer.registerAllActors();
await registerActorInContentProcess(ACTORS_URL, { await registerActorInContentProcess(ACTORS_URL, {
prefix: "testOne", prefix: "testOne",
@ -27,40 +21,23 @@ add_task(async function test() {
type: { target: true }, type: { target: true },
}); });
const transport = DebuggerServer.connectPipe(); const target = await TargetFactory.forTab(tab);
const client = new DebuggerClient(transport); await target.attach();
const [ type ] = await client.connect(); const { client } = target;
is(type, "browser", "Root actor should identify itself as a browser."); const targetFront = target.activeTab;
const form = targetFront.targetForm;
const [ grip ] = await attachTargetActorForUrl(client, TAB_URL); await testTargetScopedActor(client, form);
await testTargetScopedActor(client, grip);
await removeTab(gBrowser.selectedTab); await removeTab(gBrowser.selectedTab);
await client.close(); await target.destroy();
}); });
async function testTargetScopedActor(client, grip) { async function testTargetScopedActor(client, form) {
ok(grip.testOneActor, ok(form.testOneActor,
"Found the test target-scoped actor."); "Found the test target-scoped actor.");
ok(grip.testOneActor.includes("testOne"), ok(form.testOneActor.includes("testOne"),
"testOneActor's actorPrefix should be used."); "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."); 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;
}

Просмотреть файл

@ -9,17 +9,11 @@
* Check target-scoped actor lifetimes. * 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 ACTORS_URL = EXAMPLE_URL + "testactors.js";
const TAB_URL = EXAMPLE_URL + "doc_empty-tab-01.html"; const TAB_URL = EXAMPLE_URL + "doc_empty-tab-01.html";
add_task(async function() { add_task(async function() {
await addTab(TAB_URL); const tab = await addTab(TAB_URL);
DebuggerServer.init();
DebuggerServer.registerAllActors();
await registerActorInContentProcess(ACTORS_URL, { await registerActorInContentProcess(ACTORS_URL, {
prefix: "testOne", prefix: "testOne",
@ -27,51 +21,33 @@ add_task(async function() {
type: { target: true }, type: { target: true },
}); });
const transport = DebuggerServer.connectPipe(); const target = await TargetFactory.forTab(tab);
const client = new DebuggerClient(transport); await target.attach();
const [type] = await client.connect(); const { client } = target;
is(type, "browser", const targetFront = target.activeTab;
"Root actor should identify itself as a browser."); const form = targetFront.targetForm;
const [grip] = await attachTargetActorForUrl(client, TAB_URL); await testTargetScopedActor(client, form);
await testTargetScopedActor(client, grip); await closeTab(client, form);
await closeTab(client, grip); await target.destroy();
await client.close();
}); });
async function testTargetScopedActor(client, grip) { async function testTargetScopedActor(client, form) {
ok(grip.testOneActor, ok(form.testOneActor,
"Found the test target-scoped actor."); "Found the test target-scoped actor.");
ok(grip.testOneActor.includes("testOne"), ok(form.testOneActor.includes("testOne"),
"testOneActor's actorPrefix should be used."); "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", is(response.pong, "pong",
"Actor should respond to requests."); "Actor should respond to requests.");
} }
async function closeTab(client, grip) { async function closeTab(client, form) {
await removeTab(gBrowser.selectedTab); await removeTab(gBrowser.selectedTab);
await Assert.rejects( await Assert.rejects(
client.request({ to: grip.testOneActor, type: "ping" }), client.request({ to: form.testOneActor, type: "ping" }),
err => err.message === `'ping' active request packet to '${grip.testOneActor}' ` + err => err.message === `'ping' active request packet to '${form.testOneActor}' ` +
`can't be sent as the connection just closed.`, `can't be sent as the connection just closed.`,
"testOneActor went away."); "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;
}

Просмотреть файл

@ -15,15 +15,10 @@ const WORKER_URL = "code_WorkerTargetActor.attachThread-worker.js";
add_task(async function() { add_task(async function() {
await pushPrefs(["devtools.scratchpad.enabled", true]); 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 tab = await addTab(TAB_URL);
const { tabs } = await listTabs(client); const target = await TargetFactory.forTab(tab);
const [, targetFront] = await attachTarget(client, findTab(tabs, TAB_URL)); await target.attach();
const targetFront = target.activeTab;
await listWorkers(targetFront); await listWorkers(targetFront);
await createWorkerInTab(tab, WORKER_URL); await createWorkerInTab(tab, WORKER_URL);
@ -56,7 +51,7 @@ add_task(async function() {
terminateWorkerInTab(tab, WORKER_URL); terminateWorkerInTab(tab, WORKER_URL);
await waitForWorkerClose(workerTargetFront); await waitForWorkerClose(workerTargetFront);
await close(client); await target.destroy();
await toolbox.destroy(); await toolbox.destroy();
}); });

Просмотреть файл

@ -172,15 +172,11 @@ function getSplitConsole(toolbox, win) {
} }
async function initWorkerDebugger(TAB_URL, WORKER_URL) { 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 tab = await addTab(TAB_URL);
const { tabs } = await listTabs(client); const target = await TargetFactory.forTab(tab);
const [, targetFront] = await attachTarget(client, findTab(tabs, TAB_URL)); await target.attach();
const { client } = target;
const targetFront = target.activeTab;
await createWorkerInTab(tab, WORKER_URL); await createWorkerInTab(tab, WORKER_URL);
@ -242,28 +238,13 @@ this.removeTab = function removeTab(tab, win) {
return deferred.promise; return deferred.promise;
}; };
async function attachTargetActorForUrl(client, url) { async function attachThreadActorForTab(tab) {
const grip = await getTargetActorForUrl(client, url); const target = await TargetFactory.forTab(tab);
const [ response, front ] = await client.attachTarget(grip.actor); await target.attach();
return [grip, response, front]; const targetFront = target.activeTab;
} const [, threadClient] = await targetFront.attachThread();
async function attachThreadActorForUrl(client, url) {
const [, response] = await attachTargetActorForUrl(client, url);
const [, threadClient] = await client.attachThread(response.threadActor);
await threadClient.resume(); await threadClient.resume();
return threadClient; return { client: target.client, 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;
} }
function pushPrefs(...aPrefs) { function pushPrefs(...aPrefs) {

Просмотреть файл

@ -95,20 +95,15 @@ function onMessage({ data }) {
assertEvent(data.event, data.data); assertEvent(data.event, data.data);
} }
async function connectAndAttachTab() { async function connectAndAttachTab(tab) {
// Ensure having a minimal server const target = await TargetFactory.forTab(tab);
initDebuggerServer(); await target.attach();
const targetFront = target.activeTab;
// Connect to this tab const actorID = targetFront.targetForm.actor;
const transport = DebuggerServer.connectPipe();
const client = new DebuggerClient(transport);
const form = await connectDebuggerClient(client);
const actorID = form.actor;
const [, targetFront ] = await client.attachTarget(actorID);
targetFront.on("tabNavigated", function(packet) { targetFront.on("tabNavigated", function(packet) {
assertEvent("tabNavigated", packet); assertEvent("tabNavigated", packet);
}); });
return { client, actorID }; return { target, actorID };
} }
add_task(async function() { add_task(async function() {
@ -125,7 +120,8 @@ add_task(async function() {
// Listen for messages sent by the content task // Listen for messages sent by the content task
browser.messageManager.addMessageListener("devtools-test:event", onMessage); 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) { await ContentTask.spawn(browser, [actorID], async function(actorId) {
const { require } = ChromeUtils.import("resource://devtools/shared/Loader.jsm", {}); const { require } = ChromeUtils.import("resource://devtools/shared/Loader.jsm", {});
const { DebuggerServer } = require("devtools/server/main"); const { DebuggerServer } = require("devtools/server/main");
@ -170,7 +166,7 @@ add_task(async function() {
// Cleanup // Cleanup
browser.messageManager.removeMessageListener("devtools-test:event", onMessage); browser.messageManager.removeMessageListener("devtools-test:event", onMessage);
await client.close(); await target.destroy();
Services.obs.addObserver(httpObserver, "http-on-modify-request"); Services.obs.addObserver(httpObserver, "http-on-modify-request");
DebuggerServer.destroy(); DebuggerServer.destroy();
}); });

Просмотреть файл

@ -13,7 +13,7 @@ const ACTOR_URL = "chrome://mochitests/content/browser/devtools/server/tests/bro
const { InContentFront, InParentFront } = require(ACTOR_URL); const { InContentFront, InParentFront } = require(ACTOR_URL);
add_task(async function() { 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"); info("Register target-scoped actor in the content process");
await registerActorInContentProcess(ACTOR_URL, { await registerActorInContentProcess(ACTOR_URL, {
@ -22,9 +22,13 @@ add_task(async function() {
type: { target: true }, type: { target: true },
}); });
initDebuggerServer(); const tab = gBrowser.getTabForBrowser(browser);
const client = new DebuggerClient(DebuggerServer.connectPipe()); const target = await TargetFactory.forTab(tab);
const form = await connectDebuggerClient(client); await target.attach();
const targetFront = target.activeTab;
const { client } = target;
const form = targetFront.targetForm;
const inContentFront = InContentFront(client, form); const inContentFront = InContentFront(client, form);
const isInContent = await inContentFront.isInContent(); const isInContent = await inContentFront.isInContent();
ok(isInContent, "ContentActor really runs in the content process"); 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"); ok(conn, "`conn`, first contructor argument is a DebuggerServerConnection instance");
is(mm, "ChromeMessageSender", "`mm`, last constructor argument is a message manager"); is(mm, "ChromeMessageSender", "`mm`, last constructor argument is a message manager");
await client.close(); await target.destroy();
gBrowser.removeCurrentTab(); gBrowser.removeCurrentTab();
}); });

Просмотреть файл

@ -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. * Wait for eventName on target.
* @param {Object} target An observable object that either supports on/off or * @param {Object} target An observable object that either supports on/off or

Просмотреть файл

@ -2,8 +2,8 @@
"use strict"; "use strict";
const {require} = ChromeUtils.import("resource://devtools/shared/Loader.jsm", {}); const {require} = ChromeUtils.import("resource://devtools/shared/Loader.jsm", {});
const {DebuggerClient} = require("devtools/shared/client/debugger-client");
const {DebuggerServer} = require("devtools/server/main"); const {DebuggerServer} = require("devtools/server/main");
const {TargetFactory} = require("devtools/client/framework/target");
const Services = require("Services"); const Services = require("Services");
@ -35,37 +35,24 @@ if (!DebuggerServer.initialized) {
* the opened tab and disconnect its debugger client. * the opened tab and disconnect its debugger client.
*/ */
async function attachURL(url) { async function attachURL(url) {
let win = window.open(url, "_blank"); const tab = await addTab(url);
let client = null; const target = await TargetFactory.forTab(tab);
const { client } = target;
const cleanup = async function() { const { consoleActor } = target.form;
if (client) { const [, consoleClient] = await client.attachConsole(consoleActor, []);
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, []);
return { return {
tab: attachedTab,
targetFront,
consoleClient, 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;
}