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, TrailingWhitespace) { |
| 53 const std::string text("This is a test block of text "); |
| 54 InitStyledLabel(text); |
| 55 Label label(ASCIIToUTF16(text)); |
| 56 const gfx::Size label_preferred_size = label.GetPreferredSize(); |
| 57 EXPECT_EQ(label_preferred_size.height(), |
| 58 StyledLabelContentHeightForWidth(label_preferred_size.width() * 2)); |
| 59 } |
| 60 |
52 TEST_F(StyledLabelTest, BasicWrapping) { | 61 TEST_F(StyledLabelTest, BasicWrapping) { |
53 const std::string text("This is a test block of text"); | 62 const std::string text("This is a test block of text"); |
54 InitStyledLabel(text); | 63 InitStyledLabel(text); |
55 Label label(ASCIIToUTF16(text.substr(0, text.size() * 2 / 3))); | 64 Label label(ASCIIToUTF16(text.substr(0, text.size() * 2 / 3))); |
56 gfx::Size label_preferred_size = label.GetPreferredSize(); | 65 gfx::Size label_preferred_size = label.GetPreferredSize(); |
57 EXPECT_EQ(label_preferred_size.height() * 2, | 66 EXPECT_EQ(label_preferred_size.height() * 2, |
58 StyledLabelContentHeightForWidth(label_preferred_size.width())); | 67 StyledLabelContentHeightForWidth(label_preferred_size.width())); |
59 | 68 |
60 // Also respect the border. | 69 // Also respect the border. |
61 styled()->set_border(Border::CreateEmptyBorder(3, 3, 3, 3)); | 70 styled()->set_border(Border::CreateEmptyBorder(3, 3, 3, 3)); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 EXPECT_EQ(label_preferred_size.height() * 2, | 106 EXPECT_EQ(label_preferred_size.height() * 2, |
98 pref_height - styled()->GetInsets().height()); | 107 pref_height - styled()->GetInsets().height()); |
99 | 108 |
100 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height); | 109 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height); |
101 styled()->Layout(); | 110 styled()->Layout(); |
102 ASSERT_EQ(2, styled()->child_count()); | 111 ASSERT_EQ(2, styled()->child_count()); |
103 EXPECT_EQ(0, styled()->child_at(0)->bounds().x()); | 112 EXPECT_EQ(0, styled()->child_at(0)->bounds().x()); |
104 EXPECT_EQ(0, styled()->child_at(1)->bounds().x()); | 113 EXPECT_EQ(0, styled()->child_at(1)->bounds().x()); |
105 } | 114 } |
106 | 115 |
| 116 TEST_F(StyledLabelTest, StyledRangeWithDisabledLineWrapping) { |
| 117 const std::string text("This is a test block of text, "); |
| 118 const std::string unbreakable_text("and this should not be breaked"); |
| 119 InitStyledLabel(text + unbreakable_text); |
| 120 StyledLabel::RangeStyleInfo style_info; |
| 121 style_info.disable_line_wrapping = true; |
| 122 styled()->AddStyleRange( |
| 123 ui::Range(text.size(), text.size() + unbreakable_text.size()), |
| 124 style_info); |
| 125 |
| 126 Label label(ASCIIToUTF16( |
| 127 text + unbreakable_text.substr(0, unbreakable_text.size() / 2))); |
| 128 gfx::Size label_preferred_size = label.GetPreferredSize(); |
| 129 int pref_height = styled()->GetHeightForWidth(label_preferred_size.width()); |
| 130 EXPECT_EQ(label_preferred_size.height() * 2, |
| 131 pref_height - styled()->GetInsets().height()); |
| 132 |
| 133 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height); |
| 134 styled()->Layout(); |
| 135 ASSERT_EQ(2, styled()->child_count()); |
| 136 EXPECT_EQ(0, styled()->child_at(0)->bounds().x()); |
| 137 EXPECT_EQ(0, styled()->child_at(1)->bounds().x()); |
| 138 } |
| 139 |
| 140 TEST_F(StyledLabelTest, StyledRangeUnderlined) { |
| 141 const std::string text("This is a test block of text, "); |
| 142 const std::string underlined_text("and this should be undelined"); |
| 143 InitStyledLabel(text + underlined_text); |
| 144 StyledLabel::RangeStyleInfo style_info; |
| 145 style_info.font_style = gfx::Font::UNDERLINE; |
| 146 styled()->AddStyleRange( |
| 147 ui::Range(text.size(), text.size() + underlined_text.size()), |
| 148 style_info); |
| 149 |
| 150 styled()->SetBounds(0, 0, 1000, 1000); |
| 151 styled()->Layout(); |
| 152 |
| 153 ASSERT_EQ(2, styled()->child_count()); |
| 154 ASSERT_EQ(Label::kViewClassName, styled()->child_at(1)->GetClassName()); |
| 155 EXPECT_EQ(gfx::Font::UNDERLINE, |
| 156 static_cast<Label*>(styled()->child_at(1))->font().GetStyle()); |
| 157 } |
| 158 |
| 159 TEST_F(StyledLabelTest, StyledRangeWithTooltip) { |
| 160 const std::string text("This is a test block of text, "); |
| 161 const std::string tooltip_text("this should have a tooltip,"); |
| 162 const std::string normal_text(" this should not have a tooltip, "); |
| 163 const std::string link_text("and this should be a link"); |
| 164 |
| 165 const size_t tooltip_start = text.size(); |
| 166 const size_t link_start = |
| 167 text.size() + tooltip_text.size() + normal_text.size(); |
| 168 |
| 169 InitStyledLabel(text + tooltip_text + normal_text + link_text); |
| 170 StyledLabel::RangeStyleInfo tooltip_style; |
| 171 tooltip_style.tooltip = ASCIIToUTF16("tooltip"); |
| 172 styled()->AddStyleRange( |
| 173 ui::Range(tooltip_start, tooltip_start + tooltip_text.size()), |
| 174 tooltip_style); |
| 175 styled()->AddLink(ui::Range(link_start, link_start + link_text.size())); |
| 176 |
| 177 // Break line inside the range with the tooltip. |
| 178 Label label(ASCIIToUTF16( |
| 179 text + tooltip_text.substr(0, tooltip_text.size() - 3))); |
| 180 gfx::Size label_preferred_size = label.GetPreferredSize(); |
| 181 int pref_height = styled()->GetHeightForWidth(label_preferred_size.width()); |
| 182 EXPECT_EQ(label_preferred_size.height() * 3, |
| 183 pref_height - styled()->GetInsets().height()); |
| 184 |
| 185 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height); |
| 186 styled()->Layout(); |
| 187 |
| 188 EXPECT_EQ(label_preferred_size.width(), styled()->width()); |
| 189 |
| 190 ASSERT_EQ(5, styled()->child_count()); |
| 191 EXPECT_EQ(0, styled()->child_at(0)->bounds().x()); |
| 192 EXPECT_EQ(styled()->child_at(0)->bounds().right() - 2, |
| 193 styled()->child_at(1)->bounds().x()); |
| 194 EXPECT_EQ(0, styled()->child_at(2)->bounds().x()); |
| 195 EXPECT_EQ(styled()->child_at(2)->bounds().right() - 2, |
| 196 styled()->child_at(3)->bounds().x()); |
| 197 EXPECT_EQ(0, styled()->child_at(4)->bounds().x()); |
| 198 |
| 199 string16 tooltip; |
| 200 EXPECT_TRUE( |
| 201 styled()->child_at(1)->GetTooltipText(gfx::Point(1, 1), &tooltip)); |
| 202 EXPECT_EQ(ASCIIToUTF16("tooltip"), tooltip); |
| 203 EXPECT_TRUE( |
| 204 styled()->child_at(2)->GetTooltipText(gfx::Point(1, 1), &tooltip)); |
| 205 EXPECT_EQ(ASCIIToUTF16("tooltip"), tooltip); |
| 206 } |
| 207 |
107 TEST_F(StyledLabelTest, HandleEmptyLayout) { | 208 TEST_F(StyledLabelTest, HandleEmptyLayout) { |
108 const std::string text("This is a test block of text, "); | 209 const std::string text("This is a test block of text, "); |
109 InitStyledLabel(text); | 210 InitStyledLabel(text); |
110 styled()->Layout(); | 211 styled()->Layout(); |
111 ASSERT_EQ(0, styled()->child_count()); | 212 ASSERT_EQ(0, styled()->child_count()); |
112 } | 213 } |
113 | 214 |
114 } // namespace | 215 } // namespace |
OLD | NEW |