Bug 1560281 - Emit Target.targetCreated for the main process target. r=remote-protocol-reviewers,ato

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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Alexandre Poirot 2019-06-20 13:51:53 +00:00
Родитель 8c54f21269
Коммит c4a5d97834
4 изменённых файлов: 42 добавлений и 6 удалений

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

@ -46,6 +46,7 @@ class Target extends Domain {
browserContextId: target.id,
targetId: target.id,
type: target.type,
url: target.url,
},
});
}

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

@ -52,8 +52,13 @@ class Targets {
clear() {
for (const target of this) {
this.disconnect(target.browser);
// The main process target doesn't have a `browser` and so would fail here.
// Ignore it here, and instead destroy it individually right after this.
if (target != this.mainProcessTarget) {
this.disconnect(target.browser);
}
}
this._targets.clear();
if (this.mainProcessTarget) {
this.mainProcessTarget.disconnect();
this.mainProcessTarget = null;
@ -80,6 +85,7 @@ class Targets {
getMainProcessTarget() {
if (!this.mainProcessTarget) {
this.mainProcessTarget = new MainProcessTarget(this);
this._targets.set(this.mainProcessTarget.id, this.mainProcessTarget);
this.emit("connect", this.mainProcessTarget);
}
return this.mainProcessTarget;

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

@ -20,14 +20,31 @@ add_task(async function() {
const {Target} = client;
ok("Target" in client, "Target domain is available");
const targetCreatedForAlreadyOpenedTab = Target.targetCreated();
const onTargetsCreated = new Promise(resolve => {
let gotTabTarget = false, gotMainTarget = false;
const unsubscribe = Target.targetCreated(event => {
if (event.targetInfo.type == "page" &&
event.targetInfo.url == gBrowser.selectedBrowser.currentURI.spec) {
info("Got the current tab target");
gotTabTarget = true;
}
if (event.targetInfo.type == "browser") {
info("Got the browser target");
gotMainTarget = true;
}
if (gotTabTarget && gotMainTarget) {
unsubscribe();
resolve();
}
});
});
// Instruct the server to fire Target.targetCreated events
Target.setDiscoverTargets({ discover: true });
// Calling `setDiscoverTargets` will dispatch `targetCreated` event for all
// already opened tabs
await targetCreatedForAlreadyOpenedTab;
// already opened tabs and the browser target.
await onTargetsCreated;
// Create a new target so that the test runs against a fresh new tab
const targetCreated = Target.targetCreated();

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

@ -14,14 +14,26 @@ add_task(async function() {
info("Setup Target domain");
const { Target } = client;
const targetCreated = Target.targetCreated();
// Wait for all Target.targetCreated event. One for each tab, plus the one
// for the main process target.
const targetsCreated = new Promise(resolve => {
let targets = 0;
const unsubscribe = Target.targetCreated(event => {
if (++targets >= gBrowser.tabs.length + 1) {
unsubscribe();
resolve();
}
});
});
Target.setDiscoverTargets({ discover: true });
await targetCreated;
await targetsCreated;
info("Create a new tab and wait for the target to be created");
const otherTargetCreated = Target.targetCreated();
const tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URI);
const {targetInfo} = await otherTargetCreated;
is(targetInfo.type, "page");
const onTabClose = BrowserTestUtils.waitForEvent(tab, "TabClose");
const targetDestroyed = Target.targetDestroyed();