2012-06-10 23:44:50 +00:00
|
|
|
/* Any copyright is dedicated to the public domain.
|
|
|
|
http://creativecommons.org/publicdomain/zero/1.0/ */
|
2012-06-09 16:05:31 +00:00
|
|
|
|
2012-06-10 23:44:50 +00:00
|
|
|
// Test the getScreenshot property for mozbrowser
|
2012-05-19 00:47:20 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
SimpleTest.waitForExplicitFinish();
|
|
|
|
|
|
|
|
function runTest() {
|
|
|
|
|
2012-06-10 23:44:50 +00:00
|
|
|
browserElementTestHelpers.setEnabledPref(true);
|
2012-08-15 17:22:30 +00:00
|
|
|
browserElementTestHelpers.addPermission();
|
2012-05-19 00:47:20 +00:00
|
|
|
|
|
|
|
var iframe1 = document.createElement('iframe');
|
|
|
|
iframe1.mozbrowser = true;
|
|
|
|
document.body.appendChild(iframe1);
|
2012-05-31 02:15:12 +00:00
|
|
|
iframe1.src = 'data:text/html,<html>' +
|
|
|
|
'<body style="background:green">hello</body></html>';
|
2012-05-19 00:47:20 +00:00
|
|
|
|
2012-10-31 23:28:11 +00:00
|
|
|
var screenshotArrayBuffers = [];
|
2012-05-19 00:47:20 +00:00
|
|
|
var numLoaded = 0;
|
|
|
|
|
2012-10-31 23:28:11 +00:00
|
|
|
function screenshotTaken(screenshotArrayBuffer) {
|
|
|
|
screenshotArrayBuffers.push(screenshotArrayBuffer);
|
|
|
|
if (screenshotArrayBuffers.length === 1) {
|
2012-05-31 02:15:12 +00:00
|
|
|
ok(true, 'Got initial non blank screenshot');
|
2012-05-19 00:47:20 +00:00
|
|
|
iframe1.src = 'data:text/html,<html>' +
|
|
|
|
'<body style="background:blue">hello</body></html>';
|
2012-10-31 23:28:11 +00:00
|
|
|
|
|
|
|
// Wait until screenshotArrayBuffer !== screenshotArrayBuffers[0].
|
|
|
|
waitForScreenshot(function(screenshotArrayBuffer) {
|
|
|
|
var view1 = new Int8Array(screenshotArrayBuffer);
|
|
|
|
var view2 = new Int8Array(screenshotArrayBuffers[0]);
|
|
|
|
if (view1.length != view2.length) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < view1.length; i++) {
|
|
|
|
if (view1[i] != view2[i]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2012-05-31 02:15:12 +00:00
|
|
|
});
|
2012-05-19 00:47:20 +00:00
|
|
|
}
|
2012-10-31 23:28:11 +00:00
|
|
|
else if (screenshotArrayBuffers.length === 2) {
|
2012-05-31 02:15:12 +00:00
|
|
|
ok(true, 'Got updated screenshot after source page changed');
|
2012-05-19 00:47:20 +00:00
|
|
|
SimpleTest.finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-31 02:15:12 +00:00
|
|
|
// We continually take screenshots until we get one that we are
|
2012-10-17 04:23:08 +00:00
|
|
|
// happy with.
|
2012-05-31 02:15:12 +00:00
|
|
|
function waitForScreenshot(filter) {
|
2012-10-31 23:28:11 +00:00
|
|
|
function gotScreenshotArrayBuffer() {
|
|
|
|
// |this| is the FileReader whose result contains the screenshot as an
|
|
|
|
// ArrayBuffer.
|
2012-05-31 02:15:12 +00:00
|
|
|
|
2012-10-31 23:28:11 +00:00
|
|
|
if (filter(this.result)) {
|
|
|
|
screenshotTaken(this.result);
|
2012-05-31 02:15:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (--attempts === 0) {
|
|
|
|
ok(false, 'Timed out waiting for correct screenshot');
|
|
|
|
SimpleTest.finish();
|
|
|
|
} else {
|
2012-10-31 23:28:11 +00:00
|
|
|
setTimeout(function() {
|
|
|
|
iframe1.getScreenshot(1000, 1000).onsuccess = getScreenshotArrayBuffer;
|
2012-05-31 02:15:12 +00:00
|
|
|
}, 200);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-31 23:28:11 +00:00
|
|
|
function getScreenshotArrayBuffer(e) {
|
|
|
|
var fr = new FileReader();
|
|
|
|
fr.onloadend = gotScreenshotArrayBuffer;
|
|
|
|
fr.readAsArrayBuffer(e.target.result);
|
|
|
|
}
|
|
|
|
|
2012-05-31 02:15:12 +00:00
|
|
|
var attempts = 10;
|
2012-10-31 23:28:11 +00:00
|
|
|
iframe1.getScreenshot(1000, 1000).onsuccess = getScreenshotArrayBuffer;
|
2012-05-24 03:57:26 +00:00
|
|
|
}
|
|
|
|
|
2012-05-19 00:47:20 +00:00
|
|
|
function iframeLoadedHandler() {
|
|
|
|
numLoaded++;
|
2012-05-31 02:15:12 +00:00
|
|
|
if (numLoaded === 2) {
|
2012-10-31 23:28:11 +00:00
|
|
|
waitForScreenshot(function(screenshotArrayBuffer) {
|
|
|
|
return screenshotArrayBuffer.byteLength != 0;
|
2012-05-31 02:15:12 +00:00
|
|
|
});
|
2012-05-19 00:47:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
iframe1.addEventListener('mozbrowserloadend', iframeLoadedHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
addEventListener('load', function() { SimpleTest.executeSoon(runTest); });
|