From 6bf0a5cb5034a7e684dcc3500e841785237ce2dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:32:43 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- comm/mail/base/test/unit/test_alertHook.js | 119 +++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 comm/mail/base/test/unit/test_alertHook.js (limited to 'comm/mail/base/test/unit/test_alertHook.js') diff --git a/comm/mail/base/test/unit/test_alertHook.js b/comm/mail/base/test/unit/test_alertHook.js new file mode 100644 index 0000000000..0189930862 --- /dev/null +++ b/comm/mail/base/test/unit/test_alertHook.js @@ -0,0 +1,119 @@ +/* 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/. */ + +/** + * Tests the replace of alerts service with our own. This will let us check if we're + * prompting or not. + */ + +var { alertHook } = ChromeUtils.import( + "resource:///modules/activity/alertHook.jsm" +); +var { MailServices } = ChromeUtils.import( + "resource:///modules/MailServices.jsm" +); +var { MockRegistrar } = ChromeUtils.importESModule( + "resource://testing-common/MockRegistrar.sys.mjs" +); +var { PromiseTestUtils } = ChromeUtils.import( + "resource://testing-common/mailnews/PromiseTestUtils.jsm" +); +var { PromiseUtils } = ChromeUtils.importESModule( + "resource://gre/modules/PromiseUtils.sys.mjs" +); + +alertHook.init(); + +// Wait time of 1s for slow debug builds. +const TEST_WAITTIME = 1000; + +var gMsgWindow = Cc["@mozilla.org/messenger/msgwindow;1"].createInstance( + Ci.nsIMsgWindow +); +var mockAlertsService; +var cid; +var mailnewsURL; + +add_setup(function () { + // First register the mock alerts service. + mockAlertsService = new MockAlertsService(); + cid = MockRegistrar.register( + "@mozilla.org/alerts-service;1", + mockAlertsService + ); + // A random URL. + let uri = Services.io.newURI("news://localhost:80/1@regular.invalid"); + mailnewsURL = uri.QueryInterface(Ci.nsIMsgMailNewsUrl); +}); + +add_task(async function test_not_shown_to_user_no_url_no_window() { + // Just text, no url or window => expect no error shown to user + MailServices.mailSession.alertUser("test error"); + await Promise.race([ + PromiseTestUtils.promiseDelay(TEST_WAITTIME).then(result => { + Assert.ok(true, "Alert is not shown with no window or no url present"); + }), + mockAlertsService.promise.then(result => { + throw new Error( + "Alert is shown to the user although neither window nor url is present" + ); + }), + ]); +}); + +add_task(async function test_shown_to_user() { + // Reset promise state. + mockAlertsService.deferPromise(); + // Set a window for the URL. + mailnewsURL.msgWindow = gMsgWindow; + + // Text, url and window => expect error shown to user + MailServices.mailSession.alertUser("test error 2", mailnewsURL); + let alertShown = await mockAlertsService.promise; + Assert.ok(alertShown); +}); + +add_task(async function test_not_shown_to_user_no_window() { + // Reset promise state. + mockAlertsService.deferPromise(); + // No window for the URL. + mailnewsURL.msgWindow = null; + + // Text, url and no window => export no error shown to user + MailServices.mailSession.alertUser("test error 3", mailnewsURL); + await Promise.race([ + PromiseTestUtils.promiseDelay(TEST_WAITTIME).then(result => { + Assert.ok(true, "Alert is not shown with no window but a url present"); + }), + mockAlertsService.promise.then(result => { + throw new Error( + "Alert is shown to the user although no window in the mailnewsURL present" + ); + }), + ]); +}); + +add_task(function endTest() { + MockRegistrar.unregister(cid); +}); + +class MockAlertsService { + QueryInterface = ChromeUtils.generateQI(["nsIAlertsService"]); + + constructor() { + this._deferredPromise = PromiseUtils.defer(); + } + + showAlert() { + this._deferredPromise.resolve(true); + } + + deferPromise() { + this._deferredPromise = PromiseUtils.defer(); + } + + get promise() { + return this._deferredPromise.promise; + } +} -- cgit v1.2.3