Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "core/editing/PlainTextRange.h" | |
| 6 | |
| 7 #include "core/dom/Element.h" | |
| 8 #include "core/dom/Range.h" | |
| 9 #include "core/editing/FrameSelection.h" | |
| 10 #include "core/editing/InputMethodController.h" | |
| 11 #include "core/frame/LocalFrame.h" | |
| 12 #include "core/html/HTMLDocument.h" | |
| 13 #include "core/html/HTMLInputElement.h" | |
| 14 #include "core/testing/DummyPageHolder.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace blink { | |
| 18 | |
| 19 class PlainTextRangeTest : public ::testing::Test { | |
| 20 protected: | |
| 21 HTMLDocument& document() const { return *m_document; } | |
| 22 LocalFrame& frame() const { return m_dummyPageHolder->frame(); } | |
| 23 Element* insertHTMLElement(const char* elementCode, const char* elementId); | |
| 24 | |
| 25 private: | |
| 26 void SetUp() override; | |
| 27 | |
| 28 OwnPtr<DummyPageHolder> m_dummyPageHolder; | |
| 29 Persistent<HTMLDocument> m_document; | |
| 30 }; | |
| 31 | |
| 32 void PlainTextRangeTest::SetUp() | |
| 33 { | |
| 34 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600)); | |
| 35 m_document = toHTMLDocument(&m_dummyPageHolder->document()); | |
| 36 ASSERT(m_document); | |
| 37 } | |
| 38 | |
| 39 Element* PlainTextRangeTest::insertHTMLElement( | |
| 40 const char* elementCode, const char* elementId) | |
| 41 { | |
| 42 document().write(elementCode); | |
| 43 document().updateLayout(); | |
| 44 Element* element = document().getElementById(elementId); | |
| 45 element->focus(); | |
| 46 return element; | |
| 47 } | |
| 48 | |
| 49 TEST_F(PlainTextRangeTest, CreateDifferentRanges) | |
|
Changwan Ryu
2016/04/05 07:50:37
CreateRange?
yabinh
2016/04/05 08:06:42
Acknowledged.
| |
| 50 { | |
| 51 // Since the cursor never exceeds the left boundary, our test should focus o n right boundary cases. | |
| 52 HTMLInputElement* input = toHTMLInputElement( | |
| 53 insertHTMLElement("<input id='sample'>", "sample")); | |
| 54 | |
| 55 input->setValue("hello"); | |
| 56 Element* rootEditableElement = frame().selection().rootEditableElement(); | |
| 57 EXPECT_TRUE(rootEditableElement); | |
| 58 | |
| 59 // Start and End are on thEphemeralRangee right boundary | |
| 60 EphemeralRange range = PlainTextRange(5, 5).createRange(*rootEditableElement ); | |
| 61 EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode()); | |
| 62 EXPECT_EQ(5, range.endPosition().computeOffsetInContainerNode()); | |
| 63 | |
| 64 // Start is on the right boundary, and End exceeds right boundary | |
| 65 range = PlainTextRange(5, 10).createRange(*rootEditableElement); | |
| 66 EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode()); | |
| 67 EXPECT_EQ(5, range.endPosition().computeOffsetInContainerNode()); | |
| 68 | |
| 69 // Start and End exceed the right boundary | |
| 70 range = PlainTextRange(10, 10).createRange(*rootEditableElement); | |
| 71 EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode()); | |
| 72 EXPECT_EQ(5, range.endPosition().computeOffsetInContainerNode()); | |
| 73 | |
| 74 input->setValue(""); | |
| 75 rootEditableElement = frame().selection().rootEditableElement(); | |
| 76 EXPECT_TRUE(rootEditableElement); | |
| 77 | |
| 78 // Start and End are on thEphemeralRangee right boundary | |
| 79 range = PlainTextRange(0, 0).createRange(*rootEditableElement); | |
| 80 EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode()); | |
| 81 EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode()); | |
| 82 | |
| 83 // Start is on the right boundary, and End exceeds right boundary | |
| 84 range = PlainTextRange(0, 10).createRange(*rootEditableElement); | |
| 85 EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode()); | |
| 86 EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode()); | |
| 87 | |
| 88 // Start and End exceed the right boundary | |
| 89 range = PlainTextRange(10, 10).createRange(*rootEditableElement); | |
| 90 EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode()); | |
| 91 EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode()); | |
| 92 } | |
| 93 | |
| 94 TEST_F(PlainTextRangeTest, CreateDifferentRangesForSelection) | |
|
Changwan Ryu
2016/04/05 07:50:37
CreateRangeForSelection should be enough?
Could y
yabinh
2016/04/05 08:06:42
Acknowledged.
| |
| 95 { | |
| 96 // Since the cursor never exceeds the left boundary, our test should focus o n right boundary cases. | |
| 97 HTMLInputElement* input = toHTMLInputElement( | |
| 98 insertHTMLElement("<input id='sample'>", "sample")); | |
| 99 | |
| 100 input->setValue("hello"); | |
| 101 Element* rootEditableElement = frame().selection().rootEditableElement(); | |
| 102 EXPECT_TRUE(rootEditableElement); | |
| 103 | |
| 104 // Start and End are on the right boundary | |
|
Changwan Ryu
2016/04/05 07:50:37
no need to capitalize E and you need to add a peri
yabinh
2016/04/05 08:06:41
Acknowledged.
| |
| 105 EphemeralRange range = PlainTextRange(5, 5).createRangeForSelection(*rootEdi tableElement); | |
| 106 EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode()); | |
| 107 EXPECT_EQ(5, range.endPosition().computeOffsetInContainerNode()); | |
| 108 | |
| 109 // Start is on the right boundary, and End exceeds right boundary | |
| 110 range = PlainTextRange(5, 10).createRangeForSelection(*rootEditableElement); | |
| 111 EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode()); | |
| 112 EXPECT_EQ(5, range.endPosition().computeOffsetInContainerNode()); | |
| 113 | |
| 114 // Start and End exceed the right boundary | |
| 115 range = PlainTextRange(10, 10).createRangeForSelection(*rootEditableElement) ; | |
| 116 EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode()); | |
| 117 EXPECT_EQ(5, range.endPosition().computeOffsetInContainerNode()); | |
| 118 | |
| 119 input->setValue(""); | |
| 120 rootEditableElement = frame().selection().rootEditableElement(); | |
| 121 EXPECT_TRUE(rootEditableElement); | |
| 122 | |
| 123 // Start and End are on the right boundary | |
| 124 range = PlainTextRange(0, 0).createRangeForSelection(*rootEditableElement); | |
| 125 EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode()); | |
| 126 EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode()); | |
| 127 | |
| 128 // Start is on the right boundary, and End exceeds right boundary | |
| 129 range = PlainTextRange(0, 10).createRangeForSelection(*rootEditableElement); | |
| 130 EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode()); | |
| 131 EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode()); | |
| 132 | |
| 133 // Start and End exceed the right boundary | |
| 134 range = PlainTextRange(10, 10).createRangeForSelection(*rootEditableElement) ; | |
| 135 EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode()); | |
| 136 EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode()); | |
| 137 } | |
| 138 } | |
| OLD | NEW |