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