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/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/dom/Element.h" | 8 #include "core/dom/Element.h" |
9 #include "core/dom/Range.h" | 9 #include "core/dom/Range.h" |
10 #include "core/editing/Editor.h" | 10 #include "core/editing/Editor.h" |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 EXPECT_EQ(5, range->endOffset()); | 178 EXPECT_EQ(5, range->endOffset()); |
179 | 179 |
180 PlainTextRange plainTextRange(PlainTextRange::create(*div, *range)); | 180 PlainTextRange plainTextRange(PlainTextRange::create(*div, *range)); |
181 EXPECT_EQ(0u, plainTextRange.start()); | 181 EXPECT_EQ(0u, plainTextRange.start()); |
182 EXPECT_EQ(5u, plainTextRange.end()); | 182 EXPECT_EQ(5u, plainTextRange.end()); |
183 } | 183 } |
184 | 184 |
185 TEST_F(InputMethodControllerTest, SetCompositionKeepingStyle) { | 185 TEST_F(InputMethodControllerTest, SetCompositionKeepingStyle) { |
186 Element* div = insertHTMLElement( | 186 Element* div = insertHTMLElement( |
187 "<div id='sample' " | 187 "<div id='sample' " |
188 "contenteditable>abc1<b>2</b>34567<b>8</b>9</div>", | 188 "contenteditable>abc1<b>2</b>34567<b>8</b>9d<b>e</b>f</div>", |
189 "sample"); | 189 "sample"); |
190 | 190 |
191 Vector<CompositionUnderline> underlines; | 191 Vector<CompositionUnderline> underlines; |
192 underlines.append(CompositionUnderline(3, 12, Color(255, 0, 0), false, 0)); | 192 underlines.append(CompositionUnderline(3, 12, Color(255, 0, 0), false, 0)); |
193 controller().setCompositionFromExistingText(underlines, 3, 12); | 193 controller().setCompositionFromExistingText(underlines, 3, 12); |
194 | 194 |
195 // Subtract a character. | 195 // Subtract a character. |
196 controller().setComposition(String("12345789"), underlines, 8, 8); | 196 controller().setComposition(String("12345789"), underlines, 8, 8); |
197 EXPECT_STREQ("abc1<b>2</b>3457<b>8</b>9", div->innerHTML().utf8().data()); | 197 EXPECT_STREQ("abc1<b>2</b>3457<b>8</b>9d<b>e</b>f", |
| 198 div->innerHTML().utf8().data()); |
| 199 EXPECT_EQ(11u, controller().getSelectionOffsets().start()); |
| 200 EXPECT_EQ(11u, controller().getSelectionOffsets().end()); |
198 | 201 |
199 // Append a character. | 202 // Append a character. |
200 controller().setComposition(String("123456789"), underlines, 9, 9); | 203 controller().setComposition(String("123456789"), underlines, 9, 9); |
201 EXPECT_STREQ("abc1<b>2</b>34567<b>8</b>9", div->innerHTML().utf8().data()); | 204 EXPECT_STREQ("abc1<b>2</b>34567<b>8</b>9d<b>e</b>f", |
| 205 div->innerHTML().utf8().data()); |
| 206 EXPECT_EQ(12u, controller().getSelectionOffsets().start()); |
| 207 EXPECT_EQ(12u, controller().getSelectionOffsets().end()); |
202 | 208 |
203 // Subtract and append characters. | 209 // Subtract and append characters. |
204 controller().setComposition(String("123hello789"), underlines, 11, 11); | 210 controller().setComposition(String("123hello789"), underlines, 11, 11); |
205 EXPECT_STREQ("abc1<b>2</b>3hello7<b>8</b>9", div->innerHTML().utf8().data()); | 211 EXPECT_STREQ("abc1<b>2</b>3hello7<b>8</b>9d<b>e</b>f", |
| 212 div->innerHTML().utf8().data()); |
206 } | 213 } |
207 | 214 |
208 TEST_F(InputMethodControllerTest, SetCompositionWithEmojiKeepingStyle) { | 215 TEST_F(InputMethodControllerTest, SetCompositionWithEmojiKeepingStyle) { |
209 // U+1F3E0 = 0xF0 0x9F 0x8F 0xA0 (UTF8). It's an emoji character. | 216 // U+1F3E0 = 0xF0 0x9F 0x8F 0xA0 (UTF8). It's an emoji character. |
210 Element* div = insertHTMLElement( | 217 Element* div = insertHTMLElement( |
211 "<div id='sample' contenteditable><b>🏠</b></div>", "sample"); | 218 "<div id='sample' contenteditable><b>🏠</b></div>", "sample"); |
212 | 219 |
213 Vector<CompositionUnderline> underlines; | 220 Vector<CompositionUnderline> underlines; |
214 underlines.append(CompositionUnderline(0, 2, Color(255, 0, 0), false, 0)); | 221 underlines.append(CompositionUnderline(0, 2, Color(255, 0, 0), false, 0)); |
215 | 222 |
(...skipping 27 matching lines...) Expand all Loading... |
243 controller().setComposition(String::fromUTF8("\xE0\xB0\x83\xE0\xB0\x83"), | 250 controller().setComposition(String::fromUTF8("\xE0\xB0\x83\xE0\xB0\x83"), |
244 underlines, 2, 2); | 251 underlines, 2, 2); |
245 EXPECT_STREQ("<b>\xE0\xB0\x83\xE0\xB0\x83</b>", | 252 EXPECT_STREQ("<b>\xE0\xB0\x83\xE0\xB0\x83</b>", |
246 div->innerHTML().utf8().data()); | 253 div->innerHTML().utf8().data()); |
247 | 254 |
248 controller().setComposition(String::fromUTF8("\xE0\xB0\x83"), underlines, 1, | 255 controller().setComposition(String::fromUTF8("\xE0\xB0\x83"), underlines, 1, |
249 1); | 256 1); |
250 EXPECT_STREQ("<b>\xE0\xB0\x83</b>", div->innerHTML().utf8().data()); | 257 EXPECT_STREQ("<b>\xE0\xB0\x83</b>", div->innerHTML().utf8().data()); |
251 } | 258 } |
252 | 259 |
| 260 TEST_F(InputMethodControllerTest, FinishComposingTextKeepingStyle) { |
| 261 Element* div = insertHTMLElement( |
| 262 "<div id='sample' " |
| 263 "contenteditable>abc1<b>2</b>34567<b>8</b>9</div>", |
| 264 "sample"); |
| 265 |
| 266 Vector<CompositionUnderline> underlines; |
| 267 underlines.append(CompositionUnderline(3, 12, Color(255, 0, 0), false, 0)); |
| 268 controller().setCompositionFromExistingText(underlines, 3, 12); |
| 269 |
| 270 controller().setComposition(String("123hello789"), underlines, 11, 11); |
| 271 EXPECT_STREQ("abc1<b>2</b>3hello7<b>8</b>9", div->innerHTML().utf8().data()); |
| 272 |
| 273 controller().finishComposingText(InputMethodController::KeepSelection); |
| 274 EXPECT_STREQ("abc1<b>2</b>3hello7<b>8</b>9", div->innerHTML().utf8().data()); |
| 275 } |
| 276 |
| 277 TEST_F(InputMethodControllerTest, CommitTextKeepingStyle) { |
| 278 Element* div = insertHTMLElement( |
| 279 "<div id='sample' " |
| 280 "contenteditable>abc1<b>2</b>34567<b>8</b>9</div>", |
| 281 "sample"); |
| 282 |
| 283 Vector<CompositionUnderline> underlines; |
| 284 underlines.append(CompositionUnderline(3, 12, Color(255, 0, 0), false, 0)); |
| 285 controller().setCompositionFromExistingText(underlines, 3, 12); |
| 286 |
| 287 controller().commitText(String("123789"), 0); |
| 288 EXPECT_STREQ("abc1<b>2</b>37<b>8</b>9", div->innerHTML().utf8().data()); |
| 289 } |
| 290 |
253 TEST_F(InputMethodControllerTest, SelectionOnConfirmExistingText) { | 291 TEST_F(InputMethodControllerTest, SelectionOnConfirmExistingText) { |
254 insertHTMLElement("<div id='sample' contenteditable>hello world</div>", | 292 insertHTMLElement("<div id='sample' contenteditable>hello world</div>", |
255 "sample"); | 293 "sample"); |
256 | 294 |
257 Vector<CompositionUnderline> underlines; | 295 Vector<CompositionUnderline> underlines; |
258 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); | 296 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); |
259 controller().setCompositionFromExistingText(underlines, 0, 5); | 297 controller().setCompositionFromExistingText(underlines, 0, 5); |
260 | 298 |
261 controller().finishComposingText(InputMethodController::KeepSelection); | 299 controller().finishComposingText(InputMethodController::KeepSelection); |
262 EXPECT_EQ(0, frame().selection().start().computeOffsetInContainerNode()); | 300 EXPECT_EQ(0, frame().selection().start().computeOffsetInContainerNode()); |
(...skipping 641 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
904 // Simulate composition in the |contentEditable|. | 942 // Simulate composition in the |contentEditable|. |
905 Vector<CompositionUnderline> underlines; | 943 Vector<CompositionUnderline> underlines; |
906 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); | 944 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); |
907 | 945 |
908 document().setTitle(emptyString()); | 946 document().setTitle(emptyString()); |
909 controller().setComposition("hell", underlines, 4, 4); | 947 controller().setComposition("hell", underlines, 4, 4); |
910 EXPECT_STREQ("beforeinput.data:hell;input.data:hell;", | 948 EXPECT_STREQ("beforeinput.data:hell;input.data:hell;", |
911 document().title().utf8().data()); | 949 document().title().utf8().data()); |
912 | 950 |
913 // Replace the existing composition. | 951 // Replace the existing composition. |
914 // TODO(yabinh): should be "beforeinput.data:hello;input.data:hello;". | |
915 document().setTitle(emptyString()); | 952 document().setTitle(emptyString()); |
916 controller().setComposition("hello", underlines, 0, 0); | 953 controller().setComposition("hello", underlines, 0, 0); |
917 EXPECT_STREQ("beforeinput.data:o;input.data:o;", | 954 EXPECT_STREQ("beforeinput.data:hello;input.data:hello;", |
918 document().title().utf8().data()); | 955 document().title().utf8().data()); |
919 } | 956 } |
920 | 957 |
921 TEST_F(InputMethodControllerTest, CompositionInputEventForConfirm) { | 958 TEST_F(InputMethodControllerTest, CompositionInputEventForConfirm) { |
922 createHTMLWithCompositionInputEventListeners(); | 959 createHTMLWithCompositionInputEventListeners(); |
923 | 960 |
924 // Simulate composition in the |contentEditable|. | 961 // Simulate composition in the |contentEditable|. |
925 Vector<CompositionUnderline> underlines; | 962 Vector<CompositionUnderline> underlines; |
926 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); | 963 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); |
927 | 964 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
978 | 1015 |
979 // Insert new text with previous composition. | 1016 // Insert new text with previous composition. |
980 document().setTitle(emptyString()); | 1017 document().setTitle(emptyString()); |
981 document().updateStyleAndLayout(); | 1018 document().updateStyleAndLayout(); |
982 controller().commitText("hello", 1); | 1019 controller().commitText("hello", 1); |
983 EXPECT_STREQ( | 1020 EXPECT_STREQ( |
984 "beforeinput.data:hello;input.data:hello;compositionend.data:hello;", | 1021 "beforeinput.data:hello;input.data:hello;compositionend.data:hello;", |
985 document().title().utf8().data()); | 1022 document().title().utf8().data()); |
986 } | 1023 } |
987 | 1024 |
| 1025 TEST_F(InputMethodControllerTest, CompositionInputEventForInsertEmptyText) { |
| 1026 createHTMLWithCompositionInputEventListeners(); |
| 1027 |
| 1028 // Simulate composition in the |contentEditable|. |
| 1029 Vector<CompositionUnderline> underlines; |
| 1030 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); |
| 1031 |
| 1032 // Insert empty text without previous composition. |
| 1033 document().setTitle(emptyString()); |
| 1034 document().updateStyleAndLayout(); |
| 1035 controller().commitText("", 0); |
| 1036 EXPECT_STREQ("", document().title().utf8().data()); |
| 1037 |
| 1038 document().setTitle(emptyString()); |
| 1039 controller().setComposition("n", underlines, 1, 1); |
| 1040 EXPECT_STREQ("beforeinput.data:n;input.data:n;", |
| 1041 document().title().utf8().data()); |
| 1042 |
| 1043 // Insert empty text with previous composition. |
| 1044 document().setTitle(emptyString()); |
| 1045 document().updateStyleAndLayout(); |
| 1046 controller().commitText("", 1); |
| 1047 EXPECT_STREQ("beforeinput.data:;compositionend.data:;", |
| 1048 document().title().utf8().data()); |
| 1049 } |
| 1050 |
988 TEST_F(InputMethodControllerTest, CompositionEndEventForConfirm) { | 1051 TEST_F(InputMethodControllerTest, CompositionEndEventForConfirm) { |
989 createHTMLWithCompositionEndEventListener(CaretSelection); | 1052 createHTMLWithCompositionEndEventListener(CaretSelection); |
990 | 1053 |
991 // Simulate composition in the |contentEditable|. | 1054 // Simulate composition in the |contentEditable|. |
992 Vector<CompositionUnderline> underlines; | 1055 Vector<CompositionUnderline> underlines; |
993 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); | 1056 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); |
994 | 1057 |
995 controller().setComposition("hello", underlines, 1, 1); | 1058 controller().setComposition("hello", underlines, 1, 1); |
996 document().updateStyleAndLayout(); | 1059 document().updateStyleAndLayout(); |
997 EXPECT_EQ(1u, controller().getSelectionOffsets().start()); | 1060 EXPECT_EQ(1u, controller().getSelectionOffsets().start()); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1077 EXPECT_EQ(WebTextInputTypeNone, controller().textInputType()); | 1140 EXPECT_EQ(WebTextInputTypeNone, controller().textInputType()); |
1078 | 1141 |
1079 document().getElementById("b")->focus(); | 1142 document().getElementById("b")->focus(); |
1080 EXPECT_EQ(WebTextInputTypeTelephone, controller().textInputType()); | 1143 EXPECT_EQ(WebTextInputTypeTelephone, controller().textInputType()); |
1081 | 1144 |
1082 controller().finishComposingText(InputMethodController::KeepSelection); | 1145 controller().finishComposingText(InputMethodController::KeepSelection); |
1083 EXPECT_EQ(WebTextInputTypeTelephone, controller().textInputType()); | 1146 EXPECT_EQ(WebTextInputTypeTelephone, controller().textInputType()); |
1084 } | 1147 } |
1085 | 1148 |
1086 } // namespace blink | 1149 } // namespace blink |
OLD | NEW |