| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <string> |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/utf_string_conversions.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "ui/views/controls/link.h" |
| 12 #include "ui/views/controls/styled_label.h" |
| 13 #include "ui/views/controls/styled_label_listener.h" |
| 14 |
| 15 namespace views { |
| 16 |
| 17 class StyledLabelTest : public testing::Test, public StyledLabelListener { |
| 18 public: |
| 19 StyledLabelTest() {} |
| 20 virtual ~StyledLabelTest() {} |
| 21 |
| 22 // StyledLabelListener implementation. |
| 23 virtual void StyledLabelLinkClicked(const ui::Range& range, |
| 24 int event_flags) OVERRIDE {} |
| 25 |
| 26 protected: |
| 27 StyledLabel* styled() { return styled_.get(); } |
| 28 |
| 29 void InitStyledLabel(const std::string& ascii_text) { |
| 30 styled_.reset(new StyledLabel(ASCIIToUTF16(ascii_text), this)); |
| 31 } |
| 32 |
| 33 int StyledLabelContentHeightForWidth(int w) { |
| 34 return styled_->GetHeightForWidth(w) - styled_->GetInsets().height(); |
| 35 } |
| 36 |
| 37 private: |
| 38 scoped_ptr<StyledLabel> styled_; |
| 39 |
| 40 DISALLOW_COPY_AND_ASSIGN(StyledLabelTest); |
| 41 }; |
| 42 |
| 43 TEST_F(StyledLabelTest, NoWrapping) { |
| 44 const std::string text("This is a test block of text"); |
| 45 InitStyledLabel(text); |
| 46 Label label(ASCIIToUTF16(text)); |
| 47 const gfx::Size label_preferred_size = label.GetPreferredSize(); |
| 48 EXPECT_EQ(label_preferred_size.height(), |
| 49 StyledLabelContentHeightForWidth(label_preferred_size.width() * 2)); |
| 50 } |
| 51 |
| 52 TEST_F(StyledLabelTest, BasicWrapping) { |
| 53 const std::string text("This is a test block of text"); |
| 54 InitStyledLabel(text); |
| 55 Label label(ASCIIToUTF16(text.substr(0, text.size() * 2 / 3))); |
| 56 gfx::Size label_preferred_size = label.GetPreferredSize(); |
| 57 EXPECT_EQ(label_preferred_size.height() * 2, |
| 58 StyledLabelContentHeightForWidth(label_preferred_size.width())); |
| 59 |
| 60 // Also respect the border. |
| 61 styled()->set_border(Border::CreateEmptyBorder(3, 3, 3, 3)); |
| 62 styled()->SetBounds( |
| 63 0, |
| 64 0, |
| 65 styled()->GetInsets().width() + label_preferred_size.width(), |
| 66 styled()->GetInsets().height() + 2 * label_preferred_size.height()); |
| 67 styled()->Layout(); |
| 68 ASSERT_EQ(2, styled()->child_count()); |
| 69 EXPECT_EQ(3, styled()->child_at(0)->bounds().x()); |
| 70 EXPECT_EQ(3, styled()->child_at(0)->bounds().y()); |
| 71 EXPECT_EQ(styled()->bounds().height() - 3, |
| 72 styled()->child_at(1)->bounds().bottom()); |
| 73 } |
| 74 |
| 75 TEST_F(StyledLabelTest, CreateLinks) { |
| 76 const std::string text("This is a test block of text."); |
| 77 InitStyledLabel(text); |
| 78 styled()->AddLink(ui::Range(0, 1)); |
| 79 styled()->AddLink(ui::Range(1, 2)); |
| 80 styled()->AddLink(ui::Range(10, 11)); |
| 81 styled()->AddLink(ui::Range(12, 13)); |
| 82 |
| 83 styled()->SetBounds(0, 0, 1000, 1000); |
| 84 styled()->Layout(); |
| 85 ASSERT_EQ(7, styled()->child_count()); |
| 86 } |
| 87 |
| 88 TEST_F(StyledLabelTest, DontBreakLinks) { |
| 89 const std::string text("This is a test block of text, "); |
| 90 const std::string link_text("and this should be a link"); |
| 91 InitStyledLabel(text + link_text); |
| 92 styled()->AddLink(ui::Range(text.size(), text.size() + link_text.size())); |
| 93 |
| 94 Label label(ASCIIToUTF16(text + link_text.substr(0, link_text.size() / 2))); |
| 95 gfx::Size label_preferred_size = label.GetPreferredSize(); |
| 96 int pref_height = styled()->GetHeightForWidth(label_preferred_size.width()); |
| 97 EXPECT_EQ(label_preferred_size.height() * 2, |
| 98 pref_height - styled()->GetInsets().height()); |
| 99 |
| 100 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height); |
| 101 styled()->Layout(); |
| 102 ASSERT_EQ(2, styled()->child_count()); |
| 103 EXPECT_EQ(0, styled()->child_at(0)->bounds().x()); |
| 104 EXPECT_EQ(0, styled()->child_at(1)->bounds().x()); |
| 105 } |
| 106 |
| 107 TEST_F(StyledLabelTest, HandleEmptyLayout) { |
| 108 const std::string text("This is a test block of text, "); |
| 109 InitStyledLabel(text); |
| 110 styled()->Layout(); |
| 111 ASSERT_EQ(0, styled()->child_count()); |
| 112 } |
| 113 |
| 114 } // namespace |
| OLD | NEW |