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 <map> | |
8 | |
7 #include "base/macros.h" | 9 #include "base/macros.h" |
8 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
9 #include "build/build_config.h" | 11 #include "build/build_config.h" |
10 #include "ui/base/test/material_design_controller_test_api.h" | |
11 #include "ui/base/ui_base_types.h" | 12 #include "ui/base/ui_base_types.h" |
12 #include "ui/views/controls/button/checkbox.h" | 13 #include "ui/views/controls/button/checkbox.h" |
13 #include "ui/views/controls/button/label_button.h" | 14 #include "ui/views/controls/button/label_button.h" |
14 #include "ui/views/style/platform_style.h" | 15 #include "ui/views/style/platform_style.h" |
15 #include "ui/views/test/test_views.h" | 16 #include "ui/views/test/test_views.h" |
16 #include "ui/views/test/widget_test.h" | 17 #include "ui/views/test/widget_test.h" |
17 #include "ui/views/widget/widget.h" | 18 #include "ui/views/widget/widget.h" |
18 #include "ui/views/window/dialog_delegate.h" | 19 #include "ui/views/window/dialog_delegate.h" |
19 | 20 |
20 namespace views { | 21 namespace views { |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
124 } | 125 } |
125 | 126 |
126 View* FocusableViewAfter(View* view) { | 127 View* FocusableViewAfter(View* view) { |
127 const bool dont_loop = false; | 128 const bool dont_loop = false; |
128 const bool reverse = false; | 129 const bool reverse = false; |
129 return GetFocusManager()->GetNextFocusableView(view, GetWidget(), reverse, | 130 return GetFocusManager()->GetNextFocusableView(view, GetWidget(), reverse, |
130 dont_loop); | 131 dont_loop); |
131 } | 132 } |
132 | 133 |
133 // Set a longer than normal Cancel label so that the minimum button width is | 134 // Set a longer than normal Cancel label so that the minimum button width is |
134 // exceeded. | 135 // exceeded. The resulting width is around 160 pixels, but depends on system |
136 // fonts. | |
135 void SetLongCancelLabel() { | 137 void SetLongCancelLabel() { |
136 cancel_label_ = base::ASCIIToUTF16("Cancel Cancel Cancel"); | 138 cancel_label_ = base::ASCIIToUTF16("Cancel Cancel Cancel"); |
137 } | 139 } |
138 | 140 |
139 DialogClientView* client_view() { return client_view_; } | 141 DialogClientView* client_view() { return client_view_; } |
140 | 142 |
141 private: | 143 private: |
142 // The dialog Widget. | 144 // The dialog Widget. |
143 Widget* widget_ = nullptr; | 145 Widget* widget_ = nullptr; |
144 | 146 |
(...skipping 10 matching lines...) Expand all Loading... | |
155 | 157 |
156 gfx::Size preferred_size_; | 158 gfx::Size preferred_size_; |
157 gfx::Size min_size_; | 159 gfx::Size min_size_; |
158 gfx::Size max_size_; | 160 gfx::Size max_size_; |
159 | 161 |
160 base::string16 cancel_label_; // If set, the label for the Cancel button. | 162 base::string16 cancel_label_; // If set, the label for the Cancel button. |
161 | 163 |
162 DISALLOW_COPY_AND_ASSIGN(DialogClientViewTest); | 164 DISALLOW_COPY_AND_ASSIGN(DialogClientViewTest); |
163 }; | 165 }; |
164 | 166 |
167 // Helper to test LayoutProvider metric overrides. | |
168 class TestLayoutProvider : public LayoutProvider { | |
msw
2017/05/05 18:07:43
nit: Can you move this to its own file and use tha
tapted
2017/05/08 06:53:57
Done.
| |
169 public: | |
170 TestLayoutProvider() {} | |
171 | |
172 void Set(int metric, int value) { metrics_[metric] = value; } | |
173 | |
174 // LayoutProvider: | |
175 int GetDistanceMetric(int metric) const override { | |
176 if (metrics_.count(metric)) | |
177 return metrics_.find(metric)->second; | |
178 return LayoutProvider::GetDistanceMetric(metric); | |
179 } | |
180 | |
181 private: | |
182 std::map<int, int> metrics_; | |
183 | |
184 DISALLOW_COPY_AND_ASSIGN(TestLayoutProvider); | |
185 }; | |
186 | |
165 TEST_F(DialogClientViewTest, UpdateButtons) { | 187 TEST_F(DialogClientViewTest, UpdateButtons) { |
166 // This dialog should start with no buttons. | 188 // This dialog should start with no buttons. |
167 EXPECT_EQ(GetDialogButtons(), ui::DIALOG_BUTTON_NONE); | 189 EXPECT_EQ(GetDialogButtons(), ui::DIALOG_BUTTON_NONE); |
168 EXPECT_EQ(NULL, client_view()->ok_button()); | 190 EXPECT_EQ(NULL, client_view()->ok_button()); |
169 EXPECT_EQ(NULL, client_view()->cancel_button()); | 191 EXPECT_EQ(NULL, client_view()->cancel_button()); |
170 const int height_without_buttons = GetUpdatedClientBounds().height(); | 192 const int height_without_buttons = GetUpdatedClientBounds().height(); |
171 | 193 |
172 // Update to use both buttons. | 194 // Update to use both buttons. |
173 SetDialogButtons(ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL); | 195 SetDialogButtons(ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL); |
174 EXPECT_TRUE(client_view()->ok_button()->is_default()); | 196 EXPECT_TRUE(client_view()->ok_button()->is_default()); |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
353 EXPECT_EQ(gfx::Size(400, 450 + buttons_size.height()), | 375 EXPECT_EQ(gfx::Size(400, 450 + buttons_size.height()), |
354 client_view()->GetMinimumSize()); | 376 client_view()->GetMinimumSize()); |
355 EXPECT_EQ(gfx::Size(500, 550 + buttons_size.height()), | 377 EXPECT_EQ(gfx::Size(500, 550 + buttons_size.height()), |
356 client_view()->GetPreferredSize()); | 378 client_view()->GetPreferredSize()); |
357 EXPECT_EQ(gfx::Size(600, 650 + buttons_size.height()), | 379 EXPECT_EQ(gfx::Size(600, 650 + buttons_size.height()), |
358 client_view()->GetMaximumSize()); | 380 client_view()->GetMaximumSize()); |
359 } | 381 } |
360 | 382 |
361 // Ensure button widths are linked under MD. | 383 // Ensure button widths are linked under MD. |
362 TEST_F(DialogClientViewTest, LinkedWidths) { | 384 TEST_F(DialogClientViewTest, LinkedWidths) { |
363 ui::test::MaterialDesignControllerTestAPI md_test_api( | 385 TestLayoutProvider layout_provider; |
364 ui::MaterialDesignController::MATERIAL_NORMAL); | 386 layout_provider.Set(DISTANCE_BUTTON_MAX_LINKABLE_WIDTH, 200); |
365 md_test_api.SetSecondaryUiMaterial(true); | |
366 SetLongCancelLabel(); | 387 SetLongCancelLabel(); |
367 | 388 |
368 SetDialogButtons(ui::DIALOG_BUTTON_OK); | 389 SetDialogButtons(ui::DIALOG_BUTTON_OK); |
369 CheckContentsIsSetToPreferredSize(); | 390 CheckContentsIsSetToPreferredSize(); |
370 const int ok_button_only_width = client_view()->ok_button()->width(); | 391 const int ok_button_only_width = client_view()->ok_button()->width(); |
371 | 392 |
372 SetDialogButtons(ui::DIALOG_BUTTON_CANCEL); | 393 SetDialogButtons(ui::DIALOG_BUTTON_CANCEL); |
373 CheckContentsIsSetToPreferredSize(); | 394 CheckContentsIsSetToPreferredSize(); |
374 const int cancel_button_width = client_view()->cancel_button()->width(); | 395 const int cancel_button_width = client_view()->cancel_button()->width(); |
375 | 396 |
376 // Ensure the single buttons have different preferred widths when alone, and | 397 // Ensure the single buttons have different preferred widths when alone, and |
377 // that the Cancel button is bigger (so that it dominates the size). | 398 // that the Cancel button is bigger (so that it dominates the size). |
378 EXPECT_GT(cancel_button_width, ok_button_only_width); | 399 EXPECT_GT(cancel_button_width, ok_button_only_width); |
379 | 400 |
380 SetDialogButtons(ui::DIALOG_BUTTON_CANCEL | ui::DIALOG_BUTTON_OK); | 401 SetDialogButtons(ui::DIALOG_BUTTON_CANCEL | ui::DIALOG_BUTTON_OK); |
381 CheckContentsIsSetToPreferredSize(); | 402 CheckContentsIsSetToPreferredSize(); |
382 | 403 |
383 // OK button should now match the bigger, cancel button. | 404 // OK button should now match the bigger, cancel button. |
384 EXPECT_EQ(cancel_button_width, client_view()->ok_button()->width()); | 405 EXPECT_EQ(cancel_button_width, client_view()->ok_button()->width()); |
385 | 406 |
386 // But not under non-MD. | 407 // But not when the size of the cancel button exceeds the max linkable width. |
387 md_test_api.SetSecondaryUiMaterial(false); | 408 layout_provider.Set(DISTANCE_BUTTON_MAX_LINKABLE_WIDTH, 100); |
msw
2017/05/05 18:07:43
nit: EXPECT_GT(cancel_button_width, 100)?
tapted
2017/05/08 06:53:57
Done.
| |
388 client_view()->UpdateDialogButtons(); | 409 client_view()->UpdateDialogButtons(); |
389 CheckContentsIsSetToPreferredSize(); | 410 CheckContentsIsSetToPreferredSize(); |
390 EXPECT_EQ(ok_button_only_width, client_view()->ok_button()->width()); | 411 EXPECT_EQ(ok_button_only_width, client_view()->ok_button()->width()); |
391 md_test_api.SetSecondaryUiMaterial(true); | 412 layout_provider.Set(DISTANCE_BUTTON_MAX_LINKABLE_WIDTH, 200); |
392 | 413 |
393 // The extra view should also match, if it's a matching button type. | 414 // The extra view should also match, if it's a matching button type. |
394 LabelButton* extra_button = new LabelButton(nullptr, base::string16()); | 415 LabelButton* extra_button = new LabelButton(nullptr, base::string16()); |
395 SetExtraView(extra_button); | 416 SetExtraView(extra_button); |
396 CheckContentsIsSetToPreferredSize(); | 417 CheckContentsIsSetToPreferredSize(); |
397 EXPECT_EQ(cancel_button_width, extra_button->width()); | 418 EXPECT_EQ(cancel_button_width, extra_button->width()); |
398 | 419 |
399 // Remove |extra_button| from the View hierarchy so that it can be replaced. | 420 // Remove |extra_button| from the View hierarchy so that it can be replaced. |
400 delete extra_button; | 421 delete extra_button; |
401 | 422 |
402 // Checkbox extends LabelButton, but it should not participate in linking. | 423 // Checkbox extends LabelButton, but it should not participate in linking. |
403 extra_button = new Checkbox(base::string16()); | 424 extra_button = new Checkbox(base::string16()); |
404 SetExtraView(extra_button); | 425 SetExtraView(extra_button); |
405 CheckContentsIsSetToPreferredSize(); | 426 CheckContentsIsSetToPreferredSize(); |
406 EXPECT_NE(cancel_button_width, extra_button->width()); | 427 EXPECT_NE(cancel_button_width, extra_button->width()); |
407 | 428 |
408 // Remove |extra_button| from the View hierarchy so that it can be replaced. | 429 // Remove |extra_button| from the View hierarchy so that it can be replaced. |
409 delete extra_button; | 430 delete extra_button; |
410 | 431 |
411 // Non-buttons should always be sized to their preferred size. | 432 // Non-buttons should always be sized to their preferred size. |
412 View* boring_view = new StaticSizedView(gfx::Size(20, 20)); | 433 View* boring_view = new StaticSizedView(gfx::Size(20, 20)); |
413 SetExtraView(boring_view); | 434 SetExtraView(boring_view); |
414 CheckContentsIsSetToPreferredSize(); | 435 CheckContentsIsSetToPreferredSize(); |
415 EXPECT_EQ(20, boring_view->width()); | 436 EXPECT_EQ(20, boring_view->width()); |
416 } | 437 } |
417 | 438 |
418 } // namespace views | 439 } // namespace views |
OLD | NEW |