Chromium Code Reviews

Side by Side Diff: third_party/WebKit/Source/core/editing/InputMethodControllerTest.cpp

Issue 1889053003: Fix InputConnection.deleteSurroundingText() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: deleteContents doesn’t work well for multiple nodes. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "core/editing/InputMethodController.h" 5 #include "core/editing/InputMethodController.h"
6 6
7 #include "core/dom/Element.h" 7 #include "core/dom/Element.h"
8 #include "core/dom/Range.h" 8 #include "core/dom/Range.h"
9 #include "core/editing/FrameSelection.h" 9 #include "core/editing/FrameSelection.h"
10 #include "core/events/MouseEvent.h" 10 #include "core/events/MouseEvent.h"
(...skipping 175 matching lines...)
186 insertHTMLElement("<input id='sample' type='password' size='24'>", "samp le")); 186 insertHTMLElement("<input id='sample' type='password' size='24'>", "samp le"));
187 187
188 Vector<CompositionUnderline> underlines; 188 Vector<CompositionUnderline> underlines;
189 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); 189 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0));
190 controller().setComposition("foo", underlines, 0, 3); 190 controller().setComposition("foo", underlines, 0, 3);
191 controller().confirmComposition(); 191 controller().confirmComposition();
192 192
193 EXPECT_STREQ("foo", input->value().utf8().data()); 193 EXPECT_STREQ("foo", input->value().utf8().data());
194 } 194 }
195 195
196 TEST_F(InputMethodControllerTest, DeleteSurroundingTextWithEmptyText)
197 {
198 HTMLInputElement* input = toHTMLInputElement(
199 insertHTMLElement("<input id='sample'>", "sample"));
200
201 input->setValue("");
202 EXPECT_STREQ("", input->value().utf8().data());
203 controller().deleteSurroundingText(0, 0);
204 EXPECT_STREQ("", input->value().utf8().data());
205
206 input->setValue("");
207 EXPECT_STREQ("", input->value().utf8().data());
208 controller().deleteSurroundingText(1, 0);
209 EXPECT_STREQ("", input->value().utf8().data());
210
211 input->setValue("");
212 EXPECT_STREQ("", input->value().utf8().data());
213 controller().deleteSurroundingText(0, 1);
214 EXPECT_STREQ("", input->value().utf8().data());
215
216 input->setValue("");
217 EXPECT_STREQ("", input->value().utf8().data());
218 controller().deleteSurroundingText(1, 1);
219 EXPECT_STREQ("", input->value().utf8().data());
220 }
221
222 TEST_F(InputMethodControllerTest, DeleteSurroundingTextWithRangeSelection)
223 {
224 HTMLInputElement* input = toHTMLInputElement(
225 insertHTMLElement("<input id='sample'>", "sample"));
226
227 input->setValue("hello");
228 EXPECT_STREQ("hello", input->value().utf8().data());
229 controller().setEditableSelectionOffsets(PlainTextRange(1, 4));
230 controller().deleteSurroundingText(0, 0);
231 EXPECT_STREQ("hello", input->value().utf8().data());
232
233 input->setValue("hello");
234 EXPECT_STREQ("hello", input->value().utf8().data());
235 controller().setEditableSelectionOffsets(PlainTextRange(1, 4));
236 controller().deleteSurroundingText(1, 1);
237 EXPECT_STREQ("ell", input->value().utf8().data());
238
239 input->setValue("hello");
240 EXPECT_STREQ("hello", input->value().utf8().data());
241 controller().setEditableSelectionOffsets(PlainTextRange(1, 4));
242 controller().deleteSurroundingText(100, 0);
243 EXPECT_STREQ("ello", input->value().utf8().data());
244
245 input->setValue("hello");
246 EXPECT_STREQ("hello", input->value().utf8().data());
247 controller().setEditableSelectionOffsets(PlainTextRange(1, 4));
248 controller().deleteSurroundingText(0, 100);
249 EXPECT_STREQ("hell", input->value().utf8().data());
250
251 input->setValue("hello");
252 EXPECT_STREQ("hello", input->value().utf8().data());
253 controller().setEditableSelectionOffsets(PlainTextRange(1, 4));
254 controller().deleteSurroundingText(100, 100);
255 EXPECT_STREQ("ell", input->value().utf8().data());
256 }
257
258 TEST_F(InputMethodControllerTest, DeleteSurroundingTextWithCursorSelection)
259 {
260 HTMLInputElement* input = toHTMLInputElement(
261 insertHTMLElement("<input id='sample'>", "sample"));
262
263 input->setValue("hello");
264 EXPECT_STREQ("hello", input->value().utf8().data());
265 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
266 controller().deleteSurroundingText(1, 0);
267 EXPECT_STREQ("hllo", input->value().utf8().data());
268
269 input->setValue("hello");
270 EXPECT_STREQ("hello", input->value().utf8().data());
271 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
272 controller().deleteSurroundingText(0, 1);
273 EXPECT_STREQ("helo", input->value().utf8().data());
274
275 input->setValue("hello");
276 EXPECT_STREQ("hello", input->value().utf8().data());
277 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
278 controller().deleteSurroundingText(0, 0);
279 EXPECT_STREQ("hello", input->value().utf8().data());
280
281 input->setValue("hello");
282 EXPECT_STREQ("hello", input->value().utf8().data());
283 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
284 controller().deleteSurroundingText(1, 1);
285 EXPECT_STREQ("hlo", input->value().utf8().data());
286
287 input->setValue("hello");
288 EXPECT_STREQ("hello", input->value().utf8().data());
289 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
290 controller().deleteSurroundingText(100, 0);
291 EXPECT_STREQ("llo", input->value().utf8().data());
292
293 input->setValue("hello");
294 EXPECT_STREQ("hello", input->value().utf8().data());
295 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
296 controller().deleteSurroundingText(0, 100);
297 EXPECT_STREQ("he", input->value().utf8().data());
298
299 input->setValue("hello");
300 EXPECT_STREQ("hello", input->value().utf8().data());
301 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
302 controller().deleteSurroundingText(100, 100);
303 EXPECT_STREQ("", input->value().utf8().data());
304
305 input->setValue("h");
306 EXPECT_STREQ("h", input->value().utf8().data());
307 controller().setEditableSelectionOffsets(PlainTextRange(1, 1));
308 controller().deleteSurroundingText(1, 0);
309 EXPECT_STREQ("", input->value().utf8().data());
310
311 input->setValue("h");
312 EXPECT_STREQ("h", input->value().utf8().data());
313 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
314 controller().deleteSurroundingText(0, 1);
315 EXPECT_STREQ("", input->value().utf8().data());
316 }
317
318 TEST_F(InputMethodControllerTest, DeleteSurroundingTextWithMultiCodeTextOnTheLef t)
319 {
320 HTMLInputElement* input = toHTMLInputElement(
321 insertHTMLElement("<input id='sample'>", "sample"));
322
323 // U+2605 == "black star". It takes up 1 space.
324 input->setValue(String::fromUTF8("foo\xE2\x98\x85"));
325 controller().setEditableSelectionOffsets(PlainTextRange(4, 4));
326 EXPECT_STREQ("foo\xE2\x98\x85", input->value().utf8().data());
327 controller().deleteSurroundingText(1, 0);
328 EXPECT_STREQ("foo", input->value().utf8().data());
329
330 // U+1F3C6 == "trophy". It takes up 2 space.
331 input->setValue(String::fromUTF8("foo\xF0\x9F\x8F\x86"));
332 controller().setEditableSelectionOffsets(PlainTextRange(5, 5));
333 EXPECT_STREQ("foo\xF0\x9F\x8F\x86", input->value().utf8().data());
334 controller().deleteSurroundingText(1, 0);
335 EXPECT_STREQ("foo", input->value().utf8().data());
336
337 // composed U+0E01 "ka kai" + U+0E49 "mai tho". It takes up 2 space.
338 input->setValue(String::fromUTF8("foo\xE0\xB8\x81\xE0\xB9\x89"));
339 controller().setEditableSelectionOffsets(PlainTextRange(5, 5));
340 EXPECT_STREQ("foo\xE0\xB8\x81\xE0\xB9\x89", input->value().utf8().data());
341 controller().deleteSurroundingText(1, 0);
342 EXPECT_STREQ("foo", input->value().utf8().data());
343
344 // "trophy" + "trophy".
345 input->setValue(String::fromUTF8("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86"));
346 controller().setEditableSelectionOffsets(PlainTextRange(7, 7));
347 EXPECT_STREQ("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86", input->value().utf8().da ta());
348 controller().deleteSurroundingText(2, 0);
349 EXPECT_STREQ("foo\xF0\x9F\x8F\x86", input->value().utf8().data());
350
351 // "trophy" + "trophy".
352 input->setValue(String::fromUTF8("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86"));
353 controller().setEditableSelectionOffsets(PlainTextRange(7, 7));
354 EXPECT_STREQ("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86", input->value().utf8().da ta());
355 controller().deleteSurroundingText(3, 0);
356 EXPECT_STREQ("foo", input->value().utf8().data());
357
358 // "trophy" + "trophy".
359 input->setValue(String::fromUTF8("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86"));
360 controller().setEditableSelectionOffsets(PlainTextRange(7, 7));
361 EXPECT_STREQ("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86", input->value().utf8().da ta());
362 controller().deleteSurroundingText(4, 0);
363 EXPECT_STREQ("foo", input->value().utf8().data());
364
365 // "trophy" + "trophy".
366 input->setValue(String::fromUTF8("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86"));
367 controller().setEditableSelectionOffsets(PlainTextRange(7, 7));
368 EXPECT_STREQ("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86", input->value().utf8().da ta());
369 controller().deleteSurroundingText(5, 0);
370 EXPECT_STREQ("fo", input->value().utf8().data());
371 }
372
373 TEST_F(InputMethodControllerTest, DeleteSurroundingTextWithMultiCodeTextOnTheRig ht)
374 {
375 HTMLInputElement* input = toHTMLInputElement(
376 insertHTMLElement("<input id='sample'>", "sample"));
377
378 // U+2605 == "black star". It takes up 1 space.
379 input->setValue(String::fromUTF8("\xE2\x98\x85 foo"));
380 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
381 EXPECT_STREQ("\xE2\x98\x85 foo", input->value().utf8().data());
382 controller().deleteSurroundingText(0, 1);
383 EXPECT_STREQ(" foo", input->value().utf8().data());
384
385 // U+1F3C6 == "trophy". It takes up 2 space.
386 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86 foo"));
387 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
388 EXPECT_STREQ("\xF0\x9F\x8F\x86 foo", input->value().utf8().data());
389 controller().deleteSurroundingText(0, 1);
390 EXPECT_STREQ(" foo", input->value().utf8().data());
391
392 // composed U+0E01 "ka kai" + U+0E49 "mai tho". It takes up 2 space.
393 input->setValue(String::fromUTF8("\xE0\xB8\x81\xE0\xB9\x89 foo"));
394 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
395 EXPECT_STREQ("\xE0\xB8\x81\xE0\xB9\x89 foo", input->value().utf8().data());
396 controller().deleteSurroundingText(0, 1);
397 EXPECT_STREQ(" foo", input->value().utf8().data());
398
399 // "trophy" + "trophy".
400 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo"));
401 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
402 EXPECT_STREQ("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo", input->value().utf8().d ata());
403 controller().deleteSurroundingText(0, 2);
404 EXPECT_STREQ("\xF0\x9F\x8F\x86 foo", input->value().utf8().data());
405
406 // "trophy" + "trophy".
407 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo"));
408 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
409 EXPECT_STREQ("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo", input->value().utf8().d ata());
410 controller().deleteSurroundingText(0, 3);
411 EXPECT_STREQ(" foo", input->value().utf8().data());
412
413 // "trophy" + "trophy".
414 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo"));
415 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
416 EXPECT_STREQ("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo", input->value().utf8().d ata());
417 controller().deleteSurroundingText(0, 4);
418 EXPECT_STREQ(" foo", input->value().utf8().data());
419
420 // "trophy" + "trophy".
421 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo"));
422 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
423 EXPECT_STREQ("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo", input->value().utf8().d ata());
424 controller().deleteSurroundingText(0, 5);
425 EXPECT_STREQ("foo", input->value().utf8().data());
426 }
427
428 TEST_F(InputMethodControllerTest, DeleteSurroundingTextWithMultiCodeTextOnBothSi des)
429 {
430 HTMLInputElement* input = toHTMLInputElement(
431 insertHTMLElement("<input id='sample'>", "sample"));
432
433 // "trophy" + "trophy".
434 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86"));
435 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
436 EXPECT_STREQ("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86", input->value().utf8().data( ));
437 controller().deleteSurroundingText(1, 1);
438 EXPECT_STREQ("", input->value().utf8().data());
439 }
440
441 TEST_F(InputMethodControllerTest, DeleteSurroundingTextForMultipleNodes)
442 {
443 Element* div = insertHTMLElement(
444 "<div id='sample' contenteditable='true'>"
445 "hello"
446 "<div id='sample2' contenteditable='true'>world</div>"
447 "</div>",
448 "sample");
449
450 controller().setEditableSelectionOffsets(PlainTextRange(6, 6));
451 EXPECT_STREQ("hello\nworld", div->innerText().utf8().data());
452 EXPECT_EQ(6u, controller().getSelectionOffsets().start());
453 EXPECT_EQ(6u, controller().getSelectionOffsets().end());
454
yabinh 2016/05/23 10:24:59 deleteContents doesn’t work well for multiple node
455 // '\n' will not be deleted.
456 controller().deleteSurroundingText(1, 0);
457 EXPECT_STREQ("hello\nworld", div->innerText().utf8().data());
458
459 // '\n' will not be deleted.
460 controller().deleteSurroundingText(2, 0);
461 EXPECT_STREQ("hell\nworld", div->innerText().utf8().data());
462
463 // '\n' can be deleted when the whole node before '\n' is deleted.
464 controller().deleteSurroundingText(5, 0);
465 EXPECT_STREQ("world", div->innerText().utf8().data());
466 }
467
196 TEST_F(InputMethodControllerTest, SetCompositionForInputWithDifferentNewCursorPo sitions) 468 TEST_F(InputMethodControllerTest, SetCompositionForInputWithDifferentNewCursorPo sitions)
197 { 469 {
198 HTMLInputElement* input = toHTMLInputElement( 470 HTMLInputElement* input = toHTMLInputElement(
199 insertHTMLElement("<input id='sample'>", "sample")); 471 insertHTMLElement("<input id='sample'>", "sample"));
200 472
201 input->setValue("hello"); 473 input->setValue("hello");
202 controller().setEditableSelectionOffsets(PlainTextRange(2, 2)); 474 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
203 EXPECT_STREQ("hello", input->value().utf8().data()); 475 EXPECT_STREQ("hello", input->value().utf8().data());
204 EXPECT_EQ(2u, controller().getSelectionOffsets().start()); 476 EXPECT_EQ(2u, controller().getSelectionOffsets().start());
205 EXPECT_EQ(2u, controller().getSelectionOffsets().end()); 477 EXPECT_EQ(2u, controller().getSelectionOffsets().end());
(...skipping 163 matching lines...)
369 document().setTitle(emptyString()); 641 document().setTitle(emptyString());
370 controller().setComposition("foo", underlines, 0, 3); 642 controller().setComposition("foo", underlines, 0, 3);
371 EXPECT_STREQ("beforeinput.isComposing:true", document().title().utf8().data( )); 643 EXPECT_STREQ("beforeinput.isComposing:true", document().title().utf8().data( ));
372 644
373 document().setTitle(emptyString()); 645 document().setTitle(emptyString());
374 controller().confirmComposition(); 646 controller().confirmComposition();
375 EXPECT_STREQ("beforeinput.isComposing:false", document().title().utf8().data ()); 647 EXPECT_STREQ("beforeinput.isComposing:false", document().title().utf8().data ());
376 } 648 }
377 649
378 } // namespace blink 650 } // namespace blink
OLDNEW

Powered by Google App Engine