| 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 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 DISALLOW_COPY_AND_ASSIGN(TouchSelectionControllerImplTest); | 115 DISALLOW_COPY_AND_ASSIGN(TouchSelectionControllerImplTest); |
| 116 }; | 116 }; |
| 117 | 117 |
| 118 // Tests that the selection handles are placed appropriately when selection in | 118 // Tests that the selection handles are placed appropriately when selection in |
| 119 // a Textfield changes. | 119 // a Textfield changes. |
| 120 TEST_F(TouchSelectionControllerImplTest, SelectionInTextfieldTest) { | 120 TEST_F(TouchSelectionControllerImplTest, SelectionInTextfieldTest) { |
| 121 CreateTextfield(); | 121 CreateTextfield(); |
| 122 textfield_->SetText(ASCIIToUTF16("some text")); | 122 textfield_->SetText(ASCIIToUTF16("some text")); |
| 123 | 123 |
| 124 // Test selecting a range. | 124 // Test selecting a range. |
| 125 textfield_->SelectSelectionModel(gfx::SelectionModel(3, 7)); | 125 textfield_->SelectRange(ui::Range(3, 7)); |
| 126 VerifySelectionHandlePositions(false); | 126 VerifySelectionHandlePositions(false); |
| 127 | 127 |
| 128 // Test selecting everything. | 128 // Test selecting everything. |
| 129 textfield_->SelectAll(); | 129 textfield_->SelectAll(); |
| 130 VerifySelectionHandlePositions(false); | 130 VerifySelectionHandlePositions(false); |
| 131 | 131 |
| 132 // Test with no selection. | 132 // Test with no selection. |
| 133 textfield_->ClearSelection(); | 133 textfield_->ClearSelection(); |
| 134 VerifySelectionHandlePositions(false); | 134 VerifySelectionHandlePositions(false); |
| 135 | 135 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 146 TEST_F(TouchSelectionControllerImplTest, SelectionInBidiTextfieldTest) { | 146 TEST_F(TouchSelectionControllerImplTest, SelectionInBidiTextfieldTest) { |
| 147 CreateTextfield(); | 147 CreateTextfield(); |
| 148 textfield_->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2")); | 148 textfield_->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2")); |
| 149 | 149 |
| 150 // Test cursor at run boundary and with empty selection. | 150 // Test cursor at run boundary and with empty selection. |
| 151 textfield_->SelectSelectionModel( | 151 textfield_->SelectSelectionModel( |
| 152 gfx::SelectionModel(3, 2, gfx::SelectionModel::TRAILING)); | 152 gfx::SelectionModel(3, 2, gfx::SelectionModel::TRAILING)); |
| 153 VerifySelectionHandlePositions(false); | 153 VerifySelectionHandlePositions(false); |
| 154 | 154 |
| 155 // Test selection range inside one run and starts or ends at run boundary. | 155 // Test selection range inside one run and starts or ends at run boundary. |
| 156 textfield_->SelectSelectionModel( | 156 textfield_->SelectRange(ui::Range(2, 3)); |
| 157 gfx::SelectionModel(2, 3, 2, gfx::SelectionModel::TRAILING)); | |
| 158 VerifySelectionHandlePositions(false); | 157 VerifySelectionHandlePositions(false); |
| 159 | 158 |
| 160 // TODO(xji): change to textfield_->SelectRange(3, 2). | 159 textfield_->SelectRange(ui::Range(3, 2)); |
| 161 textfield_->SelectSelectionModel( | |
| 162 gfx::SelectionModel(3, 2, 2, gfx::SelectionModel::LEADING)); | |
| 163 VerifySelectionHandlePositions(false); | 160 VerifySelectionHandlePositions(false); |
| 164 | 161 |
| 165 textfield_->SelectSelectionModel( | 162 textfield_->SelectRange(ui::Range(3, 4)); |
| 166 gfx::SelectionModel(3, 4, 3, gfx::SelectionModel::TRAILING)); | |
| 167 VerifySelectionHandlePositions(false); | 163 VerifySelectionHandlePositions(false); |
| 168 | 164 |
| 169 textfield_->SelectSelectionModel( | 165 textfield_->SelectRange(ui::Range(4, 3)); |
| 170 gfx::SelectionModel(4, 3, 3, gfx::SelectionModel::LEADING)); | |
| 171 VerifySelectionHandlePositions(false); | 166 VerifySelectionHandlePositions(false); |
| 172 | 167 |
| 173 textfield_->SelectSelectionModel( | 168 textfield_->SelectRange(ui::Range(3, 6)); |
| 174 gfx::SelectionModel(3, 6, 5, gfx::SelectionModel::TRAILING)); | |
| 175 VerifySelectionHandlePositions(false); | 169 VerifySelectionHandlePositions(false); |
| 176 | 170 |
| 177 textfield_->SelectSelectionModel( | 171 textfield_->SelectRange(ui::Range(6, 3)); |
| 178 gfx::SelectionModel(6, 3, 3, gfx::SelectionModel::LEADING)); | |
| 179 VerifySelectionHandlePositions(false); | 172 VerifySelectionHandlePositions(false); |
| 180 | 173 |
| 181 // Test selection range accross runs. | 174 // Test selection range accross runs. |
| 182 textfield_->SelectSelectionModel( | 175 textfield_->SelectRange(ui::Range(0, 6)); |
| 183 gfx::SelectionModel(0, 6, 5, gfx::SelectionModel::TRAILING)); | |
| 184 VerifySelectionHandlePositions(false); | 176 VerifySelectionHandlePositions(false); |
| 185 | 177 |
| 186 textfield_->SelectSelectionModel( | 178 textfield_->SelectRange(ui::Range(6, 0)); |
| 187 gfx::SelectionModel(6, 0, 0, gfx::SelectionModel::LEADING)); | |
| 188 VerifySelectionHandlePositions(false); | 179 VerifySelectionHandlePositions(false); |
| 189 | 180 |
| 190 textfield_->SelectSelectionModel( | 181 textfield_->SelectRange(ui::Range(1, 4)); |
| 191 gfx::SelectionModel(1, 4, 3, gfx::SelectionModel::TRAILING)); | |
| 192 VerifySelectionHandlePositions(false); | 182 VerifySelectionHandlePositions(false); |
| 193 | 183 |
| 194 textfield_->SelectSelectionModel( | 184 textfield_->SelectRange(ui::Range(4, 1)); |
| 195 gfx::SelectionModel(4, 1, 1, gfx::SelectionModel::LEADING)); | |
| 196 VerifySelectionHandlePositions(false); | 185 VerifySelectionHandlePositions(false); |
| 197 } | 186 } |
| 198 | 187 |
| 199 // Tests if the SelectRect callback is called appropriately when selection | 188 // Tests if the SelectRect callback is called appropriately when selection |
| 200 // handles are moved. | 189 // handles are moved. |
| 201 TEST_F(TouchSelectionControllerImplTest, SelectRectCallbackTest) { | 190 TEST_F(TouchSelectionControllerImplTest, SelectRectCallbackTest) { |
| 202 CreateTextfield(); | 191 CreateTextfield(); |
| 203 textfield_->SetText(ASCIIToUTF16("textfield with selected text")); | 192 textfield_->SetText(ASCIIToUTF16("textfield with selected text")); |
| 204 textfield_->SelectSelectionModel(gfx::SelectionModel(3, 7)); | 193 textfield_->SelectRange(ui::Range(3, 7)); |
| 205 | 194 |
| 206 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "tfie"); | 195 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "tfie"); |
| 207 VerifySelectionHandlePositions(false); | 196 VerifySelectionHandlePositions(false); |
| 208 | 197 |
| 209 // Drag selection handle 2 to right by 3 chars. | 198 // Drag selection handle 2 to right by 3 chars. |
| 210 int x = textfield_->font().GetStringWidth(ASCIIToUTF16("ld ")); | 199 int x = textfield_->font().GetStringWidth(ASCIIToUTF16("ld ")); |
| 211 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); | 200 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); |
| 212 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "tfield "); | 201 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "tfield "); |
| 213 VerifySelectionHandlePositions(false); | 202 VerifySelectionHandlePositions(false); |
| 214 | 203 |
| 215 // Drag selection handle 1 to the left by a large amount (selection should | 204 // Drag selection handle 1 to the left by a large amount (selection should |
| 216 // just stick to the beginning of the textfield). | 205 // just stick to the beginning of the textfield). |
| 217 SimulateSelectionHandleDrag(gfx::Point(-50, 0), 1); | 206 SimulateSelectionHandleDrag(gfx::Point(-50, 0), 1); |
| 218 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "textfield "); | 207 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "textfield "); |
| 219 VerifySelectionHandlePositions(true); | 208 VerifySelectionHandlePositions(true); |
| 220 | 209 |
| 221 // Drag selection handle 1 across selection handle 2. | 210 // Drag selection handle 1 across selection handle 2. |
| 222 x = textfield_->font().GetStringWidth(ASCIIToUTF16("textfield with ")); | 211 x = textfield_->font().GetStringWidth(ASCIIToUTF16("textfield with ")); |
| 223 SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); | 212 SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); |
| 224 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "with "); | 213 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "with "); |
| 225 VerifySelectionHandlePositions(true); | 214 VerifySelectionHandlePositions(true); |
| 226 | 215 |
| 227 // Drag selection handle 2 across selection handle 1. | 216 // Drag selection handle 2 across selection handle 1. |
| 228 x = textfield_->font().GetStringWidth(ASCIIToUTF16("with selected ")); | 217 x = textfield_->font().GetStringWidth(ASCIIToUTF16("with selected ")); |
| 229 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); | 218 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); |
| 230 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "selected "); | 219 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "selected "); |
| 231 VerifySelectionHandlePositions(false); | 220 VerifySelectionHandlePositions(false); |
| 232 } | 221 } |
| 233 | 222 |
| 223 TEST_F(TouchSelectionControllerImplTest, SelectRectInBidiCallbackTest) { |
| 224 CreateTextfield(); |
| 225 textfield_->SetText(WideToUTF16(L"abc\x05e1\x05e2\x05e3"L"def")); |
| 226 |
| 227 // Select [c] from left to right. |
| 228 textfield_->SelectRange(ui::Range(2, 3)); |
| 229 EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText()); |
| 230 VerifySelectionHandlePositions(false); |
| 231 |
| 232 // Drag selection handle 2 to right by 1 char. |
| 233 int x = textfield_->font().GetStringWidth(WideToUTF16(L"\x05e3")); |
| 234 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); |
| 235 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); |
| 236 VerifySelectionHandlePositions(false); |
| 237 |
| 238 // Drag selection handle 1 to left by 1 char. |
| 239 x = textfield_->font().GetStringWidth(WideToUTF16(L"b")); |
| 240 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 1); |
| 241 EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText()); |
| 242 VerifySelectionHandlePositions(true); |
| 243 |
| 244 // Select [c] from right to left. |
| 245 textfield_->SelectRange(ui::Range(3, 2)); |
| 246 EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText()); |
| 247 VerifySelectionHandlePositions(false); |
| 248 |
| 249 // Drag selection handle 1 to right by 1 char. |
| 250 x = textfield_->font().GetStringWidth(WideToUTF16(L"\x05e3")); |
| 251 SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); |
| 252 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); |
| 253 VerifySelectionHandlePositions(true); |
| 254 |
| 255 // Drag selection handle 2 to left by 1 char. |
| 256 x = textfield_->font().GetStringWidth(WideToUTF16(L"b")); |
| 257 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 2); |
| 258 EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText()); |
| 259 VerifySelectionHandlePositions(false); |
| 260 |
| 261 // Select [\x5e1] from right to left. |
| 262 textfield_->SelectRange(ui::Range(3, 4)); |
| 263 EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText()); |
| 264 VerifySelectionHandlePositions(false); |
| 265 |
| 266 /* TODO(xji): for bidi text "abcDEF" whose display is "abcFEDhij", when click |
| 267 right of 'D' and select [D] then move the left selection handle to left |
| 268 by one character, it should select [ED], instead it selects [F]. |
| 269 Reason: click right of 'D' and left of 'h' return the same x-axis position, |
| 270 pass this position to FindCursorPosition() returns index of 'h'. which |
| 271 means the selection start changed from 3 to 6. |
| 272 Need further investigation on whether this is a bug in Pango and how to |
| 273 work around it. |
| 274 // Drag selection handle 2 to left by 1 char. |
| 275 x = textfield_->font().GetStringWidth(WideToUTF16(L"\x05e2")); |
| 276 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 2); |
| 277 EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText()); |
| 278 VerifySelectionHandlePositions(false); |
| 279 */ |
| 280 |
| 281 // Drag selection handle 1 to right by 1 char. |
| 282 x = textfield_->font().GetStringWidth(WideToUTF16(L"d")); |
| 283 SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); |
| 284 EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3"L"d"), textfield_->GetSelectedText()); |
| 285 VerifySelectionHandlePositions(true); |
| 286 |
| 287 // Select [\x5e1] from left to right. |
| 288 textfield_->SelectRange(ui::Range(4, 3)); |
| 289 EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText()); |
| 290 VerifySelectionHandlePositions(false); |
| 291 |
| 292 /* TODO(xji): see detail of above commented out test case. |
| 293 // Drag selection handle 1 to left by 1 char. |
| 294 x = textfield_->font().GetStringWidth(WideToUTF16(L"\x05e2")); |
| 295 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 1); |
| 296 EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText()); |
| 297 VerifySelectionHandlePositions(true); |
| 298 */ |
| 299 |
| 300 // Drag selection handle 2 to right by 1 char. |
| 301 x = textfield_->font().GetStringWidth(WideToUTF16(L"d")); |
| 302 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); |
| 303 EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3"L"d"), textfield_->GetSelectedText()); |
| 304 VerifySelectionHandlePositions(false); |
| 305 |
| 306 // Select [\x05r3] from right to left. |
| 307 textfield_->SelectRange(ui::Range(5, 6)); |
| 308 EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText()); |
| 309 VerifySelectionHandlePositions(false); |
| 310 |
| 311 // Drag selection handle 2 to left by 1 char. |
| 312 x = textfield_->font().GetStringWidth(WideToUTF16(L"c")); |
| 313 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 2); |
| 314 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); |
| 315 VerifySelectionHandlePositions(false); |
| 316 |
| 317 // Drag selection handle 1 to right by 1 char. |
| 318 x = textfield_->font().GetStringWidth(WideToUTF16(L"\x05e2")); |
| 319 SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); |
| 320 EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText()); |
| 321 VerifySelectionHandlePositions(true); |
| 322 |
| 323 // Select [\x05r3] from left to right. |
| 324 textfield_->SelectRange(ui::Range(6, 5)); |
| 325 EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText()); |
| 326 VerifySelectionHandlePositions(false); |
| 327 |
| 328 // Drag selection handle 1 to left by 1 char. |
| 329 x = textfield_->font().GetStringWidth(WideToUTF16(L"c")); |
| 330 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 1); |
| 331 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); |
| 332 VerifySelectionHandlePositions(true); |
| 333 |
| 334 // Drag selection handle 2 to right by 1 char. |
| 335 x = textfield_->font().GetStringWidth(WideToUTF16(L"\x05e2")); |
| 336 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); |
| 337 EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText()); |
| 338 VerifySelectionHandlePositions(false); |
| 339 } |
| 340 |
| 234 } // namespace views | 341 } // namespace views |
| OLD | NEW |