| 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 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 | 200 |
| 201 input->setValue("hello"); | 201 input->setValue("hello"); |
| 202 controller().setEditableSelectionOffsets(PlainTextRange(2, 2)); | 202 controller().setEditableSelectionOffsets(PlainTextRange(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. | |
| 211 // "*heABllo", where * stands for cursor. | |
| 212 controller().setComposition("AB", underlines, -100, -100); | |
| 213 EXPECT_STREQ("heABllo", input->value().utf8().data()); | |
| 214 EXPECT_EQ(0u, controller().getSelectionOffsets().start()); | |
| 215 EXPECT_EQ(0u, controller().getSelectionOffsets().end()); | |
| 216 | |
| 217 // The cursor is on left boundary. | 210 // The cursor is on left boundary. |
| 218 // "*heABllo". | 211 // "*heABllo". |
| 219 controller().setComposition("AB", underlines, -2, -2); | 212 controller().setComposition("AB", underlines, -2, -2); |
| 220 EXPECT_STREQ("heABllo", input->value().utf8().data()); | 213 EXPECT_STREQ("heABllo", input->value().utf8().data()); |
| 221 EXPECT_EQ(0u, controller().getSelectionOffsets().start()); | 214 EXPECT_EQ(0u, controller().getSelectionOffsets().start()); |
| 222 EXPECT_EQ(0u, controller().getSelectionOffsets().end()); | 215 EXPECT_EQ(0u, controller().getSelectionOffsets().end()); |
| 223 | 216 |
| 224 // The cursor is before the composing text. | 217 // The cursor is before the composing text. |
| 225 // "he*ABllo". | 218 // "he*ABllo". |
| 226 controller().setComposition("AB", underlines, 0, 0); | 219 controller().setComposition("AB", underlines, 0, 0); |
| 227 EXPECT_STREQ("heABllo", input->value().utf8().data()); | 220 EXPECT_STREQ("heABllo", input->value().utf8().data()); |
| 228 EXPECT_EQ(2u, controller().getSelectionOffsets().start()); | 221 EXPECT_EQ(2u, controller().getSelectionOffsets().start()); |
| 229 EXPECT_EQ(2u, controller().getSelectionOffsets().end()); | 222 EXPECT_EQ(2u, controller().getSelectionOffsets().end()); |
| 230 | 223 |
| 231 // The cursor is after the composing text. | 224 // The cursor is after the composing text. |
| 232 // "heAB*llo". | 225 // "heAB*llo". |
| 233 controller().setComposition("AB", underlines, 2, 2); | 226 controller().setComposition("AB", underlines, 2, 2); |
| 234 EXPECT_STREQ("heABllo", input->value().utf8().data()); | 227 EXPECT_STREQ("heABllo", input->value().utf8().data()); |
| 235 EXPECT_EQ(4u, controller().getSelectionOffsets().start()); | 228 EXPECT_EQ(4u, controller().getSelectionOffsets().start()); |
| 236 EXPECT_EQ(4u, controller().getSelectionOffsets().end()); | 229 EXPECT_EQ(4u, controller().getSelectionOffsets().end()); |
| 237 | 230 |
| 238 // The cursor is on right boundary. | 231 // The cursor is on right boundary. |
| 239 // "heABllo*". | 232 // "heABllo*". |
| 240 controller().setComposition("AB", underlines, 5, 5); | 233 controller().setComposition("AB", underlines, 5, 5); |
| 241 EXPECT_STREQ("heABllo", input->value().utf8().data()); | 234 EXPECT_STREQ("heABllo", input->value().utf8().data()); |
| 242 EXPECT_EQ(7u, controller().getSelectionOffsets().start()); | 235 EXPECT_EQ(7u, controller().getSelectionOffsets().start()); |
| 243 EXPECT_EQ(7u, controller().getSelectionOffsets().end()); | 236 EXPECT_EQ(7u, controller().getSelectionOffsets().end()); |
| 244 | |
| 245 // The cursor exceeds right boundary. | |
| 246 // "heABllo*". | |
| 247 controller().setComposition("AB", underlines, 100, 100); | |
| 248 EXPECT_STREQ("heABllo", input->value().utf8().data()); | |
| 249 EXPECT_EQ(7u, controller().getSelectionOffsets().start()); | |
| 250 EXPECT_EQ(7u, controller().getSelectionOffsets().end()); | |
| 251 } | 237 } |
| 252 | 238 |
| 253 TEST_F(InputMethodControllerTest, SetCompositionForContentEditableWithDifferentN
ewCursorPositions) | 239 TEST_F(InputMethodControllerTest, SetCompositionForContentEditableWithDifferentN
ewCursorPositions) |
| 254 { | 240 { |
| 255 // There are 7 nodes and 5+1+5+1+3+4+3 characters: "hello", '\n', "world", "
\n", "012", "3456", "789". | 241 // There are 7 nodes and 5+1+5+1+3+4+3 characters: "hello", '\n', "world", "
\n", "012", "3456", "789". |
| 256 Element* div = insertHTMLElement( | 242 Element* div = insertHTMLElement( |
| 257 "<div id='sample' contenteditable='true'>" | 243 "<div id='sample' contenteditable='true'>" |
| 258 "hello" | 244 "hello" |
| 259 "<div id='sample2' contenteditable='true'>world" | 245 "<div id='sample2' contenteditable='true'>world" |
| 260 "<p>012<b>3456</b><i>789</i></p>" | 246 "<p>012<b>3456</b><i>789</i></p>" |
| 261 "</div>" | 247 "</div>" |
| 262 "</div>", | 248 "</div>", |
| 263 "sample"); | 249 "sample"); |
| 264 | 250 |
| 265 controller().setEditableSelectionOffsets(PlainTextRange(17, 17)); | 251 controller().setEditableSelectionOffsets(PlainTextRange(17, 17)); |
| 266 EXPECT_STREQ("hello\nworld\n0123456789", div->innerText().utf8().data()); | 252 EXPECT_STREQ("hello\nworld\n0123456789", div->innerText().utf8().data()); |
| 267 EXPECT_EQ(17u, controller().getSelectionOffsets().start()); | 253 EXPECT_EQ(17u, controller().getSelectionOffsets().start()); |
| 268 EXPECT_EQ(17u, controller().getSelectionOffsets().end()); | 254 EXPECT_EQ(17u, controller().getSelectionOffsets().end()); |
| 269 | 255 |
| 270 Vector<CompositionUnderline> underlines; | 256 Vector<CompositionUnderline> underlines; |
| 271 underlines.append(CompositionUnderline(0, 2, Color(255, 0, 0), false, 0)); | 257 underlines.append(CompositionUnderline(0, 2, Color(255, 0, 0), false, 0)); |
| 272 | 258 |
| 273 // The cursor exceeds left boundary. | |
| 274 // "*hello\nworld\n01234AB56789", where * stands for cursor. | |
| 275 controller().setComposition("AB", underlines, -100, -100); | |
| 276 EXPECT_STREQ("hello\nworld\n01234AB56789", div->innerText().utf8().data()); | |
| 277 EXPECT_EQ(0u, controller().getSelectionOffsets().start()); | |
| 278 EXPECT_EQ(0u, controller().getSelectionOffsets().end()); | |
| 279 | |
| 280 // The cursor is on left boundary. | 259 // The cursor is on left boundary. |
| 281 // "*hello\nworld\n01234AB56789". | 260 // "*hello\nworld\n01234AB56789". |
| 282 controller().setComposition("AB", underlines, -17, -17); | 261 controller().setComposition("AB", underlines, -17, -17); |
| 283 EXPECT_STREQ("hello\nworld\n01234AB56789", div->innerText().utf8().data()); | 262 EXPECT_STREQ("hello\nworld\n01234AB56789", div->innerText().utf8().data()); |
| 284 EXPECT_EQ(0u, controller().getSelectionOffsets().start()); | 263 EXPECT_EQ(0u, controller().getSelectionOffsets().start()); |
| 285 EXPECT_EQ(0u, controller().getSelectionOffsets().end()); | 264 EXPECT_EQ(0u, controller().getSelectionOffsets().end()); |
| 286 | 265 |
| 287 // The cursor is in the 1st node. | 266 // The cursor is in the 1st node. |
| 288 // "he*llo\nworld\n01234AB56789". | 267 // "he*llo\nworld\n01234AB56789". |
| 289 controller().setComposition("AB", underlines, -15, -15); | 268 controller().setComposition("AB", underlines, -15, -15); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 EXPECT_STREQ("hello\nworld\n01234AB56789", div->innerText().utf8().data()); | 311 EXPECT_STREQ("hello\nworld\n01234AB56789", div->innerText().utf8().data()); |
| 333 EXPECT_EQ(19u, controller().getSelectionOffsets().start()); | 312 EXPECT_EQ(19u, controller().getSelectionOffsets().start()); |
| 334 EXPECT_EQ(19u, controller().getSelectionOffsets().end()); | 313 EXPECT_EQ(19u, controller().getSelectionOffsets().end()); |
| 335 | 314 |
| 336 // The cursor is on right boundary. | 315 // The cursor is on right boundary. |
| 337 // "hello\nworld\n01234AB56789*". | 316 // "hello\nworld\n01234AB56789*". |
| 338 controller().setComposition("AB", underlines, 7, 7); | 317 controller().setComposition("AB", underlines, 7, 7); |
| 339 EXPECT_STREQ("hello\nworld\n01234AB56789", div->innerText().utf8().data()); | 318 EXPECT_STREQ("hello\nworld\n01234AB56789", div->innerText().utf8().data()); |
| 340 EXPECT_EQ(24u, controller().getSelectionOffsets().start()); | 319 EXPECT_EQ(24u, controller().getSelectionOffsets().start()); |
| 341 EXPECT_EQ(24u, controller().getSelectionOffsets().end()); | 320 EXPECT_EQ(24u, controller().getSelectionOffsets().end()); |
| 342 | |
| 343 // The cursor exceeds right boundary. | |
| 344 // "hello\nworld\n01234AB56789*". | |
| 345 controller().setComposition("AB", underlines, 100, 100); | |
| 346 EXPECT_STREQ("hello\nworld\n01234AB56789", div->innerText().utf8().data()); | |
| 347 EXPECT_EQ(24u, controller().getSelectionOffsets().start()); | |
| 348 EXPECT_EQ(24u, controller().getSelectionOffsets().end()); | |
| 349 } | 321 } |
| 350 | 322 |
| 323 TEST_F(InputMethodControllerTest, SetCompositionWithEmptyText) |
| 324 { |
| 325 Element* div = insertHTMLElement( |
| 326 "<div id='sample' contenteditable='true'>hello</div>", |
| 327 "sample"); |
| 328 |
| 329 controller().setEditableSelectionOffsets(PlainTextRange(2, 2)); |
| 330 EXPECT_STREQ("hello", div->innerText().utf8().data()); |
| 331 EXPECT_EQ(2u, controller().getSelectionOffsets().start()); |
| 332 EXPECT_EQ(2u, controller().getSelectionOffsets().end()); |
| 333 |
| 334 Vector<CompositionUnderline> underlines0; |
| 335 underlines0.append(CompositionUnderline(0, 0, Color(255, 0, 0), false, 0)); |
| 336 Vector<CompositionUnderline> underlines2; |
| 337 underlines2.append(CompositionUnderline(0, 2, Color(255, 0, 0), false, 0)); |
| 338 |
| 339 controller().setComposition("AB", underlines2, 2, 2); |
| 340 // With previous composition. |
| 341 controller().setComposition("", underlines0, 2, 2); |
| 342 EXPECT_STREQ("hello", div->innerText().utf8().data()); |
| 343 EXPECT_EQ(4u, controller().getSelectionOffsets().start()); |
| 344 EXPECT_EQ(4u, controller().getSelectionOffsets().end()); |
| 345 |
| 346 // Without previous composition. |
| 347 controller().setComposition("", underlines0, -1, -1); |
| 348 EXPECT_STREQ("hello", div->innerText().utf8().data()); |
| 349 EXPECT_EQ(3u, controller().getSelectionOffsets().start()); |
| 350 EXPECT_EQ(3u, controller().getSelectionOffsets().end()); |
| 351 } |
| 352 |
| 351 TEST_F(InputMethodControllerTest, CompositionFireBeforeInput) | 353 TEST_F(InputMethodControllerTest, CompositionFireBeforeInput) |
| 352 { | 354 { |
| 353 document().settings()->setScriptEnabled(true); | 355 document().settings()->setScriptEnabled(true); |
| 354 Element* editable = insertHTMLElement("<div id='sample' contentEditable='tru
e'></div>", "sample"); | 356 Element* editable = insertHTMLElement("<div id='sample' contentEditable='tru
e'></div>", "sample"); |
| 355 Element* script = document().createElement("script", ASSERT_NO_EXCEPTION); | 357 Element* script = document().createElement("script", ASSERT_NO_EXCEPTION); |
| 356 script->setInnerHTML( | 358 script->setInnerHTML( |
| 357 "document.getElementById('sample').addEventListener('beforeinput', funct
ion(event) {" | 359 "document.getElementById('sample').addEventListener('beforeinput', funct
ion(event) {" |
| 358 " document.title = `beforeinput.isComposing:${event.isComposing}`;" | 360 " document.title = `beforeinput.isComposing:${event.isComposing}`;" |
| 359 "});", | 361 "});", |
| 360 ASSERT_NO_EXCEPTION); | 362 ASSERT_NO_EXCEPTION); |
| 361 document().body()->appendChild(script, ASSERT_NO_EXCEPTION); | 363 document().body()->appendChild(script, ASSERT_NO_EXCEPTION); |
| 362 document().view()->updateAllLifecyclePhases(); | 364 document().view()->updateAllLifecyclePhases(); |
| 363 | 365 |
| 364 // Simulate composition in the |contentEditable|. | 366 // Simulate composition in the |contentEditable|. |
| 365 Vector<CompositionUnderline> underlines; | 367 Vector<CompositionUnderline> underlines; |
| 366 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); | 368 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); |
| 367 editable->focus(); | 369 editable->focus(); |
| 368 | 370 |
| 369 document().setTitle(emptyString()); | 371 document().setTitle(emptyString()); |
| 370 controller().setComposition("foo", underlines, 0, 3); | 372 controller().setComposition("foo", underlines, 0, 3); |
| 371 EXPECT_STREQ("beforeinput.isComposing:true", document().title().utf8().data(
)); | 373 EXPECT_STREQ("beforeinput.isComposing:true", document().title().utf8().data(
)); |
| 372 | 374 |
| 373 document().setTitle(emptyString()); | 375 document().setTitle(emptyString()); |
| 374 controller().confirmComposition(); | 376 controller().confirmComposition(); |
| 375 // Last 'beforeinput' should also be inside composition scope. | 377 // Last 'beforeinput' should also be inside composition scope. |
| 376 EXPECT_STREQ("beforeinput.isComposing:true", document().title().utf8().data(
)); | 378 EXPECT_STREQ("beforeinput.isComposing:true", document().title().utf8().data(
)); |
| 377 } | 379 } |
| 378 | 380 |
| 379 } // namespace blink | 381 } // namespace blink |
| OLD | NEW |