2015-05-13 09:04:51 +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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Util base class to help test a captured canvas element. Initializes the
|
|
|
|
* output canvas (used for testing the color of video elements), and optionally
|
2017-09-14 19:57:12 +03:00
|
|
|
* overrides the default `createAndAppendElement` element |width| and |height|.
|
2015-05-13 09:04:51 +03:00
|
|
|
*/
|
|
|
|
function CaptureStreamTestHelper(width, height) {
|
|
|
|
if (width) {
|
|
|
|
this.elemWidth = width;
|
|
|
|
}
|
|
|
|
if (height) {
|
|
|
|
this.elemHeight = height;
|
|
|
|
}
|
2017-09-14 19:57:12 +03:00
|
|
|
|
|
|
|
/* cout is used for `getPixel`; only needs to be big enough for one pixel */
|
|
|
|
this.cout = document.createElement('canvas');
|
|
|
|
this.cout.width = 1;
|
|
|
|
this.cout.height = 1;
|
2015-05-13 09:04:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CaptureStreamTestHelper.prototype = {
|
|
|
|
/* Predefined colors for use in the methods below. */
|
|
|
|
black: { data: [0, 0, 0, 255], name: "black" },
|
2015-11-09 18:17:41 +03:00
|
|
|
blackTransparent: { data: [0, 0, 0, 0], name: "blackTransparent" },
|
2015-05-13 09:04:51 +03:00
|
|
|
green: { data: [0, 255, 0, 255], name: "green" },
|
|
|
|
red: { data: [255, 0, 0, 255], name: "red" },
|
2016-01-14 19:56:17 +03:00
|
|
|
blue: { data: [0, 0, 255, 255], name: "blue"},
|
2015-11-17 06:34:28 +03:00
|
|
|
grey: { data: [128, 128, 128, 255], name: "grey" },
|
2015-05-13 09:04:51 +03:00
|
|
|
|
|
|
|
/* Default element size for createAndAppendElement() */
|
|
|
|
elemWidth: 100,
|
|
|
|
elemHeight: 100,
|
|
|
|
|
2015-09-17 06:37:05 +03:00
|
|
|
/*
|
|
|
|
* Perform the drawing operation on each animation frame until stop is called
|
|
|
|
* on the returned object.
|
|
|
|
*/
|
|
|
|
startDrawing: function (f) {
|
|
|
|
var stop = false;
|
|
|
|
var draw = () => {
|
2017-06-27 20:55:45 +03:00
|
|
|
if (stop) { return; }
|
2015-09-17 06:37:05 +03:00
|
|
|
f();
|
2017-06-27 20:55:45 +03:00
|
|
|
window.requestAnimationFrame(draw);
|
2015-09-17 06:37:05 +03:00
|
|
|
};
|
|
|
|
draw();
|
|
|
|
return { stop: () => stop = true };
|
|
|
|
},
|
|
|
|
|
2015-05-13 09:04:51 +03:00
|
|
|
/* Request a frame from the stream played by |video|. */
|
|
|
|
requestFrame: function (video) {
|
|
|
|
info("Requesting frame from " + video.id);
|
2015-07-14 17:12:31 +03:00
|
|
|
video.srcObject.requestFrame();
|
2015-05-13 09:04:51 +03:00
|
|
|
},
|
|
|
|
|
2015-11-09 18:17:41 +03:00
|
|
|
/*
|
|
|
|
* Returns the pixel at (|offsetX|, |offsetY|) (from top left corner) of
|
2017-09-14 19:57:12 +03:00
|
|
|
* |video| as an array of the pixel's color channels: [R,G,B,A].
|
2015-11-09 18:17:41 +03:00
|
|
|
*/
|
2017-09-14 19:57:12 +03:00
|
|
|
getPixel: function (video, offsetX = 0, offsetY = 0) {
|
2016-01-05 05:16:32 +03:00
|
|
|
// Avoids old values in case of a transparent image.
|
|
|
|
CaptureStreamTestHelper2D.prototype.clear.call(this, this.cout);
|
|
|
|
|
2015-05-13 09:04:51 +03:00
|
|
|
var ctxout = this.cout.getContext('2d');
|
2017-09-14 19:57:12 +03:00
|
|
|
ctxout.drawImage(video,
|
|
|
|
offsetX, // source x coordinate
|
|
|
|
offsetY, // source y coordinate
|
|
|
|
1, // source width
|
|
|
|
1, // source height
|
|
|
|
0, // destination x coordinate
|
|
|
|
0, // destination y coordinate
|
|
|
|
1, // destination width
|
|
|
|
1); // destination height
|
|
|
|
return ctxout.getImageData(0, 0, 1, 1).data;
|
2015-11-09 18:17:41 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true if px lies within the per-channel |threshold| of the
|
|
|
|
* referenced color for all channels. px is on the form of an array of color
|
|
|
|
* channels, [R,G,B,A]. Each channel is in the range [0, 255].
|
2017-05-29 17:27:45 +03:00
|
|
|
*
|
|
|
|
* Threshold defaults to 0 which is an exact match.
|
2015-11-09 18:17:41 +03:00
|
|
|
*/
|
2017-05-29 17:27:45 +03:00
|
|
|
isPixel: function (px, refColor, threshold = 0) {
|
2015-11-09 18:17:41 +03:00
|
|
|
return px.every((ch, i) => Math.abs(ch - refColor.data[i]) <= threshold);
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true if px lies further away than |threshold| of the
|
|
|
|
* referenced color for any channel. px is on the form of an array of color
|
|
|
|
* channels, [R,G,B,A]. Each channel is in the range [0, 255].
|
2017-05-29 17:27:45 +03:00
|
|
|
*
|
|
|
|
* Threshold defaults to 127 which should be far enough for most cases.
|
2015-11-09 18:17:41 +03:00
|
|
|
*/
|
2017-05-29 17:27:45 +03:00
|
|
|
isPixelNot: function (px, refColor, threshold = 127) {
|
2015-11-09 18:17:41 +03:00
|
|
|
return px.some((ch, i) => Math.abs(ch - refColor.data[i]) > threshold);
|
2015-05-13 09:04:51 +03:00
|
|
|
},
|
|
|
|
|
2016-08-18 18:37:19 +03:00
|
|
|
/*
|
|
|
|
* Behaves like isPixelNot but ignores the alpha channel.
|
|
|
|
*/
|
|
|
|
isOpaquePixelNot: function(px, refColor, threshold) {
|
|
|
|
px[3] = refColor.data[3];
|
2017-05-04 16:23:36 +03:00
|
|
|
return this.isPixelNot(px, refColor, threshold);
|
2016-08-18 18:37:19 +03:00
|
|
|
},
|
|
|
|
|
2015-05-13 09:04:51 +03:00
|
|
|
/*
|
2015-11-09 18:17:41 +03:00
|
|
|
* Returns a promise that resolves when the provided function |test|
|
2017-05-29 17:27:45 +03:00
|
|
|
* returns true, or rejects when the optional `cancel` promise resolves.
|
2015-05-13 09:04:51 +03:00
|
|
|
*/
|
2017-05-29 17:27:45 +03:00
|
|
|
waitForPixel: async function (video, test, {
|
|
|
|
offsetX = 0, offsetY = 0,
|
|
|
|
width = 0, height = 0,
|
|
|
|
cancel = new Promise(() => {}),
|
|
|
|
} = {}) {
|
|
|
|
let aborted = false;
|
|
|
|
cancel.then(e => aborted = true);
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
await Promise.race([
|
|
|
|
new Promise(resolve => video.addEventListener("timeupdate", resolve, { once: true })),
|
|
|
|
cancel,
|
|
|
|
]);
|
|
|
|
if (aborted) {
|
|
|
|
throw await cancel;
|
|
|
|
}
|
2018-11-23 18:01:07 +03:00
|
|
|
if (test(this.getPixel(video, offsetX, offsetY, width, height))) {
|
|
|
|
return;
|
2017-05-29 17:27:45 +03:00
|
|
|
}
|
|
|
|
}
|
2015-05-13 09:04:51 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
2015-11-09 18:17:41 +03:00
|
|
|
* Returns a promise that resolves when the top left pixel of |video| matches
|
|
|
|
* on all channels. Use |threshold| for fuzzy matching the color on each
|
2017-05-29 17:27:45 +03:00
|
|
|
* channel, in the range [0,255]. 0 means exact match, 255 accepts anything.
|
2015-11-09 18:17:41 +03:00
|
|
|
*/
|
2017-05-29 17:27:45 +03:00
|
|
|
pixelMustBecome: async function (video, refColor, {
|
|
|
|
threshold = 0, infoString = "n/a",
|
|
|
|
cancel = new Promise(() => {}),
|
|
|
|
} = {}) {
|
2015-11-09 18:17:41 +03:00
|
|
|
info("Waiting for video " + video.id + " to match [" +
|
|
|
|
refColor.data.join(',') + "] - " + refColor.name +
|
|
|
|
" (" + infoString + ")");
|
2016-12-28 03:41:02 +03:00
|
|
|
var paintedFrames = video.mozPaintedFrames-1;
|
2017-05-29 17:27:45 +03:00
|
|
|
await this.waitForPixel(video, px => {
|
|
|
|
if (paintedFrames != video.mozPaintedFrames) {
|
|
|
|
info("Frame: " + video.mozPaintedFrames +
|
|
|
|
" IsPixel ref=" + refColor.data +
|
|
|
|
" threshold=" + threshold +
|
|
|
|
" value=" + px);
|
|
|
|
paintedFrames = video.mozPaintedFrames;
|
|
|
|
}
|
|
|
|
return this.isPixel(px, refColor, threshold);
|
|
|
|
}, {
|
|
|
|
offsetX: 0, offsetY: 0,
|
|
|
|
width: 0, height: 0,
|
|
|
|
cancel,
|
|
|
|
});
|
|
|
|
ok(true, video.id + " " + infoString);
|
2015-11-09 18:17:41 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
2017-05-29 17:27:45 +03:00
|
|
|
* Returns a promise that resolves after |time| ms of playback or when the
|
2015-11-09 18:17:41 +03:00
|
|
|
* top left pixel of |video| becomes |refColor|. The test is failed if the
|
2017-05-29 17:27:45 +03:00
|
|
|
* time is not reached, or if the cancel promise resolves.
|
2015-05-13 09:04:51 +03:00
|
|
|
*/
|
2017-05-29 17:27:45 +03:00
|
|
|
pixelMustNotBecome: async function (video, refColor, {
|
|
|
|
threshold = 0, time = 5000,
|
|
|
|
infoString = "n/a",
|
|
|
|
} = {}) {
|
|
|
|
info("Waiting for " + video.id + " to time out after " + time +
|
2015-11-09 18:17:41 +03:00
|
|
|
"ms against [" + refColor.data.join(',') + "] - " + refColor.name);
|
2017-05-29 17:27:45 +03:00
|
|
|
let timeout = new Promise(resolve => setTimeout(resolve, time));
|
|
|
|
let analysis = async () => {
|
|
|
|
await this.waitForPixel(video, px => this.isPixel(px, refColor, threshold), {
|
|
|
|
offsetX: 0, offsetY: 0, width: 0, height: 0,
|
|
|
|
});
|
|
|
|
throw new Error("Got color " + refColor.name + ". " + infoString);
|
|
|
|
};
|
|
|
|
await Promise.race([timeout, analysis()]);
|
|
|
|
ok(true, video.id + " " + infoString);
|
2015-05-13 09:04:51 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/* Create an element of type |type| with id |id| and append it to the body. */
|
|
|
|
createAndAppendElement: function (type, id) {
|
|
|
|
var e = document.createElement(type);
|
|
|
|
e.id = id;
|
|
|
|
e.width = this.elemWidth;
|
|
|
|
e.height = this.elemHeight;
|
|
|
|
if (type === 'video') {
|
|
|
|
e.autoplay = true;
|
|
|
|
}
|
|
|
|
document.body.appendChild(e);
|
|
|
|
return e;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sub class holding 2D-Canvas specific helpers. */
|
|
|
|
function CaptureStreamTestHelper2D(width, height) {
|
|
|
|
CaptureStreamTestHelper.call(this, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
CaptureStreamTestHelper2D.prototype = Object.create(CaptureStreamTestHelper.prototype);
|
|
|
|
CaptureStreamTestHelper2D.prototype.constructor = CaptureStreamTestHelper2D;
|
|
|
|
|
|
|
|
/* Clear all drawn content on |canvas|. */
|
|
|
|
CaptureStreamTestHelper2D.prototype.clear = function(canvas) {
|
|
|
|
var ctx = canvas.getContext('2d');
|
|
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Draw the color |color| to the source canvas |canvas|. Format [R,G,B,A]. */
|
2017-09-14 20:00:20 +03:00
|
|
|
CaptureStreamTestHelper2D.prototype.drawColor = function(canvas, color,
|
|
|
|
{ offsetX = 0,
|
|
|
|
offsetY = 0,
|
|
|
|
width = canvas.width / 2,
|
|
|
|
height = canvas.height / 2,
|
|
|
|
} = {}) {
|
2015-05-13 09:04:51 +03:00
|
|
|
var ctx = canvas.getContext('2d');
|
|
|
|
var rgba = color.data.slice(); // Copy to not overwrite the original array
|
|
|
|
rgba[3] = rgba[3] / 255.0; // Convert opacity to double in range [0,1]
|
2015-09-17 06:37:05 +03:00
|
|
|
info("Drawing color " + rgba.join(','));
|
2015-05-13 09:04:51 +03:00
|
|
|
ctx.fillStyle = "rgba(" + rgba.join(',') + ")";
|
|
|
|
|
|
|
|
// Only fill top left corner to test that output is not flipped or rotated.
|
2017-09-14 20:00:20 +03:00
|
|
|
ctx.fillRect(offsetX, offsetY, width, height);
|
2015-05-13 09:04:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Test that the given 2d canvas is NOT origin-clean. */
|
|
|
|
CaptureStreamTestHelper2D.prototype.testNotClean = function(canvas) {
|
|
|
|
var ctx = canvas.getContext('2d');
|
|
|
|
var error = "OK";
|
|
|
|
try {
|
|
|
|
var data = ctx.getImageData(0, 0, 1, 1);
|
|
|
|
} catch(e) {
|
|
|
|
error = e.name;
|
|
|
|
}
|
|
|
|
is(error, "SecurityError",
|
|
|
|
"Canvas '" + canvas.id + "' should not be origin-clean");
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Sub class holding WebGL specific helpers. */
|
|
|
|
function CaptureStreamTestHelperWebGL(width, height) {
|
|
|
|
CaptureStreamTestHelper.call(this, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
CaptureStreamTestHelperWebGL.prototype = Object.create(CaptureStreamTestHelper.prototype);
|
|
|
|
CaptureStreamTestHelperWebGL.prototype.constructor = CaptureStreamTestHelperWebGL;
|
|
|
|
|
|
|
|
/* Set the (uniform) color location for future draw calls. */
|
|
|
|
CaptureStreamTestHelperWebGL.prototype.setFragmentColorLocation = function(colorLocation) {
|
|
|
|
this.colorLocation = colorLocation;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Clear the given WebGL context with |color|. */
|
|
|
|
CaptureStreamTestHelperWebGL.prototype.clearColor = function(canvas, color) {
|
|
|
|
info("WebGL: clearColor(" + color.name + ")");
|
|
|
|
var gl = canvas.getContext('webgl');
|
|
|
|
var conv = color.data.map(i => i / 255.0);
|
|
|
|
gl.clearColor(conv[0], conv[1], conv[2], conv[3]);
|
|
|
|
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Set an already setFragmentColorLocation() to |color| and drawArrays() */
|
|
|
|
CaptureStreamTestHelperWebGL.prototype.drawColor = function(canvas, color) {
|
|
|
|
info("WebGL: drawArrays(" + color.name + ")");
|
|
|
|
var gl = canvas.getContext('webgl');
|
|
|
|
var conv = color.data.map(i => i / 255.0);
|
|
|
|
gl.uniform4f(this.colorLocation, conv[0], conv[1], conv[2], conv[3]);
|
|
|
|
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
|
|
|
|
};
|