Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / devtools / server / tests / chrome / inspector-helpers.js
blob0b7edd80356924c07231bbe2bf091602c925dda5
1 /* exported attachURL, promiseDone,
2 promiseOnce,
3 addTest, addAsyncTest,
4 runNextTest, _documentWalker */
5 "use strict";
7 const { require } = ChromeUtils.importESModule(
8 "resource://devtools/shared/loader/Loader.sys.mjs"
9 );
10 const {
11 CommandsFactory,
12 } = require("resource://devtools/shared/commands/commands-factory.js");
13 const {
14 DevToolsServer,
15 } = require("resource://devtools/server/devtools-server.js");
16 const { BrowserTestUtils } = ChromeUtils.importESModule(
17 "resource://testing-common/BrowserTestUtils.sys.mjs"
19 const {
20 DocumentWalker: _documentWalker,
21 } = require("resource://devtools/server/actors/inspector/document-walker.js");
23 // Always log packets when running tests.
24 Services.prefs.setBoolPref("devtools.debugger.log", true);
25 SimpleTest.registerCleanupFunction(function () {
26 Services.prefs.clearUserPref("devtools.debugger.log");
27 });
29 if (!DevToolsServer.initialized) {
30 DevToolsServer.init();
31 DevToolsServer.registerAllActors();
32 SimpleTest.registerCleanupFunction(function () {
33 DevToolsServer.destroy();
34 });
37 var gAttachCleanups = [];
39 SimpleTest.registerCleanupFunction(function () {
40 for (const cleanup of gAttachCleanups) {
41 cleanup();
43 });
45 /**
46 * Open a tab, load the url, wait for it to signal its readiness,
47 * connect to this tab via DevTools protocol and return.
49 * Returns an object with a few helpful attributes:
50 * - commands {Object}: The commands object defined by modules from devtools/shared/commands
51 * - target {TargetFront}: The current top-level target front.
52 * - doc {HtmlDocument}: the tab's document that got opened
54 async function attachURL(url) {
55 // Get the current browser window
56 const gBrowser =
57 Services.wm.getMostRecentWindow("navigator:browser").gBrowser;
59 // open the url in a new tab, save a reference to the new inner window global object
60 // and wait for it to load. The tests rely on this window object to send a "ready"
61 // event to its opener (the test page). This window reference is used within
62 // the test tab, to reference the webpage being tested against, which is in another
63 // tab.
64 const windowOpened = BrowserTestUtils.waitForNewTab(gBrowser, url);
65 const win = window.open(url, "_blank");
66 await windowOpened;
68 const commands = await CommandsFactory.forTab(gBrowser.selectedTab);
69 await commands.targetCommand.startListening();
71 const cleanup = async function () {
72 await commands.destroy();
73 if (win) {
74 win.close();
78 gAttachCleanups.push(cleanup);
79 return {
80 commands,
81 target: commands.targetCommand.targetFront,
82 doc: win.document,
86 function promiseOnce(target, event) {
87 return new Promise(resolve => {
88 target.on(event, (...args) => {
89 if (args.length === 1) {
90 resolve(args[0]);
91 } else {
92 resolve(args);
94 });
95 });
98 function promiseDone(currentPromise) {
99 currentPromise.catch(err => {
100 ok(false, "Promise failed: " + err);
101 if (err.stack) {
102 dump(err.stack);
104 SimpleTest.finish();
108 var _tests = [];
109 function addTest(test) {
110 _tests.push(test);
113 function addAsyncTest(generator) {
114 _tests.push(() => generator().catch(ok.bind(null, false)));
117 function runNextTest() {
118 if (!_tests.length) {
119 SimpleTest.finish();
120 return;
122 const fn = _tests.shift();
123 try {
124 fn();
125 } catch (ex) {
126 info(
127 "Test function " +
128 (fn.name ? "'" + fn.name + "' " : "") +
129 "threw an exception: " +