1 // Copyright (c) 2012 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 "base/basictypes.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "ui/gfx/geometry/point.h"
8 #include "ui/gfx/geometry/point_base.h"
9 #include "ui/gfx/geometry/point_conversions.h"
10 #include "ui/gfx/geometry/point_f.h"
16 int TestPointF(const PointF
& p
) {
22 TEST(PointTest
, ToPointF
) {
23 // Check that implicit conversion from integer to float compiles.
25 float x
= TestPointF(a
);
33 TEST(PointTest
, IsOrigin
) {
34 EXPECT_FALSE(Point(1, 0).IsOrigin());
35 EXPECT_FALSE(Point(0, 1).IsOrigin());
36 EXPECT_FALSE(Point(1, 2).IsOrigin());
37 EXPECT_FALSE(Point(-1, 0).IsOrigin());
38 EXPECT_FALSE(Point(0, -1).IsOrigin());
39 EXPECT_FALSE(Point(-1, -2).IsOrigin());
40 EXPECT_TRUE(Point(0, 0).IsOrigin());
42 EXPECT_FALSE(PointF(0.1f
, 0).IsOrigin());
43 EXPECT_FALSE(PointF(0, 0.1f
).IsOrigin());
44 EXPECT_FALSE(PointF(0.1f
, 2).IsOrigin());
45 EXPECT_FALSE(PointF(-0.1f
, 0).IsOrigin());
46 EXPECT_FALSE(PointF(0, -0.1f
).IsOrigin());
47 EXPECT_FALSE(PointF(-0.1f
, -2).IsOrigin());
48 EXPECT_TRUE(PointF(0, 0).IsOrigin());
51 TEST(PointTest
, VectorArithmetic
) {
60 { Point(4, 2), a
+ v1
},
61 { Point(-2, 8), a
- v1
},
64 { a
, a
+ Vector2d() },
65 { Point(12, 1), a
+ v1
- v2
},
66 { Point(-10, 9), a
- v1
+ v2
}
69 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
)
70 EXPECT_EQ(tests
[i
].expected
.ToString(), tests
[i
].actual
.ToString());
73 TEST(PointTest
, OffsetFromPoint
) {
76 EXPECT_EQ(Vector2d(-20 - 1, 8 - 5).ToString(), (b
- a
).ToString());
79 TEST(PointTest
, ToRoundedPoint
) {
80 EXPECT_EQ(Point(0, 0), ToRoundedPoint(PointF(0, 0)));
81 EXPECT_EQ(Point(0, 0), ToRoundedPoint(PointF(0.0001f
, 0.0001f
)));
82 EXPECT_EQ(Point(0, 0), ToRoundedPoint(PointF(0.4999f
, 0.4999f
)));
83 EXPECT_EQ(Point(1, 1), ToRoundedPoint(PointF(0.5f
, 0.5f
)));
84 EXPECT_EQ(Point(1, 1), ToRoundedPoint(PointF(0.9999f
, 0.9999f
)));
86 EXPECT_EQ(Point(10, 10), ToRoundedPoint(PointF(10, 10)));
87 EXPECT_EQ(Point(10, 10), ToRoundedPoint(PointF(10.0001f
, 10.0001f
)));
88 EXPECT_EQ(Point(10, 10), ToRoundedPoint(PointF(10.4999f
, 10.4999f
)));
89 EXPECT_EQ(Point(11, 11), ToRoundedPoint(PointF(10.5f
, 10.5f
)));
90 EXPECT_EQ(Point(11, 11), ToRoundedPoint(PointF(10.9999f
, 10.9999f
)));
92 EXPECT_EQ(Point(-10, -10), ToRoundedPoint(PointF(-10, -10)));
93 EXPECT_EQ(Point(-10, -10), ToRoundedPoint(PointF(-10.0001f
, -10.0001f
)));
94 EXPECT_EQ(Point(-10, -10), ToRoundedPoint(PointF(-10.4999f
, -10.4999f
)));
95 EXPECT_EQ(Point(-11, -11), ToRoundedPoint(PointF(-10.5f
, -10.5f
)));
96 EXPECT_EQ(Point(-11, -11), ToRoundedPoint(PointF(-10.9999f
, -10.9999f
)));
99 TEST(PointTest
, Scale
) {
100 EXPECT_EQ(PointF().ToString(), ScalePoint(Point(), 2).ToString());
101 EXPECT_EQ(PointF().ToString(), ScalePoint(Point(), 2, 2).ToString());
103 EXPECT_EQ(PointF(2, -2).ToString(),
104 ScalePoint(Point(1, -1), 2).ToString());
105 EXPECT_EQ(PointF(2, -2).ToString(),
106 ScalePoint(Point(1, -1), 2, 2).ToString());
117 EXPECT_EQ(PointF().ToString(), zero
.ToString());
118 EXPECT_EQ(PointF(6, -3).ToString(), one
.ToString());
121 TEST(PointTest
, ClampPoint
) {
125 EXPECT_EQ(Point(3, 5).ToString(), a
.ToString());
126 a
.SetToMax(Point(2, 4));
127 EXPECT_EQ(Point(3, 5).ToString(), a
.ToString());
128 a
.SetToMax(Point(3, 5));
129 EXPECT_EQ(Point(3, 5).ToString(), a
.ToString());
130 a
.SetToMax(Point(4, 2));
131 EXPECT_EQ(Point(4, 5).ToString(), a
.ToString());
132 a
.SetToMax(Point(8, 10));
133 EXPECT_EQ(Point(8, 10).ToString(), a
.ToString());
135 a
.SetToMin(Point(9, 11));
136 EXPECT_EQ(Point(8, 10).ToString(), a
.ToString());
137 a
.SetToMin(Point(8, 10));
138 EXPECT_EQ(Point(8, 10).ToString(), a
.ToString());
139 a
.SetToMin(Point(11, 9));
140 EXPECT_EQ(Point(8, 9).ToString(), a
.ToString());
141 a
.SetToMin(Point(7, 11));
142 EXPECT_EQ(Point(7, 9).ToString(), a
.ToString());
143 a
.SetToMin(Point(3, 5));
144 EXPECT_EQ(Point(3, 5).ToString(), a
.ToString());
147 TEST(PointTest
, ClampPointF
) {
150 a
= PointF(3.5f
, 5.5f
);
151 EXPECT_EQ(PointF(3.5f
, 5.5f
).ToString(), a
.ToString());
152 a
.SetToMax(PointF(2.5f
, 4.5f
));
153 EXPECT_EQ(PointF(3.5f
, 5.5f
).ToString(), a
.ToString());
154 a
.SetToMax(PointF(3.5f
, 5.5f
));
155 EXPECT_EQ(PointF(3.5f
, 5.5f
).ToString(), a
.ToString());
156 a
.SetToMax(PointF(4.5f
, 2.5f
));
157 EXPECT_EQ(PointF(4.5f
, 5.5f
).ToString(), a
.ToString());
158 a
.SetToMax(PointF(8.5f
, 10.5f
));
159 EXPECT_EQ(PointF(8.5f
, 10.5f
).ToString(), a
.ToString());
161 a
.SetToMin(PointF(9.5f
, 11.5f
));
162 EXPECT_EQ(PointF(8.5f
, 10.5f
).ToString(), a
.ToString());
163 a
.SetToMin(PointF(8.5f
, 10.5f
));
164 EXPECT_EQ(PointF(8.5f
, 10.5f
).ToString(), a
.ToString());
165 a
.SetToMin(PointF(11.5f
, 9.5f
));
166 EXPECT_EQ(PointF(8.5f
, 9.5f
).ToString(), a
.ToString());
167 a
.SetToMin(PointF(7.5f
, 11.5f
));
168 EXPECT_EQ(PointF(7.5f
, 9.5f
).ToString(), a
.ToString());
169 a
.SetToMin(PointF(3.5f
, 5.5f
));
170 EXPECT_EQ(PointF(3.5f
, 5.5f
).ToString(), a
.ToString());