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 223460103846b958d5c47b0ef3de8a3bddf3ad2e..6731759d750b7ba294c126fbbf9f2573e0cf8a90 100644 |
--- a/third_party/WebKit/Source/core/editing/InputMethodControllerTest.cpp |
+++ b/third_party/WebKit/Source/core/editing/InputMethodControllerTest.cpp |
@@ -1427,4 +1427,307 @@ TEST_F(InputMethodControllerTest, SetEmptyCompositionShouldNotMoveCaret) { |
EXPECT_STREQ("abc\ndef", textarea->value().utf8().data()); |
} |
+static String getMarkedText(DocumentMarkerController& documentMarkerController, |
+ Node* node, |
+ int markerIndex) { |
+ DocumentMarker* marker = documentMarkerController.markers()[markerIndex]; |
+ return node->textContent().substring( |
+ marker->startOffset(), marker->endOffset() - marker->startOffset()); |
+} |
+ |
+TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarker) { |
+ Element* div = insertHTMLElement( |
+ "<div id='sample' contenteditable>Initial text blah</div>", "sample"); |
+ |
+ // Add marker under "text" (use TextMatch since Composition markers don't |
+ // persist across editing operations) |
+ EphemeralRange markerRange = PlainTextRange(8, 12).createRange(*div); |
+ document().markers().addMarker(markerRange.startPosition(), |
+ markerRange.endPosition(), |
+ DocumentMarker::TextMatch); |
+ // Delete "Initial" |
+ Vector<CompositionUnderline> emptyUnderlines; |
+ controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); |
+ controller().commitText(String(""), emptyUnderlines, 0); |
+ |
+ // Delete "blah" |
+ controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); |
+ controller().commitText(String(""), emptyUnderlines, 0); |
+ |
+ ASSERT_STREQ(" text ", div->innerHTML().utf8().data()); |
Xiaocheng
2017/03/01 03:56:25
I prefer not checking precondition in a test. Inst
|
+ |
+ // Check that the marker is still attached to "text" and doesn't include |
+ // either space around it |
+ EXPECT_EQ(1u, document().markers().markersFor(div->firstChild()).size()); |
+ ASSERT_STREQ( |
Xiaocheng
2017/03/01 03:56:25
Can we use EXPECT_STREQ to align with other tests?
|
+ "text", |
+ getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); |
+} |
+ |
+TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarker2) { |
+ Element* div = insertHTMLElement( |
+ "<div id='sample' contenteditable>Initial text blah</div>", "sample"); |
+ |
+ // Add marker under " text" (use TextMatch since Composition markers don't |
+ // persist across editing operations) |
+ EphemeralRange markerRange = PlainTextRange(7, 12).createRange(*div); |
+ document().markers().addMarker(markerRange.startPosition(), |
+ markerRange.endPosition(), |
+ DocumentMarker::TextMatch); |
+ // Delete "Initial" |
+ Vector<CompositionUnderline> emptyUnderlines; |
+ controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); |
+ controller().commitText(String(""), emptyUnderlines, 0); |
+ |
+ // Delete "blah" |
+ controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); |
+ controller().commitText(String(""), emptyUnderlines, 0); |
+ |
+ ASSERT_STREQ(" text ", div->innerHTML().utf8().data()); |
+ |
+ // Check that the marker was split when the space at the beginning was |
+ // converted to an nbsp |
+ EXPECT_EQ(2u, document().markers().markers().size()); |
+ ASSERT_STREQ( |
+ "\xC2\xA0", // UTF-8 for an nbsp |
Xiaocheng
2017/03/01 03:56:25
I'm wondering why we get '\xC2'... Shouldn't nbsp
rlanday
2017/03/01 04:33:12
The Unicode code point is U+00A0. This is in the r
Xiaocheng
2017/03/01 04:40:51
I see.
Thanks.
|
+ getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); |
+ ASSERT_STREQ( |
+ "text", |
+ getMarkedText(document().markers(), div->firstChild(), 1).utf8().data()); |
+} |
+ |
+TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarker3) { |
+ Element* div = insertHTMLElement( |
+ "<div id='sample' contenteditable>Initial text blah</div>", "sample"); |
+ |
+ // Add marker under "text " (use TextMatch since Composition markers don't |
+ // persist across editing operations) |
+ EphemeralRange markerRange = PlainTextRange(8, 13).createRange(*div); |
+ document().markers().addMarker(markerRange.startPosition(), |
+ markerRange.endPosition(), |
+ DocumentMarker::TextMatch); |
+ // Delete "Initial" |
+ Vector<CompositionUnderline> emptyUnderlines; |
+ controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); |
+ controller().commitText(String(""), emptyUnderlines, 0); |
+ |
+ // Delete "blah" |
+ controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); |
+ controller().commitText(String(""), emptyUnderlines, 0); |
+ |
+ ASSERT_STREQ(" text ", div->innerHTML().utf8().data()); |
+ |
+ // Check that the marker was split when the space at the end was |
+ // converted to an nbsp |
+ EXPECT_EQ(2u, document().markers().markers().size()); |
+ ASSERT_STREQ( |
+ "text", |
+ getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); |
+ ASSERT_STREQ( |
+ "\xC2\xA0", // UTF-8 for an nbsp |
+ getMarkedText(document().markers(), div->firstChild(), 1).utf8().data()); |
+} |
+ |
+TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarker4) { |
+ Element* div = insertHTMLElement( |
+ "<div id='sample' contenteditable>Initial text blah</div>", "sample"); |
+ |
+ // Add marker under " text " (use TextMatch since Composition markers don't |
+ // persist across editing operations) |
+ EphemeralRange markerRange = PlainTextRange(7, 13).createRange(*div); |
+ document().markers().addMarker(markerRange.startPosition(), |
+ markerRange.endPosition(), |
+ DocumentMarker::TextMatch); |
+ |
+ // Delete "Initial" |
+ Vector<CompositionUnderline> emptyUnderlines; |
+ controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); |
+ controller().commitText(String(""), emptyUnderlines, 0); |
+ |
+ // Delete "blah" |
+ controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); |
+ controller().commitText(String(""), emptyUnderlines, 0); |
+ |
+ ASSERT_STREQ(" text ", div->innerHTML().utf8().data()); |
+ |
+ // Check that the marker was split into three pieces when the two spaces were |
+ // converted to nbsps |
+ EXPECT_EQ(3u, document().markers().markers().size()); |
+ ASSERT_STREQ( |
+ "\xC2\xA0", // UTF-8 for an nbsp |
+ getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); |
+ ASSERT_STREQ( |
+ "text", |
+ getMarkedText(document().markers(), div->firstChild(), 1).utf8().data()); |
+ ASSERT_STREQ( |
+ "\xC2\xA0", // UTF-8 for an nbsp |
+ getMarkedText(document().markers(), div->firstChild(), 2).utf8().data()); |
+} |
+ |
+TEST_F(InputMethodControllerTest, ReplaceStartOfMarker) { |
+ Element* div = insertHTMLElement( |
+ "<div id='sample' contenteditable>Initial text</div>", "sample"); |
+ |
+ // Add marker under "Initial text" |
+ EphemeralRange markerRange = PlainTextRange(0, 12).createRange(*div); |
+ document().markers().addMarker(markerRange.startPosition(), |
+ markerRange.endPosition(), |
+ DocumentMarker::TextMatch); |
+ |
+ // Replace "Initial" with "Original" |
+ Vector<CompositionUnderline> emptyUnderlines; |
+ controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); |
+ controller().commitText(String("Original"), emptyUnderlines, 0); |
+ |
+ ASSERT_STREQ("Original text", div->innerHTML().utf8().data()); |
+ |
+ // Verify marker is under "al text" |
+ // ("Initial" and "Original" have "al" as a common suffix) |
+ EXPECT_EQ(1u, document().markers().markers().size()); |
+ ASSERT_STREQ( |
+ "al text", |
+ getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); |
+} |
+ |
+TEST_F(InputMethodControllerTest, ReplaceBeforeAndAfterStartOfMarker) { |
Xiaocheng
2017/03/01 03:56:25
"ReplaceTextContainsStartOfMarker" seems a better
|
+ Element* div = insertHTMLElement( |
+ "<div id='sample' contenteditable>This is some initial text</div>", |
+ "sample"); |
+ |
+ // Add marker under "initial text" |
+ EphemeralRange markerRange = PlainTextRange(13, 25).createRange(*div); |
+ document().markers().addMarker(markerRange.startPosition(), |
+ markerRange.endPosition(), |
+ DocumentMarker::TextMatch); |
+ |
+ // Replace "some initial" with "boring" |
+ Vector<CompositionUnderline> emptyUnderlines; |
+ controller().setCompositionFromExistingText(emptyUnderlines, 8, 20); |
+ controller().commitText(String("boring"), emptyUnderlines, 0); |
+ |
+ ASSERT_STREQ("This is boring text", div->innerHTML().utf8().data()); |
+ |
+ // Verify marker is under " text" |
+ EXPECT_EQ(1u, document().markers().markers().size()); |
+ ASSERT_STREQ( |
+ " text", |
+ getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); |
+} |
+ |
+TEST_F(InputMethodControllerTest, ReplaceEndOfMarker) { |
+ Element* div = insertHTMLElement( |
+ "<div id='sample' contenteditable>Initial text</div>", "sample"); |
+ |
+ // Add marker under "Initial text" |
+ EphemeralRange markerRange = PlainTextRange(0, 12).createRange(*div); |
+ document().markers().addMarker(markerRange.startPosition(), |
+ markerRange.endPosition(), |
+ DocumentMarker::TextMatch); |
+ |
+ // Replace "text" with "string" |
+ Vector<CompositionUnderline> emptyUnderlines; |
+ controller().setCompositionFromExistingText(emptyUnderlines, 8, 12); |
+ controller().commitText(String("string"), emptyUnderlines, 0); |
+ |
+ ASSERT_STREQ("Initial string", div->innerHTML().utf8().data()); |
+ |
+ // Verify marker is under "Initial " |
+ EXPECT_EQ(1u, document().markers().markers().size()); |
+ ASSERT_STREQ( |
+ "Initial ", |
+ getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); |
+} |
+ |
+TEST_F(InputMethodControllerTest, ReplaceBeforeAndAfterEndOfMarker) { |
Xiaocheng
2017/03/01 03:56:25
"ReplaceTextContainsEndOfMarker" seems to be a bet
|
+ Element* div = insertHTMLElement( |
+ "<div id='sample' contenteditable>This is some initial text</div>", |
+ "sample"); |
+ |
+ // Add marker under "some initial" |
+ EphemeralRange markerRange = PlainTextRange(8, 20).createRange(*div); |
+ document().markers().addMarker(markerRange.startPosition(), |
+ markerRange.endPosition(), |
+ DocumentMarker::TextMatch); |
+ |
+ // Replace "initial text" with "content" |
+ Vector<CompositionUnderline> emptyUnderlines; |
+ controller().setCompositionFromExistingText(emptyUnderlines, 13, 25); |
+ controller().commitText(String("content"), emptyUnderlines, 0); |
+ |
+ ASSERT_STREQ("This is some content", div->innerHTML().utf8().data()); |
+ |
+ // Verify marker is under "some " |
+ EXPECT_EQ(1u, document().markers().markers().size()); |
+ ASSERT_STREQ( |
+ "some ", |
+ getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); |
+} |
+ |
+TEST_F(InputMethodControllerTest, ReplaceEntireMarker) { |
+ Element* div = insertHTMLElement( |
+ "<div id='sample' contenteditable>Initial text</div>", "sample"); |
+ |
+ // Add marker under "text" |
+ EphemeralRange markerRange = PlainTextRange(8, 12).createRange(*div); |
+ document().markers().addMarker(markerRange.startPosition(), |
+ markerRange.endPosition(), |
+ DocumentMarker::TextMatch); |
+ |
+ // Replace "text" with "string" |
+ Vector<CompositionUnderline> emptyUnderlines; |
+ controller().setCompositionFromExistingText(emptyUnderlines, 8, 12); |
+ controller().commitText(String("string"), emptyUnderlines, 0); |
+ |
+ ASSERT_STREQ("Initial string", div->innerHTML().utf8().data()); |
+ |
+ // Verify marker was removed |
+ EXPECT_EQ(0u, document().markers().markers().size()); |
+} |
+ |
+TEST_F(InputMethodControllerTest, ReplaceTextWithMarkerAtBeginning) { |
+ Element* div = insertHTMLElement( |
+ "<div id='sample' contenteditable>Initial text</div>", "sample"); |
+ |
+ // Add marker under "Initial" |
+ EphemeralRange markerRange = PlainTextRange(0, 7).createRange(*div); |
+ document().markers().addMarker(markerRange.startPosition(), |
+ markerRange.endPosition(), |
+ DocumentMarker::TextMatch); |
+ |
+ EXPECT_EQ(1u, document().markers().markers().size()); |
+ |
+ // Replace "Initial text" with "New string" |
+ Vector<CompositionUnderline> emptyUnderlines; |
+ controller().setCompositionFromExistingText(emptyUnderlines, 0, 12); |
+ controller().commitText(String("New string"), emptyUnderlines, 0); |
+ |
+ ASSERT_STREQ("New string", div->innerHTML().utf8().data()); |
+ |
+ // Verify marker was removed |
+ EXPECT_EQ(0u, document().markers().markers().size()); |
+} |
+ |
+TEST_F(InputMethodControllerTest, ReplaceTextWithMarkerAtEnd) { |
+ Element* div = insertHTMLElement( |
+ "<div id='sample' contenteditable>Initial text</div>", "sample"); |
+ |
+ // Add marker under "text" |
+ EphemeralRange markerRange = PlainTextRange(8, 12).createRange(*div); |
+ document().markers().addMarker(markerRange.startPosition(), |
+ markerRange.endPosition(), |
+ DocumentMarker::TextMatch); |
+ |
+ EXPECT_EQ(1u, document().markers().markers().size()); |
+ |
+ // Replace "Initial text" with "New string" |
+ Vector<CompositionUnderline> emptyUnderlines; |
+ controller().setCompositionFromExistingText(emptyUnderlines, 0, 12); |
+ controller().commitText(String("New string"), emptyUnderlines, 0); |
+ |
+ ASSERT_STREQ("New string", div->innerHTML().utf8().data()); |
+ |
+ // Verify marker was removed |
+ EXPECT_EQ(0u, document().markers().markers().size()); |
+} |
+ |
} // namespace blink |