commit da627e137ab76f434112ead06919bfdd5496a0b7
parent 582799e42886565014422ffdd7e5b97bcdf5150a
Author: Dan Stillman <dstillman@zotero.org>
Date: Thu, 4 Jun 2015 18:52:47 -0400
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
Diffstat:
2 files changed, 64 insertions(+), 5 deletions(-)
diff --git 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
@@ -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() {