1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/views/rect_based_targeting_utils.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/gfx/geometry/rect.h"
12 TEST(RectBasedTargetingUtils
, UsePointBasedTargeting
) {
13 gfx::Rect
rect_1(gfx::Point(-22, 30), gfx::Size(1, 1));
14 gfx::Rect
rect_2(gfx::Point(0, 0), gfx::Size(34, 55));
15 gfx::Rect
rect_3(gfx::Point(12, 12), gfx::Size(1, 0));
16 gfx::Rect
rect_4(gfx::Point(12, 120), gfx::Size(0, 0));
18 EXPECT_TRUE(UsePointBasedTargeting(rect_1
));
19 EXPECT_FALSE(UsePointBasedTargeting(rect_2
));
20 EXPECT_FALSE(UsePointBasedTargeting(rect_3
));
21 EXPECT_FALSE(UsePointBasedTargeting(rect_4
));
24 TEST(RectBasedTargetingUtils
, PercentCoveredBy
) {
25 gfx::Rect
rect_1(gfx::Point(0, 0), gfx::Size(300, 120));
26 gfx::Rect
rect_2(gfx::Point(20, 10), gfx::Size(30, 90));
27 gfx::Rect
rect_3(gfx::Point(160, 50), gfx::Size(150, 85));
28 gfx::Rect
rect_4(gfx::Point(20, 55), gfx::Size(0, 15));
31 // Passing in identical rectangles.
32 EXPECT_FLOAT_EQ(1.0f
, PercentCoveredBy(rect_1
, rect_1
));
34 // |rect_1| completely contains |rect_2|.
35 EXPECT_FLOAT_EQ(0.075f
, PercentCoveredBy(rect_1
, rect_2
));
36 EXPECT_FLOAT_EQ(1.0f
, PercentCoveredBy(rect_2
, rect_1
));
38 // |rect_2| and |rect_3| do not intersect.
39 EXPECT_FLOAT_EQ(0.0f
, PercentCoveredBy(rect_2
, rect_3
));
41 // |rect_1| and |rect_3| have a nonzero area of intersection which
42 // is smaller than the area of either rectangle.
43 EXPECT_NEAR(0.272f
, PercentCoveredBy(rect_1
, rect_3
), error
);
44 EXPECT_NEAR(0.768f
, PercentCoveredBy(rect_3
, rect_1
), error
);
46 // |rect_4| has no area.
47 EXPECT_FLOAT_EQ(0.0f
, PercentCoveredBy(rect_2
, rect_4
));
48 EXPECT_FLOAT_EQ(0.0f
, PercentCoveredBy(rect_4
, rect_2
));
51 TEST(RectBasedTargetingUtils
, DistanceSquaredFromCenterToPoint
) {
52 gfx::Rect
rect_1(gfx::Point(0, 0), gfx::Size(10, 10));
53 gfx::Rect
rect_2(gfx::Point(20, 0), gfx::Size(80, 10));
54 gfx::Rect
rect_3(gfx::Point(0, 20), gfx::Size(10, 20));
56 gfx::Point
point_1(5, 5);
57 gfx::Point
point_2(25, 5);
58 gfx::Point
point_3(11, 15);
59 gfx::Point
point_4(33, 44);
61 EXPECT_EQ(0, DistanceSquaredFromCenterToPoint(point_1
, rect_1
));
62 EXPECT_EQ(1225, DistanceSquaredFromCenterToPoint(point_2
, rect_2
));
63 EXPECT_EQ(3025, DistanceSquaredFromCenterToPoint(point_1
, rect_2
));
64 EXPECT_EQ(1025, DistanceSquaredFromCenterToPoint(point_2
, rect_3
));
65 EXPECT_EQ(2501, DistanceSquaredFromCenterToPoint(point_3
, rect_2
));
66 EXPECT_EQ(136, DistanceSquaredFromCenterToPoint(point_3
, rect_1
));
67 EXPECT_EQ(980, DistanceSquaredFromCenterToPoint(point_4
, rect_3
));