1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-23 05:10:27 +01:00

Detection test: added test for function code and for known pixel values.

This commit is contained in:
kkapsner 2017-07-18 16:19:56 +02:00
parent b7acfbb2f9
commit 45d5576eaf

View File

@ -32,15 +32,14 @@ var addTest = (function(){
return function addTest(name, func){ return function addTest(name, func){
var logs = []; var logs = [];
function log(){ function log(){
for (var i = 0; i < arguments.length; i += 1){ logs.push(Array.prototype.slice.call(arguments).join(" "));
logs.push(arguments[i]);
}
} }
var status = 0; var status = 0;
try { try {
status = func(log)? 1: 2; status = func(log)? 1: 2;
} }
catch (e){ catch (e){
console.log(e);
status = 3; status = 3;
} }
var li = document.createElement("li"); var li = document.createElement("li");
@ -60,7 +59,24 @@ var addTest = (function(){
} }
}()); }());
addTest("function length", function(){return CanvasRenderingContext2D.prototype.getImageData.length === 0;}); addTest("function length", function(log){
if (CanvasRenderingContext2D.prototype.getImageData.length !== 4){
log("expected 4 parameters for getImageData - got", CanvasRenderingContext2D.prototype.getImageData.length);
return true;
}
else {
return false;
}
});
addTest("function code", function(log){
if (!CanvasRenderingContext2D.prototype.getImageData.toString().match(/^\s*function getImageData\s*\(\)\s*\{\s*\[native code\]\s*\}\s*$/)){
log("unexpected function code:", CanvasRenderingContext2D.prototype.getImageData.toString());
return true;
}
else {
return false;
}
});
addTest("function name", function(){return CanvasRenderingContext2D.prototype.getImageData.name !== "getImageData"}); addTest("function name", function(){return CanvasRenderingContext2D.prototype.getImageData.name !== "getImageData"});
addTest("error provocation 1", function(log){ addTest("error provocation 1", function(log){
var canvas = document.createElement('canvas'); var canvas = document.createElement('canvas');
@ -112,6 +128,30 @@ addTest("error provocation 3", function(log){
} }
return canvasBlocker; return canvasBlocker;
}); });
function testKnownPixelValue(size, log){
var canvas = document.createElement("canvas");
canvas.height = size;
canvas.width = size;
var context = canvas.getContext("2d");
context.fillStyle = "rgba(0, 127, 255, 1)";
var pixelValues = [0, 127, 255, 255];
context.fillRect(0, 0, canvas.width, canvas.height);
var p = context.getImageData(0, 0, canvas.width, canvas.height).data;
for (var i = 0; i < p.length; i += 1){
if (p[i] !== pixelValues[i%4]){
log("wrong value", p[i], "at", i, "expected", pixelValues[i%4]);
return true;
}
}
return false;
}
addTest("known pixel value test 1", function(log){
return testKnownPixelValue(1, log);
});
addTest("known pixel value test 10", function(log){
return testKnownPixelValue(10, log);
});
</script> </script>
</body> </body>
</html> </html>