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
This commit is contained in:
Dan Stillman 2015-06-04 18:52:47 -04:00
parent 582799e428
commit da627e137a
2 changed files with 64 additions and 5 deletions

View file

@ -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);

View file

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