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() {