| 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, SetCompositionForInputWithDifferentNewCursorPo
sitions) |
| 197 { |
| 198 HTMLInputElement* input = toHTMLInputElement( |
| 199 insertHTMLElement("<input id='sample'>", "sample")); |
| 200 |
| 201 input->setValue("hello"); |
| 202 controller().setEditableSelectionOffsets(PlainTextRange(2, 2)); |
| 203 EXPECT_STREQ("hello", input->value().utf8().data()); |
| 204 EXPECT_EQ(2u, controller().getSelectionOffsets().start()); |
| 205 EXPECT_EQ(2u, controller().getSelectionOffsets().end()); |
| 206 |
| 207 Vector<CompositionUnderline> underlines; |
| 208 underlines.append(CompositionUnderline(0, 2, Color(255, 0, 0), false, 0)); |
| 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. |
| 218 // "*heABllo". |
| 219 controller().setComposition("AB", underlines, -2, -2); |
| 220 EXPECT_STREQ("heABllo", input->value().utf8().data()); |
| 221 EXPECT_EQ(0u, controller().getSelectionOffsets().start()); |
| 222 EXPECT_EQ(0u, controller().getSelectionOffsets().end()); |
| 223 |
| 224 // The cursor is before the composing text. |
| 225 // "he*ABllo". |
| 226 controller().setComposition("AB", underlines, 0, 0); |
| 227 EXPECT_STREQ("heABllo", input->value().utf8().data()); |
| 228 EXPECT_EQ(2u, controller().getSelectionOffsets().start()); |
| 229 EXPECT_EQ(2u, controller().getSelectionOffsets().end()); |
| 230 |
| 231 // The cursor is after the composing text. |
| 232 // "heAB*llo". |
| 233 controller().setComposition("AB", underlines, 2, 2); |
| 234 EXPECT_STREQ("heABllo", input->value().utf8().data()); |
| 235 EXPECT_EQ(4u, controller().getSelectionOffsets().start()); |
| 236 EXPECT_EQ(4u, controller().getSelectionOffsets().end()); |
| 237 |
| 238 // The cursor is on right boundary. |
| 239 // "heABllo*". |
| 240 controller().setComposition("AB", underlines, 5, 5); |
| 241 EXPECT_STREQ("heABllo", input->value().utf8().data()); |
| 242 EXPECT_EQ(7u, controller().getSelectionOffsets().start()); |
| 243 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 } |
| 252 |
| 253 TEST_F(InputMethodControllerTest, SetCompositionForContentEditableWithDifferentN
ewCursorPositions) |
| 254 { |
| 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( |
| 257 "<div id='sample' contenteditable='true'>" |
| 258 "hello" |
| 259 "<div id='sample2' contenteditable='true'>world" |
| 260 "<p>012<b>3456</b><i>789</i></p>" |
| 261 "</div>" |
| 262 "</div>", |
| 263 "sample"); |
| 264 |
| 265 controller().setEditableSelectionOffsets(PlainTextRange(17, 17)); |
| 266 EXPECT_STREQ("hello\nworld\n0123456789", div->innerText().utf8().data()); |
| 267 EXPECT_EQ(17u, controller().getSelectionOffsets().start()); |
| 268 EXPECT_EQ(17u, controller().getSelectionOffsets().end()); |
| 269 |
| 270 Vector<CompositionUnderline> underlines; |
| 271 underlines.append(CompositionUnderline(0, 2, Color(255, 0, 0), false, 0)); |
| 272 |
| 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. |
| 281 // "*hello\nworld\n01234AB56789". |
| 282 controller().setComposition("AB", underlines, -17, -17); |
| 283 EXPECT_STREQ("hello\nworld\n01234AB56789", div->innerText().utf8().data()); |
| 284 EXPECT_EQ(0u, controller().getSelectionOffsets().start()); |
| 285 EXPECT_EQ(0u, controller().getSelectionOffsets().end()); |
| 286 |
| 287 // The cursor is in the 1st node. |
| 288 // "he*llo\nworld\n01234AB56789". |
| 289 controller().setComposition("AB", underlines, -15, -15); |
| 290 EXPECT_STREQ("hello\nworld\n01234AB56789", div->innerText().utf8().data()); |
| 291 EXPECT_EQ(2u, controller().getSelectionOffsets().start()); |
| 292 EXPECT_EQ(2u, controller().getSelectionOffsets().end()); |
| 293 |
| 294 // The cursor is on right boundary of the 1st node. |
| 295 // "hello*\nworld\n01234AB56789". |
| 296 controller().setComposition("AB", underlines, -12, -12); |
| 297 EXPECT_STREQ("hello\nworld\n01234AB56789", div->innerText().utf8().data()); |
| 298 EXPECT_EQ(5u, controller().getSelectionOffsets().start()); |
| 299 EXPECT_EQ(5u, controller().getSelectionOffsets().end()); |
| 300 |
| 301 // The cursor is on right boundary of the 2nd node. |
| 302 // "hello\n*world\n01234AB56789". |
| 303 controller().setComposition("AB", underlines, -11, -11); |
| 304 EXPECT_STREQ("hello\nworld\n01234AB56789", div->innerText().utf8().data()); |
| 305 EXPECT_EQ(6u, controller().getSelectionOffsets().start()); |
| 306 EXPECT_EQ(6u, controller().getSelectionOffsets().end()); |
| 307 |
| 308 // The cursor is on right boundary of the 3rd node. |
| 309 // "hello\nworld*\n01234AB56789". |
| 310 controller().setComposition("AB", underlines, -6, -6); |
| 311 EXPECT_STREQ("hello\nworld\n01234AB56789", div->innerText().utf8().data()); |
| 312 EXPECT_EQ(11u, controller().getSelectionOffsets().start()); |
| 313 EXPECT_EQ(11u, controller().getSelectionOffsets().end()); |
| 314 |
| 315 // The cursor is on right boundary of the 4th node. |
| 316 // "hello\nworld\n*01234AB56789". |
| 317 controller().setComposition("AB", underlines, -5, -5); |
| 318 EXPECT_STREQ("hello\nworld\n01234AB56789", div->innerText().utf8().data()); |
| 319 EXPECT_EQ(12u, controller().getSelectionOffsets().start()); |
| 320 EXPECT_EQ(12u, controller().getSelectionOffsets().end()); |
| 321 |
| 322 // The cursor is before the composing text. |
| 323 // "hello\nworld\n01234*AB56789". |
| 324 controller().setComposition("AB", underlines, 0, 0); |
| 325 EXPECT_STREQ("hello\nworld\n01234AB56789", div->innerText().utf8().data()); |
| 326 EXPECT_EQ(17u, controller().getSelectionOffsets().start()); |
| 327 EXPECT_EQ(17u, controller().getSelectionOffsets().end()); |
| 328 |
| 329 // The cursor is after the composing text. |
| 330 // "hello\nworld\n01234AB*56789". |
| 331 controller().setComposition("AB", underlines, 2, 2); |
| 332 EXPECT_STREQ("hello\nworld\n01234AB56789", div->innerText().utf8().data()); |
| 333 EXPECT_EQ(19u, controller().getSelectionOffsets().start()); |
| 334 EXPECT_EQ(19u, controller().getSelectionOffsets().end()); |
| 335 |
| 336 // The cursor is on right boundary. |
| 337 // "hello\nworld\n01234AB56789*". |
| 338 controller().setComposition("AB", underlines, 7, 7); |
| 339 EXPECT_STREQ("hello\nworld\n01234AB56789", div->innerText().utf8().data()); |
| 340 EXPECT_EQ(24u, controller().getSelectionOffsets().start()); |
| 341 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 } |
| 350 |
| 196 TEST_F(InputMethodControllerTest, CompositionFireBeforeInput) | 351 TEST_F(InputMethodControllerTest, CompositionFireBeforeInput) |
| 197 { | 352 { |
| 198 document().settings()->setScriptEnabled(true); | 353 document().settings()->setScriptEnabled(true); |
| 199 Element* editable = insertHTMLElement("<div id='sample' contentEditable='tru
e'></div>", "sample"); | 354 Element* editable = insertHTMLElement("<div id='sample' contentEditable='tru
e'></div>", "sample"); |
| 200 Element* script = document().createElement("script", ASSERT_NO_EXCEPTION); | 355 Element* script = document().createElement("script", ASSERT_NO_EXCEPTION); |
| 201 script->setInnerHTML( | 356 script->setInnerHTML( |
| 202 "document.getElementById('sample').addEventListener('beforeinput', funct
ion(event) {" | 357 "document.getElementById('sample').addEventListener('beforeinput', funct
ion(event) {" |
| 203 " document.title = `beforeinput.isComposing:${event.isComposing}`;" | 358 " document.title = `beforeinput.isComposing:${event.isComposing}`;" |
| 204 "});", | 359 "});", |
| 205 ASSERT_NO_EXCEPTION); | 360 ASSERT_NO_EXCEPTION); |
| 206 document().body()->appendChild(script, ASSERT_NO_EXCEPTION); | 361 document().body()->appendChild(script, ASSERT_NO_EXCEPTION); |
| 207 document().view()->updateAllLifecyclePhases(); | 362 document().view()->updateAllLifecyclePhases(); |
| 208 | 363 |
| 209 // Simulate composition in the |contentEditable|. | 364 // Simulate composition in the |contentEditable|. |
| 210 Vector<CompositionUnderline> underlines; | 365 Vector<CompositionUnderline> underlines; |
| 211 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); | 366 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); |
| 212 editable->focus(); | 367 editable->focus(); |
| 213 | 368 |
| 214 document().setTitle(emptyString()); | 369 document().setTitle(emptyString()); |
| 215 controller().setComposition("foo", underlines, 0, 3); | 370 controller().setComposition("foo", underlines, 0, 3); |
| 216 EXPECT_STREQ("beforeinput.isComposing:true", document().title().utf8().data(
)); | 371 EXPECT_STREQ("beforeinput.isComposing:true", document().title().utf8().data(
)); |
| 217 | 372 |
| 218 document().setTitle(emptyString()); | 373 document().setTitle(emptyString()); |
| 219 controller().confirmComposition(); | 374 controller().confirmComposition(); |
| 220 EXPECT_STREQ("beforeinput.isComposing:false", document().title().utf8().data
()); | 375 EXPECT_STREQ("beforeinput.isComposing:false", document().title().utf8().data
()); |
| 221 } | 376 } |
| 222 | 377 |
| 223 } // namespace blink | 378 } // namespace blink |
| OLD | NEW |