Permalink
Browse files

Bug 795243 - utils.saveScreenshot should be synchronous

  • Loading branch information...
xabolcs authored and whimboo committed Nov 27, 2012
1 parent dbb639b commit de522004cadc2ec79c67e4510a58fefc3f9df77a
@@ -393,20 +393,10 @@ MozMillController.prototype.screenshot = function (node, name, save, highlights)
}
// Save the screenshot to disk
- let ready = false;
- let failure = false;
- function sync(status) {
- if (!Components.isSuccessCode(status))
- failure = true;
- ready = true;
- }
-
- // Save screenshot to disk and wait for ansynchronous action to be completed
- screenshot.filename = utils.saveScreenshot(screenshot.dataURL, name, sync);
- waitFor(function () {
- return ready;
- }, "Screenshot '" + screenshot.filename + "' has been saved.");
+ let {filename, failure} = utils.saveScreenshot(screenshot.dataURL, name);
+ screenshot.filename = filename;
+ screenshot.failure = failure;
if (failure) {
broker.log({'function': 'controller.screenshot()',
@@ -395,10 +395,16 @@ function takeScreenshot(node, highlights) {
}
/**
- * Takes a canvas as input and saves it to the file tempdir/name.png
- * Returns the filepath of the saved file
+ * Save the dataURL content to the specified file. It will be stored in the temporary folder.
+ *
+ * @param {String} aDataURL
+ * The dataURL to save
+ * @param {String} aFilename
+ * Target file name without extension
+ *
+ * @returns {Object} The hash containing the path of saved file, and the faillure bit
*/
-function saveScreenshot(aDataURL, aFilename, aCallback) {
+function saveScreenshot(aDataURL, aFilename) {
const FILE_PERMISSIONS = parseInt("0644", 8);
let file = Cc["@mozilla.org/file/directory_service;1"]
@@ -408,35 +414,46 @@ function saveScreenshot(aDataURL, aFilename, aCallback) {
file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, FILE_PERMISSIONS);
// Create an output stream to write to file
- var foStream = Cc["@mozilla.org/network/file-output-stream;1"]
+ let foStream = Cc["@mozilla.org/network/file-output-stream;1"]
.createInstance(Ci.nsIFileOutputStream);
foStream.init(file, 0x02 | 0x08 | 0x10, FILE_PERMISSIONS, foStream.DEFER_OPEN);
- var dataURI = NetUtil.newURI(aDataURL, "UTF8", null);
+ let dataURI = NetUtil.newURI(aDataURL, "UTF8", null);
if (!dataURI.schemeIs("data")) {
throw TypeError("aDataURL parameter has to have 'data'" +
" scheme instead of '" + dataURI.scheme + "'");
}
// Write asynchronously to buffer;
// Input and output streams are closed after write
+
+ let ready = false;
+ let failure = false;
+
+ function sync(aStatus) {
+ if (!Components.isSuccessCode(aStatus)) {
+ failure = true;
+ }
+ ready = true;
+ }
+
NetUtil.asyncFetch(dataURI, function (aInputStream, aAsyncFetchResult) {
if (!Components.isSuccessCode(aAsyncFetchResult)) {
// An error occurred!
- if (typeof(aCallback) === "function") {
- aCallback(aAsyncFetchResult);
- }
+ sync(aAsyncFetchResult);
} else {
// Consume the input stream.
NetUtil.asyncCopy(aInputStream, foStream, function (aAsyncCopyResult) {
- if (typeof(aCallback) === "function") {
- aCallback(aAsyncCopyResult);
- }
+ sync(aAsyncCopyResult);
});
}
});
- return file.path;
+ waitFor(function () {
+ return ready;
+ }, "Saving dataURL to '" + file.path + "' has been timed out.");
+
+ return {filename: file.path, failure: failure};
}
/**
@@ -12,24 +12,12 @@ var utils = {}; Cu.import('resource://mozmill/stdlib/utils.js', utils);
var testScreenshotSaveCorruption = function() {
const name = "smile5x5";
- const smile5x5DataURL = "data:image/jpeg;base64," +
+ const smile5x5DataURL = "data:image/jpeg;base64," +
"iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFAQMAAAC3obSmAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAA" +
"BlBMVEX///8AAABVwtN+AAAAF0lEQVR4XgXAgQwAAAACsCOGEkbYDcH0BEIBSflcthkAAAAASUVORK5CYII=";
- var ready = false;
- var failure = false;
-
- function sync(aResult) {
- if (!Components.isSuccessCode(aResult))
- failure = true;
- ready = true;
- }
-
- // Save dataURL to disk and wait for ansynchronous action to be completed
- var filename = utils.saveScreenshot(smile5x5DataURL, name, sync);
- utils.waitFor(function () {
- return ready;
- }, "DataURL '" + filename + "' has been saved.");
+ // Save dataURL to disk
+ var {filename, failure} = utils.saveScreenshot(smile5x5DataURL, name);
expect.ok(!failure, "No failure while saving dataURL.");

0 comments on commit de52200

Please sign in to comment.