From da627e137ab76f434112ead06919bfdd5496a0b7 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Thu, 4 Jun 2015 18:52:47 -0400 Subject: [PATCH] Add waitForDialog(onOpen, button) support function Waits for an alert or confirmation dialog to open and closes it automatically, optionally after running onOpen(dialog) to check its contents (e.g., with dialog.document.documentElement.textContent) and optionally clicking a button other than 'accept' (e.g., 'cancel', extra1'). Supports delayed accept buttons --- test/content/support.js | 64 +++++++++++++++++++++++++++- test/tests/preferences_searchTest.js | 5 +-- 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/test/content/support.js b/test/content/support.js index 18f476ddaf..6c0259cca6 100644 --- a/test/content/support.js +++ b/test/content/support.js @@ -75,7 +75,18 @@ function waitForWindow(uri, callback) { .QueryInterface(Components.interfaces.nsIInterfaceRequestor) .getInterface(Components.interfaces.nsIDOMWindow); if (callback) { - callback(win); + try { + // If callback is a promise, wait for it + let maybePromise = callback(win); + if (maybePromise && maybePromise.then) { + maybePromise.then(() => deferred.resolve(win)).catch(e => deferred.reject(e)); + return; + } + } + catch (e) { + deferred.reject(e); + return; + } } deferred.resolve(win); } @@ -89,6 +100,57 @@ function waitForWindow(uri, callback) { return deferred.promise; } +/** + * Wait for an alert or confirmation dialog to pop up and then close it + * + * @param {Function} [onOpen] - Function that is passed the dialog once it is opened. + * Can be used to make assertions on the dialog contents + * (e.g., with dialog.document.documentElement.textContent) + * @param {String} [button='accept'] - Button in dialog to press (e.g., 'cancel', 'extra1') + * @return {Promise} + */ +function waitForDialog(onOpen, button='accept') { + return waitForWindow("chrome://global/content/commonDialog.xul", Zotero.Promise.method(function (dialog, deferred) { + var failure = false; + if (onOpen) { + try { + onOpen(dialog); + } + catch (e) { + failure = e; + } + } + if (button == 'accept') { + let deferred = Zotero.Promise.defer(); + function acceptWhenEnabled() { + // Handle delayed accept buttons + if (dialog.document.documentElement.getButton('accept').disabled) { + setTimeout(function () { + acceptWhenEnabled(); + }, 250); + } + else { + dialog.document.documentElement.acceptDialog(); + if (failure) { + deferred.reject(failure); + } + else { + deferred.resolve(); + } + } + } + acceptWhenEnabled(); + return deferred.promise; + } + else { + dialog.document.documentElement.getButton(button).click(); + if (failure) { + throw failure; + } + } + })) +} + var selectLibrary = Zotero.Promise.coroutine(function* (win, libraryID) { libraryID = libraryID || Zotero.Libraries.userLibraryID; yield win.ZoteroPane.collectionsView.selectLibrary(libraryID); diff --git a/test/tests/preferences_searchTest.js b/test/tests/preferences_searchTest.js index 34e18ff41f..0a5e0d0a32 100644 --- a/test/tests/preferences_searchTest.js +++ b/test/tests/preferences_searchTest.js @@ -7,10 +7,7 @@ describe("Search Preferences", function () { action: 'pdftools-install' }); // Wait for confirmation dialog - yield waitForWindow("chrome://global/content/commonDialog.xul", function (dialog) { - // Accept confirmation dialog - dialog.document.documentElement.acceptDialog(); - }); + yield waitForDialog(); // Wait for install to finish yield waitForCallback(function() {