Skip to content

Commit

Permalink
Backed out changeset f737edc6cd59 (bug 1863022) for causing mochitest…
Browse files Browse the repository at this point in the history
…s failures in browser_protectionsUI.js. CLOSED TREE
  • Loading branch information
Stanca Serban committed Jan 9, 2024
1 parent 8557cd3 commit 3a7783c
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 297 deletions.
199 changes: 5 additions & 194 deletions browser/base/content/browser-siteProtections.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
ChromeUtils.defineESModuleGetters(this, {
ContentBlockingAllowList:
"resource://gre/modules/ContentBlockingAllowList.sys.mjs",
SpecialMessageActions:
"resource://messaging-system/lib/SpecialMessageActions.sys.mjs",
});

XPCOMUtils.defineLazyModuleGetters(this, {
ToolbarPanelHub: "resource://activity-stream/lib/ToolbarPanelHub.jsm",
});

XPCOMUtils.defineLazyServiceGetter(
Expand Down Expand Up @@ -1655,13 +1657,6 @@ var gProtectionsHandler = {
() => this.maybeSetMilestoneCounterText()
);

XPCOMUtils.defineLazyPreferenceGetter(
this,
"protectionsPanelMessageSeen",
"browser.protections_panel.infoMessage.seen",
false
);

for (let blocker of Object.values(this.blockers)) {
if (blocker.init) {
blocker.init();
Expand Down Expand Up @@ -1818,7 +1813,7 @@ var gProtectionsHandler = {

// Insert the info message if needed. This will be shown once and then
// remain collapsed.
this._insertProtectionsPanelInfoMessage(event);
ToolbarPanelHub.insertProtectionPanelMessage(event);

if (!event.target.hasAttribute("toast")) {
Services.telemetry.recordEvent(
Expand Down Expand Up @@ -2698,188 +2693,4 @@ var gProtectionsHandler = {
this._earliestRecordedDate = date;
}
},

_sendUserEventTelemetry(event, value = null, options = {}) {
// Only send telemetry for non private browsing windows
if (!PrivateBrowsingUtils.isWindowPrivate(window)) {
Services.telemetry.recordEvent(
"security.ui.protectionspopup",
event,
"protectionspopup_cfr",
value,
options
);
}
},

/**
* Dispatch the action defined in the message and user telemetry event.
*/
_dispatchUserAction(message) {
let url;
try {
// Set platform specific path variables for SUMO articles
url = Services.urlFormatter.formatURL(message.content.cta_url);
} catch (e) {
console.error(e);
url = message.content.cta_url;
}
SpecialMessageActions.handleAction(
{
type: message.content.cta_type,
data: {
args: url,
where: message.content.cta_where || "tabshifted",
},
},
window.browser
);

this._sendUserEventTelemetry("click", "learn_more_link", {
message: message.id,
});
},

/**
* Attach event listener to dispatch message defined action.
*/
_attachCommandListener(element, message) {
// Add event listener for `mouseup` not to overlap with the
// `mousedown` & `click` events dispatched from PanelMultiView.sys.mjs
// https://searchfox.org/mozilla-central/rev/7531325c8660cfa61bf71725f83501028178cbb9/browser/components/customizableui/PanelMultiView.jsm#1830-1837
element.addEventListener("mouseup", () => {
this._dispatchUserAction(message);
});
element.addEventListener("keyup", e => {
if (e.key === "Enter" || e.key === " ") {
this._dispatchUserAction(message);
}
});
},

/**
* Inserts a message into the Protections Panel. The message is visible once
* and afterwards set in a collapsed state. It can be shown again using the
* info button in the panel header.
*/
_insertProtectionsPanelInfoMessage(event) {
// const PROTECTIONS_PANEL_INFOMSG_PREF =
// "browser.protections_panel.infoMessage.seen";
const message = {
id: "PROTECTIONS_PANEL_1",
content: {
title: { string_id: "cfr-protections-panel-header" },
body: { string_id: "cfr-protections-panel-body" },
link_text: { string_id: "cfr-protections-panel-link-text" },
cta_url: `${Services.urlFormatter.formatURLPref(
"app.support.baseURL"
)}etp-promotions?as=u&utm_source=inproduct`,
cta_type: "OPEN_URL",
},
};

const doc = event.target.ownerDocument;
const container = doc.getElementById("messaging-system-message-container");
const infoButton = doc.getElementById("protections-popup-info-button");
const panelContainer = doc.getElementById("protections-popup");
const toggleMessage = () => {
const learnMoreLink = doc.querySelector(
"#messaging-system-message-container .text-link"
);
if (learnMoreLink) {
container.toggleAttribute("disabled");
infoButton.toggleAttribute("checked");
panelContainer.toggleAttribute("infoMessageShowing");
learnMoreLink.disabled = !learnMoreLink.disabled;
}
// If the message panel is opened, send impression telemetry
if (panelContainer.hasAttribute("infoMessageShowing")) {
this._sendUserEventTelemetry("open", "impression", {
message: message.id,
});
}
};
if (!container.childElementCount) {
const messageEl = this._createHeroElement(doc, message);
container.appendChild(messageEl);
infoButton.addEventListener("click", toggleMessage);
}
// Message is collapsed by default. If it was never shown before we want
// to expand it
if (
!this.protectionsPanelMessageSeen &&
container.hasAttribute("disabled")
) {
toggleMessage(message);
}
// Save state that we displayed the message
if (!this.protectionsPanelMessageSeen) {
Services.prefs.setBoolPref(
"browser.protections_panel.infoMessage.seen",
true
);
}
// Collapse the message after the panel is hidden so we don't get the
// animation when opening the panel
panelContainer.addEventListener(
"popuphidden",
() => {
if (
this.protectionsPanelMessageSeen &&
!container.hasAttribute("disabled")
) {
toggleMessage(message);
}
},
{
once: true,
}
);
},

_createElement(doc, elem, options = {}) {
const node = doc.createElementNS("http://www.w3.org/1999/xhtml", elem);
if (options.classList) {
node.classList.add(options.classList);
}
if (options.content) {
doc.l10n.setAttributes(node, options.content.string_id);
}
return node;
},

_createHeroElement(doc, message) {
const messageEl = this._createElement(doc, "div");
messageEl.setAttribute("id", "protections-popup-message");
messageEl.classList.add("whatsNew-hero-message");
const wrapperEl = this._createElement(doc, "div");
wrapperEl.classList.add("whatsNew-message-body");
messageEl.appendChild(wrapperEl);

wrapperEl.appendChild(
this._createElement(doc, "h2", {
classList: "whatsNew-message-title",
content: message.content.title,
})
);

wrapperEl.appendChild(
this._createElement(doc, "p", { content: message.content.body })
);

if (message.content.link_text) {
let linkEl = this._createElement(doc, "a", {
classList: "text-link",
content: message.content.link_text,
});

linkEl.disabled = true;
wrapperEl.appendChild(linkEl);
this._attachCommandListener(linkEl, message);
} else {
this._attachCommandListener(wrapperEl, message);
}

return messageEl;
},
};
59 changes: 0 additions & 59 deletions browser/base/content/test/protectionsUI/browser_protectionsUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,65 +47,6 @@ async function clickToggle(toggle) {
await changed;
}

add_task(async function testPanelInfoMessage() {
const PROTECTIONS_PANEL_INFOMSG_PREF =
"browser.protections_panel.infoMessage.seen";

let tab = await BrowserTestUtils.openNewForegroundTab(
gBrowser,
TRACKING_PAGE
);
// Set the infomessage pref to ensure the message is displayed every time
Services.prefs.setBoolPref(PROTECTIONS_PANEL_INFOMSG_PREF, false);

await openProtectionsPanel();

await BrowserTestUtils.waitForMutationCondition(
gProtectionsHandler._protectionsPopup,
{ attributes: true, attributeFilter: ["infoMessageShowing"] },
() =>
!gProtectionsHandler._protectionsPopup.hasAttribute("infoMessageShowing")
);

// Test that the info message is displayed when the panel opens
let container = document.getElementById("messaging-system-message-container");
let message = document.getElementById("protections-popup-message");
let learnMoreLink = document.querySelector(
"#messaging-system-message-container .text-link"
);

// Check the visibility of the info message.
ok(
BrowserTestUtils.is_visible(container),
"The message container should exist."
);

ok(BrowserTestUtils.is_visible(message), "The message should be visible.");

ok(BrowserTestUtils.is_visible(learnMoreLink), "The link should be visible.");

// Check telemetry for the info message
let events = Services.telemetry.snapshotEvents(
Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS,
false
).parent;
let messageEvents = events.filter(
e =>
e[1] == "security.ui.protectionspopup" &&
e[2] == "open" &&
e[3] == "protectionspopup_cfr" &&
e[4] == "impression"
);
is(
messageEvents.length,
1,
"recorded telemetry for showing the info message"
);

Services.telemetry.clearEvents();
BrowserTestUtils.removeTab(tab);
});

add_task(async function testToggleSwitch() {
let tab = await BrowserTestUtils.openNewForegroundTab(
gBrowser,
Expand Down
6 changes: 0 additions & 6 deletions browser/components/newtab/karma.mc.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,6 @@ module.exports = function (config) {
functions: 0,
branches: 0,
},
"lib/ToolbarPanelHub.jsm": {
statements: 88,
lines: 88,
functions: 94,
branches: 84,
},
"lib/*.jsm": {
statements: 100,
lines: 100,
Expand Down
69 changes: 69 additions & 0 deletions browser/components/newtab/lib/ToolbarPanelHub.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class _ToolbarPanelHub {
this._hideAppmenuButton = this._hideAppmenuButton.bind(this);
this._showToolbarButton = this._showToolbarButton.bind(this);
this._hideToolbarButton = this._hideToolbarButton.bind(this);
this.insertProtectionPanelMessage =
this.insertProtectionPanelMessage.bind(this);

this.state = {};
this._initialized = false;
Expand Down Expand Up @@ -509,6 +511,73 @@ class _ToolbarPanelHub {
}
}

/**
* Inserts a message into the Protections Panel. The message is visible once
* and afterwards set in a collapsed state. It can be shown again using the
* info button in the panel header.
*/
async insertProtectionPanelMessage(event) {
const win = event.target.ownerGlobal;
this.maybeInsertFTL(win);

const doc = event.target.ownerDocument;
const container = doc.getElementById("messaging-system-message-container");
const infoButton = doc.getElementById("protections-popup-info-button");
const panelContainer = doc.getElementById("protections-popup");
const toggleMessage = () => {
const learnMoreLink = doc.querySelector(
"#messaging-system-message-container .text-link"
);
if (learnMoreLink) {
container.toggleAttribute("disabled");
infoButton.toggleAttribute("checked");
panelContainer.toggleAttribute("infoMessageShowing");
learnMoreLink.disabled = !learnMoreLink.disabled;
}
};
if (!container.childElementCount) {
const message = await this._getMessages({
template: "protections_panel",
triggerId: "protectionsPanelOpen",
});
if (message) {
const messageEl = this._createHeroElement(win, doc, message);
container.appendChild(messageEl);
infoButton.addEventListener("click", toggleMessage);
this.sendUserEventTelemetry(win, "IMPRESSION", message);
}
}
// Message is collapsed by default. If it was never shown before we want
// to expand it
if (
!this.state.protectionPanelMessageSeen &&
container.hasAttribute("disabled")
) {
toggleMessage();
}
// Save state that we displayed the message
if (!this.state.protectionPanelMessageSeen) {
Services.prefs.setBoolPref(PROTECTIONS_PANEL_INFOMSG_PREF, true);
this.state.protectionPanelMessageSeen = true;
}
// Collapse the message after the panel is hidden so we don't get the
// animation when opening the panel
panelContainer.addEventListener(
"popuphidden",
() => {
if (
this.state.protectionPanelMessageSeen &&
!container.hasAttribute("disabled")
) {
toggleMessage();
}
},
{
once: true,
}
);
}

/**
* @param {object} [browser] MessageChannel target argument as a response to a
* user action. No message is shown if undefined.
Expand Down
Loading

0 comments on commit 3a7783c

Please sign in to comment.