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

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

Issue 1671403002: Switch gfx::Range to use uint32_t instead of size_t. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review comments Created 4 years, 10 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
« no previous file with comments | « ui/gfx/selection_model.h ('k') | ui/views/examples/multiline_example.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 styled()->SetBounds(0, 0, 1000, 1000); 177 styled()->SetBounds(0, 0, 1000, 1000);
178 styled()->Layout(); 178 styled()->Layout();
179 EXPECT_EQ(7, styled()->child_count()); 179 EXPECT_EQ(7, styled()->child_count());
180 } 180 }
181 181
182 TEST_F(StyledLabelTest, DontBreakLinks) { 182 TEST_F(StyledLabelTest, DontBreakLinks) {
183 const std::string text("This is a test block of text, "); 183 const std::string text("This is a test block of text, ");
184 const std::string link_text("and this should be a link"); 184 const std::string link_text("and this should be a link");
185 InitStyledLabel(text + link_text); 185 InitStyledLabel(text + link_text);
186 styled()->AddStyleRange( 186 styled()->AddStyleRange(
187 gfx::Range(text.size(), text.size() + link_text.size()), 187 gfx::Range(static_cast<uint32_t>(text.size()),
188 static_cast<uint32_t>(text.size() + link_text.size())),
188 StyledLabel::RangeStyleInfo::CreateForLink()); 189 StyledLabel::RangeStyleInfo::CreateForLink());
189 190
190 Label label(ASCIIToUTF16(text + link_text.substr(0, link_text.size() / 2))); 191 Label label(ASCIIToUTF16(text + link_text.substr(0, link_text.size() / 2)));
191 gfx::Size label_preferred_size = label.GetPreferredSize(); 192 gfx::Size label_preferred_size = label.GetPreferredSize();
192 int pref_height = styled()->GetHeightForWidth(label_preferred_size.width()); 193 int pref_height = styled()->GetHeightForWidth(label_preferred_size.width());
193 EXPECT_EQ(label_preferred_size.height() * 2, 194 EXPECT_EQ(label_preferred_size.height() * 2,
194 pref_height - styled()->GetInsets().height()); 195 pref_height - styled()->GetInsets().height());
195 196
196 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height); 197 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height);
197 styled()->Layout(); 198 styled()->Layout();
198 ASSERT_EQ(2, styled()->child_count()); 199 ASSERT_EQ(2, styled()->child_count());
199 // The label has no focus border while the link (and thus overall styled 200 // The label has no focus border while the link (and thus overall styled
200 // label) does, so the label should be inset by the width of the focus border. 201 // label) does, so the label should be inset by the width of the focus border.
201 EXPECT_EQ(Label::kFocusBorderPadding, styled()->child_at(0)->x()); 202 EXPECT_EQ(Label::kFocusBorderPadding, styled()->child_at(0)->x());
202 EXPECT_EQ(0, styled()->child_at(1)->x()); 203 EXPECT_EQ(0, styled()->child_at(1)->x());
203 } 204 }
204 205
205 TEST_F(StyledLabelTest, StyledRangeWithDisabledLineWrapping) { 206 TEST_F(StyledLabelTest, StyledRangeWithDisabledLineWrapping) {
206 const std::string text("This is a test block of text, "); 207 const std::string text("This is a test block of text, ");
207 const std::string unbreakable_text("and this should not be broken"); 208 const std::string unbreakable_text("and this should not be broken");
208 InitStyledLabel(text + unbreakable_text); 209 InitStyledLabel(text + unbreakable_text);
209 StyledLabel::RangeStyleInfo style_info; 210 StyledLabel::RangeStyleInfo style_info;
210 style_info.disable_line_wrapping = true; 211 style_info.disable_line_wrapping = true;
211 styled()->AddStyleRange( 212 styled()->AddStyleRange(
212 gfx::Range(text.size(), text.size() + unbreakable_text.size()), 213 gfx::Range(static_cast<uint32_t>(text.size()),
214 static_cast<uint32_t>(text.size() + unbreakable_text.size())),
213 style_info); 215 style_info);
214 216
215 Label label(ASCIIToUTF16( 217 Label label(ASCIIToUTF16(
216 text + unbreakable_text.substr(0, unbreakable_text.size() / 2))); 218 text + unbreakable_text.substr(0, unbreakable_text.size() / 2)));
217 gfx::Size label_preferred_size = label.GetPreferredSize(); 219 gfx::Size label_preferred_size = label.GetPreferredSize();
218 int pref_height = styled()->GetHeightForWidth(label_preferred_size.width()); 220 int pref_height = styled()->GetHeightForWidth(label_preferred_size.width());
219 EXPECT_EQ(label_preferred_size.height() * 2, 221 EXPECT_EQ(label_preferred_size.height() * 2,
220 pref_height - styled()->GetInsets().height()); 222 pref_height - styled()->GetInsets().height());
221 223
222 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height); 224 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height);
223 styled()->Layout(); 225 styled()->Layout();
224 ASSERT_EQ(2, styled()->child_count()); 226 ASSERT_EQ(2, styled()->child_count());
225 EXPECT_EQ(0, styled()->child_at(0)->x()); 227 EXPECT_EQ(0, styled()->child_at(0)->x());
226 EXPECT_EQ(0, styled()->child_at(1)->x()); 228 EXPECT_EQ(0, styled()->child_at(1)->x());
227 } 229 }
228 230
229 TEST_F(StyledLabelTest, StyledRangeUnderlined) { 231 TEST_F(StyledLabelTest, StyledRangeUnderlined) {
230 const std::string text("This is a test block of text, "); 232 const std::string text("This is a test block of text, ");
231 const std::string underlined_text("and this should be undelined"); 233 const std::string underlined_text("and this should be undelined");
232 InitStyledLabel(text + underlined_text); 234 InitStyledLabel(text + underlined_text);
233 StyledLabel::RangeStyleInfo style_info; 235 StyledLabel::RangeStyleInfo style_info;
234 style_info.font_style = gfx::Font::UNDERLINE; 236 style_info.font_style = gfx::Font::UNDERLINE;
235 styled()->AddStyleRange( 237 styled()->AddStyleRange(
236 gfx::Range(text.size(), text.size() + underlined_text.size()), 238 gfx::Range(static_cast<uint32_t>(text.size()),
239 static_cast<uint32_t>(text.size() + underlined_text.size())),
237 style_info); 240 style_info);
238 241
239 styled()->SetBounds(0, 0, 1000, 1000); 242 styled()->SetBounds(0, 0, 1000, 1000);
240 styled()->Layout(); 243 styled()->Layout();
241 244
242 ASSERT_EQ(2, styled()->child_count()); 245 ASSERT_EQ(2, styled()->child_count());
243 ASSERT_EQ(std::string(Label::kViewClassName), 246 ASSERT_EQ(std::string(Label::kViewClassName),
244 styled()->child_at(1)->GetClassName()); 247 styled()->child_at(1)->GetClassName());
245 EXPECT_EQ( 248 EXPECT_EQ(
246 gfx::Font::UNDERLINE, 249 gfx::Font::UNDERLINE,
247 static_cast<Label*>(styled()->child_at(1))->font_list().GetFontStyle()); 250 static_cast<Label*>(styled()->child_at(1))->font_list().GetFontStyle());
248 } 251 }
249 252
250 TEST_F(StyledLabelTest, StyledRangeBold) { 253 TEST_F(StyledLabelTest, StyledRangeBold) {
251 const std::string bold_text( 254 const std::string bold_text(
252 "This is a block of text whose style will be set to BOLD in the test"); 255 "This is a block of text whose style will be set to BOLD in the test");
253 const std::string text(" normal text"); 256 const std::string text(" normal text");
254 InitStyledLabel(bold_text + text); 257 InitStyledLabel(bold_text + text);
255 258
256 StyledLabel::RangeStyleInfo style_info; 259 StyledLabel::RangeStyleInfo style_info;
257 style_info.font_style = gfx::Font::BOLD; 260 style_info.font_style = gfx::Font::BOLD;
258 styled()->AddStyleRange(gfx::Range(0, bold_text.size()), style_info); 261 styled()->AddStyleRange(
262 gfx::Range(0u, static_cast<uint32_t>(bold_text.size())), style_info);
259 263
260 // Calculate the bold text width if it were a pure label view, both with bold 264 // Calculate the bold text width if it were a pure label view, both with bold
261 // and normal style. 265 // and normal style.
262 Label label(ASCIIToUTF16(bold_text)); 266 Label label(ASCIIToUTF16(bold_text));
263 const gfx::Size normal_label_size = label.GetPreferredSize(); 267 const gfx::Size normal_label_size = label.GetPreferredSize();
264 label.SetFontList(label.font_list().DeriveWithStyle(gfx::Font::BOLD)); 268 label.SetFontList(label.font_list().DeriveWithStyle(gfx::Font::BOLD));
265 const gfx::Size bold_label_size = label.GetPreferredSize(); 269 const gfx::Size bold_label_size = label.GetPreferredSize();
266 270
267 ASSERT_GE(bold_label_size.width(), normal_label_size.width()); 271 ASSERT_GE(bold_label_size.width(), normal_label_size.width());
268 272
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 } 312 }
309 313
310 TEST_F(StyledLabelTest, Color) { 314 TEST_F(StyledLabelTest, Color) {
311 const std::string text_red("RED"); 315 const std::string text_red("RED");
312 const std::string text_link("link"); 316 const std::string text_link("link");
313 const std::string text("word"); 317 const std::string text("word");
314 InitStyledLabel(text_red + text_link + text); 318 InitStyledLabel(text_red + text_link + text);
315 319
316 StyledLabel::RangeStyleInfo style_info_red; 320 StyledLabel::RangeStyleInfo style_info_red;
317 style_info_red.color = SK_ColorRED; 321 style_info_red.color = SK_ColorRED;
318 styled()->AddStyleRange(gfx::Range(0, text_red.size()), style_info_red); 322 styled()->AddStyleRange(
323 gfx::Range(0u, static_cast<uint32_t>(text_red.size())), style_info_red);
319 324
320 StyledLabel::RangeStyleInfo style_info_link = 325 StyledLabel::RangeStyleInfo style_info_link =
321 StyledLabel::RangeStyleInfo::CreateForLink(); 326 StyledLabel::RangeStyleInfo::CreateForLink();
322 styled()->AddStyleRange(gfx::Range(text_red.size(), 327 styled()->AddStyleRange(
323 text_red.size() + text_link.size()), 328 gfx::Range(static_cast<uint32_t>(text_red.size()),
324 style_info_link); 329 static_cast<uint32_t>(text_red.size() + text_link.size())),
330 style_info_link);
325 331
326 styled()->SetBounds(0, 0, 1000, 1000); 332 styled()->SetBounds(0, 0, 1000, 1000);
327 styled()->Layout(); 333 styled()->Layout();
328 334
329 Widget* widget = new Widget(); 335 Widget* widget = new Widget();
330 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); 336 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
331 widget->Init(params); 337 widget->Init(params);
332 View* container = new View(); 338 View* container = new View();
333 widget->SetContentsView(container); 339 widget->SetContentsView(container);
334 container->AddChildView(styled()); 340 container->AddChildView(styled());
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 const std::string link_text("and this should be a link"); 376 const std::string link_text("and this should be a link");
371 377
372 const size_t tooltip_start = text.size(); 378 const size_t tooltip_start = text.size();
373 const size_t link_start = 379 const size_t link_start =
374 text.size() + tooltip_text.size() + normal_text.size(); 380 text.size() + tooltip_text.size() + normal_text.size();
375 381
376 InitStyledLabel(text + tooltip_text + normal_text + link_text); 382 InitStyledLabel(text + tooltip_text + normal_text + link_text);
377 StyledLabel::RangeStyleInfo tooltip_style; 383 StyledLabel::RangeStyleInfo tooltip_style;
378 tooltip_style.tooltip = ASCIIToUTF16("tooltip"); 384 tooltip_style.tooltip = ASCIIToUTF16("tooltip");
379 styled()->AddStyleRange( 385 styled()->AddStyleRange(
380 gfx::Range(tooltip_start, tooltip_start + tooltip_text.size()), 386 gfx::Range(static_cast<uint32_t>(tooltip_start),
387 static_cast<uint32_t>(tooltip_start + tooltip_text.size())),
381 tooltip_style); 388 tooltip_style);
382 styled()->AddStyleRange(gfx::Range(link_start, link_start + link_text.size()), 389 styled()->AddStyleRange(
383 StyledLabel::RangeStyleInfo::CreateForLink()); 390 gfx::Range(static_cast<uint32_t>(link_start),
391 static_cast<uint32_t>(link_start + link_text.size())),
392 StyledLabel::RangeStyleInfo::CreateForLink());
384 393
385 // Break line inside the range with the tooltip. 394 // Break line inside the range with the tooltip.
386 Label label(ASCIIToUTF16( 395 Label label(ASCIIToUTF16(
387 text + tooltip_text.substr(0, tooltip_text.size() - 3))); 396 text + tooltip_text.substr(0, tooltip_text.size() - 3)));
388 gfx::Size label_preferred_size = label.GetPreferredSize(); 397 gfx::Size label_preferred_size = label.GetPreferredSize();
389 int pref_height = styled()->GetHeightForWidth(label_preferred_size.width()); 398 int pref_height = styled()->GetHeightForWidth(label_preferred_size.width());
390 EXPECT_EQ(label_preferred_size.height() * 3, 399 EXPECT_EQ(label_preferred_size.height() * 3,
391 pref_height - styled()->GetInsets().height()); 400 pref_height - styled()->GetInsets().height());
392 401
393 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height); 402 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height);
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 // all controls should be recreated 498 // all controls should be recreated
490 styled()->SetText(another_text); 499 styled()->SetText(another_text);
491 int updated_height = styled()->GetHeightForWidth(styled()->width()); 500 int updated_height = styled()->GetHeightForWidth(styled()->width());
492 EXPECT_NE(updated_height, real_height); 501 EXPECT_NE(updated_height, real_height);
493 View* first_child_after_text_update = styled()->has_children() ? 502 View* first_child_after_text_update = styled()->has_children() ?
494 styled()->child_at(0) : nullptr; 503 styled()->child_at(0) : nullptr;
495 EXPECT_NE(first_child_after_text_update, first_child_after_layout); 504 EXPECT_NE(first_child_after_text_update, first_child_after_layout);
496 } 505 }
497 506
498 } // namespace views 507 } // namespace views
OLDNEW
« no previous file with comments | « ui/gfx/selection_model.h ('k') | ui/views/examples/multiline_example.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698