| 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 "ui/views/controls/styled_label.h" | 5 #include "ui/views/controls/styled_label.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 styled()->SetBounds(0, 0, 1000, 1000); | 82 styled()->SetBounds(0, 0, 1000, 1000); |
| 83 styled()->Layout(); | 83 styled()->Layout(); |
| 84 | 84 |
| 85 ASSERT_EQ(1, styled()->child_count()); | 85 ASSERT_EQ(1, styled()->child_count()); |
| 86 ASSERT_EQ(std::string(Label::kViewClassName), | 86 ASSERT_EQ(std::string(Label::kViewClassName), |
| 87 styled()->child_at(0)->GetClassName()); | 87 styled()->child_at(0)->GetClassName()); |
| 88 EXPECT_EQ(ASCIIToUTF16(" This is a test block of text"), | 88 EXPECT_EQ(ASCIIToUTF16(" This is a test block of text"), |
| 89 static_cast<Label*>(styled()->child_at(0))->text()); | 89 static_cast<Label*>(styled()->child_at(0))->text()); |
| 90 } | 90 } |
| 91 | 91 |
| 92 TEST_F(StyledLabelTest, RespectLeadingSpacesInNonFirstLine) { |
| 93 const std::string indented_line = " indented line"; |
| 94 const std::string text(std::string("First line\n") + indented_line); |
| 95 InitStyledLabel(text); |
| 96 styled()->SetBounds(0, 0, 1000, 1000); |
| 97 styled()->Layout(); |
| 98 ASSERT_EQ(2, styled()->child_count()); |
| 99 ASSERT_EQ(std::string(Label::kViewClassName), |
| 100 styled()->child_at(0)->GetClassName()); |
| 101 EXPECT_EQ(ASCIIToUTF16(indented_line), |
| 102 static_cast<Label*>(styled()->child_at(1))->text()); |
| 103 } |
| 104 |
| 105 TEST_F(StyledLabelTest, CorrectWrapAtNewline) { |
| 106 const std::string first_line = "Line one"; |
| 107 const std::string second_line = " two"; |
| 108 const std::string multiline_text(first_line + "\n" + second_line); |
| 109 InitStyledLabel(multiline_text); |
| 110 Label label(ASCIIToUTF16(first_line)); |
| 111 gfx::Size label_preferred_size = label.GetPreferredSize(); |
| 112 // Correct handling of \n and label width limit encountered at the same place |
| 113 styled()->SetBounds(0, 0, label_preferred_size.width(), 1000); |
| 114 styled()->Layout(); |
| 115 ASSERT_EQ(2, styled()->child_count()); |
| 116 ASSERT_EQ(std::string(Label::kViewClassName), |
| 117 styled()->child_at(1)->GetClassName()); |
| 118 EXPECT_EQ(ASCIIToUTF16(first_line), |
| 119 static_cast<Label*>(styled()->child_at(0))->text()); |
| 120 EXPECT_EQ(ASCIIToUTF16(second_line), |
| 121 static_cast<Label*>(styled()->child_at(1))->text()); |
| 122 EXPECT_EQ(styled()->GetHeightForWidth(1000), |
| 123 styled()->child_at(1)->bounds().bottom()); |
| 124 } |
| 125 |
| 92 TEST_F(StyledLabelTest, FirstLineNotEmptyWhenLeadingWhitespaceTooLong) { | 126 TEST_F(StyledLabelTest, FirstLineNotEmptyWhenLeadingWhitespaceTooLong) { |
| 93 const std::string text(" a"); | 127 const std::string text(" a"); |
| 94 InitStyledLabel(text); | 128 InitStyledLabel(text); |
| 95 | 129 |
| 96 Label label(ASCIIToUTF16(text)); | 130 Label label(ASCIIToUTF16(text)); |
| 97 gfx::Size label_preferred_size = label.GetPreferredSize(); | 131 gfx::Size label_preferred_size = label.GetPreferredSize(); |
| 98 | 132 |
| 99 styled()->SetBounds(0, 0, label_preferred_size.width() / 2, 1000); | 133 styled()->SetBounds(0, 0, label_preferred_size.width() / 2, 1000); |
| 100 styled()->Layout(); | 134 styled()->Layout(); |
| 101 | 135 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 121 0, | 155 0, |
| 122 styled()->GetInsets().width() + label_preferred_size.width(), | 156 styled()->GetInsets().width() + label_preferred_size.width(), |
| 123 styled()->GetInsets().height() + 2 * label_preferred_size.height()); | 157 styled()->GetInsets().height() + 2 * label_preferred_size.height()); |
| 124 styled()->Layout(); | 158 styled()->Layout(); |
| 125 ASSERT_EQ(2, styled()->child_count()); | 159 ASSERT_EQ(2, styled()->child_count()); |
| 126 EXPECT_EQ(3, styled()->child_at(0)->x()); | 160 EXPECT_EQ(3, styled()->child_at(0)->x()); |
| 127 EXPECT_EQ(3, styled()->child_at(0)->y()); | 161 EXPECT_EQ(3, styled()->child_at(0)->y()); |
| 128 EXPECT_EQ(styled()->height() - 3, styled()->child_at(1)->bounds().bottom()); | 162 EXPECT_EQ(styled()->height() - 3, styled()->child_at(1)->bounds().bottom()); |
| 129 } | 163 } |
| 130 | 164 |
| 165 TEST_F(StyledLabelTest, AllowEmptyLines) { |
| 166 const std::string text("one"); |
| 167 InitStyledLabel(text); |
| 168 int default_height = styled()->GetHeightForWidth(1000); |
| 169 const std::string multiline_text("one\n\nthree"); |
| 170 InitStyledLabel(multiline_text); |
| 171 styled()->SetBounds(0, 0, 1000, 1000); |
| 172 styled()->Layout(); |
| 173 EXPECT_EQ(3 * default_height, styled()->GetHeightForWidth(1000)); |
| 174 ASSERT_EQ(2, styled()->child_count()); |
| 175 EXPECT_EQ(styled()->GetHeightForWidth(1000), |
| 176 styled()->child_at(1)->bounds().bottom()); |
| 177 } |
| 178 |
| 131 TEST_F(StyledLabelTest, WrapLongWords) { | 179 TEST_F(StyledLabelTest, WrapLongWords) { |
| 132 const std::string text("ThisIsTextAsASingleWord"); | 180 const std::string text("ThisIsTextAsASingleWord"); |
| 133 InitStyledLabel(text); | 181 InitStyledLabel(text); |
| 134 Label label(ASCIIToUTF16(text.substr(0, text.size() * 2 / 3))); | 182 Label label(ASCIIToUTF16(text.substr(0, text.size() * 2 / 3))); |
| 135 gfx::Size label_preferred_size = label.GetPreferredSize(); | 183 gfx::Size label_preferred_size = label.GetPreferredSize(); |
| 136 EXPECT_EQ(label_preferred_size.height() * 2, | 184 EXPECT_EQ(label_preferred_size.height() * 2, |
| 137 StyledLabelContentHeightForWidth(label_preferred_size.width())); | 185 StyledLabelContentHeightForWidth(label_preferred_size.width())); |
| 138 | 186 |
| 139 styled()->SetBounds( | 187 styled()->SetBounds( |
| 140 0, 0, styled()->GetInsets().width() + label_preferred_size.width(), | 188 0, 0, styled()->GetInsets().width() + label_preferred_size.width(), |
| (...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 // all controls should be recreated | 560 // all controls should be recreated |
| 513 styled()->SetText(another_text); | 561 styled()->SetText(another_text); |
| 514 int updated_height = styled()->GetHeightForWidth(styled()->width()); | 562 int updated_height = styled()->GetHeightForWidth(styled()->width()); |
| 515 EXPECT_NE(updated_height, real_height); | 563 EXPECT_NE(updated_height, real_height); |
| 516 View* first_child_after_text_update = styled()->has_children() ? | 564 View* first_child_after_text_update = styled()->has_children() ? |
| 517 styled()->child_at(0) : nullptr; | 565 styled()->child_at(0) : nullptr; |
| 518 EXPECT_NE(first_child_after_text_update, first_child_after_layout); | 566 EXPECT_NE(first_child_after_text_update, first_child_after_layout); |
| 519 } | 567 } |
| 520 | 568 |
| 521 } // namespace views | 569 } // namespace views |
| OLD | NEW |