| 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_frame_view.h" | 5 #include "ui/views/bubble/bubble_frame_view.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "ui/gfx/geometry/insets.h" | 11 #include "ui/gfx/geometry/insets.h" |
| 12 #include "ui/gfx/geometry/rect.h" | 12 #include "ui/gfx/geometry/rect.h" |
| 13 #include "ui/gfx/geometry/size.h" | 13 #include "ui/gfx/geometry/size.h" |
| 14 #include "ui/views/bubble/bubble_border.h" | 14 #include "ui/views/bubble/bubble_border.h" |
| 15 #include "ui/views/bubble/bubble_dialog_delegate.h" |
| 15 #include "ui/views/controls/button/label_button.h" | 16 #include "ui/views/controls/button/label_button.h" |
| 16 #include "ui/views/test/test_views.h" | 17 #include "ui/views/test/test_views.h" |
| 17 #include "ui/views/test/views_test_base.h" | 18 #include "ui/views/test/views_test_base.h" |
| 18 #include "ui/views/widget/widget.h" | 19 #include "ui/views/widget/widget.h" |
| 19 #include "ui/views/widget/widget_delegate.h" | 20 #include "ui/views/widget/widget_delegate.h" |
| 20 | 21 |
| 21 namespace views { | 22 namespace views { |
| 22 | 23 |
| 23 typedef ViewsTestBase BubbleFrameViewTest; | 24 typedef ViewsTestBase BubbleFrameViewTest; |
| 24 | 25 |
| (...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 481 #else | 482 #else |
| 482 maximum_rect.Inset(frame.bubble_border()->GetInsets()); | 483 maximum_rect.Inset(frame.bubble_border()->GetInsets()); |
| 483 | 484 |
| 484 // Should ignore the contents view's maximum size and use the preferred size. | 485 // Should ignore the contents view's maximum size and use the preferred size. |
| 485 gfx::Size expected_size(kPreferredClientWidth + kExpectedAdditionalWidth, | 486 gfx::Size expected_size(kPreferredClientWidth + kExpectedAdditionalWidth, |
| 486 kPreferredClientHeight + kExpectedAdditionalHeight); | 487 kPreferredClientHeight + kExpectedAdditionalHeight); |
| 487 EXPECT_EQ(expected_size, maximum_rect.size()); | 488 EXPECT_EQ(expected_size, maximum_rect.size()); |
| 488 #endif | 489 #endif |
| 489 } | 490 } |
| 490 | 491 |
| 492 namespace { |
| 493 |
| 494 class TestBubbleDialogDelegateView : public BubbleDialogDelegateView { |
| 495 public: |
| 496 TestBubbleDialogDelegateView() |
| 497 : BubbleDialogDelegateView(nullptr, BubbleBorder::NONE) { |
| 498 set_shadow(BubbleBorder::NO_ASSETS); |
| 499 SetAnchorRect(gfx::Rect()); |
| 500 } |
| 501 ~TestBubbleDialogDelegateView() override {} |
| 502 |
| 503 using BubbleDialogDelegateView::SetAnchorView; |
| 504 |
| 505 // BubbleDialogDelegateView: |
| 506 void DeleteDelegate() override { |
| 507 // This delegate is owned by the test case itself, so it should not delete |
| 508 // itself here. |
| 509 } |
| 510 |
| 511 int GetDialogButtons() const override { return ui::DIALOG_BUTTON_NONE; } |
| 512 |
| 513 gfx::Size GetPreferredSize() const override { return gfx::Size(200, 200); } |
| 514 |
| 515 private: |
| 516 DISALLOW_COPY_AND_ASSIGN(TestBubbleDialogDelegateView); |
| 517 }; |
| 518 |
| 519 class TestLayoutProvider : public LayoutProvider { |
| 520 public: |
| 521 TestLayoutProvider() : LayoutProvider() {} |
| 522 ~TestLayoutProvider() override {} |
| 523 |
| 524 // LayoutProvider: |
| 525 int GetSnappedDialogWidth(int min_width) const override { |
| 526 return snap_to_ ? snap_to_ : min_width; |
| 527 } |
| 528 |
| 529 void set_snap_to(int width) { snap_to_ = width; } |
| 530 |
| 531 private: |
| 532 int snap_to_ = 0; |
| 533 |
| 534 DISALLOW_COPY_AND_ASSIGN(TestLayoutProvider); |
| 535 }; |
| 536 |
| 537 } // namespace |
| 538 |
| 539 // This test ensures that if the installed LayoutProvider snaps dialog widths, |
| 540 // BubbleFrameView correctly sizes itself to that width. |
| 541 TEST_F(BubbleFrameViewTest, WidthSnaps) { |
| 542 TestLayoutProvider provider; |
| 543 TestBubbleDialogDelegateView delegate; |
| 544 |
| 545 Widget anchor; |
| 546 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); |
| 547 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 548 anchor.Init(params); |
| 549 anchor.Show(); |
| 550 |
| 551 delegate.SetAnchorView(anchor.GetContentsView()); |
| 552 delegate.set_margins(gfx::Insets()); |
| 553 |
| 554 Widget* w0 = BubbleDialogDelegateView::CreateBubble(&delegate); |
| 555 w0->Show(); |
| 556 EXPECT_EQ(delegate.GetPreferredSize().width(), |
| 557 w0->GetWindowBoundsInScreen().width()); |
| 558 w0->CloseNow(); |
| 559 |
| 560 constexpr int kTestWidth = 300; |
| 561 provider.set_snap_to(kTestWidth); |
| 562 |
| 563 // The Widget's snapped width should exactly match the width returned by the |
| 564 // LayoutProvider. |
| 565 Widget* w1 = BubbleDialogDelegateView::CreateBubble(&delegate); |
| 566 w1->Show(); |
| 567 EXPECT_EQ(kTestWidth, w1->GetWindowBoundsInScreen().width()); |
| 568 w1->CloseNow(); |
| 569 } |
| 570 |
| 491 } // namespace views | 571 } // namespace views |
| OLD | NEW |