OLD | NEW |
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/FrameSelection.h" | 5 #include "core/editing/FrameSelection.h" |
6 | 6 |
7 #include "bindings/core/v8/ExceptionStatePlaceholder.h" | 7 #include "bindings/core/v8/ExceptionStatePlaceholder.h" |
8 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
9 #include "core/dom/Element.h" | 9 #include "core/dom/Element.h" |
10 #include "core/dom/Text.h" | 10 #include "core/dom/Text.h" |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 document().body()->setContentEditable("true", ASSERT_NO_EXCEPTION); | 135 document().body()->setContentEditable("true", ASSERT_NO_EXCEPTION); |
136 document().body()->focus(); | 136 document().body()->focus(); |
137 EXPECT_TRUE(document().body()->focused()); | 137 EXPECT_TRUE(document().body()->focused()); |
138 | 138 |
139 selection().setCaretVisible(true); | 139 selection().setCaretVisible(true); |
140 EXPECT_TRUE(selection().isCaret()); | 140 EXPECT_TRUE(selection().isCaret()); |
141 EXPECT_TRUE(shouldPaintCaretForTesting()); | 141 EXPECT_TRUE(shouldPaintCaretForTesting()); |
142 | 142 |
143 // Simulate to type "Hello, World!". | 143 // Simulate to type "Hello, World!". |
144 DisableCompositingQueryAsserts disabler; | 144 DisableCompositingQueryAsserts disabler; |
145 selection().moveTo(createVisiblePositionDeprecated(selection().end(), select
ion().affinity()), NotUserTriggered); | 145 document().updateStyleAndLayout(); |
| 146 selection().moveTo(createVisiblePosition(selection().end(), selection().affi
nity()), NotUserTriggered); |
146 selection().setCaretRectNeedsUpdate(); | 147 selection().setCaretRectNeedsUpdate(); |
147 EXPECT_TRUE(selection().isCaretBoundsDirty()); | 148 EXPECT_TRUE(selection().isCaretBoundsDirty()); |
148 EXPECT_FALSE(isPreviousCaretDirtyForTesting()); | 149 EXPECT_FALSE(isPreviousCaretDirtyForTesting()); |
149 selection().invalidateCaretRect(); | 150 selection().invalidateCaretRect(); |
150 EXPECT_FALSE(selection().isCaretBoundsDirty()); | 151 EXPECT_FALSE(selection().isCaretBoundsDirty()); |
151 EXPECT_TRUE(isPreviousCaretDirtyForTesting()); | 152 EXPECT_TRUE(isPreviousCaretDirtyForTesting()); |
152 | 153 |
153 // Simulate to remove all except for "H". | 154 // Simulate to remove all except for "H". |
154 text->replaceWholeText("H"); | 155 text->replaceWholeText("H"); |
155 selection().moveTo(createVisiblePositionDeprecated(selection().end(), select
ion().affinity()), NotUserTriggered); | 156 document().updateStyleAndLayout(); |
| 157 selection().moveTo(createVisiblePosition(selection().end(), selection().affi
nity()), NotUserTriggered); |
156 selection().setCaretRectNeedsUpdate(); | 158 selection().setCaretRectNeedsUpdate(); |
157 EXPECT_TRUE(selection().isCaretBoundsDirty()); | 159 EXPECT_TRUE(selection().isCaretBoundsDirty()); |
158 // "H" remains so early previousCaret invalidation isn't needed. | 160 // "H" remains so early previousCaret invalidation isn't needed. |
159 EXPECT_TRUE(isPreviousCaretDirtyForTesting()); | 161 EXPECT_TRUE(isPreviousCaretDirtyForTesting()); |
160 selection().invalidateCaretRect(); | 162 selection().invalidateCaretRect(); |
161 EXPECT_FALSE(selection().isCaretBoundsDirty()); | 163 EXPECT_FALSE(selection().isCaretBoundsDirty()); |
162 EXPECT_TRUE(isPreviousCaretDirtyForTesting()); | 164 EXPECT_TRUE(isPreviousCaretDirtyForTesting()); |
163 | 165 |
164 // Simulate to remove the last character. | 166 // Simulate to remove the last character. |
165 document().body()->removeChild(text); | 167 document().body()->removeChild(text); |
166 // This line is the objective of this test. | 168 // This line is the objective of this test. |
167 // As removing the last character, early previousCaret invalidation is execu
ted. | 169 // As removing the last character, early previousCaret invalidation is execu
ted. |
168 EXPECT_FALSE(isPreviousCaretDirtyForTesting()); | 170 EXPECT_FALSE(isPreviousCaretDirtyForTesting()); |
169 document().updateStyleAndLayoutIgnorePendingStylesheets(); | 171 document().updateStyleAndLayoutIgnorePendingStylesheets(); |
170 selection().setCaretRectNeedsUpdate(); | 172 selection().setCaretRectNeedsUpdate(); |
171 EXPECT_TRUE(selection().isCaretBoundsDirty()); | 173 EXPECT_TRUE(selection().isCaretBoundsDirty()); |
172 EXPECT_FALSE(isPreviousCaretDirtyForTesting()); | 174 EXPECT_FALSE(isPreviousCaretDirtyForTesting()); |
173 selection().invalidateCaretRect(); | 175 selection().invalidateCaretRect(); |
174 EXPECT_FALSE(selection().isCaretBoundsDirty()); | 176 EXPECT_FALSE(selection().isCaretBoundsDirty()); |
175 EXPECT_TRUE(isPreviousCaretDirtyForTesting()); | 177 EXPECT_TRUE(isPreviousCaretDirtyForTesting()); |
176 } | 178 } |
177 | 179 |
178 #define EXPECT_EQ_SELECTED_TEXT(text) \ | 180 #define EXPECT_EQ_SELECTED_TEXT(text) \ |
179 EXPECT_EQ(text, WebString(selection().selectedText()).utf8()) | 181 EXPECT_EQ(text, WebString(selection().selectedText()).utf8()) |
180 | 182 |
181 TEST_F(FrameSelectionTest, SelectWordAroundPosition) | 183 TEST_F(FrameSelectionTest, SelectWordAroundPosition) |
182 { | 184 { |
183 // "Foo Bar Baz," | 185 // "Foo Bar Baz," |
184 Text* text = appendTextNode("Foo Bar Baz,"); | 186 Text* text = appendTextNode("Foo Bar Baz,"); |
| 187 updateAllLifecyclePhases(); |
| 188 |
185 // "Fo|o Bar Baz," | 189 // "Fo|o Bar Baz," |
186 EXPECT_TRUE(selection().selectWordAroundPosition(createVisiblePositionDeprec
ated(Position(text, 2)))); | 190 EXPECT_TRUE(selection().selectWordAroundPosition(createVisiblePosition(Posit
ion(text, 2)))); |
187 EXPECT_EQ_SELECTED_TEXT("Foo"); | 191 EXPECT_EQ_SELECTED_TEXT("Foo"); |
188 // "Foo| Bar Baz," | 192 // "Foo| Bar Baz," |
189 EXPECT_TRUE(selection().selectWordAroundPosition(createVisiblePositionDeprec
ated(Position(text, 3)))); | 193 EXPECT_TRUE(selection().selectWordAroundPosition(createVisiblePosition(Posit
ion(text, 3)))); |
190 EXPECT_EQ_SELECTED_TEXT("Foo"); | 194 EXPECT_EQ_SELECTED_TEXT("Foo"); |
191 // "Foo Bar | Baz," | 195 // "Foo Bar | Baz," |
192 EXPECT_FALSE(selection().selectWordAroundPosition(createVisiblePositionDepre
cated(Position(text, 13)))); | 196 EXPECT_FALSE(selection().selectWordAroundPosition(createVisiblePosition(Posi
tion(text, 13)))); |
193 // "Foo Bar Baz|," | 197 // "Foo Bar Baz|," |
194 EXPECT_TRUE(selection().selectWordAroundPosition(createVisiblePositionDeprec
ated(Position(text, 22)))); | 198 EXPECT_TRUE(selection().selectWordAroundPosition(createVisiblePosition(Posit
ion(text, 22)))); |
195 EXPECT_EQ_SELECTED_TEXT("Baz"); | 199 EXPECT_EQ_SELECTED_TEXT("Baz"); |
196 } | 200 } |
197 | 201 |
198 TEST_F(FrameSelectionTest, ModifyExtendWithFlatTree) | 202 TEST_F(FrameSelectionTest, ModifyExtendWithFlatTree) |
199 { | 203 { |
200 setBodyContent("<span id=host></span>one"); | 204 setBodyContent("<span id=host></span>one"); |
201 setShadowContent("two<content></content>", "host"); | 205 setShadowContent("two<content></content>", "host"); |
202 Element* host = document().getElementById("host"); | 206 Element* host = document().getElementById("host"); |
203 Node* const two = FlatTreeTraversal::firstChild(*host); | 207 Node* const two = FlatTreeTraversal::firstChild(*host); |
204 // Select "two" for selection in DOM tree | 208 // Select "two" for selection in DOM tree |
(...skipping 21 matching lines...) Expand all Loading... |
226 EXPECT_TRUE(selection().modify(FrameSelection::AlterationMove, DirectionForw
ard, CharacterGranularity, UserTriggered)) | 230 EXPECT_TRUE(selection().modify(FrameSelection::AlterationMove, DirectionForw
ard, CharacterGranularity, UserTriggered)) |
227 << "Selection.modify() returns true for user-triggered call"; | 231 << "Selection.modify() returns true for user-triggered call"; |
228 EXPECT_EQ(endOfText, selection().start()) | 232 EXPECT_EQ(endOfText, selection().start()) |
229 << "Selection isn't modified"; | 233 << "Selection isn't modified"; |
230 } | 234 } |
231 | 235 |
232 TEST_F(FrameSelectionTest, MoveRangeSelectionTest) | 236 TEST_F(FrameSelectionTest, MoveRangeSelectionTest) |
233 { | 237 { |
234 // "Foo Bar Baz," | 238 // "Foo Bar Baz," |
235 Text* text = appendTextNode("Foo Bar Baz,"); | 239 Text* text = appendTextNode("Foo Bar Baz,"); |
| 240 updateAllLifecyclePhases(); |
| 241 |
236 // Itinitializes with "Foo B|a>r Baz," (| means start and > means end). | 242 // Itinitializes with "Foo B|a>r Baz," (| means start and > means end). |
237 selection().setSelection(VisibleSelection(Position(text, 5), Position(text,
6))); | 243 selection().setSelection(VisibleSelection(Position(text, 5), Position(text,
6))); |
238 EXPECT_EQ_SELECTED_TEXT("a"); | 244 EXPECT_EQ_SELECTED_TEXT("a"); |
239 | 245 |
240 // "Foo B|ar B>az," with the Character granularity. | 246 // "Foo B|ar B>az," with the Character granularity. |
241 selection().moveRangeSelection(createVisiblePositionDeprecated(Position(text
, 5)), createVisiblePositionDeprecated(Position(text, 9)), CharacterGranularity)
; | 247 selection().moveRangeSelection(createVisiblePosition(Position(text, 5)), cre
ateVisiblePosition(Position(text, 9)), CharacterGranularity); |
242 EXPECT_EQ_SELECTED_TEXT("ar B"); | 248 EXPECT_EQ_SELECTED_TEXT("ar B"); |
243 // "Foo B|ar B>az," with the Word granularity. | 249 // "Foo B|ar B>az," with the Word granularity. |
244 selection().moveRangeSelection(createVisiblePositionDeprecated(Position(text
, 5)), createVisiblePositionDeprecated(Position(text, 9)), WordGranularity); | 250 selection().moveRangeSelection(createVisiblePosition(Position(text, 5)), cre
ateVisiblePosition(Position(text, 9)), WordGranularity); |
245 EXPECT_EQ_SELECTED_TEXT("Bar Baz"); | 251 EXPECT_EQ_SELECTED_TEXT("Bar Baz"); |
246 // "Fo<o B|ar Baz," with the Character granularity. | 252 // "Fo<o B|ar Baz," with the Character granularity. |
247 selection().moveRangeSelection(createVisiblePositionDeprecated(Position(text
, 5)), createVisiblePositionDeprecated(Position(text, 2)), CharacterGranularity)
; | 253 selection().moveRangeSelection(createVisiblePosition(Position(text, 5)), cre
ateVisiblePosition(Position(text, 2)), CharacterGranularity); |
248 EXPECT_EQ_SELECTED_TEXT("o B"); | 254 EXPECT_EQ_SELECTED_TEXT("o B"); |
249 // "Fo<o B|ar Baz," with the Word granularity. | 255 // "Fo<o B|ar Baz," with the Word granularity. |
250 selection().moveRangeSelection(createVisiblePositionDeprecated(Position(text
, 5)), createVisiblePositionDeprecated(Position(text, 2)), WordGranularity); | 256 selection().moveRangeSelection(createVisiblePosition(Position(text, 5)), cre
ateVisiblePosition(Position(text, 2)), WordGranularity); |
251 EXPECT_EQ_SELECTED_TEXT("Foo Bar"); | 257 EXPECT_EQ_SELECTED_TEXT("Foo Bar"); |
252 } | 258 } |
253 | 259 |
254 TEST_F(FrameSelectionTest, setNonDirectionalSelectionIfNeeded) | 260 TEST_F(FrameSelectionTest, setNonDirectionalSelectionIfNeeded) |
255 { | 261 { |
256 const char* bodyContent = "<span id=top>top</span><span id=host></span>"; | 262 const char* bodyContent = "<span id=top>top</span><span id=host></span>"; |
257 const char* shadowContent = "<span id=bottom>bottom</span>"; | 263 const char* shadowContent = "<span id=bottom>bottom</span>"; |
258 setBodyContent(bodyContent); | 264 setBodyContent(bodyContent); |
259 ShadowRoot* shadowRoot = setShadowContent(shadowContent, "host"); | 265 ShadowRoot* shadowRoot = setShadowContent(shadowContent, "host"); |
260 | 266 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 selection().updateIfNeeded(); | 317 selection().updateIfNeeded(); |
312 | 318 |
313 // TODO(yosin): Once lazy canonicalization implemented, selection.start | 319 // TODO(yosin): Once lazy canonicalization implemented, selection.start |
314 // should be Position(HTML, 0). | 320 // should be Position(HTML, 0). |
315 EXPECT_EQ(Position(), selection().start()) | 321 EXPECT_EQ(Position(), selection().start()) |
316 << "updateIfNeeded() makes selection to null."; | 322 << "updateIfNeeded() makes selection to null."; |
317 EXPECT_EQ(selection().start(), caretPosition().position()); | 323 EXPECT_EQ(selection().start(), caretPosition().position()); |
318 } | 324 } |
319 | 325 |
320 } // namespace blink | 326 } // namespace blink |
OLD | NEW |