| 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/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "ui/accessibility/ax_node_data.h" | 15 #include "ui/accessibility/ax_node_data.h" |
| 16 #include "ui/base/clipboard/clipboard.h" | 16 #include "ui/base/clipboard/clipboard.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 18 #include "ui/compositor/canvas_painter.h" | 18 #include "ui/compositor/canvas_painter.h" |
| 19 #include "ui/events/base_event_utils.h" | 19 #include "ui/events/base_event_utils.h" |
| 20 #include "ui/events/test/event_generator.h" | 20 #include "ui/events/test/event_generator.h" |
| 21 #include "ui/gfx/canvas.h" | 21 #include "ui/gfx/canvas.h" |
| 22 #include "ui/gfx/render_text.h" | 22 #include "ui/gfx/render_text.h" |
| 23 #include "ui/gfx/switches.h" | 23 #include "ui/gfx/switches.h" |
| 24 #include "ui/strings/grit/ui_strings.h" | 24 #include "ui/strings/grit/ui_strings.h" |
| 25 #include "ui/views/border.h" | 25 #include "ui/views/border.h" |
| 26 #include "ui/views/controls/link.h" |
| 26 #include "ui/views/test/focus_manager_test.h" | 27 #include "ui/views/test/focus_manager_test.h" |
| 27 #include "ui/views/test/views_test_base.h" | 28 #include "ui/views/test/views_test_base.h" |
| 28 #include "ui/views/widget/widget.h" | 29 #include "ui/views/widget/widget.h" |
| 29 | 30 |
| 30 using base::ASCIIToUTF16; | 31 using base::ASCIIToUTF16; |
| 31 using base::WideToUTF16; | 32 using base::WideToUTF16; |
| 32 | 33 |
| 33 #define EXPECT_STR_EQ(ascii, utf16) EXPECT_EQ(ASCIIToUTF16(ascii), utf16) | 34 #define EXPECT_STR_EQ(ascii, utf16) EXPECT_EQ(ASCIIToUTF16(ascii), utf16) |
| 34 | 35 |
| 35 namespace views { | 36 namespace views { |
| (...skipping 742 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 778 | 779 |
| 779 label.SetEnabledColor(SK_ColorBLUE); | 780 label.SetEnabledColor(SK_ColorBLUE); |
| 780 EXPECT_TRUE(Increased(label.schedule_paint_count(), &count)); | 781 EXPECT_TRUE(Increased(label.schedule_paint_count(), &count)); |
| 781 | 782 |
| 782 label.SimulatePaint(); | 783 label.SimulatePaint(); |
| 783 EXPECT_EQ(count, label.schedule_paint_count()); // Unchanged. | 784 EXPECT_EQ(count, label.schedule_paint_count()); // Unchanged. |
| 784 } | 785 } |
| 785 | 786 |
| 786 TEST_F(LabelTest, FocusBounds) { | 787 TEST_F(LabelTest, FocusBounds) { |
| 787 label()->SetText(ASCIIToUTF16("Example")); | 788 label()->SetText(ASCIIToUTF16("Example")); |
| 788 gfx::Size normal_size = label()->GetPreferredSize(); | 789 Link concrete_link(ASCIIToUTF16("Example")); |
| 789 | 790 Label* link = &concrete_link; // Allow LabelTest to call methods as friend. |
| 790 label()->SetFocusBehavior(View::FocusBehavior::ALWAYS); | 791 link->SetFocusBehavior(View::FocusBehavior::NEVER); |
| 791 label()->RequestFocus(); | |
| 792 gfx::Size focusable_size = label()->GetPreferredSize(); | |
| 793 // Focusable label requires larger size to paint the focus rectangle. | |
| 794 EXPECT_GT(focusable_size.width(), normal_size.width()); | |
| 795 EXPECT_GT(focusable_size.height(), normal_size.height()); | |
| 796 | 792 |
| 797 label()->SizeToPreferredSize(); | 793 label()->SizeToPreferredSize(); |
| 798 gfx::Rect focus_bounds = label()->GetFocusBounds(); | 794 link->SizeToPreferredSize(); |
| 795 |
| 796 // A regular label never draws a focus ring, so it should exactly match the |
| 797 // font height (assuming no glyphs came from fallback fonts). |
| 798 EXPECT_EQ(label()->font_list().GetHeight(), |
| 799 label()->GetFocusRingBounds().height()); |
| 800 |
| 801 // The test starts by setting the link unfocusable, so it should also match. |
| 802 EXPECT_EQ(link->font_list().GetHeight(), link->GetFocusRingBounds().height()); |
| 803 EXPECT_EQ(link->GetFocusRingBounds(), label()->GetFocusRingBounds()); |
| 804 |
| 805 // Labels are not focusable unless they are links, so don't change size when |
| 806 // the focus behavior changes. |
| 807 gfx::Size normal_label_size = label()->GetPreferredSize(); |
| 808 label()->SetFocusBehavior(View::FocusBehavior::ALWAYS); |
| 809 EXPECT_EQ(normal_label_size, label()->GetPreferredSize()); |
| 810 |
| 811 // But links get bigger in order to paint the focus rectangle. |
| 812 gfx::Size normal_link_size = link->GetPreferredSize(); |
| 813 link->SetFocusBehavior(View::FocusBehavior::ALWAYS); |
| 814 gfx::Size focusable_link_size = link->GetPreferredSize(); |
| 815 EXPECT_NE(normal_link_size, focusable_link_size); |
| 816 EXPECT_GT(focusable_link_size.width(), normal_link_size.width()); |
| 817 EXPECT_GT(focusable_link_size.height(), normal_link_size.height()); |
| 818 |
| 819 // Requesting focus doesn't change the preferred size since that would mess up |
| 820 // layout. |
| 821 label()->RequestFocus(); |
| 822 EXPECT_EQ(focusable_link_size, link->GetPreferredSize()); |
| 823 |
| 824 label()->SizeToPreferredSize(); |
| 825 gfx::Rect focus_bounds = label()->GetFocusRingBounds(); |
| 799 EXPECT_EQ(label()->GetLocalBounds().ToString(), focus_bounds.ToString()); | 826 EXPECT_EQ(label()->GetLocalBounds().ToString(), focus_bounds.ToString()); |
| 800 | 827 |
| 828 gfx::Size focusable_size = normal_label_size; |
| 801 label()->SetBounds( | 829 label()->SetBounds( |
| 802 0, 0, focusable_size.width() * 2, focusable_size.height() * 2); | 830 0, 0, focusable_size.width() * 2, focusable_size.height() * 2); |
| 803 label()->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 831 label()->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 804 focus_bounds = label()->GetFocusBounds(); | 832 focus_bounds = label()->GetFocusRingBounds(); |
| 805 EXPECT_EQ(0, focus_bounds.x()); | 833 EXPECT_EQ(0, focus_bounds.x()); |
| 806 EXPECT_LT(0, focus_bounds.y()); | 834 EXPECT_LT(0, focus_bounds.y()); |
| 807 EXPECT_GT(label()->bounds().bottom(), focus_bounds.bottom()); | 835 EXPECT_GT(label()->bounds().bottom(), focus_bounds.bottom()); |
| 808 EXPECT_EQ(focusable_size.ToString(), focus_bounds.size().ToString()); | 836 EXPECT_EQ(focusable_size.ToString(), focus_bounds.size().ToString()); |
| 809 | 837 |
| 810 label()->SetHorizontalAlignment(gfx::ALIGN_RIGHT); | 838 label()->SetHorizontalAlignment(gfx::ALIGN_RIGHT); |
| 811 focus_bounds = label()->GetFocusBounds(); | 839 focus_bounds = label()->GetFocusRingBounds(); |
| 812 EXPECT_LT(0, focus_bounds.x()); | 840 EXPECT_LT(0, focus_bounds.x()); |
| 813 EXPECT_EQ(label()->bounds().right(), focus_bounds.right()); | 841 EXPECT_EQ(label()->bounds().right(), focus_bounds.right()); |
| 814 EXPECT_LT(0, focus_bounds.y()); | 842 EXPECT_LT(0, focus_bounds.y()); |
| 815 EXPECT_GT(label()->bounds().bottom(), focus_bounds.bottom()); | 843 EXPECT_GT(label()->bounds().bottom(), focus_bounds.bottom()); |
| 816 EXPECT_EQ(focusable_size.ToString(), focus_bounds.size().ToString()); | 844 EXPECT_EQ(focusable_size.ToString(), focus_bounds.size().ToString()); |
| 817 | 845 |
| 818 label()->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 846 label()->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 819 label()->SetElideBehavior(gfx::FADE_TAIL); | 847 label()->SetElideBehavior(gfx::FADE_TAIL); |
| 820 label()->SetBounds(0, 0, focusable_size.width() / 2, focusable_size.height()); | 848 label()->SetBounds(0, 0, focusable_size.width() / 2, focusable_size.height()); |
| 821 focus_bounds = label()->GetFocusBounds(); | 849 focus_bounds = label()->GetFocusRingBounds(); |
| 822 EXPECT_EQ(0, focus_bounds.x()); | 850 EXPECT_EQ(0, focus_bounds.x()); |
| 823 EXPECT_EQ(focusable_size.width() / 2, focus_bounds.width()); | 851 EXPECT_EQ(focusable_size.width() / 2, focus_bounds.width()); |
| 824 } | 852 } |
| 825 | 853 |
| 826 TEST_F(LabelTest, EmptyLabel) { | 854 TEST_F(LabelTest, EmptyLabel) { |
| 827 label()->SetFocusBehavior(View::FocusBehavior::ALWAYS); | 855 label()->SetFocusBehavior(View::FocusBehavior::ALWAYS); |
| 828 label()->RequestFocus(); | 856 label()->RequestFocus(); |
| 829 label()->SizeToPreferredSize(); | 857 label()->SizeToPreferredSize(); |
| 830 | 858 |
| 831 gfx::Rect focus_bounds = label()->GetFocusBounds(); | 859 Link concrete_link((base::string16())); |
| 832 EXPECT_FALSE(focus_bounds.IsEmpty()); | 860 Label* link = &concrete_link; // Allow LabelTest to call methods as friend. |
| 833 EXPECT_LT(label()->font_list().GetHeight(), focus_bounds.height()); | 861 |
| 862 // With no text, neither links nor labels are focusable, and have no size in |
| 863 // any dimension. |
| 864 EXPECT_EQ(gfx::Rect(), label()->GetFocusRingBounds()); |
| 865 EXPECT_EQ(gfx::Rect(), link->GetFocusRingBounds()); |
| 834 } | 866 } |
| 835 | 867 |
| 836 TEST_F(LabelSelectionTest, Selectable) { | 868 TEST_F(LabelSelectionTest, Selectable) { |
| 837 // By default, labels don't support text selection. | 869 // By default, labels don't support text selection. |
| 838 EXPECT_FALSE(label()->selectable()); | 870 EXPECT_FALSE(label()->selectable()); |
| 839 | 871 |
| 840 ASSERT_TRUE(label()->SetSelectable(true)); | 872 ASSERT_TRUE(label()->SetSelectable(true)); |
| 841 EXPECT_TRUE(label()->selectable()); | 873 EXPECT_TRUE(label()->selectable()); |
| 842 | 874 |
| 843 // Verify that making a label multiline still causes the label to support text | 875 // Verify that making a label multiline still causes the label to support text |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1113 label()->SetObscured(false); | 1145 label()->SetObscured(false); |
| 1114 | 1146 |
| 1115 // For an empty label, both COPY and SELECT_ALL should be disabled. | 1147 // For an empty label, both COPY and SELECT_ALL should be disabled. |
| 1116 label()->SetText(base::string16()); | 1148 label()->SetText(base::string16()); |
| 1117 ASSERT_TRUE(label()->SetSelectable(true)); | 1149 ASSERT_TRUE(label()->SetSelectable(true)); |
| 1118 EXPECT_FALSE(IsMenuCommandEnabled(IDS_APP_COPY)); | 1150 EXPECT_FALSE(IsMenuCommandEnabled(IDS_APP_COPY)); |
| 1119 EXPECT_FALSE(IsMenuCommandEnabled(IDS_APP_SELECT_ALL)); | 1151 EXPECT_FALSE(IsMenuCommandEnabled(IDS_APP_SELECT_ALL)); |
| 1120 } | 1152 } |
| 1121 | 1153 |
| 1122 } // namespace views | 1154 } // namespace views |
| OLD | NEW |