From 4e87886f42787b252662c374e4e8fddfad15ee30 Mon Sep 17 00:00:00 2001 From: William Durand Date: Thu, 23 Apr 2020 14:14:49 +0000 Subject: [PATCH] Bug 1631403 - Add test case to cover RDP APIs that 'web-ext run' depends on. r=rpl,ochameau,robwu Differential Revision: https://phabricator.services.mozilla.com/D71892 --- .../server/tests/xpcshell/test_webext_apis.js | 94 +++++++++++++++++++ devtools/server/tests/xpcshell/xpcshell.ini | 1 + 2 files changed, 95 insertions(+) create mode 100644 devtools/server/tests/xpcshell/test_webext_apis.js diff --git a/devtools/server/tests/xpcshell/test_webext_apis.js b/devtools/server/tests/xpcshell/test_webext_apis.js new file mode 100644 index 000000000000..5d747b701753 --- /dev/null +++ b/devtools/server/tests/xpcshell/test_webext_apis.js @@ -0,0 +1,94 @@ +/* Any copyright is dedicated to the Public Domain. +http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +// The `AddonsManager` test helper can only be called once per test script. +// This `setup` task will run first. +add_task(async function setup() { + Services.prefs.setBoolPref("extensions.blocklist.enabled", false); + await startupAddonsManager(); +}); + +// Basic request wrapper that sends a request and resolves on the next packet. +// Will only work for very basic scenarios, without events emitted on the server +// etc... +async function sendRequest(transport, request) { + return new Promise(resolve => { + transport.hooks = { + onPacket: packet => { + dump(`received packet: ${JSON.stringify(packet)}\n`); + // Let's resolve only when we get a packet that is related to our + // request. It is needed because some methods do not return the correct + // response right away. This is the case of the `reload` method, which + // receives a `addonListChanged` message first and then a `reload` + // message. + if (packet.from === request.to) { + resolve(packet); + } + }, + onClosed: () => {}, + }; + transport.send(request); + }); +} + +// If this test case fails, please reach out to webext peers because +// https://github.com/mozilla/web-ext relies on the APIs tested here. +add_task(async function test_webext_run_apis() { + DevToolsServer.init(); + DevToolsServer.registerAllActors(); + + const transport = DevToolsServer.connectPipe(); + + // After calling connectPipe, the root actor will be created on the server + // and a packet will be emitted after a tick. Wait for the initial packet. + await new Promise(resolve => { + transport.hooks = { onPacket: resolve }; + }); + + const getRootResponse = await sendRequest(transport, { + to: "root", + type: "getRoot", + }); + + ok(getRootResponse, "received a response after calling RootActor::getRoot"); + ok(getRootResponse.addonsActor, "getRoot returned an addonsActor id"); + + // installTemporaryAddon + const addonId = "test-addons-actor@mozilla.org"; + const addonPath = getFilePath("addons/web-extension", false, true); + const promiseStarted = AddonTestUtils.promiseWebExtensionStartup(addonId); + const { addon } = await sendRequest(transport, { + to: getRootResponse.addonsActor, + type: "installTemporaryAddon", + addonPath, + }); + await promiseStarted; + + ok(addon, "addonsActor allows to install a temporary add-on"); + equal(addon.id, addonId, "temporary add-on is the expected one"); + equal(addon.actor, false, "temporary add-on does not have an actor"); + + // listAddons + const { addons } = await sendRequest(transport, { + to: "root", + type: "listAddons", + }); + ok(Array.isArray(addons), "listAddons() returns a list of add-ons"); + + const installedAddon = addons[0]; + equal(installedAddon.id, addonId, "installed add-on is the expected one"); + ok(installedAddon.actor, "returned add-on has an actor"); + + // reload + const promiseReloaded = AddonTestUtils.promiseAddonEvent("onInstalled"); + const promiseRestarted = AddonTestUtils.promiseWebExtensionStartup(addonId); + await sendRequest(transport, { + to: installedAddon.actor, + type: "reload", + }); + await Promise.all([promiseReloaded, promiseRestarted]); + + transport.close(); +}); diff --git a/devtools/server/tests/xpcshell/xpcshell.ini b/devtools/server/tests/xpcshell/xpcshell.ini index 324a6e833a5a..16d5a683c1ba 100644 --- a/devtools/server/tests/xpcshell/xpcshell.ini +++ b/devtools/server/tests/xpcshell/xpcshell.ini @@ -236,3 +236,4 @@ skip-if = true # breakpoint sliding is not supported bug 1525685 [test_safe-getter.js] [test_shapes_highlighter_helpers.js] [test_symbolactor.js] +[test_webext_apis.js]