Index: Source/core/editing/SurroundingTextTest.cpp |
diff --git a/Source/core/editing/SurroundingTextTest.cpp b/Source/core/editing/SurroundingTextTest.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..91f160ba982e9a203f7f57ddab11e891aec7b6b5 |
--- /dev/null |
+++ b/Source/core/editing/SurroundingTextTest.cpp |
@@ -0,0 +1,279 @@ |
+// Copyright 2014 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 "config.h" |
+#include "core/editing/SurroundingText.h" |
+ |
+#include "core/dom/Position.h" |
+#include "core/dom/Range.h" |
+#include "core/dom/Text.h" |
+#include "core/editing/VisibleSelection.h" |
+#include "core/html/HTMLElement.h" |
+#include "core/testing/DummyPageHolder.h" |
+#include <gtest/gtest.h> |
+ |
+using namespace WebCore; |
+ |
+namespace { |
+ |
+class SurroundingTextTest : public ::testing::Test { |
+protected: |
+ Document& document() const { return m_dummyPageHolder->document(); } |
+ void setHTML(const String&); |
+ VisibleSelection select(int offset) { return select(offset, offset); } |
+ VisibleSelection select(int start, int end); |
+ |
+private: |
+ virtual void SetUp() OVERRIDE; |
+ |
+ OwnPtr<DummyPageHolder> m_dummyPageHolder; |
+}; |
+ |
+void SurroundingTextTest::SetUp() |
+{ |
+ m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600)); |
+} |
+ |
+void SurroundingTextTest::setHTML(const String& content) |
+{ |
+ document().body()->setInnerHTML(content, ASSERT_NO_EXCEPTION); |
+} |
+ |
+VisibleSelection SurroundingTextTest::select(int start, int end) |
+{ |
+ Element* element = document().getElementById("selection"); |
+ VisibleSelection selection; |
+ selection.setBase(Position(toText(element->firstChild()), start)); |
+ selection.setExtent(Position(toText(element->firstChild()), end)); |
+ return selection; |
+} |
+ |
+TEST_F(SurroundingTextTest, BasicCaretSelection) |
+{ |
+ setHTML(String("<p id='selection'>foo bar</p>")); |
+ |
+ { |
+ VisibleSelection selection = select(0); |
+ SurroundingText surroundingText(selection.start(), 1); |
+ |
+ EXPECT_EQ("f", surroundingText.content()); |
+ EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); |
+ } |
+ |
+ { |
+ VisibleSelection selection = select(0); |
+ SurroundingText surroundingText(selection.start(), 5); |
+ |
+ // maxlength/2 is used on the left and right. |
+ EXPECT_EQ("foo", surroundingText.content()); |
+ EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); |
+ } |
+ |
+ { |
+ VisibleSelection selection = select(0); |
+ SurroundingText surroundingText(selection.start(), 42); |
+ |
+ EXPECT_EQ("foo bar", surroundingText.content()); |
+ EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); |
+ } |
+ |
+ { |
+ // FIXME: if the selection is at the end of the text, SurroundingText |
+ // will return nothing. |
+ VisibleSelection selection = select(7); |
+ SurroundingText surroundingText(selection.start(), 42); |
+ |
+ EXPECT_EQ(0u, surroundingText.content().length()); |
+ EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); |
+ } |
+ |
+ { |
+ VisibleSelection selection = select(6); |
+ SurroundingText surroundingText(selection.start(), 2); |
+ |
+ EXPECT_EQ("ar", surroundingText.content()); |
+ EXPECT_EQ(1u, surroundingText.positionOffsetInContent()); |
+ } |
+ |
+ { |
+ VisibleSelection selection = select(6); |
+ SurroundingText surroundingText(selection.start(), 42); |
+ |
+ EXPECT_EQ("foo bar", surroundingText.content()); |
+ EXPECT_EQ(6u, surroundingText.positionOffsetInContent()); |
+ } |
+} |
+ |
+TEST_F(SurroundingTextTest, TreeCaretSelection) |
+{ |
+ setHTML(String("<div>This is outside of <p id='selection'>foo bar</p> the selected node</div>")); |
+ |
+ { |
+ VisibleSelection selection = select(0); |
+ SurroundingText surroundingText(selection.start(), 1); |
+ |
+ EXPECT_EQ("f", surroundingText.content()); |
+ EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); |
+ } |
+ |
+ { |
+ VisibleSelection selection = select(0); |
+ SurroundingText surroundingText(selection.start(), 5); |
+ |
+ EXPECT_EQ("foo", surroundingText.content().simplifyWhiteSpace()); |
+ EXPECT_EQ(1u, surroundingText.positionOffsetInContent()); |
+ } |
+ |
+ { |
+ VisibleSelection selection = select(0); |
+ SurroundingText surroundingText(selection.start(), 1337); |
+ |
+ EXPECT_EQ("This is outside of foo bar the selected node", surroundingText.content().simplifyWhiteSpace()); |
+ EXPECT_EQ(19u, surroundingText.positionOffsetInContent()); |
+ } |
+ |
+ { |
+ VisibleSelection selection = select(6); |
+ SurroundingText surroundingText(selection.start(), 2); |
+ |
+ EXPECT_EQ("ar", surroundingText.content()); |
+ EXPECT_EQ(1u, surroundingText.positionOffsetInContent()); |
+ } |
+ |
+ { |
+ VisibleSelection selection = select(6); |
+ SurroundingText surroundingText(selection.start(), 1337); |
+ |
+ EXPECT_EQ("This is outside of foo bar the selected node", surroundingText.content().simplifyWhiteSpace()); |
+ EXPECT_EQ(25u, surroundingText.positionOffsetInContent()); |
+ } |
+} |
+ |
+TEST_F(SurroundingTextTest, ComplexHTMLStructuresCaret) |
+{ |
+ // Those tests are imported from what was previously layout tests. |
+ { |
+ setHTML(String("<button>.</button>12345<p id='selection'>6789 12345</p>6789<button>.</button>")); |
+ |
+ VisibleSelection selection = select(0); |
+ SurroundingText surroundingText(selection.start(), 100); |
+ |
+ EXPECT_EQ("12345 6789 12345 6789", surroundingText.content().simplifyWhiteSpace()); |
+ EXPECT_EQ(6u, surroundingText.positionOffsetInContent()); |
+ } |
+ |
+ { |
+ setHTML(String("<button>.</button>12345<p id='selection'>6789 12345</p>6789<button>.</button>")); |
+ |
+ VisibleSelection selection = select(5); |
+ SurroundingText surroundingText(selection.start(), 6); |
+ |
+ EXPECT_EQ("89 123", surroundingText.content()); |
+ EXPECT_EQ(3u, surroundingText.positionOffsetInContent()); |
+ } |
+ |
+ { |
+ setHTML(String("<button>.</button>12345<p id='selection'>6789 12345</p>6789<button>.</button>")); |
+ |
+ VisibleSelection selection = select(5); |
+ SurroundingText surroundingText(selection.start(), 0); |
+ |
+ EXPECT_TRUE(surroundingText.content().isEmpty()); |
+ EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); |
+ } |
+ |
+ { |
+ setHTML(String("<button>.</button>12345<p id='selection'>6789 12345</p>6789<button>.</button>")); |
+ |
+ VisibleSelection selection = select(5); |
+ SurroundingText surroundingText(selection.start(), 1); |
+ |
+ EXPECT_EQ("1", surroundingText.content()); |
+ EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); |
+ } |
+ |
+ { |
+ setHTML(String("<button>.</button>12345<p id='selection'>6789 12345</p>6789<button>.</button>")); |
+ |
+ VisibleSelection selection = select(6); |
+ SurroundingText surroundingText(selection.start(), 2); |
+ |
+ EXPECT_EQ("12", surroundingText.content()); |
+ EXPECT_EQ(1u, surroundingText.positionOffsetInContent()); |
+ } |
+ |
+ { |
+ setHTML(String("<select>.</select><div>57th Street and Lake Shore Drive</div> <span>Chicago</span> <span id='selection'>IL</span> <span>60637</span><select>.</select>")); |
+ |
+ VisibleSelection selection = select(0); |
+ SurroundingText surroundingText(selection.start(), 100); |
+ |
+ EXPECT_EQ("57th Street and Lake Shore Drive Chicago IL 60637", surroundingText.content().simplifyWhiteSpace()); |
+ EXPECT_EQ(42u, surroundingText.positionOffsetInContent()); |
+ } |
+ |
+ { |
+ setHTML(String("<fieldset>.</fieldset>12345<button>abc</button><p id='selection'>6789<br>12345</p>6789<textarea>abc</textarea>0123<fieldset>.</fieldset>")); |
+ |
+ VisibleSelection selection = select(5); |
+ SurroundingText surroundingText(selection.end(), 100); |
+ |
+ EXPECT_EQ("6789 12345 6789", surroundingText.content().simplifyWhiteSpace()); |
+ EXPECT_EQ(5u, surroundingText.positionOffsetInContent()); |
+ } |
+ |
+ { |
+ setHTML(String("<button>.</button><div id='selection'>This is <!-- comment --!>a test <script language='javascript'></script>example<button>.</button>")); |
+ |
+ VisibleSelection selection = select(0); |
+ SurroundingText surroundingText(selection.start(), 100); |
+ |
+ EXPECT_EQ("This is a test example", surroundingText.content().simplifyWhiteSpace()); |
+ EXPECT_EQ(1u, surroundingText.positionOffsetInContent()); |
+ } |
+ |
+ { |
+ setHTML(String("<button>.</button><div id='selection'>012345678901234567890123456789</div><button>.</button>")); |
+ |
+ VisibleSelection selection = select(15); |
+ SurroundingText surroundingText(selection.start(), 12); |
+ |
+ EXPECT_EQ("901234567890", surroundingText.content().simplifyWhiteSpace()); |
+ EXPECT_EQ(6u, surroundingText.positionOffsetInContent()); |
+ } |
+ |
+ { |
+ setHTML(String("<option>.</option>12345<button id='selection'>test</button><option>.</option>")); |
+ |
+ VisibleSelection selection = select(0); |
+ SurroundingText surroundingText(selection.start(), 100); |
+ |
+ EXPECT_TRUE(surroundingText.content().isEmpty()); |
+ EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); |
+ } |
+ |
+ { |
+ setHTML(String("<option>.</option>12345<button>te<span id='selection'>st</span></button><option>.</option>")); |
+ |
+ VisibleSelection selection = select(0); |
+ SurroundingText surroundingText(selection.start(), 100); |
+ |
+ EXPECT_TRUE(surroundingText.content().isEmpty()); |
+ EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); |
+ |
+ } |
+ |
+ { |
+ setHTML(String("<p id='selection'>.</p>")); |
+ |
+ VisibleSelection selection = select(0); |
+ SurroundingText surroundingText(selection.start(), 2); |
+ |
+ EXPECT_EQ(".", surroundingText.content()); |
+ EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); |
+ } |
+} |
+ |
+ |
+} // anonymous namespace |