Bug 1958081 - [devtools] Cover JS Tracer stringification with the new JS Objects test framework. r=devtools-reviewers,nchevobbe

Differential Revision: https://phabricator.services.mozilla.com/D245075
This commit is contained in:
Alexandre Poirot
2025-05-13 12:30:38 +00:00
committed by apoirot@mozilla.com
parent a5749b35e9
commit f9642bda04
4 changed files with 379 additions and 2 deletions

View File

@@ -0,0 +1,64 @@
"use strict";
const { JSObjectsTestUtils } = ChromeUtils.importESModule(
"resource://testing-common/JSObjectsTestUtils.sys.mjs"
);
JSObjectsTestUtils.init(this);
// Note that XPCShellContentUtils will already be initialized by JSObjectsTestUtils
const { XPCShellContentUtils } = ChromeUtils.importESModule(
"resource://testing-common/XPCShellContentUtils.sys.mjs"
);
const EXPECTED_VALUES_FILE = "test_javascript_logging.snapshot.mjs";
add_task(async function () {
// Create a content page in order to better simulate a real world page
const contentPage = await XPCShellContentUtils.loadContentPage(
"http://example.com/"
);
await JSObjectsTestUtils.runTest(EXPECTED_VALUES_FILE, async function (arg) {
// Because the test page runs in a content process, we have to execute most of the test logic via `spawn`
return contentPage.spawn([arg], async ({ context, expression }) => {
const { CONTEXTS } = ChromeUtils.importESModule(
"resource://testing-common/AllJavascriptTypes.mjs"
);
const { JSTracer } = ChromeUtils.importESModule(
"resource://devtools/server/tracer/tracer.sys.mjs"
);
const systemPrincipal =
Services.scriptSecurityManager.getSystemPrincipal();
const chromeSandbox = Cu.Sandbox(systemPrincipal);
let ref;
try {
if (context == CONTEXTS.CHROME) {
ref = Cu.evalInSandbox(
expression,
chromeSandbox,
null,
"test sandbox"
);
} else {
ref = this.content.eval(expression);
}
} catch (e) {
ref = e;
}
const str = JSTracer.objectToString(ref);
// Silence any async rejection
if (ref instanceof this.content.Promise) {
// eslint-disable-next-line max-nested-callbacks
ref.catch(function () {});
}
return str;
});
});
info("Close content page");
await contentPage.close();
});

File diff suppressed because one or more lines are too long

View File

@@ -3,4 +3,8 @@ tags = "devtools"
firefox-appdir = "browser"
run-if = ["os != 'android'"]
["test_javascript_logging.js"]
support-files = ["test_javascript_logging.snapshot.mjs"]
prefs = ["dom.security.https_first=false"] # JS HttpServer doesn't support https
["test_webconsole_l10n.js"]

View File

@@ -933,7 +933,7 @@ function objectToString(obj) {
} else if (typeof obj === "function") {
return `function ${obj.name || "anonymous"}()`;
}
return obj;
return primitiveToString(obj);
}
function primitiveToString(value) {
@@ -952,7 +952,7 @@ function primitiveToString(value) {
}
// For all other types/cases, rely on native convertion to string
return value;
return String(value);
}
/**
@@ -1112,4 +1112,5 @@ export const JSTracer = {
removeTracingListener,
NEXT_INTERACTION_MESSAGE,
DOM_MUTATIONS,
objectToString,
};