Chromium Code Reviews| Index: third_party/WebKit/Source/core/editing/PlainTextRangeTest.cpp |
| diff --git a/third_party/WebKit/Source/core/editing/PlainTextRangeTest.cpp b/third_party/WebKit/Source/core/editing/PlainTextRangeTest.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d2839a7cbb21c7f8d1c302d0100c10022d832f91 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/editing/PlainTextRangeTest.cpp |
| @@ -0,0 +1,234 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "core/editing/PlainTextRange.h" |
| + |
| +#include "core/dom/Element.h" |
| +#include "core/dom/Range.h" |
| +#include "core/editing/FrameSelection.h" |
| +#include "core/editing/InputMethodController.h" |
| +#include "core/frame/LocalFrame.h" |
| +#include "core/html/HTMLDocument.h" |
| +#include "core/html/HTMLInputElement.h" |
| +#include "core/testing/DummyPageHolder.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace blink { |
| + |
| +class PlainTextRangeTest : public ::testing::Test { |
| +protected: |
| + HTMLDocument& document() const { return *m_document; } |
| + LocalFrame& frame() const { return m_dummyPageHolder->frame(); } |
| + Element* insertHTMLElement(const char* elementCode, const char* elementId); |
| + |
| +private: |
| + void SetUp() override; |
| + |
| + OwnPtr<DummyPageHolder> m_dummyPageHolder; |
| + Persistent<HTMLDocument> m_document; |
| +}; |
| + |
| +void PlainTextRangeTest::SetUp() |
| +{ |
| + m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600)); |
| + m_document = toHTMLDocument(&m_dummyPageHolder->document()); |
| + ASSERT(m_document); |
| +} |
| + |
| +Element* PlainTextRangeTest::insertHTMLElement( |
| + const char* elementCode, const char* elementId) |
| +{ |
| + document().write(elementCode); |
| + document().updateLayout(); |
| + Element* element = document().getElementById(elementId); |
| + element->focus(); |
| + return element; |
| +} |
| + |
| +TEST_F(PlainTextRangeTest, CreateRangeForSelection) |
| +{ |
| + // Since the cursor never exceeds the left boundary, our test should focus on right boundary cases. |
| + HTMLInputElement* input = toHTMLInputElement( |
| + insertHTMLElement("<input id='sample'>", "sample")); |
| + |
| + input->setValue("hello"); |
| + Element* rootEditableElement = frame().selection().rootEditableElement(); |
| + EXPECT_TRUE(rootEditableElement); |
| + |
| + // start and end are on the right boundary. |
|
Changwan Ryu
2016/04/07 11:46:19
what happens when start / end are within boundary?
|
| + EphemeralRange range = PlainTextRange(5, 5).createRangeForSelection(*rootEditableElement); |
| + EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(5, range.endPosition().computeOffsetInContainerNode()); |
| + |
| + // start is on the right boundary, and end exceeds right boundary. |
| + range = PlainTextRange(5, 10).createRangeForSelection(*rootEditableElement); |
| + EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(5, range.endPosition().computeOffsetInContainerNode()); |
| + |
| + // start and end exceed the right boundary. |
| + range = PlainTextRange(10, 10).createRangeForSelection(*rootEditableElement); |
| + EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(5, range.endPosition().computeOffsetInContainerNode()); |
| + |
| + input->setValue(""); |
| + rootEditableElement = frame().selection().rootEditableElement(); |
| + EXPECT_TRUE(rootEditableElement); |
| + |
| + // start and end are on the right boundary. |
| + range = PlainTextRange(0, 0).createRangeForSelection(*rootEditableElement); |
| + EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode()); |
| + |
| + // start is on the right boundary, and end exceeds right boundary. |
| + range = PlainTextRange(0, 10).createRangeForSelection(*rootEditableElement); |
| + EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode()); |
| + |
| + // start and end exceed the right boundary. |
| + range = PlainTextRange(10, 10).createRangeForSelection(*rootEditableElement); |
| + EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode()); |
| +} |
| + |
| +TEST_F(PlainTextRangeTest, CreateRangeForInput) |
| +{ |
| + // Since the cursor never exceeds the left boundary, our test should focus on right boundary cases. |
|
Changwan Ryu
2016/04/07 11:46:19
this line is not needed
|
| + HTMLInputElement* input = toHTMLInputElement( |
| + insertHTMLElement("<input id='sample'>", "sample")); |
| + |
| + input->setValue("hello"); |
| + Element* rootEditableElement = frame().selection().rootEditableElement(); |
| + EXPECT_TRUE(rootEditableElement); |
| + |
| + // start and end are on the right boundary. |
| + EphemeralRange range = PlainTextRange(5, 5).createRange(*rootEditableElement); |
| + EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(5, range.endPosition().computeOffsetInContainerNode()); |
| + |
| + // start is on the right boundary, and end exceeds right boundary. |
| + range = PlainTextRange(5, 10).createRange(*rootEditableElement); |
| + EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(5, range.endPosition().computeOffsetInContainerNode()); |
| + |
| + // start and end exceed the right boundary. |
| + range = PlainTextRange(10, 10).createRange(*rootEditableElement); |
| + EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(5, range.endPosition().computeOffsetInContainerNode()); |
| + |
| + input->setValue(""); |
| + rootEditableElement = frame().selection().rootEditableElement(); |
| + EXPECT_TRUE(rootEditableElement); |
| + |
| + // start and end are on the right boundary. |
| + range = PlainTextRange(0, 0).createRange(*rootEditableElement); |
| + EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode()); |
| + |
| + // start is on the right boundary, and end exceeds right boundary. |
| + range = PlainTextRange(0, 10).createRange(*rootEditableElement); |
| + EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode()); |
| + |
| + // start and end exceed the right boundary. |
| + range = PlainTextRange(10, 10).createRange(*rootEditableElement); |
| + EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode()); |
| +} |
| + |
| +TEST_F(PlainTextRangeTest, CreateRangeForEditableWithEmptyNode) |
|
Changwan Ryu
2016/04/07 11:46:19
s/Editable/ContentEditable/ here and other places
|
| +{ |
| + // Since the cursor never exceeds the left boundary, our test should focus on right boundary cases. |
|
Changwan Ryu
2016/04/07 11:46:19
This line is probably not needed.
|
| + // There is only 1 empty node. |
| + Element* div = insertHTMLElement( |
| + "<div id='sample' contenteditable='true'></div>", "sample"); |
| + |
| + // start and end are on right boundary. |
| + EphemeralRange range = PlainTextRange(0, 0).createRange(*div); |
| + EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode()); |
| + |
| + // start is on right boundary, and end exceeds right boundary. |
| + range = PlainTextRange(0, 10).createRange(*div); |
| + EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode()); |
| + |
| + // start and end exceed right boundary. |
| + range = PlainTextRange(10, 10).createRange(*div); |
| + EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode()); |
| +} |
| + |
| +TEST_F(PlainTextRangeTest, CreateRangeForEditableWithLineBreakAtTheEnd) |
| +{ |
| + // Since the cursor never exceeds the left boundary, our test should focus on right boundary cases. |
| + // There are 2 nodes and 5+1 characters: "hello", '\n'. |
| + Element* div = insertHTMLElement( |
| + "<div id='sample' contenteditable='true'>" |
| + "hello" |
| + "<p id='sample2' contenteditable='true'></p>", |
| + "sample"); |
| + |
| + // start and end are on right boundary of the 1st node. |
| + EphemeralRange range = PlainTextRange(5, 5).createRange(*div); |
| + EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(5, range.endPosition().computeOffsetInContainerNode()); |
| + |
| + // start is on right boundary of the 1st node, and end is on right boundary of the 2nd node. |
| + range = PlainTextRange(5, 6).createRange(*div); |
| + EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(1, range.endPosition().computeOffsetInContainerNode()); |
| + |
| + // start and end are on right boundary of the 2nd node. |
| + range = PlainTextRange(6, 6).createRange(*div); |
| + EXPECT_EQ(1, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(1, range.endPosition().computeOffsetInContainerNode()); |
| + |
| + // start is on right boundary of the 2nd node, and end exceeds right boundary. |
| + range = PlainTextRange(6, 10).createRange(*div); |
| + EXPECT_EQ(1, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(1, range.endPosition().computeOffsetInContainerNode()); |
| + |
| + // start and end exceed right boundary. |
| + range = PlainTextRange(10, 10).createRange(*div); |
| + EXPECT_EQ(1, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(1, range.endPosition().computeOffsetInContainerNode()); |
| +} |
| + |
| +TEST_F(PlainTextRangeTest, CreateRangeForEditableWithLineBreakInTheMiddle) |
|
Changwan Ryu
2016/04/07 11:46:19
could you also check the cases where start and end
|
| +{ |
| + // Since the cursor never exceeds the left boundary, our test should focus on right boundary cases. |
|
Changwan Ryu
2016/04/07 11:46:19
this line is not needed
|
| + // There are 3 nodes and 5+1+6 characters: "hello", '\n', "world!". |
| + Element* div = insertHTMLElement( |
| + "<div id='sample' contenteditable='true'>" |
| + "hello" |
| + "<p id='sample2' contenteditable='true'>world!</p>" |
| + "</div>", |
| + "sample"); |
| + |
| + // start and end are on right boundary of the 1st node. |
| + EphemeralRange range = PlainTextRange(5, 5).createRange(*div); |
| + EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(5, range.endPosition().computeOffsetInContainerNode()); |
| + |
| + // start is on right boundary of the 1st node, and end is on right boundary of the 3rd node. |
| + range = PlainTextRange(5, 12).createRange(*div); |
| + EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(6, range.endPosition().computeOffsetInContainerNode()); |
| + |
| + // start is on right boundary of the 2nd node, and end is on right boundary of the 3rd node. |
| + range = PlainTextRange(6, 12).createRange(*div); |
| + EXPECT_EQ(1, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(6, range.endPosition().computeOffsetInContainerNode()); |
| + |
| + // start and end are on right boundary of the 3rd node. |
| + range = PlainTextRange(12, 12).createRange(*div); |
| + EXPECT_EQ(6, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(6, range.endPosition().computeOffsetInContainerNode()); |
| + |
| + // start and end exceed right boundary. |
| + range = PlainTextRange(100, 200).createRange(*div); |
| + EXPECT_EQ(6, range.startPosition().computeOffsetInContainerNode()); |
| + EXPECT_EQ(6, range.endPosition().computeOffsetInContainerNode()); |
| +} |
| +} |