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" |
11 #include "ui/gfx/geometry/size.h" | 11 #include "ui/gfx/geometry/size.h" |
12 #include "ui/gfx/text_utils.h" | 12 #include "ui/gfx/text_utils.h" |
13 #include "ui/views/test/views_test_base.h" | 13 #include "ui/views/test/widget_test.h" |
14 | 14 |
15 using base::ASCIIToUTF16; | 15 using base::ASCIIToUTF16; |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 gfx::ImageSkia CreateTestImage(int width, int height) { | 19 gfx::ImageSkia CreateTestImage(int width, int height) { |
20 SkBitmap bitmap; | 20 SkBitmap bitmap; |
21 bitmap.allocN32Pixels(width, height); | 21 bitmap.allocN32Pixels(width, height); |
22 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap); | 22 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap); |
23 } | 23 } |
24 | 24 |
25 } // namespace | 25 } // namespace |
26 | 26 |
27 namespace views { | 27 namespace views { |
28 | 28 |
29 typedef ViewsTestBase LabelButtonTest; | 29 class LabelButtonTest : public test::WidgetTest { |
30 public: | |
31 LabelButtonTest() {} | |
32 | |
33 // Make a test button and add it to the Widget to pick up borders (which could | |
34 // be derived from the Widget's NativeTheme). | |
35 LabelButton& MakeTestButton(const base::string16& text) { | |
msw
2015/07/10 18:35:50
hmm, returning a non-const ref is a bit odd, why n
tapted
2015/07/13 07:41:22
Heh, this was really just to make the diff less ch
| |
36 LabelButton* button = new LabelButton(nullptr, text); | |
37 test_widget_->GetContentsView()->AddChildView(button); | |
38 return *button; | |
39 } | |
40 | |
41 // testing::Test: | |
42 void SetUp() override { | |
43 WidgetTest::SetUp(); | |
44 test_widget_ = CreateTopLevelPlatformWidget(); | |
45 } | |
46 | |
47 void TearDown() override { | |
48 test_widget_->CloseNow(); | |
49 WidgetTest::TearDown(); | |
50 } | |
51 | |
52 private: | |
53 Widget* test_widget_ = nullptr; | |
54 | |
55 DISALLOW_COPY_AND_ASSIGN(LabelButtonTest); | |
56 }; | |
30 | 57 |
31 TEST_F(LabelButtonTest, Init) { | 58 TEST_F(LabelButtonTest, Init) { |
32 const base::string16 text(ASCIIToUTF16("abc")); | 59 const base::string16& text = base::ASCIIToUTF16("abc"); |
33 LabelButton button(NULL, text); | 60 LabelButton& button = MakeTestButton(text); |
34 | 61 |
35 EXPECT_TRUE(button.GetImage(Button::STATE_NORMAL).isNull()); | 62 EXPECT_TRUE(button.GetImage(Button::STATE_NORMAL).isNull()); |
36 EXPECT_TRUE(button.GetImage(Button::STATE_HOVERED).isNull()); | 63 EXPECT_TRUE(button.GetImage(Button::STATE_HOVERED).isNull()); |
37 EXPECT_TRUE(button.GetImage(Button::STATE_PRESSED).isNull()); | 64 EXPECT_TRUE(button.GetImage(Button::STATE_PRESSED).isNull()); |
38 EXPECT_TRUE(button.GetImage(Button::STATE_DISABLED).isNull()); | 65 EXPECT_TRUE(button.GetImage(Button::STATE_DISABLED).isNull()); |
39 | 66 |
40 EXPECT_EQ(text, button.GetText()); | 67 EXPECT_EQ(text, button.GetText()); |
41 EXPECT_EQ(gfx::ALIGN_LEFT, button.GetHorizontalAlignment()); | 68 EXPECT_EQ(gfx::ALIGN_LEFT, button.GetHorizontalAlignment()); |
42 EXPECT_FALSE(button.is_default()); | 69 EXPECT_FALSE(button.is_default()); |
43 EXPECT_EQ(button.style(), Button::STYLE_TEXTBUTTON); | 70 EXPECT_EQ(button.style(), Button::STYLE_TEXTBUTTON); |
44 EXPECT_EQ(Button::STATE_NORMAL, button.state()); | 71 EXPECT_EQ(Button::STATE_NORMAL, button.state()); |
45 | 72 |
46 EXPECT_EQ(button.image_->parent(), &button); | 73 EXPECT_EQ(button.image_->parent(), &button); |
47 EXPECT_EQ(button.label_->parent(), &button); | 74 EXPECT_EQ(button.label_->parent(), &button); |
48 } | 75 } |
49 | 76 |
50 TEST_F(LabelButtonTest, Label) { | 77 TEST_F(LabelButtonTest, Label) { |
51 LabelButton button(NULL, base::string16()); | 78 LabelButton& button = MakeTestButton(base::string16()); |
52 EXPECT_TRUE(button.GetText().empty()); | 79 EXPECT_TRUE(button.GetText().empty()); |
53 | 80 |
54 const gfx::FontList font_list; | 81 const gfx::FontList font_list; |
55 const base::string16 short_text(ASCIIToUTF16("abcdefghijklm")); | 82 const base::string16 short_text(ASCIIToUTF16("abcdefghijklm")); |
56 const base::string16 long_text(ASCIIToUTF16("abcdefghijklmnopqrstuvwxyz")); | 83 const base::string16 long_text(ASCIIToUTF16("abcdefghijklmnopqrstuvwxyz")); |
57 const int short_text_width = gfx::GetStringWidth(short_text, font_list); | 84 const int short_text_width = gfx::GetStringWidth(short_text, font_list); |
58 const int long_text_width = gfx::GetStringWidth(long_text, font_list); | 85 const int long_text_width = gfx::GetStringWidth(long_text, font_list); |
59 | 86 |
60 // The width increases monotonically with string size (it does not shrink). | 87 // The width increases monotonically with string size (it does not shrink). |
61 EXPECT_LT(button.GetPreferredSize().width(), short_text_width); | 88 EXPECT_LT(button.GetPreferredSize().width(), short_text_width); |
(...skipping 10 matching lines...) Expand all Loading... | |
72 button.SetMaxSize(gfx::Size(long_text_width, 1)); | 99 button.SetMaxSize(gfx::Size(long_text_width, 1)); |
73 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(long_text_width, 1)); | 100 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(long_text_width, 1)); |
74 | 101 |
75 // Clear the monotonically increasing minimum size. | 102 // Clear the monotonically increasing minimum size. |
76 button.SetMinSize(gfx::Size()); | 103 button.SetMinSize(gfx::Size()); |
77 EXPECT_GT(button.GetPreferredSize().width(), short_text_width); | 104 EXPECT_GT(button.GetPreferredSize().width(), short_text_width); |
78 EXPECT_LT(button.GetPreferredSize().width(), long_text_width); | 105 EXPECT_LT(button.GetPreferredSize().width(), long_text_width); |
79 } | 106 } |
80 | 107 |
81 TEST_F(LabelButtonTest, Image) { | 108 TEST_F(LabelButtonTest, Image) { |
82 LabelButton button(NULL, base::string16()); | 109 LabelButton& button = MakeTestButton(base::string16()); |
83 | 110 |
84 const int small_size = 50, large_size = 100; | 111 const int small_size = 50, large_size = 100; |
85 const gfx::ImageSkia small_image = CreateTestImage(small_size, small_size); | 112 const gfx::ImageSkia small_image = CreateTestImage(small_size, small_size); |
86 const gfx::ImageSkia large_image = CreateTestImage(large_size, large_size); | 113 const gfx::ImageSkia large_image = CreateTestImage(large_size, large_size); |
87 | 114 |
88 // The width increases monotonically with image size (it does not shrink). | 115 // The width increases monotonically with image size (it does not shrink). |
89 EXPECT_LT(button.GetPreferredSize().width(), small_size); | 116 EXPECT_LT(button.GetPreferredSize().width(), small_size); |
90 EXPECT_LT(button.GetPreferredSize().height(), small_size); | 117 EXPECT_LT(button.GetPreferredSize().height(), small_size); |
91 button.SetImage(Button::STATE_NORMAL, small_image); | 118 button.SetImage(Button::STATE_NORMAL, small_image); |
92 EXPECT_GT(button.GetPreferredSize().width(), small_size); | 119 EXPECT_GT(button.GetPreferredSize().width(), small_size); |
(...skipping 11 matching lines...) Expand all Loading... | |
104 button.SetMaxSize(gfx::Size(large_size, 1)); | 131 button.SetMaxSize(gfx::Size(large_size, 1)); |
105 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(large_size, 1)); | 132 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(large_size, 1)); |
106 | 133 |
107 // Clear the monotonically increasing minimum size. | 134 // Clear the monotonically increasing minimum size. |
108 button.SetMinSize(gfx::Size()); | 135 button.SetMinSize(gfx::Size()); |
109 EXPECT_GT(button.GetPreferredSize().width(), small_size); | 136 EXPECT_GT(button.GetPreferredSize().width(), small_size); |
110 EXPECT_LT(button.GetPreferredSize().width(), large_size); | 137 EXPECT_LT(button.GetPreferredSize().width(), large_size); |
111 } | 138 } |
112 | 139 |
113 TEST_F(LabelButtonTest, LabelAndImage) { | 140 TEST_F(LabelButtonTest, LabelAndImage) { |
114 LabelButton button(NULL, base::string16()); | 141 LabelButton& button = MakeTestButton(base::string16()); |
115 | 142 |
116 const gfx::FontList font_list; | 143 const gfx::FontList font_list; |
117 const base::string16 text(ASCIIToUTF16("abcdefghijklm")); | 144 const base::string16 text(ASCIIToUTF16("abcdefghijklm")); |
118 const int text_width = gfx::GetStringWidth(text, font_list); | 145 const int text_width = gfx::GetStringWidth(text, font_list); |
119 | 146 |
120 const int image_size = 50; | 147 const int image_size = 50; |
121 const gfx::ImageSkia image = CreateTestImage(image_size, image_size); | 148 const gfx::ImageSkia image = CreateTestImage(image_size, image_size); |
122 ASSERT_LT(font_list.GetHeight(), image_size); | 149 ASSERT_LT(font_list.GetHeight(), image_size); |
123 | 150 |
124 // The width increases monotonically with content size (it does not shrink). | 151 // The width increases monotonically with content size (it does not shrink). |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
161 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(image_size, 1)); | 188 EXPECT_EQ(button.GetPreferredSize(), gfx::Size(image_size, 1)); |
162 | 189 |
163 // Clear the monotonically increasing minimum size. | 190 // Clear the monotonically increasing minimum size. |
164 button.SetMinSize(gfx::Size()); | 191 button.SetMinSize(gfx::Size()); |
165 EXPECT_LT(button.GetPreferredSize().width(), text_width); | 192 EXPECT_LT(button.GetPreferredSize().width(), text_width); |
166 EXPECT_LT(button.GetPreferredSize().width(), image_size); | 193 EXPECT_LT(button.GetPreferredSize().width(), image_size); |
167 EXPECT_LT(button.GetPreferredSize().height(), image_size); | 194 EXPECT_LT(button.GetPreferredSize().height(), image_size); |
168 } | 195 } |
169 | 196 |
170 TEST_F(LabelButtonTest, FontList) { | 197 TEST_F(LabelButtonTest, FontList) { |
171 const base::string16 text(ASCIIToUTF16("abc")); | 198 LabelButton& button = MakeTestButton(base::ASCIIToUTF16("abc")); |
172 LabelButton button(NULL, text); | |
173 | 199 |
174 const gfx::FontList original_font_list = button.GetFontList(); | 200 const gfx::FontList original_font_list = button.GetFontList(); |
175 const gfx::FontList large_font_list = | 201 const gfx::FontList large_font_list = |
176 original_font_list.DeriveWithSizeDelta(100); | 202 original_font_list.DeriveWithSizeDelta(100); |
177 const int original_width = button.GetPreferredSize().width(); | 203 const int original_width = button.GetPreferredSize().width(); |
178 const int original_height = button.GetPreferredSize().height(); | 204 const int original_height = button.GetPreferredSize().height(); |
179 | 205 |
180 // The button size increases when the font size is increased. | 206 // The button size increases when the font size is increased. |
181 button.SetFontList(large_font_list); | 207 button.SetFontList(large_font_list); |
182 EXPECT_GT(button.GetPreferredSize().width(), original_width); | 208 EXPECT_GT(button.GetPreferredSize().width(), original_width); |
183 EXPECT_GT(button.GetPreferredSize().height(), original_height); | 209 EXPECT_GT(button.GetPreferredSize().height(), original_height); |
184 | 210 |
185 // The button returns to its original size when the minimal size is cleared | 211 // The button returns to its original size when the minimal size is cleared |
186 // and the original font size is restored. | 212 // and the original font size is restored. |
187 button.SetMinSize(gfx::Size()); | 213 button.SetMinSize(gfx::Size()); |
188 button.SetFontList(original_font_list); | 214 button.SetFontList(original_font_list); |
189 EXPECT_EQ(original_width, button.GetPreferredSize().width()); | 215 EXPECT_EQ(original_width, button.GetPreferredSize().width()); |
190 EXPECT_EQ(original_height, button.GetPreferredSize().height()); | 216 EXPECT_EQ(original_height, button.GetPreferredSize().height()); |
191 } | 217 } |
192 | 218 |
193 TEST_F(LabelButtonTest, ChangeTextSize) { | 219 TEST_F(LabelButtonTest, ChangeTextSize) { |
194 const base::string16 text(ASCIIToUTF16("abc")); | 220 const base::string16 text(ASCIIToUTF16("abc")); |
195 const base::string16 longer_text(ASCIIToUTF16("abcdefghijklm")); | 221 const base::string16 longer_text(ASCIIToUTF16("abcdefghijklm")); |
196 LabelButton button(NULL, text); | 222 LabelButton& button = MakeTestButton(text); |
197 | 223 |
198 const int original_width = button.GetPreferredSize().width(); | 224 const int original_width = button.GetPreferredSize().width(); |
199 | 225 |
200 // The button size increases when the text size is increased. | 226 // The button size increases when the text size is increased. |
201 button.SetText(longer_text); | 227 button.SetText(longer_text); |
202 EXPECT_GT(button.GetPreferredSize().width(), original_width); | 228 EXPECT_GT(button.GetPreferredSize().width(), original_width); |
203 | 229 |
204 // The button returns to its original size when the original text is restored. | 230 // The button returns to its original size when the original text is restored. |
205 button.SetMinSize(gfx::Size()); | 231 button.SetMinSize(gfx::Size()); |
206 button.SetText(text); | 232 button.SetText(text); |
207 EXPECT_EQ(original_width, button.GetPreferredSize().width()); | 233 EXPECT_EQ(original_width, button.GetPreferredSize().width()); |
208 } | 234 } |
209 | 235 |
210 TEST_F(LabelButtonTest, ChangeLabelImageSpacing) { | 236 TEST_F(LabelButtonTest, ChangeLabelImageSpacing) { |
211 LabelButton button(NULL, ASCIIToUTF16("abc")); | 237 LabelButton& button = MakeTestButton(base::ASCIIToUTF16("abc")); |
212 button.SetImage(Button::STATE_NORMAL, CreateTestImage(50, 50)); | 238 button.SetImage(Button::STATE_NORMAL, CreateTestImage(50, 50)); |
213 | 239 |
214 const int kOriginalSpacing = 5; | 240 const int kOriginalSpacing = 5; |
215 button.SetImageLabelSpacing(kOriginalSpacing); | 241 button.SetImageLabelSpacing(kOriginalSpacing); |
216 const int original_width = button.GetPreferredSize().width(); | 242 const int original_width = button.GetPreferredSize().width(); |
217 | 243 |
218 // Increasing the spacing between the text and label should increase the size. | 244 // Increasing the spacing between the text and label should increase the size. |
219 button.SetImageLabelSpacing(2 * kOriginalSpacing); | 245 button.SetImageLabelSpacing(2 * kOriginalSpacing); |
220 EXPECT_GT(button.GetPreferredSize().width(), original_width); | 246 EXPECT_GT(button.GetPreferredSize().width(), original_width); |
221 | 247 |
222 // The button shrinks if the original spacing is restored. | 248 // The button shrinks if the original spacing is restored. |
223 button.SetMinSize(gfx::Size()); | 249 button.SetMinSize(gfx::Size()); |
224 button.SetImageLabelSpacing(kOriginalSpacing); | 250 button.SetImageLabelSpacing(kOriginalSpacing); |
225 EXPECT_EQ(original_width, button.GetPreferredSize().width()); | 251 EXPECT_EQ(original_width, button.GetPreferredSize().width()); |
226 } | 252 } |
227 | 253 |
228 } // namespace views | 254 } // namespace views |
OLD | NEW |