konva/test/runner.js

512 lines
12 KiB
JavaScript
Raw Normal View History

mocha.ui('tdd');
mocha.setup('bdd');
var assert = chai.assert,
konvaContainer = document.getElementById('konva-container'),
origAssertEqual = assert.equal,
origAssert = assert,
origNotEqual = assert.notEqual,
origDeepEqual = assert.deepEqual,
assertionCount = 0,
assertions = document.createElement('em');
2020-05-08 22:59:35 +08:00
window.requestAnimFrame = (function (callback) {
2017-05-25 02:50:56 +08:00
return (
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
2020-05-08 22:59:35 +08:00
function (callback) {
window.setTimeout(callback, 1000 / 30);
2017-05-25 02:50:56 +08:00
}
);
})();
2013-09-30 22:52:13 +08:00
function init() {
// assert extenders so that we can count assertions
2020-05-08 22:59:35 +08:00
assert = function () {
2013-09-30 22:52:13 +08:00
origAssert.apply(this, arguments);
assertions.innerHTML = ++assertionCount;
};
2020-05-08 22:59:35 +08:00
assert.equal = function () {
2013-09-30 22:52:13 +08:00
origAssertEqual.apply(this, arguments);
assertions.innerHTML = ++assertionCount;
};
2020-05-08 22:59:35 +08:00
assert.notEqual = function () {
2013-09-30 22:52:13 +08:00
origNotEqual.apply(this, arguments);
assertions.innerHTML = ++assertionCount;
};
2020-05-08 22:59:35 +08:00
assert.deepEqual = function () {
origDeepEqual.apply(this, arguments);
assertions.innerHTML = ++assertionCount;
};
2015-02-09 07:29:31 +08:00
2020-05-08 22:59:35 +08:00
window.onload = function () {
2013-09-30 22:52:13 +08:00
var mochaStats = document.getElementById('mocha-stats');
if (mochaStats) {
var li = document.createElement('li');
var anchor = document.createElement('a');
anchor.href = '#';
anchor.innerHTML = 'assertions:';
assertions.innerHTML = 0;
li.appendChild(anchor);
li.appendChild(assertions);
mochaStats.appendChild(li);
}
};
//addStats();
2013-09-30 22:52:13 +08:00
}
2015-01-27 15:07:51 +08:00
Konva.enableTrace = true;
Konva.showWarnings = true;
2015-02-14 23:12:54 +08:00
//Konva.pixelRatio = 2;
2015-08-28 11:11:10 +08:00
window.isPhantomJS = /PhantomJS/.test(window.navigator.userAgent);
function addStats() {
stats = new Stats();
stats.setMode(0);
stats.domElement.style.position = 'fixed';
stats.domElement.style.left = '0px';
stats.domElement.style.top = '0px';
document.getElementsByTagName('body')[0].appendChild(stats.domElement);
function animate(lastTime) {
stats.begin();
2020-05-08 22:59:35 +08:00
requestAnimFrame(function () {
stats.end();
animate(lastTime);
});
}
animate();
}
function addStage(attrs) {
var container = document.createElement('div');
2019-11-13 03:38:36 +08:00
const props = Konva.Util._assign(
{
container: container,
width: 578,
2020-05-08 22:59:35 +08:00
height: 200,
},
attrs
);
var stage = new Konva.Stage(props);
2015-01-27 15:07:51 +08:00
konvaContainer.appendChild(container);
return stage;
}
2015-01-22 13:47:07 +08:00
function createCanvas() {
var canvas = document.createElement('canvas');
var ratio = Konva.pixelRatio || window.devicePixelRatio;
canvas.width = 578 * ratio;
canvas.height = 200 * ratio;
canvas.getContext('2d').scale(ratio, ratio);
canvas.ratio = ratio;
return canvas;
2015-01-22 13:47:07 +08:00
}
function get(element, content) {
element = document.createElement(element);
if (element && content) {
element.innerHTML = content;
}
return element;
2015-01-22 13:47:07 +08:00
}
function compareCanvases(canvas1, canvas2, tol) {
// don't test in PhantomJS as it use old chrome engine
// it it has opacity + shadow bug
var equal = imagediff.equal(canvas1, canvas2, tol);
if (!equal) {
var div = get('div'),
b = get('div', '<div>Expected:</div>'),
c = get('div', '<div>Diff:</div>'),
diff = imagediff.diff(canvas1, canvas2),
diffCanvas = get('canvas'),
context;
diffCanvas.height = diff.height;
diffCanvas.width = diff.width;
div.style.overflow = 'hidden';
b.style.float = 'left';
c.style.float = 'left';
canvas2.style.position = '';
canvas2.style.display = '';
context = diffCanvas.getContext('2d');
context.putImageData(diff, 0, 0);
b.appendChild(canvas2);
c.appendChild(diffCanvas);
2019-01-19 23:31:07 +08:00
var base64 = diffCanvas.toDataURL();
2019-01-19 23:26:21 +08:00
console.error('Diff image:');
console.error(base64);
div.appendChild(b);
div.appendChild(c);
konvaContainer.appendChild(div);
}
assert.equal(
equal,
true,
'Result from Konva is different with canvas result'
);
}
function compareLayerAndCanvas(layer, canvas, tol) {
compareCanvases(layer.getCanvas()._canvas, canvas, tol);
}
function compareLayers(layer1, layer2, tol) {
compareLayerAndCanvas(layer1, layer2.getCanvas()._canvas, tol);
}
function cloneAndCompareLayer(layer, tol) {
var layer2 = layer.clone();
layer.getStage().add(layer2);
layer2.hide();
compareLayers(layer, layer2, tol);
}
function cloneAndCompareLayerWithHit(layer, tol) {
var layer2 = layer.clone();
layer.getStage().add(layer2);
layer2.hide();
compareLayers(layer, layer2, tol);
compareCanvases(
layer.getHitCanvas()._canvas,
layer2.getHitCanvas()._canvas,
tol
);
}
2015-01-22 13:47:07 +08:00
function compareSceneAndHit(layer) {
compareLayerAndCanvas(layer, layer.getHitCanvas()._canvas, 254);
2015-01-22 13:47:07 +08:00
}
2013-09-09 12:36:54 +08:00
function addContainer() {
var container = document.createElement('div');
2015-01-27 15:07:51 +08:00
konvaContainer.appendChild(container);
2013-09-09 12:36:54 +08:00
return container;
}
function showCanvas(canvas) {
canvas.style.position = 'relative';
2015-01-27 15:07:51 +08:00
konvaContainer.appendChild(canvas);
}
function showHit(layer) {
var canvas = layer.hitCanvas._canvas;
canvas.style.position = 'relative';
2015-01-27 15:07:51 +08:00
konvaContainer.appendChild(canvas);
}
2020-05-08 22:59:35 +08:00
beforeEach(function () {
var title = document.createElement('h2'),
test = this.currentTest;
title.innerHTML = test.parent.title + ' - ' + test.title;
title.className = 'konva-title';
konvaContainer.appendChild(title);
// resets
Konva.inDblClickWindow = false;
Konva.DD && (Konva.DD.isDragging = false);
Konva.DD && (Konva.DD.node = undefined);
if (
!(
this.currentTest.body.indexOf('assert') !== -1 ||
this.currentTest.body.toLowerCase().indexOf('compare') !== -1
)
) {
console.error(this.currentTest.title);
}
2013-09-30 22:52:13 +08:00
});
2015-02-14 23:12:54 +08:00
Konva.UA.mobile = false;
2020-05-08 22:59:35 +08:00
afterEach(function () {
2019-01-22 06:42:02 +08:00
var isFailed = this.currentTest.state == 'failed';
var isManual = this.currentTest.parent.title === 'Manual';
2020-05-08 22:59:35 +08:00
Konva.stages.forEach(function (stage) {
2019-01-22 06:42:02 +08:00
clearTimeout(stage.dblTimeout);
});
if (!isFailed && !isManual) {
2020-05-08 22:59:35 +08:00
Konva.stages.forEach(function (stage) {
// stage.destroy();
2019-01-22 06:42:02 +08:00
});
2019-08-04 15:38:57 +08:00
if (Konva.DD._dragElements.size) {
2020-05-07 00:40:08 +08:00
throw 'Why drag elements are not cleaned?';
2019-08-04 15:38:57 +08:00
}
2019-01-22 06:42:02 +08:00
}
2015-02-14 23:12:54 +08:00
});
2020-05-08 22:59:35 +08:00
Konva.Stage.prototype.simulateMouseDown = function (pos) {
2017-05-25 02:50:56 +08:00
var top = this.content.getBoundingClientRect().top;
this._mousedown({
2017-05-25 02:50:56 +08:00
clientX: pos.x,
clientY: pos.y + top,
2020-05-08 22:59:35 +08:00
button: pos.button || 0,
});
2015-06-02 09:14:54 +08:00
};
2020-05-08 22:59:35 +08:00
Konva.Stage.prototype.simulateMouseMove = function (pos) {
2017-05-25 02:50:56 +08:00
var top = this.content.getBoundingClientRect().top;
var evt = {
2017-05-25 02:50:56 +08:00
clientX: pos.x,
clientY: pos.y + top,
2020-05-08 22:59:35 +08:00
button: pos.button || 0,
};
2015-06-02 09:14:54 +08:00
this._mousemove(evt);
Konva.DD._drag(evt);
2015-06-02 09:14:54 +08:00
};
2020-05-08 22:59:35 +08:00
Konva.Stage.prototype.simulateMouseUp = function (pos) {
2017-05-25 02:50:56 +08:00
var top = this.content.getBoundingClientRect().top;
var evt = {
2017-05-25 02:50:56 +08:00
clientX: pos.x,
clientY: pos.y + top,
2020-05-08 22:59:35 +08:00
button: pos.button || 0,
};
2015-06-02 09:14:54 +08:00
Konva.DD._endDragBefore(evt);
this._mouseup(evt);
Konva.DD._endDragAfter(evt);
};
2015-06-02 09:14:54 +08:00
2020-05-08 22:59:35 +08:00
Konva.Stage.prototype.simulateTouchStart = function (pos, changed) {
2019-02-27 22:14:07 +08:00
var top = this.content.getBoundingClientRect().top;
2019-08-04 10:41:57 +08:00
var touches;
var changedTouches;
if (Array.isArray(pos)) {
2020-05-08 22:59:35 +08:00
touches = pos.map(function (touch) {
2019-11-13 03:38:36 +08:00
return {
identifier: touch.id,
clientX: touch.x,
2020-05-08 22:59:35 +08:00
clientY: touch.y + top,
};
2019-11-13 03:38:36 +08:00
});
2020-05-08 22:59:35 +08:00
changedTouches = (changed || pos).map(function (touch) {
2019-11-13 03:38:36 +08:00
return {
identifier: touch.id,
clientX: touch.x,
2020-05-08 22:59:35 +08:00
clientY: touch.y + top,
};
2019-11-13 03:38:36 +08:00
});
2019-08-04 10:41:57 +08:00
} else {
2019-08-04 15:38:57 +08:00
changedTouches = touches = [
2019-02-27 22:14:07 +08:00
{
clientX: pos.x,
2019-08-04 15:38:57 +08:00
clientY: pos.y + top,
2020-05-08 22:59:35 +08:00
id: 0,
},
2019-08-04 10:41:57 +08:00
];
}
var evt = {
touches: touches,
2020-05-08 22:59:35 +08:00
changedTouches: changedTouches,
2019-08-04 10:41:57 +08:00
};
this._touchstart(evt);
2019-02-27 22:14:07 +08:00
};
2020-05-08 22:59:35 +08:00
Konva.Stage.prototype.simulateTouchMove = function (pos, changed) {
2019-02-27 22:14:07 +08:00
var top = this.content.getBoundingClientRect().top;
2019-08-04 10:41:57 +08:00
var touches;
var changedTouches;
if (Array.isArray(pos)) {
2020-05-08 22:59:35 +08:00
touches = pos.map(function (touch) {
2019-11-13 03:38:36 +08:00
return {
identifier: touch.id,
clientX: touch.x,
2020-05-08 22:59:35 +08:00
clientY: touch.y + top,
};
2019-11-13 03:38:36 +08:00
});
2020-05-08 22:59:35 +08:00
changedTouches = (changed || pos).map(function (touch) {
2019-11-13 03:38:36 +08:00
return {
identifier: touch.id,
clientX: touch.x,
2020-05-08 22:59:35 +08:00
clientY: touch.y + top,
};
2019-11-13 03:38:36 +08:00
});
2019-08-04 10:41:57 +08:00
} else {
2019-08-04 15:38:57 +08:00
changedTouches = touches = [
2019-02-27 22:14:07 +08:00
{
clientX: pos.x,
2019-08-04 15:38:57 +08:00
clientY: pos.y + top,
2020-05-08 22:59:35 +08:00
id: 0,
},
2019-08-04 10:41:57 +08:00
];
}
var evt = {
touches: touches,
2020-05-08 22:59:35 +08:00
changedTouches: changedTouches,
2019-02-27 22:14:07 +08:00
};
this._touchmove(evt);
Konva.DD._drag(evt);
};
2020-05-08 22:59:35 +08:00
Konva.Stage.prototype.simulateTouchEnd = function (pos, changed) {
2019-02-27 22:14:07 +08:00
var top = this.content.getBoundingClientRect().top;
2019-08-04 10:41:57 +08:00
var touches;
var changedTouches;
if (Array.isArray(pos)) {
2020-05-08 22:59:35 +08:00
touches = pos.map(function (touch) {
2019-11-13 03:38:36 +08:00
return {
identifier: touch.id,
clientX: touch.x,
2020-05-08 22:59:35 +08:00
clientY: touch.y + top,
};
2019-11-13 03:38:36 +08:00
});
2020-05-08 22:59:35 +08:00
changedTouches = (changed || pos).map(function (touch) {
2019-11-13 03:38:36 +08:00
return {
identifier: touch.id,
clientX: touch.x,
2020-05-08 22:59:35 +08:00
clientY: touch.y + top,
};
2019-11-13 03:38:36 +08:00
});
2019-08-04 10:41:57 +08:00
} else {
2019-08-04 15:38:57 +08:00
changedTouches = touches = [
2019-02-27 22:14:07 +08:00
{
clientX: pos.x,
2019-08-04 15:38:57 +08:00
clientY: pos.y + top,
2020-05-08 22:59:35 +08:00
id: 0,
},
2019-08-04 10:41:57 +08:00
];
}
var evt = {
touches: touches,
2020-05-08 22:59:35 +08:00
changedTouches: changedTouches,
2019-02-27 22:14:07 +08:00
};
Konva.DD._endDragBefore(evt);
this._touchend(evt);
Konva.DD._endDragAfter(evt);
};
2020-05-08 22:59:35 +08:00
Konva.Stage.prototype.simulatePointerDown = function (pos) {
2019-04-10 22:28:42 +08:00
var top = this.content.getBoundingClientRect().top;
this._mousedown({
clientX: pos.x,
clientY: pos.y + top,
button: pos.button || 0,
2020-05-08 22:59:35 +08:00
pointerId: pos.pointerId || 1,
2019-04-10 22:28:42 +08:00
});
};
2020-05-08 22:59:35 +08:00
Konva.Stage.prototype.simulatePointerMove = function (pos) {
2019-04-10 22:28:42 +08:00
var top = this.content.getBoundingClientRect().top;
var evt = {
clientX: pos.x,
clientY: pos.y + top,
button: pos.button || 0,
2020-05-08 22:59:35 +08:00
pointerId: pos.pointerId || 1,
2019-04-10 22:28:42 +08:00
};
this._mousemove(evt);
Konva.DD._drag(evt);
};
2020-05-08 22:59:35 +08:00
Konva.Stage.prototype.simulatePointerUp = function (pos) {
2019-04-10 22:28:42 +08:00
var top = this.content.getBoundingClientRect().top;
var evt = {
clientX: pos.x,
clientY: pos.y + top,
button: pos.button || 0,
2020-05-08 22:59:35 +08:00
pointerId: pos.pointerId || 1,
2019-04-10 22:28:42 +08:00
};
Konva.DD._endDragBefore(evt);
this._mouseup(evt);
Konva.DD._endDragAfter(evt);
};
2015-06-02 09:14:54 +08:00
init();
2019-11-13 03:38:36 +08:00
// polyfills
if (!Array.prototype.find) {
Object.defineProperty(Array.prototype, 'find', {
2020-05-08 22:59:35 +08:00
value: function (predicate) {
2019-11-13 03:38:36 +08:00
// 1. Let O be ? ToObject(this value).
if (this == null) {
throw TypeError('"this" is null or not defined');
}
var o = Object(this);
// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 0;
// 3. If IsCallable(predicate) is false, throw a TypeError exception.
if (typeof predicate !== 'function') {
throw TypeError('predicate must be a function');
}
// 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
var thisArg = arguments[1];
// 5. Let k be 0.
var k = 0;
// 6. Repeat, while k < len
while (k < len) {
// a. Let Pk be ! ToString(k).
// b. Let kValue be ? Get(O, Pk).
// c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
// d. If testResult is true, return kValue.
var kValue = o[k];
if (predicate.call(thisArg, kValue, k, o)) {
return kValue;
}
// e. Increase k by 1.
k++;
}
// 7. Return undefined.
return undefined;
},
configurable: true,
2020-05-08 22:59:35 +08:00
writable: true,
2019-11-13 03:38:36 +08:00
});
}
String.prototype.trimRight =
String.prototype.trimRight ||
function polyfill() {
return this.replace(/[\s\xa0]+$/, '');
};
2019-11-13 03:38:36 +08:00
String.prototype.trimLeft =
String.prototype.trimLeft ||
function polyfill() {
return this.replace(/^\s+/, '');
};