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

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

Issue 1819753003: Allow various font weights in gfx. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes for review issues. Created 4 years, 9 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
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 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 static_cast<Label*>(styled()->child_at(1))->font_list().GetFontStyle()); 250 static_cast<Label*>(styled()->child_at(1))->font_list().GetFontStyle());
251 } 251 }
252 252
253 TEST_F(StyledLabelTest, StyledRangeBold) { 253 TEST_F(StyledLabelTest, StyledRangeBold) {
254 const std::string bold_text( 254 const std::string bold_text(
255 "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");
256 const std::string text(" normal text"); 256 const std::string text(" normal text");
257 InitStyledLabel(bold_text + text); 257 InitStyledLabel(bold_text + text);
258 258
259 StyledLabel::RangeStyleInfo style_info; 259 StyledLabel::RangeStyleInfo style_info;
260 style_info.font_style = gfx::Font::BOLD; 260 style_info.weight = gfx::Font::WEIGHT_BOLD;
msw 2016/03/22 18:24:11 Update enum values in this file.
Mikus 2016/03/23 17:53:22 Done.
261 styled()->AddStyleRange( 261 styled()->AddStyleRange(
262 gfx::Range(0u, static_cast<uint32_t>(bold_text.size())), style_info); 262 gfx::Range(0u, static_cast<uint32_t>(bold_text.size())), style_info);
263 263
264 // 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
265 // and normal style. 265 // and normal style.
266 Label label(ASCIIToUTF16(bold_text)); 266 Label label(ASCIIToUTF16(bold_text));
267 const gfx::Size normal_label_size = label.GetPreferredSize(); 267 const gfx::Size normal_label_size = label.GetPreferredSize();
268 label.SetFontList(label.font_list().DeriveWithStyle(gfx::Font::BOLD)); 268 label.SetFontList(label.font_list().DeriveWithWeight(gfx::Font::WEIGHT_BOLD));
269 const gfx::Size bold_label_size = label.GetPreferredSize(); 269 const gfx::Size bold_label_size = label.GetPreferredSize();
270 270
271 ASSERT_GE(bold_label_size.width(), normal_label_size.width()); 271 ASSERT_GE(bold_label_size.width(), normal_label_size.width());
272 272
273 // Set the width so |bold_text| doesn't fit on a single line with bold style, 273 // Set the width so |bold_text| doesn't fit on a single line with bold style,
274 // but does with normal font style. 274 // but does with normal font style.
275 int styled_width = (normal_label_size.width() + bold_label_size.width()) / 2; 275 int styled_width = (normal_label_size.width() + bold_label_size.width()) / 2;
276 int pref_height = styled()->GetHeightForWidth(styled_width); 276 int pref_height = styled()->GetHeightForWidth(styled_width);
277 277
278 // Sanity check that |bold_text| with normal font style would fit on a single 278 // Sanity check that |bold_text| with normal font style would fit on a single
279 // line in a styled label with width |styled_width|. 279 // line in a styled label with width |styled_width|.
280 StyledLabel unstyled(ASCIIToUTF16(bold_text), this); 280 StyledLabel unstyled(ASCIIToUTF16(bold_text), this);
281 unstyled.SetBounds(0, 0, styled_width, pref_height); 281 unstyled.SetBounds(0, 0, styled_width, pref_height);
282 unstyled.Layout(); 282 unstyled.Layout();
283 EXPECT_EQ(1, unstyled.child_count()); 283 EXPECT_EQ(1, unstyled.child_count());
284 284
285 styled()->SetBounds(0, 0, styled_width, pref_height); 285 styled()->SetBounds(0, 0, styled_width, pref_height);
286 styled()->Layout(); 286 styled()->Layout();
287 287
288 ASSERT_EQ(3, styled()->child_count()); 288 ASSERT_EQ(3, styled()->child_count());
289 289
290 // The bold text should be broken up into two parts. 290 // The bold text should be broken up into two parts.
291 ASSERT_EQ(std::string(Label::kViewClassName), 291 ASSERT_EQ(std::string(Label::kViewClassName),
292 styled()->child_at(0)->GetClassName()); 292 styled()->child_at(0)->GetClassName());
293 EXPECT_EQ( 293 EXPECT_EQ(
294 gfx::Font::BOLD, 294 gfx::Font::WEIGHT_BOLD,
295 static_cast<Label*>(styled()->child_at(0))->font_list().GetFontStyle()); 295 static_cast<Label*>(styled()->child_at(0))->font_list().GetFontWeight());
296 ASSERT_EQ(std::string(Label::kViewClassName), 296 ASSERT_EQ(std::string(Label::kViewClassName),
297 styled()->child_at(1)->GetClassName()); 297 styled()->child_at(1)->GetClassName());
298 EXPECT_EQ( 298 EXPECT_EQ(
299 gfx::Font::BOLD, 299 gfx::Font::WEIGHT_BOLD,
300 static_cast<Label*>(styled()->child_at(1))->font_list().GetFontStyle()); 300 static_cast<Label*>(styled()->child_at(1))->font_list().GetFontWeight());
301 ASSERT_EQ(std::string(Label::kViewClassName), 301 ASSERT_EQ(std::string(Label::kViewClassName),
302 styled()->child_at(2)->GetClassName()); 302 styled()->child_at(2)->GetClassName());
303 EXPECT_EQ( 303 EXPECT_EQ(
304 gfx::Font::NORMAL, 304 gfx::Font::NORMAL,
305 static_cast<Label*>(styled()->child_at(2))->font_list().GetFontStyle()); 305 static_cast<Label*>(styled()->child_at(2))->font_list().GetFontStyle());
306 306
307 // The second bold part should start on a new line. 307 // The second bold part should start on a new line.
308 EXPECT_EQ(0, styled()->child_at(0)->x()); 308 EXPECT_EQ(0, styled()->child_at(0)->x());
309 EXPECT_EQ(0, styled()->child_at(1)->x()); 309 EXPECT_EQ(0, styled()->child_at(1)->x());
310 EXPECT_EQ(styled()->child_at(1)->bounds().right(), 310 EXPECT_EQ(styled()->child_at(1)->bounds().right(),
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 // all controls should be recreated 498 // all controls should be recreated
499 styled()->SetText(another_text); 499 styled()->SetText(another_text);
500 int updated_height = styled()->GetHeightForWidth(styled()->width()); 500 int updated_height = styled()->GetHeightForWidth(styled()->width());
501 EXPECT_NE(updated_height, real_height); 501 EXPECT_NE(updated_height, real_height);
502 View* first_child_after_text_update = styled()->has_children() ? 502 View* first_child_after_text_update = styled()->has_children() ?
503 styled()->child_at(0) : nullptr; 503 styled()->child_at(0) : nullptr;
504 EXPECT_NE(first_child_after_text_update, first_child_after_layout); 504 EXPECT_NE(first_child_after_text_update, first_child_after_layout);
505 } 505 }
506 506
507 } // namespace views 507 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698