From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- dom/notification/test/mochitest/MockServices.js | 177 +++++++++++++++++++++ .../test/mochitest/NotificationTest.js | 102 ++++++++++++ dom/notification/test/mochitest/blank.html | 4 + .../test/mochitest/create_notification.html | 16 ++ dom/notification/test/mochitest/mochitest.ini | 22 +++ .../test/mochitest/test_notification_basics.html | 125 +++++++++++++++ .../test_notification_crossorigin_iframe.html | 68 ++++++++ .../test_notification_insecure_context.html | 47 ++++++ .../mochitest/test_notification_permissions.html | 70 ++++++++ .../test/mochitest/test_notification_tag.html | 169 ++++++++++++++++++++ 10 files changed, 800 insertions(+) create mode 100644 dom/notification/test/mochitest/MockServices.js create mode 100644 dom/notification/test/mochitest/NotificationTest.js create mode 100644 dom/notification/test/mochitest/blank.html create mode 100644 dom/notification/test/mochitest/create_notification.html create mode 100644 dom/notification/test/mochitest/mochitest.ini create mode 100644 dom/notification/test/mochitest/test_notification_basics.html create mode 100644 dom/notification/test/mochitest/test_notification_crossorigin_iframe.html create mode 100644 dom/notification/test/mochitest/test_notification_insecure_context.html create mode 100644 dom/notification/test/mochitest/test_notification_permissions.html create mode 100644 dom/notification/test/mochitest/test_notification_tag.html (limited to 'dom/notification/test/mochitest') diff --git a/dom/notification/test/mochitest/MockServices.js b/dom/notification/test/mochitest/MockServices.js new file mode 100644 index 0000000000..0f50bcf5bf --- /dev/null +++ b/dom/notification/test/mochitest/MockServices.js @@ -0,0 +1,177 @@ +/* eslint-disable mozilla/use-chromeutils-generateqi */ +var MockServices = (function () { + "use strict"; + + const MOCK_ALERTS_CID = SpecialPowers.wrap(SpecialPowers.Components).ID( + "{48068bc2-40ab-4904-8afd-4cdfb3a385f3}" + ); + const ALERTS_SERVICE_CONTRACT_ID = "@mozilla.org/alerts-service;1"; + + const MOCK_SYSTEM_ALERTS_CID = SpecialPowers.wrap( + SpecialPowers.Components + ).ID("{e86d888c-e41b-4b78-9104-2f2742a532de}"); + const SYSTEM_ALERTS_SERVICE_CONTRACT_ID = + "@mozilla.org/system-alerts-service;1"; + + var registrar = SpecialPowers.wrap( + SpecialPowers.Components + ).manager.QueryInterface(SpecialPowers.Ci.nsIComponentRegistrar); + + var activeAlertNotifications = Object.create(null); + + var activeAppNotifications = Object.create(null); + + window.addEventListener("mock-notification-close-event", function (e) { + for (var alertName in activeAlertNotifications) { + var notif = activeAlertNotifications[alertName]; + if (notif.title === e.detail.title) { + notif.listener.observe(null, "alertfinished", null); + delete activeAlertNotifications[alertName]; + delete activeAppNotifications[alertName]; + return; + } + } + }); + + var mockAlertsService = { + showPersistentNotification(persistentData, alert, alertListener) { + this.showAlert(alert, alertListener); + }, + + showAlert(alert, alertListener) { + var listener = SpecialPowers.wrap(alertListener); + activeAlertNotifications[alert.name] = { + listener, + cookie: alert.cookie, + title: alert.title, + }; + + // fake async alert show event + if (listener) { + setTimeout(function () { + listener.observe(null, "alertshow", alert.cookie); + }, 100); + setTimeout(function () { + listener.observe(null, "alertclickcallback", alert.cookie); + }, 100); + } + }, + + showAlertNotification( + imageUrl, + title, + text, + textClickable, + cookie, + alertListener, + name + ) { + this.showAlert( + { + name, + cookie, + title, + }, + alertListener + ); + }, + + closeAlert(name) { + var alertNotification = activeAlertNotifications[name]; + if (alertNotification) { + if (alertNotification.listener) { + alertNotification.listener.observe( + null, + "alertfinished", + alertNotification.cookie + ); + } + delete activeAlertNotifications[name]; + } + + var appNotification = activeAppNotifications[name]; + if (appNotification) { + delete activeAppNotifications[name]; + } + }, + + QueryInterface(aIID) { + if ( + SpecialPowers.wrap(aIID).equals(SpecialPowers.Ci.nsISupports) || + SpecialPowers.wrap(aIID).equals(SpecialPowers.Ci.nsIAlertsService) + ) { + return this; + } + throw SpecialPowers.Components.results.NS_ERROR_NO_INTERFACE; + }, + + createInstance(aIID) { + return this.QueryInterface(aIID); + }, + }; + mockAlertsService = SpecialPowers.wrapCallbackObject(mockAlertsService); + + // MockServices API + return { + register() { + try { + this.originalAlertsCID = registrar.contractIDToCID( + ALERTS_SERVICE_CONTRACT_ID + ); + } catch (ex) { + this.originalAlertsCID = null; + } + try { + this.originalSystemAlertsCID = registrar.contractIDToCID( + SYSTEM_ALERTS_SERVICE_CONTRACT_ID + ); + } catch (ex) { + this.originalSystemAlertsCID = null; + } + + registrar.registerFactory( + MOCK_ALERTS_CID, + "alerts service", + ALERTS_SERVICE_CONTRACT_ID, + mockAlertsService + ); + + registrar.registerFactory( + MOCK_SYSTEM_ALERTS_CID, + "system alerts service", + SYSTEM_ALERTS_SERVICE_CONTRACT_ID, + mockAlertsService + ); + }, + + unregister() { + registrar.unregisterFactory(MOCK_ALERTS_CID, mockAlertsService); + registrar.unregisterFactory(MOCK_SYSTEM_ALERTS_CID, mockAlertsService); + + // Passing `null` for the factory re-maps the contract ID to the + // entry for its original CID. + + if (this.originalAlertsCID) { + registrar.registerFactory( + this.originalAlertsCID, + "alerts service", + ALERTS_SERVICE_CONTRACT_ID, + null + ); + } + + if (this.originalSystemAlertsCID) { + registrar.registerFactory( + this.originalSystemAlertsCID, + "system alerts service", + SYSTEM_ALERTS_SERVICE_CONTRACT_ID, + null + ); + } + }, + + activeAlertNotifications, + + activeAppNotifications, + }; +})(); diff --git a/dom/notification/test/mochitest/NotificationTest.js b/dom/notification/test/mochitest/NotificationTest.js new file mode 100644 index 0000000000..400ff56253 --- /dev/null +++ b/dom/notification/test/mochitest/NotificationTest.js @@ -0,0 +1,102 @@ +var NotificationTest = (function () { + "use strict"; + + function info(msg, name) { + SimpleTest.info("::Notification Tests::" + (name || ""), msg); + } + + function setup_testing_env() { + SimpleTest.waitForExplicitFinish(); + // turn on testing pref (used by notification.cpp, and mock the alerts + return SpecialPowers.setBoolPref("notification.prompt.testing", true); + } + + async function teardown_testing_env() { + await SpecialPowers.clearUserPref("notification.prompt.testing"); + await SpecialPowers.clearUserPref("notification.prompt.testing.allow"); + + SimpleTest.finish(); + } + + function executeTests(tests, callback) { + // context is `this` object in test functions + // it can be used to track data between tests + var context = {}; + + (function executeRemainingTests(remainingTests) { + if (!remainingTests.length) { + callback(); + return; + } + + var nextTest = remainingTests.shift(); + var finishTest = executeRemainingTests.bind(null, remainingTests); + var startTest = nextTest.call.bind(nextTest, context, finishTest); + + try { + startTest(); + // if no callback was defined for test function, + // we must manually invoke finish to continue + if (nextTest.length === 0) { + finishTest(); + } + } catch (e) { + ok(false, "Test threw exception!"); + finishTest(); + } + })(tests); + } + + // NotificationTest API + return { + run(tests, callback) { + let ready = setup_testing_env(); + + addLoadEvent(async function () { + await ready; + executeTests(tests, function () { + teardown_testing_env(); + callback && callback(); + }); + }); + }, + + allowNotifications() { + return SpecialPowers.setBoolPref( + "notification.prompt.testing.allow", + true + ); + }, + + denyNotifications() { + return SpecialPowers.setBoolPref( + "notification.prompt.testing.allow", + false + ); + }, + + clickNotification(notification) { + // TODO: how?? + }, + + fireCloseEvent(title) { + window.dispatchEvent( + new CustomEvent("mock-notification-close-event", { + detail: { + title, + }, + }) + ); + }, + + info, + + payload: { + body: "Body", + tag: "fakeTag", + icon: "icon.jpg", + lang: "en-US", + dir: "ltr", + }, + }; +})(); diff --git a/dom/notification/test/mochitest/blank.html b/dom/notification/test/mochitest/blank.html new file mode 100644 index 0000000000..1f9324523a --- /dev/null +++ b/dom/notification/test/mochitest/blank.html @@ -0,0 +1,4 @@ + + + + diff --git a/dom/notification/test/mochitest/create_notification.html b/dom/notification/test/mochitest/create_notification.html new file mode 100644 index 0000000000..b0387e4ffb --- /dev/null +++ b/dom/notification/test/mochitest/create_notification.html @@ -0,0 +1,16 @@ + + + + Create a notification + + + + + diff --git a/dom/notification/test/mochitest/mochitest.ini b/dom/notification/test/mochitest/mochitest.ini new file mode 100644 index 0000000000..e2e84b575a --- /dev/null +++ b/dom/notification/test/mochitest/mochitest.ini @@ -0,0 +1,22 @@ +[DEFAULT] + +support-files = + blank.html + create_notification.html + MockServices.js + NotificationTest.js +skip-if = toolkit == 'android' # Bug 1531097 + +[test_notification_basics.html] +skip-if = xorigin # Bug 1792790 +[test_notification_crossorigin_iframe.html] +# This test needs to be run on HTTP (not HTTPS). +[test_notification_insecure_context.html] +fail-if = xorigin +skip-if = + http3 +[test_notification_permissions.html] +scheme = https +[test_notification_tag.html] +skip-if = + http3 diff --git a/dom/notification/test/mochitest/test_notification_basics.html b/dom/notification/test/mochitest/test_notification_basics.html new file mode 100644 index 0000000000..3dde839a96 --- /dev/null +++ b/dom/notification/test/mochitest/test_notification_basics.html @@ -0,0 +1,125 @@ + + + + Notification Basics + + + + + + +

+ +

+
+
+
diff --git a/dom/notification/test/mochitest/test_notification_crossorigin_iframe.html b/dom/notification/test/mochitest/test_notification_crossorigin_iframe.html
new file mode 100644
index 0000000000..dfdec3b1bd
--- /dev/null
+++ b/dom/notification/test/mochitest/test_notification_crossorigin_iframe.html
@@ -0,0 +1,68 @@
+
+
+
+
+  Notification permission in cross-origin iframes
+  
+  
+
+
+  

+ +
+  
+  
+ + diff --git a/dom/notification/test/mochitest/test_notification_insecure_context.html b/dom/notification/test/mochitest/test_notification_insecure_context.html new file mode 100644 index 0000000000..cd3a22bc7f --- /dev/null +++ b/dom/notification/test/mochitest/test_notification_insecure_context.html @@ -0,0 +1,47 @@ + + + + + Notification permission in insecure context + + + + +

+ +
+  
+  
+ + diff --git a/dom/notification/test/mochitest/test_notification_permissions.html b/dom/notification/test/mochitest/test_notification_permissions.html new file mode 100644 index 0000000000..f074ccf58e --- /dev/null +++ b/dom/notification/test/mochitest/test_notification_permissions.html @@ -0,0 +1,70 @@ + + + + + Notification permissions and permissions API + + + + +

+ +
+
+  
+ + diff --git a/dom/notification/test/mochitest/test_notification_tag.html b/dom/notification/test/mochitest/test_notification_tag.html new file mode 100644 index 0000000000..f4fc72bbe3 --- /dev/null +++ b/dom/notification/test/mochitest/test_notification_tag.html @@ -0,0 +1,169 @@ + + + + + Bug 782211 + + + + +Bug 782211 +

+ +
+
+ + + -- cgit v1.2.3