Chromium Code Reviews| 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/controls/button/label_button.h" | 5 #include "ui/views/controls/button/label_button.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 "third_party/skia/include/core/SkBitmap.h" | 9 #include "third_party/skia/include/core/SkBitmap.h" |
| 10 #include "ui/accessibility/ax_view_state.h" | 10 #include "ui/accessibility/ax_view_state.h" |
| 11 #include "ui/base/material_design/material_design_controller.h" | 11 #include "ui/base/material_design/material_design_controller.h" |
| 12 #include "ui/base/test/material_design_controller_test_api.h" | 12 #include "ui/base/test/material_design_controller_test_api.h" |
| 13 #include "ui/events/test/event_generator.h" | 13 #include "ui/events/test/event_generator.h" |
| 14 #include "ui/gfx/canvas.h" | 14 #include "ui/gfx/canvas.h" |
| 15 #include "ui/gfx/font_list.h" | 15 #include "ui/gfx/font_list.h" |
| 16 #include "ui/gfx/geometry/size.h" | 16 #include "ui/gfx/geometry/size.h" |
| 17 #include "ui/gfx/geometry/vector2d.h" | 17 #include "ui/gfx/geometry/vector2d.h" |
| 18 #include "ui/gfx/text_utils.h" | 18 #include "ui/gfx/text_utils.h" |
| 19 #include "ui/native_theme/native_theme.h" | |
| 19 #include "ui/views/animation/button_ink_drop_delegate.h" | 20 #include "ui/views/animation/button_ink_drop_delegate.h" |
| 21 #include "ui/views/style/platform_style.h" | |
| 20 #include "ui/views/test/views_test_base.h" | 22 #include "ui/views/test/views_test_base.h" |
| 21 #include "ui/views/test/widget_test.h" | 23 #include "ui/views/test/widget_test.h" |
| 22 | 24 |
| 23 using base::ASCIIToUTF16; | 25 using base::ASCIIToUTF16; |
| 24 | 26 |
| 25 namespace { | 27 namespace { |
| 26 | 28 |
| 27 gfx::ImageSkia CreateTestImage(int width, int height) { | 29 gfx::ImageSkia CreateTestImage(int width, int height) { |
| 28 SkBitmap bitmap; | 30 SkBitmap bitmap; |
| 29 bitmap.allocN32Pixels(width, height); | 31 bitmap.allocN32Pixels(width, height); |
| 30 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap); | 32 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap); |
| 31 } | 33 } |
| 32 | 34 |
| 33 } // namespace | 35 } // namespace |
| 34 | 36 |
| 35 namespace views { | 37 namespace views { |
| 36 | 38 |
| 39 // Testing button that exposes protected methods. | |
| 40 class TestLabelButton : public LabelButton { | |
| 41 public: | |
| 42 TestLabelButton() : LabelButton(nullptr, base::string16()) {} | |
| 43 | |
| 44 using LabelButton::label; | |
| 45 | |
| 46 private: | |
| 47 DISALLOW_COPY_AND_ASSIGN(TestLabelButton); | |
| 48 }; | |
| 49 | |
| 37 class LabelButtonTest : public test::WidgetTest { | 50 class LabelButtonTest : public test::WidgetTest { |
| 38 public: | 51 public: |
| 39 LabelButtonTest() {} | 52 LabelButtonTest() {} |
| 40 | 53 |
| 54 // Adds a LabelButton to the test Widget with the STYLE_BUTTON platform style. | |
| 55 TestLabelButton* AddStyledButton(const char* label, bool is_default) { | |
| 56 TestLabelButton* button = new TestLabelButton; | |
| 57 button->SetText(ASCIIToUTF16(label)); | |
| 58 button->SetStyle(CustomButton::STYLE_BUTTON); | |
| 59 if (is_default) | |
| 60 button->SetIsDefault(true); | |
| 61 button_->GetWidget()->GetContentsView()->AddChildView(button); | |
| 62 button->SizeToPreferredSize(); | |
| 63 button->Layout(); | |
| 64 return button; | |
| 65 } | |
| 66 | |
| 41 // testing::Test: | 67 // testing::Test: |
| 42 void SetUp() override { | 68 void SetUp() override { |
| 43 WidgetTest::SetUp(); | 69 WidgetTest::SetUp(); |
| 44 // Make a Widget to host the button. This ensures appropriate borders are | 70 // Make a Widget to host the button. This ensures appropriate borders are |
| 45 // used (which could be derived from the Widget's NativeTheme). | 71 // used (which could be derived from the Widget's NativeTheme). |
| 46 test_widget_ = CreateTopLevelPlatformWidget(); | 72 test_widget_ = CreateTopLevelPlatformWidget(); |
| 47 | 73 |
| 48 button_ = new LabelButton(nullptr, base::string16()); | 74 button_ = new TestLabelButton; |
| 49 test_widget_->GetContentsView()->AddChildView(button_); | 75 test_widget_->GetContentsView()->AddChildView(button_); |
| 76 | |
| 77 // Establish the expected text colors for testing changes due to state. | |
| 78 themed_normal_text_color_ = button_->GetNativeTheme()->GetSystemColor( | |
| 79 ui::NativeTheme::kColorId_ButtonEnabledColor); | |
| 80 styled_highlight_text_color_ = button_->GetNativeTheme()->GetSystemColor( | |
| 81 ui::NativeTheme::kColorId_ButtonHighlightColor); | |
| 82 #if !(defined(OS_LINUX) && !defined(OS_CHROMEOS)) | |
| 83 // For styled buttons only, platforms other than Desktop Linux ignoree the | |
|
Elly Fong-Jones
2016/04/05 16:43:25
ignore
tapted
2016/04/06 12:08:33
Done. (had to tweak styled_highlight_text_color_ f
| |
| 84 // NativeTheme and just hardcode black. | |
| 85 styled_normal_text_color_ = SK_ColorBLACK; | |
| 86 #else | |
| 87 styled_normal_text_color_ = themed_normal_text_color_; | |
| 88 #endif | |
| 50 } | 89 } |
| 51 | 90 |
| 52 void TearDown() override { | 91 void TearDown() override { |
| 53 test_widget_->CloseNow(); | 92 test_widget_->CloseNow(); |
| 54 WidgetTest::TearDown(); | 93 WidgetTest::TearDown(); |
| 55 } | 94 } |
| 56 | 95 |
| 57 protected: | 96 protected: |
| 58 LabelButton* button_ = nullptr; | 97 TestLabelButton* button_ = nullptr; |
| 98 | |
| 99 SkColor themed_normal_text_color_ = 0; | |
| 100 SkColor styled_normal_text_color_ = 0; | |
| 101 SkColor styled_highlight_text_color_ = 0; | |
| 59 | 102 |
| 60 private: | 103 private: |
| 61 Widget* test_widget_ = nullptr; | 104 Widget* test_widget_ = nullptr; |
| 62 | 105 |
| 63 DISALLOW_COPY_AND_ASSIGN(LabelButtonTest); | 106 DISALLOW_COPY_AND_ASSIGN(LabelButtonTest); |
| 64 }; | 107 }; |
| 65 | 108 |
| 66 TEST_F(LabelButtonTest, Init) { | 109 TEST_F(LabelButtonTest, Init) { |
| 67 const base::string16 text(ASCIIToUTF16("abc")); | 110 const base::string16 text(ASCIIToUTF16("abc")); |
| 68 LabelButton button(NULL, text); | 111 LabelButton button(NULL, text); |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 283 // Increasing the spacing between the text and label should increase the size. | 326 // Increasing the spacing between the text and label should increase the size. |
| 284 button_->SetImageLabelSpacing(2 * kOriginalSpacing); | 327 button_->SetImageLabelSpacing(2 * kOriginalSpacing); |
| 285 EXPECT_GT(button_->GetPreferredSize().width(), original_width); | 328 EXPECT_GT(button_->GetPreferredSize().width(), original_width); |
| 286 | 329 |
| 287 // The button shrinks if the original spacing is restored. | 330 // The button shrinks if the original spacing is restored. |
| 288 button_->SetMinSize(gfx::Size()); | 331 button_->SetMinSize(gfx::Size()); |
| 289 button_->SetImageLabelSpacing(kOriginalSpacing); | 332 button_->SetImageLabelSpacing(kOriginalSpacing); |
| 290 EXPECT_EQ(original_width, button_->GetPreferredSize().width()); | 333 EXPECT_EQ(original_width, button_->GetPreferredSize().width()); |
| 291 } | 334 } |
| 292 | 335 |
| 293 // Make sure the label gets the width it asks for and bolding it (via | 336 // Ensure the label gets the correct style for default buttons (e.g. bolding) |
| 294 // SetDefault) causes the size to update. Regression test for crbug.com/578722 | 337 // and button size updates correctly. Regression test for crbug.com/578722. |
| 295 TEST_F(LabelButtonTest, ButtonStyleIsDefaultSize) { | 338 TEST_F(LabelButtonTest, ButtonStyleIsDefaultStyle) { |
| 296 LabelButton* button = new LabelButton(nullptr, base::ASCIIToUTF16("Save")); | 339 TestLabelButton* button = AddStyledButton("Save", false); |
| 297 button->SetStyle(CustomButton::STYLE_BUTTON); | 340 gfx::Size non_default_size = button->label()->size(); |
| 298 button_->GetWidget()->GetContentsView()->AddChildView(button); | 341 EXPECT_EQ(button->label()->GetPreferredSize().width(), |
| 299 button->SizeToPreferredSize(); | |
| 300 button->Layout(); | |
| 301 gfx::Size non_default_size = button->label_->size(); | |
| 302 EXPECT_EQ(button->label_->GetPreferredSize().width(), | |
| 303 non_default_size.width()); | 342 non_default_size.width()); |
| 343 EXPECT_FALSE(button->label()->font_list().GetFontStyle() & gfx::Font::BOLD); | |
| 344 EXPECT_EQ(styled_normal_text_color_, button->label()->enabled_color()); | |
| 304 button->SetIsDefault(true); | 345 button->SetIsDefault(true); |
| 305 button->SizeToPreferredSize(); | 346 button->SizeToPreferredSize(); |
| 306 button->Layout(); | 347 button->Layout(); |
| 307 EXPECT_NE(non_default_size, button->label_->size()); | 348 EXPECT_EQ(styled_highlight_text_color_, button->label()->enabled_color()); |
| 349 if (PlatformStyle::kDefaultLabelButtonHasBoldFont) { | |
| 350 EXPECT_NE(non_default_size, button->label()->size()); | |
| 351 EXPECT_TRUE(button->label()->font_list().GetFontStyle() & gfx::Font::BOLD); | |
| 352 } else { | |
| 353 EXPECT_EQ(non_default_size, button->label()->size()); | |
| 354 EXPECT_FALSE(button->label()->font_list().GetFontStyle() & gfx::Font::BOLD); | |
| 355 } | |
| 356 } | |
| 357 | |
| 358 // Ensure the label gets the correct style when pressed or becoming default. | |
| 359 TEST_F(LabelButtonTest, HighlightedButtonStyle) { | |
| 360 #if defined(OS_MACOSX) | |
| 361 // On Mac, ensure the normal and highlight colors are different, to ensure the | |
| 362 // tests are actually testing something. This might be the case on other | |
| 363 // platforms. | |
| 364 EXPECT_NE(styled_normal_text_color_, styled_highlight_text_color_); | |
| 365 #endif | |
| 366 | |
| 367 // For STYLE_TEXTBUTTON, the NativeTheme might not provide SK_ColorBLACK, but | |
| 368 // it should be the same for normal and pressed states. | |
| 369 EXPECT_EQ(themed_normal_text_color_, button_->label()->enabled_color()); | |
| 370 button_->SetState(Button::STATE_PRESSED); | |
| 371 EXPECT_EQ(themed_normal_text_color_, button_->label()->enabled_color()); | |
| 372 | |
| 373 // Add a non-default button. | |
| 374 TestLabelButton* styled_button = AddStyledButton("OK", false); | |
| 375 EXPECT_EQ(SK_ColorBLACK, styled_button->label()->enabled_color()); | |
| 376 styled_button->SetState(Button::STATE_PRESSED); | |
| 377 EXPECT_EQ(styled_highlight_text_color_, | |
| 378 styled_button->label()->enabled_color()); | |
| 379 | |
| 380 // If there's an explicit color set for STATE_PRESSED, that should be used. | |
| 381 styled_button->SetEnabledTextColors(SK_ColorRED); | |
| 382 EXPECT_EQ(SK_ColorRED, styled_button->label()->enabled_color()); | |
| 383 | |
| 384 // Test becoming default after adding to the Widget. | |
| 385 TestLabelButton* default_after = AddStyledButton("OK", false); | |
| 386 EXPECT_EQ(styled_normal_text_color_, default_after->label()->enabled_color()); | |
| 387 default_after->SetIsDefault(true); | |
| 388 EXPECT_EQ(styled_highlight_text_color_, | |
| 389 default_after->label()->enabled_color()); | |
| 390 | |
| 391 // Test becoming default before adding to the Widget. | |
| 392 TestLabelButton* default_before = AddStyledButton("OK", true); | |
| 393 EXPECT_EQ(styled_highlight_text_color_, | |
| 394 default_before->label()->enabled_color()); | |
| 308 } | 395 } |
| 309 | 396 |
| 310 // A ButtonInkDropDelegate that tracks the last hover state requested. | 397 // A ButtonInkDropDelegate that tracks the last hover state requested. |
| 311 class TestButtonInkDropDelegate : public ButtonInkDropDelegate { | 398 class TestButtonInkDropDelegate : public ButtonInkDropDelegate { |
| 312 public: | 399 public: |
| 313 TestButtonInkDropDelegate(InkDropHost* ink_drop_host, View* view) | 400 TestButtonInkDropDelegate(InkDropHost* ink_drop_host, View* view) |
| 314 : ButtonInkDropDelegate(ink_drop_host, view), is_hovered_(false) {} | 401 : ButtonInkDropDelegate(ink_drop_host, view), is_hovered_(false) {} |
| 315 | 402 |
| 316 ~TestButtonInkDropDelegate() override {} | 403 ~TestButtonInkDropDelegate() override {} |
| 317 | 404 |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 412 | 499 |
| 413 // Verifies the target event handler View is the |LabelButton| and not any of | 500 // Verifies the target event handler View is the |LabelButton| and not any of |
| 414 // the child Views. | 501 // the child Views. |
| 415 TEST_F(InkDropLabelButtonTest, TargetEventHandler) { | 502 TEST_F(InkDropLabelButtonTest, TargetEventHandler) { |
| 416 View* target_view = widget_->GetRootView()->GetEventHandlerForPoint( | 503 View* target_view = widget_->GetRootView()->GetEventHandlerForPoint( |
| 417 button_->bounds().CenterPoint()); | 504 button_->bounds().CenterPoint()); |
| 418 EXPECT_EQ(button_, target_view); | 505 EXPECT_EQ(button_, target_view); |
| 419 } | 506 } |
| 420 | 507 |
| 421 } // namespace views | 508 } // namespace views |
| OLD | NEW |