Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(804)

Unified Diff: Source/core/editing/SurroundingTextTest.cpp

Issue 307353002: Use Position instead of VisiblePosition for SurroundingText. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..a5b79abfaf8f7e5ff5d9b3ecb8ed8a4b196b1b00
--- /dev/null
+++ b/Source/core/editing/SurroundingTextTest.cpp
@@ -0,0 +1,153 @@
+// 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:
+ virtual void SetUp() OVERRIDE;
yosin_UTC9 2014/06/04 01:04:05 nit: We can make |SetUp()| as private.
mlamouri (slow - plz ping) 2014/06/04 09:52:12 Done.
+
+ 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:
+ 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(unsigned(0), surroundingText.positionOffsetInContent());
yosin_UTC9 2014/06/04 01:04:05 nit: 0u I couldn't find any usage of |unsinged(x)|
mlamouri (slow - plz ping) 2014/06/04 09:52:12 Done.
+ }
+
+ {
+ 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(unsigned(0), surroundingText.positionOffsetInContent());
+ }
+
+ {
+ VisibleSelection selection = select(0);
+ SurroundingText surroundingText(selection.start(), 42);
+
+ EXPECT_EQ("foo bar", surroundingText.content());
+ EXPECT_EQ(unsigned(0), 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(unsigned(0), surroundingText.content().length());
+ EXPECT_EQ(unsigned(0), surroundingText.positionOffsetInContent());
+ }
+
+ {
+ VisibleSelection selection = select(6);
+ SurroundingText surroundingText(selection.start(), 2);
+
+ EXPECT_EQ("ar", surroundingText.content());
+ EXPECT_EQ(unsigned(1), surroundingText.positionOffsetInContent());
+ }
+
+ {
+ VisibleSelection selection = select(6);
+ SurroundingText surroundingText(selection.start(), 42);
+
+ EXPECT_EQ("foo bar", surroundingText.content());
+ EXPECT_EQ(unsigned(6), 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(unsigned(0), surroundingText.positionOffsetInContent());
+ }
+
+ {
+ VisibleSelection selection = select(0);
+ SurroundingText surroundingText(selection.start(), 5);
+
+ EXPECT_EQ("foo", surroundingText.content().simplifyWhiteSpace());
+ EXPECT_EQ(unsigned(1), 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(unsigned(19), surroundingText.positionOffsetInContent());
+ }
+
+ {
+ VisibleSelection selection = select(6);
+ SurroundingText surroundingText(selection.start(), 2);
+
+ EXPECT_EQ("ar", surroundingText.content());
+ EXPECT_EQ(unsigned(1), 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(unsigned(25), surroundingText.positionOffsetInContent());
+ }
+}
+
+} // anonymous namespace

Powered by Google App Engine
This is Rietveld 408576698