Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / editing / VisibleSelectionTest.cpp
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/VisibleSelection.h"
7
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>
14
15 #define LOREM_IPSUM \
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."
22
23 namespace blink {
24
25 class VisibleSelectionTest : public ::testing::Test {
26 protected:
27     virtual void SetUp() override;
28
29     // Oilpan: wrapper object needed to be able to trace VisibleSelection.
30     class VisibleSelectionWrapper : public NoBaseWillBeGarbageCollectedFinalized<VisibleSelectionWrapper> {
31     public:
32         void trace(Visitor* visitor)
33         {
34             visitor->trace(m_selection);
35         }
36
37         VisibleSelection m_selection;
38     };
39
40     Document& document() const { return m_dummyPageHolder->document(); }
41     Text* textNode() const { return m_textNode.get(); }
42     VisibleSelection& selection() { return m_wrap->m_selection; }
43
44     // Helper function to set the VisibleSelection base/extent.
45     void setSelection(int base) { setSelection(base, base); }
46
47     // Helper function to set the VisibleSelection base/extent.
48     void setSelection(int base, int extend)
49     {
50         m_wrap->m_selection.setBase(Position(textNode(), base));
51         m_wrap->m_selection.setExtent(Position(textNode(), extend));
52     }
53
54 private:
55     OwnPtr<DummyPageHolder> m_dummyPageHolder;
56     RefPtrWillBePersistent<Text> m_textNode;
57     OwnPtrWillBePersistent<VisibleSelectionWrapper> m_wrap;
58 };
59
60 void blink::VisibleSelectionTest::SetUp()
61 {
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());
66 }
67
68 } // namespace blink
69
70 namespace {
71
72 using namespace blink;
73
74 TEST_F(VisibleSelectionTest, Initialisation)
75 {
76     setSelection(0);
77
78     EXPECT_FALSE(selection().isNone());
79     EXPECT_TRUE(selection().isCaret());
80
81     RefPtrWillBeRawPtr<Range> range = selection().firstRange();
82     EXPECT_EQ(0, range->startOffset());
83     EXPECT_EQ(0, range->endOffset());
84     EXPECT_EQ("", range->text());
85 }
86
87 TEST_F(VisibleSelectionTest, WordGranularity)
88 {
89     // Beginning of a word.
90     {
91         setSelection(0);
92         selection().expandUsingGranularity(WordGranularity);
93
94         RefPtrWillBeRawPtr<Range> range = selection().firstRange();
95         EXPECT_EQ(0, range->startOffset());
96         EXPECT_EQ(5, range->endOffset());
97         EXPECT_EQ("Lorem", range->text());
98     }
99
100     // Middle of a word.
101     {
102         setSelection(8);
103         selection().expandUsingGranularity(WordGranularity);
104
105         RefPtrWillBeRawPtr<Range> range = selection().firstRange();
106         EXPECT_EQ(6, range->startOffset());
107         EXPECT_EQ(11, range->endOffset());
108         EXPECT_EQ("ipsum", range->text());
109     }
110
111     // End of a word.
112     // FIXME: that sounds buggy, we might want to select the word _before_ instead
113     // of the space...
114     {
115         setSelection(5);
116         selection().expandUsingGranularity(WordGranularity);
117
118         RefPtrWillBeRawPtr<Range> range = selection().firstRange();
119         EXPECT_EQ(5, range->startOffset());
120         EXPECT_EQ(6, range->endOffset());
121         EXPECT_EQ(" ", range->text());
122     }
123
124     // Before comma.
125     // FIXME: that sounds buggy, we might want to select the word _before_ instead
126     // of the comma.
127     {
128         setSelection(26);
129         selection().expandUsingGranularity(WordGranularity);
130
131         RefPtrWillBeRawPtr<Range> range = selection().firstRange();
132         EXPECT_EQ(26, range->startOffset());
133         EXPECT_EQ(27, range->endOffset());
134         EXPECT_EQ(",", range->text());
135     }
136
137     // After comma.
138     {
139         setSelection(27);
140         selection().expandUsingGranularity(WordGranularity);
141
142         RefPtrWillBeRawPtr<Range> range = selection().firstRange();
143         EXPECT_EQ(27, range->startOffset());
144         EXPECT_EQ(28, range->endOffset());
145         EXPECT_EQ(" ", range->text());
146     }
147
148     // When selecting part of a word.
149     {
150         setSelection(0, 1);
151         selection().expandUsingGranularity(WordGranularity);
152
153         RefPtrWillBeRawPtr<Range> range = selection().firstRange();
154         EXPECT_EQ(0, range->startOffset());
155         EXPECT_EQ(5, range->endOffset());
156         EXPECT_EQ("Lorem", range->text());
157     }
158
159     // When selecting part of two words.
160     {
161         setSelection(2, 8);
162         selection().expandUsingGranularity(WordGranularity);
163
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());
168     }
169 }
170
171 } // namespace blink