Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "ui/views/bubble/bubble_border.h" | 5 #include "ui/views/bubble/bubble_border.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 302 SCOPED_TRACE(base::StringPrintf("i=%d arrow=%d", | 302 SCOPED_TRACE(base::StringPrintf("i=%d arrow=%d", |
| 303 static_cast<int>(i), cases[i].arrow)); | 303 static_cast<int>(i), cases[i].arrow)); |
| 304 | 304 |
| 305 border.set_arrow(cases[i].arrow); | 305 border.set_arrow(cases[i].arrow); |
| 306 | 306 |
| 307 border.set_paint_arrow(BubbleBorder::PAINT_NORMAL); | 307 border.set_paint_arrow(BubbleBorder::PAINT_NORMAL); |
| 308 EXPECT_EQ(cases[i].expected_with_arrow, | 308 EXPECT_EQ(cases[i].expected_with_arrow, |
| 309 border.GetSizeForContentsSize(cases[i].content)); | 309 border.GetSizeForContentsSize(cases[i].content)); |
| 310 | 310 |
| 311 border.set_paint_arrow(BubbleBorder::PAINT_TRANSPARENT); | 311 border.set_paint_arrow(BubbleBorder::PAINT_TRANSPARENT); |
| 312 EXPECT_EQ(cases[i].expected_with_arrow, | 312 EXPECT_EQ(cases[i].expected_without_arrow, |
| 313 border.GetSizeForContentsSize(cases[i].content)); | 313 border.GetSizeForContentsSize(cases[i].content)); |
| 314 | 314 |
| 315 border.set_paint_arrow(BubbleBorder::PAINT_NONE); | 315 border.set_paint_arrow(BubbleBorder::PAINT_NONE); |
| 316 EXPECT_EQ(cases[i].expected_without_arrow, | 316 EXPECT_EQ(cases[i].expected_without_arrow, |
| 317 border.GetSizeForContentsSize(cases[i].content)); | 317 border.GetSizeForContentsSize(cases[i].content)); |
| 318 } | 318 } |
| 319 } | 319 } |
| 320 | 320 |
| 321 TEST_F(BubbleBorderTest, GetBoundsOriginTest) { | 321 TEST_F(BubbleBorderTest, GetBoundsOriginTest) { |
| 322 views::BubbleBorder border(BubbleBorder::TOP_LEFT, | 322 views::BubbleBorder border(BubbleBorder::TOP_LEFT, |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 340 const gfx::Size kTotalSizeWithNoArrow = | 340 const gfx::Size kTotalSizeWithNoArrow = |
| 341 border.GetSizeForContentsSize(kContentSize); | 341 border.GetSizeForContentsSize(kContentSize); |
| 342 | 342 |
| 343 const int kBorderThickness = border.GetBorderThickness(); | 343 const int kBorderThickness = border.GetBorderThickness(); |
| 344 | 344 |
| 345 const int kArrowOffsetForHorizCenter = kTotalSizeWithHorizArrow.width() / 2; | 345 const int kArrowOffsetForHorizCenter = kTotalSizeWithHorizArrow.width() / 2; |
| 346 const int kArrowOffsetForVertCenter = kTotalSizeWithVertArrow.height() / 2; | 346 const int kArrowOffsetForVertCenter = kTotalSizeWithVertArrow.height() / 2; |
| 347 const int kArrowOffsetForNotCenter = | 347 const int kArrowOffsetForNotCenter = |
| 348 kImages->border_thickness + (kImages->arrow_width / 2); | 348 kImages->border_thickness + (kImages->arrow_width / 2); |
| 349 | 349 |
| 350 const int kArrowSize = | 350 const int kArrowShift = kImages->arrow_interior_thickness + |
|
Peter Kasting
2016/03/17 06:16:15
We have a lot of code below this point that explic
varkha
2016/03/18 01:21:13
Yes, this is indeed shorter and made it easier to
| |
| 351 kImages->arrow_interior_thickness + BubbleBorder::kStroke - | 351 BubbleBorder::kStroke - kImages->arrow_thickness; |
| 352 kImages->arrow_thickness; | 352 const int kNoArrowShift = kArrowShift + kImages->arrow_interior_thickness; |
| 353 | 353 |
| 354 const int kTopHorizArrowY = kAnchor.y() + kAnchor.height() + kArrowSize; | 354 const int kTopHorizArrowY = kAnchor.y() + kAnchor.height() + kArrowShift; |
| 355 const int kBottomHorizArrowY = | 355 const int kBottomHorizArrowY = |
| 356 kAnchor.y() - kArrowSize - kTotalSizeWithHorizArrow.height(); | 356 kAnchor.y() - kArrowShift - kTotalSizeWithHorizArrow.height(); |
| 357 const int kLeftVertArrowX = kAnchor.x() + kAnchor.width() + kArrowShift; | |
| 358 const int kRightVertArrowX = | |
| 359 kAnchor.x() - kArrowShift - kTotalSizeWithVertArrow.width(); | |
| 357 | 360 |
| 358 const int kLeftVertArrowX = kAnchor.x() + kAnchor.width() + kArrowSize; | 361 const int kTopHorizTransparentArrowY = |
| 359 const int kRightVertArrowX = | 362 kAnchor.y() + kAnchor.height() + kNoArrowShift; |
| 360 kAnchor.x() - kArrowSize - kTotalSizeWithVertArrow.width(); | 363 const int kBottomHorizTransparentArrowY = |
| 364 kAnchor.y() - kNoArrowShift - kTotalSizeWithNoArrow.height(); | |
| 365 const int kLeftVertTransparentArrowX = | |
| 366 kAnchor.x() + kAnchor.width() + kNoArrowShift; | |
| 367 const int kRightVertTransparentArrowX = | |
| 368 kAnchor.x() - kNoArrowShift - kTotalSizeWithNoArrow.width(); | |
| 369 | |
| 370 const int kTopHorizNoArrowY = kAnchor.y() + kAnchor.height() + kArrowShift; | |
| 371 const int kBottomHorizNoArrowY = | |
| 372 kAnchor.y() - kArrowShift - kTotalSizeWithNoArrow.height(); | |
| 373 const int kLeftVertNoArrowX = kAnchor.x() + kAnchor.width() + kArrowShift; | |
| 374 const int kRightVertNoArrowX = | |
| 375 kAnchor.x() - kArrowShift - kTotalSizeWithNoArrow.width(); | |
| 361 | 376 |
| 362 struct TestCase { | 377 struct TestCase { |
| 363 BubbleBorder::Arrow arrow; | 378 BubbleBorder::Arrow arrow; |
| 364 BubbleBorder::BubbleAlignment alignment; | 379 BubbleBorder::BubbleAlignment alignment; |
| 365 int expected_x; | 380 int expected_x; |
| 366 int expected_y; | 381 int expected_y; |
| 382 int expected_with_transparent_arrow_x; | |
| 383 int expected_with_transparent_arrow_y; | |
| 384 int expected_without_arrow_x; | |
| 385 int expected_without_arrow_y; | |
| 367 }; | 386 }; |
| 368 | 387 |
| 369 TestCase cases[] = { | 388 TestCase cases[] = { |
| 370 // Horizontal arrow tests. | 389 // Horizontal arrow tests. |
| 371 { BubbleBorder::TOP_LEFT, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, | 390 {BubbleBorder::TOP_LEFT, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, |
| 372 kAnchor.CenterPoint().x() - kArrowOffsetForNotCenter, kTopHorizArrowY }, | 391 kAnchor.CenterPoint().x() - kArrowOffsetForNotCenter, kTopHorizArrowY, |
| 373 { BubbleBorder::TOP_LEFT, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE, | 392 kAnchor.CenterPoint().x() - kArrowOffsetForNotCenter, |
| 374 kAnchor.x() + BubbleBorder::kStroke - kBorderThickness, kTopHorizArrowY }, | 393 kTopHorizTransparentArrowY, |
| 375 { BubbleBorder::TOP_CENTER, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, | 394 kAnchor.CenterPoint().x() - kArrowOffsetForNotCenter, kTopHorizNoArrowY}, |
| 376 kAnchor.CenterPoint().x() - kArrowOffsetForHorizCenter, kTopHorizArrowY }, | 395 {BubbleBorder::TOP_LEFT, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE, |
| 377 { BubbleBorder::BOTTOM_RIGHT, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, | 396 kAnchor.x() + BubbleBorder::kStroke - kBorderThickness, kTopHorizArrowY, |
| 378 kAnchor.CenterPoint().x() + kArrowOffsetForNotCenter - | 397 kAnchor.x() + BubbleBorder::kStroke - kBorderThickness, |
| 379 kTotalSizeWithHorizArrow.width(), kBottomHorizArrowY }, | 398 kTopHorizTransparentArrowY, |
| 380 { BubbleBorder::BOTTOM_RIGHT, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE, | 399 kAnchor.x() + BubbleBorder::kStroke - kBorderThickness, |
| 381 kAnchor.x() + kAnchor.width() - kTotalSizeWithHorizArrow.width() + | 400 kTopHorizNoArrowY}, |
| 382 kBorderThickness - BubbleBorder::kStroke, kBottomHorizArrowY }, | 401 {BubbleBorder::TOP_CENTER, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, |
| 402 kAnchor.CenterPoint().x() - kArrowOffsetForHorizCenter, kTopHorizArrowY, | |
| 403 kAnchor.CenterPoint().x() - kArrowOffsetForHorizCenter, | |
| 404 kTopHorizTransparentArrowY, | |
| 405 kAnchor.CenterPoint().x() - kArrowOffsetForHorizCenter, | |
| 406 kTopHorizNoArrowY}, | |
| 407 {BubbleBorder::BOTTOM_RIGHT, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, | |
| 408 kAnchor.CenterPoint().x() + kArrowOffsetForNotCenter - | |
| 409 kTotalSizeWithHorizArrow.width(), | |
| 410 kBottomHorizArrowY, | |
| 411 kAnchor.CenterPoint().x() + kArrowOffsetForNotCenter - | |
| 412 kTotalSizeWithHorizArrow.width(), | |
| 413 kBottomHorizTransparentArrowY, | |
| 414 kAnchor.CenterPoint().x() + kArrowOffsetForNotCenter - | |
| 415 kTotalSizeWithHorizArrow.width(), | |
| 416 kBottomHorizNoArrowY}, | |
| 417 {BubbleBorder::BOTTOM_RIGHT, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE, | |
| 418 kAnchor.x() + kAnchor.width() - kTotalSizeWithHorizArrow.width() + | |
| 419 kBorderThickness - BubbleBorder::kStroke, | |
| 420 kBottomHorizArrowY, | |
| 421 kAnchor.x() + kAnchor.width() - kTotalSizeWithHorizArrow.width() + | |
| 422 kBorderThickness - BubbleBorder::kStroke, | |
| 423 kBottomHorizTransparentArrowY, | |
| 424 kAnchor.x() + kAnchor.width() - kTotalSizeWithHorizArrow.width() + | |
| 425 kBorderThickness - BubbleBorder::kStroke, | |
| 426 kBottomHorizNoArrowY}, | |
| 383 | 427 |
| 384 // Vertical arrow tests. | 428 // Vertical arrow tests. |
| 385 { BubbleBorder::LEFT_TOP, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, | 429 {BubbleBorder::LEFT_TOP, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, |
| 386 kLeftVertArrowX, kAnchor.CenterPoint().y() - kArrowOffsetForNotCenter }, | 430 kLeftVertArrowX, kAnchor.CenterPoint().y() - kArrowOffsetForNotCenter, |
| 387 { BubbleBorder::LEFT_TOP, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE, | 431 kLeftVertTransparentArrowX, |
| 388 kLeftVertArrowX, kAnchor.y() + BubbleBorder::kStroke - kBorderThickness }, | 432 kAnchor.CenterPoint().y() - kArrowOffsetForNotCenter, kLeftVertNoArrowX, |
| 389 { BubbleBorder::LEFT_CENTER, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, | 433 kAnchor.CenterPoint().y() - kArrowOffsetForNotCenter}, |
| 390 kLeftVertArrowX, kAnchor.CenterPoint().y() - kArrowOffsetForVertCenter }, | 434 {BubbleBorder::LEFT_TOP, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE, |
| 391 { BubbleBorder::RIGHT_BOTTOM, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, | 435 kLeftVertArrowX, kAnchor.y() + BubbleBorder::kStroke - kBorderThickness, |
| 392 kRightVertArrowX, kAnchor.CenterPoint().y() + kArrowOffsetForNotCenter - | 436 kLeftVertTransparentArrowX, |
| 393 kTotalSizeWithVertArrow.height() }, | 437 kAnchor.y() + BubbleBorder::kStroke - kBorderThickness, |
| 394 { BubbleBorder::RIGHT_BOTTOM, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE, | 438 kLeftVertNoArrowX, |
| 395 kRightVertArrowX, kAnchor.y() + kAnchor.height() - | 439 kAnchor.y() + BubbleBorder::kStroke - kBorderThickness}, |
| 396 kTotalSizeWithVertArrow.height() + kBorderThickness - | 440 {BubbleBorder::LEFT_CENTER, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, |
| 397 BubbleBorder::kStroke }, | 441 kLeftVertArrowX, kAnchor.CenterPoint().y() - kArrowOffsetForVertCenter, |
| 442 kLeftVertTransparentArrowX, | |
| 443 kAnchor.CenterPoint().y() - kArrowOffsetForVertCenter, kLeftVertNoArrowX, | |
| 444 kAnchor.CenterPoint().y() - kArrowOffsetForVertCenter}, | |
| 445 {BubbleBorder::RIGHT_BOTTOM, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, | |
| 446 kRightVertArrowX, kAnchor.CenterPoint().y() + kArrowOffsetForNotCenter - | |
| 447 kTotalSizeWithVertArrow.height(), | |
| 448 kRightVertTransparentArrowX, | |
| 449 kAnchor.CenterPoint().y() + kArrowOffsetForNotCenter - | |
| 450 kTotalSizeWithVertArrow.height(), | |
| 451 kRightVertNoArrowX, | |
| 452 kAnchor.CenterPoint().y() + kArrowOffsetForNotCenter - | |
| 453 kTotalSizeWithVertArrow.height()}, | |
| 454 {BubbleBorder::RIGHT_BOTTOM, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE, | |
| 455 kRightVertArrowX, | |
| 456 kAnchor.y() + kAnchor.height() - kTotalSizeWithVertArrow.height() + | |
| 457 kBorderThickness - BubbleBorder::kStroke, | |
| 458 kRightVertTransparentArrowX, | |
| 459 kAnchor.y() + kAnchor.height() - kTotalSizeWithVertArrow.height() + | |
| 460 kBorderThickness - BubbleBorder::kStroke, | |
| 461 kRightVertNoArrowX, | |
| 462 kAnchor.y() + kAnchor.height() - kTotalSizeWithVertArrow.height() + | |
| 463 kBorderThickness - BubbleBorder::kStroke}, | |
| 398 | 464 |
| 399 // No arrow tests. | 465 // No arrow tests. |
| 400 { BubbleBorder::NONE, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, | 466 {BubbleBorder::NONE, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, |
| 401 kAnchor.x() + (kAnchor.width() - kTotalSizeWithNoArrow.width()) / 2, | 467 kAnchor.x() + (kAnchor.width() - kTotalSizeWithNoArrow.width()) / 2, |
| 402 kAnchor.y() + kAnchor.height() }, | 468 kAnchor.y() + kAnchor.height(), |
| 403 { BubbleBorder::FLOAT, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, | 469 kAnchor.x() + (kAnchor.width() - kTotalSizeWithNoArrow.width()) / 2, |
| 404 kAnchor.x() + (kAnchor.width() - kTotalSizeWithNoArrow.width()) / 2, | 470 kAnchor.y() + kAnchor.height(), |
| 405 kAnchor.y() + (kAnchor.height() - kTotalSizeWithNoArrow.height()) / 2 }, | 471 kAnchor.x() + (kAnchor.width() - kTotalSizeWithNoArrow.width()) / 2, |
| 472 kAnchor.y() + kAnchor.height()}, | |
| 473 {BubbleBorder::FLOAT, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, | |
| 474 kAnchor.x() + (kAnchor.width() - kTotalSizeWithNoArrow.width()) / 2, | |
| 475 kAnchor.y() + (kAnchor.height() - kTotalSizeWithNoArrow.height()) / 2, | |
| 476 kAnchor.x() + (kAnchor.width() - kTotalSizeWithNoArrow.width()) / 2, | |
| 477 kAnchor.y() + (kAnchor.height() - kTotalSizeWithNoArrow.height()) / 2, | |
| 478 kAnchor.x() + (kAnchor.width() - kTotalSizeWithNoArrow.width()) / 2, | |
| 479 kAnchor.y() + (kAnchor.height() - kTotalSizeWithNoArrow.height()) / 2}, | |
| 406 }; | 480 }; |
| 407 | 481 |
| 408 for (size_t i = 0; i < arraysize(cases); ++i) { | 482 for (size_t i = 0; i < arraysize(cases); ++i) { |
| 409 SCOPED_TRACE(base::StringPrintf("i=%d arrow=%d alignment=%d", | 483 SCOPED_TRACE(base::StringPrintf("i=%d arrow=%d alignment=%d", |
| 410 static_cast<int>(i), cases[i].arrow, cases[i].alignment)); | 484 static_cast<int>(i), cases[i].arrow, cases[i].alignment)); |
| 411 border.set_arrow(cases[i].arrow); | 485 border.set_arrow(cases[i].arrow); |
| 412 border.set_alignment(cases[i].alignment); | 486 border.set_alignment(cases[i].alignment); |
| 413 | 487 |
| 488 border.set_paint_arrow(BubbleBorder::PAINT_NORMAL); | |
| 414 gfx::Point origin = border.GetBounds(kAnchor, kContentSize).origin(); | 489 gfx::Point origin = border.GetBounds(kAnchor, kContentSize).origin(); |
| 415 EXPECT_EQ(cases[i].expected_x, origin.x()); | 490 EXPECT_EQ(cases[i].expected_x, origin.x()); |
| 416 EXPECT_EQ(cases[i].expected_y, origin.y()); | 491 EXPECT_EQ(cases[i].expected_y, origin.y()); |
| 492 | |
| 493 border.set_paint_arrow(BubbleBorder::PAINT_TRANSPARENT); | |
| 494 origin = border.GetBounds(kAnchor, kContentSize).origin(); | |
| 495 EXPECT_EQ(cases[i].expected_with_transparent_arrow_x, origin.x()); | |
| 496 EXPECT_EQ(cases[i].expected_with_transparent_arrow_y, origin.y()); | |
| 497 | |
| 498 border.set_paint_arrow(BubbleBorder::PAINT_NONE); | |
| 499 origin = border.GetBounds(kAnchor, kContentSize).origin(); | |
| 500 EXPECT_EQ(cases[i].expected_without_arrow_x, origin.x()); | |
| 501 EXPECT_EQ(cases[i].expected_without_arrow_y, origin.y()); | |
| 417 } | 502 } |
| 418 } | 503 } |
| 419 | 504 |
| 420 // Ensure all the shadow types pass some size validation and paint sanely. | 505 // Ensure all the shadow types pass some size validation and paint sanely. |
| 421 TEST_F(BubbleBorderTest, ShadowTypes) { | 506 TEST_F(BubbleBorderTest, ShadowTypes) { |
| 422 const gfx::Rect rect(0, 0, 320, 200); | 507 const gfx::Rect rect(0, 0, 320, 200); |
| 423 View paint_view; | 508 View paint_view; |
| 424 paint_view.SetBoundsRect(rect); | 509 paint_view.SetBoundsRect(rect); |
| 425 | 510 |
| 426 for (int i = 0; i < BubbleBorder::SHADOW_COUNT; ++i) { | 511 for (int i = 0; i < BubbleBorder::SHADOW_COUNT; ++i) { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 440 EXPECT_GE(border_images->border_thickness, | 525 EXPECT_GE(border_images->border_thickness, |
| 441 border_images->border_interior_thickness); | 526 border_images->border_interior_thickness); |
| 442 | 527 |
| 443 // For a TOP_LEFT arrow, the x-offset always matches the border thickness. | 528 // For a TOP_LEFT arrow, the x-offset always matches the border thickness. |
| 444 EXPECT_EQ(border.GetArrowRect(rect).x(), border_images->border_thickness); | 529 EXPECT_EQ(border.GetArrowRect(rect).x(), border_images->border_thickness); |
| 445 border.Paint(paint_view, &canvas); | 530 border.Paint(paint_view, &canvas); |
| 446 } | 531 } |
| 447 } | 532 } |
| 448 | 533 |
| 449 } // namespace views | 534 } // namespace views |
| OLD | NEW |