| 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/command_line.h" |    7 #include "base/command_line.h" | 
|    8 #include "base/macros.h" |    8 #include "base/macros.h" | 
|    9 #include "base/memory/ptr_util.h" |    9 #include "base/memory/ptr_util.h" | 
|   10 #include "base/strings/utf_string_conversions.h" |   10 #include "base/strings/utf_string_conversions.h" | 
| (...skipping 26 matching lines...) Expand all  Loading... | 
|   37   return gfx::ImageSkia::CreateFrom1xBitmap(bitmap); |   37   return gfx::ImageSkia::CreateFrom1xBitmap(bitmap); | 
|   38 } |   38 } | 
|   39  |   39  | 
|   40 }  // namespace |   40 }  // namespace | 
|   41  |   41  | 
|   42 namespace views { |   42 namespace views { | 
|   43  |   43  | 
|   44 // Testing button that exposes protected methods. |   44 // Testing button that exposes protected methods. | 
|   45 class TestLabelButton : public LabelButton { |   45 class TestLabelButton : public LabelButton { | 
|   46  public: |   46  public: | 
|   47   explicit TestLabelButton(const base::string16& text = base::string16()) |   47   explicit TestLabelButton(const base::string16& text = base::string16(), | 
|   48       : LabelButton(nullptr, text) {} |   48                            int button_context = style::CONTEXT_BUTTON) | 
 |   49       : LabelButton(nullptr, text, button_context) {} | 
|   49  |   50  | 
|   50   using LabelButton::label; |   51   using LabelButton::label; | 
|   51   using LabelButton::image; |   52   using LabelButton::image; | 
|   52   using LabelButton::ResetColorsFromNativeTheme; |   53   using LabelButton::ResetColorsFromNativeTheme; | 
|   53  |   54  | 
|   54  private: |   55  private: | 
|   55   DISALLOW_COPY_AND_ASSIGN(TestLabelButton); |   56   DISALLOW_COPY_AND_ASSIGN(TestLabelButton); | 
|   56 }; |   57 }; | 
|   57  |   58  | 
|   58 class LabelButtonTest : public test::WidgetTest { |   59 class LabelButtonTest : public test::WidgetTest { | 
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  141   EXPECT_EQ(button.style(), Button::STYLE_TEXTBUTTON); |  142   EXPECT_EQ(button.style(), Button::STYLE_TEXTBUTTON); | 
|  142   EXPECT_EQ(Button::STATE_NORMAL, button.state()); |  143   EXPECT_EQ(Button::STATE_NORMAL, button.state()); | 
|  143  |  144  | 
|  144   EXPECT_EQ(button.image()->parent(), &button); |  145   EXPECT_EQ(button.image()->parent(), &button); | 
|  145   EXPECT_EQ(button.label()->parent(), &button); |  146   EXPECT_EQ(button.label()->parent(), &button); | 
|  146 } |  147 } | 
|  147  |  148  | 
|  148 TEST_F(LabelButtonTest, Label) { |  149 TEST_F(LabelButtonTest, Label) { | 
|  149   EXPECT_TRUE(button_->GetText().empty()); |  150   EXPECT_TRUE(button_->GetText().empty()); | 
|  150  |  151  | 
|  151   const gfx::FontList font_list; |  152   const gfx::FontList font_list = button_->label()->font_list(); | 
|  152   const base::string16 short_text(ASCIIToUTF16("abcdefghijklm")); |  153   const base::string16 short_text(ASCIIToUTF16("abcdefghijklm")); | 
|  153   const base::string16 long_text(ASCIIToUTF16("abcdefghijklmnopqrstuvwxyz")); |  154   const base::string16 long_text(ASCIIToUTF16("abcdefghijklmnopqrstuvwxyz")); | 
|  154   const int short_text_width = gfx::GetStringWidth(short_text, font_list); |  155   const int short_text_width = gfx::GetStringWidth(short_text, font_list); | 
|  155   const int long_text_width = gfx::GetStringWidth(long_text, font_list); |  156   const int long_text_width = gfx::GetStringWidth(long_text, font_list); | 
|  156  |  157  | 
|  157   // The width increases monotonically with string size (it does not shrink). |  158   // The width increases monotonically with string size (it does not shrink). | 
|  158   EXPECT_LT(button_->GetPreferredSize().width(), short_text_width); |  159   EXPECT_LT(button_->GetPreferredSize().width(), short_text_width); | 
|  159   button_->SetText(short_text); |  160   button_->SetText(short_text); | 
|  160   EXPECT_GT(button_->GetPreferredSize().height(), font_list.GetHeight()); |  161   EXPECT_GT(button_->GetPreferredSize().height(), font_list.GetHeight()); | 
|  161   EXPECT_GT(button_->GetPreferredSize().width(), short_text_width); |  162   EXPECT_GT(button_->GetPreferredSize().width(), short_text_width); | 
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  231   button_->SetMaxSize(gfx::Size(large_size, 1)); |  232   button_->SetMaxSize(gfx::Size(large_size, 1)); | 
|  232   EXPECT_EQ(button_->GetPreferredSize(), gfx::Size(large_size, 1)); |  233   EXPECT_EQ(button_->GetPreferredSize(), gfx::Size(large_size, 1)); | 
|  233  |  234  | 
|  234   // Clear the monotonically increasing minimum size. |  235   // Clear the monotonically increasing minimum size. | 
|  235   button_->SetMinSize(gfx::Size()); |  236   button_->SetMinSize(gfx::Size()); | 
|  236   EXPECT_GT(button_->GetPreferredSize().width(), small_size); |  237   EXPECT_GT(button_->GetPreferredSize().width(), small_size); | 
|  237   EXPECT_LT(button_->GetPreferredSize().width(), large_size); |  238   EXPECT_LT(button_->GetPreferredSize().width(), large_size); | 
|  238 } |  239 } | 
|  239  |  240  | 
|  240 TEST_F(LabelButtonTest, LabelAndImage) { |  241 TEST_F(LabelButtonTest, LabelAndImage) { | 
|  241   const gfx::FontList font_list; |  242   const gfx::FontList font_list = button_->label()->font_list(); | 
|  242   const base::string16 text(ASCIIToUTF16("abcdefghijklm")); |  243   const base::string16 text(ASCIIToUTF16("abcdefghijklm")); | 
|  243   const int text_width = gfx::GetStringWidth(text, font_list); |  244   const int text_width = gfx::GetStringWidth(text, font_list); | 
|  244  |  245  | 
|  245   const int image_size = 50; |  246   const int image_size = 50; | 
|  246   const gfx::ImageSkia image = CreateTestImage(image_size, image_size); |  247   const gfx::ImageSkia image = CreateTestImage(image_size, image_size); | 
|  247   ASSERT_LT(font_list.GetHeight(), image_size); |  248   ASSERT_LT(font_list.GetHeight(), image_size); | 
|  248  |  249  | 
|  249   // The width increases monotonically with content size (it does not shrink). |  250   // The width increases monotonically with content size (it does not shrink). | 
|  250   EXPECT_LT(button_->GetPreferredSize().width(), text_width); |  251   EXPECT_LT(button_->GetPreferredSize().width(), text_width); | 
|  251   EXPECT_LT(button_->GetPreferredSize().width(), image_size); |  252   EXPECT_LT(button_->GetPreferredSize().width(), image_size); | 
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  289   button_->SetMaxSize(gfx::Size(image_size, 1)); |  290   button_->SetMaxSize(gfx::Size(image_size, 1)); | 
|  290   EXPECT_EQ(button_->GetPreferredSize(), gfx::Size(image_size, 1)); |  291   EXPECT_EQ(button_->GetPreferredSize(), gfx::Size(image_size, 1)); | 
|  291  |  292  | 
|  292   // Clear the monotonically increasing minimum size. |  293   // Clear the monotonically increasing minimum size. | 
|  293   button_->SetMinSize(gfx::Size()); |  294   button_->SetMinSize(gfx::Size()); | 
|  294   EXPECT_LT(button_->GetPreferredSize().width(), text_width); |  295   EXPECT_LT(button_->GetPreferredSize().width(), text_width); | 
|  295   EXPECT_LT(button_->GetPreferredSize().width(), image_size); |  296   EXPECT_LT(button_->GetPreferredSize().width(), image_size); | 
|  296   EXPECT_LT(button_->GetPreferredSize().height(), image_size); |  297   EXPECT_LT(button_->GetPreferredSize().height(), image_size); | 
|  297 } |  298 } | 
|  298  |  299  | 
|  299 TEST_F(LabelButtonTest, AdjustFontSize) { |  300 // Ensure that the text used for button labels correctly adjusts in response | 
|  300   button_->SetText(base::ASCIIToUTF16("abc")); |  301 // to provided style::TextContext values. | 
 |  302 TEST_F(LabelButtonTest, TextSizeFromContext) { | 
 |  303   constexpr style::TextContext kDefaultContext = style::CONTEXT_BUTTON; | 
|  301  |  304  | 
|  302   const int original_width = button_->GetPreferredSize().width(); |  305   // Although CONTEXT_DIALOG_TITLE isn't used for buttons, picking a style with | 
|  303   const int original_height = button_->GetPreferredSize().height(); |  306   // a small delta risks finding a font with a different point-size but with the | 
 |  307   // same maximum glyph height. | 
 |  308   constexpr style::TextContext kAlternateContext = style::CONTEXT_DIALOG_TITLE; | 
 |  309  | 
 |  310   // First sanity that the TextConstants used in the test give different sizes. | 
 |  311   int default_delta, alternate_delta; | 
 |  312   gfx::Font::Weight default_weight, alternate_weight; | 
 |  313   DefaultTypographyProvider::GetDefaultFont( | 
 |  314       kDefaultContext, style::STYLE_PRIMARY, &default_delta, &default_weight); | 
 |  315   DefaultTypographyProvider::GetDefaultFont( | 
 |  316       kAlternateContext, style::STYLE_PRIMARY, &alternate_delta, | 
 |  317       &alternate_weight); | 
 |  318   EXPECT_LT(default_delta, alternate_delta); | 
 |  319  | 
 |  320   const base::string16 text(ASCIIToUTF16("abcdefghijklm")); | 
 |  321   button_->SetText(text); | 
 |  322   EXPECT_EQ(default_delta, button_->label()->font_list().GetFontSize() - | 
 |  323                                gfx::FontList().GetFontSize()); | 
 |  324  | 
 |  325   TestLabelButton* alternate_button = | 
 |  326       new TestLabelButton(text, kAlternateContext); | 
 |  327   button_->parent()->AddChildView(alternate_button); | 
 |  328   EXPECT_EQ(alternate_delta, | 
 |  329             alternate_button->label()->font_list().GetFontSize() - | 
 |  330                 gfx::FontList().GetFontSize()); | 
|  304  |  331  | 
|  305   // The button size increases when the font size is increased. |  332   // The button size increases when the font size is increased. | 
|  306   button_->AdjustFontSize(100); |  333   EXPECT_LT(button_->GetPreferredSize().width(), | 
|  307   EXPECT_GT(button_->GetPreferredSize().width(), original_width); |  334             alternate_button->GetPreferredSize().width()); | 
|  308   EXPECT_GT(button_->GetPreferredSize().height(), original_height); |  335   EXPECT_LT(button_->GetPreferredSize().height(), | 
|  309  |  336             alternate_button->GetPreferredSize().height()); | 
|  310   // The button returns to its original size when the minimal size is cleared |  | 
|  311   // and the original font size is restored. |  | 
|  312   button_->SetMinSize(gfx::Size()); |  | 
|  313   button_->AdjustFontSize(-100); |  | 
|  314   EXPECT_EQ(original_width, button_->GetPreferredSize().width()); |  | 
|  315   EXPECT_EQ(original_height, button_->GetPreferredSize().height()); |  | 
|  316 } |  337 } | 
|  317  |  338  | 
|  318 TEST_F(LabelButtonTest, ChangeTextSize) { |  339 TEST_F(LabelButtonTest, ChangeTextSize) { | 
|  319   const base::string16 text(ASCIIToUTF16("abc")); |  340   const base::string16 text(ASCIIToUTF16("abc")); | 
|  320   const base::string16 longer_text(ASCIIToUTF16("abcdefghijklm")); |  341   const base::string16 longer_text(ASCIIToUTF16("abcdefghijklm")); | 
|  321   button_->SetText(text); |  342   button_->SetText(text); | 
|  322   button_->SizeToPreferredSize(); |  343   button_->SizeToPreferredSize(); | 
|  323   gfx::Rect bounds(button_->bounds()); |  344   gfx::Rect bounds(button_->bounds()); | 
|  324   const int original_width = button_->GetPreferredSize().width(); |  345   const int original_width = button_->GetPreferredSize().width(); | 
|  325   EXPECT_EQ(original_width, bounds.width()); |  346   EXPECT_EQ(original_width, bounds.width()); | 
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  509  |  530  | 
|  510 // Verifies the target event handler View is the |LabelButton| and not any of |  531 // Verifies the target event handler View is the |LabelButton| and not any of | 
|  511 // the child Views. |  532 // the child Views. | 
|  512 TEST_F(InkDropLabelButtonTest, TargetEventHandler) { |  533 TEST_F(InkDropLabelButtonTest, TargetEventHandler) { | 
|  513   View* target_view = widget_->GetRootView()->GetEventHandlerForPoint( |  534   View* target_view = widget_->GetRootView()->GetEventHandlerForPoint( | 
|  514       button_->bounds().CenterPoint()); |  535       button_->bounds().CenterPoint()); | 
|  515   EXPECT_EQ(button_, target_view); |  536   EXPECT_EQ(button_, target_view); | 
|  516 } |  537 } | 
|  517  |  538  | 
|  518 }  // namespace views |  539 }  // namespace views | 
| OLD | NEW |