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.
6 #include "core/editing/VisibleSelection.h"
8 #include "core/dom/Document.h"
9 #include "core/dom/Range.h"
10 #include "core/dom/Text.h"
11 #include "core/html/HTMLElement.h"
12 #include "core/testing/DummyPageHolder.h"
13 #include <gtest/gtest.h>
16 "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor " \
17 "incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud " \
18 "exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure " \
19 "dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur." \
20 "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt " \
21 "mollit anim id est laborum."
25 class VisibleSelectionTest : public ::testing::Test {
27 virtual void SetUp() override;
29 // Oilpan: wrapper object needed to be able to trace VisibleSelection.
30 class VisibleSelectionWrapper : public NoBaseWillBeGarbageCollectedFinalized<VisibleSelectionWrapper> {
32 void trace(Visitor* visitor)
34 visitor->trace(m_selection);
37 VisibleSelection m_selection;
40 Document& document() const { return m_dummyPageHolder->document(); }
41 Text* textNode() const { return m_textNode.get(); }
42 VisibleSelection& selection() { return m_wrap->m_selection; }
44 // Helper function to set the VisibleSelection base/extent.
45 void setSelection(int base) { setSelection(base, base); }
47 // Helper function to set the VisibleSelection base/extent.
48 void setSelection(int base, int extend)
50 m_wrap->m_selection.setBase(Position(textNode(), base));
51 m_wrap->m_selection.setExtent(Position(textNode(), extend));
55 OwnPtr<DummyPageHolder> m_dummyPageHolder;
56 RefPtrWillBePersistent<Text> m_textNode;
57 OwnPtrWillBePersistent<VisibleSelectionWrapper> m_wrap;
60 void blink::VisibleSelectionTest::SetUp()
62 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600));
63 m_textNode = document().createTextNode(LOREM_IPSUM);
64 m_wrap = adoptPtrWillBeNoop(new VisibleSelectionWrapper());
65 document().body()->appendChild(m_textNode.get());
72 using namespace blink;
74 TEST_F(VisibleSelectionTest, Initialisation)
78 EXPECT_FALSE(selection().isNone());
79 EXPECT_TRUE(selection().isCaret());
81 RefPtrWillBeRawPtr<Range> range = selection().firstRange();
82 EXPECT_EQ(0, range->startOffset());
83 EXPECT_EQ(0, range->endOffset());
84 EXPECT_EQ("", range->text());
87 TEST_F(VisibleSelectionTest, WordGranularity)
89 // Beginning of a word.
92 selection().expandUsingGranularity(WordGranularity);
94 RefPtrWillBeRawPtr<Range> range = selection().firstRange();
95 EXPECT_EQ(0, range->startOffset());
96 EXPECT_EQ(5, range->endOffset());
97 EXPECT_EQ("Lorem", range->text());
103 selection().expandUsingGranularity(WordGranularity);
105 RefPtrWillBeRawPtr<Range> range = selection().firstRange();
106 EXPECT_EQ(6, range->startOffset());
107 EXPECT_EQ(11, range->endOffset());
108 EXPECT_EQ("ipsum", range->text());
112 // FIXME: that sounds buggy, we might want to select the word _before_ instead
116 selection().expandUsingGranularity(WordGranularity);
118 RefPtrWillBeRawPtr<Range> range = selection().firstRange();
119 EXPECT_EQ(5, range->startOffset());
120 EXPECT_EQ(6, range->endOffset());
121 EXPECT_EQ(" ", range->text());
125 // FIXME: that sounds buggy, we might want to select the word _before_ instead
129 selection().expandUsingGranularity(WordGranularity);
131 RefPtrWillBeRawPtr<Range> range = selection().firstRange();
132 EXPECT_EQ(26, range->startOffset());
133 EXPECT_EQ(27, range->endOffset());
134 EXPECT_EQ(",", range->text());
140 selection().expandUsingGranularity(WordGranularity);
142 RefPtrWillBeRawPtr<Range> range = selection().firstRange();
143 EXPECT_EQ(27, range->startOffset());
144 EXPECT_EQ(28, range->endOffset());
145 EXPECT_EQ(" ", range->text());
148 // When selecting part of a word.
151 selection().expandUsingGranularity(WordGranularity);
153 RefPtrWillBeRawPtr<Range> range = selection().firstRange();
154 EXPECT_EQ(0, range->startOffset());
155 EXPECT_EQ(5, range->endOffset());
156 EXPECT_EQ("Lorem", range->text());
159 // When selecting part of two words.
162 selection().expandUsingGranularity(WordGranularity);
164 RefPtrWillBeRawPtr<Range> range = selection().firstRange();
165 EXPECT_EQ(0, range->startOffset());
166 EXPECT_EQ(11, range->endOffset());
167 EXPECT_EQ("Lorem ipsum", range->text());