Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(436)

Unified Diff: ui/views/controls/styled_label_unittest.cc

Issue 12906002: Add ability to defined ranges with different styles in StyledLabel (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« ui/views/controls/styled_label.cc ('K') | « ui/views/controls/styled_label.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/views/controls/styled_label_unittest.cc
diff --git a/ui/views/controls/styled_label_unittest.cc b/ui/views/controls/styled_label_unittest.cc
index e3462b2fb97434e6b61aa41d76b434f6197f04ca..7d88a318587db6fccf1906e0e7b1c526d851e73b 100644
--- a/ui/views/controls/styled_label_unittest.cc
+++ b/ui/views/controls/styled_label_unittest.cc
@@ -49,6 +49,30 @@ TEST_F(StyledLabelTest, NoWrapping) {
StyledLabelContentHeightForWidth(label_preferred_size.width() * 2));
}
+TEST_F(StyledLabelTest, TrailingWhitespaceiIgnored) {
+ const std::string text("This is a test block of text ");
+ InitStyledLabel(text);
+
+ styled()->SetBounds(0, 0, 1000, 1000);
+ styled()->Layout();
+
+ ASSERT_EQ(Label::kViewClassName, styled()->child_at(0)->GetClassName());
sky 2013/03/28 16:02:09 Don't you need an assert on on child_count() (same
tbarzic 2013/03/28 20:33:58 yeah, I forgot to add them in these two.
+ EXPECT_EQ(ASCIIToUTF16("This is a test block of text"),
+ static_cast<Label*>(styled()->child_at(0))->text());
+}
+
+TEST_F(StyledLabelTest, LeadingWhitespaceiIgnored) {
+ const std::string text(" This is a test block of text");
+ InitStyledLabel(text);
+
+ styled()->SetBounds(0, 0, 1000, 1000);
+ styled()->Layout();
+
+ ASSERT_EQ(Label::kViewClassName, styled()->child_at(0)->GetClassName());
+ EXPECT_EQ(ASCIIToUTF16("This is a test block of text"),
+ static_cast<Label*>(styled()->child_at(0))->text());
+}
+
TEST_F(StyledLabelTest, BasicWrapping) {
const std::string text("This is a test block of text");
InitStyledLabel(text);
@@ -104,6 +128,145 @@ TEST_F(StyledLabelTest, DontBreakLinks) {
EXPECT_EQ(0, styled()->child_at(1)->bounds().x());
}
+TEST_F(StyledLabelTest, StyledRangeWithDisabledLineWrapping) {
+ const std::string text("This is a test block of text, ");
+ const std::string unbreakable_text("and this should not be breaked");
+ InitStyledLabel(text + unbreakable_text);
+ StyledLabel::RangeStyleInfo style_info;
+ style_info.disable_line_wrapping = true;
+ styled()->AddStyleRange(
+ ui::Range(text.size(), text.size() + unbreakable_text.size()),
+ style_info);
+
+ Label label(ASCIIToUTF16(
+ text + unbreakable_text.substr(0, unbreakable_text.size() / 2)));
+ gfx::Size label_preferred_size = label.GetPreferredSize();
+ int pref_height = styled()->GetHeightForWidth(label_preferred_size.width());
+ EXPECT_EQ(label_preferred_size.height() * 2,
+ pref_height - styled()->GetInsets().height());
+
+ styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height);
+ styled()->Layout();
+ ASSERT_EQ(2, styled()->child_count());
+ EXPECT_EQ(0, styled()->child_at(0)->bounds().x());
+ EXPECT_EQ(0, styled()->child_at(1)->bounds().x());
+}
+
+TEST_F(StyledLabelTest, StyledRangeUnderlined) {
+ const std::string text("This is a test block of text, ");
+ const std::string underlined_text("and this should be undelined");
+ InitStyledLabel(text + underlined_text);
+ StyledLabel::RangeStyleInfo style_info;
+ style_info.font_style = gfx::Font::UNDERLINE;
+ styled()->AddStyleRange(
+ ui::Range(text.size(), text.size() + underlined_text.size()),
+ style_info);
+
+ styled()->SetBounds(0, 0, 1000, 1000);
+ styled()->Layout();
+
+ ASSERT_EQ(2, styled()->child_count());
+ ASSERT_EQ(Label::kViewClassName, styled()->child_at(1)->GetClassName());
+ EXPECT_EQ(gfx::Font::UNDERLINE,
+ static_cast<Label*>(styled()->child_at(1))->font().GetStyle());
+}
+
+TEST_F(StyledLabelTest, StyledRangeBold) {
+ const std::string bold_text(
+ "This is a block of text whose style will be set to BOLD in the test");
+ const std::string text(" normal text");
+ InitStyledLabel(bold_text + text);
+
+ StyledLabel::RangeStyleInfo style_info;
+ style_info.font_style = gfx::Font::BOLD;
+ styled()->AddStyleRange(ui::Range(0, bold_text.size()), style_info);
+
+ // Calculate the bold text width if it were a pure label view, both with bold
+ // and normal style.
+ Label label(ASCIIToUTF16(bold_text));
+ const gfx::Size normal_label_size = label.GetPreferredSize();
+ label.SetFont(label.font().DeriveFont(0, gfx::Font::BOLD));
+ const gfx::Size bold_label_size = label.GetPreferredSize();
+
+ ASSERT_GE(bold_label_size.width(), normal_label_size.width());
+
+ // Set the width so |bold_text| doesn't fit in a single line with bold style,
+ // but does with normal font style.
Evan Stade 2013/03/27 22:25:11 can you have the test make sure the second asserti
tbarzic 2013/03/28 20:33:58 Done.
+ int styled_width = (normal_label_size.width() + bold_label_size.width()) / 2;
+ int pref_height = styled()->GetHeightForWidth(styled_width);
+
+ styled()->SetBounds(0, 0, styled_width, pref_height);
+ styled()->Layout();
+
+ ASSERT_EQ(3, styled()->child_count());
+
+ // The bold text should be broken up into two parts.
+ ASSERT_EQ(Label::kViewClassName, styled()->child_at(0)->GetClassName());
+ EXPECT_EQ(gfx::Font::BOLD,
+ static_cast<Label*>(styled()->child_at(0))->font().GetStyle());
+ ASSERT_EQ(Label::kViewClassName, styled()->child_at(1)->GetClassName());
+ EXPECT_EQ(gfx::Font::BOLD,
+ static_cast<Label*>(styled()->child_at(1))->font().GetStyle());
+ ASSERT_EQ(Label::kViewClassName, styled()->child_at(2)->GetClassName());
+ EXPECT_EQ(gfx::Font::NORMAL,
+ static_cast<Label*>(styled()->child_at(2))->font().GetStyle());
+
+ // The second bold part should start in a new line.
Evan Stade 2013/03/27 22:25:11 nit: on a new line.
tbarzic 2013/03/28 20:33:58 Done.
+ EXPECT_EQ(0, styled()->child_at(0)->bounds().x());
+ EXPECT_EQ(0, styled()->child_at(1)->bounds().x());
+ EXPECT_EQ(styled()->child_at(1)->bounds().right() - 2,
+ styled()->child_at(2)->bounds().x());
+}
+
+TEST_F(StyledLabelTest, StyledRangeWithTooltip) {
+ const std::string text("This is a test block of text, ");
+ const std::string tooltip_text("this should have a tooltip,");
+ const std::string normal_text(" this should not have a tooltip, ");
+ const std::string link_text("and this should be a link");
+
+ const size_t tooltip_start = text.size();
+ const size_t link_start =
+ text.size() + tooltip_text.size() + normal_text.size();
+
+ InitStyledLabel(text + tooltip_text + normal_text + link_text);
+ StyledLabel::RangeStyleInfo tooltip_style;
+ tooltip_style.tooltip = ASCIIToUTF16("tooltip");
+ styled()->AddStyleRange(
+ ui::Range(tooltip_start, tooltip_start + tooltip_text.size()),
+ tooltip_style);
+ styled()->AddLink(ui::Range(link_start, link_start + link_text.size()));
+
+ // Break line inside the range with the tooltip.
+ Label label(ASCIIToUTF16(
+ text + tooltip_text.substr(0, tooltip_text.size() - 3)));
+ gfx::Size label_preferred_size = label.GetPreferredSize();
+ int pref_height = styled()->GetHeightForWidth(label_preferred_size.width());
+ EXPECT_EQ(label_preferred_size.height() * 3,
+ pref_height - styled()->GetInsets().height());
+
+ styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height);
+ styled()->Layout();
+
+ EXPECT_EQ(label_preferred_size.width(), styled()->width());
+
+ ASSERT_EQ(5, styled()->child_count());
+ EXPECT_EQ(0, styled()->child_at(0)->bounds().x());
+ EXPECT_EQ(styled()->child_at(0)->bounds().right() - 2,
+ styled()->child_at(1)->bounds().x());
+ EXPECT_EQ(0, styled()->child_at(2)->bounds().x());
+ EXPECT_EQ(styled()->child_at(2)->bounds().right() - 2,
+ styled()->child_at(3)->bounds().x());
+ EXPECT_EQ(0, styled()->child_at(4)->bounds().x());
+
+ string16 tooltip;
+ EXPECT_TRUE(
+ styled()->child_at(1)->GetTooltipText(gfx::Point(1, 1), &tooltip));
+ EXPECT_EQ(ASCIIToUTF16("tooltip"), tooltip);
+ EXPECT_TRUE(
+ styled()->child_at(2)->GetTooltipText(gfx::Point(1, 1), &tooltip));
+ EXPECT_EQ(ASCIIToUTF16("tooltip"), tooltip);
+}
+
TEST_F(StyledLabelTest, HandleEmptyLayout) {
const std::string text("This is a test block of text, ");
InitStyledLabel(text);
« ui/views/controls/styled_label.cc ('K') | « ui/views/controls/styled_label.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698