| 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 "ui/views/controls/styled_label.h" | 5 #include "ui/views/controls/styled_label.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "third_party/skia/include/core/SkColor.h" | 15 #include "third_party/skia/include/core/SkColor.h" |
| 16 #include "ui/gfx/font_list.h" | 16 #include "ui/gfx/font_list.h" |
| 17 #include "ui/views/border.h" | 17 #include "ui/views/border.h" |
| 18 #include "ui/views/controls/link.h" | 18 #include "ui/views/controls/link.h" |
| 19 #include "ui/views/controls/styled_label_listener.h" | 19 #include "ui/views/controls/styled_label_listener.h" |
| 20 #include "ui/views/test/views_test_base.h" | 20 #include "ui/views/test/widget_test.h" |
| 21 #include "ui/views/widget/widget.h" | 21 #include "ui/views/widget/widget.h" |
| 22 | 22 |
| 23 using base::ASCIIToUTF16; | 23 using base::ASCIIToUTF16; |
| 24 | 24 |
| 25 namespace views { | 25 namespace views { |
| 26 | 26 |
| 27 class StyledLabelTest : public ViewsTestBase, public StyledLabelListener { | 27 class StyledLabelTest : public test::WidgetTest, public StyledLabelListener { |
| 28 public: | 28 public: |
| 29 StyledLabelTest() {} | 29 StyledLabelTest() {} |
| 30 ~StyledLabelTest() override {} | 30 ~StyledLabelTest() override {} |
| 31 | 31 |
| 32 // StyledLabelListener implementation. | 32 // StyledLabelListener implementation. |
| 33 void StyledLabelLinkClicked(StyledLabel* label, | 33 void StyledLabelLinkClicked(StyledLabel* label, |
| 34 const gfx::Range& range, | 34 const gfx::Range& range, |
| 35 int event_flags) override {} | 35 int event_flags) override {} |
| 36 | 36 |
| 37 protected: | 37 protected: |
| 38 StyledLabel* styled() { return styled_.get(); } | 38 StyledLabel* styled() { return styled_; } |
| 39 | 39 |
| 40 void InitStyledLabel(const std::string& ascii_text) { | 40 void InitStyledLabel(const std::string& ascii_text) { |
| 41 styled_.reset(new StyledLabel(ASCIIToUTF16(ascii_text), this)); | 41 styled_ = new StyledLabel(ASCIIToUTF16(ascii_text), this); |
| 42 styled_->set_owned_by_client(); | 42 widget_->GetContentsView()->RemoveAllChildViews(true); |
| 43 // Since Label::GetInsets() depends on IsFocusable(), add |styled_| to the |
| 44 // view hierarchy. |
| 45 widget_->GetContentsView()->AddChildView(styled_); |
| 43 } | 46 } |
| 44 | 47 |
| 45 int StyledLabelContentHeightForWidth(int w) { | 48 int StyledLabelContentHeightForWidth(int w) { |
| 46 return styled_->GetHeightForWidth(w) - styled_->GetInsets().height(); | 49 return styled_->GetHeightForWidth(w) - styled_->GetInsets().height(); |
| 47 } | 50 } |
| 48 | 51 |
| 52 void SetUp() override { |
| 53 test::WidgetTest::SetUp(); |
| 54 widget_ = CreateTopLevelFramelessPlatformWidget(); |
| 55 widget_->SetContentsView(new View); |
| 56 widget_->Show(); |
| 57 } |
| 58 |
| 59 void TearDown() override { |
| 60 widget_->CloseNow(); |
| 61 test::WidgetTest::TearDown(); |
| 62 } |
| 63 |
| 49 private: | 64 private: |
| 50 std::unique_ptr<StyledLabel> styled_; | 65 Widget* widget_; |
| 66 StyledLabel* styled_; |
| 51 | 67 |
| 52 DISALLOW_COPY_AND_ASSIGN(StyledLabelTest); | 68 DISALLOW_COPY_AND_ASSIGN(StyledLabelTest); |
| 53 }; | 69 }; |
| 54 | 70 |
| 55 TEST_F(StyledLabelTest, NoWrapping) { | 71 TEST_F(StyledLabelTest, NoWrapping) { |
| 56 const std::string text("This is a test block of text"); | 72 const std::string text("This is a test block of text"); |
| 57 InitStyledLabel(text); | 73 InitStyledLabel(text); |
| 58 Label label(ASCIIToUTF16(text)); | 74 Label label(ASCIIToUTF16(text)); |
| 59 const gfx::Size label_preferred_size = label.GetPreferredSize(); | 75 const gfx::Size label_preferred_size = label.GetPreferredSize(); |
| 60 EXPECT_EQ(label_preferred_size.height(), | 76 EXPECT_EQ(label_preferred_size.height(), |
| (...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 // all controls should be recreated | 515 // all controls should be recreated |
| 500 styled()->SetText(another_text); | 516 styled()->SetText(another_text); |
| 501 int updated_height = styled()->GetHeightForWidth(styled()->width()); | 517 int updated_height = styled()->GetHeightForWidth(styled()->width()); |
| 502 EXPECT_NE(updated_height, real_height); | 518 EXPECT_NE(updated_height, real_height); |
| 503 View* first_child_after_text_update = styled()->has_children() ? | 519 View* first_child_after_text_update = styled()->has_children() ? |
| 504 styled()->child_at(0) : nullptr; | 520 styled()->child_at(0) : nullptr; |
| 505 EXPECT_NE(first_child_after_text_update, first_child_after_layout); | 521 EXPECT_NE(first_child_after_text_update, first_child_after_layout); |
| 506 } | 522 } |
| 507 | 523 |
| 508 } // namespace views | 524 } // namespace views |
| OLD | NEW |