Files
sousa-gecko/devtools/client/webconsole/test/browser/browser_jsterm_copy_command.js
T
Nicolas Chevobbe 6bd56b5c52 Bug 1946678 - [devtools] Waive Xrays in console copy command. r=devtools-reviewers,jdescottes.
We need to do this so we can iterate accessor properties.
This fixes copying objects like typed arrays.
A test case is added.

Depends on D238212

Differential Revision: https://phabricator.services.mozilla.com/D238764
2025-02-19 14:09:40 +00:00

82 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
// Tests that the `copy` console helper works as intended.
"use strict";
const text =
"Lorem ipsum dolor sit amet, consectetur adipisicing " +
"elit, sed do eiusmod tempor incididunt ut labore et dolore magna " +
"aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco " +
"laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure " +
"dolor in reprehenderit in voluptate velit esse cillum dolore eu " +
"fugiat nulla pariatur. Excepteur sint occaecat cupidatat non " +
"proident, sunt in culpa qui officia deserunt mollit anim id est laborum." +
new Date();
const id = "select-me";
const TEST_URI = `data:text/html;charset=utf-8,<!DOCTYPE html>
<body>
<div>
<h1>Testing copy command</h1>
<p>This is some example text</p>
<p id="${id}">${text}</p>
</div>
<div><p></p></div>
</body>`;
add_task(async function () {
const hud = await openNewTabAndConsole(TEST_URI);
const random = Math.random();
const string = "Text: " + random;
const obj = { a: 1, b: "foo", c: random };
await testCopy(hud, random, random.toString());
await testCopy(hud, JSON.stringify(string), string);
await testCopy(hud, obj.toSource(), JSON.stringify(obj, null, " "));
await testCopy(
hud,
`{ typedArray: new Float32Array([1, 2, 3]) }`,
`{
"typedArray": {
"0": 1,
"1": 2,
"2": 3
}
}`
);
const outerHTML = await SpecialPowers.spawn(
gBrowser.selectedBrowser,
[id],
function (elementId) {
return content.document.getElementById(elementId).outerHTML;
}
);
await testCopy(hud, `$("#${id}")`, outerHTML);
});
add_task(async function () {
const hud = await openNewTabAndConsole(TEST_URI);
await executeAndWaitForErrorMessage(
hud,
"var a = {}; a.b = a; copy(a);",
"`copy` command failed, object can’t be stringified: TypeError: cyclic object value"
);
});
function testCopy(hud, stringToCopy, expectedResult) {
return waitForClipboardPromise(async () => {
info(`Attempting to copy: "${stringToCopy}"`);
const command = `copy(${stringToCopy})`;
info(`Executing command: "${command}"`);
await executeAndWaitForMessageByType(
hud,
command,
"String was copied to clipboard",
".console-api"
);
}, expectedResult);
}