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_conversions.h"
9 #include "ui/gfx/geometry/point_f.h"
15 int TestPointF(const PointF
& p
) {
21 TEST(PointTest
, ToPointF
) {
22 // Check that implicit conversion from integer to float compiles.
24 float x
= TestPointF(a
);
32 TEST(PointTest
, IsOrigin
) {
33 EXPECT_FALSE(Point(1, 0).IsOrigin());
34 EXPECT_FALSE(Point(0, 1).IsOrigin());
35 EXPECT_FALSE(Point(1, 2).IsOrigin());
36 EXPECT_FALSE(Point(-1, 0).IsOrigin());
37 EXPECT_FALSE(Point(0, -1).IsOrigin());
38 EXPECT_FALSE(Point(-1, -2).IsOrigin());
39 EXPECT_TRUE(Point(0, 0).IsOrigin());
41 EXPECT_FALSE(PointF(0.1f
, 0).IsOrigin());
42 EXPECT_FALSE(PointF(0, 0.1f
).IsOrigin());
43 EXPECT_FALSE(PointF(0.1f
, 2).IsOrigin());
44 EXPECT_FALSE(PointF(-0.1f
, 0).IsOrigin());
45 EXPECT_FALSE(PointF(0, -0.1f
).IsOrigin());
46 EXPECT_FALSE(PointF(-0.1f
, -2).IsOrigin());
47 EXPECT_TRUE(PointF(0, 0).IsOrigin());
50 TEST(PointTest
, VectorArithmetic
) {
59 { Point(4, 2), a
+ v1
},
60 { Point(-2, 8), a
- v1
},
63 { a
, a
+ Vector2d() },
64 { Point(12, 1), a
+ v1
- v2
},
65 { Point(-10, 9), a
- v1
+ v2
}
68 for (size_t i
= 0; i
< arraysize(tests
); ++i
)
69 EXPECT_EQ(tests
[i
].expected
.ToString(), tests
[i
].actual
.ToString());
72 TEST(PointTest
, OffsetFromPoint
) {
75 EXPECT_EQ(Vector2d(-20 - 1, 8 - 5).ToString(), (b
- a
).ToString());
78 TEST(PointTest
, ToRoundedPoint
) {
79 EXPECT_EQ(Point(0, 0), ToRoundedPoint(PointF(0, 0)));
80 EXPECT_EQ(Point(0, 0), ToRoundedPoint(PointF(0.0001f
, 0.0001f
)));
81 EXPECT_EQ(Point(0, 0), ToRoundedPoint(PointF(0.4999f
, 0.4999f
)));
82 EXPECT_EQ(Point(1, 1), ToRoundedPoint(PointF(0.5f
, 0.5f
)));
83 EXPECT_EQ(Point(1, 1), ToRoundedPoint(PointF(0.9999f
, 0.9999f
)));
85 EXPECT_EQ(Point(10, 10), ToRoundedPoint(PointF(10, 10)));
86 EXPECT_EQ(Point(10, 10), ToRoundedPoint(PointF(10.0001f
, 10.0001f
)));
87 EXPECT_EQ(Point(10, 10), ToRoundedPoint(PointF(10.4999f
, 10.4999f
)));
88 EXPECT_EQ(Point(11, 11), ToRoundedPoint(PointF(10.5f
, 10.5f
)));
89 EXPECT_EQ(Point(11, 11), ToRoundedPoint(PointF(10.9999f
, 10.9999f
)));
91 EXPECT_EQ(Point(-10, -10), ToRoundedPoint(PointF(-10, -10)));
92 EXPECT_EQ(Point(-10, -10), ToRoundedPoint(PointF(-10.0001f
, -10.0001f
)));
93 EXPECT_EQ(Point(-10, -10), ToRoundedPoint(PointF(-10.4999f
, -10.4999f
)));
94 EXPECT_EQ(Point(-11, -11), ToRoundedPoint(PointF(-10.5f
, -10.5f
)));
95 EXPECT_EQ(Point(-11, -11), ToRoundedPoint(PointF(-10.9999f
, -10.9999f
)));
98 TEST(PointTest
, Scale
) {
99 EXPECT_EQ(PointF().ToString(), ScalePoint(Point(), 2).ToString());
100 EXPECT_EQ(PointF().ToString(), ScalePoint(Point(), 2, 2).ToString());
102 EXPECT_EQ(PointF(2, -2).ToString(),
103 ScalePoint(Point(1, -1), 2).ToString());
104 EXPECT_EQ(PointF(2, -2).ToString(),
105 ScalePoint(Point(1, -1), 2, 2).ToString());
116 EXPECT_EQ(PointF().ToString(), zero
.ToString());
117 EXPECT_EQ(PointF(6, -3).ToString(), one
.ToString());
120 TEST(PointTest
, ClampPoint
) {
124 EXPECT_EQ(Point(3, 5).ToString(), a
.ToString());
125 a
.SetToMax(Point(2, 4));
126 EXPECT_EQ(Point(3, 5).ToString(), a
.ToString());
127 a
.SetToMax(Point(3, 5));
128 EXPECT_EQ(Point(3, 5).ToString(), a
.ToString());
129 a
.SetToMax(Point(4, 2));
130 EXPECT_EQ(Point(4, 5).ToString(), a
.ToString());
131 a
.SetToMax(Point(8, 10));
132 EXPECT_EQ(Point(8, 10).ToString(), a
.ToString());
134 a
.SetToMin(Point(9, 11));
135 EXPECT_EQ(Point(8, 10).ToString(), a
.ToString());
136 a
.SetToMin(Point(8, 10));
137 EXPECT_EQ(Point(8, 10).ToString(), a
.ToString());
138 a
.SetToMin(Point(11, 9));
139 EXPECT_EQ(Point(8, 9).ToString(), a
.ToString());
140 a
.SetToMin(Point(7, 11));
141 EXPECT_EQ(Point(7, 9).ToString(), a
.ToString());
142 a
.SetToMin(Point(3, 5));
143 EXPECT_EQ(Point(3, 5).ToString(), a
.ToString());
146 TEST(PointTest
, ClampPointF
) {
149 a
= PointF(3.5f
, 5.5f
);
150 EXPECT_EQ(PointF(3.5f
, 5.5f
).ToString(), a
.ToString());
151 a
.SetToMax(PointF(2.5f
, 4.5f
));
152 EXPECT_EQ(PointF(3.5f
, 5.5f
).ToString(), a
.ToString());
153 a
.SetToMax(PointF(3.5f
, 5.5f
));
154 EXPECT_EQ(PointF(3.5f
, 5.5f
).ToString(), a
.ToString());
155 a
.SetToMax(PointF(4.5f
, 2.5f
));
156 EXPECT_EQ(PointF(4.5f
, 5.5f
).ToString(), a
.ToString());
157 a
.SetToMax(PointF(8.5f
, 10.5f
));
158 EXPECT_EQ(PointF(8.5f
, 10.5f
).ToString(), a
.ToString());
160 a
.SetToMin(PointF(9.5f
, 11.5f
));
161 EXPECT_EQ(PointF(8.5f
, 10.5f
).ToString(), a
.ToString());
162 a
.SetToMin(PointF(8.5f
, 10.5f
));
163 EXPECT_EQ(PointF(8.5f
, 10.5f
).ToString(), a
.ToString());
164 a
.SetToMin(PointF(11.5f
, 9.5f
));
165 EXPECT_EQ(PointF(8.5f
, 9.5f
).ToString(), a
.ToString());
166 a
.SetToMin(PointF(7.5f
, 11.5f
));
167 EXPECT_EQ(PointF(7.5f
, 9.5f
).ToString(), a
.ToString());
168 a
.SetToMin(PointF(3.5f
, 5.5f
));
169 EXPECT_EQ(PointF(3.5f
, 5.5f
).ToString(), a
.ToString());