Bug 1570631 - Part 5: test cases; r=k88hudson

Differential Revision: https://phabricator.services.mozilla.com/D43753

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Liang-Heng Chen 2019-09-11 09:16:11 +00:00
parent 1855d174bc
commit 11d0632c82
5 changed files with 181 additions and 2 deletions

View File

@ -13,16 +13,22 @@ const createDummyRecommendation = ({
category,
heading_text,
layout,
skip_address_bar_notifier,
}) => ({
content: {
layout: layout || "addon_recommendation",
category,
anchor_id: "page-action-buttons",
skip_address_bar_notifier,
notification_text: "Mochitest",
heading_text: heading_text || "Mochitest",
info_icon: {
label: { attributes: { tooltiptext: "Why am I seeing this" } },
sumo_path: "extensionrecommendations",
},
icon: "foo",
icon_dark_theme: "bar",
learn_more: "extensionrecommendations",
addon: {
id: "addon-id",
title: "Addon name",
@ -107,6 +113,19 @@ function checkCFRAddonsElements(notification) {
);
}
function checkCFRSocialTrackingProtection(notification) {
Assert.ok(notification.hidden === false, "Panel should be visible");
Assert.ok(
notification.getAttribute("data-notification-category") ===
"icon_and_message",
"Panel have corret data attribute"
);
Assert.ok(
notification.querySelector("#cfr-notification-footer-learn-more-link"),
"Panel should have learn more link"
);
}
function clearNotifications() {
for (let notification of PopupNotifications._currentNotifications) {
notification.remove();
@ -128,6 +147,8 @@ function trigger_cfr_panel(
heading_text,
category = "cfrAddons",
layout,
skip_address_bar_notifier = false,
use_single_secondary_button = false,
} = {}
) {
// a fake action type will result in the action being ignored
@ -136,10 +157,16 @@ function trigger_cfr_panel(
category,
heading_text,
layout,
skip_address_bar_notifier,
});
if (category !== "cfrAddons") {
delete recommendation.content.addon;
}
if (use_single_secondary_button) {
recommendation.content.buttons.secondary = [
recommendation.content.buttons.secondary[0],
];
}
clearNotifications();
@ -400,6 +427,49 @@ add_task(async function test_cfr_pin_tab_notification_show() {
);
});
add_task(
async function test_cfr_social_tracking_protection_notification_show() {
// addRecommendation checks that scheme starts with http and host matches
let browser = gBrowser.selectedBrowser;
await BrowserTestUtils.loadURI(browser, "http://example.com/");
await BrowserTestUtils.browserLoaded(browser, false, "http://example.com/");
const showPanel = BrowserTestUtils.waitForEvent(
PopupNotifications.panel,
"popupshown"
);
const response = await trigger_cfr_panel(browser, "example.com", {
action: { type: "OPEN_PROTECTION_PANEL" },
category: "cfrFeatures",
layout: "icon_and_message",
skip_address_bar_notifier: true,
use_single_secondary_button: true,
});
Assert.ok(
response,
"Should return true if addRecommendation checks were successful"
);
await showPanel;
const notification = document.getElementById(
"contextual-feature-recommendation-notification"
);
checkCFRSocialTrackingProtection(notification);
// Check there is a primary button and click it. It will trigger the callback.
Assert.ok(notification.button);
let hidePanel = BrowserTestUtils.waitForEvent(
PopupNotifications.panel,
"popuphidden"
);
document
.getElementById("contextual-feature-recommendation-notification")
.button.click();
await hidePanel;
}
);
add_task(async function test_cfr_features_and_addon_show() {
// addRecommendation checks that scheme starts with http and host matches
let browser = gBrowser.selectedBrowser;

View File

@ -141,3 +141,97 @@ add_task(async function check_newSavedLogin_listener() {
}
);
});
add_task(async function check_trackingProtection_listener() {
const TEST_URL =
"https://example.com/browser/browser/components/newtab/test/browser/red_page.html";
const contentBlockingEvent = 1234;
let observerEvent = 0;
let pageLoadSum = 0;
const triggerHandler = (target, trigger) => {
const {
id,
param: { host },
context: { pageLoad },
} = trigger;
is(id, "trackingProtection", "should match event name");
is(host, TEST_URL, "should match test URL");
observerEvent += 1;
pageLoadSum += pageLoad;
};
const trackingProtectionListener = ASRouterTriggerListeners.get(
"trackingProtection"
);
// Previously initialized by the Router
trackingProtectionListener.uninit();
// Initialise listener
await trackingProtectionListener.init(triggerHandler, [contentBlockingEvent]);
await BrowserTestUtils.withNewTab(
TEST_URL,
async function triggerTrackingProtection(browser) {
Services.obs.notifyObservers(
{
wrappedJSObject: {
browser,
host: TEST_URL,
event: contentBlockingEvent + 1,
},
},
"SiteProtection:ContentBlockingEvent"
);
}
);
is(observerEvent, 0, "shouldn't receive unrelated observer notification");
is(pageLoadSum, 0, "shouldn't receive unrelated observer notification");
await BrowserTestUtils.withNewTab(
TEST_URL,
async function triggerTrackingProtection(browser) {
Services.obs.notifyObservers(
{
wrappedJSObject: {
browser,
host: TEST_URL,
event: contentBlockingEvent,
},
},
"SiteProtection:ContentBlockingEvent"
);
await BrowserTestUtils.waitForCondition(
() => observerEvent !== 0,
"Wait for the observer notification to run"
);
is(observerEvent, 1, "should receive observer notification");
is(pageLoadSum, 2, "should receive observer notification");
}
);
// Uninitialise listener
trackingProtectionListener.uninit();
await BrowserTestUtils.withNewTab(
TEST_URL,
async function triggerTrackingProtectionAfterUninit(browser) {
Services.obs.notifyObservers(
{
wrappedJSObject: {
browser,
host: TEST_URL,
event: contentBlockingEvent,
},
},
"SiteProtection:ContentBlockingEvent"
);
await new Promise(resolve => executeSoon(resolve));
is(observerEvent, 1, "shouldn't receive obs. notification after uninit");
is(pageLoadSum, 2, "shouldn't receive obs. notification after uninit");
}
);
});

View File

@ -1971,6 +1971,18 @@ describe("ASRouter", () => {
});
});
describe("#onMessage: OPEN_PROTECTION_PANEL", () => {
it("should open protection panel", async () => {
const msg = fakeExecuteUserAction({ type: "OPEN_PROTECTION_PANEL" });
let { gProtectionsHandler } = msg.target.browser.ownerGlobal;
await Router.onMessage(msg);
assert.calledOnce(gProtectionsHandler.showProtectionsPopup);
assert.calledWithExactly(gProtectionsHandler.showProtectionsPopup, {});
});
});
describe("#dispatch(action, target)", () => {
it("should an action and target to onMessage", async () => {
// use the IMPRESSION action to make sure actions are actually getting processed

View File

@ -11,8 +11,8 @@ const REGULAR_IDS = [
];
describe("CFRMessageProvider", () => {
it("should have a total of 5 messages", () => {
assert.lengthOf(messages, 5);
it("should have a total of 8 messages", () => {
assert.lengthOf(messages, 8);
});
it("should have one message each for the three regular addons", () => {
for (const id of REGULAR_IDS) {

View File

@ -149,6 +149,9 @@ export class FakeRemotePageManager {
ConfirmationHint: {
show: sinon.stub(),
},
gProtectionsHandler: {
showProtectionsPopup: sinon.stub(),
},
},
};
this.portID = "6000:2";