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 "ui/gfx/point_base.h"
7 #include "base/basictypes.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/gfx/point.h"
10 #include "ui/gfx/point_conversions.h"
11 #include "ui/gfx/point_f.h"
17 int TestPointF(const PointF
& p
) {
23 TEST(PointTest
, ToPointF
) {
24 // Check that implicit conversion from integer to float compiles.
26 float x
= TestPointF(a
);
34 TEST(PointTest
, IsOrigin
) {
35 EXPECT_FALSE(Point(1, 0).IsOrigin());
36 EXPECT_FALSE(Point(0, 1).IsOrigin());
37 EXPECT_FALSE(Point(1, 2).IsOrigin());
38 EXPECT_FALSE(Point(-1, 0).IsOrigin());
39 EXPECT_FALSE(Point(0, -1).IsOrigin());
40 EXPECT_FALSE(Point(-1, -2).IsOrigin());
41 EXPECT_TRUE(Point(0, 0).IsOrigin());
43 EXPECT_FALSE(PointF(0.1f
, 0).IsOrigin());
44 EXPECT_FALSE(PointF(0, 0.1f
).IsOrigin());
45 EXPECT_FALSE(PointF(0.1f
, 2).IsOrigin());
46 EXPECT_FALSE(PointF(-0.1f
, 0).IsOrigin());
47 EXPECT_FALSE(PointF(0, -0.1f
).IsOrigin());
48 EXPECT_FALSE(PointF(-0.1f
, -2).IsOrigin());
49 EXPECT_TRUE(PointF(0, 0).IsOrigin());
52 TEST(PointTest
, VectorArithmetic
) {
61 { Point(4, 2), a
+ v1
},
62 { Point(-2, 8), a
- v1
},
65 { a
, a
+ Vector2d() },
66 { Point(12, 1), a
+ v1
- v2
},
67 { Point(-10, 9), a
- v1
+ v2
}
70 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
)
71 EXPECT_EQ(tests
[i
].expected
.ToString(), tests
[i
].actual
.ToString());
74 TEST(PointTest
, OffsetFromPoint
) {
77 EXPECT_EQ(Vector2d(-20 - 1, 8 - 5).ToString(), (b
- a
).ToString());
80 TEST(PointTest
, ToRoundedPoint
) {
81 EXPECT_EQ(Point(0, 0), ToRoundedPoint(PointF(0, 0)));
82 EXPECT_EQ(Point(0, 0), ToRoundedPoint(PointF(0.0001f
, 0.0001f
)));
83 EXPECT_EQ(Point(0, 0), ToRoundedPoint(PointF(0.4999f
, 0.4999f
)));
84 EXPECT_EQ(Point(1, 1), ToRoundedPoint(PointF(0.5f
, 0.5f
)));
85 EXPECT_EQ(Point(1, 1), ToRoundedPoint(PointF(0.9999f
, 0.9999f
)));
87 EXPECT_EQ(Point(10, 10), ToRoundedPoint(PointF(10, 10)));
88 EXPECT_EQ(Point(10, 10), ToRoundedPoint(PointF(10.0001f
, 10.0001f
)));
89 EXPECT_EQ(Point(10, 10), ToRoundedPoint(PointF(10.4999f
, 10.4999f
)));
90 EXPECT_EQ(Point(11, 11), ToRoundedPoint(PointF(10.5f
, 10.5f
)));
91 EXPECT_EQ(Point(11, 11), ToRoundedPoint(PointF(10.9999f
, 10.9999f
)));
93 EXPECT_EQ(Point(-10, -10), ToRoundedPoint(PointF(-10, -10)));
94 EXPECT_EQ(Point(-10, -10), ToRoundedPoint(PointF(-10.0001f
, -10.0001f
)));
95 EXPECT_EQ(Point(-10, -10), ToRoundedPoint(PointF(-10.4999f
, -10.4999f
)));
96 EXPECT_EQ(Point(-11, -11), ToRoundedPoint(PointF(-10.5f
, -10.5f
)));
97 EXPECT_EQ(Point(-11, -11), ToRoundedPoint(PointF(-10.9999f
, -10.9999f
)));
100 TEST(PointTest
, Scale
) {
101 EXPECT_EQ(PointF().ToString(), ScalePoint(Point(), 2).ToString());
102 EXPECT_EQ(PointF().ToString(), ScalePoint(Point(), 2, 2).ToString());
104 EXPECT_EQ(PointF(2, -2).ToString(),
105 ScalePoint(Point(1, -1), 2).ToString());
106 EXPECT_EQ(PointF(2, -2).ToString(),
107 ScalePoint(Point(1, -1), 2, 2).ToString());
118 EXPECT_EQ(PointF().ToString(), zero
.ToString());
119 EXPECT_EQ(PointF(6, -3).ToString(), one
.ToString());
122 TEST(PointTest
, ClampPoint
) {
126 EXPECT_EQ(Point(3, 5).ToString(), a
.ToString());
127 a
.SetToMax(Point(2, 4));
128 EXPECT_EQ(Point(3, 5).ToString(), a
.ToString());
129 a
.SetToMax(Point(3, 5));
130 EXPECT_EQ(Point(3, 5).ToString(), a
.ToString());
131 a
.SetToMax(Point(4, 2));
132 EXPECT_EQ(Point(4, 5).ToString(), a
.ToString());
133 a
.SetToMax(Point(8, 10));
134 EXPECT_EQ(Point(8, 10).ToString(), a
.ToString());
136 a
.SetToMin(Point(9, 11));
137 EXPECT_EQ(Point(8, 10).ToString(), a
.ToString());
138 a
.SetToMin(Point(8, 10));
139 EXPECT_EQ(Point(8, 10).ToString(), a
.ToString());
140 a
.SetToMin(Point(11, 9));
141 EXPECT_EQ(Point(8, 9).ToString(), a
.ToString());
142 a
.SetToMin(Point(7, 11));
143 EXPECT_EQ(Point(7, 9).ToString(), a
.ToString());
144 a
.SetToMin(Point(3, 5));
145 EXPECT_EQ(Point(3, 5).ToString(), a
.ToString());
148 TEST(PointTest
, ClampPointF
) {
151 a
= PointF(3.5f
, 5.5f
);
152 EXPECT_EQ(PointF(3.5f
, 5.5f
).ToString(), a
.ToString());
153 a
.SetToMax(PointF(2.5f
, 4.5f
));
154 EXPECT_EQ(PointF(3.5f
, 5.5f
).ToString(), a
.ToString());
155 a
.SetToMax(PointF(3.5f
, 5.5f
));
156 EXPECT_EQ(PointF(3.5f
, 5.5f
).ToString(), a
.ToString());
157 a
.SetToMax(PointF(4.5f
, 2.5f
));
158 EXPECT_EQ(PointF(4.5f
, 5.5f
).ToString(), a
.ToString());
159 a
.SetToMax(PointF(8.5f
, 10.5f
));
160 EXPECT_EQ(PointF(8.5f
, 10.5f
).ToString(), a
.ToString());
162 a
.SetToMin(PointF(9.5f
, 11.5f
));
163 EXPECT_EQ(PointF(8.5f
, 10.5f
).ToString(), a
.ToString());
164 a
.SetToMin(PointF(8.5f
, 10.5f
));
165 EXPECT_EQ(PointF(8.5f
, 10.5f
).ToString(), a
.ToString());
166 a
.SetToMin(PointF(11.5f
, 9.5f
));
167 EXPECT_EQ(PointF(8.5f
, 9.5f
).ToString(), a
.ToString());
168 a
.SetToMin(PointF(7.5f
, 11.5f
));
169 EXPECT_EQ(PointF(7.5f
, 9.5f
).ToString(), a
.ToString());
170 a
.SetToMin(PointF(3.5f
, 5.5f
));
171 EXPECT_EQ(PointF(3.5f
, 5.5f
).ToString(), a
.ToString());