Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(854)

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

Issue 2020973002: Reland: Fix setComposingText with empty text when newCursorPosition != 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed a unit test. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 element->focus(); 48 element->focus();
49 return element; 49 return element;
50 } 50 }
51 51
52 TEST_F(InputMethodControllerTest, BackspaceFromEndOfInput) 52 TEST_F(InputMethodControllerTest, BackspaceFromEndOfInput)
53 { 53 {
54 HTMLInputElement* input = toHTMLInputElement( 54 HTMLInputElement* input = toHTMLInputElement(
55 insertHTMLElement("<input id='sample'>", "sample")); 55 insertHTMLElement("<input id='sample'>", "sample"));
56 56
57 input->setValue("fooX"); 57 input->setValue("fooX");
58 controller().setEditableSelectionOffsets(PlainTextRange(4, 4)); 58 controller().setEditableSelectionOffsets(4, 4);
59 EXPECT_STREQ("fooX", input->value().utf8().data()); 59 EXPECT_STREQ("fooX", input->value().utf8().data());
60 controller().extendSelectionAndDelete(0, 0); 60 controller().extendSelectionAndDelete(0, 0);
61 EXPECT_STREQ("fooX", input->value().utf8().data()); 61 EXPECT_STREQ("fooX", input->value().utf8().data());
62 62
63 input->setValue("fooX"); 63 input->setValue("fooX");
64 controller().setEditableSelectionOffsets(PlainTextRange(4, 4)); 64 controller().setEditableSelectionOffsets(4, 4);
65 EXPECT_STREQ("fooX", input->value().utf8().data()); 65 EXPECT_STREQ("fooX", input->value().utf8().data());
66 controller().extendSelectionAndDelete(1, 0); 66 controller().extendSelectionAndDelete(1, 0);
67 EXPECT_STREQ("foo", input->value().utf8().data()); 67 EXPECT_STREQ("foo", input->value().utf8().data());
68 68
69 input->setValue(String::fromUTF8("foo\xE2\x98\x85")); // U+2605 == "black st ar" 69 input->setValue(String::fromUTF8("foo\xE2\x98\x85")); // U+2605 == "black st ar"
70 controller().setEditableSelectionOffsets(PlainTextRange(4, 4)); 70 controller().setEditableSelectionOffsets(4, 4);
71 EXPECT_STREQ("foo\xE2\x98\x85", input->value().utf8().data()); 71 EXPECT_STREQ("foo\xE2\x98\x85", input->value().utf8().data());
72 controller().extendSelectionAndDelete(1, 0); 72 controller().extendSelectionAndDelete(1, 0);
73 EXPECT_STREQ("foo", input->value().utf8().data()); 73 EXPECT_STREQ("foo", input->value().utf8().data());
74 74
75 input->setValue(String::fromUTF8("foo\xF0\x9F\x8F\x86")); // U+1F3C6 == "tro phy" 75 input->setValue(String::fromUTF8("foo\xF0\x9F\x8F\x86")); // U+1F3C6 == "tro phy"
76 controller().setEditableSelectionOffsets(PlainTextRange(4, 4)); 76 controller().setEditableSelectionOffsets(4, 4);
77 EXPECT_STREQ("foo\xF0\x9F\x8F\x86", input->value().utf8().data()); 77 EXPECT_STREQ("foo\xF0\x9F\x8F\x86", input->value().utf8().data());
78 controller().extendSelectionAndDelete(1, 0); 78 controller().extendSelectionAndDelete(1, 0);
79 EXPECT_STREQ("foo", input->value().utf8().data()); 79 EXPECT_STREQ("foo", input->value().utf8().data());
80 80
81 input->setValue(String::fromUTF8("foo\xE0\xB8\x81\xE0\xB9\x89")); // compose d U+0E01 "ka kai" + U+0E49 "mai tho" 81 input->setValue(String::fromUTF8("foo\xE0\xB8\x81\xE0\xB9\x89")); // compose d U+0E01 "ka kai" + U+0E49 "mai tho"
82 controller().setEditableSelectionOffsets(PlainTextRange(4, 4)); 82 controller().setEditableSelectionOffsets(4, 4);
83 EXPECT_STREQ("foo\xE0\xB8\x81\xE0\xB9\x89", input->value().utf8().data()); 83 EXPECT_STREQ("foo\xE0\xB8\x81\xE0\xB9\x89", input->value().utf8().data());
84 controller().extendSelectionAndDelete(1, 0); 84 controller().extendSelectionAndDelete(1, 0);
85 EXPECT_STREQ("foo", input->value().utf8().data()); 85 EXPECT_STREQ("foo", input->value().utf8().data());
86 86
87 input->setValue("fooX"); 87 input->setValue("fooX");
88 controller().setEditableSelectionOffsets(PlainTextRange(4, 4)); 88 controller().setEditableSelectionOffsets(4, 4);
89 EXPECT_STREQ("fooX", input->value().utf8().data()); 89 EXPECT_STREQ("fooX", input->value().utf8().data());
90 controller().extendSelectionAndDelete(0, 1); 90 controller().extendSelectionAndDelete(0, 1);
91 EXPECT_STREQ("fooX", input->value().utf8().data()); 91 EXPECT_STREQ("fooX", input->value().utf8().data());
92 } 92 }
93 93
94 TEST_F(InputMethodControllerTest, SetCompositionFromExistingText) 94 TEST_F(InputMethodControllerTest, SetCompositionFromExistingText)
95 { 95 {
96 Element* div = insertHTMLElement( 96 Element* div = insertHTMLElement(
97 "<div id='sample' contenteditable='true'>hello world</div>", "sample"); 97 "<div id='sample' contenteditable='true'>hello world</div>", "sample");
98 98
(...skipping 23 matching lines...) Expand all
122 EXPECT_EQ(0, frame().selection().start().computeOffsetInContainerNode()); 122 EXPECT_EQ(0, frame().selection().start().computeOffsetInContainerNode());
123 EXPECT_EQ(0, frame().selection().end().computeOffsetInContainerNode()); 123 EXPECT_EQ(0, frame().selection().end().computeOffsetInContainerNode());
124 } 124 }
125 125
126 TEST_F(InputMethodControllerTest, DeleteBySettingEmptyComposition) 126 TEST_F(InputMethodControllerTest, DeleteBySettingEmptyComposition)
127 { 127 {
128 HTMLInputElement* input = toHTMLInputElement( 128 HTMLInputElement* input = toHTMLInputElement(
129 insertHTMLElement("<input id='sample'>", "sample")); 129 insertHTMLElement("<input id='sample'>", "sample"));
130 130
131 input->setValue("foo "); 131 input->setValue("foo ");
132 controller().setEditableSelectionOffsets(PlainTextRange(4, 4)); 132 controller().setEditableSelectionOffsets(4, 4);
133 EXPECT_STREQ("foo ", input->value().utf8().data()); 133 EXPECT_STREQ("foo ", input->value().utf8().data());
134 controller().extendSelectionAndDelete(0, 0); 134 controller().extendSelectionAndDelete(0, 0);
135 EXPECT_STREQ("foo ", input->value().utf8().data()); 135 EXPECT_STREQ("foo ", input->value().utf8().data());
136 136
137 input->setValue("foo "); 137 input->setValue("foo ");
138 controller().setEditableSelectionOffsets(PlainTextRange(4, 4)); 138 controller().setEditableSelectionOffsets(4, 4);
139 EXPECT_STREQ("foo ", input->value().utf8().data()); 139 EXPECT_STREQ("foo ", input->value().utf8().data());
140 controller().extendSelectionAndDelete(1, 0); 140 controller().extendSelectionAndDelete(1, 0);
141 EXPECT_STREQ("foo", input->value().utf8().data()); 141 EXPECT_STREQ("foo", input->value().utf8().data());
142 142
143 Vector<CompositionUnderline> underlines; 143 Vector<CompositionUnderline> underlines;
144 underlines.append(CompositionUnderline(0, 3, Color(255, 0, 0), false, 0)); 144 underlines.append(CompositionUnderline(0, 3, Color(255, 0, 0), false, 0));
145 controller().setCompositionFromExistingText(underlines, 0, 3); 145 controller().setCompositionFromExistingText(underlines, 0, 3);
146 146
147 controller().setComposition(String(""), underlines, 0, 3); 147 controller().setComposition(String(""), underlines, 0, 3);
148 148
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
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, SetCompositionForInputWithDifferentNewCursorPo sitions) 196 TEST_F(InputMethodControllerTest, SetCompositionForInputWithDifferentNewCursorPo sitions)
197 { 197 {
198 HTMLInputElement* input = toHTMLInputElement( 198 HTMLInputElement* input = toHTMLInputElement(
199 insertHTMLElement("<input id='sample'>", "sample")); 199 insertHTMLElement("<input id='sample'>", "sample"));
200 200
201 input->setValue("hello"); 201 input->setValue("hello");
202 controller().setEditableSelectionOffsets(PlainTextRange(2, 2)); 202 controller().setEditableSelectionOffsets(2, 2);
203 EXPECT_STREQ("hello", input->value().utf8().data()); 203 EXPECT_STREQ("hello", input->value().utf8().data());
204 EXPECT_EQ(2u, controller().getSelectionOffsets().start()); 204 EXPECT_EQ(2u, controller().getSelectionOffsets().start());
205 EXPECT_EQ(2u, controller().getSelectionOffsets().end()); 205 EXPECT_EQ(2u, controller().getSelectionOffsets().end());
206 206
207 Vector<CompositionUnderline> underlines; 207 Vector<CompositionUnderline> underlines;
208 underlines.append(CompositionUnderline(0, 2, Color(255, 0, 0), false, 0)); 208 underlines.append(CompositionUnderline(0, 2, Color(255, 0, 0), false, 0));
209 209
210 // The cursor exceeds left boundary. 210 // The cursor exceeds left boundary.
211 // "*heABllo", where * stands for cursor. 211 // "*heABllo", where * stands for cursor.
212 controller().setComposition("AB", underlines, -100, -100); 212 controller().setComposition("AB", underlines, -100, -100);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 // There are 7 nodes and 5+1+5+1+3+4+3 characters: "hello", '\n', "world", " \n", "012", "3456", "789". 255 // There are 7 nodes and 5+1+5+1+3+4+3 characters: "hello", '\n', "world", " \n", "012", "3456", "789".
256 Element* div = insertHTMLElement( 256 Element* div = insertHTMLElement(
257 "<div id='sample' contenteditable='true'>" 257 "<div id='sample' contenteditable='true'>"
258 "hello" 258 "hello"
259 "<div id='sample2' contenteditable='true'>world" 259 "<div id='sample2' contenteditable='true'>world"
260 "<p>012<b>3456</b><i>789</i></p>" 260 "<p>012<b>3456</b><i>789</i></p>"
261 "</div>" 261 "</div>"
262 "</div>", 262 "</div>",
263 "sample"); 263 "sample");
264 264
265 controller().setEditableSelectionOffsets(PlainTextRange(17, 17)); 265 controller().setEditableSelectionOffsets(17, 17);
266 EXPECT_STREQ("hello\nworld\n0123456789", div->innerText().utf8().data()); 266 EXPECT_STREQ("hello\nworld\n0123456789", div->innerText().utf8().data());
267 EXPECT_EQ(17u, controller().getSelectionOffsets().start()); 267 EXPECT_EQ(17u, controller().getSelectionOffsets().start());
268 EXPECT_EQ(17u, controller().getSelectionOffsets().end()); 268 EXPECT_EQ(17u, controller().getSelectionOffsets().end());
269 269
270 Vector<CompositionUnderline> underlines; 270 Vector<CompositionUnderline> underlines;
271 underlines.append(CompositionUnderline(0, 2, Color(255, 0, 0), false, 0)); 271 underlines.append(CompositionUnderline(0, 2, Color(255, 0, 0), false, 0));
272 272
273 // The cursor exceeds left boundary. 273 // The cursor exceeds left boundary.
274 // "*hello\nworld\n01234AB56789", where * stands for cursor. 274 // "*hello\nworld\n01234AB56789", where * stands for cursor.
275 controller().setComposition("AB", underlines, -100, -100); 275 controller().setComposition("AB", underlines, -100, -100);
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 EXPECT_EQ(24u, controller().getSelectionOffsets().end()); 341 EXPECT_EQ(24u, controller().getSelectionOffsets().end());
342 342
343 // The cursor exceeds right boundary. 343 // The cursor exceeds right boundary.
344 // "hello\nworld\n01234AB56789*". 344 // "hello\nworld\n01234AB56789*".
345 controller().setComposition("AB", underlines, 100, 100); 345 controller().setComposition("AB", underlines, 100, 100);
346 EXPECT_STREQ("hello\nworld\n01234AB56789", div->innerText().utf8().data()); 346 EXPECT_STREQ("hello\nworld\n01234AB56789", div->innerText().utf8().data());
347 EXPECT_EQ(24u, controller().getSelectionOffsets().start()); 347 EXPECT_EQ(24u, controller().getSelectionOffsets().start());
348 EXPECT_EQ(24u, controller().getSelectionOffsets().end()); 348 EXPECT_EQ(24u, controller().getSelectionOffsets().end());
349 } 349 }
350 350
351 TEST_F(InputMethodControllerTest, SetCompositionWithEmptyText)
352 {
353 Element* div = insertHTMLElement(
354 "<div id='sample' contenteditable='true'>hello</div>",
355 "sample");
356
357 controller().setEditableSelectionOffsets(2, 2);
358 EXPECT_STREQ("hello", div->innerText().utf8().data());
359 EXPECT_EQ(2u, controller().getSelectionOffsets().start());
360 EXPECT_EQ(2u, controller().getSelectionOffsets().end());
361
362 Vector<CompositionUnderline> underlines0;
363 underlines0.append(CompositionUnderline(0, 0, Color(255, 0, 0), false, 0));
364 Vector<CompositionUnderline> underlines2;
365 underlines2.append(CompositionUnderline(0, 2, Color(255, 0, 0), false, 0));
366
367 controller().setComposition("AB", underlines2, 2, 2);
368 // With previous composition.
369 controller().setComposition("", underlines0, 2, 2);
370 EXPECT_STREQ("hello", div->innerText().utf8().data());
371 EXPECT_EQ(4u, controller().getSelectionOffsets().start());
372 EXPECT_EQ(4u, controller().getSelectionOffsets().end());
373
374 // Without previous composition.
375 controller().setComposition("", underlines0, -1, -1);
376 EXPECT_STREQ("hello", div->innerText().utf8().data());
377 EXPECT_EQ(3u, controller().getSelectionOffsets().start());
378 EXPECT_EQ(3u, controller().getSelectionOffsets().end());
379 }
380
351 TEST_F(InputMethodControllerTest, CompositionFireBeforeInput) 381 TEST_F(InputMethodControllerTest, CompositionFireBeforeInput)
352 { 382 {
353 document().settings()->setScriptEnabled(true); 383 document().settings()->setScriptEnabled(true);
354 Element* editable = insertHTMLElement("<div id='sample' contentEditable='tru e'></div>", "sample"); 384 Element* editable = insertHTMLElement("<div id='sample' contentEditable='tru e'></div>", "sample");
355 Element* script = document().createElement("script", ASSERT_NO_EXCEPTION); 385 Element* script = document().createElement("script", ASSERT_NO_EXCEPTION);
356 script->setInnerHTML( 386 script->setInnerHTML(
357 "document.getElementById('sample').addEventListener('beforeinput', funct ion(event) {" 387 "document.getElementById('sample').addEventListener('beforeinput', funct ion(event) {"
358 " document.title = `beforeinput.isComposing:${event.isComposing}`;" 388 " document.title = `beforeinput.isComposing:${event.isComposing}`;"
359 "});", 389 "});",
360 ASSERT_NO_EXCEPTION); 390 ASSERT_NO_EXCEPTION);
361 document().body()->appendChild(script, ASSERT_NO_EXCEPTION); 391 document().body()->appendChild(script, ASSERT_NO_EXCEPTION);
362 document().view()->updateAllLifecyclePhases(); 392 document().view()->updateAllLifecyclePhases();
363 393
364 // Simulate composition in the |contentEditable|. 394 // Simulate composition in the |contentEditable|.
365 Vector<CompositionUnderline> underlines; 395 Vector<CompositionUnderline> underlines;
366 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); 396 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0));
367 editable->focus(); 397 editable->focus();
368 398
369 document().setTitle(emptyString()); 399 document().setTitle(emptyString());
370 controller().setComposition("foo", underlines, 0, 3); 400 controller().setComposition("foo", underlines, 0, 3);
371 EXPECT_STREQ("beforeinput.isComposing:true", document().title().utf8().data( )); 401 EXPECT_STREQ("beforeinput.isComposing:true", document().title().utf8().data( ));
372 402
373 document().setTitle(emptyString()); 403 document().setTitle(emptyString());
374 controller().confirmComposition(); 404 controller().confirmComposition();
375 // Last 'beforeinput' should also be inside composition scope. 405 // Last 'beforeinput' should also be inside composition scope.
376 EXPECT_STREQ("beforeinput.isComposing:true", document().title().utf8().data( )); 406 EXPECT_STREQ("beforeinput.isComposing:true", document().title().utf8().data( ));
377 } 407 }
378 408
379 } // namespace blink 409 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698