Fix #977: Implement unit-tests.

This commit is contained in:
Jürg Lehni 2016-02-16 20:52:07 +01:00
parent 4081afb635
commit 6df4602b2b
3 changed files with 36 additions and 5 deletions

View file

@ -564,7 +564,8 @@ var Point = Base.extend(/** @lends Point# */{
* @function * @function
* @operator * @operator
* @param {Number} number the number to multiply by * @param {Number} number the number to multiply by
* @return {Point} the multiplication of the point and the value as a new point * @return {Point} the multiplication of the point and the value as a new
* point
* *
* @example * @example
* var point = new Point(10, 20); * var point = new Point(10, 20);

View file

@ -352,7 +352,7 @@ new function() { // Scope for _contains() and _hitTestSelf() code.
if (hitStroke || hitFill) { if (hitStroke || hitFill) {
var type = this._type, var type = this._type,
radius = this._radius, radius = this._radius,
strokeRadius = hitStroke ? style.getStrokeWidth() / 2 : 0; strokeRadius = hitStroke ? style.getStrokeWidth() / 2 : 0,
strokePadding = options._tolerancePadding.add( strokePadding = options._tolerancePadding.add(
Path._getStrokePadding(strokeRadius, Path._getStrokePadding(strokeRadius,
!style.getStrokeScaling() && strokeMatrix)); !style.getStrokeScaling() && strokeMatrix));
@ -374,6 +374,9 @@ new function() { // Scope for _contains() and _hitTestSelf() code.
hit = isOnEllipseStroke(point, radius, strokePadding); hit = isOnEllipseStroke(point, radius, strokePadding);
} }
} }
// NOTE: The above test is only for stroke, and the added tolerance
// when testing for fill. The actual fill test happens in
// Item#_hitTestSelf(), through its call of #_contains().
return hit ? new HitResult(hitStroke ? 'stroke' : 'fill', this) return hit ? new HitResult(hitStroke ? 'stroke' : 'fill', this)
: _hitTestSelf.base.apply(this, arguments); : _hitTestSelf.base.apply(this, arguments);
} }

View file

@ -641,21 +641,47 @@ test('hit-testing guides.', function() {
}, true); }, true);
}); });
test('hit-testing fill with tolerance', function() { test('hit-testing fills with tolerance', function() {
var path = new Path.Rectangle({ var path = new Path.Rectangle({
from: [50, 50], from: [50, 50],
to: [200, 200], to: [200, 200],
fillColor: 'red' fillColor: 'red'
}); });
var tolerance = 10;
var point = path.bounds.bottomRight.add(tolerance / Math.sqrt(2));
equals(function() { equals(function() {
var tolerance = 10; var result = paper.project.hitTest(point, {
var result = paper.project.hitTest(path.bounds.bottomRight.add(tolerance / Math.sqrt(2)), {
tolerance: tolerance, tolerance: tolerance,
fill: true fill: true
}); });
return result && result.item === path; return result && result.item === path;
}, true); }, true);
var point = new Point(20, 20);
var size = new Size(40, 40);
var hitPoint = new Point(10, 10);
var options = {
fill: true,
tolerance: 20
};
var shapeRect = new Shape.Rectangle(point, size);
shapeRect.fillColor = 'black';
var pathRect = new Path.Rectangle(point, size);
pathRect.fillColor = 'black';
equals(function() {
var hit = shapeRect.hitTest(hitPoint, options);
return hit && hit.type === 'fill';
}, true);
equals(function() {
var hit = pathRect.hitTest(hitPoint, options);
return hit && hit.type === 'fill';
}, true);
}); });
test('hit-testing compound-paths', function() { test('hit-testing compound-paths', function() {
@ -833,5 +859,6 @@ test('hit-testing for all items', function() {
return result.length === 0; return result.length === 0;
}, true); }, true);
}); });
// TODO: project.hitTest(point, {type: AnItemType}); // TODO: project.hitTest(point, {type: AnItemType});