browser_commands_registration.js (2545B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 // Test for Web Console commands registration. 7 8 add_task(async function () { 9 const tab = await addTab("data:text/html,<div id=quack></div>"); 10 11 const commands = await CommandsFactory.forTab(tab); 12 await commands.targetCommand.startListening(); 13 14 // Fetch WebConsoleCommandsManager so that it is available for next Content Tasks 15 await ContentTask.spawn(gBrowser.selectedBrowser, null, function () { 16 const { require } = ChromeUtils.importESModule( 17 "resource://devtools/shared/loader/Loader.sys.mjs" 18 ); 19 const { 20 WebConsoleCommandsManager, 21 } = require("resource://devtools/server/actors/webconsole/commands/manager.js"); 22 23 // Bind the symbol on this in order to make it available for next tasks 24 this.WebConsoleCommandsManager = WebConsoleCommandsManager; 25 }); 26 27 await registerNewCommand(commands); 28 await registerAccessor(commands); 29 }); 30 31 async function evaluateJSAndCheckResult(commands, input, expected) { 32 const response = await commands.scriptCommand.execute(input); 33 checkObject(response, expected); 34 } 35 36 async function registerNewCommand(commands) { 37 await ContentTask.spawn(gBrowser.selectedBrowser, null, function () { 38 this.WebConsoleCommandsManager.register({ 39 name: "setFoo", 40 isSideEffectFree: false, 41 command(owner, value) { 42 owner.window.foo = value; 43 return "ok"; 44 }, 45 }); 46 }); 47 48 const command = "setFoo('bar')"; 49 await evaluateJSAndCheckResult(commands, command, { 50 input: command, 51 result: "ok", 52 }); 53 54 await ContentTask.spawn(gBrowser.selectedBrowser, null, function () { 55 is(content.top.foo, "bar", "top.foo should equal to 'bar'"); 56 }); 57 } 58 59 async function registerAccessor(commands) { 60 await ContentTask.spawn(gBrowser.selectedBrowser, null, function () { 61 this.WebConsoleCommandsManager.register({ 62 name: "$foo", 63 isSideEffectFree: true, 64 command: { 65 get(owner) { 66 const foo = owner.window.document.getElementById("quack"); 67 return owner.makeDebuggeeValue(foo); 68 }, 69 }, 70 }); 71 }); 72 73 const command = "$foo.textContent = '>o_/'"; 74 await evaluateJSAndCheckResult(commands, command, { 75 input: command, 76 result: ">o_/", 77 }); 78 79 await ContentTask.spawn(gBrowser.selectedBrowser, null, function () { 80 is( 81 content.document.getElementById("quack").textContent, 82 ">o_/", 83 '#foo textContent should equal to ">o_/"' 84 ); 85 }); 86 }