OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/editing/EphemeralRange.h" | 5 #include "core/editing/EphemeralRange.h" |
6 | 6 |
7 #include "core/dom/Range.h" | 7 #include "core/dom/Range.h" |
8 #include "core/editing/EditingTestBase.h" | 8 #include "core/editing/EditingTestBase.h" |
9 #include <sstream> | 9 #include <sstream> |
10 | 10 |
11 namespace blink { | 11 namespace blink { |
12 | 12 |
13 class EphemeralRangeTest : public EditingTestBase { | 13 class EphemeralRangeTest : public EditingTestBase { |
14 protected: | 14 protected: |
15 template <typename Traversal = NodeTraversal> | 15 template <typename Traversal = NodeTraversal> |
16 std::string traverseRange(Range*) const; | 16 std::string traverseRange(Range*) const; |
17 | 17 |
18 template <typename Strategy> | 18 template <typename Strategy> |
19 std::string traverseRange(const EphemeralRangeTemplate<Strategy>&) const; | 19 std::string traverseRange(const EphemeralRangeTemplate<Strategy>&) const; |
20 | 20 |
21 template <typename Strategy> | |
22 std::string commonAncesstorNode( | |
23 const EphemeralRangeTemplate<Strategy>&) const; | |
24 | |
21 Range* getBodyRange() const; | 25 Range* getBodyRange() const; |
22 }; | 26 }; |
23 | 27 |
24 template <typename Traversal> | 28 template <typename Traversal> |
25 std::string EphemeralRangeTest::traverseRange(Range* range) const { | 29 std::string EphemeralRangeTest::traverseRange(Range* range) const { |
26 std::stringstream nodesContent; | 30 std::stringstream nodesContent; |
27 for (Node* node = range->firstNode(); node != range->pastLastNode(); | 31 for (Node* node = range->firstNode(); node != range->pastLastNode(); |
28 node = Traversal::next(*node)) { | 32 node = Traversal::next(*node)) { |
29 nodesContent << "[" << *node << "]"; | 33 nodesContent << "[" << *node << "]"; |
30 } | 34 } |
(...skipping 10 matching lines...) Expand all Loading... | |
41 | 45 |
42 return nodesContent.str(); | 46 return nodesContent.str(); |
43 } | 47 } |
44 | 48 |
45 Range* EphemeralRangeTest::getBodyRange() const { | 49 Range* EphemeralRangeTest::getBodyRange() const { |
46 Range* range = Range::create(document()); | 50 Range* range = Range::create(document()); |
47 range->selectNode(document().body()); | 51 range->selectNode(document().body()); |
48 return range; | 52 return range; |
49 } | 53 } |
50 | 54 |
55 template <typename Strategy> | |
56 std::string EphemeralRangeTest::commonAncesstorNode( | |
57 const EphemeralRangeTemplate<Strategy>& range) const { | |
58 std::stringstream nodesContent; | |
59 | |
60 nodesContent << "[" << *range.commonAncestorContainer() << "]"; | |
61 | |
62 return nodesContent.str(); | |
63 } | |
64 | |
51 // Tests that |EphemeralRange::nodes()| will traverse the whole range exactly as | 65 // Tests that |EphemeralRange::nodes()| will traverse the whole range exactly as |
52 // |for (Node* n = firstNode(); n != pastLastNode(); n = Traversal::next(*n))| | 66 // |for (Node* n = firstNode(); n != pastLastNode(); n = Traversal::next(*n))| |
53 // does. | 67 // does. |
54 TEST_F(EphemeralRangeTest, rangeTraversal) { | 68 TEST_F(EphemeralRangeTest, rangeTraversal) { |
55 const char* bodyContent = | 69 const char* bodyContent = |
56 "<p id='host'><b id='one'></b><b id='two'>22</b></p>"; | 70 "<p id='host'><b id='one'></b><b id='two'>22</b></p>"; |
57 setBodyContent(bodyContent); | 71 setBodyContent(bodyContent); |
58 | 72 |
59 const std::string expectedNodes( | 73 const std::string expectedNodes( |
60 "[BODY][P id=\"host\"][B id=\"one\"][B id=\"two\"][#text \"22\"]"); | 74 "[BODY][P id=\"host\"][B id=\"one\"][B id=\"two\"][#text \"22\"]"); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 // Tree iterators have only |operator !=| ATM. | 155 // Tree iterators have only |operator !=| ATM. |
142 EXPECT_FALSE(iterable.begin() != iterable.end()); | 156 EXPECT_FALSE(iterable.begin() != iterable.end()); |
143 | 157 |
144 const EphemeralRange singlePositionRange(getBodyRange()->startPosition()); | 158 const EphemeralRange singlePositionRange(getBodyRange()->startPosition()); |
145 EXPECT_FALSE(singlePositionRange.isNull()); | 159 EXPECT_FALSE(singlePositionRange.isNull()); |
146 EXPECT_EQ(std::string(), traverseRange(singlePositionRange)); | 160 EXPECT_EQ(std::string(), traverseRange(singlePositionRange)); |
147 EXPECT_EQ(singlePositionRange.startPosition().nodeAsRangeFirstNode(), | 161 EXPECT_EQ(singlePositionRange.startPosition().nodeAsRangeFirstNode(), |
148 singlePositionRange.endPosition().nodeAsRangePastLastNode()); | 162 singlePositionRange.endPosition().nodeAsRangePastLastNode()); |
149 } | 163 } |
150 | 164 |
165 TEST_F(EphemeralRangeTest, commonAncesstor) { | |
Xiaocheng
2017/03/02 20:00:16
Each test case is supposed to be minimized.
So, w
tanvir
2017/03/04 13:55:31
Separate test cases for DOM and Flat tree is added
| |
166 const char* bodyContent = | |
167 "<p id='host'><b id='one'></b><input type='text' " | |
Xiaocheng
2017/03/02 20:00:16
Hmm... I don't like the existing test cases at all
tanvir
2017/03/04 13:55:31
New HTML used.
| |
168 "value='some'><span " | |
169 "id='two'></p>"; | |
170 | |
171 setBodyContent(bodyContent); | |
172 | |
173 Range* range = getBodyRange(); | |
174 range->setStart(document().getElementById("one"), 0, | |
175 IGNORE_EXCEPTION_FOR_TESTING); | |
176 | |
177 // Range* endPos = getBodyRange(); | |
178 range->setEnd(document().getElementById("two"), 0, | |
179 IGNORE_EXCEPTION_FOR_TESTING); | |
180 | |
181 const EphemeralRange ephemeralRange(range->startPosition(), | |
182 range->endPosition()); | |
183 EXPECT_EQ("[P id=\"host\"]", commonAncesstorNode<>(ephemeralRange)); | |
Xiaocheng
2017/03/02 20:00:16
There's no need to do string comparison, as there
tanvir
2017/03/04 13:55:31
Done!!
| |
184 | |
185 // CommonAncesstorCotainer for FlatTree | |
186 EXPECT_EQ("[P id=\"host\"]", | |
Xiaocheng
2017/03/02 20:00:16
For the test case for flat tree, to make the test
tanvir
2017/03/04 13:55:31
Taken care of this scenario. This tree is returnin
| |
187 commonAncesstorNode(EphemeralRangeInFlatTree( | |
188 toPositionInFlatTree(range->startPosition()), | |
189 toPositionInFlatTree(range->endPosition())))); | |
190 } | |
191 | |
151 } // namespace blink | 192 } // namespace blink |
OLD | NEW |