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

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

Issue 24012002: Move Range code to gfx. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: d Created 7 years, 3 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/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/skia/include/core/SkColor.h" 11 #include "third_party/skia/include/core/SkColor.h"
12 #include "ui/views/controls/link.h" 12 #include "ui/views/controls/link.h"
13 #include "ui/views/controls/styled_label.h" 13 #include "ui/views/controls/styled_label.h"
14 #include "ui/views/controls/styled_label_listener.h" 14 #include "ui/views/controls/styled_label_listener.h"
15 15
16 namespace views { 16 namespace views {
17 17
18 class StyledLabelTest : public testing::Test, public StyledLabelListener { 18 class StyledLabelTest : public testing::Test, public StyledLabelListener {
19 public: 19 public:
20 StyledLabelTest() {} 20 StyledLabelTest() {}
21 virtual ~StyledLabelTest() {} 21 virtual ~StyledLabelTest() {}
22 22
23 // StyledLabelListener implementation. 23 // StyledLabelListener implementation.
24 virtual void StyledLabelLinkClicked(const ui::Range& range, 24 virtual void StyledLabelLinkClicked(const gfx::Range& range,
25 int event_flags) OVERRIDE {} 25 int event_flags) OVERRIDE {}
26 26
27 protected: 27 protected:
28 StyledLabel* styled() { return styled_.get(); } 28 StyledLabel* styled() { return styled_.get(); }
29 29
30 void InitStyledLabel(const std::string& ascii_text) { 30 void InitStyledLabel(const std::string& ascii_text) {
31 styled_.reset(new StyledLabel(ASCIIToUTF16(ascii_text), this)); 31 styled_.reset(new StyledLabel(ASCIIToUTF16(ascii_text), this));
32 } 32 }
33 33
34 int StyledLabelContentHeightForWidth(int w) { 34 int StyledLabelContentHeightForWidth(int w) {
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 ASSERT_EQ(2, styled()->child_count()); 114 ASSERT_EQ(2, styled()->child_count());
115 EXPECT_EQ(3, styled()->child_at(0)->bounds().x()); 115 EXPECT_EQ(3, styled()->child_at(0)->bounds().x());
116 EXPECT_EQ(3, styled()->child_at(0)->bounds().y()); 116 EXPECT_EQ(3, styled()->child_at(0)->bounds().y());
117 EXPECT_EQ(styled()->bounds().height() - 3, 117 EXPECT_EQ(styled()->bounds().height() - 3,
118 styled()->child_at(1)->bounds().bottom()); 118 styled()->child_at(1)->bounds().bottom());
119 } 119 }
120 120
121 TEST_F(StyledLabelTest, CreateLinks) { 121 TEST_F(StyledLabelTest, CreateLinks) {
122 const std::string text("This is a test block of text."); 122 const std::string text("This is a test block of text.");
123 InitStyledLabel(text); 123 InitStyledLabel(text);
124 styled()->AddStyleRange(ui::Range(0, 1), 124 styled()->AddStyleRange(gfx::Range(0, 1),
125 StyledLabel::RangeStyleInfo::CreateForLink()); 125 StyledLabel::RangeStyleInfo::CreateForLink());
126 styled()->AddStyleRange(ui::Range(1, 2), 126 styled()->AddStyleRange(gfx::Range(1, 2),
127 StyledLabel::RangeStyleInfo::CreateForLink()); 127 StyledLabel::RangeStyleInfo::CreateForLink());
128 styled()->AddStyleRange(ui::Range(10, 11), 128 styled()->AddStyleRange(gfx::Range(10, 11),
129 StyledLabel::RangeStyleInfo::CreateForLink()); 129 StyledLabel::RangeStyleInfo::CreateForLink());
130 styled()->AddStyleRange(ui::Range(12, 13), 130 styled()->AddStyleRange(gfx::Range(12, 13),
131 StyledLabel::RangeStyleInfo::CreateForLink()); 131 StyledLabel::RangeStyleInfo::CreateForLink());
132 132
133 styled()->SetBounds(0, 0, 1000, 1000); 133 styled()->SetBounds(0, 0, 1000, 1000);
134 styled()->Layout(); 134 styled()->Layout();
135 ASSERT_EQ(7, styled()->child_count()); 135 ASSERT_EQ(7, styled()->child_count());
136 } 136 }
137 137
138 TEST_F(StyledLabelTest, DontBreakLinks) { 138 TEST_F(StyledLabelTest, DontBreakLinks) {
139 const std::string text("This is a test block of text, "); 139 const std::string text("This is a test block of text, ");
140 const std::string link_text("and this should be a link"); 140 const std::string link_text("and this should be a link");
141 InitStyledLabel(text + link_text); 141 InitStyledLabel(text + link_text);
142 styled()->AddStyleRange( 142 styled()->AddStyleRange(
143 ui::Range(text.size(), text.size() + link_text.size()), 143 gfx::Range(text.size(), text.size() + link_text.size()),
144 StyledLabel::RangeStyleInfo::CreateForLink()); 144 StyledLabel::RangeStyleInfo::CreateForLink());
145 145
146 Label label(ASCIIToUTF16(text + link_text.substr(0, link_text.size() / 2))); 146 Label label(ASCIIToUTF16(text + link_text.substr(0, link_text.size() / 2)));
147 gfx::Size label_preferred_size = label.GetPreferredSize(); 147 gfx::Size label_preferred_size = label.GetPreferredSize();
148 int pref_height = styled()->GetHeightForWidth(label_preferred_size.width()); 148 int pref_height = styled()->GetHeightForWidth(label_preferred_size.width());
149 EXPECT_EQ(label_preferred_size.height() * 2, 149 EXPECT_EQ(label_preferred_size.height() * 2,
150 pref_height - styled()->GetInsets().height()); 150 pref_height - styled()->GetInsets().height());
151 151
152 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height); 152 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height);
153 styled()->Layout(); 153 styled()->Layout();
154 ASSERT_EQ(2, styled()->child_count()); 154 ASSERT_EQ(2, styled()->child_count());
155 EXPECT_EQ(0, styled()->child_at(0)->bounds().x()); 155 EXPECT_EQ(0, styled()->child_at(0)->bounds().x());
156 EXPECT_EQ(0, styled()->child_at(1)->bounds().x()); 156 EXPECT_EQ(0, styled()->child_at(1)->bounds().x());
157 } 157 }
158 158
159 TEST_F(StyledLabelTest, StyledRangeWithDisabledLineWrapping) { 159 TEST_F(StyledLabelTest, StyledRangeWithDisabledLineWrapping) {
160 const std::string text("This is a test block of text, "); 160 const std::string text("This is a test block of text, ");
161 const std::string unbreakable_text("and this should not be breaked"); 161 const std::string unbreakable_text("and this should not be breaked");
162 InitStyledLabel(text + unbreakable_text); 162 InitStyledLabel(text + unbreakable_text);
163 StyledLabel::RangeStyleInfo style_info; 163 StyledLabel::RangeStyleInfo style_info;
164 style_info.disable_line_wrapping = true; 164 style_info.disable_line_wrapping = true;
165 styled()->AddStyleRange( 165 styled()->AddStyleRange(
166 ui::Range(text.size(), text.size() + unbreakable_text.size()), 166 gfx::Range(text.size(), text.size() + unbreakable_text.size()),
167 style_info); 167 style_info);
168 168
169 Label label(ASCIIToUTF16( 169 Label label(ASCIIToUTF16(
170 text + unbreakable_text.substr(0, unbreakable_text.size() / 2))); 170 text + unbreakable_text.substr(0, unbreakable_text.size() / 2)));
171 gfx::Size label_preferred_size = label.GetPreferredSize(); 171 gfx::Size label_preferred_size = label.GetPreferredSize();
172 int pref_height = styled()->GetHeightForWidth(label_preferred_size.width()); 172 int pref_height = styled()->GetHeightForWidth(label_preferred_size.width());
173 EXPECT_EQ(label_preferred_size.height() * 2, 173 EXPECT_EQ(label_preferred_size.height() * 2,
174 pref_height - styled()->GetInsets().height()); 174 pref_height - styled()->GetInsets().height());
175 175
176 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height); 176 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height);
177 styled()->Layout(); 177 styled()->Layout();
178 ASSERT_EQ(2, styled()->child_count()); 178 ASSERT_EQ(2, styled()->child_count());
179 EXPECT_EQ(0, styled()->child_at(0)->bounds().x()); 179 EXPECT_EQ(0, styled()->child_at(0)->bounds().x());
180 EXPECT_EQ(0, styled()->child_at(1)->bounds().x()); 180 EXPECT_EQ(0, styled()->child_at(1)->bounds().x());
181 } 181 }
182 182
183 TEST_F(StyledLabelTest, StyledRangeUnderlined) { 183 TEST_F(StyledLabelTest, StyledRangeUnderlined) {
184 const std::string text("This is a test block of text, "); 184 const std::string text("This is a test block of text, ");
185 const std::string underlined_text("and this should be undelined"); 185 const std::string underlined_text("and this should be undelined");
186 InitStyledLabel(text + underlined_text); 186 InitStyledLabel(text + underlined_text);
187 StyledLabel::RangeStyleInfo style_info; 187 StyledLabel::RangeStyleInfo style_info;
188 style_info.font_style = gfx::Font::UNDERLINE; 188 style_info.font_style = gfx::Font::UNDERLINE;
189 styled()->AddStyleRange( 189 styled()->AddStyleRange(
190 ui::Range(text.size(), text.size() + underlined_text.size()), 190 gfx::Range(text.size(), text.size() + underlined_text.size()),
191 style_info); 191 style_info);
192 192
193 styled()->SetBounds(0, 0, 1000, 1000); 193 styled()->SetBounds(0, 0, 1000, 1000);
194 styled()->Layout(); 194 styled()->Layout();
195 195
196 ASSERT_EQ(2, styled()->child_count()); 196 ASSERT_EQ(2, styled()->child_count());
197 ASSERT_EQ(std::string(Label::kViewClassName), 197 ASSERT_EQ(std::string(Label::kViewClassName),
198 styled()->child_at(1)->GetClassName()); 198 styled()->child_at(1)->GetClassName());
199 EXPECT_EQ(gfx::Font::UNDERLINE, 199 EXPECT_EQ(gfx::Font::UNDERLINE,
200 static_cast<Label*>(styled()->child_at(1))->font().GetStyle()); 200 static_cast<Label*>(styled()->child_at(1))->font().GetStyle());
201 } 201 }
202 202
203 TEST_F(StyledLabelTest, StyledRangeBold) { 203 TEST_F(StyledLabelTest, StyledRangeBold) {
204 const std::string bold_text( 204 const std::string bold_text(
205 "This is a block of text whose style will be set to BOLD in the test"); 205 "This is a block of text whose style will be set to BOLD in the test");
206 const std::string text(" normal text"); 206 const std::string text(" normal text");
207 InitStyledLabel(bold_text + text); 207 InitStyledLabel(bold_text + text);
208 208
209 StyledLabel::RangeStyleInfo style_info; 209 StyledLabel::RangeStyleInfo style_info;
210 style_info.font_style = gfx::Font::BOLD; 210 style_info.font_style = gfx::Font::BOLD;
211 styled()->AddStyleRange(ui::Range(0, bold_text.size()), style_info); 211 styled()->AddStyleRange(gfx::Range(0, bold_text.size()), style_info);
212 212
213 // Calculate the bold text width if it were a pure label view, both with bold 213 // Calculate the bold text width if it were a pure label view, both with bold
214 // and normal style. 214 // and normal style.
215 Label label(ASCIIToUTF16(bold_text)); 215 Label label(ASCIIToUTF16(bold_text));
216 const gfx::Size normal_label_size = label.GetPreferredSize(); 216 const gfx::Size normal_label_size = label.GetPreferredSize();
217 label.SetFont(label.font().DeriveFont(0, gfx::Font::BOLD)); 217 label.SetFont(label.font().DeriveFont(0, gfx::Font::BOLD));
218 const gfx::Size bold_label_size = label.GetPreferredSize(); 218 const gfx::Size bold_label_size = label.GetPreferredSize();
219 219
220 ASSERT_GE(bold_label_size.width(), normal_label_size.width()); 220 ASSERT_GE(bold_label_size.width(), normal_label_size.width());
221 221
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 } 258 }
259 259
260 TEST_F(StyledLabelTest, Color) { 260 TEST_F(StyledLabelTest, Color) {
261 const std::string text_red("RED"); 261 const std::string text_red("RED");
262 const std::string text_link("link"); 262 const std::string text_link("link");
263 const std::string text("word"); 263 const std::string text("word");
264 InitStyledLabel(text_red + text_link + text); 264 InitStyledLabel(text_red + text_link + text);
265 265
266 StyledLabel::RangeStyleInfo style_info_red; 266 StyledLabel::RangeStyleInfo style_info_red;
267 style_info_red.color = SK_ColorRED; 267 style_info_red.color = SK_ColorRED;
268 styled()->AddStyleRange(ui::Range(0, text_red.size()), style_info_red); 268 styled()->AddStyleRange(gfx::Range(0, text_red.size()), style_info_red);
269 269
270 StyledLabel::RangeStyleInfo style_info_link = 270 StyledLabel::RangeStyleInfo style_info_link =
271 StyledLabel::RangeStyleInfo::CreateForLink(); 271 StyledLabel::RangeStyleInfo::CreateForLink();
272 styled()->AddStyleRange(ui::Range(text_red.size(), 272 styled()->AddStyleRange(gfx::Range(text_red.size(),
273 text_red.size() + text_link.size()), 273 text_red.size() + text_link.size()),
274 style_info_link); 274 style_info_link);
275 275
276 // Obtain the default text color for a label. 276 // Obtain the default text color for a label.
277 Label label(ASCIIToUTF16(text)); 277 Label label(ASCIIToUTF16(text));
278 const SkColor kDefaultTextColor = label.enabled_color(); 278 const SkColor kDefaultTextColor = label.enabled_color();
279 279
280 // Obtain the default text color for a link; 280 // Obtain the default text color for a link;
281 Link link(ASCIIToUTF16(text_link)); 281 Link link(ASCIIToUTF16(text_link));
282 const SkColor kDefaultLinkColor = link.enabled_color(); 282 const SkColor kDefaultLinkColor = link.enabled_color();
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 const std::string link_text("and this should be a link"); 316 const std::string link_text("and this should be a link");
317 317
318 const size_t tooltip_start = text.size(); 318 const size_t tooltip_start = text.size();
319 const size_t link_start = 319 const size_t link_start =
320 text.size() + tooltip_text.size() + normal_text.size(); 320 text.size() + tooltip_text.size() + normal_text.size();
321 321
322 InitStyledLabel(text + tooltip_text + normal_text + link_text); 322 InitStyledLabel(text + tooltip_text + normal_text + link_text);
323 StyledLabel::RangeStyleInfo tooltip_style; 323 StyledLabel::RangeStyleInfo tooltip_style;
324 tooltip_style.tooltip = ASCIIToUTF16("tooltip"); 324 tooltip_style.tooltip = ASCIIToUTF16("tooltip");
325 styled()->AddStyleRange( 325 styled()->AddStyleRange(
326 ui::Range(tooltip_start, tooltip_start + tooltip_text.size()), 326 gfx::Range(tooltip_start, tooltip_start + tooltip_text.size()),
327 tooltip_style); 327 tooltip_style);
328 styled()->AddStyleRange(ui::Range(link_start, link_start + link_text.size()), 328 styled()->AddStyleRange(gfx::Range(link_start, link_start + link_text.size()),
329 StyledLabel::RangeStyleInfo::CreateForLink()); 329 StyledLabel::RangeStyleInfo::CreateForLink());
330 330
331 // Break line inside the range with the tooltip. 331 // Break line inside the range with the tooltip.
332 Label label(ASCIIToUTF16( 332 Label label(ASCIIToUTF16(
333 text + tooltip_text.substr(0, tooltip_text.size() - 3))); 333 text + tooltip_text.substr(0, tooltip_text.size() - 3)));
334 gfx::Size label_preferred_size = label.GetPreferredSize(); 334 gfx::Size label_preferred_size = label.GetPreferredSize();
335 int pref_height = styled()->GetHeightForWidth(label_preferred_size.width()); 335 int pref_height = styled()->GetHeightForWidth(label_preferred_size.width());
336 EXPECT_EQ(label_preferred_size.height() * 3, 336 EXPECT_EQ(label_preferred_size.height() * 3,
337 pref_height - styled()->GetInsets().height()); 337 pref_height - styled()->GetInsets().height());
338 338
(...skipping 21 matching lines...) Expand all
360 } 360 }
361 361
362 TEST_F(StyledLabelTest, HandleEmptyLayout) { 362 TEST_F(StyledLabelTest, HandleEmptyLayout) {
363 const std::string text("This is a test block of text, "); 363 const std::string text("This is a test block of text, ");
364 InitStyledLabel(text); 364 InitStyledLabel(text);
365 styled()->Layout(); 365 styled()->Layout();
366 ASSERT_EQ(0, styled()->child_count()); 366 ASSERT_EQ(0, styled()->child_count());
367 } 367 }
368 368
369 } // namespace 369 } // namespace
OLDNEW
« no previous file with comments | « ui/views/controls/styled_label_listener.h ('k') | ui/views/controls/textfield/native_textfield_views.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698