2015-02-23 07:11:22 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This module implements a number of utilities useful for testing.
|
|
|
|
*
|
|
|
|
* Additions to this module should be generic and useful to testing multiple
|
|
|
|
* features. Utilities only useful to a sepcific testing framework should live
|
|
|
|
* in a module specific to that framework, such as BrowserTestUtils.jsm in the
|
|
|
|
* case of mochitest-browser-chrome.
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = [
|
|
|
|
"TestUtils",
|
|
|
|
];
|
|
|
|
|
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
|
|
|
this.TestUtils = {
|
|
|
|
executeSoon(callbackFn) {
|
2017-04-14 19:29:12 +03:00
|
|
|
Services.tm.dispatchToMainThread(callbackFn);
|
2015-02-23 07:11:22 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Waits for the specified topic to be observed.
|
|
|
|
*
|
|
|
|
* @param {string} topic
|
|
|
|
* The topic to observe.
|
2015-03-13 18:54:43 +03:00
|
|
|
* @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.
|
|
|
|
*
|
2015-02-23 07:11:22 +03:00
|
|
|
* @return {Promise}
|
2015-03-13 18:54:43 +03:00
|
|
|
* @resolves The array [subject, data] from the observed notification.
|
2015-02-23 07:11:22 +03:00
|
|
|
*/
|
2015-03-13 18:54:43 +03:00
|
|
|
topicObserved(topic, checkFn) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Services.obs.addObserver(function observer(subject, topic, data) {
|
|
|
|
try {
|
2015-03-20 20:26:01 +03:00
|
|
|
if (checkFn && !checkFn(subject, data)) {
|
|
|
|
return;
|
2015-03-13 18:54:43 +03:00
|
|
|
}
|
2015-03-20 20:26:01 +03:00
|
|
|
Services.obs.removeObserver(observer, topic);
|
2015-03-13 18:54:43 +03:00
|
|
|
resolve([subject, data]);
|
|
|
|
} catch (ex) {
|
2015-03-20 20:26:01 +03:00
|
|
|
Services.obs.removeObserver(observer, topic);
|
2015-03-13 18:54:43 +03:00
|
|
|
reject(ex);
|
|
|
|
}
|
2017-04-14 22:51:38 +03:00
|
|
|
}, topic);
|
2015-02-23 07:11:22 +03:00
|
|
|
});
|
|
|
|
},
|
2017-04-19 12:41:49 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a screenshot of an area and returns it as a data URL.
|
|
|
|
*
|
|
|
|
* @param eltOrRect
|
|
|
|
* The DOM node or rect ({left, top, width, height}) to screenshot.
|
|
|
|
* @param win
|
|
|
|
* The current window.
|
|
|
|
*/
|
|
|
|
screenshotArea(eltOrRect, win) {
|
|
|
|
if (eltOrRect instanceof Ci.nsIDOMElement) {
|
|
|
|
eltOrRect = eltOrRect.getBoundingClientRect();
|
|
|
|
}
|
|
|
|
let { left, top, width, height } = eltOrRect;
|
|
|
|
let canvas = win.document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
|
|
|
|
let ctx = canvas.getContext("2d");
|
|
|
|
let ratio = win.devicePixelRatio;
|
|
|
|
canvas.width = width * ratio;
|
|
|
|
canvas.height = height * ratio;
|
|
|
|
ctx.scale(ratio, ratio);
|
|
|
|
ctx.drawWindow(win, left, top, width, height, "#fff");
|
|
|
|
return canvas.toDataURL();
|
|
|
|
}
|
2015-02-23 07:11:22 +03:00
|
|
|
};
|