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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 | 42 |
43 TEST_F(StyledLabelTest, NoWrapping) { | 43 TEST_F(StyledLabelTest, NoWrapping) { |
44 const std::string text("This is a test block of text"); | 44 const std::string text("This is a test block of text"); |
45 InitStyledLabel(text); | 45 InitStyledLabel(text); |
46 Label label(ASCIIToUTF16(text)); | 46 Label label(ASCIIToUTF16(text)); |
47 const gfx::Size label_preferred_size = label.GetPreferredSize(); | 47 const gfx::Size label_preferred_size = label.GetPreferredSize(); |
48 EXPECT_EQ(label_preferred_size.height(), | 48 EXPECT_EQ(label_preferred_size.height(), |
49 StyledLabelContentHeightForWidth(label_preferred_size.width() * 2)); | 49 StyledLabelContentHeightForWidth(label_preferred_size.width() * 2)); |
50 } | 50 } |
51 | 51 |
| 52 TEST_F(StyledLabelTest, TrailingWhitespaceiIgnored) { |
| 53 const std::string text("This is a test block of text "); |
| 54 InitStyledLabel(text); |
| 55 |
| 56 styled()->SetBounds(0, 0, 1000, 1000); |
| 57 styled()->Layout(); |
| 58 |
| 59 ASSERT_EQ(1, styled()->child_count()); |
| 60 ASSERT_EQ(Label::kViewClassName, styled()->child_at(0)->GetClassName()); |
| 61 EXPECT_EQ(ASCIIToUTF16("This is a test block of text"), |
| 62 static_cast<Label*>(styled()->child_at(0))->text()); |
| 63 } |
| 64 |
| 65 TEST_F(StyledLabelTest, RespectLeadingWhitespace) { |
| 66 const std::string text(" This is a test block of text"); |
| 67 InitStyledLabel(text); |
| 68 |
| 69 styled()->SetBounds(0, 0, 1000, 1000); |
| 70 styled()->Layout(); |
| 71 |
| 72 ASSERT_EQ(1, styled()->child_count()); |
| 73 ASSERT_EQ(Label::kViewClassName, styled()->child_at(0)->GetClassName()); |
| 74 EXPECT_EQ(ASCIIToUTF16(" This is a test block of text"), |
| 75 static_cast<Label*>(styled()->child_at(0))->text()); |
| 76 } |
| 77 |
| 78 TEST_F(StyledLabelTest, FirstLineNotEmptyWhenLeadingWhitespaceTooLong) { |
| 79 const std::string text(" a"); |
| 80 InitStyledLabel(text); |
| 81 |
| 82 Label label(ASCIIToUTF16(text)); |
| 83 gfx::Size label_preferred_size = label.GetPreferredSize(); |
| 84 |
| 85 styled()->SetBounds(0, 0, label_preferred_size.width() / 2, 1000); |
| 86 styled()->Layout(); |
| 87 |
| 88 ASSERT_EQ(1, styled()->child_count()); |
| 89 ASSERT_EQ(Label::kViewClassName, styled()->child_at(0)->GetClassName()); |
| 90 EXPECT_EQ(ASCIIToUTF16("a"), |
| 91 static_cast<Label*>(styled()->child_at(0))->text()); |
| 92 } |
| 93 |
52 TEST_F(StyledLabelTest, BasicWrapping) { | 94 TEST_F(StyledLabelTest, BasicWrapping) { |
53 const std::string text("This is a test block of text"); | 95 const std::string text("This is a test block of text"); |
54 InitStyledLabel(text); | 96 InitStyledLabel(text); |
55 Label label(ASCIIToUTF16(text.substr(0, text.size() * 2 / 3))); | 97 Label label(ASCIIToUTF16(text.substr(0, text.size() * 2 / 3))); |
56 gfx::Size label_preferred_size = label.GetPreferredSize(); | 98 gfx::Size label_preferred_size = label.GetPreferredSize(); |
57 EXPECT_EQ(label_preferred_size.height() * 2, | 99 EXPECT_EQ(label_preferred_size.height() * 2, |
58 StyledLabelContentHeightForWidth(label_preferred_size.width())); | 100 StyledLabelContentHeightForWidth(label_preferred_size.width())); |
59 | 101 |
60 // Also respect the border. | 102 // Also respect the border. |
61 styled()->set_border(Border::CreateEmptyBorder(3, 3, 3, 3)); | 103 styled()->set_border(Border::CreateEmptyBorder(3, 3, 3, 3)); |
62 styled()->SetBounds( | 104 styled()->SetBounds( |
63 0, | 105 0, |
64 0, | 106 0, |
65 styled()->GetInsets().width() + label_preferred_size.width(), | 107 styled()->GetInsets().width() + label_preferred_size.width(), |
66 styled()->GetInsets().height() + 2 * label_preferred_size.height()); | 108 styled()->GetInsets().height() + 2 * label_preferred_size.height()); |
67 styled()->Layout(); | 109 styled()->Layout(); |
68 ASSERT_EQ(2, styled()->child_count()); | 110 ASSERT_EQ(2, styled()->child_count()); |
69 EXPECT_EQ(3, styled()->child_at(0)->bounds().x()); | 111 EXPECT_EQ(3, styled()->child_at(0)->bounds().x()); |
70 EXPECT_EQ(3, styled()->child_at(0)->bounds().y()); | 112 EXPECT_EQ(3, styled()->child_at(0)->bounds().y()); |
71 EXPECT_EQ(styled()->bounds().height() - 3, | 113 EXPECT_EQ(styled()->bounds().height() - 3, |
72 styled()->child_at(1)->bounds().bottom()); | 114 styled()->child_at(1)->bounds().bottom()); |
73 } | 115 } |
74 | 116 |
75 TEST_F(StyledLabelTest, CreateLinks) { | 117 TEST_F(StyledLabelTest, CreateLinks) { |
76 const std::string text("This is a test block of text."); | 118 const std::string text("This is a test block of text."); |
77 InitStyledLabel(text); | 119 InitStyledLabel(text); |
78 styled()->AddLink(ui::Range(0, 1)); | 120 styled()->AddStyleRange(ui::Range(0, 1), |
79 styled()->AddLink(ui::Range(1, 2)); | 121 StyledLabel::RangeStyleInfo::CreateForLink()); |
80 styled()->AddLink(ui::Range(10, 11)); | 122 styled()->AddStyleRange(ui::Range(1, 2), |
81 styled()->AddLink(ui::Range(12, 13)); | 123 StyledLabel::RangeStyleInfo::CreateForLink()); |
| 124 styled()->AddStyleRange(ui::Range(10, 11), |
| 125 StyledLabel::RangeStyleInfo::CreateForLink()); |
| 126 styled()->AddStyleRange(ui::Range(12, 13), |
| 127 StyledLabel::RangeStyleInfo::CreateForLink()); |
82 | 128 |
83 styled()->SetBounds(0, 0, 1000, 1000); | 129 styled()->SetBounds(0, 0, 1000, 1000); |
84 styled()->Layout(); | 130 styled()->Layout(); |
85 ASSERT_EQ(7, styled()->child_count()); | 131 ASSERT_EQ(7, styled()->child_count()); |
86 } | 132 } |
87 | 133 |
88 TEST_F(StyledLabelTest, DontBreakLinks) { | 134 TEST_F(StyledLabelTest, DontBreakLinks) { |
89 const std::string text("This is a test block of text, "); | 135 const std::string text("This is a test block of text, "); |
90 const std::string link_text("and this should be a link"); | 136 const std::string link_text("and this should be a link"); |
91 InitStyledLabel(text + link_text); | 137 InitStyledLabel(text + link_text); |
92 styled()->AddLink(ui::Range(text.size(), text.size() + link_text.size())); | 138 styled()->AddStyleRange( |
| 139 ui::Range(text.size(), text.size() + link_text.size()), |
| 140 StyledLabel::RangeStyleInfo::CreateForLink()); |
93 | 141 |
94 Label label(ASCIIToUTF16(text + link_text.substr(0, link_text.size() / 2))); | 142 Label label(ASCIIToUTF16(text + link_text.substr(0, link_text.size() / 2))); |
95 gfx::Size label_preferred_size = label.GetPreferredSize(); | 143 gfx::Size label_preferred_size = label.GetPreferredSize(); |
96 int pref_height = styled()->GetHeightForWidth(label_preferred_size.width()); | 144 int pref_height = styled()->GetHeightForWidth(label_preferred_size.width()); |
97 EXPECT_EQ(label_preferred_size.height() * 2, | 145 EXPECT_EQ(label_preferred_size.height() * 2, |
98 pref_height - styled()->GetInsets().height()); | 146 pref_height - styled()->GetInsets().height()); |
99 | 147 |
100 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height); | 148 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height); |
101 styled()->Layout(); | 149 styled()->Layout(); |
102 ASSERT_EQ(2, styled()->child_count()); | 150 ASSERT_EQ(2, styled()->child_count()); |
103 EXPECT_EQ(0, styled()->child_at(0)->bounds().x()); | 151 EXPECT_EQ(0, styled()->child_at(0)->bounds().x()); |
104 EXPECT_EQ(0, styled()->child_at(1)->bounds().x()); | 152 EXPECT_EQ(0, styled()->child_at(1)->bounds().x()); |
105 } | 153 } |
106 | 154 |
| 155 TEST_F(StyledLabelTest, StyledRangeWithDisabledLineWrapping) { |
| 156 const std::string text("This is a test block of text, "); |
| 157 const std::string unbreakable_text("and this should not be breaked"); |
| 158 InitStyledLabel(text + unbreakable_text); |
| 159 StyledLabel::RangeStyleInfo style_info; |
| 160 style_info.disable_line_wrapping = true; |
| 161 styled()->AddStyleRange( |
| 162 ui::Range(text.size(), text.size() + unbreakable_text.size()), |
| 163 style_info); |
| 164 |
| 165 Label label(ASCIIToUTF16( |
| 166 text + unbreakable_text.substr(0, unbreakable_text.size() / 2))); |
| 167 gfx::Size label_preferred_size = label.GetPreferredSize(); |
| 168 int pref_height = styled()->GetHeightForWidth(label_preferred_size.width()); |
| 169 EXPECT_EQ(label_preferred_size.height() * 2, |
| 170 pref_height - styled()->GetInsets().height()); |
| 171 |
| 172 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height); |
| 173 styled()->Layout(); |
| 174 ASSERT_EQ(2, styled()->child_count()); |
| 175 EXPECT_EQ(0, styled()->child_at(0)->bounds().x()); |
| 176 EXPECT_EQ(0, styled()->child_at(1)->bounds().x()); |
| 177 } |
| 178 |
| 179 TEST_F(StyledLabelTest, StyledRangeUnderlined) { |
| 180 const std::string text("This is a test block of text, "); |
| 181 const std::string underlined_text("and this should be undelined"); |
| 182 InitStyledLabel(text + underlined_text); |
| 183 StyledLabel::RangeStyleInfo style_info; |
| 184 style_info.font_style = gfx::Font::UNDERLINE; |
| 185 styled()->AddStyleRange( |
| 186 ui::Range(text.size(), text.size() + underlined_text.size()), |
| 187 style_info); |
| 188 |
| 189 styled()->SetBounds(0, 0, 1000, 1000); |
| 190 styled()->Layout(); |
| 191 |
| 192 ASSERT_EQ(2, styled()->child_count()); |
| 193 ASSERT_EQ(Label::kViewClassName, styled()->child_at(1)->GetClassName()); |
| 194 EXPECT_EQ(gfx::Font::UNDERLINE, |
| 195 static_cast<Label*>(styled()->child_at(1))->font().GetStyle()); |
| 196 } |
| 197 |
| 198 TEST_F(StyledLabelTest, StyledRangeBold) { |
| 199 const std::string bold_text( |
| 200 "This is a block of text whose style will be set to BOLD in the test"); |
| 201 const std::string text(" normal text"); |
| 202 InitStyledLabel(bold_text + text); |
| 203 |
| 204 StyledLabel::RangeStyleInfo style_info; |
| 205 style_info.font_style = gfx::Font::BOLD; |
| 206 styled()->AddStyleRange(ui::Range(0, bold_text.size()), style_info); |
| 207 |
| 208 // Calculate the bold text width if it were a pure label view, both with bold |
| 209 // and normal style. |
| 210 Label label(ASCIIToUTF16(bold_text)); |
| 211 const gfx::Size normal_label_size = label.GetPreferredSize(); |
| 212 label.SetFont(label.font().DeriveFont(0, gfx::Font::BOLD)); |
| 213 const gfx::Size bold_label_size = label.GetPreferredSize(); |
| 214 |
| 215 ASSERT_GE(bold_label_size.width(), normal_label_size.width()); |
| 216 |
| 217 // Set the width so |bold_text| doesn't fit on a single line with bold style, |
| 218 // but does with normal font style. |
| 219 int styled_width = (normal_label_size.width() + bold_label_size.width()) / 2; |
| 220 int pref_height = styled()->GetHeightForWidth(styled_width); |
| 221 |
| 222 // Sanity check that |bold_text| with normal font style would fit on a single |
| 223 // line in a styled label with width |styled_width|. |
| 224 StyledLabel unstyled(ASCIIToUTF16(bold_text), this); |
| 225 unstyled.SetBounds(0, 0, styled_width, pref_height); |
| 226 unstyled.Layout(); |
| 227 ASSERT_EQ(1, unstyled.child_count()); |
| 228 |
| 229 styled()->SetBounds(0, 0, styled_width, pref_height); |
| 230 styled()->Layout(); |
| 231 |
| 232 ASSERT_EQ(3, styled()->child_count()); |
| 233 |
| 234 // The bold text should be broken up into two parts. |
| 235 ASSERT_EQ(Label::kViewClassName, styled()->child_at(0)->GetClassName()); |
| 236 EXPECT_EQ(gfx::Font::BOLD, |
| 237 static_cast<Label*>(styled()->child_at(0))->font().GetStyle()); |
| 238 ASSERT_EQ(Label::kViewClassName, styled()->child_at(1)->GetClassName()); |
| 239 EXPECT_EQ(gfx::Font::BOLD, |
| 240 static_cast<Label*>(styled()->child_at(1))->font().GetStyle()); |
| 241 ASSERT_EQ(Label::kViewClassName, styled()->child_at(2)->GetClassName()); |
| 242 EXPECT_EQ(gfx::Font::NORMAL, |
| 243 static_cast<Label*>(styled()->child_at(2))->font().GetStyle()); |
| 244 |
| 245 // The second bold part should start on a new line. |
| 246 EXPECT_EQ(0, styled()->child_at(0)->bounds().x()); |
| 247 EXPECT_EQ(0, styled()->child_at(1)->bounds().x()); |
| 248 EXPECT_EQ(styled()->child_at(1)->bounds().right() - 2, |
| 249 styled()->child_at(2)->bounds().x()); |
| 250 } |
| 251 |
| 252 TEST_F(StyledLabelTest, StyledRangeWithTooltip) { |
| 253 const std::string text("This is a test block of text, "); |
| 254 const std::string tooltip_text("this should have a tooltip,"); |
| 255 const std::string normal_text(" this should not have a tooltip, "); |
| 256 const std::string link_text("and this should be a link"); |
| 257 |
| 258 const size_t tooltip_start = text.size(); |
| 259 const size_t link_start = |
| 260 text.size() + tooltip_text.size() + normal_text.size(); |
| 261 |
| 262 InitStyledLabel(text + tooltip_text + normal_text + link_text); |
| 263 StyledLabel::RangeStyleInfo tooltip_style; |
| 264 tooltip_style.tooltip = ASCIIToUTF16("tooltip"); |
| 265 styled()->AddStyleRange( |
| 266 ui::Range(tooltip_start, tooltip_start + tooltip_text.size()), |
| 267 tooltip_style); |
| 268 styled()->AddStyleRange(ui::Range(link_start, link_start + link_text.size()), |
| 269 StyledLabel::RangeStyleInfo::CreateForLink()); |
| 270 |
| 271 // Break line inside the range with the tooltip. |
| 272 Label label(ASCIIToUTF16( |
| 273 text + tooltip_text.substr(0, tooltip_text.size() - 3))); |
| 274 gfx::Size label_preferred_size = label.GetPreferredSize(); |
| 275 int pref_height = styled()->GetHeightForWidth(label_preferred_size.width()); |
| 276 EXPECT_EQ(label_preferred_size.height() * 3, |
| 277 pref_height - styled()->GetInsets().height()); |
| 278 |
| 279 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height); |
| 280 styled()->Layout(); |
| 281 |
| 282 EXPECT_EQ(label_preferred_size.width(), styled()->width()); |
| 283 |
| 284 ASSERT_EQ(5, styled()->child_count()); |
| 285 EXPECT_EQ(0, styled()->child_at(0)->bounds().x()); |
| 286 EXPECT_EQ(styled()->child_at(0)->bounds().right() - 2, |
| 287 styled()->child_at(1)->bounds().x()); |
| 288 EXPECT_EQ(0, styled()->child_at(2)->bounds().x()); |
| 289 EXPECT_EQ(styled()->child_at(2)->bounds().right() - 2, |
| 290 styled()->child_at(3)->bounds().x()); |
| 291 EXPECT_EQ(0, styled()->child_at(4)->bounds().x()); |
| 292 |
| 293 string16 tooltip; |
| 294 EXPECT_TRUE( |
| 295 styled()->child_at(1)->GetTooltipText(gfx::Point(1, 1), &tooltip)); |
| 296 EXPECT_EQ(ASCIIToUTF16("tooltip"), tooltip); |
| 297 EXPECT_TRUE( |
| 298 styled()->child_at(2)->GetTooltipText(gfx::Point(1, 1), &tooltip)); |
| 299 EXPECT_EQ(ASCIIToUTF16("tooltip"), tooltip); |
| 300 } |
| 301 |
107 TEST_F(StyledLabelTest, HandleEmptyLayout) { | 302 TEST_F(StyledLabelTest, HandleEmptyLayout) { |
108 const std::string text("This is a test block of text, "); | 303 const std::string text("This is a test block of text, "); |
109 InitStyledLabel(text); | 304 InitStyledLabel(text); |
110 styled()->Layout(); | 305 styled()->Layout(); |
111 ASSERT_EQ(0, styled()->child_count()); | 306 ASSERT_EQ(0, styled()->child_count()); |
112 } | 307 } |
113 | 308 |
114 } // namespace | 309 } // namespace |
OLD | NEW |