| 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/frame/LocalFrame.h" | 10 #include "core/frame/LocalFrame.h" |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 } | 167 } |
| 168 | 168 |
| 169 TEST_F(InputMethodControllerTest, SetCompositionFromExistingTextWithInvalidOffse
ts) | 169 TEST_F(InputMethodControllerTest, SetCompositionFromExistingTextWithInvalidOffse
ts) |
| 170 { | 170 { |
| 171 insertHTMLElement("<div id='sample' contenteditable='true'>test</div>", "sam
ple"); | 171 insertHTMLElement("<div id='sample' contenteditable='true'>test</div>", "sam
ple"); |
| 172 | 172 |
| 173 Vector<CompositionUnderline> underlines; | 173 Vector<CompositionUnderline> underlines; |
| 174 underlines.append(CompositionUnderline(7, 8, Color(255, 0, 0), false, 0)); | 174 underlines.append(CompositionUnderline(7, 8, Color(255, 0, 0), false, 0)); |
| 175 controller().setCompositionFromExistingText(underlines, 7, 8); | 175 controller().setCompositionFromExistingText(underlines, 7, 8); |
| 176 | 176 |
| 177 EXPECT_FALSE(controller().compositionRange()); | 177 // If it exceeds the right boundary, it should stay at the right boundary |
| 178 RawPtr<Range> range = controller().compositionRange(); |
| 179 EXPECT_EQ(4, range->startOffset()); |
| 180 EXPECT_EQ(4, range->endOffset()); |
| 178 } | 181 } |
| 179 | 182 |
| 180 TEST_F(InputMethodControllerTest, ConfirmPasswordComposition) | 183 TEST_F(InputMethodControllerTest, ConfirmPasswordComposition) |
| 181 { | 184 { |
| 182 HTMLInputElement* input = toHTMLInputElement( | 185 HTMLInputElement* input = toHTMLInputElement( |
| 183 insertHTMLElement("<input id='sample' type='password' size='24'>", "samp
le")); | 186 insertHTMLElement("<input id='sample' type='password' size='24'>", "samp
le")); |
| 184 | 187 |
| 185 Vector<CompositionUnderline> underlines; | 188 Vector<CompositionUnderline> underlines; |
| 186 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); | 189 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); |
| 187 controller().setComposition("foo", underlines, 0, 3); | 190 controller().setComposition("foo", underlines, 0, 3); |
| 188 controller().confirmComposition(); | 191 controller().confirmComposition(); |
| 189 | 192 |
| 190 EXPECT_STREQ("foo", input->value().utf8().data()); | 193 EXPECT_STREQ("foo", input->value().utf8().data()); |
| 191 } | 194 } |
| 192 | 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(2, frame().selection().start().computeOffsetInContainerNode()); |
| 205 EXPECT_EQ(2, frame().selection().end().computeOffsetInContainerNode()); |
| 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, -10, -10); |
| 213 EXPECT_STREQ("heABllo", input->value().utf8().data()); |
| 214 EXPECT_EQ(0, frame().selection().start().computeOffsetInContainerNode()); |
| 215 EXPECT_EQ(0, frame().selection().end().computeOffsetInContainerNode()); |
| 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(0, frame().selection().start().computeOffsetInContainerNode()); |
| 222 EXPECT_EQ(0, frame().selection().end().computeOffsetInContainerNode()); |
| 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(2, frame().selection().start().computeOffsetInContainerNode()); |
| 229 EXPECT_EQ(2, frame().selection().end().computeOffsetInContainerNode()); |
| 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(4, frame().selection().start().computeOffsetInContainerNode()); |
| 236 EXPECT_EQ(4, frame().selection().end().computeOffsetInContainerNode()); |
| 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(7, frame().selection().start().computeOffsetInContainerNode()); |
| 243 EXPECT_EQ(7, frame().selection().end().computeOffsetInContainerNode()); |
| 244 |
| 245 // The cursor exceeds right boundary. |
| 246 // "heABllo*". |
| 247 controller().setComposition("AB", underlines, 10, 10); |
| 248 EXPECT_STREQ("heABllo", input->value().utf8().data()); |
| 249 EXPECT_EQ(7, frame().selection().start().computeOffsetInContainerNode()); |
| 250 EXPECT_EQ(7, frame().selection().end().computeOffsetInContainerNode()); |
| 251 } |
| 252 |
| 253 TEST_F(InputMethodControllerTest, SetCompositionForEditableWithDifferentNewCurso
rPositions) |
| 254 { |
| 255 // There are 3 nodes and 5+1+6 characters: "hello", '\n', "world!". |
| 256 Element* div = insertHTMLElement( |
| 257 "<div id='sample' contenteditable='true'>" |
| 258 "hello" |
| 259 "<p id='sample2' contenteditable='true'>world!</p>" |
| 260 "</div>", |
| 261 "sample"); |
| 262 |
| 263 controller().setEditableSelectionOffsets(PlainTextRange(2, 2)); |
| 264 EXPECT_STREQ("hello\nworld!", div->innerText().utf8().data()); |
| 265 EXPECT_EQ(2, frame().selection().start().computeOffsetInContainerNode()); |
| 266 EXPECT_EQ(2, frame().selection().end().computeOffsetInContainerNode()); |
| 267 |
| 268 Vector<CompositionUnderline> underlines; |
| 269 underlines.append(CompositionUnderline(0, 2, Color(255, 0, 0), false, 0)); |
| 270 |
| 271 // The cursor exceeds left boundary. |
| 272 // "*heABllo\nworld!", where * stands for cursor. |
| 273 controller().setComposition("AB", underlines, -10, -10); |
| 274 EXPECT_STREQ("heABllo\nworld!", div->innerText().utf8().data()); |
| 275 EXPECT_EQ(0, frame().selection().start().computeOffsetInContainerNode()); |
| 276 EXPECT_EQ(0, frame().selection().end().computeOffsetInContainerNode()); |
| 277 |
| 278 // The cursor is on left boundary. |
| 279 // "*heABllo\nworld!" |
| 280 controller().setComposition("AB", underlines, -2, -2); |
| 281 EXPECT_STREQ("heABllo\nworld!", div->innerText().utf8().data()); |
| 282 EXPECT_EQ(0, frame().selection().start().computeOffsetInContainerNode()); |
| 283 EXPECT_EQ(0, frame().selection().end().computeOffsetInContainerNode()); |
| 284 |
| 285 // The cursor is before the composing text. |
| 286 // "he*ABllo\nworld!" |
| 287 controller().setComposition("AB", underlines, 0, 0); |
| 288 EXPECT_STREQ("heABllo\nworld!", div->innerText().utf8().data()); |
| 289 EXPECT_EQ(2, frame().selection().start().computeOffsetInContainerNode()); |
| 290 EXPECT_EQ(2, frame().selection().end().computeOffsetInContainerNode()); |
| 291 |
| 292 // The cursor is after the composing text. |
| 293 // "heAB*llo\nworld!" |
| 294 controller().setComposition("AB", underlines, 2, 2); |
| 295 EXPECT_STREQ("heABllo\nworld!", div->innerText().utf8().data()); |
| 296 EXPECT_EQ(4, frame().selection().start().computeOffsetInContainerNode()); |
| 297 EXPECT_EQ(4, frame().selection().end().computeOffsetInContainerNode()); |
| 298 |
| 299 // The cursor is on right boundary of the 1st node. |
| 300 // "heABllo*\nworld!" |
| 301 controller().setComposition("AB", underlines, 5, 5); |
| 302 EXPECT_STREQ("heABllo\nworld!", div->innerText().utf8().data()); |
| 303 EXPECT_EQ(7, frame().selection().start().computeOffsetInContainerNode()); |
| 304 EXPECT_EQ(7, frame().selection().end().computeOffsetInContainerNode()); |
| 305 |
| 306 // The cursor is on left boundary of the 3rd node. |
| 307 // "heABllo\n*world!". |
| 308 controller().setComposition("AB", underlines, 6, 6); |
| 309 EXPECT_STREQ("heABllo\nworld!", div->innerText().utf8().data()); |
| 310 EXPECT_EQ(0, frame().selection().start().computeOffsetInContainerNode()); |
| 311 EXPECT_EQ(0, frame().selection().end().computeOffsetInContainerNode()); |
| 312 |
| 313 // The cursor is on right boundary of the 3rd node. |
| 314 // "heABllo\nworld!*" |
| 315 controller().setComposition("AB", underlines, 12, 12); |
| 316 EXPECT_STREQ("heABllo\nworld!", div->innerText().utf8().data()); |
| 317 EXPECT_EQ(6, frame().selection().start().computeOffsetInContainerNode()); |
| 318 EXPECT_EQ(6, frame().selection().end().computeOffsetInContainerNode()); |
| 319 |
| 320 // The cursor exceeds right boundary. |
| 321 // "heABllo\nworld!*" |
| 322 controller().setComposition("AB", underlines, 20, 20); |
| 323 EXPECT_STREQ("heABllo\nworld!", div->innerText().utf8().data()); |
| 324 EXPECT_EQ(6, frame().selection().start().computeOffsetInContainerNode()); |
| 325 EXPECT_EQ(6, frame().selection().end().computeOffsetInContainerNode()); |
| 326 } |
| 193 } // namespace blink | 327 } // namespace blink |
| OLD | NEW |