test + refactoring

This commit is contained in:
Лаврёнов Антон
2014-05-17 21:10:49 +08:00
parent 012db6594c
commit 1db8fec21c
5 changed files with 69 additions and 16 deletions

View File

@@ -292,4 +292,62 @@ suite('Layer', function() {
assert.equal(layer.hitGraphEnabled(), true);
assert.equal(layer.shouldDrawHit(), true);
});
// ======================================================
test('hit graph caching', function() {
var stage = addStage();
var layer = new Kinetic.Layer();
var originGetImageData = layer.getHitCanvas().getContext().getImageData;
var count = 0;
layer.getHitCanvas().getContext().getImageData = function() {
count += 1;
return originGetImageData.apply(this, arguments);
};
stage.add(layer);
var circle = new Kinetic.Circle({
x: stage.getWidth() / 2,
y: stage.getHeight() / 2,
radius: 70,
fill: 'red',
stroke: 'black',
strokeWidth: 4
});
layer.add(circle);
layer.draw();
assert.equal(count, 0, 'draw should not touch getImageData');
var top = stage.content.getBoundingClientRect().top;
stage._mousemove({
clientX: stage.getWidth() / 2,
clientY: stage.getHeight() / 2 + top
});
// while mouse event we need hit canvas info
assert.equal(count, 1, 'getImageData should be called once');
stage._mousemove({
clientX: stage.getWidth() / 2,
clientY: stage.getHeight() / 2 + top + 2
});
assert.equal(count, 1, 'getImageData should not be called, because data is cached');
var group = new Kinetic.Group();
group.cache({
width : 1,
height : 1
});
layer.add(group);
group.draw();
stage._mousemove({
clientX: stage.getWidth() / 2,
clientY: stage.getHeight() / 2 + top + 2
});
// after drawing group hit cache should be cleared
assert.equal(count, 2, 'while creating new cache getImageData should be called');
});
});