| OLD | NEW |
| 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 Loading... |
| 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; |
| 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( |
| 269 label.font_list().DeriveWithWeight(gfx::Font::Weight::BOLD)); |
| 269 const gfx::Size bold_label_size = label.GetPreferredSize(); | 270 const gfx::Size bold_label_size = label.GetPreferredSize(); |
| 270 | 271 |
| 271 ASSERT_GE(bold_label_size.width(), normal_label_size.width()); | 272 ASSERT_GE(bold_label_size.width(), normal_label_size.width()); |
| 272 | 273 |
| 273 // Set the width so |bold_text| doesn't fit on a single line with bold style, | 274 // Set the width so |bold_text| doesn't fit on a single line with bold style, |
| 274 // but does with normal font style. | 275 // but does with normal font style. |
| 275 int styled_width = (normal_label_size.width() + bold_label_size.width()) / 2; | 276 int styled_width = (normal_label_size.width() + bold_label_size.width()) / 2; |
| 276 int pref_height = styled()->GetHeightForWidth(styled_width); | 277 int pref_height = styled()->GetHeightForWidth(styled_width); |
| 277 | 278 |
| 278 // Sanity check that |bold_text| with normal font style would fit on a single | 279 // Sanity check that |bold_text| with normal font style would fit on a single |
| 279 // line in a styled label with width |styled_width|. | 280 // line in a styled label with width |styled_width|. |
| 280 StyledLabel unstyled(ASCIIToUTF16(bold_text), this); | 281 StyledLabel unstyled(ASCIIToUTF16(bold_text), this); |
| 281 unstyled.SetBounds(0, 0, styled_width, pref_height); | 282 unstyled.SetBounds(0, 0, styled_width, pref_height); |
| 282 unstyled.Layout(); | 283 unstyled.Layout(); |
| 283 EXPECT_EQ(1, unstyled.child_count()); | 284 EXPECT_EQ(1, unstyled.child_count()); |
| 284 | 285 |
| 285 styled()->SetBounds(0, 0, styled_width, pref_height); | 286 styled()->SetBounds(0, 0, styled_width, pref_height); |
| 286 styled()->Layout(); | 287 styled()->Layout(); |
| 287 | 288 |
| 288 ASSERT_EQ(3, styled()->child_count()); | 289 ASSERT_EQ(3, styled()->child_count()); |
| 289 | 290 |
| 290 // The bold text should be broken up into two parts. | 291 // The bold text should be broken up into two parts. |
| 291 ASSERT_EQ(std::string(Label::kViewClassName), | 292 ASSERT_EQ(std::string(Label::kViewClassName), |
| 292 styled()->child_at(0)->GetClassName()); | 293 styled()->child_at(0)->GetClassName()); |
| 293 EXPECT_EQ( | 294 EXPECT_EQ( |
| 294 gfx::Font::BOLD, | 295 gfx::Font::Weight::BOLD, |
| 295 static_cast<Label*>(styled()->child_at(0))->font_list().GetFontStyle()); | 296 static_cast<Label*>(styled()->child_at(0))->font_list().GetFontWeight()); |
| 296 ASSERT_EQ(std::string(Label::kViewClassName), | 297 ASSERT_EQ(std::string(Label::kViewClassName), |
| 297 styled()->child_at(1)->GetClassName()); | 298 styled()->child_at(1)->GetClassName()); |
| 298 EXPECT_EQ( | 299 EXPECT_EQ( |
| 299 gfx::Font::BOLD, | 300 gfx::Font::Weight::BOLD, |
| 300 static_cast<Label*>(styled()->child_at(1))->font_list().GetFontStyle()); | 301 static_cast<Label*>(styled()->child_at(1))->font_list().GetFontWeight()); |
| 301 ASSERT_EQ(std::string(Label::kViewClassName), | 302 ASSERT_EQ(std::string(Label::kViewClassName), |
| 302 styled()->child_at(2)->GetClassName()); | 303 styled()->child_at(2)->GetClassName()); |
| 303 EXPECT_EQ( | 304 EXPECT_EQ( |
| 304 gfx::Font::NORMAL, | 305 gfx::Font::NORMAL, |
| 305 static_cast<Label*>(styled()->child_at(2))->font_list().GetFontStyle()); | 306 static_cast<Label*>(styled()->child_at(2))->font_list().GetFontStyle()); |
| 306 | 307 |
| 307 // The second bold part should start on a new line. | 308 // The second bold part should start on a new line. |
| 308 EXPECT_EQ(0, styled()->child_at(0)->x()); | 309 EXPECT_EQ(0, styled()->child_at(0)->x()); |
| 309 EXPECT_EQ(0, styled()->child_at(1)->x()); | 310 EXPECT_EQ(0, styled()->child_at(1)->x()); |
| 310 EXPECT_EQ(styled()->child_at(1)->bounds().right(), | 311 EXPECT_EQ(styled()->child_at(1)->bounds().right(), |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 // all controls should be recreated | 499 // all controls should be recreated |
| 499 styled()->SetText(another_text); | 500 styled()->SetText(another_text); |
| 500 int updated_height = styled()->GetHeightForWidth(styled()->width()); | 501 int updated_height = styled()->GetHeightForWidth(styled()->width()); |
| 501 EXPECT_NE(updated_height, real_height); | 502 EXPECT_NE(updated_height, real_height); |
| 502 View* first_child_after_text_update = styled()->has_children() ? | 503 View* first_child_after_text_update = styled()->has_children() ? |
| 503 styled()->child_at(0) : nullptr; | 504 styled()->child_at(0) : nullptr; |
| 504 EXPECT_NE(first_child_after_text_update, first_child_after_layout); | 505 EXPECT_NE(first_child_after_text_update, first_child_after_layout); |
| 505 } | 506 } |
| 506 | 507 |
| 507 } // namespace views | 508 } // namespace views |
| OLD | NEW |