| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/label.h" | 5 #include "ui/views/controls/label.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "ui/accessibility/ax_view_state.h" | 13 #include "ui/accessibility/ax_view_state.h" |
| 14 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" |
| 15 #include "ui/compositor/canvas_painter.h" | 15 #include "ui/compositor/canvas_painter.h" |
| 16 #include "ui/gfx/canvas.h" | 16 #include "ui/gfx/canvas.h" |
| 17 #include "ui/views/border.h" | 17 #include "ui/views/border.h" |
| 18 #include "ui/views/test/focus_manager_test.h" | 18 #include "ui/views/test/focus_manager_test.h" |
| 19 #include "ui/views/test/views_test_base.h" | 19 #include "ui/views/test/views_test_base.h" |
| 20 #include "ui/views/widget/widget.h" | 20 #include "ui/views/widget/widget.h" |
| 21 | 21 |
| 22 using base::ASCIIToUTF16; | 22 using base::ASCIIToUTF16; |
| 23 | 23 |
| 24 namespace views { | 24 namespace views { |
| 25 namespace { |
| 25 | 26 |
| 26 typedef ViewsTestBase LabelTest; | 27 // All text sizing measurements (width and height) should be greater than this. |
| 28 const int kMinTextDimension = 4; |
| 29 |
| 30 using LabelTest = ViewsTestBase; |
| 27 | 31 |
| 28 class LabelFocusTest : public FocusManagerTest { | 32 class LabelFocusTest : public FocusManagerTest { |
| 29 public: | 33 public: |
| 30 LabelFocusTest() {} | 34 LabelFocusTest() {} |
| 31 ~LabelFocusTest() override {} | 35 ~LabelFocusTest() override {} |
| 32 | 36 |
| 33 protected: | 37 protected: |
| 34 views::Label* label() { return label_; } | 38 Label* label() { return label_; } |
| 35 | 39 |
| 36 private: | 40 private: |
| 37 // FocusManagerTest: | 41 // FocusManagerTest: |
| 38 void InitContentView() override { | 42 void InitContentView() override { |
| 39 label_ = new views::Label(); | 43 label_ = new views::Label(); |
| 40 GetContentsView()->AddChildView(label_); | 44 GetContentsView()->AddChildView(label_); |
| 41 } | 45 } |
| 42 | 46 |
| 43 views::Label* label_; | 47 Label* label_; |
| 44 }; | 48 }; |
| 45 | 49 |
| 46 // All text sizing measurements (width and height) should be greater than this. | 50 class TestLabel : public Label { |
| 47 const int kMinTextDimension = 4; | 51 public: |
| 52 TestLabel() : Label(ASCIIToUTF16("TestLabel")) { SizeToPreferredSize(); } |
| 53 |
| 54 int schedule_paint_count() const { return schedule_paint_count_; } |
| 55 |
| 56 void SimulatePaint() { |
| 57 gfx::Canvas canvas(bounds().size(), 1.0, false /* is_opaque */); |
| 58 Paint(ui::CanvasPainter(&canvas, 1.f).context()); |
| 59 } |
| 60 |
| 61 // View: |
| 62 void SchedulePaintInRect(const gfx::Rect& r) override { |
| 63 ++schedule_paint_count_; |
| 64 Label::SchedulePaintInRect(r); |
| 65 } |
| 66 |
| 67 private: |
| 68 int schedule_paint_count_ = 0; |
| 69 |
| 70 DISALLOW_COPY_AND_ASSIGN(TestLabel); |
| 71 }; |
| 48 | 72 |
| 49 // A test utility function to set the application default text direction. | 73 // A test utility function to set the application default text direction. |
| 50 void SetRTL(bool rtl) { | 74 void SetRTL(bool rtl) { |
| 51 // Override the current locale/direction. | 75 // Override the current locale/direction. |
| 52 base::i18n::SetICUDefaultLocale(rtl ? "he" : "en"); | 76 base::i18n::SetICUDefaultLocale(rtl ? "he" : "en"); |
| 53 EXPECT_EQ(rtl, base::i18n::IsRTL()); | 77 EXPECT_EQ(rtl, base::i18n::IsRTL()); |
| 54 } | 78 } |
| 55 | 79 |
| 80 // Returns true if |current| is bigger than |last|. Sets |last| to |current|. |
| 81 bool Increased(int current, int* last) { |
| 82 bool increased = current > *last; |
| 83 *last = current; |
| 84 return increased; |
| 85 } |
| 86 |
| 87 } // namespace |
| 88 |
| 56 // Crashes on Linux only. http://crbug.com/612406 | 89 // Crashes on Linux only. http://crbug.com/612406 |
| 57 #if defined(OS_LINUX) | 90 #if defined(OS_LINUX) |
| 58 #define MAYBE_FontPropertySymbol DISABLED_FontPropertySymbol | 91 #define MAYBE_FontPropertySymbol DISABLED_FontPropertySymbol |
| 59 #else | 92 #else |
| 60 #define MAYBE_FontPropertySymbol FontPropertySymbol | 93 #define MAYBE_FontPropertySymbol FontPropertySymbol |
| 61 #endif | 94 #endif |
| 62 TEST_F(LabelTest, MAYBE_FontPropertySymbol) { | 95 TEST_F(LabelTest, MAYBE_FontPropertySymbol) { |
| 63 Label label; | 96 Label label; |
| 64 std::string font_name("symbol"); | 97 std::string font_name("symbol"); |
| 65 gfx::Font font(font_name, 26); | 98 gfx::Font font(font_name, 26); |
| (...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 605 label.SizeToPreferredSize(); | 638 label.SizeToPreferredSize(); |
| 606 | 639 |
| 607 gfx::Canvas canvas(label.GetPreferredSize(), 1.0f, true); | 640 gfx::Canvas canvas(label.GetPreferredSize(), 1.0f, true); |
| 608 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); | 641 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); |
| 609 | 642 |
| 610 // There's only one 'line', RenderText itself supports multiple lines. | 643 // There's only one 'line', RenderText itself supports multiple lines. |
| 611 EXPECT_EQ(1u, label.lines_.size()); | 644 EXPECT_EQ(1u, label.lines_.size()); |
| 612 } | 645 } |
| 613 #endif | 646 #endif |
| 614 | 647 |
| 648 // Ensures SchedulePaint() calls are not made in OnPaint(). |
| 649 TEST_F(LabelTest, NoSchedulePaintInOnPaint) { |
| 650 TestLabel label; |
| 651 |
| 652 // Initialization should schedule at least one paint, but the precise number |
| 653 // doesn't really matter. |
| 654 int count = label.schedule_paint_count(); |
| 655 EXPECT_LT(0, count); |
| 656 |
| 657 // Painting should never schedule another paint. |
| 658 label.SimulatePaint(); |
| 659 EXPECT_EQ(count, label.schedule_paint_count()); |
| 660 |
| 661 // Test a few things that should schedule paints. Multiple times is OK. |
| 662 label.SetEnabled(false); |
| 663 EXPECT_TRUE(Increased(label.schedule_paint_count(), &count)); |
| 664 |
| 665 label.SetText(label.text() + ASCIIToUTF16("Changed")); |
| 666 EXPECT_TRUE(Increased(label.schedule_paint_count(), &count)); |
| 667 |
| 668 label.SizeToPreferredSize(); |
| 669 EXPECT_TRUE(Increased(label.schedule_paint_count(), &count)); |
| 670 |
| 671 label.SetEnabledColor(SK_ColorBLUE); |
| 672 EXPECT_TRUE(Increased(label.schedule_paint_count(), &count)); |
| 673 |
| 674 label.SimulatePaint(); |
| 675 EXPECT_EQ(count, label.schedule_paint_count()); // Unchanged. |
| 676 } |
| 677 |
| 615 TEST_F(LabelFocusTest, FocusBounds) { | 678 TEST_F(LabelFocusTest, FocusBounds) { |
| 616 label()->SetText(ASCIIToUTF16("Example")); | 679 label()->SetText(ASCIIToUTF16("Example")); |
| 617 gfx::Size normal_size = label()->GetPreferredSize(); | 680 gfx::Size normal_size = label()->GetPreferredSize(); |
| 618 | 681 |
| 619 label()->SetFocusBehavior(View::FocusBehavior::ALWAYS); | 682 label()->SetFocusBehavior(View::FocusBehavior::ALWAYS); |
| 620 label()->RequestFocus(); | 683 label()->RequestFocus(); |
| 621 gfx::Size focusable_size = label()->GetPreferredSize(); | 684 gfx::Size focusable_size = label()->GetPreferredSize(); |
| 622 // Focusable label requires larger size to paint the focus rectangle. | 685 // Focusable label requires larger size to paint the focus rectangle. |
| 623 EXPECT_GT(focusable_size.width(), normal_size.width()); | 686 EXPECT_GT(focusable_size.width(), normal_size.width()); |
| 624 EXPECT_GT(focusable_size.height(), normal_size.height()); | 687 EXPECT_GT(focusable_size.height(), normal_size.height()); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 656 label()->SetFocusBehavior(View::FocusBehavior::ALWAYS); | 719 label()->SetFocusBehavior(View::FocusBehavior::ALWAYS); |
| 657 label()->RequestFocus(); | 720 label()->RequestFocus(); |
| 658 label()->SizeToPreferredSize(); | 721 label()->SizeToPreferredSize(); |
| 659 | 722 |
| 660 gfx::Rect focus_bounds = label()->GetFocusBounds(); | 723 gfx::Rect focus_bounds = label()->GetFocusBounds(); |
| 661 EXPECT_FALSE(focus_bounds.IsEmpty()); | 724 EXPECT_FALSE(focus_bounds.IsEmpty()); |
| 662 EXPECT_LT(label()->font_list().GetHeight(), focus_bounds.height()); | 725 EXPECT_LT(label()->font_list().GetHeight(), focus_bounds.height()); |
| 663 } | 726 } |
| 664 | 727 |
| 665 } // namespace views | 728 } // namespace views |
| OLD | NEW |