| 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 "base/macros.h" | 5 #include "base/macros.h" |
| 6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 #include "ui/base/ui_base_types.h" | 8 #include "ui/base/ui_base_types.h" |
| 9 #include "ui/views/controls/button/label_button.h" | 9 #include "ui/views/controls/button/label_button.h" |
| 10 #include "ui/views/test/test_views.h" | 10 #include "ui/views/test/test_views.h" |
| 11 #include "ui/views/test/views_test_base.h" | 11 #include "ui/views/test/views_test_base.h" |
| 12 #include "ui/views/widget/widget.h" | 12 #include "ui/views/widget/widget.h" |
| 13 #include "ui/views/window/dialog_client_view.h" | 13 #include "ui/views/window/dialog_client_view.h" |
| 14 #include "ui/views/window/dialog_delegate.h" | 14 #include "ui/views/window/dialog_delegate.h" |
| 15 | 15 |
| 16 namespace views { | 16 namespace views { |
| 17 | 17 |
| 18 class TestDialogClientView : public DialogClientView { | 18 class TestDialogClientView : public DialogClientView { |
| 19 public: | 19 public: |
| 20 TestDialogClientView(View* contents_view, | 20 explicit TestDialogClientView(DialogDelegateView* dialog_delegate_view) |
| 21 DialogDelegate* dialog_delegate) | 21 : DialogClientView(dialog_delegate_view), |
| 22 : DialogClientView(contents_view), | 22 dialog_delegate_view_(dialog_delegate_view) {} |
| 23 dialog_(dialog_delegate) {} | |
| 24 ~TestDialogClientView() override {} | 23 ~TestDialogClientView() override {} |
| 25 | 24 |
| 26 // DialogClientView implementation. | 25 // DialogClientView implementation. |
| 27 DialogDelegate* GetDialogDelegate() const override { return dialog_; } | 26 DialogDelegate* GetDialogDelegate() const override { |
| 27 return dialog_delegate_view_; |
| 28 } |
| 28 | 29 |
| 29 View* GetContentsView() { return contents_view(); } | 30 View* GetContentsView() { return contents_view(); } |
| 30 | 31 |
| 31 void CreateExtraViews() { | 32 void CreateExtraViews() { |
| 32 CreateExtraView(); | 33 CreateExtraView(); |
| 33 } | 34 } |
| 34 | 35 |
| 35 private: | 36 private: |
| 36 DialogDelegate* dialog_; | 37 DialogDelegateView* dialog_delegate_view_; |
| 37 | 38 |
| 38 DISALLOW_COPY_AND_ASSIGN(TestDialogClientView); | 39 DISALLOW_COPY_AND_ASSIGN(TestDialogClientView); |
| 39 }; | 40 }; |
| 40 | 41 |
| 42 // Base class for tests. Also acts as the dialog delegate and contents view for |
| 43 // TestDialogClientView. |
| 41 class DialogClientViewTest : public ViewsTestBase, | 44 class DialogClientViewTest : public ViewsTestBase, |
| 42 public DialogDelegateView { | 45 public DialogDelegateView { |
| 43 public: | 46 public: |
| 44 DialogClientViewTest() | 47 DialogClientViewTest() |
| 45 : dialog_buttons_(ui::DIALOG_BUTTON_NONE), | 48 : dialog_buttons_(ui::DIALOG_BUTTON_NONE), |
| 46 extra_view_(nullptr) {} | 49 extra_view_(nullptr) {} |
| 47 ~DialogClientViewTest() override {} | 50 ~DialogClientViewTest() override {} |
| 48 | 51 |
| 49 // testing::Test implementation. | 52 // testing::Test implementation. |
| 50 void SetUp() override { | 53 void SetUp() override { |
| 51 dialog_buttons_ = ui::DIALOG_BUTTON_NONE; | 54 dialog_buttons_ = ui::DIALOG_BUTTON_NONE; |
| 52 contents_.reset(new StaticSizedView(gfx::Size(100, 200))); | 55 client_view_.reset(new TestDialogClientView(this)); |
| 53 client_view_.reset(new TestDialogClientView(contents_.get(), this)); | 56 // Add this i.e. the contents view as a child of |client_view_|. This is |
| 54 | 57 // generally done when the client view is added to the view hierarchy. |
| 58 client_view_->AddChildViewAt(this, 0); |
| 55 ViewsTestBase::SetUp(); | 59 ViewsTestBase::SetUp(); |
| 56 } | 60 } |
| 57 | 61 |
| 58 // DialogDelegateView implementation. | 62 // DialogDelegateView implementation. |
| 59 View* GetContentsView() override { return contents_.get(); } | |
| 60 View* CreateExtraView() override { return extra_view_; } | 63 View* CreateExtraView() override { return extra_view_; } |
| 61 bool GetExtraViewPadding(int* padding) override { | 64 bool GetExtraViewPadding(int* padding) override { |
| 62 if (extra_view_padding_) | 65 if (extra_view_padding_) |
| 63 *padding = *extra_view_padding_; | 66 *padding = *extra_view_padding_; |
| 64 return extra_view_padding_.get() != nullptr; | 67 return extra_view_padding_.get() != nullptr; |
| 65 } | 68 } |
| 66 int GetDialogButtons() const override { return dialog_buttons_; } | 69 int GetDialogButtons() const override { return dialog_buttons_; } |
| 67 | 70 |
| 68 protected: | 71 protected: |
| 69 gfx::Rect GetUpdatedClientBounds() { | 72 gfx::Rect GetUpdatedClientBounds() { |
| 70 client_view_->SizeToPreferredSize(); | 73 client_view_->SizeToPreferredSize(); |
| 71 client_view_->Layout(); | 74 client_view_->Layout(); |
| 72 return client_view_->bounds(); | 75 return client_view_->bounds(); |
| 73 } | 76 } |
| 74 | 77 |
| 75 // Makes sure that the content view is sized correctly. Width must be at least | 78 // Makes sure that the content view is sized correctly. Width must be at least |
| 76 // the requested amount, but height should always match exactly. | 79 // the requested amount, but height should always match exactly. |
| 77 void CheckContentsIsSetToPreferredSize() { | 80 void CheckContentsIsSetToPreferredSize() { |
| 78 const gfx::Rect client_bounds = GetUpdatedClientBounds(); | 81 const gfx::Rect client_bounds = GetUpdatedClientBounds(); |
| 79 const gfx::Size preferred_size = contents_->GetPreferredSize(); | 82 const gfx::Size preferred_size = this->GetPreferredSize(); |
| 80 EXPECT_EQ(preferred_size.height(), contents_->bounds().height()); | 83 EXPECT_EQ(preferred_size.height(), this->bounds().height()); |
| 81 EXPECT_LE(preferred_size.width(), contents_->bounds().width()); | 84 EXPECT_LE(preferred_size.width(), this->bounds().width()); |
| 82 EXPECT_EQ(contents_->bounds().origin(), client_bounds.origin()); | 85 EXPECT_EQ(this->bounds().origin(), client_bounds.origin()); |
| 83 EXPECT_EQ(contents_->bounds().right(), client_bounds.right()); | 86 EXPECT_EQ(this->bounds().right(), client_bounds.right()); |
| 84 } | 87 } |
| 85 | 88 |
| 86 // Sets the buttons to show in the dialog and refreshes the dialog. | 89 // Sets the buttons to show in the dialog and refreshes the dialog. |
| 87 void SetDialogButtons(int dialog_buttons) { | 90 void SetDialogButtons(int dialog_buttons) { |
| 88 dialog_buttons_ = dialog_buttons; | 91 dialog_buttons_ = dialog_buttons; |
| 89 client_view_->UpdateDialogButtons(); | 92 client_view_->UpdateDialogButtons(); |
| 90 } | 93 } |
| 91 | 94 |
| 92 // Sets the extra view. | 95 // Sets the extra view. |
| 93 void SetExtraView(View* view) { | 96 void SetExtraView(View* view) { |
| 94 DCHECK(!extra_view_); | 97 DCHECK(!extra_view_); |
| 95 extra_view_ = view; | 98 extra_view_ = view; |
| 96 client_view_->CreateExtraViews(); | 99 client_view_->CreateExtraViews(); |
| 97 } | 100 } |
| 98 | 101 |
| 99 // Sets the extra view padding. | 102 // Sets the extra view padding. |
| 100 void SetExtraViewPadding(int padding) { | 103 void SetExtraViewPadding(int padding) { |
| 101 DCHECK(!extra_view_padding_); | 104 DCHECK(!extra_view_padding_); |
| 102 extra_view_padding_.reset(new int(padding)); | 105 extra_view_padding_.reset(new int(padding)); |
| 103 client_view_->Layout(); | 106 client_view_->Layout(); |
| 104 } | 107 } |
| 105 | 108 |
| 106 TestDialogClientView* client_view() { return client_view_.get(); } | 109 TestDialogClientView* client_view() { return client_view_.get(); } |
| 107 | 110 |
| 108 private: | 111 private: |
| 109 // The contents of the dialog. | |
| 110 scoped_ptr<View> contents_; | |
| 111 // The DialogClientView that's being tested. | 112 // The DialogClientView that's being tested. |
| 112 scoped_ptr<TestDialogClientView> client_view_; | 113 scoped_ptr<TestDialogClientView> client_view_; |
| 113 // The bitmask of buttons to show in the dialog. | 114 // The bitmask of buttons to show in the dialog. |
| 114 int dialog_buttons_; | 115 int dialog_buttons_; |
| 115 View* extra_view_; // weak | 116 View* extra_view_; // weak |
| 116 scoped_ptr<int> extra_view_padding_; // Null by default. | 117 scoped_ptr<int> extra_view_padding_; // Null by default. |
| 117 | 118 |
| 118 DISALLOW_COPY_AND_ASSIGN(DialogClientViewTest); | 119 DISALLOW_COPY_AND_ASSIGN(DialogClientViewTest); |
| 119 }; | 120 }; |
| 120 | 121 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 | 196 |
| 196 // Add extra view and remove OK button. | 197 // Add extra view and remove OK button. |
| 197 View* extra_view = new StaticSizedView(gfx::Size(200, 200)); | 198 View* extra_view = new StaticSizedView(gfx::Size(200, 200)); |
| 198 SetExtraView(extra_view); | 199 SetExtraView(extra_view); |
| 199 SetDialogButtons(ui::DIALOG_BUTTON_CANCEL); | 200 SetDialogButtons(ui::DIALOG_BUTTON_CANCEL); |
| 200 | 201 |
| 201 EXPECT_EQ(extra_view, | 202 EXPECT_EQ(extra_view, |
| 202 client_view()->GetContentsView()->GetNextFocusableView()); | 203 client_view()->GetContentsView()->GetNextFocusableView()); |
| 203 EXPECT_EQ(client_view()->cancel_button(), extra_view->GetNextFocusableView()); | 204 EXPECT_EQ(client_view()->cancel_button(), extra_view->GetNextFocusableView()); |
| 204 EXPECT_EQ(nullptr, client_view()->cancel_button()->GetNextFocusableView()); | 205 EXPECT_EQ(nullptr, client_view()->cancel_button()->GetNextFocusableView()); |
| 206 |
| 207 // Add a dummy view to the client view. |
| 208 View* dummy_view = new StaticSizedView(gfx::Size(200, 200)); |
| 209 client_view()->AddChildView(dummy_view); |
| 210 EXPECT_EQ(dummy_view, client_view()->cancel_button()->GetNextFocusableView()); |
| 205 } | 211 } |
| 206 | 212 |
| 207 // Test that the contents view gets its preferred size in the basic dialog | 213 // Test that the contents view gets its preferred size in the basic dialog |
| 208 // configuration. | 214 // configuration. |
| 209 TEST_F(DialogClientViewTest, ContentsSize) { | 215 TEST_F(DialogClientViewTest, ContentsSize) { |
| 210 CheckContentsIsSetToPreferredSize(); | 216 CheckContentsIsSetToPreferredSize(); |
| 211 EXPECT_EQ(GetContentsView()->bounds().bottom(), | 217 EXPECT_EQ(GetContentsView()->bounds().bottom(), |
| 212 client_view()->bounds().bottom()); | 218 client_view()->bounds().bottom()); |
| 213 } | 219 } |
| 214 | 220 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 239 EXPECT_EQ(no_extra_view_size.width(), client_view()->bounds().width()); | 245 EXPECT_EQ(no_extra_view_size.width(), client_view()->bounds().width()); |
| 240 | 246 |
| 241 // Try with a reduced-size dialog. | 247 // Try with a reduced-size dialog. |
| 242 extra_view->SetVisible(true); | 248 extra_view->SetVisible(true); |
| 243 client_view()->SetBoundsRect(gfx::Rect(gfx::Point(0, 0), no_extra_view_size)); | 249 client_view()->SetBoundsRect(gfx::Rect(gfx::Point(0, 0), no_extra_view_size)); |
| 244 client_view()->Layout(); | 250 client_view()->Layout(); |
| 245 EXPECT_GT(width_of_extra_view, extra_view->bounds().width()); | 251 EXPECT_GT(width_of_extra_view, extra_view->bounds().width()); |
| 246 } | 252 } |
| 247 | 253 |
| 248 } // namespace views | 254 } // namespace views |
| OLD | NEW |