Chromium Code Reviews| Index: ui/gfx/path_mac_unittest.mm |
| diff --git a/ui/gfx/path_mac_unittest.mm b/ui/gfx/path_mac_unittest.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e41fbfff078ecdb5c29dc632921fa79026ffbb8d |
| --- /dev/null |
| +++ b/ui/gfx/path_mac_unittest.mm |
| @@ -0,0 +1,259 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import "ui/gfx/path_mac.h" |
| + |
| +#include <cmath> |
| +#include <vector> |
| + |
| +#import <Cocoa/Cocoa.h> |
| + |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "third_party/skia/include/core/SkRegion.h" |
| +#include "ui/gfx/geometry/rect.h" |
| +#include "ui/gfx/geometry/rect_conversions.h" |
| +#include "ui/gfx/path.h" |
| + |
| +namespace { |
| + |
| +// Returns the point at a distance of |radius| from the point (|centre_x|, |
| +// |centre_y|), and angle |degrees| from the positive horizontal axis, measured |
| +// anti-clockwise. |
| +NSPoint GetRadialPoint(double radius, |
| + double degrees, |
| + double centre_x, |
| + double centre_y) { |
| + const double radian = (degrees * SK_ScalarPI) / 180; |
| + return NSMakePoint(centre_x + radius * std::cos(radian), |
| + centre_y + radius * std::sin(radian)); |
| +} |
| + |
| +// Returns the area of a circle with the given |radius|. |
| +double CalculateCircleArea(double radius) { |
| + return SK_ScalarPI * radius * radius; |
| +} |
| + |
| +// Returns the area of a simple polygon. |path| should represent a simple |
| +// polygon. |
| +double CalculatePolygonArea(NSBezierPath* path) { |
| + // If path represents a single polygon, it will have MoveTo, followed by |
| + // multiple LineTo, followed By ClosePath, followed by another MoveTo |
| + // NSBezierPathElement. |
| + const size_t element_count = [path elementCount]; |
| + NSPoint points[3]; |
| + std::vector<NSPoint> poly; |
| + |
| + for (size_t i = 0; i < element_count - 1; i++) { |
| + NSBezierPathElement element = |
| + [path elementAtIndex:i associatedPoints:points]; |
| + poly.push_back(points[0]); |
| + DCHECK_EQ(element, |
| + i ? (i == element_count - 2 ? NSClosePathBezierPathElement |
| + : NSLineToBezierPathElement) |
| + : NSMoveToBezierPathElement); |
| + } |
| + DCHECK_EQ([path elementAtIndex:element_count - 1 associatedPoints:points], |
|
msw
2016/02/11 23:57:37
optional nit: you could omit associatedPoints here
karandeepb
2016/02/12 03:45:52
Done.
|
| + NSMoveToBezierPathElement); |
| + DCHECK(NSEqualPoints(points[0], poly.front())); |
| + |
| + // Shoelace Algorithm to find the area of a simple polygon. |
| + DCHECK(NSEqualPoints(poly.front(), poly.back())); |
| + double area = 0; |
| + for (size_t i = 0; i < poly.size() - 1; i++) |
| + area += poly[i].x * poly[i + 1].y - poly[i].y * poly[i + 1].x; |
|
msw
2016/02/11 23:57:37
q: don't you technically need incldue these terms
karandeepb
2016/02/12 03:45:52
The last point in the poly vector is the same as t
msw
2016/02/12 19:26:55
Acknowledged.
|
| + |
| + return std::fabs(area) / 2.0; |
| +} |
| + |
| +// Returns the area of a rounded rectangle with the given |width|, |height| and |
| +// |radius|. |
| +double CalculateRoundedRectangleArea(double width, |
| + double height, |
| + double radius) { |
| + const double inside_width = width - 2 * radius; |
| + const double inside_height = height - 2 * radius; |
| + return inside_width * inside_height + |
| + 2 * radius * (inside_width + inside_height) + |
| + CalculateCircleArea(radius); |
| +} |
| + |
| +// Returns the bounding box of |path| as a gfx::Rect. |
|
msw
2016/02/11 23:57:38
nit: put this anon namespace inside gfx, and then
karandeepb
2016/02/12 03:45:52
Done.
|
| +gfx::Rect GetBoundingBox(NSBezierPath* path) { |
| + const NSRect bounds = [path bounds]; |
| + return gfx::ToNearestRect(gfx::RectF(bounds.origin.x, bounds.origin.y, |
| + bounds.size.width, bounds.size.height)); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace gfx { |
| + |
| +// Check that empty NSBezierPath is returned for empty SkPath. |
| +TEST(CreateNSBezierPathFromSkPathTest, EmptyPath) { |
| + NSBezierPath* result = CreateNSBezierPathFromSkPath(SkPath()); |
|
msw
2016/02/11 23:57:38
Don't you need to release (CFRelease?) all the pat
karandeepb
2016/02/12 03:45:52
It is autoreleased when it is no longer in scope(a
|
| + EXPECT_TRUE([result isEmpty]); |
| +} |
| + |
| +// Check that the returned NSBezierPath has the correct winding rule. |
| +TEST(CreateNSBezierPathFromSkPathTest, FillType) { |
| + SkPath path; |
| + path.setFillType(SkPath::kWinding_FillType); |
| + NSBezierPath* result = CreateNSBezierPathFromSkPath(path); |
| + EXPECT_EQ(NSNonZeroWindingRule, [result windingRule]); |
| + |
| + path.setFillType(SkPath::kEvenOdd_FillType); |
| + result = CreateNSBezierPathFromSkPath(path); |
| + EXPECT_EQ(NSEvenOddWindingRule, [result windingRule]); |
| +} |
| + |
| +// Check that a path containing multiple subpaths, in this case two rectangles, |
| +// is correctly converted to a NSBezierPath. |
| +TEST(CreateNSBezierPathFromSkPathTest, TwoRectanglesPath) { |
| + const SkRect rects[] = { |
| + {0, 0, 50, 50}, {100, 100, 150, 150}, |
| + }; |
| + const NSPoint inside_points[] = { |
| + {1, 1}, {1, 49}, {49, 49}, {49, 1}, {101, 101}, |
| + {101, 149}, {149, 149}, {149, 101}, {25, 25}, {125, 125}}; |
|
msw
2016/02/11 23:57:38
nit: put {25, 25} after {49, 1}, bump {101, 101} t
karandeepb
2016/02/12 03:45:51
Done.
|
| + const NSPoint outside_points[] = {{-1, -1}, {-1, 51}, {51, 51}, {51, -1}, |
|
msw
2016/02/11 23:57:38
EXPECT/ASSERT_EQ(arraysize(inside_points), arraysi
karandeepb
2016/02/12 03:45:52
Done.
|
| + {99, 99}, {99, 151}, {151, 151}, {151, 99}, |
| + {75, 75}, {-5, -5}}; |
| + const gfx::Rect expected_bounds(0, 0, 150, 150); |
|
msw
2016/02/11 23:57:38
nit: 'gfx::' not needed
karandeepb
2016/02/12 03:45:52
Done.
|
| + |
| + SkPath path; |
| + path.addRect(rects[0]); |
| + path.addRect(rects[1]); |
| + NSBezierPath* result = CreateNSBezierPathFromSkPath(path); |
| + |
| + // Check points near the boundary of the path and verify that they are |
| + // reported correctly as being inside/outside the path. |
| + for (size_t i = 0; i < arraysize(inside_points); i++) { |
| + EXPECT_TRUE([result containsPoint:inside_points[i]]); |
| + EXPECT_FALSE([result containsPoint:outside_points[i]]); |
| + } |
| + |
| + // Check that the returned result has the correct bounding box. GetBoundingBox |
| + // rounds the coordinates to nearest integer values. |
| + EXPECT_EQ(expected_bounds, GetBoundingBox(result)); |
| +} |
| + |
| +// Test that an SKPath containing a circle is converted correctly to a |
| +// NSBezierPath. |
| +TEST(CreateNSBezierPathFromSkPathTest, CirclePath) { |
| + const int kRadius = 5; |
| + const int kCentreX = 10; |
| + const int kCentreY = 15; |
| + const double kCushion = 0.1; |
| + // Expected bounding box of the circle. |
| + const gfx::Rect expected_bounds(kCentreX - kRadius, kCentreY - kRadius, |
|
msw
2016/02/11 23:57:38
nit: 'gfx::' not needed
karandeepb
2016/02/12 03:45:51
Done.
|
| + 2 * kRadius, 2 * kRadius); |
| + |
| + SkPath path; |
| + path.addCircle(SkIntToScalar(kCentreX), SkIntToScalar(kCentreY), |
| + SkIntToScalar(kRadius)); |
| + NSBezierPath* result = CreateNSBezierPathFromSkPath(path); |
| + |
| + // Check points near the boundary of the circle and verify that they are |
| + // reported correctly as being inside/outside the path. |
| + for (size_t deg = 0; deg < 360; deg++) { |
| + NSPoint inside_point = |
| + GetRadialPoint(kRadius - kCushion, deg, kCentreX, kCentreY); |
| + NSPoint outside_point = |
| + GetRadialPoint(kRadius + kCushion, deg, kCentreX, kCentreY); |
| + EXPECT_TRUE([result containsPoint:inside_point]); |
| + EXPECT_FALSE([result containsPoint:outside_point]); |
| + } |
| + |
| + // Check that the returned result has the correct bounding box. GetBoundingBox |
| + // rounds the coordinates to nearest integer values. |
| + EXPECT_EQ(expected_bounds, GetBoundingBox(result)); |
| + |
| + // Check area of converted path is correct upto a certain tolerance value. To |
|
msw
2016/02/11 23:57:38
nit: "up to"
karandeepb
2016/02/12 03:45:52
Done.
|
| + // find the area of the NSBezierPath returned, flatten it i.e. convert it to a |
| + // polygon. |
| + [NSBezierPath setDefaultFlatness:0.01]; |
| + NSBezierPath* polygon = [result bezierPathByFlatteningPath]; |
|
msw
2016/02/11 23:57:38
nit: delete this object at some point or use base:
karandeepb
2016/02/12 03:45:51
It is autoreleased when it is no longer in scope(a
|
| + const double kTolerance = 0.14; |
| + EXPECT_NEAR(CalculateCircleArea(kRadius), CalculatePolygonArea(polygon), |
| + kTolerance); |
| +} |
| + |
| +// Test that an SKPath containing a rounded rectangle is converted correctly to |
| +// a NSBezierPath. |
| +TEST(CreateNSBezierPathFromSkPathTest, RoundedRectanglePath) { |
| + const int kRectangleWidth = 50; |
| + const int kRectangleHeight = 100; |
| + const int kCornerRadius = 5; |
| + const double kCushion = 0.1; |
| + // Expected bounding box of the rounded rectangle. |
| + const gfx::Rect expected_bounds(kRectangleWidth, kRectangleHeight); |
|
msw
2016/02/11 23:57:38
nit: 'gfx::' not needed
karandeepb
2016/02/12 03:45:52
Done.
|
| + |
| + SkRRect rrect; |
| + rrect.setRectXY(SkRect::MakeWH(kRectangleWidth, kRectangleHeight), |
| + kCornerRadius, kCornerRadius); |
| + |
| + const NSPoint inside_points[] = { |
| + // Bottom left corner. |
| + {kCornerRadius / 2.0, kCornerRadius / 2.0}, |
| + // Bottom right corner. |
| + {kRectangleWidth - kCornerRadius / 2.0, kCornerRadius / 2.0}, |
| + // Top Right corner. |
| + {kRectangleWidth - kCornerRadius / 2.0, |
| + kRectangleHeight - kCornerRadius / 2.0}, |
| + // Top left corner. |
| + {kCornerRadius / 2.0, kRectangleHeight - kCornerRadius / 2.0}, |
| + // Bottom middle. |
| + {kRectangleWidth / 2.0, kCushion}, |
| + // Right middle. |
| + {kRectangleWidth - kCushion, kRectangleHeight / 2.0}, |
| + // Top middle. |
| + {kRectangleWidth / 2.0, kRectangleHeight - kCushion}, |
| + // Left middle. |
| + {kCushion, kRectangleHeight / 2.0}}; |
| + const NSPoint outside_points[] = { |
|
msw
2016/02/11 23:57:38
EXPECT/ASSERT_EQ(arraysize(inside_points), arraysi
karandeepb
2016/02/12 03:45:52
Done.
|
| + // Bottom left corner. |
| + {0, 0}, |
| + // Bottom right corner. |
| + {kRectangleWidth, 0}, |
| + // Top right corner. |
| + {kRectangleWidth, kRectangleHeight}, |
| + // Top left corner. |
| + {0, kRectangleHeight}, |
| + // Bottom middle. |
| + {kRectangleWidth / 2.0, -kCushion}, |
| + // Right middle. |
| + {kRectangleWidth + kCushion, kRectangleHeight / 2.0}, |
| + // Top middle. |
| + {kRectangleWidth / 2.0, kRectangleHeight + kCushion}, |
| + // Left middle. |
| + {-kCushion, kRectangleHeight / 2.0}}; |
| + const size_t kNumPoints = arraysize(inside_points); |
| + |
| + SkPath path; |
| + path.addRRect(rrect); |
| + NSBezierPath* result = CreateNSBezierPathFromSkPath(path); |
| + |
| + // Check points near the boundary of the path and verify that they are |
| + // reported correctly as being inside/outside the path. |
| + for (size_t i = 0; i < kNumPoints; i++) { |
| + EXPECT_TRUE([result containsPoint:inside_points[i]]); |
| + EXPECT_FALSE([result containsPoint:outside_points[i]]); |
| + } |
| + |
| + // Check that the returned result has the correct bounding box. GetBoundingBox |
| + // rounds the coordinates to nearest integer values. |
| + EXPECT_EQ(expected_bounds, GetBoundingBox(result)); |
| + |
| + // Check area of converted path is correct upto a certain tolerance value. To |
|
msw
2016/02/11 23:57:38
nit: "up to"
karandeepb
2016/02/12 03:45:51
Done.
|
| + // find the area of the NSBezierPath returned, flatten it i.e. convert it to a |
| + // polygon. |
| + [NSBezierPath setDefaultFlatness:0.01]; |
| + NSBezierPath* polygon = [result bezierPathByFlatteningPath]; |
| + const double kTolerance = 0.14; |
| + EXPECT_NEAR(CalculateRoundedRectangleArea(kRectangleWidth, kRectangleHeight, |
| + kCornerRadius), |
| + CalculatePolygonArea(polygon), kTolerance); |
| +} |
| + |
| +} // namespace gfx |