| 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 |
| 102 ASSERT_EQ(1, styled()->child_count()); | 136 ASSERT_EQ(1, styled()->child_count()); |
| 103 ASSERT_EQ(std::string(Label::kViewClassName), | 137 ASSERT_EQ(std::string(Label::kViewClassName), |
| 104 styled()->child_at(0)->GetClassName()); | 138 styled()->child_at(0)->GetClassName()); |
| 105 EXPECT_EQ(ASCIIToUTF16("a"), | 139 EXPECT_EQ(ASCIIToUTF16("a"), |
| 106 static_cast<Label*>(styled()->child_at(0))->text()); | 140 static_cast<Label*>(styled()->child_at(0))->text()); |
| 141 EXPECT_EQ(label_preferred_size.height(), |
| 142 styled()->GetHeightForWidth(label_preferred_size.width() / 2)); |
| 107 } | 143 } |
| 108 | 144 |
| 109 TEST_F(StyledLabelTest, BasicWrapping) { | 145 TEST_F(StyledLabelTest, BasicWrapping) { |
| 110 const std::string text("This is a test block of text"); | 146 const std::string text("This is a test block of text"); |
| 111 InitStyledLabel(text); | 147 InitStyledLabel(text); |
| 112 Label label(ASCIIToUTF16(text.substr(0, text.size() * 2 / 3))); | 148 Label label(ASCIIToUTF16(text.substr(0, text.size() * 2 / 3))); |
| 113 gfx::Size label_preferred_size = label.GetPreferredSize(); | 149 gfx::Size label_preferred_size = label.GetPreferredSize(); |
| 114 EXPECT_EQ(label_preferred_size.height() * 2, | 150 EXPECT_EQ(label_preferred_size.height() * 2, |
| 115 StyledLabelContentHeightForWidth(label_preferred_size.width())); | 151 StyledLabelContentHeightForWidth(label_preferred_size.width())); |
| 116 | 152 |
| 117 // Also respect the border. | 153 // Also respect the border. |
| 118 styled()->SetBorder(CreateEmptyBorder(3, 3, 3, 3)); | 154 styled()->SetBorder(CreateEmptyBorder(3, 3, 3, 3)); |
| 119 styled()->SetBounds( | 155 styled()->SetBounds( |
| 120 0, | 156 0, |
| 121 0, | 157 0, |
| 122 styled()->GetInsets().width() + label_preferred_size.width(), | 158 styled()->GetInsets().width() + label_preferred_size.width(), |
| 123 styled()->GetInsets().height() + 2 * label_preferred_size.height()); | 159 styled()->GetInsets().height() + 2 * label_preferred_size.height()); |
| 124 styled()->Layout(); | 160 styled()->Layout(); |
| 125 ASSERT_EQ(2, styled()->child_count()); | 161 ASSERT_EQ(2, styled()->child_count()); |
| 126 EXPECT_EQ(3, styled()->child_at(0)->x()); | 162 EXPECT_EQ(3, styled()->child_at(0)->x()); |
| 127 EXPECT_EQ(3, styled()->child_at(0)->y()); | 163 EXPECT_EQ(3, styled()->child_at(0)->y()); |
| 128 EXPECT_EQ(styled()->height() - 3, styled()->child_at(1)->bounds().bottom()); | 164 EXPECT_EQ(styled()->height() - 3, styled()->child_at(1)->bounds().bottom()); |
| 129 } | 165 } |
| 130 | 166 |
| 167 TEST_F(StyledLabelTest, AllowEmptyLines) { |
| 168 const std::string text("one"); |
| 169 InitStyledLabel(text); |
| 170 int default_height = styled()->GetHeightForWidth(1000); |
| 171 const std::string multiline_text("one\n\nthree"); |
| 172 InitStyledLabel(multiline_text); |
| 173 styled()->SetBounds(0, 0, 1000, 1000); |
| 174 styled()->Layout(); |
| 175 EXPECT_EQ(3 * default_height, styled()->GetHeightForWidth(1000)); |
| 176 ASSERT_EQ(2, styled()->child_count()); |
| 177 EXPECT_EQ(styled()->GetHeightForWidth(1000), |
| 178 styled()->child_at(1)->bounds().bottom()); |
| 179 } |
| 180 |
| 131 TEST_F(StyledLabelTest, WrapLongWords) { | 181 TEST_F(StyledLabelTest, WrapLongWords) { |
| 132 const std::string text("ThisIsTextAsASingleWord"); | 182 const std::string text("ThisIsTextAsASingleWord"); |
| 133 InitStyledLabel(text); | 183 InitStyledLabel(text); |
| 134 Label label(ASCIIToUTF16(text.substr(0, text.size() * 2 / 3))); | 184 Label label(ASCIIToUTF16(text.substr(0, text.size() * 2 / 3))); |
| 135 gfx::Size label_preferred_size = label.GetPreferredSize(); | 185 gfx::Size label_preferred_size = label.GetPreferredSize(); |
| 136 EXPECT_EQ(label_preferred_size.height() * 2, | 186 EXPECT_EQ(label_preferred_size.height() * 2, |
| 137 StyledLabelContentHeightForWidth(label_preferred_size.width())); | 187 StyledLabelContentHeightForWidth(label_preferred_size.width())); |
| 138 | 188 |
| 139 styled()->SetBounds( | 189 styled()->SetBounds( |
| 140 0, 0, styled()->GetInsets().width() + label_preferred_size.width(), | 190 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 | 562 // all controls should be recreated |
| 513 styled()->SetText(another_text); | 563 styled()->SetText(another_text); |
| 514 int updated_height = styled()->GetHeightForWidth(styled()->width()); | 564 int updated_height = styled()->GetHeightForWidth(styled()->width()); |
| 515 EXPECT_NE(updated_height, real_height); | 565 EXPECT_NE(updated_height, real_height); |
| 516 View* first_child_after_text_update = styled()->has_children() ? | 566 View* first_child_after_text_update = styled()->has_children() ? |
| 517 styled()->child_at(0) : nullptr; | 567 styled()->child_at(0) : nullptr; |
| 518 EXPECT_NE(first_child_after_text_update, first_child_after_layout); | 568 EXPECT_NE(first_child_after_text_update, first_child_after_layout); |
| 519 } | 569 } |
| 520 | 570 |
| 521 } // namespace views | 571 } // namespace views |
| OLD | NEW |