Created
November 22, 2010 21:54
Mozmill modal dialog shared module refactor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# HG changeset patch | |
# Parent 6585aa4bca819847388eb3ea0e43622b0f9739e0 | |
# User Henrik Skupin <hskupin@mozilla.com> | |
Bug 560820 - Refactor modal dialog to lower count of test failures. r=ctalbert | |
diff --git a/shared-modules/modal-dialog.js b/shared-modules/modal-dialog.js | |
--- a/shared-modules/modal-dialog.js | |
+++ b/shared-modules/modal-dialog.js | |
@@ -42,144 +42,101 @@ | |
* | |
* @version 1.0.2 | |
*/ | |
/* Huge amounts of code have been leveraged from the password manager mochitest suite. | |
* http://mxr.mozilla.org/mozilla-central/source/toolkit/components/passwordmgr/test/prompt_common.js | |
*/ | |
-var frame = {}; Components.utils.import('resource://mozmill/modules/frame.js', frame); | |
- | |
/** | |
* Observer object to find the modal dialog | |
*/ | |
var mdObserver = { | |
- QueryInterface : function (iid) { | |
- const interfaces = [Ci.nsIObserver, | |
- Ci.nsISupports, | |
- Ci.nsISupportsWeakReference]; | |
+ controller : null, | |
+ callback : null, | |
+ exception : null, | |
+ finished : null, | |
+ startTimer : null, | |
- if (!interfaces.some( function(v) { return iid.equals(v) } )) | |
- throw Components.results.NS_ERROR_NO_INTERFACE; | |
- return this; | |
+ /** | |
+ * Check the top most window if the opener is the equal to the calling window | |
+ * | |
+ * @returns True if the window has been found | |
+ * @type {Boolean} | |
+ */ | |
+ findWindow : function mdObserver_findWindow() { | |
+ var window = mozmill.wm.getMostRecentWindow(''); | |
+ | |
+ return window && | |
+ (window.opener == this.controller.window) && | |
+ ("documentLoaded" in window); | |
}, | |
- observe : function (subject, topic, data) | |
- { | |
- if (this.docFinder()) { | |
+ observe : function (subject, topic, data) { | |
+ // Check if the window has been found | |
+ if (this.findWindow()) { | |
try { | |
var window = mozmill.wm.getMostRecentWindow(""); | |
- this.handler(new mozmill.controller.MozMillController(window)); | |
+ this.callback(new mozmill.controller.MozMillController(window)); | |
} catch (ex) { | |
- window.close(); | |
- frame.events.fail({'function':ex}); | |
+ window.close(); | |
+ this.exception = ex; | |
} | |
+ | |
+ this.finished = true; | |
} else { | |
// try again in a bit | |
- this.startTimer(null, this); | |
+ this.timer.init(this, 100, Ci.nsITimer.TYPE_ONE_SHOT); | |
} | |
- }, | |
+ } | |
+}; | |
- handler: null, | |
- startTimer: null, | |
- docFinder: null | |
-}; | |
/** | |
* Create a new modalDialog instance. | |
* | |
* @class A class to handle modal dialogs | |
* @constructor | |
- * @param {function} callback | |
- * The callback handler to use to interact with the modal dialog | |
+ * | |
+ * @param {MozMillController} aController | |
+ * MozMillController which is the opener of the modal dialog | |
*/ | |
-function modalDialog(callback) | |
-{ | |
+function modalDialog(aController) { | |
this.observer = mdObserver; | |
- this.observer.handler = callback; | |
- this.observer.startTimer = this.start; | |
- this.observer.docFinder = this.getDialog; | |
+ this.observer.controller = aController; | |
+ | |
+ this.observer.timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); | |
} | |
-/** | |
- * Set a new callback handler. | |
- * | |
- * @param {function} callback | |
- * The callback handler to use to interact with the modal dialog | |
- */ | |
-modalDialog.prototype.setHandler = function modalDialog_setHandler(callback) | |
-{ | |
- this.observer.handler = callback; | |
-} | |
+modalDialog.prototype = { | |
-/** | |
- * Start timer to wait for the modal dialog. | |
- * | |
- * @param {Number} delay | |
- * Initial delay before the observer gets called | |
- * @param {object} observer | |
- * (Optional) Observer for modal dialog checks | |
- */ | |
-modalDialog.prototype.start = function modalDialog_start(delay, observer) | |
-{ | |
- const dialogDelay = (delay == undefined) ? 100 : delay; | |
+ /** | |
+ * Start timer to wait for the modal dialog. | |
+ * | |
+ * @param {function} aCallback | |
+ * The callback handler to use to interact with the modal dialog | |
+ */ | |
+ start : function modalDialog_start(aCallback) { | |
+ this.observer.callback = aCallback; | |
- var modalDialogTimer = Cc["@mozilla.org/timer;1"]. | |
- createInstance(Ci.nsITimer); | |
+ this.observer.exception = null; | |
+ this.observer.finished = false; | |
+ this.observer.timer.init(this.observer, 100, Ci.nsITimer.TYPE_ONE_SHOT); | |
+ }, | |
- // If we are not called from the observer, we have to use the supplied | |
- // observer instead of this.observer | |
- if (observer) { | |
- modalDialogTimer.init(observer, | |
- dialogDelay, | |
- Ci.nsITimer.TYPE_ONE_SHOT); | |
- } else { | |
- modalDialogTimer.init(this.observer, | |
- dialogDelay, | |
- Ci.nsITimer.TYPE_ONE_SHOT); | |
+ /** | |
+ * Wait until the modal dialog has been closed | |
+ */ | |
+ waitForClosed : function modalDialog_waitForClosed() { | |
+ this.observer.controller.waitFor(function () { | |
+ return this.observer.finished; | |
+ }, "Modal dialog has been closed", undefined, undefined, this); | |
+ | |
+ if (this.observer.exception) { | |
+ throw this.observer.exception; | |
+ } | |
} | |
} | |
-/** | |
- * Check if the modal dialog has been opened | |
- * | |
- * @private | |
- * @return Returns if the modal dialog has been found or not | |
- * @type Boolean | |
- */ | |
-modalDialog.prototype.getDialog = function modalDialog_getDialog() | |
-{ | |
- var enumerator = mozmill.wm.getXULWindowEnumerator(""); | |
- | |
- // Find the <browser> which contains notifyWindow, by looking | |
- // through all the open windows and all the <browsers> in each. | |
- while (enumerator.hasMoreElements()) { | |
- var win = enumerator.getNext(); | |
- var windowDocShell = win.QueryInterface(Ci.nsIXULWindow).docShell; | |
- | |
- var containedDocShells = windowDocShell.getDocShellEnumerator( | |
- Ci.nsIDocShellTreeItem.typeChrome, | |
- Ci.nsIDocShell.ENUMERATE_FORWARDS); | |
- | |
- while (containedDocShells.hasMoreElements()) { | |
- // Get the corresponding document for this docshell | |
- var childDocShell = containedDocShells.getNext(); | |
- | |
- // We don't want it if it's not done loading. | |
- if (childDocShell.busyFlags != Ci.nsIDocShell.BUSY_FLAGS_NONE) | |
- continue; | |
- | |
- // Ensure that we are only returning true if it is indeed the modal | |
- // dialog we were looking for. | |
- var chrome = win.QueryInterface(Ci.nsIInterfaceRequestor). | |
- getInterface(Ci.nsIWebBrowserChrome); | |
- if (chrome.isWindowModal()) { | |
- return true; | |
- } | |
- } | |
- } | |
- | |
- return false; | |
-} | |
// Export of classes | |
exports.modalDialog = modalDialog; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment