OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "core/editing/SurroundingText.h" |
| 7 |
| 8 #include "core/dom/Position.h" |
| 9 #include "core/dom/Range.h" |
| 10 #include "core/dom/Text.h" |
| 11 #include "core/editing/VisibleSelection.h" |
| 12 #include "core/html/HTMLElement.h" |
| 13 #include "core/testing/DummyPageHolder.h" |
| 14 #include <gtest/gtest.h> |
| 15 |
| 16 using namespace WebCore; |
| 17 |
| 18 namespace { |
| 19 |
| 20 class SurroundingTextTest : public ::testing::Test { |
| 21 protected: |
| 22 Document& document() const { return m_dummyPageHolder->document(); } |
| 23 void setHTML(const String&); |
| 24 VisibleSelection select(int offset) { return select(offset, offset); } |
| 25 VisibleSelection select(int start, int end); |
| 26 |
| 27 private: |
| 28 virtual void SetUp() OVERRIDE; |
| 29 |
| 30 OwnPtr<DummyPageHolder> m_dummyPageHolder; |
| 31 }; |
| 32 |
| 33 void SurroundingTextTest::SetUp() |
| 34 { |
| 35 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600)); |
| 36 } |
| 37 |
| 38 void SurroundingTextTest::setHTML(const String& content) |
| 39 { |
| 40 document().body()->setInnerHTML(content, ASSERT_NO_EXCEPTION); |
| 41 } |
| 42 |
| 43 VisibleSelection SurroundingTextTest::select(int start, int end) |
| 44 { |
| 45 Element* element = document().getElementById("selection"); |
| 46 VisibleSelection selection; |
| 47 selection.setBase(Position(toText(element->firstChild()), start)); |
| 48 selection.setExtent(Position(toText(element->firstChild()), end)); |
| 49 return selection; |
| 50 } |
| 51 |
| 52 TEST_F(SurroundingTextTest, BasicCaretSelection) |
| 53 { |
| 54 setHTML(String("<p id='selection'>foo bar</p>")); |
| 55 |
| 56 { |
| 57 VisibleSelection selection = select(0); |
| 58 SurroundingText surroundingText(selection.start(), 1); |
| 59 |
| 60 EXPECT_EQ("f", surroundingText.content()); |
| 61 EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); |
| 62 } |
| 63 |
| 64 { |
| 65 VisibleSelection selection = select(0); |
| 66 SurroundingText surroundingText(selection.start(), 5); |
| 67 |
| 68 // maxlength/2 is used on the left and right. |
| 69 EXPECT_EQ("foo", surroundingText.content().simplifyWhiteSpace()); |
| 70 EXPECT_EQ(1u, surroundingText.positionOffsetInContent()); |
| 71 } |
| 72 |
| 73 { |
| 74 VisibleSelection selection = select(0); |
| 75 SurroundingText surroundingText(selection.start(), 42); |
| 76 |
| 77 EXPECT_EQ("foo bar", surroundingText.content().simplifyWhiteSpace()); |
| 78 EXPECT_EQ(1u, surroundingText.positionOffsetInContent()); |
| 79 } |
| 80 |
| 81 { |
| 82 // FIXME: if the selection is at the end of the text, SurroundingText |
| 83 // will return nothing. |
| 84 VisibleSelection selection = select(7); |
| 85 SurroundingText surroundingText(selection.start(), 42); |
| 86 |
| 87 EXPECT_EQ(0u, surroundingText.content().length()); |
| 88 EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); |
| 89 } |
| 90 |
| 91 { |
| 92 VisibleSelection selection = select(6); |
| 93 SurroundingText surroundingText(selection.start(), 2); |
| 94 |
| 95 EXPECT_EQ("ar", surroundingText.content()); |
| 96 EXPECT_EQ(1u, surroundingText.positionOffsetInContent()); |
| 97 } |
| 98 |
| 99 { |
| 100 VisibleSelection selection = select(6); |
| 101 SurroundingText surroundingText(selection.start(), 42); |
| 102 |
| 103 EXPECT_EQ("foo bar", surroundingText.content().simplifyWhiteSpace()); |
| 104 EXPECT_EQ(7u, surroundingText.positionOffsetInContent()); |
| 105 } |
| 106 } |
| 107 |
| 108 TEST_F(SurroundingTextTest, TreeCaretSelection) |
| 109 { |
| 110 setHTML(String("<div>This is outside of <p id='selection'>foo bar</p> the se
lected node</div>")); |
| 111 |
| 112 { |
| 113 VisibleSelection selection = select(0); |
| 114 SurroundingText surroundingText(selection.start(), 1); |
| 115 |
| 116 EXPECT_EQ("f", surroundingText.content()); |
| 117 EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); |
| 118 } |
| 119 |
| 120 { |
| 121 VisibleSelection selection = select(0); |
| 122 SurroundingText surroundingText(selection.start(), 5); |
| 123 |
| 124 EXPECT_EQ("foo", surroundingText.content().simplifyWhiteSpace()); |
| 125 EXPECT_EQ(1u, surroundingText.positionOffsetInContent()); |
| 126 } |
| 127 |
| 128 { |
| 129 VisibleSelection selection = select(0); |
| 130 SurroundingText surroundingText(selection.start(), 1337); |
| 131 |
| 132 EXPECT_EQ("This is outside of foo bar the selected node", surroundingTex
t.content().simplifyWhiteSpace()); |
| 133 EXPECT_EQ(20u, surroundingText.positionOffsetInContent()); |
| 134 } |
| 135 |
| 136 { |
| 137 VisibleSelection selection = select(6); |
| 138 SurroundingText surroundingText(selection.start(), 2); |
| 139 |
| 140 EXPECT_EQ("ar", surroundingText.content()); |
| 141 EXPECT_EQ(1u, surroundingText.positionOffsetInContent()); |
| 142 } |
| 143 |
| 144 { |
| 145 VisibleSelection selection = select(6); |
| 146 SurroundingText surroundingText(selection.start(), 1337); |
| 147 |
| 148 EXPECT_EQ("This is outside of foo bar the selected node", surroundingTex
t.content().simplifyWhiteSpace()); |
| 149 EXPECT_EQ(26u, surroundingText.positionOffsetInContent()); |
| 150 } |
| 151 } |
| 152 |
| 153 } // anonymous namespace |
OLD | NEW |