OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "testing/gtest/include/gtest/gtest.h" | 6 #include "testing/gtest/include/gtest/gtest.h" |
7 #include "ui/gfx/rect.h" | 7 #include "ui/gfx/rect.h" |
8 #include "ui/gfx/rect_conversions.h" | 8 #include "ui/gfx/rect_conversions.h" |
9 #include "ui/gfx/skia_util.h" | 9 #include "ui/gfx/skia_util.h" |
10 | 10 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 3, 1, 4, 2, | 100 3, 1, 4, 2, |
101 3, 1, 1, 2 }, | 101 3, 1, 1, 2 }, |
102 { 3, 0, 2, 2, // gap | 102 { 3, 0, 2, 2, // gap |
103 0, 0, 2, 2, | 103 0, 0, 2, 2, |
104 0, 0, 0, 0 } | 104 0, 0, 0, 0 } |
105 }; | 105 }; |
106 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 106 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
107 gfx::Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1); | 107 gfx::Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1); |
108 gfx::Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2); | 108 gfx::Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2); |
109 gfx::Rect r3(tests[i].x3, tests[i].y3, tests[i].w3, tests[i].h3); | 109 gfx::Rect r3(tests[i].x3, tests[i].y3, tests[i].w3, tests[i].h3); |
110 gfx::Rect ir = r1.Intersect(r2); | 110 gfx::Rect ir = r1; |
| 111 ir.Intersect(r2); |
111 EXPECT_EQ(r3.x(), ir.x()); | 112 EXPECT_EQ(r3.x(), ir.x()); |
112 EXPECT_EQ(r3.y(), ir.y()); | 113 EXPECT_EQ(r3.y(), ir.y()); |
113 EXPECT_EQ(r3.width(), ir.width()); | 114 EXPECT_EQ(r3.width(), ir.width()); |
114 EXPECT_EQ(r3.height(), ir.height()); | 115 EXPECT_EQ(r3.height(), ir.height()); |
115 } | 116 } |
116 } | 117 } |
117 | 118 |
118 TEST(RectTest, Union) { | 119 TEST(RectTest, Union) { |
119 static const struct Test { | 120 static const struct Test { |
120 int x1; // rect 1 | 121 int x1; // rect 1 |
(...skipping 28 matching lines...) Expand all Loading... |
149 0, 0, 2, 2, | 150 0, 0, 2, 2, |
150 0, 0, 5, 5 }, | 151 0, 0, 5, 5 }, |
151 { 0, 0, 0, 0, // union with empty rect | 152 { 0, 0, 0, 0, // union with empty rect |
152 2, 2, 2, 2, | 153 2, 2, 2, 2, |
153 2, 2, 2, 2 } | 154 2, 2, 2, 2 } |
154 }; | 155 }; |
155 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 156 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
156 gfx::Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1); | 157 gfx::Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1); |
157 gfx::Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2); | 158 gfx::Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2); |
158 gfx::Rect r3(tests[i].x3, tests[i].y3, tests[i].w3, tests[i].h3); | 159 gfx::Rect r3(tests[i].x3, tests[i].y3, tests[i].w3, tests[i].h3); |
159 gfx::Rect u = r1.Union(r2); | 160 gfx::Rect u = r1; |
| 161 u.Union(r2); |
160 EXPECT_EQ(r3.x(), u.x()); | 162 EXPECT_EQ(r3.x(), u.x()); |
161 EXPECT_EQ(r3.y(), u.y()); | 163 EXPECT_EQ(r3.y(), u.y()); |
162 EXPECT_EQ(r3.width(), u.width()); | 164 EXPECT_EQ(r3.width(), u.width()); |
163 EXPECT_EQ(r3.height(), u.height()); | 165 EXPECT_EQ(r3.height(), u.height()); |
164 } | 166 } |
165 } | 167 } |
166 | 168 |
167 TEST(RectTest, Equals) { | 169 TEST(RectTest, Equals) { |
168 ASSERT_TRUE(gfx::Rect(0, 0, 0, 0) == gfx::Rect(0, 0, 0, 0)); | 170 ASSERT_TRUE(gfx::Rect(0, 0, 0, 0) == gfx::Rect(0, 0, 0, 0)); |
169 ASSERT_TRUE(gfx::Rect(1, 2, 3, 4) == gfx::Rect(1, 2, 3, 4)); | 171 ASSERT_TRUE(gfx::Rect(1, 2, 3, 4) == gfx::Rect(1, 2, 3, 4)); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 0, 0, 3, 3, | 203 0, 0, 3, 3, |
202 0, 0, 3, 3 }, | 204 0, 0, 3, 3 }, |
203 { 2, 2, 1, 1, | 205 { 2, 2, 1, 1, |
204 0, 0, 3, 3, | 206 0, 0, 3, 3, |
205 2, 2, 1, 1 } | 207 2, 2, 1, 1 } |
206 }; | 208 }; |
207 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 209 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
208 gfx::Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1); | 210 gfx::Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1); |
209 gfx::Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2); | 211 gfx::Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2); |
210 gfx::Rect r3(tests[i].x3, tests[i].y3, tests[i].w3, tests[i].h3); | 212 gfx::Rect r3(tests[i].x3, tests[i].y3, tests[i].w3, tests[i].h3); |
211 gfx::Rect u(r1.AdjustToFit(r2)); | 213 gfx::Rect u = r1; |
| 214 u.AdjustToFit(r2); |
212 EXPECT_EQ(r3.x(), u.x()); | 215 EXPECT_EQ(r3.x(), u.x()); |
213 EXPECT_EQ(r3.y(), u.y()); | 216 EXPECT_EQ(r3.y(), u.y()); |
214 EXPECT_EQ(r3.width(), u.width()); | 217 EXPECT_EQ(r3.width(), u.width()); |
215 EXPECT_EQ(r3.height(), u.height()); | 218 EXPECT_EQ(r3.height(), u.height()); |
216 } | 219 } |
217 } | 220 } |
218 | 221 |
219 TEST(RectTest, Subtract) { | 222 TEST(RectTest, Subtract) { |
| 223 gfx::Rect result; |
| 224 |
220 // Matching | 225 // Matching |
221 EXPECT_TRUE( | 226 result = gfx::Rect(10, 10, 20, 20); |
222 gfx::Rect(10, 10, 20, 20).Subtract( | 227 result.Subtract(gfx::Rect(10, 10, 20, 20)); |
223 gfx::Rect(10, 10, 20, 20)) == | 228 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(), result.ToString()); |
224 gfx::Rect(0, 0, 0, 0)); | |
225 | 229 |
226 // Contains | 230 // Contains |
227 EXPECT_TRUE( | 231 result = gfx::Rect(10, 10, 20, 20); |
228 gfx::Rect(10, 10, 20, 20).Subtract( | 232 result.Subtract(gfx::Rect(5, 5, 30, 30)); |
229 gfx::Rect(5, 5, 30, 30)) == | 233 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(), result.ToString()); |
230 gfx::Rect(0, 0, 0, 0)); | |
231 | 234 |
232 // No intersection | 235 // No intersection |
233 EXPECT_TRUE( | 236 result = gfx::Rect(10, 10, 20, 20); |
234 gfx::Rect(10, 10, 20, 20).Subtract( | 237 result.Subtract(gfx::Rect(30, 30, 30, 30)); |
235 gfx::Rect(30, 30, 20, 20)) == | 238 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), result.ToString()); |
236 gfx::Rect(10, 10, 20, 20)); | |
237 | 239 |
238 // Not a complete intersection in either direction | 240 // Not a complete intersection in either direction |
239 EXPECT_TRUE( | 241 result = gfx::Rect(10, 10, 20, 20); |
240 gfx::Rect(10, 10, 20, 20).Subtract( | 242 result.Subtract(gfx::Rect(15, 15, 20, 20)); |
241 gfx::Rect(15, 15, 20, 20)) == | 243 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), result.ToString()); |
242 gfx::Rect(10, 10, 20, 20)); | |
243 | 244 |
244 // Complete intersection in the x-direction | 245 // Complete intersection in the x-direction |
245 EXPECT_TRUE( | 246 result = gfx::Rect(10, 10, 20, 20); |
246 gfx::Rect(10, 10, 20, 20).Subtract( | 247 result.Subtract(gfx::Rect(10, 15, 20, 20)); |
247 gfx::Rect(10, 15, 20, 20)) == | 248 EXPECT_EQ(gfx::Rect(10, 10, 20, 5).ToString(), result.ToString()); |
248 gfx::Rect(10, 10, 20, 5)); | |
249 | 249 |
250 // Complete intersection in the x-direction | 250 // Complete intersection in the x-direction |
251 EXPECT_TRUE( | 251 result = gfx::Rect(10, 10, 20, 20); |
252 gfx::Rect(10, 10, 20, 20).Subtract( | 252 result.Subtract(gfx::Rect(5, 15, 30, 20)); |
253 gfx::Rect(5, 15, 30, 20)) == | 253 EXPECT_EQ(gfx::Rect(10, 10, 20, 5).ToString(), result.ToString()); |
254 gfx::Rect(10, 10, 20, 5)); | |
255 | 254 |
256 // Complete intersection in the x-direction | 255 // Complete intersection in the x-direction |
257 EXPECT_TRUE( | 256 result = gfx::Rect(10, 10, 20, 20); |
258 gfx::Rect(10, 10, 20, 20).Subtract( | 257 result.Subtract(gfx::Rect(5, 5, 30, 20)); |
259 gfx::Rect(5, 5, 30, 20)) == | 258 EXPECT_EQ(gfx::Rect(10, 25, 20, 5).ToString(), result.ToString()); |
260 gfx::Rect(10, 25, 20, 5)); | |
261 | 259 |
262 // Complete intersection in the y-direction | 260 // Complete intersection in the y-direction |
263 EXPECT_TRUE( | 261 result = gfx::Rect(10, 10, 20, 20); |
264 gfx::Rect(10, 10, 20, 20).Subtract( | 262 result.Subtract(gfx::Rect(10, 10, 10, 30)); |
265 gfx::Rect(10, 10, 10, 30)) == | 263 EXPECT_EQ(gfx::Rect(20, 10, 10, 20).ToString(), result.ToString()); |
266 gfx::Rect(20, 10, 10, 20)); | |
267 | 264 |
268 // Complete intersection in the y-direction | 265 // Complete intersection in the y-direction |
269 EXPECT_TRUE( | 266 result = gfx::Rect(10, 10, 20, 20); |
270 gfx::Rect(10, 10, 20, 20).Subtract( | 267 result.Subtract(gfx::Rect(5, 5, 20, 30)); |
271 gfx::Rect(5, 5, 20, 30)) == | 268 EXPECT_EQ(gfx::Rect(25, 10, 5, 20).ToString(), result.ToString()); |
272 gfx::Rect(25, 10, 5, 20)); | |
273 } | 269 } |
274 | 270 |
275 TEST(RectTest, IsEmpty) { | 271 TEST(RectTest, IsEmpty) { |
276 EXPECT_TRUE(gfx::Rect(0, 0, 0, 0).IsEmpty()); | 272 EXPECT_TRUE(gfx::Rect(0, 0, 0, 0).IsEmpty()); |
277 EXPECT_TRUE(gfx::Rect(0, 0, 0, 0).size().IsEmpty()); | 273 EXPECT_TRUE(gfx::Rect(0, 0, 0, 0).size().IsEmpty()); |
278 EXPECT_TRUE(gfx::Rect(0, 0, 10, 0).IsEmpty()); | 274 EXPECT_TRUE(gfx::Rect(0, 0, 10, 0).IsEmpty()); |
279 EXPECT_TRUE(gfx::Rect(0, 0, 10, 0).size().IsEmpty()); | 275 EXPECT_TRUE(gfx::Rect(0, 0, 10, 0).size().IsEmpty()); |
280 EXPECT_TRUE(gfx::Rect(0, 0, 0, 10).IsEmpty()); | 276 EXPECT_TRUE(gfx::Rect(0, 0, 0, 10).IsEmpty()); |
281 EXPECT_TRUE(gfx::Rect(0, 0, 0, 10).size().IsEmpty()); | 277 EXPECT_TRUE(gfx::Rect(0, 0, 0, 10).size().IsEmpty()); |
282 EXPECT_FALSE(gfx::Rect(0, 0, 10, 10).IsEmpty()); | 278 EXPECT_FALSE(gfx::Rect(0, 0, 10, 10).IsEmpty()); |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 std::numeric_limits<float>::max() }, | 439 std::numeric_limits<float>::max() }, |
444 { 3, 3, 3, 3, | 440 { 3, 3, 3, 3, |
445 -1.0f, | 441 -1.0f, |
446 -3.0f, -3.0f, 0.0f, 0.0f } | 442 -3.0f, -3.0f, 0.0f, 0.0f } |
447 }; | 443 }; |
448 | 444 |
449 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 445 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
450 gfx::Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1); | 446 gfx::Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1); |
451 gfx::RectF r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2); | 447 gfx::RectF r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2); |
452 | 448 |
453 gfx::RectF scaled = r1.Scale(tests[i].scale); | 449 gfx::RectF scaled = r1; |
| 450 scaled.Scale(tests[i].scale); |
454 EXPECT_FLOAT_AND_NAN_EQ(r2.x(), scaled.x()); | 451 EXPECT_FLOAT_AND_NAN_EQ(r2.x(), scaled.x()); |
455 EXPECT_FLOAT_AND_NAN_EQ(r2.y(), scaled.y()); | 452 EXPECT_FLOAT_AND_NAN_EQ(r2.y(), scaled.y()); |
456 EXPECT_FLOAT_AND_NAN_EQ(r2.width(), scaled.width()); | 453 EXPECT_FLOAT_AND_NAN_EQ(r2.width(), scaled.width()); |
457 EXPECT_FLOAT_AND_NAN_EQ(r2.height(), scaled.height()); | 454 EXPECT_FLOAT_AND_NAN_EQ(r2.height(), scaled.height()); |
458 } | 455 } |
459 } | 456 } |
460 | 457 |
461 TEST(RectTest, ToEnclosedRect) { | 458 TEST(RectTest, ToEnclosedRect) { |
462 static const struct Test { | 459 static const struct Test { |
463 float x1; // source | 460 float x1; // source |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
565 gfx::Rect test1(rect_1); | 562 gfx::Rect test1(rect_1); |
566 gfx::Rect test2(rect_2); | 563 gfx::Rect test2(rect_2); |
567 } | 564 } |
568 #endif | 565 #endif |
569 | 566 |
570 TEST(RectTest, ToRectF) { | 567 TEST(RectTest, ToRectF) { |
571 // Check that implicit conversion from integer to float compiles. | 568 // Check that implicit conversion from integer to float compiles. |
572 gfx::Rect a(10, 20, 30, 40); | 569 gfx::Rect a(10, 20, 30, 40); |
573 gfx::RectF b(10, 20, 30, 40); | 570 gfx::RectF b(10, 20, 30, 40); |
574 | 571 |
575 gfx::RectF intersect = b.Intersect(a); | 572 gfx::RectF intersect = b; |
| 573 intersect.Intersect(a); |
576 EXPECT_EQ(b.ToString(), intersect.ToString()); | 574 EXPECT_EQ(b.ToString(), intersect.ToString()); |
577 | 575 |
578 EXPECT_EQ(a, b); | 576 EXPECT_EQ(a, b); |
579 EXPECT_EQ(b, a); | 577 EXPECT_EQ(b, a); |
580 } | 578 } |
581 | 579 |
582 } // namespace ui | 580 } // namespace ui |
OLD | NEW |