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

Side by Side Diff: third_party/WebKit/Source/core/editing/InputMethodControllerTest.cpp

Issue 2723663002: Refactor DocumentMarkerController (Closed)
Patch Set: Make requested changes, rebase (HashMap::remove() => HashMap::erase()) Created 3 years, 9 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 unified diff | Download patch
OLDNEW
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 <memory> 7 #include <memory>
8 #include "core/dom/Document.h" 8 #include "core/dom/Document.h"
9 #include "core/dom/Element.h" 9 #include "core/dom/Element.h"
10 #include "core/dom/Range.h" 10 #include "core/dom/Range.h"
(...skipping 1490 matching lines...) Expand 10 before | Expand all | Expand 10 after
1501 DocumentMarker::TextMatch); 1501 DocumentMarker::TextMatch);
1502 // Delete "Initial" 1502 // Delete "Initial"
1503 Vector<CompositionUnderline> emptyUnderlines; 1503 Vector<CompositionUnderline> emptyUnderlines;
1504 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); 1504 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7);
1505 controller().commitText(String(""), emptyUnderlines, 0); 1505 controller().commitText(String(""), emptyUnderlines, 0);
1506 1506
1507 // Delete "blah" 1507 // Delete "blah"
1508 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); 1508 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10);
1509 controller().commitText(String(""), emptyUnderlines, 0); 1509 controller().commitText(String(""), emptyUnderlines, 0);
1510 1510
1511 // Check that the marker was split when the space at the beginning was 1511 // Check that the marker is still attached to " text" and includes the space
1512 // converted to an nbsp 1512 // before "text" but not the space after
1513 EXPECT_EQ(2u, document().markers().markers().size()); 1513 EXPECT_EQ(1u, document().markers().markers().size());
1514 EXPECT_STREQ( 1514 ASSERT_STREQ(
1515 "\xC2\xA0", // UTF-8 for an nbsp 1515 "\xC2\xA0text",
1516 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); 1516 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data());
1517 EXPECT_STREQ(
1518 "text",
1519 getMarkedText(document().markers(), div->firstChild(), 1).utf8().data());
1520 } 1517 }
1521 1518
1522 TEST_F(InputMethodControllerTest, 1519 TEST_F(InputMethodControllerTest,
1523 Marker_WhitespaceFixupAroundMarkerEndingWithSpace) { 1520 Marker_WhitespaceFixupAroundMarkerEndingWithSpace) {
1524 Element* div = insertHTMLElement( 1521 Element* div = insertHTMLElement(
1525 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); 1522 "<div id='sample' contenteditable>Initial text blah</div>", "sample");
1526 1523
1527 // Add marker under "text " (use TextMatch since Composition markers don't 1524 // Add marker under "text " (use TextMatch since Composition markers don't
1528 // persist across editing operations) 1525 // persist across editing operations)
1529 EphemeralRange markerRange = PlainTextRange(8, 13).createRange(*div); 1526 EphemeralRange markerRange = PlainTextRange(8, 13).createRange(*div);
1530 document().markers().addMarker(markerRange.startPosition(), 1527 document().markers().addMarker(markerRange.startPosition(),
1531 markerRange.endPosition(), 1528 markerRange.endPosition(),
1532 DocumentMarker::TextMatch); 1529 DocumentMarker::TextMatch);
1533 // Delete "Initial" 1530 // Delete "Initial"
1534 Vector<CompositionUnderline> emptyUnderlines; 1531 Vector<CompositionUnderline> emptyUnderlines;
1535 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); 1532 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7);
1536 controller().commitText(String(""), emptyUnderlines, 0); 1533 controller().commitText(String(""), emptyUnderlines, 0);
1537 1534
1538 // Delete "blah" 1535 // Delete "blah"
1539 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); 1536 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10);
1540 controller().commitText(String(""), emptyUnderlines, 0); 1537 controller().commitText(String(""), emptyUnderlines, 0);
1541 1538
1542 // Check that the marker was split when the space at the end was 1539 // Check that the marker is still attached to "text " and includes the space
1543 // converted to an nbsp 1540 // after "text" but not the space before
1544 EXPECT_EQ(2u, document().markers().markers().size()); 1541 EXPECT_EQ(1u, document().markers().markers().size());
1545 EXPECT_STREQ( 1542 ASSERT_STREQ(
1546 "text", 1543 "text\xC2\xA0",
1547 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); 1544 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data());
1548 EXPECT_STREQ(
1549 "\xC2\xA0", // UTF-8 for an nbsp
1550 getMarkedText(document().markers(), div->firstChild(), 1).utf8().data());
1551 } 1545 }
1552 1546
1553 TEST_F(InputMethodControllerTest, 1547 TEST_F(InputMethodControllerTest,
1554 Marker_WhitespaceFixupAroundMarkerBeginningAndEndingWithSpaces) { 1548 Marker_WhitespaceFixupAroundMarkerBeginningAndEndingWithSpaces) {
1555 Element* div = insertHTMLElement( 1549 Element* div = insertHTMLElement(
1556 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); 1550 "<div id='sample' contenteditable>Initial text blah</div>", "sample");
1557 1551
1558 // Add marker under " text " (use TextMatch since Composition markers don't 1552 // Add marker under " text " (use TextMatch since Composition markers don't
1559 // persist across editing operations) 1553 // persist across editing operations)
1560 EphemeralRange markerRange = PlainTextRange(7, 13).createRange(*div); 1554 EphemeralRange markerRange = PlainTextRange(7, 13).createRange(*div);
1561 document().markers().addMarker(markerRange.startPosition(), 1555 document().markers().addMarker(markerRange.startPosition(),
1562 markerRange.endPosition(), 1556 markerRange.endPosition(),
1563 DocumentMarker::TextMatch); 1557 DocumentMarker::TextMatch);
1564 1558
1565 // Delete "Initial" 1559 // Delete "Initial"
1566 Vector<CompositionUnderline> emptyUnderlines; 1560 Vector<CompositionUnderline> emptyUnderlines;
1567 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); 1561 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7);
1568 controller().commitText(String(""), emptyUnderlines, 0); 1562 controller().commitText(String(""), emptyUnderlines, 0);
1569 1563
1570 // Delete "blah" 1564 // Delete "blah"
1571 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); 1565 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10);
1572 controller().commitText(String(""), emptyUnderlines, 0); 1566 controller().commitText(String(""), emptyUnderlines, 0);
1573 1567
1574 // Check that the marker was split into three pieces when the two spaces were 1568 // Check that the marker is still attached to " text " and includes both the
1575 // converted to nbsps 1569 // space before "text" and the space after
1576 EXPECT_EQ(3u, document().markers().markers().size()); 1570 EXPECT_EQ(1u, document().markers().markers().size());
1577 EXPECT_STREQ( 1571 ASSERT_STREQ(
1578 "\xC2\xA0", // UTF-8 for an nbsp 1572 "\xC2\xA0text\xC2\xA0",
1579 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); 1573 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data());
1580 EXPECT_STREQ(
1581 "text",
1582 getMarkedText(document().markers(), div->firstChild(), 1).utf8().data());
1583 EXPECT_STREQ(
1584 "\xC2\xA0", // UTF-8 for an nbsp
1585 getMarkedText(document().markers(), div->firstChild(), 2).utf8().data());
1586 } 1574 }
1587 1575
1588 TEST_F(InputMethodControllerTest, Marker_ReplaceStartOfMarker) { 1576 TEST_F(InputMethodControllerTest, Marker_ReplaceStartOfMarker) {
1589 Element* div = insertHTMLElement( 1577 Element* div = insertHTMLElement(
1590 "<div id='sample' contenteditable>Initial text</div>", "sample"); 1578 "<div id='sample' contenteditable>Initial text</div>", "sample");
1591 1579
1592 // Add marker under "Initial text" 1580 // Add marker under "Initial text"
1593 EphemeralRange markerRange = PlainTextRange(0, 12).createRange(*div); 1581 EphemeralRange markerRange = PlainTextRange(0, 12).createRange(*div);
1594 document().markers().addMarker(markerRange.startPosition(), 1582 document().markers().addMarker(markerRange.startPosition(),
1595 markerRange.endPosition(), 1583 markerRange.endPosition(),
1596 DocumentMarker::TextMatch); 1584 DocumentMarker::TextMatch);
1597 1585
1598 // Replace "Initial" with "Original" 1586 // Replace "Initial" with "Original"
1599 Vector<CompositionUnderline> emptyUnderlines; 1587 Vector<CompositionUnderline> emptyUnderlines;
1600 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); 1588 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7);
1601 controller().commitText(String("Original"), emptyUnderlines, 0); 1589 controller().commitText(String("Original"), emptyUnderlines, 0);
1602 1590
1603 // Verify marker is under "al text" 1591 // Verify marker is under "Original text"
1604 // ("Initial" and "Original" have "al" as a common suffix)
1605 EXPECT_EQ(1u, document().markers().markers().size()); 1592 EXPECT_EQ(1u, document().markers().markers().size());
1606 EXPECT_STREQ( 1593 ASSERT_STREQ(
1607 "al text", 1594 "Original text",
1608 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); 1595 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data());
1609 } 1596 }
1610 1597
1611 TEST_F(InputMethodControllerTest, Marker_ReplaceTextContainsStartOfMarker) { 1598 TEST_F(InputMethodControllerTest, Marker_ReplaceTextContainsStartOfMarker) {
1612 Element* div = insertHTMLElement( 1599 Element* div = insertHTMLElement(
1613 "<div id='sample' contenteditable>This is some initial text</div>", 1600 "<div id='sample' contenteditable>This is some initial text</div>",
1614 "sample"); 1601 "sample");
1615 1602
1616 // Add marker under "initial text" 1603 // Add marker under "initial text"
1617 EphemeralRange markerRange = PlainTextRange(13, 25).createRange(*div); 1604 EphemeralRange markerRange = PlainTextRange(13, 25).createRange(*div);
(...skipping 21 matching lines...) Expand all
1639 EphemeralRange markerRange = PlainTextRange(0, 12).createRange(*div); 1626 EphemeralRange markerRange = PlainTextRange(0, 12).createRange(*div);
1640 document().markers().addMarker(markerRange.startPosition(), 1627 document().markers().addMarker(markerRange.startPosition(),
1641 markerRange.endPosition(), 1628 markerRange.endPosition(),
1642 DocumentMarker::TextMatch); 1629 DocumentMarker::TextMatch);
1643 1630
1644 // Replace "text" with "string" 1631 // Replace "text" with "string"
1645 Vector<CompositionUnderline> emptyUnderlines; 1632 Vector<CompositionUnderline> emptyUnderlines;
1646 controller().setCompositionFromExistingText(emptyUnderlines, 8, 12); 1633 controller().setCompositionFromExistingText(emptyUnderlines, 8, 12);
1647 controller().commitText(String("string"), emptyUnderlines, 0); 1634 controller().commitText(String("string"), emptyUnderlines, 0);
1648 1635
1649 // Verify marker is under "Initial " 1636 // Verify marker is under "Initial string"
1650 EXPECT_EQ(1u, document().markers().markers().size()); 1637 EXPECT_EQ(1u, document().markers().markers().size());
1651 EXPECT_STREQ( 1638 ASSERT_STREQ(
1652 "Initial ", 1639 "Initial string",
1653 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); 1640 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data());
1654 } 1641 }
1655 1642
1656 TEST_F(InputMethodControllerTest, Marker_ReplaceTextContainsEndOfMarker) { 1643 TEST_F(InputMethodControllerTest, Marker_ReplaceTextContainsEndOfMarker) {
1657 Element* div = insertHTMLElement( 1644 Element* div = insertHTMLElement(
1658 "<div id='sample' contenteditable>This is some initial text</div>", 1645 "<div id='sample' contenteditable>This is some initial text</div>",
1659 "sample"); 1646 "sample");
1660 1647
1661 // Add marker under "some initial" 1648 // Add marker under "some initial"
1662 EphemeralRange markerRange = PlainTextRange(8, 20).createRange(*div); 1649 EphemeralRange markerRange = PlainTextRange(8, 20).createRange(*div);
(...skipping 23 matching lines...) Expand all
1686 EphemeralRange markerRange = PlainTextRange(8, 12).createRange(*div); 1673 EphemeralRange markerRange = PlainTextRange(8, 12).createRange(*div);
1687 document().markers().addMarker(markerRange.startPosition(), 1674 document().markers().addMarker(markerRange.startPosition(),
1688 markerRange.endPosition(), 1675 markerRange.endPosition(),
1689 DocumentMarker::TextMatch); 1676 DocumentMarker::TextMatch);
1690 1677
1691 // Replace "text" with "string" 1678 // Replace "text" with "string"
1692 Vector<CompositionUnderline> emptyUnderlines; 1679 Vector<CompositionUnderline> emptyUnderlines;
1693 controller().setCompositionFromExistingText(emptyUnderlines, 8, 12); 1680 controller().setCompositionFromExistingText(emptyUnderlines, 8, 12);
1694 controller().commitText(String("string"), emptyUnderlines, 0); 1681 controller().commitText(String("string"), emptyUnderlines, 0);
1695 1682
1696 // Verify marker was removed 1683 // Verify marker is under "string"
1697 EXPECT_EQ(0u, document().markers().markers().size()); 1684 EXPECT_EQ(1u, document().markers().markers().size());
1685 ASSERT_STREQ(
1686 "string",
1687 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data());
1698 } 1688 }
1699 1689
1700 TEST_F(InputMethodControllerTest, Marker_ReplaceTextWithMarkerAtBeginning) { 1690 TEST_F(InputMethodControllerTest, Marker_ReplaceTextWithMarkerAtBeginning) {
1701 Element* div = insertHTMLElement( 1691 Element* div = insertHTMLElement(
1702 "<div id='sample' contenteditable>Initial text</div>", "sample"); 1692 "<div id='sample' contenteditable>Initial text</div>", "sample");
1703 1693
1704 // Add marker under "Initial" 1694 // Add marker under "Initial"
1705 EphemeralRange markerRange = PlainTextRange(0, 7).createRange(*div); 1695 EphemeralRange markerRange = PlainTextRange(0, 7).createRange(*div);
1706 document().markers().addMarker(markerRange.startPosition(), 1696 document().markers().addMarker(markerRange.startPosition(),
1707 markerRange.endPosition(), 1697 markerRange.endPosition(),
(...skipping 25 matching lines...) Expand all
1733 // Replace "Initial text" with "New string" 1723 // Replace "Initial text" with "New string"
1734 Vector<CompositionUnderline> emptyUnderlines; 1724 Vector<CompositionUnderline> emptyUnderlines;
1735 controller().setCompositionFromExistingText(emptyUnderlines, 0, 12); 1725 controller().setCompositionFromExistingText(emptyUnderlines, 0, 12);
1736 controller().commitText(String("New string"), emptyUnderlines, 0); 1726 controller().commitText(String("New string"), emptyUnderlines, 0);
1737 1727
1738 // Verify marker was removed 1728 // Verify marker was removed
1739 EXPECT_EQ(0u, document().markers().markers().size()); 1729 EXPECT_EQ(0u, document().markers().markers().size());
1740 } 1730 }
1741 1731
1742 } // namespace blink 1732 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698