Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(115)

Side by Side Diff: ui/views/window/dialog_client_view_unittest.cc

Issue 2706423002: Use GridLayout for DialogClientView's button row. (Closed)
Patch Set: rebase for r452937 Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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_size,
112 const gfx::Size& preferred_size,
113 const gfx::Size& max_size) {
114 min_size_ = min_size;
115 preferred_size_ = preferred_size;
116 max_size_ = max_size;
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
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
235 // Test that the contents view gets its preferred size in the basic dialog 254 // Test that the contents view gets its preferred size in the basic dialog
236 // configuration. 255 // configuration.
237 TEST_F(DialogClientViewTest, ContentsSize) { 256 TEST_F(DialogClientViewTest, ContentsSize) {
238 CheckContentsIsSetToPreferredSize(); 257 CheckContentsIsSetToPreferredSize();
239 EXPECT_EQ(GetContentsView()->size(), client_view()->size()); 258 EXPECT_EQ(GetContentsView()->size(), client_view()->size());
240 // There's nothing in the contents view (i.e. |this|), so it should be 0x0. 259 // There's nothing in the contents view (i.e. |this|), so it should be 0x0.
241 EXPECT_EQ(gfx::Size(), client_view()->size()); 260 EXPECT_EQ(gfx::Size(), client_view()->size());
242 } 261 }
243 262
244 // Test the effect of the button strip on layout. 263 // Test the effect of the button strip on layout.
245 TEST_F(DialogClientViewTest, LayoutWithButtons) { 264 TEST_F(DialogClientViewTest, LayoutWithButtons) {
246 SetDialogButtons(ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL); 265 SetDialogButtons(ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL);
247 CheckContentsIsSetToPreferredSize(); 266 CheckContentsIsSetToPreferredSize();
248 267
249 EXPECT_LT(GetContentsView()->bounds().bottom(), 268 EXPECT_LT(GetContentsView()->bounds().bottom(),
250 client_view()->bounds().bottom()); 269 client_view()->bounds().bottom());
251 gfx::Size no_extra_view_size = client_view()->bounds().size(); 270 const gfx::Size no_extra_view_size = client_view()->bounds().size();
252 271
253 View* extra_view = new StaticSizedView(gfx::Size(200, 200)); 272 View* extra_view = new StaticSizedView(gfx::Size(200, 200));
254 SetExtraView(extra_view); 273 SetExtraView(extra_view);
255 CheckContentsIsSetToPreferredSize(); 274 CheckContentsIsSetToPreferredSize();
256 EXPECT_GT(client_view()->bounds().height(), no_extra_view_size.height()); 275 EXPECT_GT(client_view()->bounds().height(), no_extra_view_size.height());
257 int width_of_dialog = client_view()->bounds().width(); 276 const int width_of_dialog_small_padding = client_view()->width();
258 int width_of_extra_view = extra_view->bounds().width();
259 277
260 // Try with an adjusted padding for the extra view. 278 // Try with an adjusted padding for the extra view.
261 SetExtraViewPadding(250); 279 SetExtraViewPadding(250);
262 CheckContentsIsSetToPreferredSize(); 280 CheckContentsIsSetToPreferredSize();
263 EXPECT_GT(client_view()->bounds().width(), width_of_dialog); 281 EXPECT_GT(client_view()->bounds().width(), width_of_dialog_small_padding);
264 282
265 // Visibility of extra view is respected. 283 const gfx::Size with_extra_view_size = client_view()->size();
284 EXPECT_NE(no_extra_view_size, with_extra_view_size);
285
286 // Hiding the extra view removes it as well as the extra padding.
266 extra_view->SetVisible(false); 287 extra_view->SetVisible(false);
267 CheckContentsIsSetToPreferredSize(); 288 CheckContentsIsSetToPreferredSize();
268 EXPECT_EQ(no_extra_view_size.height(), client_view()->bounds().height()); 289 EXPECT_EQ(no_extra_view_size, client_view()->size());
269 EXPECT_EQ(no_extra_view_size.width(), client_view()->bounds().width());
270 290
271 // Try with a reduced-size dialog. 291 // Making it visible again adds it all back.
272 extra_view->SetVisible(true); 292 extra_view->SetVisible(true);
273 client_view()->SetBoundsRect(gfx::Rect(gfx::Point(0, 0), no_extra_view_size)); 293 CheckContentsIsSetToPreferredSize();
274 client_view()->Layout(); 294 EXPECT_EQ(with_extra_view_size, client_view()->size());
275 EXPECT_GT(width_of_extra_view, extra_view->bounds().width()); 295
296 // Leave |extra_view| hidden. It should still have a parent, to ensure it is
297 // owned by a View hierarchy and gets deleted.
298 extra_view->SetVisible(false);
299 EXPECT_TRUE(extra_view->parent());
300 }
301
302 // Ensure the minimum, maximum and preferred sizes of the contents view are
303 // respected by the client view, and that the client view includes the button
304 // row in its minimum and preferred size calculations.
305 TEST_F(DialogClientViewTest, MinMaxPreferredSize) {
306 SetDialogButtons(ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL);
307 const gfx::Size buttons_size = client_view()->GetPreferredSize();
308 EXPECT_FALSE(buttons_size.IsEmpty());
309
310 // When the contents view has no preference, just fit the buttons. The
311 // maximum size should be unconstrained in both directions.
312 EXPECT_EQ(buttons_size, client_view()->GetMinimumSize());
313 EXPECT_EQ(gfx::Size(), client_view()->GetMaximumSize());
314
315 // Ensure buttons are between these widths, for the constants below.
316 EXPECT_LT(20, buttons_size.width());
317 EXPECT_GT(300, buttons_size.width());
318
319 // With no buttons, client view should match the contents view.
320 SetDialogButtons(ui::DIALOG_BUTTON_NONE);
321 SetSizeConstraints(gfx::Size(10, 15), gfx::Size(20, 25), gfx::Size(300, 350));
322 EXPECT_EQ(gfx::Size(10, 15), client_view()->GetMinimumSize());
323 EXPECT_EQ(gfx::Size(20, 25), client_view()->GetPreferredSize());
324 EXPECT_EQ(gfx::Size(300, 350), client_view()->GetMaximumSize());
325
326 // With buttons, size should increase vertically only.
327 SetDialogButtons(ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL);
328 EXPECT_EQ(gfx::Size(buttons_size.width(), 15 + buttons_size.height()),
329 client_view()->GetMinimumSize());
330 EXPECT_EQ(gfx::Size(buttons_size.width(), 25 + buttons_size.height()),
331 client_view()->GetPreferredSize());
332 EXPECT_EQ(gfx::Size(300, 350 + buttons_size.height()),
333 client_view()->GetMaximumSize());
334
335 // If the contents view gets bigger, it should take over the width.
336 SetSizeConstraints(gfx::Size(400, 450), gfx::Size(500, 550),
337 gfx::Size(600, 650));
338 EXPECT_EQ(gfx::Size(400, 450 + buttons_size.height()),
339 client_view()->GetMinimumSize());
340 EXPECT_EQ(gfx::Size(500, 550 + buttons_size.height()),
341 client_view()->GetPreferredSize());
342 EXPECT_EQ(gfx::Size(600, 650 + buttons_size.height()),
343 client_view()->GetMaximumSize());
276 } 344 }
277 345
278 } // namespace views 346 } // namespace views
Bret 2017/03/01 00:29:50 no new test for linking the button sizes?
tapted 2017/03/01 10:44:58 Done.
OLDNEW
« ui/views/window/dialog_client_view.cc ('K') | « ui/views/window/dialog_client_view.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698