browser_addons_debug_webextension.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. "use strict";
  4. // Avoid test timeouts that can occur while waiting for the "addon-console-works" message.
  5. requestLongerTimeout(2);
  6. const ADDON_ID = "test-devtools-webextension@mozilla.org";
  7. const ADDON_NAME = "test-devtools-webextension";
  8. const ADDON_MANIFEST_PATH = "addons/test-devtools-webextension/manifest.json";
  9. const {
  10. BrowserToolboxProcess
  11. } = Cu.import("resource://devtools/client/framework/ToolboxProcess.jsm", {});
  12. /**
  13. * This test file ensures that the webextension addon developer toolbox:
  14. * - when the debug button is clicked on a webextension, the opened toolbox
  15. * has a working webconsole with the background page as default target;
  16. */
  17. add_task(function* testWebExtensionsToolboxWebConsole() {
  18. let {
  19. tab, document, debugBtn,
  20. } = yield setupTestAboutDebuggingWebExtension(ADDON_NAME, ADDON_MANIFEST_PATH);
  21. // Wait for a notification sent by a script evaluated the test addon via
  22. // the web console.
  23. let onCustomMessage = new Promise(done => {
  24. Services.obs.addObserver(function listener(message, topic) {
  25. let apiMessage = message.wrappedJSObject;
  26. if (!apiMessage.originAttributes ||
  27. apiMessage.originAttributes.addonId != ADDON_ID) {
  28. return;
  29. }
  30. Services.obs.removeObserver(listener, "console-api-log-event");
  31. done(apiMessage.arguments);
  32. }, "console-api-log-event", false);
  33. });
  34. // Be careful, this JS function is going to be executed in the addon toolbox,
  35. // which lives in another process. So do not try to use any scope variable!
  36. let env = Cc["@mozilla.org/process/environment;1"]
  37. .getService(Ci.nsIEnvironment);
  38. let testScript = function () {
  39. /* eslint-disable no-undef */
  40. toolbox.selectTool("webconsole")
  41. .then(console => {
  42. let { jsterm } = console.hud;
  43. return jsterm.execute("myWebExtensionAddonFunction()");
  44. })
  45. .then(() => toolbox.destroy());
  46. /* eslint-enable no-undef */
  47. };
  48. env.set("MOZ_TOOLBOX_TEST_SCRIPT", "new " + testScript);
  49. registerCleanupFunction(() => {
  50. env.set("MOZ_TOOLBOX_TEST_SCRIPT", "");
  51. });
  52. let onToolboxClose = BrowserToolboxProcess.once("close");
  53. debugBtn.click();
  54. let args = yield onCustomMessage;
  55. ok(true, "Received console message from the background page function as expected");
  56. is(args[0], "Background page function called", "Got the expected console message");
  57. is(args[1] && args[1].name, ADDON_NAME,
  58. "Got the expected manifest from WebExtension API");
  59. yield onToolboxClose;
  60. ok(true, "Addon toolbox closed");
  61. yield uninstallAddon({document, id: ADDON_ID, name: ADDON_NAME});
  62. yield closeAboutDebugging(tab);
  63. });