Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(150)

Side by Side Diff: ui/views/controls/styled_label_unittest.cc

Issue 12906002: Add ability to defined ranges with different styles in StyledLabel (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 <string> 5 #include <string>
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 42
43 TEST_F(StyledLabelTest, NoWrapping) { 43 TEST_F(StyledLabelTest, NoWrapping) {
44 const std::string text("This is a test block of text"); 44 const std::string text("This is a test block of text");
45 InitStyledLabel(text); 45 InitStyledLabel(text);
46 Label label(ASCIIToUTF16(text)); 46 Label label(ASCIIToUTF16(text));
47 const gfx::Size label_preferred_size = label.GetPreferredSize(); 47 const gfx::Size label_preferred_size = label.GetPreferredSize();
48 EXPECT_EQ(label_preferred_size.height(), 48 EXPECT_EQ(label_preferred_size.height(),
49 StyledLabelContentHeightForWidth(label_preferred_size.width() * 2)); 49 StyledLabelContentHeightForWidth(label_preferred_size.width() * 2));
50 } 50 }
51 51
52 TEST_F(StyledLabelTest, TrailingWhitespaceiIgnored) {
53 const std::string text("This is a test block of text ");
54 InitStyledLabel(text);
55
56 styled()->SetBounds(0, 0, 1000, 1000);
57 styled()->Layout();
58
59 ASSERT_EQ(Label::kViewClassName, styled()->child_at(0)->GetClassName());
sky 2013/03/28 16:02:09 Don't you need an assert on on child_count() (same
tbarzic 2013/03/28 20:33:58 yeah, I forgot to add them in these two.
60 EXPECT_EQ(ASCIIToUTF16("This is a test block of text"),
61 static_cast<Label*>(styled()->child_at(0))->text());
62 }
63
64 TEST_F(StyledLabelTest, LeadingWhitespaceiIgnored) {
65 const std::string text(" This is a test block of text");
66 InitStyledLabel(text);
67
68 styled()->SetBounds(0, 0, 1000, 1000);
69 styled()->Layout();
70
71 ASSERT_EQ(Label::kViewClassName, styled()->child_at(0)->GetClassName());
72 EXPECT_EQ(ASCIIToUTF16("This is a test block of text"),
73 static_cast<Label*>(styled()->child_at(0))->text());
74 }
75
52 TEST_F(StyledLabelTest, BasicWrapping) { 76 TEST_F(StyledLabelTest, BasicWrapping) {
53 const std::string text("This is a test block of text"); 77 const std::string text("This is a test block of text");
54 InitStyledLabel(text); 78 InitStyledLabel(text);
55 Label label(ASCIIToUTF16(text.substr(0, text.size() * 2 / 3))); 79 Label label(ASCIIToUTF16(text.substr(0, text.size() * 2 / 3)));
56 gfx::Size label_preferred_size = label.GetPreferredSize(); 80 gfx::Size label_preferred_size = label.GetPreferredSize();
57 EXPECT_EQ(label_preferred_size.height() * 2, 81 EXPECT_EQ(label_preferred_size.height() * 2,
58 StyledLabelContentHeightForWidth(label_preferred_size.width())); 82 StyledLabelContentHeightForWidth(label_preferred_size.width()));
59 83
60 // Also respect the border. 84 // Also respect the border.
61 styled()->set_border(Border::CreateEmptyBorder(3, 3, 3, 3)); 85 styled()->set_border(Border::CreateEmptyBorder(3, 3, 3, 3));
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 EXPECT_EQ(label_preferred_size.height() * 2, 121 EXPECT_EQ(label_preferred_size.height() * 2,
98 pref_height - styled()->GetInsets().height()); 122 pref_height - styled()->GetInsets().height());
99 123
100 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height); 124 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height);
101 styled()->Layout(); 125 styled()->Layout();
102 ASSERT_EQ(2, styled()->child_count()); 126 ASSERT_EQ(2, styled()->child_count());
103 EXPECT_EQ(0, styled()->child_at(0)->bounds().x()); 127 EXPECT_EQ(0, styled()->child_at(0)->bounds().x());
104 EXPECT_EQ(0, styled()->child_at(1)->bounds().x()); 128 EXPECT_EQ(0, styled()->child_at(1)->bounds().x());
105 } 129 }
106 130
131 TEST_F(StyledLabelTest, StyledRangeWithDisabledLineWrapping) {
132 const std::string text("This is a test block of text, ");
133 const std::string unbreakable_text("and this should not be breaked");
134 InitStyledLabel(text + unbreakable_text);
135 StyledLabel::RangeStyleInfo style_info;
136 style_info.disable_line_wrapping = true;
137 styled()->AddStyleRange(
138 ui::Range(text.size(), text.size() + unbreakable_text.size()),
139 style_info);
140
141 Label label(ASCIIToUTF16(
142 text + unbreakable_text.substr(0, unbreakable_text.size() / 2)));
143 gfx::Size label_preferred_size = label.GetPreferredSize();
144 int pref_height = styled()->GetHeightForWidth(label_preferred_size.width());
145 EXPECT_EQ(label_preferred_size.height() * 2,
146 pref_height - styled()->GetInsets().height());
147
148 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height);
149 styled()->Layout();
150 ASSERT_EQ(2, styled()->child_count());
151 EXPECT_EQ(0, styled()->child_at(0)->bounds().x());
152 EXPECT_EQ(0, styled()->child_at(1)->bounds().x());
153 }
154
155 TEST_F(StyledLabelTest, StyledRangeUnderlined) {
156 const std::string text("This is a test block of text, ");
157 const std::string underlined_text("and this should be undelined");
158 InitStyledLabel(text + underlined_text);
159 StyledLabel::RangeStyleInfo style_info;
160 style_info.font_style = gfx::Font::UNDERLINE;
161 styled()->AddStyleRange(
162 ui::Range(text.size(), text.size() + underlined_text.size()),
163 style_info);
164
165 styled()->SetBounds(0, 0, 1000, 1000);
166 styled()->Layout();
167
168 ASSERT_EQ(2, styled()->child_count());
169 ASSERT_EQ(Label::kViewClassName, styled()->child_at(1)->GetClassName());
170 EXPECT_EQ(gfx::Font::UNDERLINE,
171 static_cast<Label*>(styled()->child_at(1))->font().GetStyle());
172 }
173
174 TEST_F(StyledLabelTest, StyledRangeBold) {
175 const std::string bold_text(
176 "This is a block of text whose style will be set to BOLD in the test");
177 const std::string text(" normal text");
178 InitStyledLabel(bold_text + text);
179
180 StyledLabel::RangeStyleInfo style_info;
181 style_info.font_style = gfx::Font::BOLD;
182 styled()->AddStyleRange(ui::Range(0, bold_text.size()), style_info);
183
184 // Calculate the bold text width if it were a pure label view, both with bold
185 // and normal style.
186 Label label(ASCIIToUTF16(bold_text));
187 const gfx::Size normal_label_size = label.GetPreferredSize();
188 label.SetFont(label.font().DeriveFont(0, gfx::Font::BOLD));
189 const gfx::Size bold_label_size = label.GetPreferredSize();
190
191 ASSERT_GE(bold_label_size.width(), normal_label_size.width());
192
193 // Set the width so |bold_text| doesn't fit in a single line with bold style,
194 // but does with normal font style.
Evan Stade 2013/03/27 22:25:11 can you have the test make sure the second asserti
tbarzic 2013/03/28 20:33:58 Done.
195 int styled_width = (normal_label_size.width() + bold_label_size.width()) / 2;
196 int pref_height = styled()->GetHeightForWidth(styled_width);
197
198 styled()->SetBounds(0, 0, styled_width, pref_height);
199 styled()->Layout();
200
201 ASSERT_EQ(3, styled()->child_count());
202
203 // The bold text should be broken up into two parts.
204 ASSERT_EQ(Label::kViewClassName, styled()->child_at(0)->GetClassName());
205 EXPECT_EQ(gfx::Font::BOLD,
206 static_cast<Label*>(styled()->child_at(0))->font().GetStyle());
207 ASSERT_EQ(Label::kViewClassName, styled()->child_at(1)->GetClassName());
208 EXPECT_EQ(gfx::Font::BOLD,
209 static_cast<Label*>(styled()->child_at(1))->font().GetStyle());
210 ASSERT_EQ(Label::kViewClassName, styled()->child_at(2)->GetClassName());
211 EXPECT_EQ(gfx::Font::NORMAL,
212 static_cast<Label*>(styled()->child_at(2))->font().GetStyle());
213
214 // The second bold part should start in a new line.
Evan Stade 2013/03/27 22:25:11 nit: on a new line.
tbarzic 2013/03/28 20:33:58 Done.
215 EXPECT_EQ(0, styled()->child_at(0)->bounds().x());
216 EXPECT_EQ(0, styled()->child_at(1)->bounds().x());
217 EXPECT_EQ(styled()->child_at(1)->bounds().right() - 2,
218 styled()->child_at(2)->bounds().x());
219 }
220
221 TEST_F(StyledLabelTest, StyledRangeWithTooltip) {
222 const std::string text("This is a test block of text, ");
223 const std::string tooltip_text("this should have a tooltip,");
224 const std::string normal_text(" this should not have a tooltip, ");
225 const std::string link_text("and this should be a link");
226
227 const size_t tooltip_start = text.size();
228 const size_t link_start =
229 text.size() + tooltip_text.size() + normal_text.size();
230
231 InitStyledLabel(text + tooltip_text + normal_text + link_text);
232 StyledLabel::RangeStyleInfo tooltip_style;
233 tooltip_style.tooltip = ASCIIToUTF16("tooltip");
234 styled()->AddStyleRange(
235 ui::Range(tooltip_start, tooltip_start + tooltip_text.size()),
236 tooltip_style);
237 styled()->AddLink(ui::Range(link_start, link_start + link_text.size()));
238
239 // Break line inside the range with the tooltip.
240 Label label(ASCIIToUTF16(
241 text + tooltip_text.substr(0, tooltip_text.size() - 3)));
242 gfx::Size label_preferred_size = label.GetPreferredSize();
243 int pref_height = styled()->GetHeightForWidth(label_preferred_size.width());
244 EXPECT_EQ(label_preferred_size.height() * 3,
245 pref_height - styled()->GetInsets().height());
246
247 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height);
248 styled()->Layout();
249
250 EXPECT_EQ(label_preferred_size.width(), styled()->width());
251
252 ASSERT_EQ(5, styled()->child_count());
253 EXPECT_EQ(0, styled()->child_at(0)->bounds().x());
254 EXPECT_EQ(styled()->child_at(0)->bounds().right() - 2,
255 styled()->child_at(1)->bounds().x());
256 EXPECT_EQ(0, styled()->child_at(2)->bounds().x());
257 EXPECT_EQ(styled()->child_at(2)->bounds().right() - 2,
258 styled()->child_at(3)->bounds().x());
259 EXPECT_EQ(0, styled()->child_at(4)->bounds().x());
260
261 string16 tooltip;
262 EXPECT_TRUE(
263 styled()->child_at(1)->GetTooltipText(gfx::Point(1, 1), &tooltip));
264 EXPECT_EQ(ASCIIToUTF16("tooltip"), tooltip);
265 EXPECT_TRUE(
266 styled()->child_at(2)->GetTooltipText(gfx::Point(1, 1), &tooltip));
267 EXPECT_EQ(ASCIIToUTF16("tooltip"), tooltip);
268 }
269
107 TEST_F(StyledLabelTest, HandleEmptyLayout) { 270 TEST_F(StyledLabelTest, HandleEmptyLayout) {
108 const std::string text("This is a test block of text, "); 271 const std::string text("This is a test block of text, ");
109 InitStyledLabel(text); 272 InitStyledLabel(text);
110 styled()->Layout(); 273 styled()->Layout();
111 ASSERT_EQ(0, styled()->child_count()); 274 ASSERT_EQ(0, styled()->child_count());
112 } 275 }
113 276
114 } // namespace 277 } // namespace
OLDNEW
« ui/views/controls/styled_label.cc ('K') | « ui/views/controls/styled_label.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698