Bug 1142108 - Make TestUtils.topicObserved cover more use cases. r=smacleod

This commit is contained in:
Paolo Amadini 2015-03-13 15:54:43 +00:00
Родитель 8af71d403d
Коммит 3b18d2938b
2 изменённых файлов: 41 добавлений и 29 удалений

Просмотреть файл

@ -69,26 +69,24 @@ this.BrowserTestUtils = {
* Resolves with the new window once it is loaded.
*/
openNewBrowserWindow(options) {
return new Promise(resolve => {
let argString = Cc["@mozilla.org/supports-string;1"].
createInstance(Ci.nsISupportsString);
argString.data = "";
let features = "chrome,dialog=no,all";
let argString = Cc["@mozilla.org/supports-string;1"].
createInstance(Ci.nsISupportsString);
argString.data = "";
let features = "chrome,dialog=no,all";
if (options && options.private || false) {
features += ",private";
}
if (options && options.private || false) {
features += ",private";
}
let win = Services.ww.openWindow(
null, Services.prefs.getCharPref("browser.chromeURL"), "_blank",
features, argString);
let win = Services.ww.openWindow(
null, Services.prefs.getCharPref("browser.chromeURL"), "_blank",
features, argString);
// Wait for browser-delayed-startup-finished notification, it indicates
// that the window has loaded completely and is ready to be used for
// testing.
TestUtils.topicObserved("browser-delayed-startup-finished", win).then(
() => resolve(win));
});
// Wait for browser-delayed-startup-finished notification, it indicates
// that the window has loaded completely and is ready to be used for
// testing.
return TestUtils.topicObserved("browser-delayed-startup-finished",
subject => subject == win).then(() => win);
},
/**

Просмотреть файл

@ -32,20 +32,34 @@ this.TestUtils = {
*
* @param {string} topic
* The topic to observe.
* @param {*} subject
* A value to check the notification subject against. Only a
* notification with a matching subject will cause the promise to
* resolve.
* @param {function} checkFn [optional]
* Called with (subject, data) as arguments, should return true if the
* notification is the expected one, or false if it should be ignored
* and listening should continue. If not specified, the first
* notification for the specified topic resolves the returned promise.
*
* @note Because this function is intended for testing, any error in checkFn
* will cause the returned promise to be rejected instead of waiting for
* the next notification, since this is probably a bug in the test.
*
* @return {Promise}
* Resolves with the data provided when the topic has been observed.
* @resolves The array [subject, data] from the observed notification.
*/
topicObserved(topic, subject=null) {
return new Promise(resolve => {
Services.obs.addObserver(function observer(observedSubject, topic, data) {
if (subject !== null && subject !== observedSubject) { return; }
Services.obs.removeObserver(observer, topic);
resolve(data);
topicObserved(topic, checkFn) {
return new Promise((resolve, reject) => {
Services.obs.addObserver(function observer(subject, topic, data) {
try {
try {
if (checkFn && !checkFn(subject, data)) {
return;
}
} finally {
Services.obs.removeObserver(observer, topic);
}
resolve([subject, data]);
} catch (ex) {
reject(ex);
}
}, topic, false);
});
},