| 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" |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 } | 90 } |
| 91 | 91 |
| 92 gfx::Point GetCursorPoint(int cursor_pos) { | 92 gfx::Point GetCursorPoint(int cursor_pos) { |
| 93 return label() | 93 return label() |
| 94 ->GetRenderTextForSelectionController() | 94 ->GetRenderTextForSelectionController() |
| 95 ->GetCursorBounds(gfx::SelectionModel(cursor_pos, gfx::CURSOR_FORWARD), | 95 ->GetCursorBounds(gfx::SelectionModel(cursor_pos, gfx::CURSOR_FORWARD), |
| 96 false) | 96 false) |
| 97 .origin(); | 97 .origin(); |
| 98 } | 98 } |
| 99 | 99 |
| 100 base::string16 GetSelectedText() { | |
| 101 const gfx::RenderText* render_text = | |
| 102 label()->GetRenderTextForSelectionController(); | |
| 103 return render_text->GetTextFromRange(render_text->selection()); | |
| 104 } | |
| 105 | |
| 106 private: | 100 private: |
| 107 Label* label_ = nullptr; | 101 Label* label_ = nullptr; |
| 108 Widget widget_; | 102 Widget widget_; |
| 109 | 103 |
| 110 DISALLOW_COPY_AND_ASSIGN(LabelTest); | 104 DISALLOW_COPY_AND_ASSIGN(LabelTest); |
| 111 }; | 105 }; |
| 112 | 106 |
| 113 namespace { | 107 namespace { |
| 114 | 108 |
| 115 // All text sizing measurements (width and height) should be greater than this. | 109 // All text sizing measurements (width and height) should be greater than this. |
| (...skipping 720 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 836 EXPECT_EQ(label(), GetFocusedView()); | 830 EXPECT_EQ(label(), GetFocusedView()); |
| 837 } | 831 } |
| 838 | 832 |
| 839 // Verify label text selection behavior on double and triple clicks. | 833 // Verify label text selection behavior on double and triple clicks. |
| 840 TEST_F(LabelTest, DoubleTripleClick) { | 834 TEST_F(LabelTest, DoubleTripleClick) { |
| 841 label()->SetText(ASCIIToUTF16("Label double click")); | 835 label()->SetText(ASCIIToUTF16("Label double click")); |
| 842 label()->SizeToPreferredSize(); | 836 label()->SizeToPreferredSize(); |
| 843 ASSERT_TRUE(label()->SetSelectable(true)); | 837 ASSERT_TRUE(label()->SetSelectable(true)); |
| 844 | 838 |
| 845 PerformClick(gfx::Point()); | 839 PerformClick(gfx::Point()); |
| 846 EXPECT_TRUE(GetSelectedText().empty()); | 840 EXPECT_TRUE(label()->GetSelectedText().empty()); |
| 847 | 841 |
| 848 // Double clicking should select the word under cursor. | 842 // Double clicking should select the word under cursor. |
| 849 PerformClick(gfx::Point(), ui::EF_IS_DOUBLE_CLICK); | 843 PerformClick(gfx::Point(), ui::EF_IS_DOUBLE_CLICK); |
| 850 EXPECT_STR_EQ("Label", GetSelectedText()); | 844 EXPECT_STR_EQ("Label", label()->GetSelectedText()); |
| 851 | 845 |
| 852 // Triple clicking should select all the text. | 846 // Triple clicking should select all the text. |
| 853 PerformClick(gfx::Point()); | 847 PerformClick(gfx::Point()); |
| 854 EXPECT_EQ(label()->text(), GetSelectedText()); | 848 EXPECT_EQ(label()->text(), label()->GetSelectedText()); |
| 855 | 849 |
| 856 // Clicking again should alternate to double click. | 850 // Clicking again should alternate to double click. |
| 857 PerformClick(gfx::Point()); | 851 PerformClick(gfx::Point()); |
| 858 EXPECT_STR_EQ("Label", GetSelectedText()); | 852 EXPECT_STR_EQ("Label", label()->GetSelectedText()); |
| 859 | 853 |
| 860 // Clicking at another location should clear the selection. | 854 // Clicking at another location should clear the selection. |
| 861 PerformClick(GetCursorPoint(8)); | 855 PerformClick(GetCursorPoint(8)); |
| 862 EXPECT_TRUE(GetSelectedText().empty()); | 856 EXPECT_TRUE(label()->GetSelectedText().empty()); |
| 863 PerformClick(GetCursorPoint(8), ui::EF_IS_DOUBLE_CLICK); | 857 PerformClick(GetCursorPoint(8), ui::EF_IS_DOUBLE_CLICK); |
| 864 EXPECT_STR_EQ("double", GetSelectedText()); | 858 EXPECT_STR_EQ("double", label()->GetSelectedText()); |
| 865 } | 859 } |
| 866 | 860 |
| 867 // Verify label text selection behavior on mouse drag. | 861 // Verify label text selection behavior on mouse drag. |
| 868 TEST_F(LabelTest, MouseDrag) { | 862 TEST_F(LabelTest, MouseDrag) { |
| 869 label()->SetText(ASCIIToUTF16("Label mouse drag")); | 863 label()->SetText(ASCIIToUTF16("Label mouse drag")); |
| 870 label()->SizeToPreferredSize(); | 864 label()->SizeToPreferredSize(); |
| 871 ASSERT_TRUE(label()->SetSelectable(true)); | 865 ASSERT_TRUE(label()->SetSelectable(true)); |
| 872 | 866 |
| 873 PerformMousePress(GetCursorPoint(5)); | 867 PerformMousePress(GetCursorPoint(5)); |
| 874 PerformMouseDragTo(gfx::Point()); | 868 PerformMouseDragTo(gfx::Point()); |
| 875 EXPECT_STR_EQ("Label", GetSelectedText()); | 869 EXPECT_STR_EQ("Label", label()->GetSelectedText()); |
| 876 | 870 |
| 877 PerformMouseDragTo(GetCursorPoint(8)); | 871 PerformMouseDragTo(GetCursorPoint(8)); |
| 878 EXPECT_STR_EQ(" mo", GetSelectedText()); | 872 EXPECT_STR_EQ(" mo", label()->GetSelectedText()); |
| 879 | 873 |
| 880 PerformMouseDragTo(gfx::Point(200, 0)); | 874 PerformMouseDragTo(gfx::Point(200, 0)); |
| 881 PerformMouseRelease(gfx::Point(200, 0)); | 875 PerformMouseRelease(gfx::Point(200, 0)); |
| 882 EXPECT_STR_EQ(" mouse drag", GetSelectedText()); | 876 EXPECT_STR_EQ(" mouse drag", label()->GetSelectedText()); |
| 883 } | 877 } |
| 884 | 878 |
| 885 // Verify the initially selected word on a double click, remains selected on | 879 // Verify the initially selected word on a double click, remains selected on |
| 886 // mouse dragging. | 880 // mouse dragging. |
| 887 TEST_F(LabelTest, MouseDragWord) { | 881 TEST_F(LabelTest, MouseDragWord) { |
| 888 label()->SetText(ASCIIToUTF16("Label drag word")); | 882 label()->SetText(ASCIIToUTF16("Label drag word")); |
| 889 label()->SizeToPreferredSize(); | 883 label()->SizeToPreferredSize(); |
| 890 ASSERT_TRUE(label()->SetSelectable(true)); | 884 ASSERT_TRUE(label()->SetSelectable(true)); |
| 891 | 885 |
| 892 PerformClick(GetCursorPoint(8)); | 886 PerformClick(GetCursorPoint(8)); |
| 893 PerformMousePress(GetCursorPoint(8), ui::EF_IS_DOUBLE_CLICK); | 887 PerformMousePress(GetCursorPoint(8), ui::EF_IS_DOUBLE_CLICK); |
| 894 EXPECT_STR_EQ("drag", GetSelectedText()); | 888 EXPECT_STR_EQ("drag", label()->GetSelectedText()); |
| 895 | 889 |
| 896 PerformMouseDragTo(gfx::Point()); | 890 PerformMouseDragTo(gfx::Point()); |
| 897 EXPECT_STR_EQ("Label drag", GetSelectedText()); | 891 EXPECT_STR_EQ("Label drag", label()->GetSelectedText()); |
| 898 | 892 |
| 899 PerformMouseDragTo(gfx::Point(200, 0)); | 893 PerformMouseDragTo(gfx::Point(200, 0)); |
| 900 PerformMouseRelease(gfx::Point(200, 0)); | 894 PerformMouseRelease(gfx::Point(200, 0)); |
| 901 EXPECT_STR_EQ("drag word", GetSelectedText()); | 895 EXPECT_STR_EQ("drag word", label()->GetSelectedText()); |
| 902 } | 896 } |
| 903 #endif // OS_MACOSX | 897 #endif // OS_MACOSX |
| 904 | 898 |
| 905 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | 899 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 906 // Verify selection clipboard behavior on text selection. Disabled due to | 900 // Verify selection clipboard behavior on text selection. Disabled due to |
| 907 // http://crbug.com/396477. | 901 // http://crbug.com/396477. |
| 908 TEST_F(LabelTest, DISABLED_SelectionClipboard) { | 902 TEST_F(LabelTest, DISABLED_SelectionClipboard) { |
| 909 label()->SetText(ASCIIToUTF16("Label selection clipboard")); | 903 label()->SetText(ASCIIToUTF16("Label selection clipboard")); |
| 910 label()->SizeToPreferredSize(); | 904 label()->SizeToPreferredSize(); |
| 911 ASSERT_TRUE(label()->SetSelectable(true)); | 905 ASSERT_TRUE(label()->SetSelectable(true)); |
| 912 | 906 |
| 913 // Verify programmatic modification of selection, does not modify the | 907 // Verify programmatic modification of selection, does not modify the |
| 914 // selection clipboard. | 908 // selection clipboard. |
| 915 label()->SelectRange(gfx::Range(2, 5)); | 909 label()->SelectRange(gfx::Range(2, 5)); |
| 916 EXPECT_STR_EQ("bel", GetSelectedText()); | 910 EXPECT_STR_EQ("bel", label()->GetSelectedText()); |
| 917 EXPECT_TRUE(GetSelectionClipboardText().empty()); | 911 EXPECT_TRUE(GetSelectionClipboardText().empty()); |
| 918 | 912 |
| 919 // Verify text selection using the mouse updates the selection clipboard. | 913 // Verify text selection using the mouse updates the selection clipboard. |
| 920 PerformMousePress(GetCursorPoint(5)); | 914 PerformMousePress(GetCursorPoint(5)); |
| 921 PerformMouseDragTo(gfx::Point()); | 915 PerformMouseDragTo(gfx::Point()); |
| 922 PerformMouseRelease(gfx::Point()); | 916 PerformMouseRelease(gfx::Point()); |
| 923 EXPECT_STR_EQ("Label", GetSelectedText()); | 917 EXPECT_STR_EQ("Label", label()->GetSelectedText()); |
| 924 EXPECT_STR_EQ("Label", GetSelectionClipboardText()); | 918 EXPECT_STR_EQ("Label", GetSelectionClipboardText()); |
| 925 } | 919 } |
| 926 #endif | 920 #endif |
| 927 | 921 |
| 928 } // namespace views | 922 } // namespace views |
| OLD | NEW |