diff --git a/browser/components/originattributes/test/browser/browser_httpauth.js b/browser/components/originattributes/test/browser/browser_httpauth.js index 61d429349949..7e9a990a9d52 100644 --- a/browser/components/originattributes/test/browser/browser_httpauth.js +++ b/browser/components/originattributes/test/browser/browser_httpauth.js @@ -1,5 +1,14 @@ let { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js"); +let authPromptModalType = Services.prefs.getIntPref( + "prompts.modalType.httpAuth" +); + +let commonDialogEnabled = + authPromptModalType === Services.prompt.MODAL_TYPE_WINDOW || + (authPromptModalType === Services.prompt.MODAL_TYPE_TAB && + Services.prefs.getBoolPref("prompts.tabChromePromptSubDialog")); + let server = new HttpServer(); server.registerPathHandler("/file.html", fileHandler); server.start(-1); @@ -27,17 +36,27 @@ function fileHandler(metadata, response) { } function onCommonDialogLoaded(subject) { + let dialog; + if (commonDialogEnabled) { + dialog = subject.Dialog; + } else { + let promptBox = + subject.ownerGlobal.gBrowser.selectedBrowser.tabModalPromptBox; + dialog = promptBox.getPrompt(subject).Dialog; + } // Submit random account and password - let dialog = subject.Dialog; dialog.ui.loginTextbox.setAttribute("value", Math.random()); dialog.ui.password1Textbox.setAttribute("value", Math.random()); dialog.ui.button0.click(); } -Services.obs.addObserver(onCommonDialogLoaded, "common-dialog-loaded"); +let authPromptTopic = commonDialogEnabled + ? "common-dialog-loaded" + : "tabmodal-dialog-loaded"; +Services.obs.addObserver(onCommonDialogLoaded, authPromptTopic); registerCleanupFunction(() => { - Services.obs.removeObserver(onCommonDialogLoaded, "common-dialog-loaded"); + Services.obs.removeObserver(onCommonDialogLoaded, authPromptTopic); server.stop(() => { server = null; }); diff --git a/toolkit/components/passwordmgr/test/browser/browser_basicAuth_rateLimit.js b/toolkit/components/passwordmgr/test/browser/browser_basicAuth_rateLimit.js index d29b485ab07c..6046c71e0eb7 100644 --- a/toolkit/components/passwordmgr/test/browser/browser_basicAuth_rateLimit.js +++ b/toolkit/components/passwordmgr/test/browser/browser_basicAuth_rateLimit.js @@ -5,34 +5,14 @@ // This tests that the basic auth dialog can not be used for DOS attacks // and that the protections are reset on user-initiated navigation/reload. -const PROMPT_URL = "chrome://global/content/commonDialog.xhtml"; +let promptModalType = Services.prefs.getIntPref("prompts.modalType.httpAuth"); + function promiseAuthWindowShown() { - return new Promise(resolve => { - let listener = { - onOpenWindow(xulWin) { - let domwindow = xulWin.docShell.domWindow; - waitForFocus(() => { - is( - domwindow.document.location.href, - PROMPT_URL, - "Should have seen a prompt window" - ); - is( - domwindow.args.promptType, - "promptUserAndPass", - "Should be an authenticate prompt" - ); - domwindow.document.getElementById("commonDialog").cancelDialog(); - Services.wm.removeListener(listener); - resolve(); - }, domwindow); - }, - - onCloseWindow() {}, - }; - - Services.wm.addListener(listener); - }); + return PromptTestUtils.handleNextPrompt( + window, + { modalType: promptModalType, promptType: "promptUserAndPass" }, + { buttonNumClick: 1 } + ); } add_task(async function test() { diff --git a/toolkit/components/passwordmgr/test/browser/browser_basicAuth_switchTab.js b/toolkit/components/passwordmgr/test/browser/browser_basicAuth_switchTab.js index 100f7411cc25..1625217c0f44 100644 --- a/toolkit/components/passwordmgr/test/browser/browser_basicAuth_switchTab.js +++ b/toolkit/components/passwordmgr/test/browser/browser_basicAuth_switchTab.js @@ -2,47 +2,33 @@ * 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/. */ -const PROMPT_URL = "chrome://global/content/commonDialog.xhtml"; +let modalType = Services.prefs.getIntPref("prompts.modalType.httpAuth"); add_task(async function test() { - await new Promise(resolve => { - let tab = BrowserTestUtils.addTab(gBrowser); - isnot(tab, gBrowser.selectedTab, "New tab shouldn't be selected"); + let tab = BrowserTestUtils.addTab(gBrowser); + isnot(tab, gBrowser.selectedTab, "New tab shouldn't be selected"); - let listener = { - onOpenWindow(xulWin) { - var domwindow = xulWin.docShell.domWindow; - waitForFocus(() => { - is( - domwindow.document.location.href, - PROMPT_URL, - "Should have seen a prompt window" - ); - is( - domwindow.args.promptType, - "promptUserAndPass", - "Should be an authenticate prompt" - ); - - is(gBrowser.selectedTab, tab, "Should have selected the new tab"); - - domwindow.document.getElementById("commonDialog").cancelDialog(); - }, domwindow); - }, - - onCloseWindow() {}, - }; - - Services.wm.addListener(listener); - registerCleanupFunction(() => { - Services.wm.removeListener(listener); - gBrowser.removeTab(tab); - }); - - BrowserTestUtils.browserLoaded(tab.linkedBrowser).then(() => finish()); - BrowserTestUtils.loadURI( - tab.linkedBrowser, - "http://example.com/browser/toolkit/components/passwordmgr/test/browser/authenticate.sjs" - ); + let authPromptShown = PromptTestUtils.waitForPrompt(tab.linkedBrowser, { + modalType, + promptType: "promptUserAndPass", }); + + let loadPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser); + BrowserTestUtils.loadURI( + tab.linkedBrowser, + "http://example.com/browser/toolkit/components/passwordmgr/test/browser/authenticate.sjs" + ); + + // Wait for the basic auth prompt + let dialog = await authPromptShown; + + is(gBrowser.selectedTab, tab, "Should have selected the new tab"); + + // Cancel the auth prompt + PromptTestUtils.handlePrompt(dialog, { buttonNumClick: 1 }); + + // After closing the prompt the load should finish + await loadPromise; + + gBrowser.removeTab(tab); }); diff --git a/toolkit/components/passwordmgr/test/browser/browser_private_window.js b/toolkit/components/passwordmgr/test/browser/browser_private_window.js index a77b98cf2fd8..52bad4dd01c1 100644 --- a/toolkit/components/passwordmgr/test/browser/browser_private_window.js +++ b/toolkit/components/passwordmgr/test/browser/browser_private_window.js @@ -74,14 +74,13 @@ async function loadAccessRestrictedURL(browser, url, username, password) { let browserLoaded = BrowserTestUtils.browserLoaded(browser); BrowserTestUtils.loadURI(browser, url); - let promptDoc = await waitForAuthPrompt(); - let dialogUI = promptDoc.defaultView.Dialog.ui; - ok(dialogUI, "Got expected HTTP auth dialog Dialog.ui"); + // Wait for the auth prompt, enter the login details and close the prompt + await PromptTestUtils.handleNextPrompt( + browser, + { modalType: authPromptModalType, promptType: "promptUserAndPass" }, + { buttonNumClick: 0, loginInput: username, passwordInput: password } + ); - // fill and submit the dialog form - dialogUI.loginTextbox.value = username; - dialogUI.password1Textbox.value = password; - promptDoc.getElementById("commonDialog").acceptDialog(); await SimpleTest.promiseFocus(browser.ownerGlobal); await browserLoaded; } @@ -107,11 +106,13 @@ const authUrl = `https://example.com/${DIRECTORY_PATH}authenticate.sjs`; let normalWin; let privateWin; +let authPromptModalType; // XXX: Note that tasks are currently run in sequence. Some tests may assume the state // resulting from successful or unsuccessful logins in previous tasks add_task(async function test_setup() { + authPromptModalType = Services.prefs.getIntPref("prompts.modalType.httpAuth"); normalWin = await BrowserTestUtils.openNewBrowserWindow({ private: false }); privateWin = await BrowserTestUtils.openNewBrowserWindow({ private: true }); Services.logins.removeAllLogins(); diff --git a/toolkit/components/passwordmgr/test/browser/head.js b/toolkit/components/passwordmgr/test/browser/head.js index e5415ae7a29b..015cedf9515e 100644 --- a/toolkit/components/passwordmgr/test/browser/head.js +++ b/toolkit/components/passwordmgr/test/browser/head.js @@ -7,6 +7,7 @@ const { LoginManagerParent } = ChromeUtils.import( ChromeUtils.import("resource://testing-common/LoginTestUtils.jsm", this); ChromeUtils.import("resource://testing-common/ContentTaskUtils.jsm", this); ChromeUtils.import("resource://testing-common/TelemetryTestUtils.jsm", this); +ChromeUtils.import("resource://testing-common/PromptTestUtils.jsm", this); add_task(async function common_initialize() { await SpecialPowers.pushPrefEnv({ diff --git a/toolkit/components/passwordmgr/test/mochitest/pwmgr_common.js b/toolkit/components/passwordmgr/test/mochitest/pwmgr_common.js index 81695766a551..d9159d54f56c 100644 --- a/toolkit/components/passwordmgr/test/mochitest/pwmgr_common.js +++ b/toolkit/components/passwordmgr/test/mochitest/pwmgr_common.js @@ -32,6 +32,23 @@ const { const LOGIN_FIELD_UTILS = LoginTestUtils.loginField; const TESTS_DIR = "/tests/toolkit/components/passwordmgr/test/"; +// Depending on pref state we either show auth prompts as windows or on tab level. +let authPromptModalType = SpecialPowers.Services.prompt.MODAL_TYPE_WINDOW; +if (SpecialPowers.Services.prefs.getBoolPref("prompts.tab_modal.enabled")) { + authPromptModalType = SpecialPowers.Services.prefs.getIntPref( + "prompts.modalType.httpAuth" + ); +} + +// Whether the auth prompt is a commonDialog.xhtml or a TabModalPrompt +let authPromptIsCommonDialog = + authPromptModalType === SpecialPowers.Services.prompt.MODAL_TYPE_WINDOW || + (authPromptModalType === SpecialPowers.Services.prompt.MODAL_TYPE_TAB && + SpecialPowers.Services.prefs.getBoolPref( + "prompts.tabChromePromptSubDialog", + false + )); + /** * Returns the element with the specified |name| attribute. */ diff --git a/toolkit/components/passwordmgr/test/mochitest/test_bug_627616.html b/toolkit/components/passwordmgr/test/mochitest/test_bug_627616.html index 7bb27e62fb84..f28fb31aae08 100644 --- a/toolkit/components/passwordmgr/test/mochitest/test_bug_627616.html +++ b/toolkit/components/passwordmgr/test/mochitest/test_bug_627616.html @@ -119,12 +119,30 @@ sendAsyncMessage("prepareForNextTestDone"); }); - let dialogObserverTopic = "common-dialog-loaded"; + let modalType = Services.prefs.getIntPref( + "prompts.modalType.httpAuth" + ); + let authPromptIsCommonDialog = + modalType === Services.prompt.MODAL_TYPE_WINDOW + || (modalType === Services.prompt.MODAL_TYPE_TAB + && Services.prefs.getBoolPref( + "prompts.tabChromePromptSubDialog", + false + )); + + let dialogObserverTopic = authPromptIsCommonDialog + ? "common-dialog-loaded" : "tabmodal-dialog-loaded"; function dialogObserver(subj, topic, data) { - subj.Dialog.ui.prompt.document + if (authPromptIsCommonDialog) { + subj.Dialog.ui.prompt.document .getElementById("commonDialog") .acceptDialog(); + } else { + let prompt = subj.ownerGlobal.gBrowser.selectedBrowser + .tabModalPromptBox.getPrompt(subj); + prompt.Dialog.ui.button0.click(); // Accept button + } sendAsyncMessage("promptAccepted"); } diff --git a/toolkit/components/passwordmgr/test/mochitest/test_prompt_async.html b/toolkit/components/passwordmgr/test/mochitest/test_prompt_async.html index d0bd3b020380..0cac26cf1c0e 100644 --- a/toolkit/components/passwordmgr/test/mochitest/test_prompt_async.html +++ b/toolkit/components/passwordmgr/test/mochitest/test_prompt_async.html @@ -16,9 +16,8 @@ const EXAMPLE_ORG = "http://example.org/tests/toolkit/components/passwordmgr/test/mochitest/"; let mozproxyOrigin; - // Force parent to not look for tab-modal prompts, as they're not used for auth prompts. - // Used by prompt_common.js. - modalType = Ci.nsIPrompt.MODAL_TYPE_WINDOW; + // Let prompt_common know what kind of modal type is enabled for auth prompts. + modalType = authPromptModalType; // These are magically defined on the window due to the iframe IDs /* global iframe1, iframe2a, iframe2b */ diff --git a/toolkit/components/passwordmgr/test/mochitest/test_prompt_http.html b/toolkit/components/passwordmgr/test/mochitest/test_prompt_http.html index 9ad5a676b513..0222c58968ee 100644 --- a/toolkit/components/passwordmgr/test/mochitest/test_prompt_http.html +++ b/toolkit/components/passwordmgr/test/mochitest/test_prompt_http.html @@ -19,8 +19,8 @@