Revision control

Copy as Markdown

Other Tools

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const { MockRegistrar } = ChromeUtils.importESModule(
);
const InAppNotifications = ChromeUtils.importESModule(
).InAppNotifications;
add_setup(() => {
/** @implements {nsIExternalProtocolService} */
const mockExternalProtocolService = {
QueryInterface: ChromeUtils.generateQI(["nsIExternalProtocolService"]),
externalProtocolHandlerExists() {},
isExposedProtocol() {},
loadURI() {},
};
const mockExternalProtocolServiceCID = MockRegistrar.register(
"@mozilla.org/uriloader/external-protocol-service;1",
mockExternalProtocolService
);
registerCleanupFunction(() => {
InAppNotifications.updateNotifications([]);
MockRegistrar.unregister(mockExternalProtocolServiceCID);
});
});
add_task(function test_maintainsFocusWhenOpened() {
const searchBar = document.querySelector("global-search-bar");
searchBar.focus();
InAppNotifications.updateNotifications([
{
id: "testNotification" + Date.now(),
title: "Test notification",
description: "Long prose text",
CTA: "Click me!",
severity: 1,
type: "donation",
start_at: new Date(Date.now() - 100000).toISOString(),
end_at: new Date(Date.now() + 9999999999).toISOString(),
targeting: {},
},
]);
Assert.equal(
document.querySelectorAll("in-app-notification").length,
1,
"Notification Shown"
);
Assert.equal(
searchBar,
document.activeElement,
"the search bar still has focus"
);
});
add_task(async function test_maintainsFocusWhenClosed() {
InAppNotifications.updateNotifications([
{
id: "testNotification" + Date.now(),
title: "Test notification",
description: "Long prose text",
CTA: "Click me!",
severity: 1,
type: "donation",
start_at: new Date(Date.now() - 100000).toISOString(),
end_at: new Date(Date.now() + 9999999999).toISOString(),
targeting: {},
},
]);
const searchBar = document.querySelector("global-search-bar");
searchBar.focus();
Assert.equal(
document.querySelectorAll("in-app-notification").length,
1,
"Notification Shown"
);
EventUtils.synthesizeMouseAtCenter(
document
.querySelector("in-app-notification")
.shadowRoot.querySelector("in-app-notification-container")
.shadowRoot.querySelector('[is="in-app-notification-close-button"]'),
{}
);
Assert.equal(
searchBar,
document.activeElement,
"the search bar still has focus"
);
});
add_task(async function test_doesNotStoreFocusElementInsideNotification() {
InAppNotifications.updateNotifications([
{
id: "testNotification" + Date.now(),
title: "Test notification",
description: "Long prose text",
CTA: "Click me!",
severity: 1,
type: "donation",
start_at: new Date(Date.now() - 100000).toISOString(),
end_at: new Date(Date.now() + 9999999999).toISOString(),
targeting: {},
},
]);
const searchBar = document.querySelector("global-search-bar");
searchBar.focus();
const container = document
.querySelector("in-app-notification")
.shadowRoot.querySelector("in-app-notification-container").shadowRoot;
container.querySelector(".in-app-notification-container").focus();
EventUtils.synthesizeMouseAtCenter(
container.querySelector('[is="in-app-notification-close-button"]'),
{}
);
Assert.equal(
searchBar,
document.activeElement,
"the search bar still has focus"
);
});