Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(300)

Unified Diff: third_party/WebKit/Source/core/editing/InputMethodControllerTest.cpp

Issue 2493703002: Make "compositionend" event fired after setting caret position (Closed)
Patch Set: Add more test. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/WebKit/Source/core/editing/InputMethodController.cpp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/core/editing/InputMethodControllerTest.cpp
diff --git a/third_party/WebKit/Source/core/editing/InputMethodControllerTest.cpp b/third_party/WebKit/Source/core/editing/InputMethodControllerTest.cpp
index 1d1eaf64d0da2b65835dd646f456a9ec1104c2e7..4a9864afec421b068861714b3821b1dac39db177 100644
--- a/third_party/WebKit/Source/core/editing/InputMethodControllerTest.cpp
+++ b/third_party/WebKit/Source/core/editing/InputMethodControllerTest.cpp
@@ -811,11 +811,11 @@ TEST_F(InputMethodControllerTest, CompositionInputEventIsComposing) {
Element* script = document().createElement("script", ASSERT_NO_EXCEPTION);
script->setInnerHTML(
"document.getElementById('sample').addEventListener('beforeinput', "
- "function(event) {"
+ "event => {"
" document.title = `beforeinput.isComposing:${event.isComposing};`;"
"});"
"document.getElementById('sample').addEventListener('input', "
- "function(event) {"
+ "event => {"
" document.title += `input.isComposing:${event.isComposing};`;"
"});",
ASSERT_NO_EXCEPTION);
@@ -839,19 +839,23 @@ TEST_F(InputMethodControllerTest, CompositionInputEventIsComposing) {
document().title().utf8().data());
}
-TEST_F(InputMethodControllerTest, CompositionInputEventData) {
+TEST_F(InputMethodControllerTest, CompositionInputEventForReplace) {
yosin_UTC9 2016/11/17 01:39:55 Could you make another CL for re-factoring IMCTest
yabinh 2016/11/17 05:15:54 Done.
document().settings()->setScriptEnabled(true);
Element* editable = insertHTMLElement(
"<div id='sample' contentEditable='true'></div>", "sample");
yosin_UTC9 2016/11/17 01:39:55 nit: s/contentEditable='true'/contenteditable/
yabinh 2016/11/17 05:15:55 Done. Also applied to other places.
Element* script = document().createElement("script", ASSERT_NO_EXCEPTION);
script->setInnerHTML(
"document.getElementById('sample').addEventListener('beforeinput', "
- "function(event) {"
+ "event => {"
yosin_UTC9 2016/11/17 01:39:55 nit: You can write this in one line: event => doc
yabinh 2016/11/17 05:15:55 Done.
" document.title = `beforeinput.data:${event.data};`;"
"});"
"document.getElementById('sample').addEventListener('input', "
- "function(event) {"
+ "event => {"
" document.title += `input.data:${event.data};`;"
+ "});"
+ "document.getElementById('sample').addEventListener('compositionend', "
+ "event => {"
+ " document.title += `compositionend.data:${event.data};`;"
"});",
ASSERT_NO_EXCEPTION);
document().body()->appendChild(script, ASSERT_NO_EXCEPTION);
@@ -863,19 +867,284 @@ TEST_F(InputMethodControllerTest, CompositionInputEventData) {
editable->focus();
document().setTitle(emptyString());
- controller().setComposition("n", underlines, 0, 1);
- EXPECT_STREQ("beforeinput.data:n;input.data:n;",
+ controller().setComposition("hell", underlines, 4, 4);
+ EXPECT_STREQ("beforeinput.data:hell;input.data:hell;",
+ document().title().utf8().data());
+
+ // Replace the existing composition.
+ // TODO(yabinh): should be "beforeinput.data:hello;input.data:hello;".
+ document().setTitle(emptyString());
+ controller().setComposition("hello", underlines, 0, 0);
+ EXPECT_STREQ("beforeinput.data:o;input.data:o;",
document().title().utf8().data());
+}
+
+TEST_F(InputMethodControllerTest, CompositionInputEventForConfirm) {
+ document().settings()->setScriptEnabled(true);
+ Element* editable = insertHTMLElement(
+ "<div id='sample' contentEditable='true'></div>", "sample");
+ Element* script = document().createElement("script", ASSERT_NO_EXCEPTION);
yosin_UTC9 2016/11/17 01:39:55 nit: No need to have |ASSERT_NO_EXCEPTION|
yabinh 2016/11/17 05:15:54 Done.
+ script->setInnerHTML(
+ "document.getElementById('sample').addEventListener('beforeinput', "
+ "event => {"
+ " document.title = `beforeinput.data:${event.data};`;"
+ "});"
+ "document.getElementById('sample').addEventListener('input', "
+ "event => {"
+ " document.title += `input.data:${event.data};`;"
+ "});"
+ "document.getElementById('sample').addEventListener('compositionend', "
+ "event => {"
+ " document.title += `compositionend.data:${event.data};`;"
+ "});",
+ ASSERT_NO_EXCEPTION);
yosin_UTC9 2016/11/17 01:39:55 Note: I'm going to make |ASSERT_NO_EXCPETION| in |
yabinh 2016/11/17 05:15:54 Done.
+ document().body()->appendChild(script, ASSERT_NO_EXCEPTION);
+ document().view()->updateAllLifecyclePhases();
yosin_UTC9 2016/11/17 01:39:55 nit: No need to have |ASSERT_NO_EXCEPTION|
yabinh 2016/11/17 05:15:54 Done.
+
+ // Simulate composition in the |contentEditable|.
+ Vector<CompositionUnderline> underlines;
+ underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0));
+ editable->focus();
document().setTitle(emptyString());
- controller().setComposition("ni", underlines, 0, 1);
- EXPECT_STREQ("beforeinput.data:i;input.data:i;",
+ controller().setComposition("hello", underlines, 5, 5);
+ EXPECT_STREQ("beforeinput.data:hello;input.data:hello;",
document().title().utf8().data());
+ // Confirm the ongoing composition.
document().setTitle(emptyString());
controller().finishComposingText(InputMethodController::KeepSelection);
- EXPECT_STREQ("beforeinput.data:ni;input.data:ni;",
+ EXPECT_STREQ(
+ "beforeinput.data:hello;input.data:hello;compositionend.data:hello;",
+ document().title().utf8().data());
+}
+
+TEST_F(InputMethodControllerTest, CompositionInputEventForDelete) {
+ document().settings()->setScriptEnabled(true);
+ Element* editable = insertHTMLElement(
+ "<div id='sample' contentEditable='true'></div>", "sample");
+ Element* script = document().createElement("script", ASSERT_NO_EXCEPTION);
yosin_UTC9 2016/11/17 01:39:55 nit: No need to have |ASSERT_NO_EXCEPTION|
yabinh 2016/11/17 05:15:54 Done.
+ script->setInnerHTML(
+ "document.getElementById('sample').addEventListener('beforeinput', "
+ "event => {"
+ " document.title = `beforeinput.data:${event.data};`;"
+ "});"
+ "document.getElementById('sample').addEventListener('input', "
+ "event => {"
+ " document.title += `input.data:${event.data};`;"
+ "});"
+ "document.getElementById('sample').addEventListener('compositionend', "
+ "event => {"
+ " document.title += `compositionend.data:${event.data};`;"
+ "});",
+ ASSERT_NO_EXCEPTION);
+ document().body()->appendChild(script, ASSERT_NO_EXCEPTION);
yosin_UTC9 2016/11/17 01:39:55 nit: No need to have |ASSERT_NO_EXCEPTION|
yabinh 2016/11/17 05:15:54 Done.
+ document().view()->updateAllLifecyclePhases();
+
+ // Simulate composition in the |contentEditable|.
+ Vector<CompositionUnderline> underlines;
+ underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0));
+ editable->focus();
+
+ document().setTitle(emptyString());
+ controller().setComposition("hello", underlines, 5, 5);
+ EXPECT_STREQ("beforeinput.data:hello;input.data:hello;",
+ document().title().utf8().data());
+
+ // Delete the existing composition.
+ document().setTitle(emptyString());
+ controller().setComposition("", underlines, 0, 0);
+ EXPECT_STREQ("beforeinput.data:;compositionend.data:;",
document().title().utf8().data());
}
+TEST_F(InputMethodControllerTest, CompositionInputEventForInsert) {
+ document().settings()->setScriptEnabled(true);
+ Element* editable = insertHTMLElement(
+ "<div id='sample' contentEditable='true'></div>", "sample");
+ Element* script = document().createElement("script", ASSERT_NO_EXCEPTION);
yosin_UTC9 2016/11/17 01:39:55 nit: No need to have |ASSERT_NO_EXCEPTION|
yabinh 2016/11/17 05:15:54 Done.
+ script->setInnerHTML(
+ "document.getElementById('sample').addEventListener('beforeinput', "
+ "event => {"
+ " document.title = `beforeinput.data:${event.data};`;"
+ "});"
+ "document.getElementById('sample').addEventListener('input', "
+ "event => {"
+ " document.title += `input.data:${event.data};`;"
+ "});"
+ "document.getElementById('sample').addEventListener('compositionend', "
+ "event => {"
+ " document.title += `compositionend.data:${event.data};`;"
+ "});",
+ ASSERT_NO_EXCEPTION);
+ document().body()->appendChild(script, ASSERT_NO_EXCEPTION);
+ document().view()->updateAllLifecyclePhases();
+
+ // Simulate composition in the |contentEditable|.
+ Vector<CompositionUnderline> underlines;
+ underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0));
+ editable->focus();
+
+ // Insert new text without previous composition.
+ document().setTitle(emptyString());
+ document().updateStyleAndLayout();
+ controller().commitText("hello", 0);
+ EXPECT_STREQ("beforeinput.data:hello;input.data:hello;",
+ document().title().utf8().data());
+
+ document().setTitle(emptyString());
+ controller().setComposition("n", underlines, 1, 1);
+ EXPECT_STREQ("beforeinput.data:n;input.data:n;",
+ document().title().utf8().data());
+
+ // Insert new text with previous composition.
+ document().setTitle(emptyString());
+ document().updateStyleAndLayout();
+ controller().commitText("hello", 1);
+ EXPECT_STREQ(
+ "beforeinput.data:hello;input.data:hello;compositionend.data:hello;",
+ document().title().utf8().data());
+}
+
+TEST_F(InputMethodControllerTest, CompositionEndEventForConfirm) {
+ document().settings()->setScriptEnabled(true);
+ Element* editable = insertHTMLElement(
+ "<div id='sample' contentEditable='true'></div>", "sample");
+ Element* script = document().createElement("script", ASSERT_NO_EXCEPTION);
+ script->setInnerHTML(
+ "document.getElementById('sample').addEventListener('compositionend', "
+ "event => {"
+ // If the caret position is set before firing 'compositonend' event (and
+ // it should), the final caret position will be reset to [3,3].
+ " const node = document.getElementById('sample').firstChild;"
+ " getSelection().collapse(node, 3);"
+ "});",
+ ASSERT_NO_EXCEPTION);
+ document().body()->appendChild(script, ASSERT_NO_EXCEPTION);
+ document().view()->updateAllLifecyclePhases();
+
+ // Simulate composition in the |contentEditable|.
+ Vector<CompositionUnderline> underlines;
+ underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0));
+ editable->focus();
+
+ controller().setComposition("hello", underlines, 1, 1);
+ document().updateStyleAndLayout();
+ EXPECT_EQ(1u, controller().getSelectionOffsets().start());
+ EXPECT_EQ(1u, controller().getSelectionOffsets().end());
+
+ // Confirm the ongoing composition. Note that it moves the caret to the end of
+ // text [5,5] before firing 'compositonend' event.
+ controller().finishComposingText(InputMethodController::DoNotKeepSelection);
+ document().updateStyleAndLayout();
+ EXPECT_EQ(3u, controller().getSelectionOffsets().start());
+ EXPECT_EQ(3u, controller().getSelectionOffsets().end());
+}
+
+TEST_F(InputMethodControllerTest, CompositionEndEventForInsert) {
+ document().settings()->setScriptEnabled(true);
+ Element* editable = insertHTMLElement(
+ "<div id='sample' contentEditable='true'></div>", "sample");
+ Element* script = document().createElement("script", ASSERT_NO_EXCEPTION);
yosin_UTC9 2016/11/17 01:39:55 nit: No need to have |ASSERT_NO_EXCEPTION|
yabinh 2016/11/17 05:15:54 Done.
+ script->setInnerHTML(
+ "document.getElementById('sample').addEventListener('compositionend', "
+ "event => {"
+ // If the caret position is set before firing 'compositonend' event (and
+ // it should), the final caret position will be reset to [3,3].
+ " const node = document.getElementById('sample').firstChild;"
+ " getSelection().collapse(node, 3);"
+ "});",
+ ASSERT_NO_EXCEPTION);
+ document().body()->appendChild(script, ASSERT_NO_EXCEPTION);
+ document().view()->updateAllLifecyclePhases();
+
+ // Simulate composition in the |contentEditable|.
+ Vector<CompositionUnderline> underlines;
+ underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0));
+ editable->focus();
+
+ controller().setComposition("n", underlines, 1, 1);
+
+ // Insert new text with previous composition. Note that it moves the caret to
+ // [4,4] before firing 'compositonend' event.
+ document().updateStyleAndLayout();
+ controller().commitText("hello", -1);
+ document().updateStyleAndLayout();
+ EXPECT_EQ(3u, controller().getSelectionOffsets().start());
+ EXPECT_EQ(3u, controller().getSelectionOffsets().end());
+}
+
+TEST_F(InputMethodControllerTest, CompositionEndEventWithRangeSelection) {
+ document().settings()->setScriptEnabled(true);
+ Element* editable = insertHTMLElement(
+ "<div id='sample' contentEditable='true'></div>", "sample");
+ Element* script = document().createElement("script", ASSERT_NO_EXCEPTION);
yosin_UTC9 2016/11/17 01:39:55 nit: No need to have |ASSERT_NO_EXCEPTION|
yabinh 2016/11/17 05:15:55 Done.
+ script->setInnerHTML(
+ "document.getElementById('sample').addEventListener('compositionend', "
+ "event => {"
+ // If the caret position is set before firing 'compositonend' event (and
+ // it should), the final caret position will be reset to [2,4].
+ " const node = document.getElementById('sample').firstChild;"
+ " var range = document.createRange();"
+ " range.setStart(node, 2);"
+ " range.setEnd(node, 4);"
+ " const selection = getSelection();"
+ " selection.removeAllRanges();"
+ " selection.addRange(range);"
+ "});",
+ ASSERT_NO_EXCEPTION);
+ document().body()->appendChild(script, ASSERT_NO_EXCEPTION);
+ document().view()->updateAllLifecyclePhases();
+
+ // Simulate composition in the |contentEditable|.
+ Vector<CompositionUnderline> underlines;
+ underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0));
+ editable->focus();
+
+ controller().setComposition("hello", underlines, 1, 1);
+ document().updateStyleAndLayout();
+ EXPECT_EQ(1u, controller().getSelectionOffsets().start());
+ EXPECT_EQ(1u, controller().getSelectionOffsets().end());
+
+ // Confirm the ongoing composition. Note that it moves the caret to the end of
+ // text [5,5] before firing 'compositonend' event.
+ controller().finishComposingText(InputMethodController::DoNotKeepSelection);
+ document().updateStyleAndLayout();
+ EXPECT_EQ(2u, controller().getSelectionOffsets().start());
+ EXPECT_EQ(4u, controller().getSelectionOffsets().end());
+}
+
+TEST_F(InputMethodControllerTest, CompositionEndEventWithNoneSelection) {
+ document().settings()->setScriptEnabled(true);
+ Element* editable = insertHTMLElement(
+ "<div id='sample' contentEditable='true'></div>", "sample");
+ Element* script = document().createElement("script", ASSERT_NO_EXCEPTION);
yosin_UTC9 2016/11/17 01:39:55 nit: No need to have |ASSERT_NO_EXCEPTION|
yabinh 2016/11/17 05:15:54 Done.
+ script->setInnerHTML(
+ "document.getElementById('sample').addEventListener('compositionend', "
+ "event => {"
+ // If the caret position is set before firing 'compositonend' event (and
+ // it should), the final caret position will be reset to null.
+ " getSelection().removeAllRanges();"
+ "});",
+ ASSERT_NO_EXCEPTION);
+ document().body()->appendChild(script, ASSERT_NO_EXCEPTION);
+ document().view()->updateAllLifecyclePhases();
+
+ // Simulate composition in the |contentEditable|.
+ Vector<CompositionUnderline> underlines;
+ underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0));
+ editable->focus();
+
+ controller().setComposition("hello", underlines, 1, 1);
+ document().updateStyleAndLayout();
+ EXPECT_EQ(1u, controller().getSelectionOffsets().start());
+ EXPECT_EQ(1u, controller().getSelectionOffsets().end());
+
+ // Confirm the ongoing composition. Note that it moves the caret to the end of
+ // text [5,5] before firing 'compositonend' event.
+ controller().finishComposingText(InputMethodController::DoNotKeepSelection);
+ document().updateStyleAndLayout();
+ EXPECT_TRUE(controller().getSelectionOffsets().isNull());
+}
+
} // namespace blink
« no previous file with comments | « third_party/WebKit/Source/core/editing/InputMethodController.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698