Backed out changeset 118838e56e20 (bug 1961230) for causing failures on browser_asrouter_infobar.js CLOSED TREE

This commit is contained in:
pstanciu
2025-04-22 16:43:03 +03:00
parent bd71aa837f
commit c17bdcd640
2 changed files with 8 additions and 186 deletions

View File

@@ -7,8 +7,6 @@ const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.sys.mjs",
RemoteL10n: "resource:///modules/asrouter/RemoteL10n.sys.mjs",
SpecialMessageActions:
"resource://messaging-system/lib/SpecialMessageActions.sys.mjs",
});
class InfoBarNotification {
@@ -42,7 +40,7 @@ class InfoBarNotification {
this.notification = await notificationContainer.appendNotification(
this.message.id,
{
label: this.formatMessageConfig(doc, browser, content.text),
label: this.formatMessageConfig(doc, content.text),
image: content.icon || "chrome://branding/content/icon64.png",
priority,
eventCallback: this.infobarCallback,
@@ -55,53 +53,14 @@ class InfoBarNotification {
this.addImpression();
}
formatMessageConfig(doc, browser, content) {
const frag = doc.createDocumentFragment();
const parts = Array.isArray(content) ? content : [content];
for (const part of parts) {
let node;
if (typeof part === "string") {
node = doc.createTextNode(part);
// Handle embedded link
} else if (part.href) {
const a = doc.createElement("a");
a.href = part.href;
a.addEventListener("click", e => {
e.preventDefault();
lazy.SpecialMessageActions.handleAction(
{ type: "OPEN_URL", data: { args: a.href, where: part.where } },
browser
);
});
formatMessageConfig(doc, content) {
let docFragment = doc.createDocumentFragment();
// notificationbox will only `appendChild` for documentFragments
docFragment.appendChild(
lazy.RemoteL10n.createElement(doc, "span", { content })
);
if (part.string_id) {
const l10n = lazy.RemoteL10n.createElement(doc, "span", {
content: {
string_id: part.string_id,
...(part.args && { args: part.args }),
},
});
a.appendChild(l10n);
} else {
a.textContent = part.raw || "";
}
node = a;
} else if (part.string_id) {
node = lazy.RemoteL10n.createElement(doc, "span", {
content: {
string_id: part.string_id,
...(part.args && { args: part.args }),
},
});
} else {
const text = part.raw !== null ? part.raw : String(part);
node = doc.createTextNode(text);
}
frag.appendChild(node);
}
return frag;
return docFragment;
}
formatButtonConfig(button) {

View File

@@ -252,140 +252,3 @@ add_task(
);
}
);
function getMeaningfulNodes(infobar) {
return [...infobar.notification.messageText.childNodes].filter(
n =>
n.nodeType === Node.ELEMENT_NODE ||
(n.nodeType === Node.TEXT_NODE && n.textContent.trim())
);
}
async function showInfobar(text, box, browser) {
let msg = {
id: "Test Infobar",
content: {
text,
type: "global",
priority: box.PRIORITY_INFO_LOW,
buttons: [{ label: "Close", action: { type: "CANCEL" } }],
},
};
let stub = sinon.stub();
let infobar = await InfoBar.showInfoBarMessage(browser, msg, stub);
return { infobar, stub };
}
add_task(async function test_formatMessageConfig_single_string() {
const win = BrowserWindowTracker.getTopWindow();
const browser = win.gBrowser.selectedBrowser;
const box = win.gNotificationBox;
let { infobar } = await showInfobar("Just a plain string", box, browser);
const nodes = getMeaningfulNodes(infobar);
Assert.equal(nodes.length, 1, "One meaningful node for single string");
Assert.equal(nodes[0].nodeType, Node.TEXT_NODE, "That node is a text node");
Assert.equal(nodes[0].textContent.trim(), "Just a plain string");
infobar.notification.closeButton.click();
await BrowserTestUtils.waitForCondition(() => !InfoBar._activeInfobar);
});
add_task(async function test_formatMessageConfig_single_l10n() {
const win = BrowserWindowTracker.getTopWindow();
const browser = win.gBrowser.selectedBrowser;
const box = win.gNotificationBox;
let l10nObj = { string_id: "data-reporting-notification-message", args: {} };
let { infobar } = await showInfobar(l10nObj, box, browser);
const nodes = getMeaningfulNodes(infobar);
Assert.equal(nodes.length, 1, "One meaningful node for single l10n object");
Assert.equal(nodes[0].nodeType, Node.ELEMENT_NODE, "The node is an element");
Assert.equal(
nodes[0].getAttribute("fluent-remote-id"),
"data-reporting-notification-message",
"Fluent ID on remote-text matches"
);
infobar.notification.closeButton.click();
await BrowserTestUtils.waitForCondition(() => !InfoBar._activeInfobar);
});
add_task(async function test_formatMessageConfig_array() {
const win = BrowserWindowTracker.getTopWindow();
const browser = win.gBrowser.selectedBrowser;
const box = win.gNotificationBox;
let parts = [
"A",
{ raw: "B" },
{ string_id: "launch-on-login-infobar-message" },
{ href: "https://x.test/", raw: "LINK" },
"Z",
];
let { infobar } = await showInfobar(parts, box, browser);
const nodes = getMeaningfulNodes(infobar);
Assert.equal(nodes.length, parts.length, "One node per array part");
Assert.equal(nodes[0].textContent, "A", "Plain text");
Assert.equal(nodes[1].textContent, "B", "Raw text");
Assert.equal(nodes[2].localName, "remote-text", "L10n element");
Assert.equal(
nodes[2].getAttribute("fluent-remote-id"),
"launch-on-login-infobar-message",
"Fluent ID"
);
const [, , , a] = nodes;
Assert.equal(a.localName, "a", "It's a link");
Assert.equal(a.getAttribute("href"), "https://x.test/", "hred preserved");
Assert.equal(a.textContent, "LINK", "Link text");
Assert.equal(nodes[4].textContent, "Z", "Trailing text");
infobar.notification.closeButton.click();
await BrowserTestUtils.waitForCondition(() => !InfoBar._activeInfobar);
});
add_task(async function test_specialMessageAction_onLinkClick() {
const win = BrowserWindowTracker.getTopWindow();
const browser = win.gBrowser.selectedBrowser;
const box = win.gNotificationBox;
const { SpecialMessageActions } = ChromeUtils.importESModule(
"resource://messaging-system/lib/SpecialMessageActions.sys.mjs"
);
let handleStub = sinon.stub(SpecialMessageActions, "handleAction");
const parts = [
"Click ",
{ raw: "here", href: "https://example.com/foo", where: "tab" },
" to continue",
];
let { infobar } = await showInfobar(parts, box, browser);
let link = infobar.notification.messageText.querySelector("a[href]");
Assert.ok(link, "Found the link");
EventUtils.synthesizeMouseAtCenter(link, {}, browser.ownerGlobal);
Assert.equal(handleStub.callCount, 1, "handleAction was invoked once");
let [actionArg, browserArg] = handleStub.firstCall.args;
Assert.deepEqual(
actionArg,
{
type: "OPEN_URL",
data: { args: "https://example.com/foo", where: "tab" },
},
"Passed correct action to handleAction"
);
Assert.equal(
browserArg,
browser,
"Passed the selectedBrowser to handleAction"
);
infobar.notification.closeButton.click();
await BrowserTestUtils.waitForCondition(() => !InfoBar._activeInfobar);
handleStub.restore();
});