head.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/ */
  4. /* import-globals-from shared-head.js */
  5. // shared-head.js handles imports, constants, and utility functions
  6. Services.scriptloader.loadSubScript("chrome://mochitests/content/browser/devtools/client/framework/test/shared-head.js", this);
  7. function toggleAllTools(state) {
  8. for (let [, tool] of gDevTools._tools) {
  9. if (!tool.visibilityswitch) {
  10. continue;
  11. }
  12. if (state) {
  13. Services.prefs.setBoolPref(tool.visibilityswitch, true);
  14. } else {
  15. Services.prefs.clearUserPref(tool.visibilityswitch);
  16. }
  17. }
  18. }
  19. function getChromeActors(callback)
  20. {
  21. let { DebuggerServer } = require("devtools/server/main");
  22. let { DebuggerClient } = require("devtools/shared/client/main");
  23. if (!DebuggerServer.initialized) {
  24. DebuggerServer.init();
  25. DebuggerServer.addBrowserActors();
  26. }
  27. DebuggerServer.allowChromeProcess = true;
  28. let client = new DebuggerClient(DebuggerServer.connectPipe());
  29. client.connect()
  30. .then(() => client.getProcess())
  31. .then(response => {
  32. callback(client, response.form);
  33. });
  34. SimpleTest.registerCleanupFunction(() => {
  35. DebuggerServer.destroy();
  36. });
  37. }
  38. function getSourceActor(aSources, aURL) {
  39. let item = aSources.getItemForAttachment(a => a.source.url === aURL);
  40. return item && item.value;
  41. }
  42. /**
  43. * Open a Scratchpad window.
  44. *
  45. * @return nsIDOMWindow
  46. * The new window object that holds Scratchpad.
  47. */
  48. function* openScratchpadWindow() {
  49. let { promise: p, resolve } = defer();
  50. let win = ScratchpadManager.openScratchpad();
  51. yield once(win, "load");
  52. win.Scratchpad.addObserver({
  53. onReady: function () {
  54. win.Scratchpad.removeObserver(this);
  55. resolve(win);
  56. }
  57. });
  58. return p;
  59. }
  60. /**
  61. * Wait for a content -> chrome message on the message manager (the window
  62. * messagemanager is used).
  63. * @param {String} name The message name
  64. * @return {Promise} A promise that resolves to the response data when the
  65. * message has been received
  66. */
  67. function waitForContentMessage(name) {
  68. info("Expecting message " + name + " from content");
  69. let mm = gBrowser.selectedBrowser.messageManager;
  70. let def = defer();
  71. mm.addMessageListener(name, function onMessage(msg) {
  72. mm.removeMessageListener(name, onMessage);
  73. def.resolve(msg.data);
  74. });
  75. return def.promise;
  76. }
  77. /**
  78. * Send an async message to the frame script (chrome -> content) and wait for a
  79. * response message with the same name (content -> chrome).
  80. * @param {String} name The message name. Should be one of the messages defined
  81. * in doc_frame_script.js
  82. * @param {Object} data Optional data to send along
  83. * @param {Object} objects Optional CPOW objects to send along
  84. * @param {Boolean} expectResponse If set to false, don't wait for a response
  85. * with the same name from the content script. Defaults to true.
  86. * @return {Promise} Resolves to the response data if a response is expected,
  87. * immediately resolves otherwise
  88. */
  89. function executeInContent(name, data = {}, objects = {}, expectResponse = true) {
  90. info("Sending message " + name + " to content");
  91. let mm = gBrowser.selectedBrowser.messageManager;
  92. mm.sendAsyncMessage(name, data, objects);
  93. if (expectResponse) {
  94. return waitForContentMessage(name);
  95. } else {
  96. return promise.resolve();
  97. }
  98. }
  99. /**
  100. * Synthesize a keypress from a <key> element, taking into account
  101. * any modifiers.
  102. * @param {Element} el the <key> element to synthesize
  103. */
  104. function synthesizeKeyElement(el) {
  105. let key = el.getAttribute("key") || el.getAttribute("keycode");
  106. let mod = {};
  107. el.getAttribute("modifiers").split(" ").forEach((m) => mod[m + "Key"] = true);
  108. info(`Synthesizing: key=${key}, mod=${JSON.stringify(mod)}`);
  109. EventUtils.synthesizeKey(key, mod, el.ownerDocument.defaultView);
  110. }
  111. /* Check the toolbox host type and prefs to make sure they match the
  112. * expected values
  113. * @param {Toolbox}
  114. * @param {HostType} hostType
  115. * One of {SIDE, BOTTOM, WINDOW} from Toolbox.HostType
  116. * @param {HostType} Optional previousHostType
  117. * The host that will be switched to when calling switchToPreviousHost
  118. */
  119. function checkHostType(toolbox, hostType, previousHostType) {
  120. is(toolbox.hostType, hostType, "host type is " + hostType);
  121. let pref = Services.prefs.getCharPref("devtools.toolbox.host");
  122. is(pref, hostType, "host pref is " + hostType);
  123. if (previousHostType) {
  124. is(Services.prefs.getCharPref("devtools.toolbox.previousHost"),
  125. previousHostType, "The previous host is correct");
  126. }
  127. }