| 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" |
| 16 #include "ui/views/test/test_layout_provider.h" |
| 15 #include "ui/views/test/test_views.h" | 17 #include "ui/views/test/test_views.h" |
| 16 #include "ui/views/test/widget_test.h" | 18 #include "ui/views/test/widget_test.h" |
| 17 #include "ui/views/widget/widget.h" | 19 #include "ui/views/widget/widget.h" |
| 18 #include "ui/views/window/dialog_delegate.h" | 20 #include "ui/views/window/dialog_delegate.h" |
| 19 | 21 |
| 20 namespace views { | 22 namespace views { |
| 21 | 23 |
| 22 // Base class for tests. Also acts as the dialog delegate and contents view for | 24 // Base class for tests. Also acts as the dialog delegate and contents view for |
| 23 // TestDialogClientView. | 25 // TestDialogClientView. |
| 24 class DialogClientViewTest : public test::WidgetTest, | 26 class DialogClientViewTest : public test::WidgetTest, |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 } | 126 } |
| 125 | 127 |
| 126 View* FocusableViewAfter(View* view) { | 128 View* FocusableViewAfter(View* view) { |
| 127 const bool dont_loop = false; | 129 const bool dont_loop = false; |
| 128 const bool reverse = false; | 130 const bool reverse = false; |
| 129 return GetFocusManager()->GetNextFocusableView(view, GetWidget(), reverse, | 131 return GetFocusManager()->GetNextFocusableView(view, GetWidget(), reverse, |
| 130 dont_loop); | 132 dont_loop); |
| 131 } | 133 } |
| 132 | 134 |
| 133 // Set a longer than normal Cancel label so that the minimum button width is | 135 // Set a longer than normal Cancel label so that the minimum button width is |
| 134 // exceeded. | 136 // exceeded. The resulting width is around 160 pixels, but depends on system |
| 137 // fonts. |
| 135 void SetLongCancelLabel() { | 138 void SetLongCancelLabel() { |
| 136 cancel_label_ = base::ASCIIToUTF16("Cancel Cancel Cancel"); | 139 cancel_label_ = base::ASCIIToUTF16("Cancel Cancel Cancel"); |
| 137 } | 140 } |
| 138 | 141 |
| 139 DialogClientView* client_view() { return client_view_; } | 142 DialogClientView* client_view() { return client_view_; } |
| 140 | 143 |
| 141 private: | 144 private: |
| 142 // The dialog Widget. | 145 // The dialog Widget. |
| 143 Widget* widget_ = nullptr; | 146 Widget* widget_ = nullptr; |
| 144 | 147 |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 EXPECT_EQ(gfx::Size(400, 450 + buttons_size.height()), | 356 EXPECT_EQ(gfx::Size(400, 450 + buttons_size.height()), |
| 354 client_view()->GetMinimumSize()); | 357 client_view()->GetMinimumSize()); |
| 355 EXPECT_EQ(gfx::Size(500, 550 + buttons_size.height()), | 358 EXPECT_EQ(gfx::Size(500, 550 + buttons_size.height()), |
| 356 client_view()->GetPreferredSize()); | 359 client_view()->GetPreferredSize()); |
| 357 EXPECT_EQ(gfx::Size(600, 650 + buttons_size.height()), | 360 EXPECT_EQ(gfx::Size(600, 650 + buttons_size.height()), |
| 358 client_view()->GetMaximumSize()); | 361 client_view()->GetMaximumSize()); |
| 359 } | 362 } |
| 360 | 363 |
| 361 // Ensure button widths are linked under MD. | 364 // Ensure button widths are linked under MD. |
| 362 TEST_F(DialogClientViewTest, LinkedWidths) { | 365 TEST_F(DialogClientViewTest, LinkedWidths) { |
| 363 ui::test::MaterialDesignControllerTestAPI md_test_api( | 366 test::TestLayoutProvider layout_provider; |
| 364 ui::MaterialDesignController::MATERIAL_NORMAL); | 367 layout_provider.SetDistanceMetric(DISTANCE_BUTTON_MAX_LINKABLE_WIDTH, 200); |
| 365 md_test_api.SetSecondaryUiMaterial(true); | |
| 366 SetLongCancelLabel(); | 368 SetLongCancelLabel(); |
| 367 | 369 |
| 368 SetDialogButtons(ui::DIALOG_BUTTON_OK); | 370 SetDialogButtons(ui::DIALOG_BUTTON_OK); |
| 369 CheckContentsIsSetToPreferredSize(); | 371 CheckContentsIsSetToPreferredSize(); |
| 370 const int ok_button_only_width = client_view()->ok_button()->width(); | 372 const int ok_button_only_width = client_view()->ok_button()->width(); |
| 371 | 373 |
| 372 SetDialogButtons(ui::DIALOG_BUTTON_CANCEL); | 374 SetDialogButtons(ui::DIALOG_BUTTON_CANCEL); |
| 373 CheckContentsIsSetToPreferredSize(); | 375 CheckContentsIsSetToPreferredSize(); |
| 374 const int cancel_button_width = client_view()->cancel_button()->width(); | 376 const int cancel_button_width = client_view()->cancel_button()->width(); |
| 377 EXPECT_LT(cancel_button_width, 200); |
| 375 | 378 |
| 376 // Ensure the single buttons have different preferred widths when alone, and | 379 // Ensure the single buttons have different preferred widths when alone, and |
| 377 // that the Cancel button is bigger (so that it dominates the size). | 380 // that the Cancel button is bigger (so that it dominates the size). |
| 378 EXPECT_GT(cancel_button_width, ok_button_only_width); | 381 EXPECT_GT(cancel_button_width, ok_button_only_width); |
| 379 | 382 |
| 380 SetDialogButtons(ui::DIALOG_BUTTON_CANCEL | ui::DIALOG_BUTTON_OK); | 383 SetDialogButtons(ui::DIALOG_BUTTON_CANCEL | ui::DIALOG_BUTTON_OK); |
| 381 CheckContentsIsSetToPreferredSize(); | 384 CheckContentsIsSetToPreferredSize(); |
| 382 | 385 |
| 383 // OK button should now match the bigger, cancel button. | 386 // OK button should now match the bigger, cancel button. |
| 384 EXPECT_EQ(cancel_button_width, client_view()->ok_button()->width()); | 387 EXPECT_EQ(cancel_button_width, client_view()->ok_button()->width()); |
| 385 | 388 |
| 386 // But not under non-MD. | 389 // But not when the size of the cancel button exceeds the max linkable width. |
| 387 md_test_api.SetSecondaryUiMaterial(false); | 390 layout_provider.SetDistanceMetric(DISTANCE_BUTTON_MAX_LINKABLE_WIDTH, 100); |
| 391 EXPECT_GT(cancel_button_width, 100); |
| 392 |
| 388 client_view()->UpdateDialogButtons(); | 393 client_view()->UpdateDialogButtons(); |
| 389 CheckContentsIsSetToPreferredSize(); | 394 CheckContentsIsSetToPreferredSize(); |
| 390 EXPECT_EQ(ok_button_only_width, client_view()->ok_button()->width()); | 395 EXPECT_EQ(ok_button_only_width, client_view()->ok_button()->width()); |
| 391 md_test_api.SetSecondaryUiMaterial(true); | 396 layout_provider.SetDistanceMetric(DISTANCE_BUTTON_MAX_LINKABLE_WIDTH, 200); |
| 392 | 397 |
| 393 // The extra view should also match, if it's a matching button type. | 398 // The extra view should also match, if it's a matching button type. |
| 394 LabelButton* extra_button = new LabelButton(nullptr, base::string16()); | 399 LabelButton* extra_button = new LabelButton(nullptr, base::string16()); |
| 395 SetExtraView(extra_button); | 400 SetExtraView(extra_button); |
| 396 CheckContentsIsSetToPreferredSize(); | 401 CheckContentsIsSetToPreferredSize(); |
| 397 EXPECT_EQ(cancel_button_width, extra_button->width()); | 402 EXPECT_EQ(cancel_button_width, extra_button->width()); |
| 398 | 403 |
| 399 // Remove |extra_button| from the View hierarchy so that it can be replaced. | 404 // Remove |extra_button| from the View hierarchy so that it can be replaced. |
| 400 delete extra_button; | 405 delete extra_button; |
| 401 | 406 |
| 402 // Checkbox extends LabelButton, but it should not participate in linking. | 407 // Checkbox extends LabelButton, but it should not participate in linking. |
| 403 extra_button = new Checkbox(base::string16()); | 408 extra_button = new Checkbox(base::string16()); |
| 404 SetExtraView(extra_button); | 409 SetExtraView(extra_button); |
| 405 CheckContentsIsSetToPreferredSize(); | 410 CheckContentsIsSetToPreferredSize(); |
| 406 EXPECT_NE(cancel_button_width, extra_button->width()); | 411 EXPECT_NE(cancel_button_width, extra_button->width()); |
| 407 | 412 |
| 408 // Remove |extra_button| from the View hierarchy so that it can be replaced. | 413 // Remove |extra_button| from the View hierarchy so that it can be replaced. |
| 409 delete extra_button; | 414 delete extra_button; |
| 410 | 415 |
| 411 // Non-buttons should always be sized to their preferred size. | 416 // Non-buttons should always be sized to their preferred size. |
| 412 View* boring_view = new StaticSizedView(gfx::Size(20, 20)); | 417 View* boring_view = new StaticSizedView(gfx::Size(20, 20)); |
| 413 SetExtraView(boring_view); | 418 SetExtraView(boring_view); |
| 414 CheckContentsIsSetToPreferredSize(); | 419 CheckContentsIsSetToPreferredSize(); |
| 415 EXPECT_EQ(20, boring_view->width()); | 420 EXPECT_EQ(20, boring_view->width()); |
| 416 } | 421 } |
| 417 | 422 |
| 418 } // namespace views | 423 } // namespace views |
| OLD | NEW |