Chromium Code Reviews| 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(" two"), | |
| 119 static_cast<Label*>(styled()->child_at(1))->text()); | |
|
Evan Stade
2016/12/06 17:04:47
nit: check contents of first line as well?
| |
| 120 EXPECT_EQ(styled()->GetHeightForWidth(1000), | |
| 121 styled()->child_at(1)->bounds().bottom()); | |
| 122 } | |
| 123 | |
| 92 TEST_F(StyledLabelTest, FirstLineNotEmptyWhenLeadingWhitespaceTooLong) { | 124 TEST_F(StyledLabelTest, FirstLineNotEmptyWhenLeadingWhitespaceTooLong) { |
| 93 const std::string text(" a"); | 125 const std::string text(" a"); |
| 94 InitStyledLabel(text); | 126 InitStyledLabel(text); |
| 95 | 127 |
| 96 Label label(ASCIIToUTF16(text)); | 128 Label label(ASCIIToUTF16(text)); |
| 97 gfx::Size label_preferred_size = label.GetPreferredSize(); | 129 gfx::Size label_preferred_size = label.GetPreferredSize(); |
| 98 | 130 |
| 99 styled()->SetBounds(0, 0, label_preferred_size.width() / 2, 1000); | 131 styled()->SetBounds(0, 0, label_preferred_size.width() / 2, 1000); |
| 100 styled()->Layout(); | 132 styled()->Layout(); |
| 101 | 133 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 121 0, | 153 0, |
| 122 styled()->GetInsets().width() + label_preferred_size.width(), | 154 styled()->GetInsets().width() + label_preferred_size.width(), |
| 123 styled()->GetInsets().height() + 2 * label_preferred_size.height()); | 155 styled()->GetInsets().height() + 2 * label_preferred_size.height()); |
| 124 styled()->Layout(); | 156 styled()->Layout(); |
| 125 ASSERT_EQ(2, styled()->child_count()); | 157 ASSERT_EQ(2, styled()->child_count()); |
| 126 EXPECT_EQ(3, styled()->child_at(0)->x()); | 158 EXPECT_EQ(3, styled()->child_at(0)->x()); |
| 127 EXPECT_EQ(3, styled()->child_at(0)->y()); | 159 EXPECT_EQ(3, styled()->child_at(0)->y()); |
| 128 EXPECT_EQ(styled()->height() - 3, styled()->child_at(1)->bounds().bottom()); | 160 EXPECT_EQ(styled()->height() - 3, styled()->child_at(1)->bounds().bottom()); |
| 129 } | 161 } |
| 130 | 162 |
| 163 TEST_F(StyledLabelTest, AllowEmptyLines) { | |
| 164 const std::string text("one"); | |
| 165 InitStyledLabel(text); | |
| 166 int default_height = styled()->GetHeightForWidth(1000); | |
| 167 const std::string multiline_text("one\n\nthree"); | |
| 168 InitStyledLabel(multiline_text); | |
| 169 styled()->SetBounds(0, 0, 1000, 1000); | |
| 170 styled()->Layout(); | |
| 171 EXPECT_EQ(3 * default_height, styled()->GetHeightForWidth(1000)); | |
| 172 ASSERT_EQ(2, styled()->child_count()); | |
| 173 EXPECT_EQ(styled()->GetHeightForWidth(1000), | |
| 174 styled()->child_at(1)->bounds().bottom()); | |
| 175 } | |
| 176 | |
| 131 TEST_F(StyledLabelTest, WrapLongWords) { | 177 TEST_F(StyledLabelTest, WrapLongWords) { |
| 132 const std::string text("ThisIsTextAsASingleWord"); | 178 const std::string text("ThisIsTextAsASingleWord"); |
| 133 InitStyledLabel(text); | 179 InitStyledLabel(text); |
| 134 Label label(ASCIIToUTF16(text.substr(0, text.size() * 2 / 3))); | 180 Label label(ASCIIToUTF16(text.substr(0, text.size() * 2 / 3))); |
| 135 gfx::Size label_preferred_size = label.GetPreferredSize(); | 181 gfx::Size label_preferred_size = label.GetPreferredSize(); |
| 136 EXPECT_EQ(label_preferred_size.height() * 2, | 182 EXPECT_EQ(label_preferred_size.height() * 2, |
| 137 StyledLabelContentHeightForWidth(label_preferred_size.width())); | 183 StyledLabelContentHeightForWidth(label_preferred_size.width())); |
| 138 | 184 |
| 139 styled()->SetBounds( | 185 styled()->SetBounds( |
| 140 0, 0, styled()->GetInsets().width() + label_preferred_size.width(), | 186 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 | 558 // all controls should be recreated |
| 513 styled()->SetText(another_text); | 559 styled()->SetText(another_text); |
| 514 int updated_height = styled()->GetHeightForWidth(styled()->width()); | 560 int updated_height = styled()->GetHeightForWidth(styled()->width()); |
| 515 EXPECT_NE(updated_height, real_height); | 561 EXPECT_NE(updated_height, real_height); |
| 516 View* first_child_after_text_update = styled()->has_children() ? | 562 View* first_child_after_text_update = styled()->has_children() ? |
| 517 styled()->child_at(0) : nullptr; | 563 styled()->child_at(0) : nullptr; |
| 518 EXPECT_NE(first_child_after_text_update, first_child_after_layout); | 564 EXPECT_NE(first_child_after_text_update, first_child_after_layout); |
| 519 } | 565 } |
| 520 | 566 |
| 521 } // namespace views | 567 } // namespace views |
| OLD | NEW |