Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / ui / gfx / rect_unittest.cc
blobf0bf7d5170d1e8645ac6aca07fb014c0b4270d3c
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
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "ui/gfx/rect.h"
8 #include "ui/gfx/rect_conversions.h"
9 #include "ui/gfx/skia_util.h"
11 #include <limits>
13 namespace gfx {
15 TEST(RectTest, Contains) {
16 static const struct ContainsCase {
17 int rect_x;
18 int rect_y;
19 int rect_width;
20 int rect_height;
21 int point_x;
22 int point_y;
23 bool contained;
24 } contains_cases[] = {
25 {0, 0, 10, 10, 0, 0, true},
26 {0, 0, 10, 10, 5, 5, true},
27 {0, 0, 10, 10, 9, 9, true},
28 {0, 0, 10, 10, 5, 10, false},
29 {0, 0, 10, 10, 10, 5, false},
30 {0, 0, 10, 10, -1, -1, false},
31 {0, 0, 10, 10, 50, 50, false},
32 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
33 {0, 0, -10, -10, 0, 0, false},
34 #endif
36 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(contains_cases); ++i) {
37 const ContainsCase& value = contains_cases[i];
38 Rect rect(value.rect_x, value.rect_y, value.rect_width, value.rect_height);
39 EXPECT_EQ(value.contained, rect.Contains(value.point_x, value.point_y));
43 TEST(RectTest, Intersects) {
44 static const struct {
45 int x1; // rect 1
46 int y1;
47 int w1;
48 int h1;
49 int x2; // rect 2
50 int y2;
51 int w2;
52 int h2;
53 bool intersects;
54 } tests[] = {
55 { 0, 0, 0, 0, 0, 0, 0, 0, false },
56 { 0, 0, 10, 10, 0, 0, 10, 10, true },
57 { 0, 0, 10, 10, 10, 10, 10, 10, false },
58 { 10, 10, 10, 10, 0, 0, 10, 10, false },
59 { 10, 10, 10, 10, 5, 5, 10, 10, true },
60 { 10, 10, 10, 10, 15, 15, 10, 10, true },
61 { 10, 10, 10, 10, 20, 15, 10, 10, false },
62 { 10, 10, 10, 10, 21, 15, 10, 10, false }
64 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
65 Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
66 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
67 EXPECT_EQ(tests[i].intersects, r1.Intersects(r2));
71 TEST(RectTest, Intersect) {
72 static const struct {
73 int x1; // rect 1
74 int y1;
75 int w1;
76 int h1;
77 int x2; // rect 2
78 int y2;
79 int w2;
80 int h2;
81 int x3; // rect 3: the union of rects 1 and 2
82 int y3;
83 int w3;
84 int h3;
85 } tests[] = {
86 { 0, 0, 0, 0, // zeros
87 0, 0, 0, 0,
88 0, 0, 0, 0 },
89 { 0, 0, 4, 4, // equal
90 0, 0, 4, 4,
91 0, 0, 4, 4 },
92 { 0, 0, 4, 4, // neighboring
93 4, 4, 4, 4,
94 0, 0, 0, 0 },
95 { 0, 0, 4, 4, // overlapping corners
96 2, 2, 4, 4,
97 2, 2, 2, 2 },
98 { 0, 0, 4, 4, // T junction
99 3, 1, 4, 2,
100 3, 1, 1, 2 },
101 { 3, 0, 2, 2, // gap
102 0, 0, 2, 2,
103 0, 0, 0, 0 }
105 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
106 Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
107 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
108 Rect r3(tests[i].x3, tests[i].y3, tests[i].w3, tests[i].h3);
109 Rect ir = IntersectRects(r1, r2);
110 EXPECT_EQ(r3.x(), ir.x());
111 EXPECT_EQ(r3.y(), ir.y());
112 EXPECT_EQ(r3.width(), ir.width());
113 EXPECT_EQ(r3.height(), ir.height());
117 TEST(RectTest, Union) {
118 static const struct Test {
119 int x1; // rect 1
120 int y1;
121 int w1;
122 int h1;
123 int x2; // rect 2
124 int y2;
125 int w2;
126 int h2;
127 int x3; // rect 3: the union of rects 1 and 2
128 int y3;
129 int w3;
130 int h3;
131 } tests[] = {
132 { 0, 0, 0, 0,
133 0, 0, 0, 0,
134 0, 0, 0, 0 },
135 { 0, 0, 4, 4,
136 0, 0, 4, 4,
137 0, 0, 4, 4 },
138 { 0, 0, 4, 4,
139 4, 4, 4, 4,
140 0, 0, 8, 8 },
141 { 0, 0, 4, 4,
142 0, 5, 4, 4,
143 0, 0, 4, 9 },
144 { 0, 0, 2, 2,
145 3, 3, 2, 2,
146 0, 0, 5, 5 },
147 { 3, 3, 2, 2, // reverse r1 and r2 from previous test
148 0, 0, 2, 2,
149 0, 0, 5, 5 },
150 { 0, 0, 0, 0, // union with empty rect
151 2, 2, 2, 2,
152 2, 2, 2, 2 }
154 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
155 Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
156 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
157 Rect r3(tests[i].x3, tests[i].y3, tests[i].w3, tests[i].h3);
158 Rect u = UnionRects(r1, r2);
159 EXPECT_EQ(r3.x(), u.x());
160 EXPECT_EQ(r3.y(), u.y());
161 EXPECT_EQ(r3.width(), u.width());
162 EXPECT_EQ(r3.height(), u.height());
166 TEST(RectTest, Equals) {
167 ASSERT_TRUE(Rect(0, 0, 0, 0) == Rect(0, 0, 0, 0));
168 ASSERT_TRUE(Rect(1, 2, 3, 4) == Rect(1, 2, 3, 4));
169 ASSERT_FALSE(Rect(0, 0, 0, 0) == Rect(0, 0, 0, 1));
170 ASSERT_FALSE(Rect(0, 0, 0, 0) == Rect(0, 0, 1, 0));
171 ASSERT_FALSE(Rect(0, 0, 0, 0) == Rect(0, 1, 0, 0));
172 ASSERT_FALSE(Rect(0, 0, 0, 0) == Rect(1, 0, 0, 0));
175 TEST(RectTest, AdjustToFit) {
176 static const struct Test {
177 int x1; // source
178 int y1;
179 int w1;
180 int h1;
181 int x2; // target
182 int y2;
183 int w2;
184 int h2;
185 int x3; // rect 3: results of invoking AdjustToFit
186 int y3;
187 int w3;
188 int h3;
189 } tests[] = {
190 { 0, 0, 2, 2,
191 0, 0, 2, 2,
192 0, 0, 2, 2 },
193 { 2, 2, 3, 3,
194 0, 0, 4, 4,
195 1, 1, 3, 3 },
196 { -1, -1, 5, 5,
197 0, 0, 4, 4,
198 0, 0, 4, 4 },
199 { 2, 2, 4, 4,
200 0, 0, 3, 3,
201 0, 0, 3, 3 },
202 { 2, 2, 1, 1,
203 0, 0, 3, 3,
204 2, 2, 1, 1 }
206 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
207 Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
208 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
209 Rect r3(tests[i].x3, tests[i].y3, tests[i].w3, tests[i].h3);
210 Rect u = r1;
211 u.AdjustToFit(r2);
212 EXPECT_EQ(r3.x(), u.x());
213 EXPECT_EQ(r3.y(), u.y());
214 EXPECT_EQ(r3.width(), u.width());
215 EXPECT_EQ(r3.height(), u.height());
219 TEST(RectTest, Subtract) {
220 Rect result;
222 // Matching
223 result = Rect(10, 10, 20, 20);
224 result.Subtract(Rect(10, 10, 20, 20));
225 EXPECT_EQ(Rect(0, 0, 0, 0).ToString(), result.ToString());
227 // Contains
228 result = Rect(10, 10, 20, 20);
229 result.Subtract(Rect(5, 5, 30, 30));
230 EXPECT_EQ(Rect(0, 0, 0, 0).ToString(), result.ToString());
232 // No intersection
233 result = Rect(10, 10, 20, 20);
234 result.Subtract(Rect(30, 30, 30, 30));
235 EXPECT_EQ(Rect(10, 10, 20, 20).ToString(), result.ToString());
237 // Not a complete intersection in either direction
238 result = Rect(10, 10, 20, 20);
239 result.Subtract(Rect(15, 15, 20, 20));
240 EXPECT_EQ(Rect(10, 10, 20, 20).ToString(), result.ToString());
242 // Complete intersection in the x-direction
243 result = Rect(10, 10, 20, 20);
244 result.Subtract(Rect(10, 15, 20, 20));
245 EXPECT_EQ(Rect(10, 10, 20, 5).ToString(), result.ToString());
247 // Complete intersection in the x-direction
248 result = Rect(10, 10, 20, 20);
249 result.Subtract(Rect(5, 15, 30, 20));
250 EXPECT_EQ(Rect(10, 10, 20, 5).ToString(), result.ToString());
252 // Complete intersection in the x-direction
253 result = Rect(10, 10, 20, 20);
254 result.Subtract(Rect(5, 5, 30, 20));
255 EXPECT_EQ(Rect(10, 25, 20, 5).ToString(), result.ToString());
257 // Complete intersection in the y-direction
258 result = Rect(10, 10, 20, 20);
259 result.Subtract(Rect(10, 10, 10, 30));
260 EXPECT_EQ(Rect(20, 10, 10, 20).ToString(), result.ToString());
262 // Complete intersection in the y-direction
263 result = Rect(10, 10, 20, 20);
264 result.Subtract(Rect(5, 5, 20, 30));
265 EXPECT_EQ(Rect(25, 10, 5, 20).ToString(), result.ToString());
268 TEST(RectTest, IsEmpty) {
269 EXPECT_TRUE(Rect(0, 0, 0, 0).IsEmpty());
270 EXPECT_TRUE(Rect(0, 0, 0, 0).size().IsEmpty());
271 EXPECT_TRUE(Rect(0, 0, 10, 0).IsEmpty());
272 EXPECT_TRUE(Rect(0, 0, 10, 0).size().IsEmpty());
273 EXPECT_TRUE(Rect(0, 0, 0, 10).IsEmpty());
274 EXPECT_TRUE(Rect(0, 0, 0, 10).size().IsEmpty());
275 EXPECT_FALSE(Rect(0, 0, 10, 10).IsEmpty());
276 EXPECT_FALSE(Rect(0, 0, 10, 10).size().IsEmpty());
279 TEST(RectTest, SplitVertically) {
280 Rect left_half, right_half;
282 // Splitting when origin is (0, 0).
283 Rect(0, 0, 20, 20).SplitVertically(&left_half, &right_half);
284 EXPECT_TRUE(left_half == Rect(0, 0, 10, 20));
285 EXPECT_TRUE(right_half == Rect(10, 0, 10, 20));
287 // Splitting when origin is arbitrary.
288 Rect(10, 10, 20, 10).SplitVertically(&left_half, &right_half);
289 EXPECT_TRUE(left_half == Rect(10, 10, 10, 10));
290 EXPECT_TRUE(right_half == Rect(20, 10, 10, 10));
292 // Splitting a rectangle of zero width.
293 Rect(10, 10, 0, 10).SplitVertically(&left_half, &right_half);
294 EXPECT_TRUE(left_half == Rect(10, 10, 0, 10));
295 EXPECT_TRUE(right_half == Rect(10, 10, 0, 10));
297 // Splitting a rectangle of odd width.
298 Rect(10, 10, 5, 10).SplitVertically(&left_half, &right_half);
299 EXPECT_TRUE(left_half == Rect(10, 10, 2, 10));
300 EXPECT_TRUE(right_half == Rect(12, 10, 3, 10));
303 TEST(RectTest, CenterPoint) {
304 Point center;
306 // When origin is (0, 0).
307 center = Rect(0, 0, 20, 20).CenterPoint();
308 EXPECT_TRUE(center == Point(10, 10));
310 // When origin is even.
311 center = Rect(10, 10, 20, 20).CenterPoint();
312 EXPECT_TRUE(center == Point(20, 20));
314 // When origin is odd.
315 center = Rect(11, 11, 20, 20).CenterPoint();
316 EXPECT_TRUE(center == Point(21, 21));
318 // When 0 width or height.
319 center = Rect(10, 10, 0, 20).CenterPoint();
320 EXPECT_TRUE(center == Point(10, 20));
321 center = Rect(10, 10, 20, 0).CenterPoint();
322 EXPECT_TRUE(center == Point(20, 10));
324 // When an odd size.
325 center = Rect(10, 10, 21, 21).CenterPoint();
326 EXPECT_TRUE(center == Point(20, 20));
328 // When an odd size and position.
329 center = Rect(11, 11, 21, 21).CenterPoint();
330 EXPECT_TRUE(center == Point(21, 21));
333 TEST(RectTest, CenterPointF) {
334 PointF center;
336 // When origin is (0, 0).
337 center = RectF(0, 0, 20, 20).CenterPoint();
338 EXPECT_TRUE(center == PointF(10, 10));
340 // When origin is even.
341 center = RectF(10, 10, 20, 20).CenterPoint();
342 EXPECT_TRUE(center == PointF(20, 20));
344 // When origin is odd.
345 center = RectF(11, 11, 20, 20).CenterPoint();
346 EXPECT_TRUE(center == PointF(21, 21));
348 // When 0 width or height.
349 center = RectF(10, 10, 0, 20).CenterPoint();
350 EXPECT_TRUE(center == PointF(10, 20));
351 center = RectF(10, 10, 20, 0).CenterPoint();
352 EXPECT_TRUE(center == PointF(20, 10));
354 // When an odd size.
355 center = RectF(10, 10, 21, 21).CenterPoint();
356 EXPECT_TRUE(center == PointF(20.5f, 20.5f));
358 // When an odd size and position.
359 center = RectF(11, 11, 21, 21).CenterPoint();
360 EXPECT_TRUE(center == PointF(21.5f, 21.5f));
363 TEST(RectTest, SharesEdgeWith) {
364 Rect r(2, 3, 4, 5);
366 // Must be non-overlapping
367 EXPECT_FALSE(r.SharesEdgeWith(r));
369 Rect just_above(2, 1, 4, 2);
370 Rect just_below(2, 8, 4, 2);
371 Rect just_left(0, 3, 2, 5);
372 Rect just_right(6, 3, 2, 5);
374 EXPECT_TRUE(r.SharesEdgeWith(just_above));
375 EXPECT_TRUE(r.SharesEdgeWith(just_below));
376 EXPECT_TRUE(r.SharesEdgeWith(just_left));
377 EXPECT_TRUE(r.SharesEdgeWith(just_right));
379 // Wrong placement
380 Rect same_height_no_edge(0, 0, 1, 5);
381 Rect same_width_no_edge(0, 0, 4, 1);
383 EXPECT_FALSE(r.SharesEdgeWith(same_height_no_edge));
384 EXPECT_FALSE(r.SharesEdgeWith(same_width_no_edge));
386 Rect just_above_no_edge(2, 1, 5, 2); // too wide
387 Rect just_below_no_edge(2, 8, 3, 2); // too narrow
388 Rect just_left_no_edge(0, 3, 2, 6); // too tall
389 Rect just_right_no_edge(6, 3, 2, 4); // too short
391 EXPECT_FALSE(r.SharesEdgeWith(just_above_no_edge));
392 EXPECT_FALSE(r.SharesEdgeWith(just_below_no_edge));
393 EXPECT_FALSE(r.SharesEdgeWith(just_left_no_edge));
394 EXPECT_FALSE(r.SharesEdgeWith(just_right_no_edge));
397 TEST(RectTest, SkiaRectConversions) {
398 Rect isrc(10, 20, 30, 40);
399 RectF fsrc(10.5f, 20.5f, 30.5f, 40.5f);
401 SkIRect skirect = RectToSkIRect(isrc);
402 EXPECT_EQ(isrc.ToString(), SkIRectToRect(skirect).ToString());
404 SkRect skrect = RectToSkRect(isrc);
405 EXPECT_EQ(gfx::RectF(isrc).ToString(), SkRectToRectF(skrect).ToString());
407 skrect = RectFToSkRect(fsrc);
408 EXPECT_EQ(fsrc.ToString(), SkRectToRectF(skrect).ToString());
411 // Similar to EXPECT_FLOAT_EQ, but lets NaN equal NaN
412 #define EXPECT_FLOAT_AND_NAN_EQ(a, b) \
413 { if (a == a || b == b) { EXPECT_FLOAT_EQ(a, b); } }
415 TEST(RectTest, ScaleRect) {
416 static const struct Test {
417 int x1; // source
418 int y1;
419 int w1;
420 int h1;
421 float scale;
422 float x2; // target
423 float y2;
424 float w2;
425 float h2;
426 } tests[] = {
427 { 3, 3, 3, 3,
428 1.5f,
429 4.5f, 4.5f, 4.5f, 4.5f },
430 { 3, 3, 3, 3,
431 0.0f,
432 0.0f, 0.0f, 0.0f, 0.0f },
433 { 3, 3, 3, 3,
434 std::numeric_limits<float>::quiet_NaN(),
435 std::numeric_limits<float>::quiet_NaN(),
436 std::numeric_limits<float>::quiet_NaN(),
437 std::numeric_limits<float>::quiet_NaN(),
438 std::numeric_limits<float>::quiet_NaN() },
439 { 3, 3, 3, 3,
440 std::numeric_limits<float>::max(),
441 std::numeric_limits<float>::max(),
442 std::numeric_limits<float>::max(),
443 std::numeric_limits<float>::max(),
444 std::numeric_limits<float>::max() }
447 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
448 Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
449 RectF r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
451 RectF scaled = ScaleRect(r1, tests[i].scale);
452 EXPECT_FLOAT_AND_NAN_EQ(r2.x(), scaled.x());
453 EXPECT_FLOAT_AND_NAN_EQ(r2.y(), scaled.y());
454 EXPECT_FLOAT_AND_NAN_EQ(r2.width(), scaled.width());
455 EXPECT_FLOAT_AND_NAN_EQ(r2.height(), scaled.height());
459 TEST(RectTest, ToEnclosedRect) {
460 static const struct Test {
461 float x1; // source
462 float y1;
463 float w1;
464 float h1;
465 int x2; // target
466 int y2;
467 int w2;
468 int h2;
469 } tests [] = {
470 { 0.0f, 0.0f, 0.0f, 0.0f,
471 0, 0, 0, 0 },
472 { -1.5f, -1.5f, 3.0f, 3.0f,
473 -1, -1, 2, 2 },
474 { -1.5f, -1.5f, 3.5f, 3.5f,
475 -1, -1, 3, 3 },
476 { std::numeric_limits<float>::max(),
477 std::numeric_limits<float>::max(),
478 2.0f, 2.0f,
479 std::numeric_limits<int>::max(),
480 std::numeric_limits<int>::max(),
481 0, 0 },
482 { 0.0f, 0.0f,
483 std::numeric_limits<float>::max(),
484 std::numeric_limits<float>::max(),
485 0, 0,
486 std::numeric_limits<int>::max(),
487 std::numeric_limits<int>::max() },
488 { 20000.5f, 20000.5f, 0.5f, 0.5f,
489 20001, 20001, 0, 0 },
490 { std::numeric_limits<float>::quiet_NaN(),
491 std::numeric_limits<float>::quiet_NaN(),
492 std::numeric_limits<float>::quiet_NaN(),
493 std::numeric_limits<float>::quiet_NaN(),
494 0, 0, 0, 0 }
497 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
498 RectF r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
499 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
501 Rect enclosed = ToEnclosedRect(r1);
502 EXPECT_FLOAT_AND_NAN_EQ(r2.x(), enclosed.x());
503 EXPECT_FLOAT_AND_NAN_EQ(r2.y(), enclosed.y());
504 EXPECT_FLOAT_AND_NAN_EQ(r2.width(), enclosed.width());
505 EXPECT_FLOAT_AND_NAN_EQ(r2.height(), enclosed.height());
509 TEST(RectTest, ToEnclosingRect) {
510 static const struct Test {
511 float x1; // source
512 float y1;
513 float w1;
514 float h1;
515 int x2; // target
516 int y2;
517 int w2;
518 int h2;
519 } tests [] = {
520 { 0.0f, 0.0f, 0.0f, 0.0f,
521 0, 0, 0, 0 },
522 { -1.5f, -1.5f, 3.0f, 3.0f,
523 -2, -2, 4, 4 },
524 { -1.5f, -1.5f, 3.5f, 3.5f,
525 -2, -2, 4, 4 },
526 { std::numeric_limits<float>::max(),
527 std::numeric_limits<float>::max(),
528 2.0f, 2.0f,
529 std::numeric_limits<int>::max(),
530 std::numeric_limits<int>::max(),
531 0, 0 },
532 { 0.0f, 0.0f,
533 std::numeric_limits<float>::max(),
534 std::numeric_limits<float>::max(),
535 0, 0,
536 std::numeric_limits<int>::max(),
537 std::numeric_limits<int>::max() },
538 { 20000.5f, 20000.5f, 0.5f, 0.5f,
539 20000, 20000, 1, 1 },
540 { std::numeric_limits<float>::quiet_NaN(),
541 std::numeric_limits<float>::quiet_NaN(),
542 std::numeric_limits<float>::quiet_NaN(),
543 std::numeric_limits<float>::quiet_NaN(),
544 0, 0, 0, 0 }
547 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
548 RectF r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
549 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
551 Rect enclosed = ToEnclosingRect(r1);
552 EXPECT_FLOAT_AND_NAN_EQ(r2.x(), enclosed.x());
553 EXPECT_FLOAT_AND_NAN_EQ(r2.y(), enclosed.y());
554 EXPECT_FLOAT_AND_NAN_EQ(r2.width(), enclosed.width());
555 EXPECT_FLOAT_AND_NAN_EQ(r2.height(), enclosed.height());
559 TEST(RectTest, ToNearestRect) {
560 Rect rect;
561 EXPECT_EQ(rect.ToString(), ToNearestRect(RectF(rect)).ToString());
563 rect = Rect(-1, -1, 3, 3);
564 EXPECT_EQ(rect.ToString(), ToNearestRect(RectF(rect)).ToString());
566 RectF rectf(-1.00001f, -0.999999f, 3.0000001f, 2.999999f);
567 EXPECT_EQ(rect.ToString(), ToNearestRect(rectf).ToString());
570 TEST(RectTest, ToFlooredRect) {
571 static const struct Test {
572 float x1; // source
573 float y1;
574 float w1;
575 float h1;
576 int x2; // target
577 int y2;
578 int w2;
579 int h2;
580 } tests [] = {
581 { 0.0f, 0.0f, 0.0f, 0.0f,
582 0, 0, 0, 0 },
583 { -1.5f, -1.5f, 3.0f, 3.0f,
584 -2, -2, 3, 3 },
585 { -1.5f, -1.5f, 3.5f, 3.5f,
586 -2, -2, 3, 3 },
587 { 20000.5f, 20000.5f, 0.5f, 0.5f,
588 20000, 20000, 0, 0 },
591 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
592 RectF r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
593 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
595 Rect floored = ToFlooredRectDeprecated(r1);
596 EXPECT_FLOAT_EQ(r2.x(), floored.x());
597 EXPECT_FLOAT_EQ(r2.y(), floored.y());
598 EXPECT_FLOAT_EQ(r2.width(), floored.width());
599 EXPECT_FLOAT_EQ(r2.height(), floored.height());
603 #if defined(OS_WIN)
604 TEST(RectTest, ConstructAndAssign) {
605 const RECT rect_1 = { 0, 0, 10, 10 };
606 const RECT rect_2 = { 0, 0, -10, -10 };
607 Rect test1(rect_1);
608 Rect test2(rect_2);
610 #endif
612 TEST(RectTest, ToRectF) {
613 // Check that implicit conversion from integer to float compiles.
614 Rect a(10, 20, 30, 40);
615 RectF b(10, 20, 30, 40);
617 RectF intersect = IntersectRects(a, b);
618 EXPECT_EQ(b.ToString(), intersect.ToString());
620 EXPECT_EQ(a, b);
621 EXPECT_EQ(b, a);
624 TEST(RectTest, BoundingRect) {
625 struct {
626 Point a;
627 Point b;
628 Rect expected;
629 } int_tests[] = {
630 // If point B dominates A, then A should be the origin.
631 { Point(4, 6), Point(4, 6), Rect(4, 6, 0, 0) },
632 { Point(4, 6), Point(8, 6), Rect(4, 6, 4, 0) },
633 { Point(4, 6), Point(4, 9), Rect(4, 6, 0, 3) },
634 { Point(4, 6), Point(8, 9), Rect(4, 6, 4, 3) },
635 // If point A dominates B, then B should be the origin.
636 { Point(4, 6), Point(4, 6), Rect(4, 6, 0, 0) },
637 { Point(8, 6), Point(4, 6), Rect(4, 6, 4, 0) },
638 { Point(4, 9), Point(4, 6), Rect(4, 6, 0, 3) },
639 { Point(8, 9), Point(4, 6), Rect(4, 6, 4, 3) },
640 // If neither point dominates, then the origin is a combination of the two.
641 { Point(4, 6), Point(6, 4), Rect(4, 4, 2, 2) },
642 { Point(-4, -6), Point(-6, -4), Rect(-6, -6, 2, 2) },
643 { Point(-4, 6), Point(6, -4), Rect(-4, -4, 10, 10) },
646 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(int_tests); ++i) {
647 Rect actual = BoundingRect(int_tests[i].a, int_tests[i].b);
648 EXPECT_EQ(int_tests[i].expected.ToString(), actual.ToString());
651 struct {
652 PointF a;
653 PointF b;
654 RectF expected;
655 } float_tests[] = {
656 // If point B dominates A, then A should be the origin.
657 { PointF(4.2f, 6.8f), PointF(4.2f, 6.8f),
658 RectF(4.2f, 6.8f, 0, 0) },
659 { PointF(4.2f, 6.8f), PointF(8.5f, 6.8f),
660 RectF(4.2f, 6.8f, 4.3f, 0) },
661 { PointF(4.2f, 6.8f), PointF(4.2f, 9.3f),
662 RectF(4.2f, 6.8f, 0, 2.5f) },
663 { PointF(4.2f, 6.8f), PointF(8.5f, 9.3f),
664 RectF(4.2f, 6.8f, 4.3f, 2.5f) },
665 // If point A dominates B, then B should be the origin.
666 { PointF(4.2f, 6.8f), PointF(4.2f, 6.8f),
667 RectF(4.2f, 6.8f, 0, 0) },
668 { PointF(8.5f, 6.8f), PointF(4.2f, 6.8f),
669 RectF(4.2f, 6.8f, 4.3f, 0) },
670 { PointF(4.2f, 9.3f), PointF(4.2f, 6.8f),
671 RectF(4.2f, 6.8f, 0, 2.5f) },
672 { PointF(8.5f, 9.3f), PointF(4.2f, 6.8f),
673 RectF(4.2f, 6.8f, 4.3f, 2.5f) },
674 // If neither point dominates, then the origin is a combination of the two.
675 { PointF(4.2f, 6.8f), PointF(6.8f, 4.2f),
676 RectF(4.2f, 4.2f, 2.6f, 2.6f) },
677 { PointF(-4.2f, -6.8f), PointF(-6.8f, -4.2f),
678 RectF(-6.8f, -6.8f, 2.6f, 2.6f) },
679 { PointF(-4.2f, 6.8f), PointF(6.8f, -4.2f),
680 RectF(-4.2f, -4.2f, 11.0f, 11.0f) }
683 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(float_tests); ++i) {
684 RectF actual = BoundingRect(float_tests[i].a, float_tests[i].b);
685 EXPECT_EQ(float_tests[i].expected.ToString(), actual.ToString());
689 TEST(RectTest, IsExpressibleAsRect) {
690 EXPECT_TRUE(RectF().IsExpressibleAsRect());
692 float min = std::numeric_limits<int>::min();
693 float max = std::numeric_limits<int>::max();
694 float infinity = std::numeric_limits<float>::infinity();
696 EXPECT_TRUE(RectF(
697 min + 200, min + 200, max - 200, max - 200).IsExpressibleAsRect());
698 EXPECT_FALSE(RectF(
699 min - 200, min + 200, max + 200, max + 200).IsExpressibleAsRect());
700 EXPECT_FALSE(RectF(
701 min + 200 , min - 200, max + 200, max + 200).IsExpressibleAsRect());
702 EXPECT_FALSE(RectF(
703 min + 200, min + 200, max + 200, max - 200).IsExpressibleAsRect());
704 EXPECT_FALSE(RectF(
705 min + 200, min + 200, max - 200, max + 200).IsExpressibleAsRect());
707 EXPECT_TRUE(RectF(0, 0, max - 200, max - 200).IsExpressibleAsRect());
708 EXPECT_FALSE(RectF(200, 0, max + 200, max - 200).IsExpressibleAsRect());
709 EXPECT_FALSE(RectF(0, 200, max - 200, max + 200).IsExpressibleAsRect());
710 EXPECT_FALSE(RectF(0, 0, max + 200, max - 200).IsExpressibleAsRect());
711 EXPECT_FALSE(RectF(0, 0, max - 200, max + 200).IsExpressibleAsRect());
713 EXPECT_FALSE(RectF(infinity, 0, 1, 1).IsExpressibleAsRect());
714 EXPECT_FALSE(RectF(0, infinity, 1, 1).IsExpressibleAsRect());
715 EXPECT_FALSE(RectF(0, 0, infinity, 1).IsExpressibleAsRect());
716 EXPECT_FALSE(RectF(0, 0, 1, infinity).IsExpressibleAsRect());
719 TEST(RectTest, Offset) {
720 Rect i(1, 2, 3, 4);
722 EXPECT_EQ(Rect(2, 1, 3, 4).ToString(), (i + Vector2d(1, -1)).ToString());
723 EXPECT_EQ(Rect(2, 1, 3, 4).ToString(), (Vector2d(1, -1) + i).ToString());
724 i += Vector2d(1, -1);
725 EXPECT_EQ(Rect(2, 1, 3, 4).ToString(), i.ToString());
726 EXPECT_EQ(Rect(1, 2, 3, 4).ToString(), (i - Vector2d(1, -1)).ToString());
727 i -= Vector2d(1, -1);
728 EXPECT_EQ(Rect(1, 2, 3, 4).ToString(), i.ToString());
730 RectF f(1.1f, 2.2f, 3.3f, 4.4f);
731 EXPECT_EQ(RectF(2.2f, 1.1f, 3.3f, 4.4f).ToString(),
732 (f + Vector2dF(1.1f, -1.1f)).ToString());
733 EXPECT_EQ(RectF(2.2f, 1.1f, 3.3f, 4.4f).ToString(),
734 (Vector2dF(1.1f, -1.1f) + f).ToString());
735 f += Vector2dF(1.1f, -1.1f);
736 EXPECT_EQ(RectF(2.2f, 1.1f, 3.3f, 4.4f).ToString(), f.ToString());
737 EXPECT_EQ(RectF(1.1f, 2.2f, 3.3f, 4.4f).ToString(),
738 (f - Vector2dF(1.1f, -1.1f)).ToString());
739 f -= Vector2dF(1.1f, -1.1f);
740 EXPECT_EQ(RectF(1.1f, 2.2f, 3.3f, 4.4f).ToString(), f.ToString());
743 TEST(RectTest, Corners) {
744 Rect i(1, 2, 3, 4);
745 RectF f(1.1f, 2.1f, 3.1f, 4.1f);
747 EXPECT_EQ(Point(1, 2).ToString(), i.origin().ToString());
748 EXPECT_EQ(Point(4, 2).ToString(), i.top_right().ToString());
749 EXPECT_EQ(Point(1, 6).ToString(), i.bottom_left().ToString());
750 EXPECT_EQ(Point(4, 6).ToString(), i.bottom_right().ToString());
752 EXPECT_EQ(PointF(1.1f, 2.1f).ToString(), f.origin().ToString());
753 EXPECT_EQ(PointF(4.2f, 2.1f).ToString(), f.top_right().ToString());
754 EXPECT_EQ(PointF(1.1f, 6.2f).ToString(), f.bottom_left().ToString());
755 EXPECT_EQ(PointF(4.2f, 6.2f).ToString(), f.bottom_right().ToString());
758 } // namespace gfx