diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /browser/base/content/test/notificationbox | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'browser/base/content/test/notificationbox')
4 files changed, 442 insertions, 0 deletions
diff --git a/browser/base/content/test/notificationbox/browser.ini b/browser/base/content/test/notificationbox/browser.ini new file mode 100644 index 0000000000..2a2c4cced1 --- /dev/null +++ b/browser/base/content/test/notificationbox/browser.ini @@ -0,0 +1,3 @@ +[browser_notificationbar_telemetry.js] +[browser_notification_stacking.js] +[browser_tabnotificationbox_switch_tabs.js] diff --git a/browser/base/content/test/notificationbox/browser_notification_stacking.js b/browser/base/content/test/notificationbox/browser_notification_stacking.js new file mode 100644 index 0000000000..bd8817ea4b --- /dev/null +++ b/browser/base/content/test/notificationbox/browser_notification_stacking.js @@ -0,0 +1,78 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +async function addNotification(box, label, value, priorityName) { + let added = BrowserTestUtils.waitForNotificationInNotificationBox(box, value); + let priority = gNotificationBox[`PRIORITY_${priorityName}_MEDIUM`]; + let notification = box.appendNotification(value, { label, priority }); + await added; + return notification; +} + +add_task(async function testStackingOrder() { + const tabNotificationBox = gBrowser.getNotificationBox(); + ok( + gNotificationBox.stack.hasAttribute("prepend-notifications"), + "Browser stack will prepend" + ); + ok( + !tabNotificationBox.stack.hasAttribute("prepend-notifications"), + "Tab stack will append" + ); + + let browserOne = await addNotification( + gNotificationBox, + "My first browser notification", + "browser-one", + "INFO" + ); + + let tabOne = await addNotification( + tabNotificationBox, + "My first tab notification", + "tab-one", + "CRITICAL" + ); + + let browserTwo = await addNotification( + gNotificationBox, + "My second browser notification", + "browser-two", + "CRITICAL" + ); + let browserThree = await addNotification( + gNotificationBox, + "My third browser notification", + "browser-three", + "WARNING" + ); + + let tabTwo = await addNotification( + tabNotificationBox, + "My second tab notification", + "tab-two", + "INFO" + ); + let tabThree = await addNotification( + tabNotificationBox, + "My third tab notification", + "tab-three", + "WARNING" + ); + + Assert.deepEqual( + [browserThree, browserTwo, browserOne], + [...gNotificationBox.stack.children], + "Browser notifications prepended" + ); + Assert.deepEqual( + [tabOne, tabTwo, tabThree], + [...tabNotificationBox.stack.children], + "Tab notifications appended" + ); + + gNotificationBox.removeAllNotifications(true); + tabNotificationBox.removeAllNotifications(true); +}); diff --git a/browser/base/content/test/notificationbox/browser_notificationbar_telemetry.js b/browser/base/content/test/notificationbox/browser_notificationbar_telemetry.js new file mode 100644 index 0000000000..7810d4022d --- /dev/null +++ b/browser/base/content/test/notificationbox/browser_notificationbar_telemetry.js @@ -0,0 +1,219 @@ +const TELEMETRY_BASE = "notificationbar."; + +ChromeUtils.defineESModuleGetters(this, { + TelemetryTestUtils: "resource://testing-common/TelemetryTestUtils.sys.mjs", +}); + +add_task(async function showNotification() { + Services.telemetry.clearScalars(); + + let tab1 = await BrowserTestUtils.openNewForegroundTab( + gBrowser, + "https://example.com/" + ); + + ok(!gBrowser.readNotificationBox(), "no notificationbox created yet"); + + let box1 = gBrowser.getNotificationBox(); + + ok(gBrowser.readNotificationBox(), "notificationbox was created"); + + let tab2 = await BrowserTestUtils.openNewForegroundTab( + gBrowser, + "https://example.org/" + ); + + let tab3 = await BrowserTestUtils.openNewForegroundTab( + gBrowser, + "data:text/html,<body>Hello</body>" + ); + let box3 = gBrowser.getNotificationBox(); + + verifyTelemetry("initial", 0, 0, 0, 0, 0, 0); + + let notif3 = box3.appendNotification("infobar-testtwo-value", { + label: "Message for tab 3", + priority: box3.PRIORITY_INFO_HIGH, + telemetry: TELEMETRY_BASE + "testtwo", + }); + + verifyTelemetry("first notification", 0, 0, 0, 0, 0, 1); + + let notif1 = box1.appendNotification( + "infobar-testone-value", + { + label: "Message for tab 1", + priority: box1.PRIORITY_INFO_HIGH, + telemetry: TELEMETRY_BASE + "testone", + }, + [ + { + label: "Button1", + telemetry: "button1-pressed", + }, + { + label: "Button2", + telemetry: "button2-pressed", + }, + { + label: "Button3", + }, + ] + ); + verifyTelemetry("second notification", 0, 0, 0, 0, 0, 1); + + await BrowserTestUtils.switchTab(gBrowser, tab1); + verifyTelemetry("switch to first tab", 1, 0, 0, 0, 0, 1); + + await BrowserTestUtils.switchTab(gBrowser, tab2); + verifyTelemetry("switch to second tab", 1, 0, 0, 0, 0, 1); + + await BrowserTestUtils.switchTab(gBrowser, tab3); + verifyTelemetry("switch to third tab", 1, 0, 0, 0, 0, 1); + + await BrowserTestUtils.switchTab(gBrowser, tab1); + verifyTelemetry("switch to first tab again", 1, 0, 0, 0, 0, 1); + + notif1.buttonContainer.lastElementChild.click(); + verifyTelemetry("press third button", 1, 1, 0, 0, 0, 1); + + notif1.buttonContainer.lastElementChild.previousElementSibling.click(); + verifyTelemetry("press second button", 1, 1, 0, 1, 0, 1); + + notif1.buttonContainer.lastElementChild.previousElementSibling.previousElementSibling.click(); + verifyTelemetry("press first button", 1, 1, 1, 1, 0, 1); + + notif1.dismiss(); + verifyTelemetry("dismiss notification for box 1", 1, 1, 1, 1, 1, 1); + + notif3.dismiss(); + verifyTelemetry("dismiss notification for box 3", 1, 1, 1, 1, 1, 1, 1); + + let notif4 = box1.appendNotification( + "infobar-testtwo-value", + { + label: "Additional message for tab 1", + priority: box1.PRIORITY_INFO_HIGH, + telemetry: TELEMETRY_BASE + "testone", + telemetryFilter: ["shown"], + }, + [ + { + label: "Button1", + }, + ] + ); + verifyTelemetry("show first filtered notification", 2, 1, 1, 1, 1, 1, 1); + + notif4.buttonContainer.lastElementChild.click(); + notif4.dismiss(); + verifyTelemetry("dismiss first filtered notification", 2, 1, 1, 1, 1, 1, 1); + + let notif5 = box1.appendNotification( + "infobar-testtwo-value", + { + label: "Dimissed additional message for tab 1", + priority: box1.PRIORITY_INFO_HIGH, + telemetry: TELEMETRY_BASE + "testone", + telemetryFilter: ["dismissed"], + }, + [ + { + label: "Button1", + }, + ] + ); + verifyTelemetry("show second filtered notification", 2, 1, 1, 1, 1, 1, 1); + + notif5.buttonContainer.lastElementChild.click(); + notif5.dismiss(); + verifyTelemetry("dismiss second filtered notification", 2, 1, 1, 1, 2, 1, 1); + + let notif6 = box1.appendNotification( + "infobar-testtwo-value", + { + label: "Dimissed additional message for tab 1", + priority: box1.PRIORITY_INFO_HIGH, + telemetry: TELEMETRY_BASE + "testone", + telemetryFilter: ["button1-pressed", "dismissed"], + }, + [ + { + label: "Button1", + telemetry: "button1-pressed", + }, + ] + ); + verifyTelemetry("show third filtered notification", 2, 1, 1, 1, 2, 1, 1); + + notif6.buttonContainer.lastElementChild.click(); + verifyTelemetry( + "click button in third filtered notification", + 2, + 1, + 2, + 1, + 2, + 1, + 1 + ); + notif6.dismiss(); + verifyTelemetry("dismiss third filtered notification", 2, 1, 2, 1, 3, 1, 1); + + BrowserTestUtils.removeTab(tab1); + BrowserTestUtils.removeTab(tab2); + BrowserTestUtils.removeTab(tab3); +}); + +function verify(scalars, scalar, key, expected, exists) { + scalar = TELEMETRY_BASE + scalar; + + if (expected > 0) { + TelemetryTestUtils.assertKeyedScalar(scalars, scalar, key, expected); + return; + } + + Assert.equal( + scalar in scalars, + exists, + `expected ${scalar} to be ${exists ? "present" : "unset"}` + ); + + if (exists) { + Assert.ok( + !(key in scalars[scalar]), + "expected key " + key + " to be unset" + ); + } +} + +function verifyTelemetry( + desc, + box1shown, + box1action, + box1button1, + box1button2, + box1dismissed, + box3shown, + box3dismissed = 0 +) { + let scalars = TelemetryTestUtils.getProcessScalars("parent", true, false); + + info(desc); + let n1exists = + box1shown || box1action || box1button1 || box1button2 || box1dismissed; + + verify(scalars, "testone", "shown", box1shown, n1exists); + verify(scalars, "testone", "action", box1action, n1exists); + verify(scalars, "testone", "button1-pressed", box1button1, n1exists); + verify(scalars, "testone", "button2-pressed", box1button2, n1exists); + verify(scalars, "testone", "dismissed", box1dismissed, n1exists); + verify(scalars, "testtwo", "shown", box3shown, box3shown || box3dismissed); + verify( + scalars, + "testtwo", + "dismissed", + box3dismissed, + box3shown || box3dismissed + ); +} diff --git a/browser/base/content/test/notificationbox/browser_tabnotificationbox_switch_tabs.js b/browser/base/content/test/notificationbox/browser_tabnotificationbox_switch_tabs.js new file mode 100644 index 0000000000..f00916c773 --- /dev/null +++ b/browser/base/content/test/notificationbox/browser_tabnotificationbox_switch_tabs.js @@ -0,0 +1,142 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +function assertNotificationBoxHidden(reason, browser) { + let notificationBox = gBrowser.readNotificationBox(browser); + + if (!notificationBox) { + ok(!notificationBox, `Notification box has not been created ${reason}`); + return; + } + + let name = notificationBox._stack.getAttribute("name"); + ok(name, `Notification box has a name ${reason}`); + + let { selectedViewName } = notificationBox._stack.parentElement; + ok( + selectedViewName != name, + `Box is not shown ${reason} ${selectedViewName} != ${name}` + ); +} + +function assertNotificationBoxShown(reason, browser) { + let notificationBox = gBrowser.readNotificationBox(browser); + ok(notificationBox, `Notification box has been created ${reason}`); + + let name = notificationBox._stack.getAttribute("name"); + ok(name, `Notification box has a name ${reason}`); + + let { selectedViewName } = notificationBox._stack.parentElement; + is(selectedViewName, name, `Box is shown ${reason}`); +} + +function createNotification({ browser, label, value, priority }) { + let notificationBox = gBrowser.getNotificationBox(browser); + let notification = notificationBox.appendNotification(value, { + label, + priority: notificationBox[priority], + }); + return notification; +} + +add_task(async function testNotificationInBackgroundTab() { + let firstTab = gBrowser.selectedTab; + + // Navigating to a page should not create the notification box + await BrowserTestUtils.withNewTab("https://example.com", async browser => { + let notificationBox = gBrowser.readNotificationBox(browser); + ok(!notificationBox, "The notification box has not been created"); + + gBrowser.selectedTab = firstTab; + assertNotificationBoxHidden("initial first tab"); + + createNotification({ + browser, + label: "My notification body", + value: "test-notification", + priority: "PRIORITY_INFO_LOW", + }); + + gBrowser.selectedTab = gBrowser.getTabForBrowser(browser); + assertNotificationBoxShown("notification created"); + }); +}); + +add_task(async function testNotificationInActiveTab() { + // Open about:blank so the notification box isn't created on tab open. + await BrowserTestUtils.withNewTab("about:blank", async browser => { + ok(!gBrowser.readNotificationBox(browser), "No notifications for new tab"); + + createNotification({ + browser, + label: "Notification!", + value: "test-notification", + priority: "PRIORITY_INFO_LOW", + }); + assertNotificationBoxShown("after appendNotification"); + }); +}); + +add_task(async function testNotificationMultipleTabs() { + let tabOne = gBrowser.selectedTab; + let tabTwo = await BrowserTestUtils.openNewForegroundTab({ + gBrowser, + url: "about:blank", + }); + let tabThree = await BrowserTestUtils.openNewForegroundTab({ + gBrowser, + url: "https://example.com", + }); + let browserOne = tabOne.linkedBrowser; + let browserTwo = tabTwo.linkedBrowser; + let browserThree = tabThree.linkedBrowser; + + is(gBrowser.selectedBrowser, browserThree, "example.com selected"); + + let notificationBoxOne = gBrowser.readNotificationBox(browserOne); + let notificationBoxTwo = gBrowser.readNotificationBox(browserTwo); + let notificationBoxThree = gBrowser.readNotificationBox(browserThree); + + ok(!notificationBoxOne, "no initial tab box"); + ok(!notificationBoxTwo, "no about:blank box"); + ok(!notificationBoxThree, "no example.com box"); + + // Verify the correct box is shown after creating tabs. + assertNotificationBoxHidden("after open", browserOne); + assertNotificationBoxHidden("after open", browserTwo); + assertNotificationBoxHidden("after open", browserThree); + + createNotification({ + browser: browserTwo, + label: "Test blank", + value: "blank", + priority: "PRIORITY_INFO_LOW", + }); + notificationBoxTwo = gBrowser.readNotificationBox(browserTwo); + ok(notificationBoxTwo, "Notification box was created"); + + // Verify the selected browser's notification box is still hidden. + assertNotificationBoxHidden("hidden create", browserTwo); + assertNotificationBoxHidden("other create", browserThree); + + createNotification({ + browser: browserThree, + label: "Test active tab", + value: "active", + priority: "PRIORITY_CRITICAL_LOW", + }); + // Verify the selected browser's notification box is still shown. + assertNotificationBoxHidden("active create", browserTwo); + assertNotificationBoxShown("active create", browserThree); + + gBrowser.selectedTab = tabTwo; + + // Verify the notification box for the tab that has one gets shown. + assertNotificationBoxShown("tab switch", browserTwo); + assertNotificationBoxHidden("tab switch", browserThree); + + BrowserTestUtils.removeTab(tabTwo); + BrowserTestUtils.removeTab(tabThree); +}); |