summaryrefslogtreecommitdiffstats
path: root/chromium/ui/views/controls/styled_label_unittest.cc
diff options
context:
space:
mode:
Diffstat (limited to 'chromium/ui/views/controls/styled_label_unittest.cc')
-rw-r--r--chromium/ui/views/controls/styled_label_unittest.cc161
1 files changed, 103 insertions, 58 deletions
diff --git a/chromium/ui/views/controls/styled_label_unittest.cc b/chromium/ui/views/controls/styled_label_unittest.cc
index 18595edf3d5..d360aedb1b0 100644
--- a/chromium/ui/views/controls/styled_label_unittest.cc
+++ b/chromium/ui/views/controls/styled_label_unittest.cc
@@ -9,14 +9,19 @@
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkColor.h"
+#include "ui/gfx/font_list.h"
#include "ui/views/border.h"
#include "ui/views/controls/link.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/controls/styled_label_listener.h"
+#include "ui/views/test/views_test_base.h"
+#include "ui/views/widget/widget.h"
+
+using base::ASCIIToUTF16;
namespace views {
-class StyledLabelTest : public testing::Test, public StyledLabelListener {
+class StyledLabelTest : public ViewsTestBase, public StyledLabelListener {
public:
StyledLabelTest() {}
virtual ~StyledLabelTest() {}
@@ -30,6 +35,7 @@ class StyledLabelTest : public testing::Test, public StyledLabelListener {
void InitStyledLabel(const std::string& ascii_text) {
styled_.reset(new StyledLabel(ASCIIToUTF16(ascii_text), this));
+ styled_->set_owned_by_client();
}
int StyledLabelContentHeightForWidth(int w) {
@@ -105,7 +111,7 @@ TEST_F(StyledLabelTest, BasicWrapping) {
StyledLabelContentHeightForWidth(label_preferred_size.width()));
// Also respect the border.
- styled()->set_border(Border::CreateEmptyBorder(3, 3, 3, 3));
+ styled()->SetBorder(Border::CreateEmptyBorder(3, 3, 3, 3));
styled()->SetBounds(
0,
0,
@@ -113,15 +119,19 @@ TEST_F(StyledLabelTest, BasicWrapping) {
styled()->GetInsets().height() + 2 * label_preferred_size.height());
styled()->Layout();
ASSERT_EQ(2, styled()->child_count());
- EXPECT_EQ(3, styled()->child_at(0)->bounds().x());
- EXPECT_EQ(3, styled()->child_at(0)->bounds().y());
- EXPECT_EQ(styled()->bounds().height() - 3,
- styled()->child_at(1)->bounds().bottom());
+ EXPECT_EQ(3, styled()->child_at(0)->x());
+ EXPECT_EQ(3, styled()->child_at(0)->y());
+ EXPECT_EQ(styled()->height() - 3, styled()->child_at(1)->bounds().bottom());
}
TEST_F(StyledLabelTest, CreateLinks) {
const std::string text("This is a test block of text.");
InitStyledLabel(text);
+
+ // Without links, there should be no focus border.
+ EXPECT_TRUE(styled()->GetInsets().empty());
+
+ // Now let's add some links.
styled()->AddStyleRange(gfx::Range(0, 1),
StyledLabel::RangeStyleInfo::CreateForLink());
styled()->AddStyleRange(gfx::Range(1, 2),
@@ -131,9 +141,13 @@ TEST_F(StyledLabelTest, CreateLinks) {
styled()->AddStyleRange(gfx::Range(12, 13),
StyledLabel::RangeStyleInfo::CreateForLink());
+ // Now there should be a focus border because there are non-empty Links.
+ EXPECT_FALSE(styled()->GetInsets().empty());
+
+ // Verify layout creates the right number of children.
styled()->SetBounds(0, 0, 1000, 1000);
styled()->Layout();
- ASSERT_EQ(7, styled()->child_count());
+ EXPECT_EQ(7, styled()->child_count());
}
TEST_F(StyledLabelTest, DontBreakLinks) {
@@ -153,13 +167,15 @@ TEST_F(StyledLabelTest, DontBreakLinks) {
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());
+ // The label has no focus border while the link (and thus overall styled
+ // label) does, so the label should be inset by the width of the focus border.
+ EXPECT_EQ(Label::kFocusBorderPadding, styled()->child_at(0)->x());
+ EXPECT_EQ(0, styled()->child_at(1)->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");
+ const std::string unbreakable_text("and this should not be broken");
InitStyledLabel(text + unbreakable_text);
StyledLabel::RangeStyleInfo style_info;
style_info.disable_line_wrapping = true;
@@ -177,8 +193,8 @@ TEST_F(StyledLabelTest, StyledRangeWithDisabledLineWrapping) {
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());
+ EXPECT_EQ(0, styled()->child_at(0)->x());
+ EXPECT_EQ(0, styled()->child_at(1)->x());
}
TEST_F(StyledLabelTest, StyledRangeUnderlined) {
@@ -197,8 +213,9 @@ TEST_F(StyledLabelTest, StyledRangeUnderlined) {
ASSERT_EQ(2, styled()->child_count());
ASSERT_EQ(std::string(Label::kViewClassName),
styled()->child_at(1)->GetClassName());
- EXPECT_EQ(gfx::Font::UNDERLINE,
- static_cast<Label*>(styled()->child_at(1))->font().GetStyle());
+ EXPECT_EQ(
+ gfx::Font::UNDERLINE,
+ static_cast<Label*>(styled()->child_at(1))->font_list().GetFontStyle());
}
TEST_F(StyledLabelTest, StyledRangeBold) {
@@ -215,7 +232,7 @@ TEST_F(StyledLabelTest, StyledRangeBold) {
// 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));
+ label.SetFontList(label.font_list().DeriveWithStyle(gfx::Font::BOLD));
const gfx::Size bold_label_size = label.GetPreferredSize();
ASSERT_GE(bold_label_size.width(), normal_label_size.width());
@@ -230,7 +247,7 @@ TEST_F(StyledLabelTest, StyledRangeBold) {
StyledLabel unstyled(ASCIIToUTF16(bold_text), this);
unstyled.SetBounds(0, 0, styled_width, pref_height);
unstyled.Layout();
- ASSERT_EQ(1, unstyled.child_count());
+ EXPECT_EQ(1, unstyled.child_count());
styled()->SetBounds(0, 0, styled_width, pref_height);
styled()->Layout();
@@ -240,22 +257,25 @@ TEST_F(StyledLabelTest, StyledRangeBold) {
// The bold text should be broken up into two parts.
ASSERT_EQ(std::string(Label::kViewClassName),
styled()->child_at(0)->GetClassName());
- EXPECT_EQ(gfx::Font::BOLD,
- static_cast<Label*>(styled()->child_at(0))->font().GetStyle());
+ EXPECT_EQ(
+ gfx::Font::BOLD,
+ static_cast<Label*>(styled()->child_at(0))->font_list().GetFontStyle());
ASSERT_EQ(std::string(Label::kViewClassName),
styled()->child_at(1)->GetClassName());
- EXPECT_EQ(gfx::Font::BOLD,
- static_cast<Label*>(styled()->child_at(1))->font().GetStyle());
+ EXPECT_EQ(
+ gfx::Font::BOLD,
+ static_cast<Label*>(styled()->child_at(1))->font_list().GetFontStyle());
ASSERT_EQ(std::string(Label::kViewClassName),
styled()->child_at(2)->GetClassName());
- EXPECT_EQ(gfx::Font::NORMAL,
- static_cast<Label*>(styled()->child_at(2))->font().GetStyle());
+ EXPECT_EQ(
+ gfx::Font::NORMAL,
+ static_cast<Label*>(styled()->child_at(2))->font_list().GetFontStyle());
// The second bold part should start on a new line.
- 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());
+ EXPECT_EQ(0, styled()->child_at(0)->x());
+ EXPECT_EQ(0, styled()->child_at(1)->x());
+ EXPECT_EQ(styled()->child_at(1)->bounds().right(),
+ styled()->child_at(2)->x());
}
TEST_F(StyledLabelTest, Color) {
@@ -274,16 +294,25 @@ TEST_F(StyledLabelTest, Color) {
text_red.size() + text_link.size()),
style_info_link);
+ styled()->SetBounds(0, 0, 1000, 1000);
+ styled()->Layout();
+
+ Widget* widget = new Widget();
+ Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
+ widget->Init(params);
+ View* container = new View();
+ widget->SetContentsView(container);
+ container->AddChildView(styled());
+
// Obtain the default text color for a label.
- Label label(ASCIIToUTF16(text));
- const SkColor kDefaultTextColor = label.enabled_color();
+ Label* label = new Label(ASCIIToUTF16(text));
+ container->AddChildView(label);
+ const SkColor kDefaultTextColor = label->enabled_color();
// Obtain the default text color for a link;
- Link link(ASCIIToUTF16(text_link));
- const SkColor kDefaultLinkColor = link.enabled_color();
-
- styled()->SetBounds(0, 0, 1000, 1000);
- styled()->Layout();
+ Link* link = new Link(ASCIIToUTF16(text_link));
+ container->AddChildView(link);
+ const SkColor kDefaultLinkColor = link->enabled_color();
EXPECT_EQ(SK_ColorRED,
static_cast<Label*>(styled()->child_at(0))->enabled_color());
@@ -291,23 +320,18 @@ TEST_F(StyledLabelTest, Color) {
static_cast<Label*>(styled()->child_at(1))->enabled_color());
EXPECT_EQ(kDefaultTextColor,
static_cast<Label*>(styled()->child_at(2))->enabled_color());
-}
-TEST_F(StyledLabelTest, ColorReadability) {
- const std::string text(
- "This is a block of text that needs color adjustment.");
- InitStyledLabel(text);
+ // Test adjusted color readability.
styled()->SetDisplayedOnBackgroundColor(SK_ColorBLACK);
-
- // Obtain the text color if it were a pure label.
- Label label(ASCIIToUTF16(text));
- label.SetBackgroundColor(SK_ColorBLACK);
-
- styled()->SetBounds(0, 0, 1000, 1000);
styled()->Layout();
+ label->SetBackgroundColor(SK_ColorBLACK);
- EXPECT_EQ(label.enabled_color(),
- static_cast<Label*>(styled()->child_at(0))->enabled_color());
+ const SkColor kAdjustedTextColor = label->enabled_color();
+ EXPECT_NE(kAdjustedTextColor, kDefaultTextColor);
+ EXPECT_EQ(kAdjustedTextColor,
+ static_cast<Label*>(styled()->child_at(2))->enabled_color());
+
+ widget->CloseNow();
}
TEST_F(StyledLabelTest, StyledRangeWithTooltip) {
@@ -343,15 +367,18 @@ TEST_F(StyledLabelTest, StyledRangeWithTooltip) {
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;
+ // The labels have no focus border while the link (and thus overall styled
+ // label) does, so the labels should be inset by the width of the focus
+ // border.
+ EXPECT_EQ(Label::kFocusBorderPadding, styled()->child_at(0)->x());
+ EXPECT_EQ(styled()->child_at(0)->bounds().right(),
+ styled()->child_at(1)->x());
+ EXPECT_EQ(Label::kFocusBorderPadding, styled()->child_at(2)->x());
+ EXPECT_EQ(styled()->child_at(2)->bounds().right(),
+ styled()->child_at(3)->x());
+ EXPECT_EQ(0, styled()->child_at(4)->x());
+
+ base::string16 tooltip;
EXPECT_TRUE(
styled()->child_at(1)->GetTooltipText(gfx::Point(1, 1), &tooltip));
EXPECT_EQ(ASCIIToUTF16("tooltip"), tooltip);
@@ -360,11 +387,29 @@ TEST_F(StyledLabelTest, StyledRangeWithTooltip) {
EXPECT_EQ(ASCIIToUTF16("tooltip"), tooltip);
}
+TEST_F(StyledLabelTest, SetBaseFontList) {
+ const std::string text("This is a test block of text.");
+ InitStyledLabel(text);
+ std::string font_name("arial");
+ gfx::Font font(font_name, 30);
+ styled()->SetBaseFontList(gfx::FontList(font));
+ Label label(ASCIIToUTF16(text), gfx::FontList(font));
+
+ styled()->SetBounds(0,
+ 0,
+ label.GetPreferredSize().width(),
+ label.GetPreferredSize().height());
+
+ // Make sure we have the same sizing as a label.
+ EXPECT_EQ(label.GetPreferredSize().height(), styled()->height());
+ EXPECT_EQ(label.GetPreferredSize().width(), styled()->width());
+}
+
TEST_F(StyledLabelTest, HandleEmptyLayout) {
- const std::string text("This is a test block of text, ");
+ const std::string text("This is a test block of text.");
InitStyledLabel(text);
styled()->Layout();
- ASSERT_EQ(0, styled()->child_count());
+ EXPECT_EQ(0, styled()->child_count());
}
-} // namespace
+} // namespace views