OLD | NEW |
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...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |
| 217 TEST_F(InputMethodControllerTest, DeleteSurroundingTextWithRangeSelection) |
| 218 { |
| 219 HTMLInputElement* input = toHTMLInputElement( |
| 220 insertHTMLElement("<input id='sample'>", "sample")); |
| 221 |
| 222 input->setValue("hello"); |
| 223 EXPECT_STREQ("hello", input->value().utf8().data()); |
| 224 controller().setEditableSelectionOffsets(PlainTextRange(1, 4)); |
| 225 controller().deleteSurroundingText(0, 0); |
| 226 EXPECT_STREQ("hello", input->value().utf8().data()); |
| 227 |
| 228 input->setValue("hello"); |
| 229 EXPECT_STREQ("hello", input->value().utf8().data()); |
| 230 controller().setEditableSelectionOffsets(PlainTextRange(1, 4)); |
| 231 controller().deleteSurroundingText(1, 1); |
| 232 EXPECT_STREQ("ell", input->value().utf8().data()); |
| 233 |
| 234 input->setValue("hello"); |
| 235 EXPECT_STREQ("hello", input->value().utf8().data()); |
| 236 controller().setEditableSelectionOffsets(PlainTextRange(1, 4)); |
| 237 controller().deleteSurroundingText(100, 0); |
| 238 EXPECT_STREQ("ello", input->value().utf8().data()); |
| 239 |
| 240 input->setValue("hello"); |
| 241 EXPECT_STREQ("hello", input->value().utf8().data()); |
| 242 controller().setEditableSelectionOffsets(PlainTextRange(1, 4)); |
| 243 controller().deleteSurroundingText(0, 100); |
| 244 EXPECT_STREQ("hell", input->value().utf8().data()); |
| 245 } |
| 246 |
| 247 TEST_F(InputMethodControllerTest, DeleteSurroundingTextWithCursorSelection) |
| 248 { |
| 249 HTMLInputElement* input = toHTMLInputElement( |
| 250 insertHTMLElement("<input id='sample'>", "sample")); |
| 251 |
| 252 input->setValue("hello"); |
| 253 EXPECT_STREQ("hello", input->value().utf8().data()); |
| 254 controller().setEditableSelectionOffsets(PlainTextRange(2, 2)); |
| 255 controller().deleteSurroundingText(0, 0); |
| 256 EXPECT_STREQ("hello", input->value().utf8().data()); |
| 257 |
| 258 input->setValue("hello"); |
| 259 EXPECT_STREQ("hello", input->value().utf8().data()); |
| 260 controller().setEditableSelectionOffsets(PlainTextRange(2, 2)); |
| 261 controller().deleteSurroundingText(1, 1); |
| 262 EXPECT_STREQ("hlo", input->value().utf8().data()); |
| 263 |
| 264 input->setValue("hello"); |
| 265 EXPECT_STREQ("hello", input->value().utf8().data()); |
| 266 controller().setEditableSelectionOffsets(PlainTextRange(2, 2)); |
| 267 controller().deleteSurroundingText(100, 0); |
| 268 EXPECT_STREQ("llo", input->value().utf8().data()); |
| 269 |
| 270 input->setValue("hello"); |
| 271 EXPECT_STREQ("hello", input->value().utf8().data()); |
| 272 controller().setEditableSelectionOffsets(PlainTextRange(2, 2)); |
| 273 controller().deleteSurroundingText(0, 100); |
| 274 EXPECT_STREQ("he", input->value().utf8().data()); |
| 275 } |
| 276 |
| 277 TEST_F(InputMethodControllerTest, DeleteSurroundingTextWithMultiCodeTextOnTheLef
t) |
| 278 { |
| 279 HTMLInputElement* input = toHTMLInputElement( |
| 280 insertHTMLElement("<input id='sample'>", "sample")); |
| 281 |
| 282 // U+2605 == "black star". It takes up 1 space. |
| 283 input->setValue(String::fromUTF8("foo\xE2\x98\x85")); |
| 284 controller().setEditableSelectionOffsets(PlainTextRange(4, 4)); |
| 285 EXPECT_STREQ("foo\xE2\x98\x85", input->value().utf8().data()); |
| 286 controller().deleteSurroundingText(1, 0); |
| 287 EXPECT_STREQ("foo", input->value().utf8().data()); |
| 288 |
| 289 // U+1F3C6 == "trophy". It takes up 2 space. |
| 290 input->setValue(String::fromUTF8("foo\xF0\x9F\x8F\x86")); |
| 291 controller().setEditableSelectionOffsets(PlainTextRange(5, 5)); |
| 292 EXPECT_STREQ("foo\xF0\x9F\x8F\x86", input->value().utf8().data()); |
| 293 controller().deleteSurroundingText(1, 0); |
| 294 EXPECT_STREQ("foo", input->value().utf8().data()); |
| 295 |
| 296 // composed U+0E01 "ka kai" + U+0E49 "mai tho". It takes up 2 space. |
| 297 input->setValue(String::fromUTF8("foo\xE0\xB8\x81\xE0\xB9\x89")); |
| 298 controller().setEditableSelectionOffsets(PlainTextRange(5, 5)); |
| 299 EXPECT_STREQ("foo\xE0\xB8\x81\xE0\xB9\x89", input->value().utf8().data()); |
| 300 controller().deleteSurroundingText(1, 0); |
| 301 EXPECT_STREQ("foo", input->value().utf8().data()); |
| 302 |
| 303 // "trophy" + "trophy". |
| 304 input->setValue(String::fromUTF8("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86")); |
| 305 controller().setEditableSelectionOffsets(PlainTextRange(7, 7)); |
| 306 EXPECT_STREQ("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86", input->value().utf8().da
ta()); |
| 307 controller().deleteSurroundingText(2, 0); |
| 308 EXPECT_STREQ("foo\xF0\x9F\x8F\x86", input->value().utf8().data()); |
| 309 |
| 310 // "trophy" + "trophy". |
| 311 input->setValue(String::fromUTF8("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86")); |
| 312 controller().setEditableSelectionOffsets(PlainTextRange(7, 7)); |
| 313 EXPECT_STREQ("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86", input->value().utf8().da
ta()); |
| 314 controller().deleteSurroundingText(3, 0); |
| 315 EXPECT_STREQ("foo", input->value().utf8().data()); |
| 316 |
| 317 // "trophy" + "trophy". |
| 318 input->setValue(String::fromUTF8("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86")); |
| 319 controller().setEditableSelectionOffsets(PlainTextRange(7, 7)); |
| 320 EXPECT_STREQ("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86", input->value().utf8().da
ta()); |
| 321 controller().deleteSurroundingText(4, 0); |
| 322 EXPECT_STREQ("foo", input->value().utf8().data()); |
| 323 |
| 324 // "trophy" + "trophy". |
| 325 input->setValue(String::fromUTF8("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86")); |
| 326 controller().setEditableSelectionOffsets(PlainTextRange(7, 7)); |
| 327 EXPECT_STREQ("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86", input->value().utf8().da
ta()); |
| 328 controller().deleteSurroundingText(5, 0); |
| 329 EXPECT_STREQ("fo", input->value().utf8().data()); |
| 330 } |
| 331 |
| 332 TEST_F(InputMethodControllerTest, DeleteSurroundingTextWithMultiCodeTextOnTheRig
ht) |
| 333 { |
| 334 HTMLInputElement* input = toHTMLInputElement( |
| 335 insertHTMLElement("<input id='sample'>", "sample")); |
| 336 |
| 337 // U+2605 == "black star". It takes up 1 space. |
| 338 input->setValue(String::fromUTF8("\xE2\x98\x85 foo")); |
| 339 controller().setEditableSelectionOffsets(PlainTextRange(0, 0)); |
| 340 EXPECT_STREQ("\xE2\x98\x85 foo", input->value().utf8().data()); |
| 341 controller().deleteSurroundingText(0, 1); |
| 342 EXPECT_STREQ(" foo", input->value().utf8().data()); |
| 343 |
| 344 // U+1F3C6 == "trophy". It takes up 2 space. |
| 345 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86 foo")); |
| 346 controller().setEditableSelectionOffsets(PlainTextRange(0, 0)); |
| 347 EXPECT_STREQ("\xF0\x9F\x8F\x86 foo", input->value().utf8().data()); |
| 348 controller().deleteSurroundingText(0, 1); |
| 349 EXPECT_STREQ(" foo", input->value().utf8().data()); |
| 350 |
| 351 // composed U+0E01 "ka kai" + U+0E49 "mai tho". It takes up 2 space. |
| 352 input->setValue(String::fromUTF8("\xE0\xB8\x81\xE0\xB9\x89 foo")); |
| 353 controller().setEditableSelectionOffsets(PlainTextRange(0, 0)); |
| 354 EXPECT_STREQ("\xE0\xB8\x81\xE0\xB9\x89 foo", input->value().utf8().data()); |
| 355 controller().deleteSurroundingText(0, 1); |
| 356 EXPECT_STREQ(" foo", input->value().utf8().data()); |
| 357 |
| 358 // "trophy" + "trophy". |
| 359 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo")); |
| 360 controller().setEditableSelectionOffsets(PlainTextRange(0, 0)); |
| 361 EXPECT_STREQ("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo", input->value().utf8().d
ata()); |
| 362 controller().deleteSurroundingText(0, 2); |
| 363 EXPECT_STREQ("\xF0\x9F\x8F\x86 foo", input->value().utf8().data()); |
| 364 |
| 365 // "trophy" + "trophy". |
| 366 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo")); |
| 367 controller().setEditableSelectionOffsets(PlainTextRange(0, 0)); |
| 368 EXPECT_STREQ("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo", input->value().utf8().d
ata()); |
| 369 controller().deleteSurroundingText(0, 3); |
| 370 EXPECT_STREQ(" foo", input->value().utf8().data()); |
| 371 |
| 372 // "trophy" + "trophy". |
| 373 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo")); |
| 374 controller().setEditableSelectionOffsets(PlainTextRange(0, 0)); |
| 375 EXPECT_STREQ("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo", input->value().utf8().d
ata()); |
| 376 controller().deleteSurroundingText(0, 4); |
| 377 EXPECT_STREQ(" foo", input->value().utf8().data()); |
| 378 |
| 379 // "trophy" + "trophy". |
| 380 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo")); |
| 381 controller().setEditableSelectionOffsets(PlainTextRange(0, 0)); |
| 382 EXPECT_STREQ("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo", input->value().utf8().d
ata()); |
| 383 controller().deleteSurroundingText(0, 5); |
| 384 EXPECT_STREQ("foo", input->value().utf8().data()); |
| 385 } |
| 386 |
| 387 TEST_F(InputMethodControllerTest, DeleteSurroundingTextWithMultiCodeTextOnBothSi
des) |
| 388 { |
| 389 HTMLInputElement* input = toHTMLInputElement( |
| 390 insertHTMLElement("<input id='sample'>", "sample")); |
| 391 |
| 392 // "trophy" + "trophy". |
| 393 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86")); |
| 394 controller().setEditableSelectionOffsets(PlainTextRange(2, 2)); |
| 395 EXPECT_STREQ("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86", input->value().utf8().data(
)); |
| 396 controller().deleteSurroundingText(1, 1); |
| 397 EXPECT_STREQ("", input->value().utf8().data()); |
| 398 } |
| 399 |
196 TEST_F(InputMethodControllerTest, CompositionFireBeforeInput) | 400 TEST_F(InputMethodControllerTest, CompositionFireBeforeInput) |
197 { | 401 { |
198 document().settings()->setScriptEnabled(true); | 402 document().settings()->setScriptEnabled(true); |
199 Element* editable = insertHTMLElement("<div id='sample' contentEditable='tru
e'></div>", "sample"); | 403 Element* editable = insertHTMLElement("<div id='sample' contentEditable='tru
e'></div>", "sample"); |
200 Element* script = document().createElement("script", ASSERT_NO_EXCEPTION); | 404 Element* script = document().createElement("script", ASSERT_NO_EXCEPTION); |
201 script->setInnerHTML( | 405 script->setInnerHTML( |
202 "document.getElementById('sample').addEventListener('beforeinput', funct
ion(event) {" | 406 "document.getElementById('sample').addEventListener('beforeinput', funct
ion(event) {" |
203 " document.title = `beforeinput.isComposing:${event.isComposing}`;" | 407 " document.title = `beforeinput.isComposing:${event.isComposing}`;" |
204 "});", | 408 "});", |
205 ASSERT_NO_EXCEPTION); | 409 ASSERT_NO_EXCEPTION); |
206 document().body()->appendChild(script, ASSERT_NO_EXCEPTION); | 410 document().body()->appendChild(script, ASSERT_NO_EXCEPTION); |
207 document().view()->updateAllLifecyclePhases(); | 411 document().view()->updateAllLifecyclePhases(); |
208 | 412 |
209 // Simulate composition in the |contentEditable|. | 413 // Simulate composition in the |contentEditable|. |
210 Vector<CompositionUnderline> underlines; | 414 Vector<CompositionUnderline> underlines; |
211 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); | 415 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); |
212 editable->focus(); | 416 editable->focus(); |
213 | 417 |
214 document().setTitle(emptyString()); | 418 document().setTitle(emptyString()); |
215 controller().setComposition("foo", underlines, 0, 3); | 419 controller().setComposition("foo", underlines, 0, 3); |
216 EXPECT_STREQ("beforeinput.isComposing:true", document().title().utf8().data(
)); | 420 EXPECT_STREQ("beforeinput.isComposing:true", document().title().utf8().data(
)); |
217 | 421 |
218 document().setTitle(emptyString()); | 422 document().setTitle(emptyString()); |
219 controller().confirmComposition(); | 423 controller().confirmComposition(); |
220 EXPECT_STREQ("beforeinput.isComposing:false", document().title().utf8().data
()); | 424 EXPECT_STREQ("beforeinput.isComposing:false", document().title().utf8().data
()); |
221 } | 425 } |
222 | 426 |
223 } // namespace blink | 427 } // namespace blink |
OLD | NEW |