OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/utf_string_conversions.h" | 5 #include "base/utf_string_conversions.h" |
6 #include "ui/gfx/point.h" | 6 #include "ui/gfx/point.h" |
7 #include "ui/gfx/rect.h" | 7 #include "ui/gfx/rect.h" |
8 #include "ui/gfx/render_text.h" | 8 #include "ui/gfx/render_text.h" |
9 #include "views/controls/textfield/native_textfield_views.h" | 9 #include "views/controls/textfield/native_textfield_views.h" |
10 #include "views/controls/textfield/textfield.h" | 10 #include "views/controls/textfield/textfield.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
42 textfield_view_ = static_cast<NativeTextfieldViews*>( | 42 textfield_view_ = static_cast<NativeTextfieldViews*>( |
43 textfield_->GetNativeWrapperForTesting()); | 43 textfield_->GetNativeWrapperForTesting()); |
44 textfield_view_->SetBoundsRect(params.bounds); | 44 textfield_view_->SetBoundsRect(params.bounds); |
45 textfield_->set_id(1); | 45 textfield_->set_id(1); |
46 | 46 |
47 DCHECK(textfield_view_); | 47 DCHECK(textfield_view_); |
48 textfield_->RequestFocus(); | 48 textfield_->RequestFocus(); |
49 } | 49 } |
50 | 50 |
51 protected: | 51 protected: |
52 gfx::Point GetCursorPosition(int cursor_pos) { | 52 gfx::Point GetCursorPosition(const gfx::SelectionModel& sel) { |
53 gfx::RenderText* render_text = textfield_view_->GetRenderText(); | 53 gfx::RenderText* render_text = textfield_view_->GetRenderText(); |
54 gfx::Rect cursor_bounds = render_text->GetCursorBounds( | 54 gfx::Rect cursor_bounds = render_text->GetCursorBounds(sel, true); |
55 gfx::SelectionModel(cursor_pos), false); | |
56 return gfx::Point(cursor_bounds.x(), cursor_bounds.bottom() - 1); | 55 return gfx::Point(cursor_bounds.x(), cursor_bounds.bottom() - 1); |
57 } | 56 } |
58 | 57 |
59 TouchSelectionControllerImpl* GetSelectionController() { | 58 TouchSelectionControllerImpl* GetSelectionController() { |
60 return static_cast<TouchSelectionControllerImpl*>( | 59 return static_cast<TouchSelectionControllerImpl*>( |
61 textfield_view_->touch_selection_controller_.get()); | 60 textfield_view_->touch_selection_controller_.get()); |
62 } | 61 } |
63 | 62 |
64 void SimulateSelectionHandleDrag(gfx::Point p, int selection_handle) { | 63 void SimulateSelectionHandleDrag(gfx::Point p, int selection_handle) { |
65 TouchSelectionControllerImpl* controller = GetSelectionController(); | 64 TouchSelectionControllerImpl* controller = GetSelectionController(); |
(...skipping 12 matching lines...) Expand all Loading... | |
78 // If textfield has selection, this method verifies that the selection handles | 77 // If textfield has selection, this method verifies that the selection handles |
79 // are visible and at the correct positions (at the end points of selection). | 78 // are visible and at the correct positions (at the end points of selection). |
80 // |cursor_at_selection_handle_1| is used to decide whether selection | 79 // |cursor_at_selection_handle_1| is used to decide whether selection |
81 // handle 1's position is matched against the start of selection or the end. | 80 // handle 1's position is matched against the start of selection or the end. |
82 void VerifySelectionHandlePositions(bool cursor_at_selection_handle_1) { | 81 void VerifySelectionHandlePositions(bool cursor_at_selection_handle_1) { |
83 if (textfield_->HasSelection()) { | 82 if (textfield_->HasSelection()) { |
84 EXPECT_TRUE(GetSelectionController()->IsSelectionHandle1Visible()); | 83 EXPECT_TRUE(GetSelectionController()->IsSelectionHandle1Visible()); |
85 EXPECT_TRUE(GetSelectionController()->IsSelectionHandle2Visible()); | 84 EXPECT_TRUE(GetSelectionController()->IsSelectionHandle2Visible()); |
86 gfx::SelectionModel sel; | 85 gfx::SelectionModel sel; |
87 textfield_view_->GetSelectionModel(&sel); | 86 textfield_view_->GetSelectionModel(&sel); |
88 gfx::Point selection_start = GetCursorPosition(sel.selection_start()); | 87 gfx::SelectionModel sel_start = textfield_view_->GetRenderText()-> |
89 gfx::Point selection_end = GetCursorPosition(sel.selection_end()); | 88 GetSelectionModelForSelectionStart(); |
89 gfx::Point selection_start = GetCursorPosition(sel_start); | |
90 gfx::Point selection_end = GetCursorPosition(sel); | |
90 gfx::Point sh1 = GetSelectionController()->GetSelectionHandle1Position(); | 91 gfx::Point sh1 = GetSelectionController()->GetSelectionHandle1Position(); |
91 gfx::Point sh2 = GetSelectionController()->GetSelectionHandle2Position(); | 92 gfx::Point sh2 = GetSelectionController()->GetSelectionHandle2Position(); |
92 sh1.Offset(10, 0); // offset by kSelectionHandleRadius. | 93 sh1.Offset(10, 0); // offset by kSelectionHandleRadius. |
93 sh2.Offset(10, 0); | 94 sh2.Offset(10, 0); |
94 | 95 |
95 if (cursor_at_selection_handle_1) { | 96 if (cursor_at_selection_handle_1) { |
96 EXPECT_EQ(sh1, selection_end); | 97 EXPECT_EQ(sh1, selection_end); |
97 EXPECT_EQ(sh2, selection_start); | 98 EXPECT_EQ(sh2, selection_start); |
98 } else { | 99 } else { |
99 EXPECT_EQ(sh1, selection_start); | 100 EXPECT_EQ(sh1, selection_start); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
134 | 135 |
135 // Test with lost focus. | 136 // Test with lost focus. |
136 widget_->GetFocusManager()->ClearFocus(); | 137 widget_->GetFocusManager()->ClearFocus(); |
137 VerifySelectionHandlePositions(false); | 138 VerifySelectionHandlePositions(false); |
138 | 139 |
139 // Test with focus re-gained. | 140 // Test with focus re-gained. |
140 widget_->GetFocusManager()->SetFocusedView(textfield_); | 141 widget_->GetFocusManager()->SetFocusedView(textfield_); |
141 VerifySelectionHandlePositions(false); | 142 VerifySelectionHandlePositions(false); |
142 } | 143 } |
143 | 144 |
145 // Tests that the selection handles are placed appropriately in bidi text. | |
146 TEST_F(TouchSelectionControllerImplTest, SelectionInBidiTextfieldTest) { | |
147 CreateTextfield(); | |
148 textfield_->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2")); | |
149 | |
150 // Test cursor at run boundary and with empty selection. | |
151 textfield_->SelectSelectionModel( | |
152 gfx::SelectionModel(3, 2, gfx::SelectionModel::TRAILING)); | |
153 VerifySelectionHandlePositions(false); | |
154 | |
155 // Test selection range (3, 2). | |
msw
2011/09/29 02:20:21
Please also test some other pertinent BiDi ranges.
xji
2011/10/03 23:18:57
Done.
| |
156 textfield_->SelectSelectionModel(gfx::SelectionModel(3, 2)); | |
157 VerifySelectionHandlePositions(false); | |
158 } | |
159 | |
144 // Tests if the SelectRect callback is called appropriately when selection | 160 // Tests if the SelectRect callback is called appropriately when selection |
145 // handles are moved. | 161 // handles are moved. |
146 TEST_F(TouchSelectionControllerImplTest, SelectRectCallbackTest) { | 162 TEST_F(TouchSelectionControllerImplTest, SelectRectCallbackTest) { |
147 CreateTextfield(); | 163 CreateTextfield(); |
148 textfield_->SetText(ASCIIToUTF16("textfield with selected text")); | 164 textfield_->SetText(ASCIIToUTF16("textfield with selected text")); |
149 textfield_->SelectSelectionModel(gfx::SelectionModel(3, 7)); | 165 textfield_->SelectSelectionModel(gfx::SelectionModel(3, 7)); |
150 | 166 |
151 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "tfie"); | 167 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "tfie"); |
152 VerifySelectionHandlePositions(false); | 168 VerifySelectionHandlePositions(false); |
153 | 169 |
(...skipping 16 matching lines...) Expand all Loading... | |
170 VerifySelectionHandlePositions(true); | 186 VerifySelectionHandlePositions(true); |
171 | 187 |
172 // Drag selection handle 2 across selection handle 1. | 188 // Drag selection handle 2 across selection handle 1. |
173 x = textfield_->font().GetStringWidth(ASCIIToUTF16("with selected ")); | 189 x = textfield_->font().GetStringWidth(ASCIIToUTF16("with selected ")); |
174 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); | 190 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); |
175 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "selected "); | 191 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "selected "); |
176 VerifySelectionHandlePositions(false); | 192 VerifySelectionHandlePositions(false); |
177 } | 193 } |
178 | 194 |
179 } // namespace views | 195 } // namespace views |
OLD | NEW |