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

Side by Side Diff: third_party/WebKit/Source/core/editing/VisibleSelectionTest.cpp

Issue 1675163002: Rename ComposedTree to FlatTree (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: wip Created 4 years, 10 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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/VisibleSelection.h" 5 #include "core/editing/VisibleSelection.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 9
10 #define LOREM_IPSUM \ 10 #define LOREM_IPSUM \
(...skipping 18 matching lines...) Expand all
29 // Helper function to set the VisibleSelection base/extent. 29 // Helper function to set the VisibleSelection base/extent.
30 template <typename Strategy> 30 template <typename Strategy>
31 void setSelection(VisibleSelectionTemplate<Strategy>& selection, int base, i nt extend) 31 void setSelection(VisibleSelectionTemplate<Strategy>& selection, int base, i nt extend)
32 { 32 {
33 Node* node = document().body()->firstChild(); 33 Node* node = document().body()->firstChild();
34 selection.setBase(PositionTemplate<Strategy>(node, base)); 34 selection.setBase(PositionTemplate<Strategy>(node, base));
35 selection.setExtent(PositionTemplate<Strategy>(node, extend)); 35 selection.setExtent(PositionTemplate<Strategy>(node, extend));
36 } 36 }
37 }; 37 };
38 38
39 static void testComposedTreePositionsToEqualToDOMTreePositions(const VisibleSele ction& selection, const VisibleSelectionInComposedTree& selectionInComposedTree) 39 static void testFlatTreePositionsToEqualToDOMTreePositions(const VisibleSelectio n& selection, const VisibleSelectionInFlatTree& selectionInFlatTree)
40 { 40 {
41 // Since DOM tree positions can't be map to composed tree version, e.g. 41 // Since DOM tree positions can't be map to flat tree version, e.g.
42 // shadow root, not distributed node, we map a position in composed tree 42 // shadow root, not distributed node, we map a position in flat tree
43 // to DOM tree position. 43 // to DOM tree position.
44 EXPECT_EQ(selection.start(), toPositionInDOMTree(selectionInComposedTree.sta rt())); 44 EXPECT_EQ(selection.start(), toPositionInDOMTree(selectionInFlatTree.start() ));
45 EXPECT_EQ(selection.end(), toPositionInDOMTree(selectionInComposedTree.end() )); 45 EXPECT_EQ(selection.end(), toPositionInDOMTree(selectionInFlatTree.end()));
46 EXPECT_EQ(selection.base(), toPositionInDOMTree(selectionInComposedTree.base ())); 46 EXPECT_EQ(selection.base(), toPositionInDOMTree(selectionInFlatTree.base())) ;
47 EXPECT_EQ(selection.extent(), toPositionInDOMTree(selectionInComposedTree.ex tent())); 47 EXPECT_EQ(selection.extent(), toPositionInDOMTree(selectionInFlatTree.extent ()));
48 } 48 }
49 49
50 TEST_F(VisibleSelectionTest, expandUsingGranularity) 50 TEST_F(VisibleSelectionTest, expandUsingGranularity)
51 { 51 {
52 const char* bodyContent = "<span id=host><a id=one>1</a><a id=two>22</a></sp an>"; 52 const char* bodyContent = "<span id=host><a id=one>1</a><a id=two>22</a></sp an>";
53 const char* shadowContent = "<p><b id=three>333</b><content select=#two></co ntent><b id=four>4444</b><span id=space> </span><content select=#one></content> <b id=five>55555</b></p>"; 53 const char* shadowContent = "<p><b id=three>333</b><content select=#two></co ntent><b id=four>4444</b><span id=space> </span><content select=#one></content> <b id=five>55555</b></p>";
54 setBodyContent(bodyContent); 54 setBodyContent(bodyContent);
55 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = setShadowContent(shadowContent, "host"); 55 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = setShadowContent(shadowContent, "host");
56 updateLayoutAndStyleForPainting(); 56 updateLayoutAndStyleForPainting();
57 57
58 Node* one = document().getElementById("one")->firstChild(); 58 Node* one = document().getElementById("one")->firstChild();
59 Node* two = document().getElementById("two")->firstChild(); 59 Node* two = document().getElementById("two")->firstChild();
60 Node* three = shadowRoot->getElementById("three")->firstChild(); 60 Node* three = shadowRoot->getElementById("three")->firstChild();
61 Node* four = shadowRoot->getElementById("four")->firstChild(); 61 Node* four = shadowRoot->getElementById("four")->firstChild();
62 Node* five = shadowRoot->getElementById("five")->firstChild(); 62 Node* five = shadowRoot->getElementById("five")->firstChild();
63 63
64 VisibleSelection selection; 64 VisibleSelection selection;
65 VisibleSelectionInComposedTree selectionInComposedTree; 65 VisibleSelectionInFlatTree selectionInFlatTree;
66 66
67 // From a position at distributed node 67 // From a position at distributed node
68 selection = VisibleSelection(createVisiblePosition(Position(one, 1))); 68 selection = VisibleSelection(createVisiblePosition(Position(one, 1)));
69 selection.expandUsingGranularity(WordGranularity); 69 selection.expandUsingGranularity(WordGranularity);
70 selectionInComposedTree = VisibleSelectionInComposedTree(createVisiblePositi on(PositionInComposedTree(one, 1))); 70 selectionInFlatTree = VisibleSelectionInFlatTree(createVisiblePosition(Posit ionInFlatTree(one, 1)));
71 selectionInComposedTree.expandUsingGranularity(WordGranularity); 71 selectionInFlatTree.expandUsingGranularity(WordGranularity);
72 72
73 EXPECT_EQ(Position(one, 1), selection.base()); 73 EXPECT_EQ(Position(one, 1), selection.base());
74 EXPECT_EQ(Position(one, 1), selection.extent()); 74 EXPECT_EQ(Position(one, 1), selection.extent());
75 EXPECT_EQ(Position(one, 0), selection.start()); 75 EXPECT_EQ(Position(one, 0), selection.start());
76 EXPECT_EQ(Position(two, 2), selection.end()); 76 EXPECT_EQ(Position(two, 2), selection.end());
77 77
78 EXPECT_EQ(PositionInComposedTree(one, 1), selectionInComposedTree.base()); 78 EXPECT_EQ(PositionInFlatTree(one, 1), selectionInFlatTree.base());
79 EXPECT_EQ(PositionInComposedTree(one, 1), selectionInComposedTree.extent()); 79 EXPECT_EQ(PositionInFlatTree(one, 1), selectionInFlatTree.extent());
80 EXPECT_EQ(PositionInComposedTree(one, 0), selectionInComposedTree.start()); 80 EXPECT_EQ(PositionInFlatTree(one, 0), selectionInFlatTree.start());
81 EXPECT_EQ(PositionInComposedTree(five, 5), selectionInComposedTree.end()); 81 EXPECT_EQ(PositionInFlatTree(five, 5), selectionInFlatTree.end());
82 82
83 // From a position at distributed node 83 // From a position at distributed node
84 selection = VisibleSelection(createVisiblePosition(Position(two, 1))); 84 selection = VisibleSelection(createVisiblePosition(Position(two, 1)));
85 selection.expandUsingGranularity(WordGranularity); 85 selection.expandUsingGranularity(WordGranularity);
86 selectionInComposedTree = VisibleSelectionInComposedTree(createVisiblePositi on(PositionInComposedTree(two, 1))); 86 selectionInFlatTree = VisibleSelectionInFlatTree(createVisiblePosition(Posit ionInFlatTree(two, 1)));
87 selectionInComposedTree.expandUsingGranularity(WordGranularity); 87 selectionInFlatTree.expandUsingGranularity(WordGranularity);
88 88
89 EXPECT_EQ(Position(two, 1), selection.base()); 89 EXPECT_EQ(Position(two, 1), selection.base());
90 EXPECT_EQ(Position(two, 1), selection.extent()); 90 EXPECT_EQ(Position(two, 1), selection.extent());
91 EXPECT_EQ(Position(one, 0), selection.start()); 91 EXPECT_EQ(Position(one, 0), selection.start());
92 EXPECT_EQ(Position(two, 2), selection.end()); 92 EXPECT_EQ(Position(two, 2), selection.end());
93 93
94 EXPECT_EQ(PositionInComposedTree(two, 1), selectionInComposedTree.base()); 94 EXPECT_EQ(PositionInFlatTree(two, 1), selectionInFlatTree.base());
95 EXPECT_EQ(PositionInComposedTree(two, 1), selectionInComposedTree.extent()); 95 EXPECT_EQ(PositionInFlatTree(two, 1), selectionInFlatTree.extent());
96 EXPECT_EQ(PositionInComposedTree(three, 0), selectionInComposedTree.start()) ; 96 EXPECT_EQ(PositionInFlatTree(three, 0), selectionInFlatTree.start());
97 EXPECT_EQ(PositionInComposedTree(four, 4), selectionInComposedTree.end()); 97 EXPECT_EQ(PositionInFlatTree(four, 4), selectionInFlatTree.end());
98 98
99 // From a position at node in shadow tree 99 // From a position at node in shadow tree
100 selection = VisibleSelection(createVisiblePosition(Position(three, 1))); 100 selection = VisibleSelection(createVisiblePosition(Position(three, 1)));
101 selection.expandUsingGranularity(WordGranularity); 101 selection.expandUsingGranularity(WordGranularity);
102 selectionInComposedTree = VisibleSelectionInComposedTree(createVisiblePositi on(PositionInComposedTree(three, 1))); 102 selectionInFlatTree = VisibleSelectionInFlatTree(createVisiblePosition(Posit ionInFlatTree(three, 1)));
103 selectionInComposedTree.expandUsingGranularity(WordGranularity); 103 selectionInFlatTree.expandUsingGranularity(WordGranularity);
104 104
105 EXPECT_EQ(Position(three, 1), selection.base()); 105 EXPECT_EQ(Position(three, 1), selection.base());
106 EXPECT_EQ(Position(three, 1), selection.extent()); 106 EXPECT_EQ(Position(three, 1), selection.extent());
107 EXPECT_EQ(Position(three, 0), selection.start()); 107 EXPECT_EQ(Position(three, 0), selection.start());
108 EXPECT_EQ(Position(four, 4), selection.end()); 108 EXPECT_EQ(Position(four, 4), selection.end());
109 109
110 EXPECT_EQ(PositionInComposedTree(three, 1), selectionInComposedTree.base()); 110 EXPECT_EQ(PositionInFlatTree(three, 1), selectionInFlatTree.base());
111 EXPECT_EQ(PositionInComposedTree(three, 1), selectionInComposedTree.extent() ); 111 EXPECT_EQ(PositionInFlatTree(three, 1), selectionInFlatTree.extent());
112 EXPECT_EQ(PositionInComposedTree(three, 0), selectionInComposedTree.start()) ; 112 EXPECT_EQ(PositionInFlatTree(three, 0), selectionInFlatTree.start());
113 EXPECT_EQ(PositionInComposedTree(four, 4), selectionInComposedTree.end()); 113 EXPECT_EQ(PositionInFlatTree(four, 4), selectionInFlatTree.end());
114 114
115 // From a position at node in shadow tree 115 // From a position at node in shadow tree
116 selection = VisibleSelection(createVisiblePosition(Position(four, 1))); 116 selection = VisibleSelection(createVisiblePosition(Position(four, 1)));
117 selection.expandUsingGranularity(WordGranularity); 117 selection.expandUsingGranularity(WordGranularity);
118 selectionInComposedTree = VisibleSelectionInComposedTree(createVisiblePositi on(PositionInComposedTree(four, 1))); 118 selectionInFlatTree = VisibleSelectionInFlatTree(createVisiblePosition(Posit ionInFlatTree(four, 1)));
119 selectionInComposedTree.expandUsingGranularity(WordGranularity); 119 selectionInFlatTree.expandUsingGranularity(WordGranularity);
120 120
121 EXPECT_EQ(Position(four, 1), selection.base()); 121 EXPECT_EQ(Position(four, 1), selection.base());
122 EXPECT_EQ(Position(four, 1), selection.extent()); 122 EXPECT_EQ(Position(four, 1), selection.extent());
123 EXPECT_EQ(Position(three, 0), selection.start()); 123 EXPECT_EQ(Position(three, 0), selection.start());
124 EXPECT_EQ(Position(four, 4), selection.end()); 124 EXPECT_EQ(Position(four, 4), selection.end());
125 125
126 EXPECT_EQ(PositionInComposedTree(four, 1), selectionInComposedTree.base()); 126 EXPECT_EQ(PositionInFlatTree(four, 1), selectionInFlatTree.base());
127 EXPECT_EQ(PositionInComposedTree(four, 1), selectionInComposedTree.extent()) ; 127 EXPECT_EQ(PositionInFlatTree(four, 1), selectionInFlatTree.extent());
128 EXPECT_EQ(PositionInComposedTree(three, 0), selectionInComposedTree.start()) ; 128 EXPECT_EQ(PositionInFlatTree(three, 0), selectionInFlatTree.start());
129 EXPECT_EQ(PositionInComposedTree(four, 4), selectionInComposedTree.end()); 129 EXPECT_EQ(PositionInFlatTree(four, 4), selectionInFlatTree.end());
130 130
131 // From a position at node in shadow tree 131 // From a position at node in shadow tree
132 selection = VisibleSelection(createVisiblePosition(Position(five, 1))); 132 selection = VisibleSelection(createVisiblePosition(Position(five, 1)));
133 selection.expandUsingGranularity(WordGranularity); 133 selection.expandUsingGranularity(WordGranularity);
134 selectionInComposedTree = VisibleSelectionInComposedTree(createVisiblePositi on(PositionInComposedTree(five, 1))); 134 selectionInFlatTree = VisibleSelectionInFlatTree(createVisiblePosition(Posit ionInFlatTree(five, 1)));
135 selectionInComposedTree.expandUsingGranularity(WordGranularity); 135 selectionInFlatTree.expandUsingGranularity(WordGranularity);
136 136
137 EXPECT_EQ(Position(five, 1), selection.base()); 137 EXPECT_EQ(Position(five, 1), selection.base());
138 EXPECT_EQ(Position(five, 1), selection.extent()); 138 EXPECT_EQ(Position(five, 1), selection.extent());
139 EXPECT_EQ(Position(five, 0), selection.start()); 139 EXPECT_EQ(Position(five, 0), selection.start());
140 EXPECT_EQ(Position(five, 5), selection.end()); 140 EXPECT_EQ(Position(five, 5), selection.end());
141 141
142 EXPECT_EQ(PositionInComposedTree(five, 1), selectionInComposedTree.base()); 142 EXPECT_EQ(PositionInFlatTree(five, 1), selectionInFlatTree.base());
143 EXPECT_EQ(PositionInComposedTree(five, 1), selectionInComposedTree.extent()) ; 143 EXPECT_EQ(PositionInFlatTree(five, 1), selectionInFlatTree.extent());
144 EXPECT_EQ(PositionInComposedTree(one, 0), selectionInComposedTree.start()); 144 EXPECT_EQ(PositionInFlatTree(one, 0), selectionInFlatTree.start());
145 EXPECT_EQ(PositionInComposedTree(five, 5), selectionInComposedTree.end()); 145 EXPECT_EQ(PositionInFlatTree(five, 5), selectionInFlatTree.end());
146 } 146 }
147 147
148 TEST_F(VisibleSelectionTest, Initialisation) 148 TEST_F(VisibleSelectionTest, Initialisation)
149 { 149 {
150 setBodyContent(LOREM_IPSUM); 150 setBodyContent(LOREM_IPSUM);
151 151
152 VisibleSelection selection; 152 VisibleSelection selection;
153 VisibleSelectionInComposedTree selectionInComposedTree; 153 VisibleSelectionInFlatTree selectionInFlatTree;
154 setSelection(selection, 0); 154 setSelection(selection, 0);
155 setSelection(selectionInComposedTree, 0); 155 setSelection(selectionInFlatTree, 0);
156 156
157 EXPECT_FALSE(selection.isNone()); 157 EXPECT_FALSE(selection.isNone());
158 EXPECT_FALSE(selectionInComposedTree.isNone()); 158 EXPECT_FALSE(selectionInFlatTree.isNone());
159 EXPECT_TRUE(selection.isCaret()); 159 EXPECT_TRUE(selection.isCaret());
160 EXPECT_TRUE(selectionInComposedTree.isCaret()); 160 EXPECT_TRUE(selectionInFlatTree.isCaret());
161 161
162 RefPtrWillBeRawPtr<Range> range = firstRangeOf(selection); 162 RefPtrWillBeRawPtr<Range> range = firstRangeOf(selection);
163 EXPECT_EQ(0, range->startOffset()); 163 EXPECT_EQ(0, range->startOffset());
164 EXPECT_EQ(0, range->endOffset()); 164 EXPECT_EQ(0, range->endOffset());
165 EXPECT_EQ("", range->text()); 165 EXPECT_EQ("", range->text());
166 testComposedTreePositionsToEqualToDOMTreePositions(selection, selectionInCom posedTree); 166 testFlatTreePositionsToEqualToDOMTreePositions(selection, selectionInFlatTre e);
167 } 167 }
168 168
169 TEST_F(VisibleSelectionTest, ShadowCrossing) 169 TEST_F(VisibleSelectionTest, ShadowCrossing)
170 { 170 {
171 const char* bodyContent = "<p id='host'>00<b id='one'>11</b><b id='two'>22</ b>33</p>"; 171 const char* bodyContent = "<p id='host'>00<b id='one'>11</b><b id='two'>22</ b>33</p>";
172 const char* shadowContent = "<a><span id='s4'>44</span><content select=#two> </content><span id='s5'>55</span><content select=#one></content><span id='s6'>66 </span></a>"; 172 const char* shadowContent = "<a><span id='s4'>44</span><content select=#two> </content><span id='s5'>55</span><content select=#one></content><span id='s6'>66 </span></a>";
173 setBodyContent(bodyContent); 173 setBodyContent(bodyContent);
174 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = setShadowContent(shadowContent, "host"); 174 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = setShadowContent(shadowContent, "host");
175 175
176 RefPtrWillBeRawPtr<Element> body = document().body(); 176 RefPtrWillBeRawPtr<Element> body = document().body();
177 RefPtrWillBeRawPtr<Element> host = body->querySelector("#host", ASSERT_NO_EX CEPTION); 177 RefPtrWillBeRawPtr<Element> host = body->querySelector("#host", ASSERT_NO_EX CEPTION);
178 RefPtrWillBeRawPtr<Element> one = body->querySelector("#one", ASSERT_NO_EXCE PTION); 178 RefPtrWillBeRawPtr<Element> one = body->querySelector("#one", ASSERT_NO_EXCE PTION);
179 RefPtrWillBeRawPtr<Element> six = shadowRoot->querySelector("#s6", ASSERT_NO _EXCEPTION); 179 RefPtrWillBeRawPtr<Element> six = shadowRoot->querySelector("#s6", ASSERT_NO _EXCEPTION);
180 180
181 VisibleSelection selection(Position::firstPositionInNode(one.get()), Positio n::lastPositionInNode(shadowRoot.get())); 181 VisibleSelection selection(Position::firstPositionInNode(one.get()), Positio n::lastPositionInNode(shadowRoot.get()));
182 VisibleSelectionInComposedTree selectionInComposedTree(PositionInComposedTre e::firstPositionInNode(one.get()), PositionInComposedTree::lastPositionInNode(ho st.get())); 182 VisibleSelectionInFlatTree selectionInFlatTree(PositionInFlatTree::firstPosi tionInNode(one.get()), PositionInFlatTree::lastPositionInNode(host.get()));
183 183
184 EXPECT_EQ(Position(host.get(), PositionAnchorType::BeforeAnchor), selection. start()); 184 EXPECT_EQ(Position(host.get(), PositionAnchorType::BeforeAnchor), selection. start());
185 EXPECT_EQ(Position(one->firstChild(), 0), selection.end()); 185 EXPECT_EQ(Position(one->firstChild(), 0), selection.end());
186 EXPECT_EQ(PositionInComposedTree(one->firstChild(), 0), selectionInComposedT ree.start()); 186 EXPECT_EQ(PositionInFlatTree(one->firstChild(), 0), selectionInFlatTree.star t());
187 EXPECT_EQ(PositionInComposedTree(six->firstChild(), 2), selectionInComposedT ree.end()); 187 EXPECT_EQ(PositionInFlatTree(six->firstChild(), 2), selectionInFlatTree.end( ));
188 } 188 }
189 189
190 TEST_F(VisibleSelectionTest, ShadowV0DistributedNodes) 190 TEST_F(VisibleSelectionTest, ShadowV0DistributedNodes)
191 { 191 {
192 const char* bodyContent = "<p id='host'>00<b id='one'>11</b><b id='two'>22</ b>33</p>"; 192 const char* bodyContent = "<p id='host'>00<b id='one'>11</b><b id='two'>22</ b>33</p>";
193 const char* shadowContent = "<a><span id='s4'>44</span><content select=#two> </content><span id='s5'>55</span><content select=#one></content><span id='s6'>66 </span></a>"; 193 const char* shadowContent = "<a><span id='s4'>44</span><content select=#two> </content><span id='s5'>55</span><content select=#one></content><span id='s6'>66 </span></a>";
194 setBodyContent(bodyContent); 194 setBodyContent(bodyContent);
195 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = setShadowContent(shadowContent, "host"); 195 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = setShadowContent(shadowContent, "host");
196 196
197 RefPtrWillBeRawPtr<Element> body = document().body(); 197 RefPtrWillBeRawPtr<Element> body = document().body();
198 RefPtrWillBeRawPtr<Element> one = body->querySelector("#one", ASSERT_NO_EXCE PTION); 198 RefPtrWillBeRawPtr<Element> one = body->querySelector("#one", ASSERT_NO_EXCE PTION);
199 RefPtrWillBeRawPtr<Element> two = body->querySelector("#two", ASSERT_NO_EXCE PTION); 199 RefPtrWillBeRawPtr<Element> two = body->querySelector("#two", ASSERT_NO_EXCE PTION);
200 RefPtrWillBeRawPtr<Element> five = shadowRoot->querySelector("#s5", ASSERT_N O_EXCEPTION); 200 RefPtrWillBeRawPtr<Element> five = shadowRoot->querySelector("#s5", ASSERT_N O_EXCEPTION);
201 201
202 VisibleSelection selection(Position::firstPositionInNode(one.get()), Positio n::lastPositionInNode(two.get())); 202 VisibleSelection selection(Position::firstPositionInNode(one.get()), Positio n::lastPositionInNode(two.get()));
203 VisibleSelectionInComposedTree selectionInComposedTree(PositionInComposedTre e::firstPositionInNode(one.get()), PositionInComposedTree::lastPositionInNode(tw o.get())); 203 VisibleSelectionInFlatTree selectionInFlatTree(PositionInFlatTree::firstPosi tionInNode(one.get()), PositionInFlatTree::lastPositionInNode(two.get()));
204 204
205 EXPECT_EQ(Position(one->firstChild(), 0), selection.start()); 205 EXPECT_EQ(Position(one->firstChild(), 0), selection.start());
206 EXPECT_EQ(Position(two->firstChild(), 2), selection.end()); 206 EXPECT_EQ(Position(two->firstChild(), 2), selection.end());
207 EXPECT_EQ(PositionInComposedTree(five->firstChild(), 0), selectionInComposed Tree.start()); 207 EXPECT_EQ(PositionInFlatTree(five->firstChild(), 0), selectionInFlatTree.sta rt());
208 EXPECT_EQ(PositionInComposedTree(five->firstChild(), 2), selectionInComposed Tree.end()); 208 EXPECT_EQ(PositionInFlatTree(five->firstChild(), 2), selectionInFlatTree.end ());
209 } 209 }
210 210
211 TEST_F(VisibleSelectionTest, ShadowNested) 211 TEST_F(VisibleSelectionTest, ShadowNested)
212 { 212 {
213 const char* bodyContent = "<p id='host'>00<b id='one'>11</b><b id='two'>22</ b>33</p>"; 213 const char* bodyContent = "<p id='host'>00<b id='one'>11</b><b id='two'>22</ b>33</p>";
214 const char* shadowContent = "<a><span id='s4'>44</span><content select=#two> </content><span id='s5'>55</span><content select=#one></content><span id='s6'>66 </span></a>"; 214 const char* shadowContent = "<a><span id='s4'>44</span><content select=#two> </content><span id='s5'>55</span><content select=#one></content><span id='s6'>66 </span></a>";
215 const char* shadowContent2 = "<span id='s7'>77</span><content></content><spa n id='s8'>88</span>"; 215 const char* shadowContent2 = "<span id='s7'>77</span><content></content><spa n id='s8'>88</span>";
216 setBodyContent(bodyContent); 216 setBodyContent(bodyContent);
217 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = setShadowContent(shadowContent, "host"); 217 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = setShadowContent(shadowContent, "host");
218 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot2 = createShadowRootForElementWithI DAndSetInnerHTML(*shadowRoot, "s5", shadowContent2); 218 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot2 = createShadowRootForElementWithI DAndSetInnerHTML(*shadowRoot, "s5", shadowContent2);
219 219
220 // Composed tree is something like below: 220 // Flat tree is something like below:
221 // <p id="host"> 221 // <p id="host">
222 // <span id="s4">44</span> 222 // <span id="s4">44</span>
223 // <b id="two">22</b> 223 // <b id="two">22</b>
224 // <span id="s5"><span id="s7">77>55</span id="s8">88</span> 224 // <span id="s5"><span id="s7">77>55</span id="s8">88</span>
225 // <b id="one">11</b> 225 // <b id="one">11</b>
226 // <span id="s6">66</span> 226 // <span id="s6">66</span>
227 // </p> 227 // </p>
228 RefPtrWillBeRawPtr<Element> body = document().body(); 228 RefPtrWillBeRawPtr<Element> body = document().body();
229 RefPtrWillBeRawPtr<Element> host = body->querySelector("#host", ASSERT_NO_EX CEPTION); 229 RefPtrWillBeRawPtr<Element> host = body->querySelector("#host", ASSERT_NO_EX CEPTION);
230 RefPtrWillBeRawPtr<Element> one = body->querySelector("#one", ASSERT_NO_EXCE PTION); 230 RefPtrWillBeRawPtr<Element> one = body->querySelector("#one", ASSERT_NO_EXCE PTION);
231 RefPtrWillBeRawPtr<Element> eight = shadowRoot2->querySelector("#s8", ASSERT _NO_EXCEPTION); 231 RefPtrWillBeRawPtr<Element> eight = shadowRoot2->querySelector("#s8", ASSERT _NO_EXCEPTION);
232 232
233 VisibleSelection selection(Position::firstPositionInNode(one.get()), Positio n::lastPositionInNode(shadowRoot2.get())); 233 VisibleSelection selection(Position::firstPositionInNode(one.get()), Positio n::lastPositionInNode(shadowRoot2.get()));
234 VisibleSelectionInComposedTree selectionInComposedTree(PositionInComposedTre e::firstPositionInNode(one.get()), PositionInComposedTree::afterNode(eight.get() )); 234 VisibleSelectionInFlatTree selectionInFlatTree(PositionInFlatTree::firstPosi tionInNode(one.get()), PositionInFlatTree::afterNode(eight.get()));
235 235
236 EXPECT_EQ(Position(host.get(), PositionAnchorType::BeforeAnchor), selection. start()); 236 EXPECT_EQ(Position(host.get(), PositionAnchorType::BeforeAnchor), selection. start());
237 EXPECT_EQ(Position(one->firstChild(), 0), selection.end()); 237 EXPECT_EQ(Position(one->firstChild(), 0), selection.end());
238 EXPECT_EQ(PositionInComposedTree(eight->firstChild(), 2), selectionInCompose dTree.start()); 238 EXPECT_EQ(PositionInFlatTree(eight->firstChild(), 2), selectionInFlatTree.st art());
239 EXPECT_EQ(PositionInComposedTree(eight->firstChild(), 2), selectionInCompose dTree.end()); 239 EXPECT_EQ(PositionInFlatTree(eight->firstChild(), 2), selectionInFlatTree.en d());
240 } 240 }
241 241
242 TEST_F(VisibleSelectionTest, WordGranularity) 242 TEST_F(VisibleSelectionTest, WordGranularity)
243 { 243 {
244 setBodyContent(LOREM_IPSUM); 244 setBodyContent(LOREM_IPSUM);
245 245
246 VisibleSelection selection; 246 VisibleSelection selection;
247 VisibleSelectionInComposedTree selectionInComposedTree; 247 VisibleSelectionInFlatTree selectionInFlatTree;
248 248
249 // Beginning of a word. 249 // Beginning of a word.
250 { 250 {
251 setSelection(selection, 0); 251 setSelection(selection, 0);
252 setSelection(selectionInComposedTree, 0); 252 setSelection(selectionInFlatTree, 0);
253 selection.expandUsingGranularity(WordGranularity); 253 selection.expandUsingGranularity(WordGranularity);
254 selectionInComposedTree.expandUsingGranularity(WordGranularity); 254 selectionInFlatTree.expandUsingGranularity(WordGranularity);
255 255
256 RefPtrWillBeRawPtr<Range> range = firstRangeOf(selection); 256 RefPtrWillBeRawPtr<Range> range = firstRangeOf(selection);
257 EXPECT_EQ(0, range->startOffset()); 257 EXPECT_EQ(0, range->startOffset());
258 EXPECT_EQ(5, range->endOffset()); 258 EXPECT_EQ(5, range->endOffset());
259 EXPECT_EQ("Lorem", range->text()); 259 EXPECT_EQ("Lorem", range->text());
260 testComposedTreePositionsToEqualToDOMTreePositions(selection, selectionI nComposedTree); 260 testFlatTreePositionsToEqualToDOMTreePositions(selection, selectionInFla tTree);
261 261
262 } 262 }
263 263
264 // Middle of a word. 264 // Middle of a word.
265 { 265 {
266 setSelection(selection, 8); 266 setSelection(selection, 8);
267 setSelection(selectionInComposedTree, 8); 267 setSelection(selectionInFlatTree, 8);
268 selection.expandUsingGranularity(WordGranularity); 268 selection.expandUsingGranularity(WordGranularity);
269 selectionInComposedTree.expandUsingGranularity(WordGranularity); 269 selectionInFlatTree.expandUsingGranularity(WordGranularity);
270 270
271 RefPtrWillBeRawPtr<Range> range = firstRangeOf(selection); 271 RefPtrWillBeRawPtr<Range> range = firstRangeOf(selection);
272 EXPECT_EQ(6, range->startOffset()); 272 EXPECT_EQ(6, range->startOffset());
273 EXPECT_EQ(11, range->endOffset()); 273 EXPECT_EQ(11, range->endOffset());
274 EXPECT_EQ("ipsum", range->text()); 274 EXPECT_EQ("ipsum", range->text());
275 testComposedTreePositionsToEqualToDOMTreePositions(selection, selectionI nComposedTree); 275 testFlatTreePositionsToEqualToDOMTreePositions(selection, selectionInFla tTree);
276 276
277 } 277 }
278 278
279 // End of a word. 279 // End of a word.
280 // FIXME: that sounds buggy, we might want to select the word _before_ inste ad 280 // FIXME: that sounds buggy, we might want to select the word _before_ inste ad
281 // of the space... 281 // of the space...
282 { 282 {
283 setSelection(selection, 5); 283 setSelection(selection, 5);
284 setSelection(selectionInComposedTree, 5); 284 setSelection(selectionInFlatTree, 5);
285 selection.expandUsingGranularity(WordGranularity); 285 selection.expandUsingGranularity(WordGranularity);
286 selectionInComposedTree.expandUsingGranularity(WordGranularity); 286 selectionInFlatTree.expandUsingGranularity(WordGranularity);
287 287
288 RefPtrWillBeRawPtr<Range> range = firstRangeOf(selection); 288 RefPtrWillBeRawPtr<Range> range = firstRangeOf(selection);
289 EXPECT_EQ(5, range->startOffset()); 289 EXPECT_EQ(5, range->startOffset());
290 EXPECT_EQ(6, range->endOffset()); 290 EXPECT_EQ(6, range->endOffset());
291 EXPECT_EQ(" ", range->text()); 291 EXPECT_EQ(" ", range->text());
292 testComposedTreePositionsToEqualToDOMTreePositions(selection, selectionI nComposedTree); 292 testFlatTreePositionsToEqualToDOMTreePositions(selection, selectionInFla tTree);
293 } 293 }
294 294
295 // Before comma. 295 // Before comma.
296 // FIXME: that sounds buggy, we might want to select the word _before_ inste ad 296 // FIXME: that sounds buggy, we might want to select the word _before_ inste ad
297 // of the comma. 297 // of the comma.
298 { 298 {
299 setSelection(selection, 26); 299 setSelection(selection, 26);
300 setSelection(selectionInComposedTree, 26); 300 setSelection(selectionInFlatTree, 26);
301 selection.expandUsingGranularity(WordGranularity); 301 selection.expandUsingGranularity(WordGranularity);
302 selectionInComposedTree.expandUsingGranularity(WordGranularity); 302 selectionInFlatTree.expandUsingGranularity(WordGranularity);
303 303
304 RefPtrWillBeRawPtr<Range> range = firstRangeOf(selection); 304 RefPtrWillBeRawPtr<Range> range = firstRangeOf(selection);
305 EXPECT_EQ(26, range->startOffset()); 305 EXPECT_EQ(26, range->startOffset());
306 EXPECT_EQ(27, range->endOffset()); 306 EXPECT_EQ(27, range->endOffset());
307 EXPECT_EQ(",", range->text()); 307 EXPECT_EQ(",", range->text());
308 testComposedTreePositionsToEqualToDOMTreePositions(selection, selectionI nComposedTree); 308 testFlatTreePositionsToEqualToDOMTreePositions(selection, selectionInFla tTree);
309 } 309 }
310 310
311 // After comma. 311 // After comma.
312 { 312 {
313 setSelection(selection, 27); 313 setSelection(selection, 27);
314 setSelection(selectionInComposedTree, 27); 314 setSelection(selectionInFlatTree, 27);
315 selection.expandUsingGranularity(WordGranularity); 315 selection.expandUsingGranularity(WordGranularity);
316 selectionInComposedTree.expandUsingGranularity(WordGranularity); 316 selectionInFlatTree.expandUsingGranularity(WordGranularity);
317 317
318 RefPtrWillBeRawPtr<Range> range = firstRangeOf(selection); 318 RefPtrWillBeRawPtr<Range> range = firstRangeOf(selection);
319 EXPECT_EQ(27, range->startOffset()); 319 EXPECT_EQ(27, range->startOffset());
320 EXPECT_EQ(28, range->endOffset()); 320 EXPECT_EQ(28, range->endOffset());
321 EXPECT_EQ(" ", range->text()); 321 EXPECT_EQ(" ", range->text());
322 testComposedTreePositionsToEqualToDOMTreePositions(selection, selectionI nComposedTree); 322 testFlatTreePositionsToEqualToDOMTreePositions(selection, selectionInFla tTree);
323 } 323 }
324 324
325 // When selecting part of a word. 325 // When selecting part of a word.
326 { 326 {
327 setSelection(selection, 0, 1); 327 setSelection(selection, 0, 1);
328 setSelection(selectionInComposedTree, 0, 1); 328 setSelection(selectionInFlatTree, 0, 1);
329 selection.expandUsingGranularity(WordGranularity); 329 selection.expandUsingGranularity(WordGranularity);
330 selectionInComposedTree.expandUsingGranularity(WordGranularity); 330 selectionInFlatTree.expandUsingGranularity(WordGranularity);
331 331
332 RefPtrWillBeRawPtr<Range> range = firstRangeOf(selection); 332 RefPtrWillBeRawPtr<Range> range = firstRangeOf(selection);
333 EXPECT_EQ(0, range->startOffset()); 333 EXPECT_EQ(0, range->startOffset());
334 EXPECT_EQ(5, range->endOffset()); 334 EXPECT_EQ(5, range->endOffset());
335 EXPECT_EQ("Lorem", range->text()); 335 EXPECT_EQ("Lorem", range->text());
336 testComposedTreePositionsToEqualToDOMTreePositions(selection, selectionI nComposedTree); 336 testFlatTreePositionsToEqualToDOMTreePositions(selection, selectionInFla tTree);
337 } 337 }
338 338
339 // When selecting part of two words. 339 // When selecting part of two words.
340 { 340 {
341 setSelection(selection, 2, 8); 341 setSelection(selection, 2, 8);
342 setSelection(selectionInComposedTree, 2, 8); 342 setSelection(selectionInFlatTree, 2, 8);
343 selection.expandUsingGranularity(WordGranularity); 343 selection.expandUsingGranularity(WordGranularity);
344 selectionInComposedTree.expandUsingGranularity(WordGranularity); 344 selectionInFlatTree.expandUsingGranularity(WordGranularity);
345 345
346 RefPtrWillBeRawPtr<Range> range = firstRangeOf(selection); 346 RefPtrWillBeRawPtr<Range> range = firstRangeOf(selection);
347 EXPECT_EQ(0, range->startOffset()); 347 EXPECT_EQ(0, range->startOffset());
348 EXPECT_EQ(11, range->endOffset()); 348 EXPECT_EQ(11, range->endOffset());
349 EXPECT_EQ("Lorem ipsum", range->text()); 349 EXPECT_EQ("Lorem ipsum", range->text());
350 testComposedTreePositionsToEqualToDOMTreePositions(selection, selectionI nComposedTree); 350 testFlatTreePositionsToEqualToDOMTreePositions(selection, selectionInFla tTree);
351 } 351 }
352 } 352 }
353 353
354 } // namespace blink 354 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698