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" |
(...skipping 12 matching lines...) Expand all Loading... |
23 dialog_(dialog_delegate) {} | 23 dialog_(dialog_delegate) {} |
24 ~TestDialogClientView() override {} | 24 ~TestDialogClientView() override {} |
25 | 25 |
26 // DialogClientView implementation. | 26 // DialogClientView implementation. |
27 DialogDelegate* GetDialogDelegate() const override { return dialog_; } | 27 DialogDelegate* GetDialogDelegate() const override { return dialog_; } |
28 | 28 |
29 View* GetContentsView() { return contents_view(); } | 29 View* GetContentsView() { return contents_view(); } |
30 | 30 |
31 void CreateExtraViews() { | 31 void CreateExtraViews() { |
32 CreateExtraView(); | 32 CreateExtraView(); |
33 CreateFootnoteView(); | |
34 } | 33 } |
35 | 34 |
36 private: | 35 private: |
37 DialogDelegate* dialog_; | 36 DialogDelegate* dialog_; |
38 | 37 |
39 DISALLOW_COPY_AND_ASSIGN(TestDialogClientView); | 38 DISALLOW_COPY_AND_ASSIGN(TestDialogClientView); |
40 }; | 39 }; |
41 | 40 |
42 class DialogClientViewTest : public ViewsTestBase, | 41 class DialogClientViewTest : public ViewsTestBase, |
43 public DialogDelegateView { | 42 public DialogDelegateView { |
44 public: | 43 public: |
45 DialogClientViewTest() | 44 DialogClientViewTest() |
46 : dialog_buttons_(ui::DIALOG_BUTTON_NONE), | 45 : dialog_buttons_(ui::DIALOG_BUTTON_NONE), |
47 extra_view_(NULL), | 46 extra_view_(nullptr) {} |
48 footnote_view_(NULL) {} | |
49 ~DialogClientViewTest() override {} | 47 ~DialogClientViewTest() override {} |
50 | 48 |
51 // testing::Test implementation. | 49 // testing::Test implementation. |
52 void SetUp() override { | 50 void SetUp() override { |
53 dialog_buttons_ = ui::DIALOG_BUTTON_NONE; | 51 dialog_buttons_ = ui::DIALOG_BUTTON_NONE; |
54 contents_.reset(new StaticSizedView(gfx::Size(100, 200))); | 52 contents_.reset(new StaticSizedView(gfx::Size(100, 200))); |
55 client_view_.reset(new TestDialogClientView(contents_.get(), this)); | 53 client_view_.reset(new TestDialogClientView(contents_.get(), this)); |
56 | 54 |
57 ViewsTestBase::SetUp(); | 55 ViewsTestBase::SetUp(); |
58 } | 56 } |
59 | 57 |
60 // DialogDelegateView implementation. | 58 // DialogDelegateView implementation. |
61 View* GetContentsView() override { return contents_.get(); } | 59 View* GetContentsView() override { return contents_.get(); } |
62 View* CreateExtraView() override { return extra_view_; } | 60 View* CreateExtraView() override { return extra_view_; } |
63 bool GetExtraViewPadding(int* padding) override { | 61 bool GetExtraViewPadding(int* padding) override { |
64 if (extra_view_padding_) | 62 if (extra_view_padding_) |
65 *padding = *extra_view_padding_; | 63 *padding = *extra_view_padding_; |
66 return extra_view_padding_.get() != nullptr; | 64 return extra_view_padding_.get() != nullptr; |
67 } | 65 } |
68 View* CreateFootnoteView() override { return footnote_view_; } | |
69 int GetDialogButtons() const override { return dialog_buttons_; } | 66 int GetDialogButtons() const override { return dialog_buttons_; } |
70 | 67 |
71 protected: | 68 protected: |
72 gfx::Rect GetUpdatedClientBounds() { | 69 gfx::Rect GetUpdatedClientBounds() { |
73 client_view_->SizeToPreferredSize(); | 70 client_view_->SizeToPreferredSize(); |
74 client_view_->Layout(); | 71 client_view_->Layout(); |
75 return client_view_->bounds(); | 72 return client_view_->bounds(); |
76 } | 73 } |
77 | 74 |
78 // Makes sure that the content view is sized correctly. Width must be at least | 75 // Makes sure that the content view is sized correctly. Width must be at least |
(...skipping 20 matching lines...) Expand all Loading... |
99 client_view_->CreateExtraViews(); | 96 client_view_->CreateExtraViews(); |
100 } | 97 } |
101 | 98 |
102 // Sets the extra view padding. | 99 // Sets the extra view padding. |
103 void SetExtraViewPadding(int padding) { | 100 void SetExtraViewPadding(int padding) { |
104 DCHECK(!extra_view_padding_); | 101 DCHECK(!extra_view_padding_); |
105 extra_view_padding_.reset(new int(padding)); | 102 extra_view_padding_.reset(new int(padding)); |
106 client_view_->Layout(); | 103 client_view_->Layout(); |
107 } | 104 } |
108 | 105 |
109 // Sets the footnote view. | |
110 void SetFootnoteView(View* view) { | |
111 DCHECK(!footnote_view_); | |
112 footnote_view_ = view; | |
113 client_view_->CreateExtraViews(); | |
114 } | |
115 | |
116 TestDialogClientView* client_view() { return client_view_.get(); } | 106 TestDialogClientView* client_view() { return client_view_.get(); } |
117 | 107 |
118 private: | 108 private: |
119 // The contents of the dialog. | 109 // The contents of the dialog. |
120 scoped_ptr<View> contents_; | 110 scoped_ptr<View> contents_; |
121 // The DialogClientView that's being tested. | 111 // The DialogClientView that's being tested. |
122 scoped_ptr<TestDialogClientView> client_view_; | 112 scoped_ptr<TestDialogClientView> client_view_; |
123 // The bitmask of buttons to show in the dialog. | 113 // The bitmask of buttons to show in the dialog. |
124 int dialog_buttons_; | 114 int dialog_buttons_; |
125 View* extra_view_; // weak | 115 View* extra_view_; // weak |
126 scoped_ptr<int> extra_view_padding_; // Null by default. | 116 scoped_ptr<int> extra_view_padding_; // Null by default. |
127 View* footnote_view_; // weak | |
128 | 117 |
129 DISALLOW_COPY_AND_ASSIGN(DialogClientViewTest); | 118 DISALLOW_COPY_AND_ASSIGN(DialogClientViewTest); |
130 }; | 119 }; |
131 | 120 |
132 TEST_F(DialogClientViewTest, UpdateButtons) { | 121 TEST_F(DialogClientViewTest, UpdateButtons) { |
133 // This dialog should start with no buttons. | 122 // This dialog should start with no buttons. |
134 EXPECT_EQ(GetDialogButtons(), ui::DIALOG_BUTTON_NONE); | 123 EXPECT_EQ(GetDialogButtons(), ui::DIALOG_BUTTON_NONE); |
135 EXPECT_EQ(NULL, client_view()->ok_button()); | 124 EXPECT_EQ(NULL, client_view()->ok_button()); |
136 EXPECT_EQ(NULL, client_view()->cancel_button()); | 125 EXPECT_EQ(NULL, client_view()->cancel_button()); |
137 const int height_without_buttons = GetUpdatedClientBounds().height(); | 126 const int height_without_buttons = GetUpdatedClientBounds().height(); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 EXPECT_EQ(no_extra_view_size.height(), client_view()->bounds().height()); | 238 EXPECT_EQ(no_extra_view_size.height(), client_view()->bounds().height()); |
250 EXPECT_EQ(no_extra_view_size.width(), client_view()->bounds().width()); | 239 EXPECT_EQ(no_extra_view_size.width(), client_view()->bounds().width()); |
251 | 240 |
252 // Try with a reduced-size dialog. | 241 // Try with a reduced-size dialog. |
253 extra_view->SetVisible(true); | 242 extra_view->SetVisible(true); |
254 client_view()->SetBoundsRect(gfx::Rect(gfx::Point(0, 0), no_extra_view_size)); | 243 client_view()->SetBoundsRect(gfx::Rect(gfx::Point(0, 0), no_extra_view_size)); |
255 client_view()->Layout(); | 244 client_view()->Layout(); |
256 EXPECT_GT(width_of_extra_view, extra_view->bounds().width()); | 245 EXPECT_GT(width_of_extra_view, extra_view->bounds().width()); |
257 } | 246 } |
258 | 247 |
259 // Test the effect of the footnote view on layout. | |
260 TEST_F(DialogClientViewTest, LayoutWithFootnote) { | |
261 CheckContentsIsSetToPreferredSize(); | |
262 gfx::Size no_footnote_size = client_view()->bounds().size(); | |
263 | |
264 View* footnote_view = new StaticSizedView(gfx::Size(200, 200)); | |
265 SetFootnoteView(footnote_view); | |
266 CheckContentsIsSetToPreferredSize(); | |
267 EXPECT_GT(client_view()->bounds().height(), no_footnote_size.height()); | |
268 EXPECT_EQ(200, footnote_view->bounds().height()); | |
269 gfx::Size with_footnote_size = client_view()->bounds().size(); | |
270 EXPECT_EQ(with_footnote_size.width(), footnote_view->bounds().width()); | |
271 | |
272 SetDialogButtons(ui::DIALOG_BUTTON_CANCEL); | |
273 CheckContentsIsSetToPreferredSize(); | |
274 EXPECT_LE(with_footnote_size.height(), client_view()->bounds().height()); | |
275 EXPECT_LE(with_footnote_size.width(), client_view()->bounds().width()); | |
276 gfx::Size with_footnote_and_button_size = client_view()->bounds().size(); | |
277 | |
278 SetDialogButtons(ui::DIALOG_BUTTON_NONE); | |
279 footnote_view->SetVisible(false); | |
280 CheckContentsIsSetToPreferredSize(); | |
281 EXPECT_EQ(no_footnote_size.height(), client_view()->bounds().height()); | |
282 EXPECT_EQ(no_footnote_size.width(), client_view()->bounds().width()); | |
283 } | |
284 | |
285 // Test that GetHeightForWidth is respected for the footnote view. | |
286 TEST_F(DialogClientViewTest, LayoutWithFootnoteHeightForWidth) { | |
287 CheckContentsIsSetToPreferredSize(); | |
288 gfx::Size no_footnote_size = client_view()->bounds().size(); | |
289 | |
290 View* footnote_view = new ProportionallySizedView(3); | |
291 SetFootnoteView(footnote_view); | |
292 CheckContentsIsSetToPreferredSize(); | |
293 EXPECT_GT(client_view()->bounds().height(), no_footnote_size.height()); | |
294 EXPECT_EQ(footnote_view->bounds().width() * 3, | |
295 footnote_view->bounds().height()); | |
296 } | |
297 | |
298 } // namespace views | 248 } // namespace views |
OLD | NEW |