From 6df4602b2b0a256c125ab34cb5dc7592fd9cdf56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Tue, 16 Feb 2016 20:52:07 +0100 Subject: [PATCH] Fix #977: Implement unit-tests. --- src/basic/Point.js | 3 ++- src/item/Shape.js | 5 ++++- test/tests/HitResult.js | 33 ++++++++++++++++++++++++++++++--- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/basic/Point.js b/src/basic/Point.js index d597fe8e..56eb1159 100644 --- a/src/basic/Point.js +++ b/src/basic/Point.js @@ -564,7 +564,8 @@ var Point = Base.extend(/** @lends Point# */{ * @function * @operator * @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 * var point = new Point(10, 20); diff --git a/src/item/Shape.js b/src/item/Shape.js index 6b73e486..9ad56380 100644 --- a/src/item/Shape.js +++ b/src/item/Shape.js @@ -352,7 +352,7 @@ new function() { // Scope for _contains() and _hitTestSelf() code. if (hitStroke || hitFill) { var type = this._type, radius = this._radius, - strokeRadius = hitStroke ? style.getStrokeWidth() / 2 : 0; + strokeRadius = hitStroke ? style.getStrokeWidth() / 2 : 0, strokePadding = options._tolerancePadding.add( Path._getStrokePadding(strokeRadius, !style.getStrokeScaling() && strokeMatrix)); @@ -374,6 +374,9 @@ new function() { // Scope for _contains() and _hitTestSelf() code. 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) : _hitTestSelf.base.apply(this, arguments); } diff --git a/test/tests/HitResult.js b/test/tests/HitResult.js index 2140f3e0..db3490de 100644 --- a/test/tests/HitResult.js +++ b/test/tests/HitResult.js @@ -641,21 +641,47 @@ test('hit-testing guides.', function() { }, true); }); -test('hit-testing fill with tolerance', function() { +test('hit-testing fills with tolerance', function() { var path = new Path.Rectangle({ from: [50, 50], to: [200, 200], fillColor: 'red' }); + var tolerance = 10; + var point = path.bounds.bottomRight.add(tolerance / Math.sqrt(2)); + equals(function() { - var tolerance = 10; - var result = paper.project.hitTest(path.bounds.bottomRight.add(tolerance / Math.sqrt(2)), { + var result = paper.project.hitTest(point, { tolerance: tolerance, fill: true }); return result && result.item === path; }, 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() { @@ -833,5 +859,6 @@ test('hit-testing for all items', function() { return result.length === 0; }, true); }); + // TODO: project.hitTest(point, {type: AnItemType});