| 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/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "third_party/skia/include/core/SkBitmap.h" | 8 #include "third_party/skia/include/core/SkBitmap.h" |
| 9 #include "ui/gfx/canvas.h" | 9 #include "ui/gfx/canvas.h" |
| 10 #include "ui/gfx/font_list.h" | 10 #include "ui/gfx/font_list.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 } | 23 } |
| 24 | 24 |
| 25 } // namespace | 25 } // namespace |
| 26 | 26 |
| 27 namespace views { | 27 namespace views { |
| 28 | 28 |
| 29 typedef ViewsTestBase LabelButtonTest; | 29 typedef ViewsTestBase LabelButtonTest; |
| 30 | 30 |
| 31 TEST_F(LabelButtonTest, Init) { | 31 TEST_F(LabelButtonTest, Init) { |
| 32 const base::string16 text(ASCIIToUTF16("abc")); | 32 const base::string16 text(ASCIIToUTF16("abc")); |
| 33 LabelButton button(NULL, text); | 33 LabelButton button(nullptr); |
| 34 button.Init(text); |
| 34 | 35 |
| 35 EXPECT_TRUE(button.GetImage(Button::STATE_NORMAL).isNull()); | 36 EXPECT_TRUE(button.GetImage(Button::STATE_NORMAL).isNull()); |
| 36 EXPECT_TRUE(button.GetImage(Button::STATE_HOVERED).isNull()); | 37 EXPECT_TRUE(button.GetImage(Button::STATE_HOVERED).isNull()); |
| 37 EXPECT_TRUE(button.GetImage(Button::STATE_PRESSED).isNull()); | 38 EXPECT_TRUE(button.GetImage(Button::STATE_PRESSED).isNull()); |
| 38 EXPECT_TRUE(button.GetImage(Button::STATE_DISABLED).isNull()); | 39 EXPECT_TRUE(button.GetImage(Button::STATE_DISABLED).isNull()); |
| 39 | 40 |
| 40 EXPECT_EQ(text, button.GetText()); | 41 EXPECT_EQ(text, button.GetText()); |
| 41 EXPECT_EQ(gfx::ALIGN_LEFT, button.GetHorizontalAlignment()); | 42 EXPECT_EQ(gfx::ALIGN_LEFT, button.GetHorizontalAlignment()); |
| 42 EXPECT_FALSE(button.is_default()); | 43 EXPECT_FALSE(button.is_default()); |
| 43 EXPECT_EQ(button.style(), Button::STYLE_TEXTBUTTON); | 44 EXPECT_EQ(button.style(), Button::STYLE_TEXTBUTTON); |
| 44 EXPECT_EQ(Button::STATE_NORMAL, button.state()); | 45 EXPECT_EQ(Button::STATE_NORMAL, button.state()); |
| 45 | 46 |
| 46 EXPECT_EQ(button.image_->parent(), &button); | 47 EXPECT_EQ(button.image_->parent(), &button); |
| 47 EXPECT_EQ(button.label_->parent(), &button); | 48 EXPECT_EQ(button.label_->parent(), &button); |
| 48 } | 49 } |
| 49 | 50 |
| 50 TEST_F(LabelButtonTest, Label) { | 51 TEST_F(LabelButtonTest, Label) { |
| 51 LabelButton button(NULL, base::string16()); | 52 LabelButton button(nullptr); |
| 53 EXPECT_TRUE(button.GetText().empty()); |
| 54 button.Init(base::string16()); |
| 52 EXPECT_TRUE(button.GetText().empty()); | 55 EXPECT_TRUE(button.GetText().empty()); |
| 53 | 56 |
| 54 const gfx::FontList font_list; | 57 const gfx::FontList font_list; |
| 55 const base::string16 short_text(ASCIIToUTF16("abcdefghijklm")); | 58 const base::string16 short_text(ASCIIToUTF16("abcdefghijklm")); |
| 56 const base::string16 long_text(ASCIIToUTF16("abcdefghijklmnopqrstuvwxyz")); | 59 const base::string16 long_text(ASCIIToUTF16("abcdefghijklmnopqrstuvwxyz")); |
| 57 const int short_text_width = gfx::GetStringWidth(short_text, font_list); | 60 const int short_text_width = gfx::GetStringWidth(short_text, font_list); |
| 58 const int long_text_width = gfx::GetStringWidth(long_text, font_list); | 61 const int long_text_width = gfx::GetStringWidth(long_text, font_list); |
| 59 | 62 |
| 60 // The width increases monotonically with string size (it does not shrink). | 63 // The width increases monotonically with string size (it does not shrink). |
| 61 EXPECT_LT(button.GetPreferredSize().width(), short_text_width); | 64 EXPECT_LT(button.GetPreferredSize().width(), short_text_width); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 72 button.SetMaxSize(gfx::Size(long_text_width, 1)); | 75 button.SetMaxSize(gfx::Size(long_text_width, 1)); |
| 73 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(long_text_width, 1)); | 76 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(long_text_width, 1)); |
| 74 | 77 |
| 75 // Clear the monotonically increasing minimum size. | 78 // Clear the monotonically increasing minimum size. |
| 76 button.SetMinSize(gfx::Size()); | 79 button.SetMinSize(gfx::Size()); |
| 77 EXPECT_GT(button.GetPreferredSize().width(), short_text_width); | 80 EXPECT_GT(button.GetPreferredSize().width(), short_text_width); |
| 78 EXPECT_LT(button.GetPreferredSize().width(), long_text_width); | 81 EXPECT_LT(button.GetPreferredSize().width(), long_text_width); |
| 79 } | 82 } |
| 80 | 83 |
| 81 TEST_F(LabelButtonTest, Image) { | 84 TEST_F(LabelButtonTest, Image) { |
| 82 LabelButton button(NULL, base::string16()); | 85 LabelButton button(nullptr); |
| 86 button.Init(base::string16()); |
| 83 | 87 |
| 84 const int small_size = 50, large_size = 100; | 88 const int small_size = 50, large_size = 100; |
| 85 const gfx::ImageSkia small_image = CreateTestImage(small_size, small_size); | 89 const gfx::ImageSkia small_image = CreateTestImage(small_size, small_size); |
| 86 const gfx::ImageSkia large_image = CreateTestImage(large_size, large_size); | 90 const gfx::ImageSkia large_image = CreateTestImage(large_size, large_size); |
| 87 | 91 |
| 88 // The width increases monotonically with image size (it does not shrink). | 92 // The width increases monotonically with image size (it does not shrink). |
| 89 EXPECT_LT(button.GetPreferredSize().width(), small_size); | 93 EXPECT_LT(button.GetPreferredSize().width(), small_size); |
| 90 EXPECT_LT(button.GetPreferredSize().height(), small_size); | 94 EXPECT_LT(button.GetPreferredSize().height(), small_size); |
| 91 button.SetImage(Button::STATE_NORMAL, small_image); | 95 button.SetImage(Button::STATE_NORMAL, small_image); |
| 92 EXPECT_GT(button.GetPreferredSize().width(), small_size); | 96 EXPECT_GT(button.GetPreferredSize().width(), small_size); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 104 button.SetMaxSize(gfx::Size(large_size, 1)); | 108 button.SetMaxSize(gfx::Size(large_size, 1)); |
| 105 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(large_size, 1)); | 109 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(large_size, 1)); |
| 106 | 110 |
| 107 // Clear the monotonically increasing minimum size. | 111 // Clear the monotonically increasing minimum size. |
| 108 button.SetMinSize(gfx::Size()); | 112 button.SetMinSize(gfx::Size()); |
| 109 EXPECT_GT(button.GetPreferredSize().width(), small_size); | 113 EXPECT_GT(button.GetPreferredSize().width(), small_size); |
| 110 EXPECT_LT(button.GetPreferredSize().width(), large_size); | 114 EXPECT_LT(button.GetPreferredSize().width(), large_size); |
| 111 } | 115 } |
| 112 | 116 |
| 113 TEST_F(LabelButtonTest, LabelAndImage) { | 117 TEST_F(LabelButtonTest, LabelAndImage) { |
| 114 LabelButton button(NULL, base::string16()); | 118 LabelButton button(nullptr); |
| 119 button.Init(base::string16()); |
| 115 | 120 |
| 116 const gfx::FontList font_list; | 121 const gfx::FontList font_list; |
| 117 const base::string16 text(ASCIIToUTF16("abcdefghijklm")); | 122 const base::string16 text(ASCIIToUTF16("abcdefghijklm")); |
| 118 const int text_width = gfx::GetStringWidth(text, font_list); | 123 const int text_width = gfx::GetStringWidth(text, font_list); |
| 119 | 124 |
| 120 const int image_size = 50; | 125 const int image_size = 50; |
| 121 const gfx::ImageSkia image = CreateTestImage(image_size, image_size); | 126 const gfx::ImageSkia image = CreateTestImage(image_size, image_size); |
| 122 ASSERT_LT(font_list.GetHeight(), image_size); | 127 ASSERT_LT(font_list.GetHeight(), image_size); |
| 123 | 128 |
| 124 // The width increases monotonically with content size (it does not shrink). | 129 // The width increases monotonically with content size (it does not shrink). |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 | 167 |
| 163 // Clear the monotonically increasing minimum size. | 168 // Clear the monotonically increasing minimum size. |
| 164 button.SetMinSize(gfx::Size()); | 169 button.SetMinSize(gfx::Size()); |
| 165 EXPECT_LT(button.GetPreferredSize().width(), text_width); | 170 EXPECT_LT(button.GetPreferredSize().width(), text_width); |
| 166 EXPECT_LT(button.GetPreferredSize().width(), image_size); | 171 EXPECT_LT(button.GetPreferredSize().width(), image_size); |
| 167 EXPECT_LT(button.GetPreferredSize().height(), image_size); | 172 EXPECT_LT(button.GetPreferredSize().height(), image_size); |
| 168 } | 173 } |
| 169 | 174 |
| 170 TEST_F(LabelButtonTest, FontList) { | 175 TEST_F(LabelButtonTest, FontList) { |
| 171 const base::string16 text(ASCIIToUTF16("abc")); | 176 const base::string16 text(ASCIIToUTF16("abc")); |
| 172 LabelButton button(NULL, text); | 177 LabelButton button(nullptr); |
| 178 button.Init(text); |
| 173 | 179 |
| 174 const gfx::FontList original_font_list = button.GetFontList(); | 180 const gfx::FontList original_font_list = button.GetFontList(); |
| 175 const gfx::FontList large_font_list = | 181 const gfx::FontList large_font_list = |
| 176 original_font_list.DeriveWithSizeDelta(100); | 182 original_font_list.DeriveWithSizeDelta(100); |
| 177 const int original_width = button.GetPreferredSize().width(); | 183 const int original_width = button.GetPreferredSize().width(); |
| 178 const int original_height = button.GetPreferredSize().height(); | 184 const int original_height = button.GetPreferredSize().height(); |
| 179 | 185 |
| 180 // The button size increases when the font size is increased. | 186 // The button size increases when the font size is increased. |
| 181 button.SetFontList(large_font_list); | 187 button.SetFontList(large_font_list); |
| 182 EXPECT_GT(button.GetPreferredSize().width(), original_width); | 188 EXPECT_GT(button.GetPreferredSize().width(), original_width); |
| 183 EXPECT_GT(button.GetPreferredSize().height(), original_height); | 189 EXPECT_GT(button.GetPreferredSize().height(), original_height); |
| 184 | 190 |
| 185 // The button returns to its original size when the minimal size is cleared | 191 // The button returns to its original size when the minimal size is cleared |
| 186 // and the original font size is restored. | 192 // and the original font size is restored. |
| 187 button.SetMinSize(gfx::Size()); | 193 button.SetMinSize(gfx::Size()); |
| 188 button.SetFontList(original_font_list); | 194 button.SetFontList(original_font_list); |
| 189 EXPECT_EQ(original_width, button.GetPreferredSize().width()); | 195 EXPECT_EQ(original_width, button.GetPreferredSize().width()); |
| 190 EXPECT_EQ(original_height, button.GetPreferredSize().height()); | 196 EXPECT_EQ(original_height, button.GetPreferredSize().height()); |
| 191 } | 197 } |
| 192 | 198 |
| 193 TEST_F(LabelButtonTest, ChangeTextSize) { | 199 TEST_F(LabelButtonTest, ChangeTextSize) { |
| 194 const base::string16 text(ASCIIToUTF16("abc")); | 200 const base::string16 text(ASCIIToUTF16("abc")); |
| 195 const base::string16 longer_text(ASCIIToUTF16("abcdefghijklm")); | 201 const base::string16 longer_text(ASCIIToUTF16("abcdefghijklm")); |
| 196 LabelButton button(NULL, text); | 202 LabelButton button(nullptr); |
| 203 button.Init(text); |
| 197 | 204 |
| 198 const int original_width = button.GetPreferredSize().width(); | 205 const int original_width = button.GetPreferredSize().width(); |
| 199 | 206 |
| 200 // The button size increases when the text size is increased. | 207 // The button size increases when the text size is increased. |
| 201 button.SetText(longer_text); | 208 button.SetText(longer_text); |
| 202 EXPECT_GT(button.GetPreferredSize().width(), original_width); | 209 EXPECT_GT(button.GetPreferredSize().width(), original_width); |
| 203 | 210 |
| 204 // The button returns to its original size when the original text is restored. | 211 // The button returns to its original size when the original text is restored. |
| 205 button.SetMinSize(gfx::Size()); | 212 button.SetMinSize(gfx::Size()); |
| 206 button.SetText(text); | 213 button.SetText(text); |
| 207 EXPECT_EQ(original_width, button.GetPreferredSize().width()); | 214 EXPECT_EQ(original_width, button.GetPreferredSize().width()); |
| 208 } | 215 } |
| 209 | 216 |
| 210 TEST_F(LabelButtonTest, ChangeLabelImageSpacing) { | 217 TEST_F(LabelButtonTest, ChangeLabelImageSpacing) { |
| 211 LabelButton button(NULL, ASCIIToUTF16("abc")); | 218 LabelButton button(nullptr); |
| 219 button.Init(ASCIIToUTF16("abc")); |
| 212 button.SetImage(Button::STATE_NORMAL, CreateTestImage(50, 50)); | 220 button.SetImage(Button::STATE_NORMAL, CreateTestImage(50, 50)); |
| 213 | 221 |
| 214 const int kOriginalSpacing = 5; | 222 const int kOriginalSpacing = 5; |
| 215 button.SetImageLabelSpacing(kOriginalSpacing); | 223 button.SetImageLabelSpacing(kOriginalSpacing); |
| 216 const int original_width = button.GetPreferredSize().width(); | 224 const int original_width = button.GetPreferredSize().width(); |
| 217 | 225 |
| 218 // Increasing the spacing between the text and label should increase the size. | 226 // Increasing the spacing between the text and label should increase the size. |
| 219 button.SetImageLabelSpacing(2 * kOriginalSpacing); | 227 button.SetImageLabelSpacing(2 * kOriginalSpacing); |
| 220 EXPECT_GT(button.GetPreferredSize().width(), original_width); | 228 EXPECT_GT(button.GetPreferredSize().width(), original_width); |
| 221 | 229 |
| 222 // The button shrinks if the original spacing is restored. | 230 // The button shrinks if the original spacing is restored. |
| 223 button.SetMinSize(gfx::Size()); | 231 button.SetMinSize(gfx::Size()); |
| 224 button.SetImageLabelSpacing(kOriginalSpacing); | 232 button.SetImageLabelSpacing(kOriginalSpacing); |
| 225 EXPECT_EQ(original_width, button.GetPreferredSize().width()); | 233 EXPECT_EQ(original_width, button.GetPreferredSize().width()); |
| 226 } | 234 } |
| 227 | 235 |
| 228 } // namespace views | 236 } // namespace views |
| OLD | NEW |