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/window/dialog_client_view.h" | 5 #include "ui/views/window/dialog_client_view.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 #include "ui/base/ui_base_types.h" | 10 #include "ui/base/ui_base_types.h" |
(...skipping 25 matching lines...) Expand all Loading... | |
36 widget_->Init(params); | 36 widget_->Init(params); |
37 EXPECT_EQ(this, GetContentsView()); | 37 EXPECT_EQ(this, GetContentsView()); |
38 } | 38 } |
39 | 39 |
40 void TearDown() override { | 40 void TearDown() override { |
41 widget_->CloseNow(); | 41 widget_->CloseNow(); |
42 WidgetTest::TearDown(); | 42 WidgetTest::TearDown(); |
43 } | 43 } |
44 | 44 |
45 // DialogDelegateView: | 45 // DialogDelegateView: |
46 gfx::Size GetPreferredSize() const override { return preferred_size_; } | |
47 gfx::Size GetMinimumSize() const override { return min_size_; } | |
48 gfx::Size GetMaximumSize() const override { return max_size_; } | |
46 ClientView* CreateClientView(Widget* widget) override { | 49 ClientView* CreateClientView(Widget* widget) override { |
47 client_view_ = new DialogClientView(widget, this); | 50 client_view_ = new DialogClientView(widget, this); |
48 return client_view_; | 51 return client_view_; |
49 } | 52 } |
50 | 53 |
51 bool ShouldUseCustomFrame() const override { return false; } | 54 bool ShouldUseCustomFrame() const override { return false; } |
52 | 55 |
53 void DeleteDelegate() override { | 56 void DeleteDelegate() override { |
54 // DialogDelegateView would delete this, but |this| is owned by the test. | 57 // DialogDelegateView would delete this, but |this| is owned by the test. |
55 } | 58 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 EXPECT_FALSE(next_extra_view_); | 98 EXPECT_FALSE(next_extra_view_); |
96 next_extra_view_ = base::WrapUnique(view); | 99 next_extra_view_ = base::WrapUnique(view); |
97 client_view_->UpdateDialogButtons(); | 100 client_view_->UpdateDialogButtons(); |
98 EXPECT_FALSE(next_extra_view_); | 101 EXPECT_FALSE(next_extra_view_); |
99 } | 102 } |
100 | 103 |
101 // Sets the extra view padding. | 104 // Sets the extra view padding. |
102 void SetExtraViewPadding(int padding) { | 105 void SetExtraViewPadding(int padding) { |
103 DCHECK(!extra_view_padding_); | 106 DCHECK(!extra_view_padding_); |
104 extra_view_padding_.reset(new int(padding)); | 107 extra_view_padding_.reset(new int(padding)); |
105 client_view_->Layout(); | 108 client_view_->UpdateDialogButtons(); |
109 } | |
110 | |
111 void SetSizeConstraints(const gfx::Size& min, | |
Peter Kasting
2017/02/25 06:04:08
Nit: Suffix these args with _size
tapted
2017/02/27 10:04:20
Done.
| |
112 const gfx::Size& preferred, | |
113 const gfx::Size& max) { | |
114 min_size_ = min; | |
115 preferred_size_ = preferred; | |
116 max_size_ = max; | |
106 } | 117 } |
107 | 118 |
108 View* FocusableViewAfter(View* view) { | 119 View* FocusableViewAfter(View* view) { |
109 const bool dont_loop = false; | 120 const bool dont_loop = false; |
110 const bool reverse = false; | 121 const bool reverse = false; |
111 return GetFocusManager()->GetNextFocusableView(view, GetWidget(), reverse, | 122 return GetFocusManager()->GetNextFocusableView(view, GetWidget(), reverse, |
112 dont_loop); | 123 dont_loop); |
113 } | 124 } |
114 | 125 |
115 DialogClientView* client_view() { return client_view_; } | 126 DialogClientView* client_view() { return client_view_; } |
116 | 127 |
117 private: | 128 private: |
118 // The dialog Widget. | 129 // The dialog Widget. |
119 Widget* widget_ = nullptr; | 130 Widget* widget_ = nullptr; |
120 | 131 |
121 // The DialogClientView that's being tested. Owned by |widget_|. | 132 // The DialogClientView that's being tested. Owned by |widget_|. |
122 DialogClientView* client_view_; | 133 DialogClientView* client_view_; |
123 | 134 |
124 // The bitmask of buttons to show in the dialog. | 135 // The bitmask of buttons to show in the dialog. |
125 int dialog_buttons_ = ui::DIALOG_BUTTON_NONE; | 136 int dialog_buttons_ = ui::DIALOG_BUTTON_NONE; |
126 | 137 |
127 // Set and cleared in SetExtraView(). | 138 // Set and cleared in SetExtraView(). |
128 std::unique_ptr<View> next_extra_view_; | 139 std::unique_ptr<View> next_extra_view_; |
129 | 140 |
130 std::unique_ptr<int> extra_view_padding_; | 141 std::unique_ptr<int> extra_view_padding_; |
131 | 142 |
143 gfx::Size preferred_size_; | |
144 gfx::Size min_size_; | |
145 gfx::Size max_size_; | |
146 | |
132 DISALLOW_COPY_AND_ASSIGN(DialogClientViewTest); | 147 DISALLOW_COPY_AND_ASSIGN(DialogClientViewTest); |
133 }; | 148 }; |
134 | 149 |
135 TEST_F(DialogClientViewTest, UpdateButtons) { | 150 TEST_F(DialogClientViewTest, UpdateButtons) { |
136 // This dialog should start with no buttons. | 151 // This dialog should start with no buttons. |
137 EXPECT_EQ(GetDialogButtons(), ui::DIALOG_BUTTON_NONE); | 152 EXPECT_EQ(GetDialogButtons(), ui::DIALOG_BUTTON_NONE); |
138 EXPECT_EQ(NULL, client_view()->ok_button()); | 153 EXPECT_EQ(NULL, client_view()->ok_button()); |
139 EXPECT_EQ(NULL, client_view()->cancel_button()); | 154 EXPECT_EQ(NULL, client_view()->cancel_button()); |
140 const int height_without_buttons = GetUpdatedClientBounds().height(); | 155 const int height_without_buttons = GetUpdatedClientBounds().height(); |
141 | 156 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
181 } | 196 } |
182 | 197 |
183 // Test that views inside the dialog client view have the correct focus order. | 198 // Test that views inside the dialog client view have the correct focus order. |
184 TEST_F(DialogClientViewTest, SetupFocusChain) { | 199 TEST_F(DialogClientViewTest, SetupFocusChain) { |
185 #if defined(OS_WIN) || defined(OS_CHROMEOS) | 200 #if defined(OS_WIN) || defined(OS_CHROMEOS) |
186 const bool kIsOkButtonOnLeftSide = true; | 201 const bool kIsOkButtonOnLeftSide = true; |
187 #else | 202 #else |
188 const bool kIsOkButtonOnLeftSide = false; | 203 const bool kIsOkButtonOnLeftSide = false; |
189 #endif | 204 #endif |
190 | 205 |
206 GetContentsView()->SetFocusBehavior(View::FocusBehavior::ALWAYS); | |
191 // Initially the dialog client view only contains the content view. | 207 // Initially the dialog client view only contains the content view. |
192 EXPECT_EQ(nullptr, GetContentsView()->GetNextFocusableView()); | 208 EXPECT_EQ(GetContentsView(), FocusableViewAfter(GetContentsView())); |
193 | 209 |
194 // Add OK and cancel buttons. | 210 // Add OK and cancel buttons. |
195 SetDialogButtons(ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL); | 211 SetDialogButtons(ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL); |
196 | 212 |
197 if (kIsOkButtonOnLeftSide) { | 213 if (kIsOkButtonOnLeftSide) { |
198 EXPECT_EQ(client_view()->ok_button(), | 214 EXPECT_EQ(client_view()->ok_button(), |
199 GetContentsView()->GetNextFocusableView()); | 215 FocusableViewAfter(GetContentsView())); |
200 EXPECT_EQ(client_view()->cancel_button(), | 216 EXPECT_EQ(client_view()->cancel_button(), |
201 client_view()->ok_button()->GetNextFocusableView()); | 217 FocusableViewAfter(client_view()->ok_button())); |
202 EXPECT_EQ(nullptr, client_view()->cancel_button()->GetNextFocusableView()); | 218 EXPECT_EQ(GetContentsView(), |
219 FocusableViewAfter(client_view()->cancel_button())); | |
203 } else { | 220 } else { |
204 EXPECT_EQ(client_view()->cancel_button(), | 221 EXPECT_EQ(client_view()->cancel_button(), |
205 GetContentsView()->GetNextFocusableView()); | 222 FocusableViewAfter(GetContentsView())); |
206 EXPECT_EQ(client_view()->ok_button(), | 223 EXPECT_EQ(client_view()->ok_button(), |
207 client_view()->cancel_button()->GetNextFocusableView()); | 224 FocusableViewAfter(client_view()->cancel_button())); |
208 EXPECT_EQ(nullptr, client_view()->ok_button()->GetNextFocusableView()); | 225 EXPECT_EQ(GetContentsView(), |
226 FocusableViewAfter(client_view()->ok_button())); | |
209 } | 227 } |
210 | 228 |
211 // Add extra view and remove OK button. | 229 // Add extra view and remove OK button. |
212 View* extra_view = new StaticSizedView(gfx::Size(200, 200)); | 230 View* extra_view = new StaticSizedView(gfx::Size(200, 200)); |
213 extra_view->SetFocusBehavior(View::FocusBehavior::ALWAYS); | 231 extra_view->SetFocusBehavior(View::FocusBehavior::ALWAYS); |
214 SetExtraView(extra_view); | 232 SetExtraView(extra_view); |
215 SetDialogButtons(ui::DIALOG_BUTTON_CANCEL); | 233 SetDialogButtons(ui::DIALOG_BUTTON_CANCEL); |
216 | 234 |
217 EXPECT_EQ(extra_view, GetContentsView()->GetNextFocusableView()); | 235 EXPECT_EQ(extra_view, FocusableViewAfter(GetContentsView())); |
218 EXPECT_EQ(client_view()->cancel_button(), extra_view->GetNextFocusableView()); | 236 EXPECT_EQ(client_view()->cancel_button(), FocusableViewAfter(extra_view)); |
219 EXPECT_EQ(nullptr, client_view()->cancel_button()->GetNextFocusableView()); | 237 EXPECT_EQ(GetContentsView(), FocusableViewAfter(client_view())); |
220 | 238 |
221 // Add a dummy view to the contents view. Consult the FocusManager for the | 239 // Add a dummy view to the contents view. Consult the FocusManager for the |
222 // traversal order since it now spans different levels of the view hierarchy. | 240 // traversal order since it now spans different levels of the view hierarchy. |
223 View* dummy_view = new StaticSizedView(gfx::Size(200, 200)); | 241 View* dummy_view = new StaticSizedView(gfx::Size(200, 200)); |
224 dummy_view->SetFocusBehavior(View::FocusBehavior::ALWAYS); | 242 dummy_view->SetFocusBehavior(View::FocusBehavior::ALWAYS); |
243 GetContentsView()->SetFocusBehavior(View::FocusBehavior::NEVER); | |
225 GetContentsView()->AddChildView(dummy_view); | 244 GetContentsView()->AddChildView(dummy_view); |
226 EXPECT_EQ(dummy_view, FocusableViewAfter(client_view()->cancel_button())); | 245 EXPECT_EQ(dummy_view, FocusableViewAfter(client_view()->cancel_button())); |
227 EXPECT_EQ(extra_view, FocusableViewAfter(dummy_view)); | 246 EXPECT_EQ(extra_view, FocusableViewAfter(dummy_view)); |
228 EXPECT_EQ(client_view()->cancel_button(), FocusableViewAfter(extra_view)); | 247 EXPECT_EQ(client_view()->cancel_button(), FocusableViewAfter(extra_view)); |
229 | 248 |
230 // Views are added to the contents view, not the client view, so the focus | 249 // Views are added to the contents view, not the client view, so the focus |
231 // chain within the client view is not affected. | 250 // chain within the client view is not affected. |
232 EXPECT_EQ(nullptr, client_view()->cancel_button()->GetNextFocusableView()); | 251 EXPECT_EQ(nullptr, client_view()->cancel_button()->GetNextFocusableView()); |
233 } | 252 } |
234 | 253 |
(...skipping 30 matching lines...) Expand all Loading... | |
265 // Visibility of extra view is respected. | 284 // Visibility of extra view is respected. |
266 extra_view->SetVisible(false); | 285 extra_view->SetVisible(false); |
267 CheckContentsIsSetToPreferredSize(); | 286 CheckContentsIsSetToPreferredSize(); |
268 EXPECT_EQ(no_extra_view_size.height(), client_view()->bounds().height()); | 287 EXPECT_EQ(no_extra_view_size.height(), client_view()->bounds().height()); |
269 EXPECT_EQ(no_extra_view_size.width(), client_view()->bounds().width()); | 288 EXPECT_EQ(no_extra_view_size.width(), client_view()->bounds().width()); |
270 | 289 |
271 // Try with a reduced-size dialog. | 290 // Try with a reduced-size dialog. |
272 extra_view->SetVisible(true); | 291 extra_view->SetVisible(true); |
273 client_view()->SetBoundsRect(gfx::Rect(gfx::Point(0, 0), no_extra_view_size)); | 292 client_view()->SetBoundsRect(gfx::Rect(gfx::Point(0, 0), no_extra_view_size)); |
274 client_view()->Layout(); | 293 client_view()->Layout(); |
294 | |
295 // Initially nothing happens. The dialog always incorporates the preferred | |
296 // size of the extra view into the layout. | |
297 EXPECT_EQ(width_of_extra_view, extra_view->bounds().width()); | |
298 | |
299 // Set a smaller size and try again (rely on ChildPreferredSizeChanged() to | |
300 // trigger Layout()). | |
301 extra_view->SetSize(gfx::Size(10, 10)); | |
275 EXPECT_GT(width_of_extra_view, extra_view->bounds().width()); | 302 EXPECT_GT(width_of_extra_view, extra_view->bounds().width()); |
276 } | 303 } |
277 | 304 |
305 // Ensure the minimum, maximum and preferred sizes of the contents view are | |
306 // respected by the client view, and that the client view includes the button | |
307 // row in its minimum and preferred size calculations. | |
308 TEST_F(DialogClientViewTest, MinMaxPreferredSize) { | |
309 SetDialogButtons(ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL); | |
310 const gfx::Size buttons = client_view()->GetPreferredSize(); | |
Peter Kasting
2017/02/25 06:04:08
Nit: Use a name like |buttons_size|, since |button
tapted
2017/02/27 10:04:20
Done.
| |
311 EXPECT_FALSE(buttons.IsEmpty()); | |
312 | |
313 // When the contents view has no preference, just fit the buttons. | |
314 EXPECT_EQ(buttons, client_view()->GetMinimumSize()); | |
315 EXPECT_EQ(gfx::Size(), client_view()->GetMaximumSize()); | |
Peter Kasting
2017/02/25 06:04:08
Nit: IsEmpty()?
tapted
2017/02/27 10:04:20
Same as the other ones - I want to check that both
| |
316 | |
317 // Ensure buttons are between these widths, for the constants below. | |
318 EXPECT_LT(20, buttons.width()); | |
319 EXPECT_GT(300, buttons.width()); | |
320 | |
321 // With no buttons, client view should match the contents view. | |
322 SetDialogButtons(ui::DIALOG_BUTTON_NONE); | |
323 SetSizeConstraints(gfx::Size(10, 15), gfx::Size(20, 25), gfx::Size(300, 350)); | |
324 EXPECT_EQ(gfx::Size(10, 15), client_view()->GetMinimumSize()); | |
325 EXPECT_EQ(gfx::Size(20, 25), client_view()->GetPreferredSize()); | |
326 EXPECT_EQ(gfx::Size(300, 350), client_view()->GetMaximumSize()); | |
327 | |
328 // With buttons, size should increase vertically only. | |
329 SetDialogButtons(ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL); | |
330 EXPECT_EQ(gfx::Size(buttons.width(), 15 + buttons.height()), | |
331 client_view()->GetMinimumSize()); | |
332 EXPECT_EQ(gfx::Size(buttons.width(), 25 + buttons.height()), | |
333 client_view()->GetPreferredSize()); | |
334 EXPECT_EQ(gfx::Size(300, 350 + buttons.height()), | |
335 client_view()->GetMaximumSize()); | |
336 | |
337 // If the contents view gets bigger, it should take over the width. | |
338 SetSizeConstraints(gfx::Size(400, 450), gfx::Size(500, 550), | |
339 gfx::Size(600, 650)); | |
340 EXPECT_EQ(gfx::Size(400, 450 + buttons.height()), | |
341 client_view()->GetMinimumSize()); | |
342 EXPECT_EQ(gfx::Size(500, 550 + buttons.height()), | |
343 client_view()->GetPreferredSize()); | |
344 EXPECT_EQ(gfx::Size(600, 650 + buttons.height()), | |
345 client_view()->GetMaximumSize()); | |
346 } | |
347 | |
278 } // namespace views | 348 } // namespace views |
tapted
2017/02/27 10:04:20
This is the dubious Layout call:
void RootView::S
| |
OLD | NEW |