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/skia_util.h" | 9 #include "ui/gfx/skia_util.h" |
9 | 10 |
| 11 #include <limits> |
| 12 |
10 namespace ui { | 13 namespace ui { |
11 | 14 |
12 TEST(RectTest, Contains) { | 15 TEST(RectTest, Contains) { |
13 static const struct ContainsCase { | 16 static const struct ContainsCase { |
14 int rect_x; | 17 int rect_x; |
15 int rect_y; | 18 int rect_y; |
16 int rect_width; | 19 int rect_width; |
17 int rect_height; | 20 int rect_height; |
18 int point_x; | 21 int point_x; |
19 int point_y; | 22 int point_y; |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 EXPECT_FALSE(r.SharesEdgeWith(just_left_no_edge)); | 340 EXPECT_FALSE(r.SharesEdgeWith(just_left_no_edge)); |
338 EXPECT_FALSE(r.SharesEdgeWith(just_right_no_edge)); | 341 EXPECT_FALSE(r.SharesEdgeWith(just_right_no_edge)); |
339 } | 342 } |
340 | 343 |
341 TEST(RectTest, SkRectToRect) { | 344 TEST(RectTest, SkRectToRect) { |
342 gfx::Rect src(10, 20, 30, 40); | 345 gfx::Rect src(10, 20, 30, 40); |
343 SkRect skrect = gfx::RectToSkRect(src); | 346 SkRect skrect = gfx::RectToSkRect(src); |
344 EXPECT_EQ(src, gfx::SkRectToRect(skrect)); | 347 EXPECT_EQ(src, gfx::SkRectToRect(skrect)); |
345 } | 348 } |
346 | 349 |
| 350 // Similar to EXPECT_FLOAT_EQ, but lets NaN equal NaN |
| 351 #define EXPECT_FLOAT_AND_NAN_EQ(a, b) \ |
| 352 { if (a == a || b == b) { EXPECT_FLOAT_EQ(a, b); } } |
| 353 |
| 354 |
| 355 TEST(RectTest, ScaleRect) { |
| 356 static const struct Test { |
| 357 int x1; // source |
| 358 int y1; |
| 359 int w1; |
| 360 int h1; |
| 361 float scale; |
| 362 float x2; // target |
| 363 float y2; |
| 364 float w2; |
| 365 float h2; |
| 366 } tests[] = { |
| 367 { 3, 3, 3, 3, |
| 368 1.5f, |
| 369 4.5f, 4.5f, 4.5f, 4.5f }, |
| 370 { 3, 3, 3, 3, |
| 371 0.0f, |
| 372 0.0f, 0.0f, 0.0f, 0.0f }, |
| 373 { 3, 3, 3, 3, |
| 374 std::numeric_limits<float>::quiet_NaN(), |
| 375 std::numeric_limits<float>::quiet_NaN(), |
| 376 std::numeric_limits<float>::quiet_NaN(), |
| 377 std::numeric_limits<float>::quiet_NaN(), |
| 378 std::numeric_limits<float>::quiet_NaN() }, |
| 379 { 3, 3, 3, 3, |
| 380 std::numeric_limits<float>::max(), |
| 381 std::numeric_limits<float>::max(), |
| 382 std::numeric_limits<float>::max(), |
| 383 std::numeric_limits<float>::max(), |
| 384 std::numeric_limits<float>::max() }, |
| 385 { 3, 3, 3, 3, |
| 386 -1.0f, |
| 387 -3.0f, -3.0f, 0.0f, 0.0f } |
| 388 }; |
| 389 |
| 390 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 391 gfx::Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1); |
| 392 gfx::RectF r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2); |
| 393 |
| 394 gfx::RectF scaled = r1.Scale(tests[i].scale); |
| 395 EXPECT_FLOAT_AND_NAN_EQ(r2.x(), scaled.x()); |
| 396 EXPECT_FLOAT_AND_NAN_EQ(r2.y(), scaled.y()); |
| 397 EXPECT_FLOAT_AND_NAN_EQ(r2.width(), scaled.width()); |
| 398 EXPECT_FLOAT_AND_NAN_EQ(r2.height(), scaled.height()); |
| 399 } |
| 400 } |
| 401 |
| 402 TEST(RectTest, ToEnclosedRect) { |
| 403 static const struct Test { |
| 404 float x1; // source |
| 405 float y1; |
| 406 float w1; |
| 407 float h1; |
| 408 int x2; // target |
| 409 int y2; |
| 410 int w2; |
| 411 int h2; |
| 412 } tests [] = { |
| 413 { 0.0f, 0.0f, 0.0f, 0.0f, |
| 414 0, 0, 0, 0 }, |
| 415 { -1.5f, -1.5f, 3.0f, 3.0f, |
| 416 -1, -1, 2, 2 }, |
| 417 { -1.5f, -1.5f, 3.5f, 3.5f, |
| 418 -1, -1, 3, 3 }, |
| 419 { std::numeric_limits<float>::max(), |
| 420 std::numeric_limits<float>::max(), |
| 421 2.0f, 2.0f, |
| 422 std::numeric_limits<int>::max(), |
| 423 std::numeric_limits<int>::max(), |
| 424 0, 0 }, |
| 425 { 0.0f, 0.0f, |
| 426 std::numeric_limits<float>::max(), |
| 427 std::numeric_limits<float>::max(), |
| 428 0, 0, |
| 429 std::numeric_limits<int>::max(), |
| 430 std::numeric_limits<int>::max() }, |
| 431 { 20000.5f, 20000.5f, 0.5f, 0.5f, |
| 432 20001, 20001, 0, 0 }, |
| 433 { std::numeric_limits<float>::quiet_NaN(), |
| 434 std::numeric_limits<float>::quiet_NaN(), |
| 435 std::numeric_limits<float>::quiet_NaN(), |
| 436 std::numeric_limits<float>::quiet_NaN(), |
| 437 0, 0, 0, 0 } |
| 438 }; |
| 439 |
| 440 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 441 gfx::RectF r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1); |
| 442 gfx::Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2); |
| 443 |
| 444 gfx::Rect enclosed = ToEnclosedRect(r1); |
| 445 EXPECT_FLOAT_AND_NAN_EQ(r2.x(), enclosed.x()); |
| 446 EXPECT_FLOAT_AND_NAN_EQ(r2.y(), enclosed.y()); |
| 447 EXPECT_FLOAT_AND_NAN_EQ(r2.width(), enclosed.width()); |
| 448 EXPECT_FLOAT_AND_NAN_EQ(r2.height(), enclosed.height()); |
| 449 } |
| 450 } |
| 451 |
| 452 TEST(RectTest, ToEnclosingRect) { |
| 453 static const struct Test { |
| 454 float x1; // source |
| 455 float y1; |
| 456 float w1; |
| 457 float h1; |
| 458 int x2; // target |
| 459 int y2; |
| 460 int w2; |
| 461 int h2; |
| 462 } tests [] = { |
| 463 { 0.0f, 0.0f, 0.0f, 0.0f, |
| 464 0, 0, 0, 0 }, |
| 465 { -1.5f, -1.5f, 3.0f, 3.0f, |
| 466 -2, -2, 4, 4 }, |
| 467 { -1.5f, -1.5f, 3.5f, 3.5f, |
| 468 -2, -2, 4, 4 }, |
| 469 { std::numeric_limits<float>::max(), |
| 470 std::numeric_limits<float>::max(), |
| 471 2.0f, 2.0f, |
| 472 std::numeric_limits<int>::max(), |
| 473 std::numeric_limits<int>::max(), |
| 474 0, 0 }, |
| 475 { 0.0f, 0.0f, |
| 476 std::numeric_limits<float>::max(), |
| 477 std::numeric_limits<float>::max(), |
| 478 0, 0, |
| 479 std::numeric_limits<int>::max(), |
| 480 std::numeric_limits<int>::max() }, |
| 481 { 20000.5f, 20000.5f, 0.5f, 0.5f, |
| 482 20000, 20000, 1, 1 }, |
| 483 { std::numeric_limits<float>::quiet_NaN(), |
| 484 std::numeric_limits<float>::quiet_NaN(), |
| 485 std::numeric_limits<float>::quiet_NaN(), |
| 486 std::numeric_limits<float>::quiet_NaN(), |
| 487 0, 0, 0, 0 } |
| 488 }; |
| 489 |
| 490 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 491 gfx::RectF r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1); |
| 492 gfx::Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2); |
| 493 |
| 494 gfx::Rect enclosed = ToEnclosingRect(r1); |
| 495 EXPECT_FLOAT_AND_NAN_EQ(r2.x(), enclosed.x()); |
| 496 EXPECT_FLOAT_AND_NAN_EQ(r2.y(), enclosed.y()); |
| 497 EXPECT_FLOAT_AND_NAN_EQ(r2.width(), enclosed.width()); |
| 498 EXPECT_FLOAT_AND_NAN_EQ(r2.height(), enclosed.height()); |
| 499 } |
| 500 } |
| 501 |
347 #if defined(OS_WIN) | 502 #if defined(OS_WIN) |
348 TEST(RectTest, ConstructAndAssign) { | 503 TEST(RectTest, ConstructAndAssign) { |
349 const RECT rect_1 = { 0, 0, 10, 10 }; | 504 const RECT rect_1 = { 0, 0, 10, 10 }; |
350 const RECT rect_2 = { 0, 0, -10, -10 }; | 505 const RECT rect_2 = { 0, 0, -10, -10 }; |
351 gfx::Rect test1(rect_1); | 506 gfx::Rect test1(rect_1); |
352 gfx::Rect test2(rect_2); | 507 gfx::Rect test2(rect_2); |
353 } | 508 } |
354 #endif | 509 #endif |
355 | 510 |
356 } // namespace ui | 511 } // namespace ui |
OLD | NEW |