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

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

Issue 2617443002: Implement ThreadedInputConnection.deleteSurroundingTextInCodePoints() (Closed)
Patch Set: Add more comments Created 3 years, 11 months 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
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 d3e75f53cfb9b944f6abb23d6cf3d14b33c7f226..1b7066de5275facedcbd31c2700c307ad5908d7d 100644
--- a/third_party/WebKit/Source/core/editing/InputMethodControllerTest.cpp
+++ b/third_party/WebKit/Source/core/editing/InputMethodControllerTest.cpp
@@ -686,6 +686,228 @@ TEST_F(InputMethodControllerTest, DeleteSurroundingTextForMultipleNodes) {
EXPECT_EQ(2u, controller().getSelectionOffsets().end());
}
+TEST_F(InputMethodControllerTest, DeleteSurroundingTextInCodePoints) {
+ HTMLInputElement* input =
+ toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample"));
+
+ input->setValue("abc123");
+ document().updateStyleAndLayout();
+ controller().setEditableSelectionOffsets(PlainTextRange(3, 3));
+ EXPECT_STREQ("abc123", input->value().utf8().data());
+
+ controller().deleteSurroundingTextInCodePoints(0, 0);
+ EXPECT_STREQ("abc123", input->value().utf8().data());
+
+ controller().deleteSurroundingTextInCodePoints(1, 0);
+ EXPECT_STREQ("ab123", input->value().utf8().data());
+
+ // Exceeds left boundary.
+ controller().deleteSurroundingTextInCodePoints(10, 0);
+ EXPECT_STREQ("123", input->value().utf8().data());
+
+ input->setValue("abc123");
+ document().updateStyleAndLayout();
+ controller().setEditableSelectionOffsets(PlainTextRange(3, 3));
+ EXPECT_STREQ("abc123", input->value().utf8().data());
+
+ controller().deleteSurroundingTextInCodePoints(0, 1);
+ EXPECT_STREQ("abc23", input->value().utf8().data());
+
+ // Exceeds right boundary.
+ controller().deleteSurroundingTextInCodePoints(0, 10);
+ EXPECT_STREQ("abc", input->value().utf8().data());
+
+ input->setValue("abc123");
+ document().updateStyleAndLayout();
+ controller().setEditableSelectionOffsets(PlainTextRange(3, 3));
+ EXPECT_STREQ("abc123", input->value().utf8().data());
+
+ controller().deleteSurroundingTextInCodePoints(1, 2);
+ EXPECT_STREQ("ab3", input->value().utf8().data());
+
+ // Exceeds both left and right boundary.
+ controller().deleteSurroundingTextInCodePoints(10, 10);
+ EXPECT_STREQ("", input->value().utf8().data());
+}
+
+TEST_F(InputMethodControllerTest,
+ DeleteSurroundingTextInCodePointsWithEmptyText) {
+ HTMLInputElement* input =
+ toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample"));
+
+ input->setValue("");
+ document().updateStyleAndLayout();
+ EXPECT_STREQ("", input->value().utf8().data());
+
+ controller().deleteSurroundingTextInCodePoints(2, 2);
+ EXPECT_STREQ("", input->value().utf8().data());
+}
+
+TEST_F(InputMethodControllerTest,
+ DeleteSurroundingTextInCodePointsWithMultiCodeTextOnTheLeft) {
+ HTMLInputElement* input =
+ toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample"));
+
+ // 'a' + "black star" + SPACE + "trophy" + SPACE + composed text (U+0E01
+ // "ka kai" + U+0E49 "mai tho").
+ // A "black star" is 1 grapheme cluster. It has 1 code point, and its length
+ // is 1 (abbreviated as [1,1,1]). A "trophy": [1,1,2]. The composed text:
+ // [1,2,2].
+ input->setValue(String::fromUTF8(
+ "a\xE2\x98\x85 \xF0\x9F\x8F\x86 \xE0\xB8\x81\xE0\xB9\x89"));
+ document().updateStyleAndLayout();
+ // The cursor is at the end of the text.
+ controller().setEditableSelectionOffsets(PlainTextRange(8, 8));
+
+ controller().deleteSurroundingTextInCodePoints(2, 0);
+ EXPECT_STREQ("a\xE2\x98\x85 \xF0\x9F\x8F\x86 ", input->value().utf8().data());
+ controller().deleteSurroundingTextInCodePoints(4, 0);
+ EXPECT_STREQ("a", input->value().utf8().data());
+
+ // 'a' + "black star" + SPACE + "trophy" + SPACE + composed text
+ input->setValue(String::fromUTF8(
+ "a\xE2\x98\x85 \xF0\x9F\x8F\x86 \xE0\xB8\x81\xE0\xB9\x89"));
+ document().updateStyleAndLayout();
+ // The cursor is at the end of the text.
+ controller().setEditableSelectionOffsets(PlainTextRange(8, 8));
+
+ // TODO(yabinh): We should only delete 1 code point instead of the entire
+ // grapheme cluster (2 code points). The root cause is that we adjust the
+ // selection by grapheme cluster in deleteSurroundingText().
+ controller().deleteSurroundingTextInCodePoints(1, 0);
+ EXPECT_STREQ("a\xE2\x98\x85 \xF0\x9F\x8F\x86 ", input->value().utf8().data());
+}
+
+TEST_F(InputMethodControllerTest,
+ DeleteSurroundingTextInCodePointsWithMultiCodeTextOnTheRight) {
+ HTMLInputElement* input =
+ toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample"));
+
+ // 'a' + "black star" + SPACE + "trophy" + SPACE + composed text
+ input->setValue(String::fromUTF8(
+ "a\xE2\x98\x85 \xF0\x9F\x8F\x86 \xE0\xB8\x81\xE0\xB9\x89"));
+ document().updateStyleAndLayout();
+ controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
+
+ controller().deleteSurroundingTextInCodePoints(0, 5);
+ EXPECT_STREQ("\xE0\xB8\x81\xE0\xB9\x89", input->value().utf8().data());
+
+ controller().deleteSurroundingTextInCodePoints(0, 1);
+ // TODO(yabinh): Same here. We should only delete 1 code point.
+ EXPECT_STREQ("", input->value().utf8().data());
+}
+
+TEST_F(InputMethodControllerTest,
+ DeleteSurroundingTextInCodePointsWithMultiCodeTextOnBothSides) {
+ HTMLInputElement* input =
+ toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample"));
+
+ // 'a' + "black star" + SPACE + "trophy" + SPACE + composed text
+ input->setValue(String::fromUTF8(
+ "a\xE2\x98\x85 \xF0\x9F\x8F\x86 \xE0\xB8\x81\xE0\xB9\x89"));
+ document().updateStyleAndLayout();
+ controller().setEditableSelectionOffsets(PlainTextRange(3, 3));
+ controller().deleteSurroundingTextInCodePoints(2, 2);
+ EXPECT_STREQ("a\xE0\xB8\x81\xE0\xB9\x89", input->value().utf8().data());
+}
+
+TEST_F(InputMethodControllerTest,
+ DeleteSurroundingTextInCodePointsWithRangeSelection) {
+ HTMLInputElement* input =
+ toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample"));
+
+ input->setValue("abc123");
+ document().updateStyleAndLayout();
+ controller().setEditableSelectionOffsets(PlainTextRange(2, 4));
+ EXPECT_STREQ("abc123", input->value().utf8().data());
+
+ controller().deleteSurroundingTextInCodePoints(1, 2);
+ EXPECT_STREQ("ac1", input->value().utf8().data());
+}
+
+TEST_F(InputMethodControllerTest,
+ DeleteSurroundingTextInCodePointsForMultipleNodes) {
+ Element* div = insertHTMLElement(
+ "<div id='sample' contenteditable>aaa"
+ "<div id='sample2' contenteditable>bbb"
+ "<div id='sample3' contenteditable>ccc"
+ "<div id='sample4' contenteditable>ddd"
+ "<div id='sample5' contenteditable>eee"
+ "</div></div></div></div></div>",
+ "sample");
+
+ controller().setEditableSelectionOffsets(PlainTextRange(8, 8));
+ EXPECT_STREQ("aaa\nbbb\nccc\nddd\neee", div->innerText().utf8().data());
+ EXPECT_EQ(8u, controller().getSelectionOffsets().start());
+ EXPECT_EQ(8u, controller().getSelectionOffsets().end());
+
+ controller().deleteSurroundingTextInCodePoints(1, 0);
+ EXPECT_STREQ("aaa\nbbbccc\nddd\neee", div->innerText().utf8().data());
+ EXPECT_EQ(7u, controller().getSelectionOffsets().start());
+ EXPECT_EQ(7u, controller().getSelectionOffsets().end());
+
+ controller().deleteSurroundingTextInCodePoints(0, 4);
+ EXPECT_STREQ("aaa\nbbbddd\neee", div->innerText().utf8().data());
+ EXPECT_EQ(7u, controller().getSelectionOffsets().start());
+ EXPECT_EQ(7u, controller().getSelectionOffsets().end());
+
+ controller().deleteSurroundingTextInCodePoints(5, 5);
+ EXPECT_STREQ("aaee", div->innerText().utf8().data());
+ EXPECT_EQ(2u, controller().getSelectionOffsets().start());
+ EXPECT_EQ(2u, controller().getSelectionOffsets().end());
+}
+
+TEST_F(InputMethodControllerTest, DeleteSurroundingTextInCodePointsWithImage) {
+ Element* div = insertHTMLElement(
+ "<div id='sample' contenteditable>aaa"
+ "<img src='empty.png'>bbb</div>",
+ "sample");
+
+ controller().setEditableSelectionOffsets(PlainTextRange(4, 4));
+ controller().deleteSurroundingTextInCodePoints(1, 1);
+ EXPECT_STREQ("aaabb", div->innerText().utf8().data());
+ EXPECT_EQ(3u, controller().getSelectionOffsets().start());
+ EXPECT_EQ(3u, controller().getSelectionOffsets().end());
+}
+
+TEST_F(InputMethodControllerTest,
+ DeleteSurroundingTextInCodePointsWithInvalidSurrogatePair) {
+ HTMLInputElement* input =
+ toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample"));
+
+ // 'a' + high surrogate of "trophy" + "black star" + low surrogate of "trophy"
+ // + SPACE
+ const UChar UText[] = {'a', 0xD83C, 0x2605, 0xDFC6, ' ', '\0'};
+ const String& text = String(UText);
+
+ input->setValue(text);
+ document().updateStyleAndLayout();
+ // The invalid high surrogate is encoded as '\xED\xA0\xBC', and invalid low
+ // surrogate is encoded as '\xED\xBF\x86'.
+ EXPECT_STREQ("a\xED\xA0\xBC\xE2\x98\x85\xED\xBF\x86 ",
+ input->value().utf8().data());
+
+ controller().setEditableSelectionOffsets(PlainTextRange(5, 5));
+ // Delete a SPACE.
+ controller().deleteSurroundingTextInCodePoints(1, 0);
+ EXPECT_STREQ("a\xED\xA0\xBC\xE2\x98\x85\xED\xBF\x86",
+ input->value().utf8().data());
+ // Do nothing since there is an invalid surrogate in the requested range.
+ controller().deleteSurroundingTextInCodePoints(2, 0);
+ EXPECT_STREQ("a\xED\xA0\xBC\xE2\x98\x85\xED\xBF\x86",
+ input->value().utf8().data());
+
+ controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
+ // Delete 'a'.
+ controller().deleteSurroundingTextInCodePoints(0, 1);
+ EXPECT_STREQ("\xED\xA0\xBC\xE2\x98\x85\xED\xBF\x86",
+ input->value().utf8().data());
+ // Do nothing since there is an invalid surrogate in the requested range.
+ controller().deleteSurroundingTextInCodePoints(0, 2);
+ EXPECT_STREQ("\xED\xA0\xBC\xE2\x98\x85\xED\xBF\x86",
+ input->value().utf8().data());
+}
+
TEST_F(InputMethodControllerTest, SetCompositionForInputWithNewCaretPositions) {
HTMLInputElement* input =
toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample"));

Powered by Google App Engine
This is Rietveld 408576698