| 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/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.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/test/test_views.h" | 10 #include "ui/views/test/test_views.h" |
| 10 #include "ui/views/window/dialog_client_view.h" | 11 #include "ui/views/window/dialog_client_view.h" |
| 11 #include "ui/views/window/dialog_delegate.h" | 12 #include "ui/views/window/dialog_delegate.h" |
| 12 | 13 |
| 13 namespace views { | 14 namespace views { |
| 14 | 15 |
| 15 class TestDialogClientView : public DialogClientView { | 16 class TestDialogClientView : public DialogClientView { |
| 16 public: | 17 public: |
| 17 TestDialogClientView(View* contents_view, | 18 TestDialogClientView(View* contents_view, |
| 18 DialogDelegate* delegate) | 19 DialogDelegate* dialog_delegate) |
| 19 : DialogClientView(contents_view), | 20 : DialogClientView(contents_view), |
| 20 delegate_(delegate) {} | 21 dialog_(dialog_delegate) {} |
| 21 virtual ~TestDialogClientView() {} | 22 virtual ~TestDialogClientView() {} |
| 22 | 23 |
| 23 // DialogClientView implementation. | 24 // DialogClientView implementation. |
| 24 virtual DialogDelegate* GetDialogDelegate() const OVERRIDE { | 25 virtual DialogDelegate* GetDialogDelegate() const OVERRIDE { return dialog_; } |
| 25 return delegate_; | |
| 26 } | |
| 27 | 26 |
| 28 View* GetContentsView() { | 27 View* GetContentsView() { return contents_view(); } |
| 29 return contents_view(); | |
| 30 } | |
| 31 | 28 |
| 32 void CreateExtraViews() { | 29 void CreateExtraViews() { |
| 33 CreateExtraView(); | 30 CreateExtraView(); |
| 34 CreateFootnoteView(); | 31 CreateFootnoteView(); |
| 35 } | 32 } |
| 36 | 33 |
| 37 private: | 34 private: |
| 38 DialogDelegate* delegate_; | 35 DialogDelegate* dialog_; |
| 39 | 36 |
| 40 DISALLOW_COPY_AND_ASSIGN(TestDialogClientView); | 37 DISALLOW_COPY_AND_ASSIGN(TestDialogClientView); |
| 41 }; | 38 }; |
| 42 | 39 |
| 43 class DialogClientViewTest : public testing::Test, | 40 class DialogClientViewTest : public testing::Test, |
| 44 public DialogDelegateView { | 41 public DialogDelegateView { |
| 45 public: | 42 public: |
| 46 DialogClientViewTest() | 43 DialogClientViewTest() |
| 47 : dialog_buttons_(ui::DIALOG_BUTTON_NONE), | 44 : dialog_buttons_(ui::DIALOG_BUTTON_NONE), |
| 48 extra_view_(NULL), | 45 extra_view_(NULL), |
| 49 footnote_view_(NULL) {} | 46 footnote_view_(NULL) {} |
| 50 virtual ~DialogClientViewTest() {} | 47 virtual ~DialogClientViewTest() {} |
| 51 | 48 |
| 52 // testing::Test implementation. | 49 // testing::Test implementation. |
| 53 virtual void SetUp() OVERRIDE { | 50 virtual void SetUp() OVERRIDE { |
| 54 dialog_buttons_ = ui::DIALOG_BUTTON_NONE; | 51 dialog_buttons_ = ui::DIALOG_BUTTON_NONE; |
| 55 contents_.reset(new StaticSizedView(gfx::Size(100, 200))); | 52 contents_.reset(new StaticSizedView(gfx::Size(100, 200))); |
| 56 client_view_.reset(new TestDialogClientView(contents_.get(), this)); | 53 client_view_.reset(new TestDialogClientView(contents_.get(), this)); |
| 57 } | 54 } |
| 58 | 55 |
| 59 // DialogDelegateView implementation. | 56 // DialogDelegateView implementation. |
| 60 virtual View* GetContentsView() OVERRIDE { | 57 virtual View* GetContentsView() OVERRIDE { return contents_.get(); } |
| 61 return contents_.get(); | 58 virtual View* CreateExtraView() OVERRIDE { return extra_view_; } |
| 62 } | 59 virtual View* CreateFootnoteView() OVERRIDE { return footnote_view_; } |
| 63 virtual View* CreateExtraView() OVERRIDE { | 60 virtual int GetDialogButtons() const OVERRIDE { return dialog_buttons_; } |
| 64 return extra_view_; | 61 |
| 65 } | 62 protected: |
| 66 virtual View* CreateFootnoteView() OVERRIDE { | 63 gfx::Rect GetUpdatedClientBounds() { |
| 67 return footnote_view_; | 64 client_view_->SizeToPreferredSize(); |
| 68 } | 65 client_view_->Layout(); |
| 69 virtual int GetDialogButtons() const OVERRIDE { | 66 return client_view_->bounds(); |
| 70 return dialog_buttons_; | |
| 71 } | 67 } |
| 72 | 68 |
| 73 protected: | 69 // Makes sure that the content view is sized correctly. Width must be at least |
| 74 void ResizeAndLayoutClientView() { | |
| 75 client_view_->SizeToPreferredSize(); | |
| 76 client_view_->Layout(); | |
| 77 } | |
| 78 | |
| 79 // Maes sure that the content view is sized correctly. Width must be at least | |
| 80 // the requested amount, but height should always match exactly. | 70 // the requested amount, but height should always match exactly. |
| 81 void CheckContentsIsSetToPreferredSize() { | 71 void CheckContentsIsSetToPreferredSize() { |
| 82 ResizeAndLayoutClientView(); | 72 const gfx::Rect client_bounds = GetUpdatedClientBounds(); |
| 83 gfx::Size preferred_size = contents_->GetPreferredSize(); | 73 const gfx::Size preferred_size = contents_->GetPreferredSize(); |
| 84 EXPECT_EQ(preferred_size.height(), contents_->bounds().height()); | 74 EXPECT_EQ(preferred_size.height(), contents_->bounds().height()); |
| 85 EXPECT_LE(preferred_size.width(), contents_->bounds().width()); | 75 EXPECT_LE(preferred_size.width(), contents_->bounds().width()); |
| 86 EXPECT_EQ(contents_->bounds().x(), client_view()->bounds().x()); | 76 EXPECT_EQ(contents_->bounds().origin(), client_bounds.origin()); |
| 87 EXPECT_EQ(contents_->bounds().y(), client_view()->bounds().y()); | 77 EXPECT_EQ(contents_->bounds().right(), client_bounds.right()); |
| 88 EXPECT_EQ(contents_->bounds().right(), client_view()->bounds().right()); | |
| 89 } | 78 } |
| 90 | 79 |
| 91 // Sets the buttons to show in the dialog and refreshes the dialog. | 80 // Sets the buttons to show in the dialog and refreshes the dialog. |
| 92 void SetDialogButtons(int dialog_buttons) { | 81 void SetDialogButtons(int dialog_buttons) { |
| 93 dialog_buttons_ = dialog_buttons; | 82 dialog_buttons_ = dialog_buttons; |
| 94 client_view_->UpdateDialogButtons(); | 83 client_view_->UpdateDialogButtons(); |
| 95 } | 84 } |
| 96 | 85 |
| 97 // Sets the extra view. | 86 // Sets the extra view. |
| 98 void SetExtraView(View* view) { | 87 void SetExtraView(View* view) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 116 // The DialogClientView that's being tested. | 105 // The DialogClientView that's being tested. |
| 117 scoped_ptr<TestDialogClientView> client_view_; | 106 scoped_ptr<TestDialogClientView> client_view_; |
| 118 // The bitmask of buttons to show in the dialog. | 107 // The bitmask of buttons to show in the dialog. |
| 119 int dialog_buttons_; | 108 int dialog_buttons_; |
| 120 View* extra_view_; // weak | 109 View* extra_view_; // weak |
| 121 View* footnote_view_; // weak | 110 View* footnote_view_; // weak |
| 122 | 111 |
| 123 DISALLOW_COPY_AND_ASSIGN(DialogClientViewTest); | 112 DISALLOW_COPY_AND_ASSIGN(DialogClientViewTest); |
| 124 }; | 113 }; |
| 125 | 114 |
| 126 TEST_F(DialogClientViewTest, ButtonStateCanChange) { | 115 TEST_F(DialogClientViewTest, UpdateButtons) { |
| 127 ResizeAndLayoutClientView(); | 116 // This dialog should start with no buttons. |
| 128 int no_button_height = client_view()->bounds().height(); | 117 EXPECT_EQ(GetDialogButtons(), ui::DIALOG_BUTTON_NONE); |
| 118 EXPECT_EQ(NULL, client_view()->ok_button()); |
| 119 EXPECT_EQ(NULL, client_view()->cancel_button()); |
| 120 const int height_without_buttons = GetUpdatedClientBounds().height(); |
| 129 | 121 |
| 122 // Update to use both buttons. |
| 130 SetDialogButtons(ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL); | 123 SetDialogButtons(ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL); |
| 131 ResizeAndLayoutClientView(); | 124 EXPECT_TRUE(client_view()->ok_button()->is_default()); |
| 132 EXPECT_GT(client_view()->bounds().height(), no_button_height); | 125 EXPECT_FALSE(client_view()->cancel_button()->is_default()); |
| 126 const int height_with_buttons = GetUpdatedClientBounds().height(); |
| 127 EXPECT_GT(height_with_buttons, height_without_buttons); |
| 128 |
| 129 // Remove the dialog buttons. |
| 130 SetDialogButtons(ui::DIALOG_BUTTON_NONE); |
| 131 EXPECT_EQ(NULL, client_view()->ok_button()); |
| 132 EXPECT_EQ(NULL, client_view()->cancel_button()); |
| 133 EXPECT_EQ(GetUpdatedClientBounds().height(), height_without_buttons); |
| 134 |
| 135 // Reset with just an ok button. |
| 136 SetDialogButtons(ui::DIALOG_BUTTON_OK); |
| 137 EXPECT_TRUE(client_view()->ok_button()->is_default()); |
| 138 EXPECT_EQ(NULL, client_view()->cancel_button()); |
| 139 EXPECT_EQ(GetUpdatedClientBounds().height(), height_with_buttons); |
| 140 |
| 141 // Reset with just a cancel button. |
| 142 SetDialogButtons(ui::DIALOG_BUTTON_CANCEL); |
| 143 EXPECT_EQ(NULL, client_view()->ok_button()); |
| 144 EXPECT_TRUE(client_view()->cancel_button()->is_default()); |
| 145 EXPECT_EQ(GetUpdatedClientBounds().height(), height_with_buttons); |
| 146 } |
| 147 |
| 148 TEST_F(DialogClientViewTest, RemoveAndUpdateButtons) { |
| 149 // Removing buttons from another context should clear the local pointer. |
| 150 SetDialogButtons(ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL); |
| 151 LabelButton* ok_button = client_view()->ok_button(); |
| 152 client_view()->RemoveChildView(ok_button); |
| 153 EXPECT_EQ(NULL, client_view()->ok_button()); |
| 154 delete ok_button; |
| 155 LabelButton* cancel_button = client_view()->cancel_button(); |
| 156 client_view()->RemoveChildView(cancel_button); |
| 157 EXPECT_EQ(NULL, client_view()->cancel_button()); |
| 158 delete cancel_button; |
| 159 |
| 160 // Updating should restore the requested buttons properly. |
| 161 SetDialogButtons(ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL); |
| 162 EXPECT_TRUE(client_view()->ok_button()->is_default()); |
| 163 EXPECT_FALSE(client_view()->cancel_button()->is_default()); |
| 133 } | 164 } |
| 134 | 165 |
| 135 // Test that the contents view gets its preferred size in the basic dialog | 166 // Test that the contents view gets its preferred size in the basic dialog |
| 136 // configuration. | 167 // configuration. |
| 137 TEST_F(DialogClientViewTest, ContentsSize) { | 168 TEST_F(DialogClientViewTest, ContentsSize) { |
| 138 CheckContentsIsSetToPreferredSize(); | 169 CheckContentsIsSetToPreferredSize(); |
| 139 EXPECT_EQ(GetContentsView()->bounds().bottom(), | 170 EXPECT_EQ(GetContentsView()->bounds().bottom(), |
| 140 client_view()->bounds().bottom()); | 171 client_view()->bounds().bottom()); |
| 141 } | 172 } |
| 142 | 173 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 | 231 |
| 201 View* footnote_view = new ProportionallySizedView(3); | 232 View* footnote_view = new ProportionallySizedView(3); |
| 202 SetFootnoteView(footnote_view); | 233 SetFootnoteView(footnote_view); |
| 203 CheckContentsIsSetToPreferredSize(); | 234 CheckContentsIsSetToPreferredSize(); |
| 204 EXPECT_GT(client_view()->bounds().height(), no_footnote_size.height()); | 235 EXPECT_GT(client_view()->bounds().height(), no_footnote_size.height()); |
| 205 EXPECT_EQ(footnote_view->bounds().width() * 3, | 236 EXPECT_EQ(footnote_view->bounds().width() * 3, |
| 206 footnote_view->bounds().height()); | 237 footnote_view->bounds().height()); |
| 207 } | 238 } |
| 208 | 239 |
| 209 } // namespace views | 240 } // namespace views |
| OLD | NEW |