rewrote screenshot logic

This commit is contained in:
sebv
2014-03-28 23:56:50 +08:00
parent b19d92efd7
commit 5e44dee358
+63 -57
View File
@@ -925,17 +925,29 @@ iOSController.localScreenshot = function (desiredFile, cb) {
function (cb) { this.proxy(command, cb); }.bind(this),
function (cb) {
var srcFile = filePath + ".png";
var waitForFile = function () {
if (fs.existsSync(srcFile)) {
var screenshotExists = null;
var count = 0;
async.until(
function () { return screenshotExists; },
function (cb) {
setTimeout(function () {
fs.exists(srcFile, function (exists) {
if (!exists && ++count > 20) {
return cb(new Error("Timed out waiting for screenshot file."));
}
screenshotExists = exists;
cb();
});
}, 300);
},
function (err) {
if (err) return cb(err);
var desiredFolder = path.dirname(desiredFile);
mkdirp.sync(desiredFolder);
fs.rename(filePath + ".png", desiredFile, cb);
} else {
setTimeout(waitForFile, 500);
fs.rename(srcFile, desiredFile, cb);
}
};
waitForFile();
// must exist or rename will fail.
);
},
], function () {
cb(null, {
@@ -955,58 +967,52 @@ iOSController.getScreenshot = function (cb) {
}
var shotPath = [screenshotFolder, 'screenshot', guid, ".png"].join("");
this.proxy(command, function (err, response) {
if (err) {
cb(err, response);
} else {
var delayTimes = 0;
var onErr = function () {
delayTimes++;
var next = function () {
if (delayTimes <= 10) {
read(onErr);
} else {
read();
}
};
setTimeout(next, 300);
};
var read = function (onErr) {
var doRead = function () {
fs.readFile(shotPath, function read(err, data) {
if (err) {
if (onErr) {
return onErr();
} else {
response = null;
err = new Error("Timed out waiting for screenshot file. " + err.toString());
async.waterfall([
function (cb) { this.proxy(command, cb); }.bind(this),
function (response, cb) {
var data = null;
var count = 0;
async.until(
function () { return data; },
function (cb) {
setTimeout(function () {
fs.readFile(shotPath, function (err, _data) {
if (err && ++count > 20) {
return cb(new Error("Timed out waiting for screenshot file. " + err.toString()));
}
} else {
var b64data = new Buffer(data).toString('base64');
response.value = b64data;
}
cb(err, response);
});
};
if (this.curOrientation === "LANDSCAPE") {
// need to rotate 90 deg CC
logger.info("Rotating landscape screenshot");
rotateImage(shotPath, -90, function (err) {
if (err && onErr) {
return onErr();
} else if (err) {
cb(new Error("Could not rotate screenshot appropriately"), null);
} else {
doRead();
}
});
} else {
doRead();
if (!err) {
data = _data;
}
cb();
});
}, 300);
},
function (err) {
cb(err, response, data);
}
}.bind(this);
read(onErr);
);
}.bind(this),
function (response, data, cb) {
// rotate if necessary
if (this.curOrientation === "LANDSCAPE") {
// need to rotate 90 deg CC
logger.info("Rotating landscape screenshot");
rotateImage(shotPath, -90, function (err) {
if (err) return cb(new Error("Could not rotate screenshot appropriately"), null);
fs.readFile(shotPath, function read(err, _data) {
if (err) return cb(new Error("Could not retrieve screenshot file following rotate. " + err.toString()));
cb(null, response, _data);
});
});
} else cb(null, response, data);
}.bind(this),
function (response, data, cb) {
var b64data = new Buffer(data).toString('base64');
response.value = b64data;
cb(null, response);
}
}.bind(this));
], cb);
};
iOSController.fakeFlick = function (xSpeed, ySpeed, swipe, cb) {