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

Refined property descriptor detection test.

This commit is contained in:
kkapsner 2018-07-11 17:38:01 +02:00
parent 6d1c051323
commit 96118e5690

View File

@ -71,45 +71,64 @@ addTest("function name", function(){
addTest("property descriptor", function(log){ addTest("property descriptor", function(log){
"use strict"; "use strict";
var descriptor = Object.getOwnPropertyDescriptor(CanvasRenderingContext2D.prototype, "getImageData"); const properties = [
var desiredDescriptor = { {
value: function getImageData(x, y, w, h){}, object: CanvasRenderingContext2D.prototype,
writable: true, name: "getImageData",
enumerable: true, descriptor: {
configurable: true value: function getImageData(x, y, w, h){},
}; writable: true,
enumerable: true,
configurable: true
}
},
{
object: HTMLCanvasElement.prototype,
name: "toDataURL",
descriptor: {
value: function toDataURL(){},
writable: true,
enumerable: true,
configurable: true
}
},
];
return Object.keys(desiredDescriptor).reduce(function(pass, key){ return properties.reduce(function(pass, property){
function keyLog(type, expected, got){ const desiredDescriptor = property.descriptor;
log("wrong " + type + " for ", key, "- expected:", expected, "- got:", got); const descriptor = Object.getOwnPropertyDescriptor(property.object, property.name);
} return Object.keys(desiredDescriptor).reduce(function(pass, key){
var desiredValue = desiredDescriptor[key]; function keyLog(type, expected, got){
var value = descriptor[key]; log(property.name + ": wrong " + type + " for ", key, "- expected:", expected, "- got:", got);
var keyPass = false; }
if ((typeof desiredValue) === (typeof value)){ var desiredValue = desiredDescriptor[key];
if ((typeof desiredValue) === "function"){ var value = descriptor[key];
if (value.name !== desiredValue.name){ var keyPass = false;
keyPass = true; if ((typeof desiredValue) === (typeof value)){
keyLog("function name", desiredValue.name, value.name); if ((typeof desiredValue) === "function"){
if (value.name !== desiredValue.name){
keyPass = true;
keyLog("function name", desiredValue.name, value.name);
}
if (value.length !== desiredValue.length){
keyPass = true;
keyLog("function length", desiredValue.length, value.length);
}
} }
if (value.length !== desiredValue.length){ else {
keyPass = true; if (desiredValue !== value){
keyLog("function length", desiredValue.length, value.length); keyPass = true;
keyLog("value", desiredValue, value);
}
} }
} }
else { else {
if (desiredValue !== value){ keyPass = true;
keyPass = true; keyLog("type", typeof desiredValue, typeof value);
keyLog("value", desiredValue, value);
}
} }
} return pass || keyPass;
else { }, false) || pass;
keyPass = true;
keyLog("type", typeof desiredValue, typeof value);
}
return pass || keyPass;
}, false); }, false);
}); });
addTest("error provocation 1", function(log){ addTest("error provocation 1", function(log){
@ -191,7 +210,8 @@ addTest("error properties", function(log){
log("Error stack starts wrong. Expected:", start, "- got :", err.stack.split(/\n/g, 2)[0]); log("Error stack starts wrong. Expected:", start, "- got :", err.stack.split(/\n/g, 2)[0]);
canvasBlocker = true; canvasBlocker = true;
} }
var message = "'getImageData' called on an object that does not implement interface CanvasRenderingContext2D."; var message = "'getImageData' called on an object that " +
"does not implement interface CanvasRenderingContext2D.";
if (err.message !== message){ if (err.message !== message){
log("Error message wrong. Expected: ", message, "- got:", err.message); log("Error message wrong. Expected: ", message, "- got:", err.message);
canvasBlocker = true; canvasBlocker = true;