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

Side by Side Diff: third_party/WebKit/Source/core/dom/StaticRangeTest.cpp

Issue 2022863002: [InputEvent] Introduce |StaticRange| and use in |InputEvent::getRanges()| (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Introduce |StaticRange| and add tests Created 4 years, 6 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 "core/dom/StaticRange.h"
6
7 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
8 #include "core/dom/Element.h"
9 #include "core/dom/NodeList.h"
10 #include "core/dom/Range.h"
11 #include "core/dom/Text.h"
12 #include "core/html/HTMLBodyElement.h"
13 #include "core/html/HTMLDocument.h"
14 #include "core/html/HTMLElement.h"
15 #include "core/html/HTMLHtmlElement.h"
16 #include "platform/heap/Handle.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "wtf/Compiler.h"
19 #include "wtf/RefPtr.h"
20 #include "wtf/text/AtomicString.h"
21
22 namespace blink {
23
24 class StaticRangeTest : public testing::Test {
25 protected:
26 void SetUp() override;
27
28 HTMLDocument& document() const;
29
30 // Used to access private method |toRangeImpl()|;
31 Range* StaticRangeToRange(StaticRange*, ExceptionState&) const;
32
33 private:
34 Persistent<HTMLDocument> m_document;
35 };
36
37 void StaticRangeTest::SetUp()
38 {
39 m_document = HTMLDocument::create();
40 HTMLHtmlElement* html = HTMLHtmlElement::create(*m_document);
41 html->appendChild(HTMLBodyElement::create(*m_document));
42 m_document->appendChild(html);
43 }
44
45 HTMLDocument& StaticRangeTest::document() const
46 {
47 return *m_document;
48 }
49
50 Range* StaticRangeTest::StaticRangeToRange(StaticRange* staticRange, ExceptionSt ate& exceptionState) const
51 {
52 return staticRange->toRangeImpl(document(), exceptionState);
53 }
54
55 TEST_F(StaticRangeTest, SplitTextNodeRangeWithinText)
56 {
57 document().body()->setInnerHTML("1234", ASSERT_NO_EXCEPTION);
58 Text* oldText = toText(document().body()->firstChild());
59
60 StaticRange* staticRange04 = StaticRange::create(oldText, 0, oldText, 4);
61 StaticRange* staticRange02 = StaticRange::create(oldText, 0, oldText, 2);
62 StaticRange* staticRange22 = StaticRange::create(oldText, 2, oldText, 2);
63 StaticRange* staticRange24 = StaticRange::create(oldText, 2, oldText, 4);
64
65 Range* range04 = StaticRangeToRange(staticRange04, ASSERT_NO_EXCEPTION);
66 Range* range02 = StaticRangeToRange(staticRange02, ASSERT_NO_EXCEPTION);
67 Range* range22 = StaticRangeToRange(staticRange22, ASSERT_NO_EXCEPTION);
68 Range* range24 = StaticRangeToRange(staticRange24, ASSERT_NO_EXCEPTION);
69
70 oldText->splitText(2, ASSERT_NO_EXCEPTION);
71 Text* newText = toText(oldText->nextSibling());
72
73 // Range should mutate.
74 EXPECT_TRUE(range04->boundaryPointsValid());
75 EXPECT_EQ(oldText, range04->startContainer());
76 EXPECT_EQ(0, range04->startOffset());
77 EXPECT_EQ(newText, range04->endContainer());
78 EXPECT_EQ(2, range04->endOffset());
79
80 EXPECT_TRUE(range02->boundaryPointsValid());
81 EXPECT_EQ(oldText, range02->startContainer());
82 EXPECT_EQ(0, range02->startOffset());
83 EXPECT_EQ(oldText, range02->endContainer());
84 EXPECT_EQ(2, range02->endOffset());
85
86 // Our implementation always moves the boundary point at the separation poin t to the end of the original text node.
87 EXPECT_TRUE(range22->boundaryPointsValid());
88 EXPECT_EQ(oldText, range22->startContainer());
89 EXPECT_EQ(2, range22->startOffset());
90 EXPECT_EQ(oldText, range22->endContainer());
91 EXPECT_EQ(2, range22->endOffset());
92
93 EXPECT_TRUE(range24->boundaryPointsValid());
94 EXPECT_EQ(oldText, range24->startContainer());
95 EXPECT_EQ(2, range24->startOffset());
96 EXPECT_EQ(newText, range24->endContainer());
97 EXPECT_EQ(2, range24->endOffset());
98
99 // StaticRange shouldn't mutate.
100 EXPECT_EQ(oldText, staticRange04->startContainer());
101 EXPECT_EQ(0, staticRange04->startOffset());
102 EXPECT_EQ(oldText, staticRange04->endContainer());
103 EXPECT_EQ(4, staticRange04->endOffset());
104
105 EXPECT_EQ(oldText, staticRange02->startContainer());
106 EXPECT_EQ(0, staticRange02->startOffset());
107 EXPECT_EQ(oldText, staticRange02->endContainer());
108 EXPECT_EQ(2, staticRange02->endOffset());
109
110 EXPECT_EQ(oldText, staticRange22->startContainer());
111 EXPECT_EQ(2, staticRange22->startOffset());
112 EXPECT_EQ(oldText, staticRange22->endContainer());
113 EXPECT_EQ(2, staticRange22->endOffset());
114
115 EXPECT_EQ(oldText, staticRange24->startContainer());
116 EXPECT_EQ(2, staticRange24->startOffset());
117 EXPECT_EQ(oldText, staticRange24->endContainer());
118 EXPECT_EQ(4, staticRange24->endOffset());
119 }
120
121 TEST_F(StaticRangeTest, SplitTextNodeRangeOutsideText)
122 {
123 document().body()->setInnerHTML("<span id=\"outer\">0<span id=\"inner-left\" >1</span>SPLITME<span id=\"inner-right\">2</span>3</span>", ASSERT_NO_EXCEPTION) ;
124
125 Element* outer = document().getElementById(AtomicString::fromUTF8("outer"));
126 Element* innerLeft = document().getElementById(AtomicString::fromUTF8("inner -left"));
127 Element* innerRight = document().getElementById(AtomicString::fromUTF8("inne r-right"));
128 Text* oldText = toText(outer->childNodes()->item(2));
129
130 StaticRange* staticRangeOuterOutside = StaticRange::create(outer, 0, outer, 5);
131 StaticRange* staticRangeOuterInside = StaticRange::create(outer, 1, outer, 4 );
132 StaticRange* staticRangeOuterSurroundingText = StaticRange::create(outer, 2, outer, 3);
133 StaticRange* staticRangeInnerLeft = StaticRange::create(innerLeft, 0, innerL eft, 1);
134 StaticRange* staticRangeInnerRight = StaticRange::create(innerRight, 0, inne rRight, 1);
135 StaticRange* staticRangeFromTextToMiddleOfElement = StaticRange::create(oldT ext, 6, outer, 3);
136
137 Range* rangeOuterOutside = StaticRangeToRange(staticRangeOuterOutside, ASSER T_NO_EXCEPTION);
138 Range* rangeOuterInside = StaticRangeToRange(staticRangeOuterInside, ASSERT_ NO_EXCEPTION);
139 Range* rangeOuterSurroundingText = StaticRangeToRange(staticRangeOuterSurrou ndingText, ASSERT_NO_EXCEPTION);
140 Range* rangeInnerLeft = StaticRangeToRange(staticRangeInnerLeft, ASSERT_NO_E XCEPTION);
141 Range* rangeInnerRight = StaticRangeToRange(staticRangeInnerRight, ASSERT_NO _EXCEPTION);
142 Range* rangeFromTextToMiddleOfElement = StaticRangeToRange(staticRangeFromTe xtToMiddleOfElement, ASSERT_NO_EXCEPTION);
143
144 oldText->splitText(3, ASSERT_NO_EXCEPTION);
145 Text* newText = toText(oldText->nextSibling());
146
147 // Range should mutate.
148 EXPECT_TRUE(rangeOuterOutside->boundaryPointsValid());
149 EXPECT_EQ(outer, rangeOuterOutside->startContainer());
150 EXPECT_EQ(0, rangeOuterOutside->startOffset());
151 EXPECT_EQ(outer, rangeOuterOutside->endContainer());
152 EXPECT_EQ(6, rangeOuterOutside->endOffset()); // Increased by 1 since a new node is inserted.
153
154 EXPECT_TRUE(rangeOuterInside->boundaryPointsValid());
155 EXPECT_EQ(outer, rangeOuterInside->startContainer());
156 EXPECT_EQ(1, rangeOuterInside->startOffset());
157 EXPECT_EQ(outer, rangeOuterInside->endContainer());
158 EXPECT_EQ(5, rangeOuterInside->endOffset());
159
160 EXPECT_TRUE(rangeOuterSurroundingText->boundaryPointsValid());
161 EXPECT_EQ(outer, rangeOuterSurroundingText->startContainer());
162 EXPECT_EQ(2, rangeOuterSurroundingText->startOffset());
163 EXPECT_EQ(outer, rangeOuterSurroundingText->endContainer());
164 EXPECT_EQ(4, rangeOuterSurroundingText->endOffset());
165
166 EXPECT_TRUE(rangeInnerLeft->boundaryPointsValid());
167 EXPECT_EQ(innerLeft, rangeInnerLeft->startContainer());
168 EXPECT_EQ(0, rangeInnerLeft->startOffset());
169 EXPECT_EQ(innerLeft, rangeInnerLeft->endContainer());
170 EXPECT_EQ(1, rangeInnerLeft->endOffset());
171
172 EXPECT_TRUE(rangeInnerRight->boundaryPointsValid());
173 EXPECT_EQ(innerRight, rangeInnerRight->startContainer());
174 EXPECT_EQ(0, rangeInnerRight->startOffset());
175 EXPECT_EQ(innerRight, rangeInnerRight->endContainer());
176 EXPECT_EQ(1, rangeInnerRight->endOffset());
177
178 EXPECT_TRUE(rangeFromTextToMiddleOfElement->boundaryPointsValid());
179 EXPECT_EQ(newText, rangeFromTextToMiddleOfElement->startContainer());
180 EXPECT_EQ(3, rangeFromTextToMiddleOfElement->startOffset());
181 EXPECT_EQ(outer, rangeFromTextToMiddleOfElement->endContainer());
182 EXPECT_EQ(4, rangeFromTextToMiddleOfElement->endOffset());
183
184 // StaticRange shouldn't mutate.
185 EXPECT_EQ(outer, staticRangeOuterOutside->startContainer());
186 EXPECT_EQ(0, staticRangeOuterOutside->startOffset());
187 EXPECT_EQ(outer, staticRangeOuterOutside->endContainer());
188 EXPECT_EQ(5, staticRangeOuterOutside->endOffset());
189
190 EXPECT_EQ(outer, staticRangeOuterInside->startContainer());
191 EXPECT_EQ(1, staticRangeOuterInside->startOffset());
192 EXPECT_EQ(outer, staticRangeOuterInside->endContainer());
193 EXPECT_EQ(4, staticRangeOuterInside->endOffset());
194
195 EXPECT_EQ(outer, staticRangeOuterSurroundingText->startContainer());
196 EXPECT_EQ(2, staticRangeOuterSurroundingText->startOffset());
197 EXPECT_EQ(outer, staticRangeOuterSurroundingText->endContainer());
198 EXPECT_EQ(3, staticRangeOuterSurroundingText->endOffset());
199
200 EXPECT_EQ(innerLeft, staticRangeInnerLeft->startContainer());
201 EXPECT_EQ(0, staticRangeInnerLeft->startOffset());
202 EXPECT_EQ(innerLeft, staticRangeInnerLeft->endContainer());
203 EXPECT_EQ(1, staticRangeInnerLeft->endOffset());
204
205 EXPECT_EQ(innerRight, staticRangeInnerRight->startContainer());
206 EXPECT_EQ(0, staticRangeInnerRight->startOffset());
207 EXPECT_EQ(innerRight, staticRangeInnerRight->endContainer());
208 EXPECT_EQ(1, staticRangeInnerRight->endOffset());
209
210 EXPECT_EQ(oldText, staticRangeFromTextToMiddleOfElement->startContainer());
211 EXPECT_EQ(6, staticRangeFromTextToMiddleOfElement->startOffset());
212 EXPECT_EQ(outer, staticRangeFromTextToMiddleOfElement->endContainer());
213 EXPECT_EQ(3, staticRangeFromTextToMiddleOfElement->endOffset());
214 }
215
216 TEST_F(StaticRangeTest, InvalidToRange)
217 {
218 document().body()->setInnerHTML("1234", ASSERT_NO_EXCEPTION);
219 Text* oldText = toText(document().body()->firstChild());
220
221 StaticRange* staticRange04 = StaticRange::create(oldText, 0, oldText, 4);
222
223 // Valid StaticRange.
224 StaticRangeToRange(staticRange04, ASSERT_NO_EXCEPTION);
225
226 oldText->splitText(2, ASSERT_NO_EXCEPTION);
227 // StaticRange shouldn't mutate, endOffset() become invalid after splitText( ).
228 EXPECT_EQ(oldText, staticRange04->startContainer());
229 EXPECT_EQ(0, staticRange04->startOffset());
230 EXPECT_EQ(oldText, staticRange04->endContainer());
231 EXPECT_EQ(4, staticRange04->endOffset());
232
233 // Invalid StaticRange.
234 TrackExceptionState exceptionState;
235 StaticRangeToRange(staticRange04, exceptionState);
236 EXPECT_TRUE(exceptionState.hadException());
237 }
238
239 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698