Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / core / editing / SurroundingTextTest.cpp
blob052cdfc4a7a09456880bab79e4ecc5e0f7ae0233
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.
5 #include "config.h"
6 #include "core/editing/SurroundingText.h"
8 #include "core/dom/Document.h"
9 #include "core/dom/Range.h"
10 #include "core/dom/Text.h"
11 #include "core/editing/Position.h"
12 #include "core/editing/VisibleSelection.h"
13 #include "core/html/HTMLElement.h"
14 #include "core/testing/DummyPageHolder.h"
15 #include <gtest/gtest.h>
17 namespace blink {
19 class SurroundingTextTest : public ::testing::Test {
20 protected:
21 Document& document() const { return m_dummyPageHolder->document(); }
22 void setHTML(const String&);
23 VisibleSelection select(int offset) { return select(offset, offset); }
24 VisibleSelection select(int start, int end);
26 private:
27 void SetUp() override;
29 OwnPtr<DummyPageHolder> m_dummyPageHolder;
32 void SurroundingTextTest::SetUp()
34 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600));
37 void SurroundingTextTest::setHTML(const String& content)
39 document().body()->setInnerHTML(content, ASSERT_NO_EXCEPTION);
42 VisibleSelection SurroundingTextTest::select(int start, int end)
44 Element* element = document().getElementById("selection");
45 VisibleSelection selection;
46 selection.setBase(Position(toText(element->firstChild()), start));
47 selection.setExtent(Position(toText(element->firstChild()), end));
48 return selection;
51 TEST_F(SurroundingTextTest, BasicCaretSelection)
53 setHTML(String("<p id='selection'>foo bar</p>"));
56 VisibleSelection selection = select(0);
57 SurroundingText surroundingText(selection.start(), 1);
59 EXPECT_EQ("f", surroundingText.content());
60 EXPECT_EQ(0u, surroundingText.startOffsetInContent());
61 EXPECT_EQ(0u, surroundingText.endOffsetInContent());
65 VisibleSelection selection = select(0);
66 SurroundingText surroundingText(selection.start(), 5);
68 // maxlength/2 is used on the left and right.
69 EXPECT_EQ("foo", surroundingText.content().simplifyWhiteSpace());
70 EXPECT_EQ(1u, surroundingText.startOffsetInContent());
71 EXPECT_EQ(1u, surroundingText.endOffsetInContent());
75 VisibleSelection selection = select(0);
76 SurroundingText surroundingText(selection.start(), 42);
78 EXPECT_EQ("foo bar", surroundingText.content().simplifyWhiteSpace());
79 EXPECT_EQ(1u, surroundingText.startOffsetInContent());
80 EXPECT_EQ(1u, surroundingText.endOffsetInContent());
84 // FIXME: if the selection is at the end of the text, SurroundingText
85 // will return nothing.
86 VisibleSelection selection = select(7);
87 SurroundingText surroundingText(selection.start(), 42);
89 EXPECT_EQ(0u, surroundingText.content().length());
90 EXPECT_EQ(0u, surroundingText.startOffsetInContent());
91 EXPECT_EQ(0u, surroundingText.endOffsetInContent());
95 VisibleSelection selection = select(6);
96 SurroundingText surroundingText(selection.start(), 2);
98 EXPECT_EQ("ar", surroundingText.content());
99 EXPECT_EQ(1u, surroundingText.startOffsetInContent());
100 EXPECT_EQ(1u, surroundingText.endOffsetInContent());
104 VisibleSelection selection = select(6);
105 SurroundingText surroundingText(selection.start(), 42);
107 EXPECT_EQ("foo bar", surroundingText.content().simplifyWhiteSpace());
108 EXPECT_EQ(7u, surroundingText.startOffsetInContent());
109 EXPECT_EQ(7u, surroundingText.endOffsetInContent());
113 TEST_F(SurroundingTextTest, BasicRangeSelection)
115 setHTML(String("<p id='selection'>Lorem ipsum dolor sit amet</p>"));
118 VisibleSelection selection = select(0, 5);
119 SurroundingText surroundingText(*firstRangeOf(selection), 1);
121 EXPECT_EQ("Lorem ", surroundingText.content());
122 EXPECT_EQ(0u, surroundingText.startOffsetInContent());
123 EXPECT_EQ(5u, surroundingText.endOffsetInContent());
127 VisibleSelection selection = select(0, 5);
128 SurroundingText surroundingText(*firstRangeOf(selection), 5);
130 EXPECT_EQ("Lorem ip", surroundingText.content().simplifyWhiteSpace());
131 EXPECT_EQ(1u, surroundingText.startOffsetInContent());
132 EXPECT_EQ(6u, surroundingText.endOffsetInContent());
136 VisibleSelection selection = select(0, 5);
137 SurroundingText surroundingText(*firstRangeOf(selection), 42);
139 EXPECT_EQ("Lorem ipsum dolor sit amet", surroundingText.content().simplifyWhiteSpace());
140 EXPECT_EQ(1u, surroundingText.startOffsetInContent());
141 EXPECT_EQ(6u, surroundingText.endOffsetInContent());
145 VisibleSelection selection = select(6, 11);
146 SurroundingText surroundingText(*firstRangeOf(selection), 2);
148 EXPECT_EQ(" ipsum ", surroundingText.content());
149 EXPECT_EQ(1u, surroundingText.startOffsetInContent());
150 EXPECT_EQ(6u, surroundingText.endOffsetInContent());
154 VisibleSelection selection = select(6, 11);
155 SurroundingText surroundingText(*firstRangeOf(selection), 42);
157 EXPECT_EQ("Lorem ipsum dolor sit amet", surroundingText.content().simplifyWhiteSpace());
158 EXPECT_EQ(7u, surroundingText.startOffsetInContent());
159 EXPECT_EQ(12u, surroundingText.endOffsetInContent());
163 TEST_F(SurroundingTextTest, TreeCaretSelection)
165 setHTML(String("<div>This is outside of <p id='selection'>foo bar</p> the selected node</div>"));
168 VisibleSelection selection = select(0);
169 SurroundingText surroundingText(selection.start(), 1);
171 EXPECT_EQ("f", surroundingText.content());
172 EXPECT_EQ(0u, surroundingText.startOffsetInContent());
173 EXPECT_EQ(0u, surroundingText.endOffsetInContent());
177 VisibleSelection selection = select(0);
178 SurroundingText surroundingText(selection.start(), 5);
180 EXPECT_EQ("foo", surroundingText.content().simplifyWhiteSpace());
181 EXPECT_EQ(1u, surroundingText.startOffsetInContent());
182 EXPECT_EQ(1u, surroundingText.endOffsetInContent());
186 VisibleSelection selection = select(0);
187 SurroundingText surroundingText(selection.start(), 1337);
189 EXPECT_EQ("This is outside of foo bar the selected node", surroundingText.content().simplifyWhiteSpace());
190 EXPECT_EQ(20u, surroundingText.startOffsetInContent());
191 EXPECT_EQ(20u, surroundingText.endOffsetInContent());
195 VisibleSelection selection = select(6);
196 SurroundingText surroundingText(selection.start(), 2);
198 EXPECT_EQ("ar", surroundingText.content());
199 EXPECT_EQ(1u, surroundingText.startOffsetInContent());
200 EXPECT_EQ(1u, surroundingText.endOffsetInContent());
204 VisibleSelection selection = select(6);
205 SurroundingText surroundingText(selection.start(), 1337);
207 EXPECT_EQ("This is outside of foo bar the selected node", surroundingText.content().simplifyWhiteSpace());
208 EXPECT_EQ(26u, surroundingText.startOffsetInContent());
209 EXPECT_EQ(26u, surroundingText.endOffsetInContent());
213 TEST_F(SurroundingTextTest, TreeRangeSelection)
215 setHTML(String("<div>This is outside of <p id='selection'>foo bar</p> the selected node</div>"));
218 VisibleSelection selection = select(0, 1);
219 SurroundingText surroundingText(*firstRangeOf(selection), 1);
221 EXPECT_EQ("fo", surroundingText.content().simplifyWhiteSpace());
222 EXPECT_EQ(0u, surroundingText.startOffsetInContent());
223 EXPECT_EQ(1u, surroundingText.endOffsetInContent());
227 VisibleSelection selection = select(0, 3);
228 SurroundingText surroundingText(*firstRangeOf(selection), 12);
230 EXPECT_EQ("e of foo bar", surroundingText.content().simplifyWhiteSpace());
231 EXPECT_EQ(5u, surroundingText.startOffsetInContent());
232 EXPECT_EQ(8u, surroundingText.endOffsetInContent());
236 VisibleSelection selection = select(0, 3);
237 SurroundingText surroundingText(*firstRangeOf(selection), 1337);
239 EXPECT_EQ("This is outside of foo bar the selected node", surroundingText.content().simplifyWhiteSpace());
240 EXPECT_EQ(20u, surroundingText.startOffsetInContent());
241 EXPECT_EQ(23u, surroundingText.endOffsetInContent());
245 VisibleSelection selection = select(4, 7);
246 SurroundingText surroundingText(*firstRangeOf(selection), 12);
248 EXPECT_EQ("foo bar the se", surroundingText.content().simplifyWhiteSpace());
249 EXPECT_EQ(5u, surroundingText.startOffsetInContent());
250 EXPECT_EQ(8u, surroundingText.endOffsetInContent());
254 VisibleSelection selection = select(0, 7);
255 SurroundingText surroundingText(*firstRangeOf(selection), 1337);
257 EXPECT_EQ("This is outside of foo bar the selected node", surroundingText.content().simplifyWhiteSpace());
258 EXPECT_EQ(20u, surroundingText.startOffsetInContent());
259 EXPECT_EQ(27u, surroundingText.endOffsetInContent());
263 } // namespace blink