| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/CaretDisplayItemClient.h" |
| 6 |
| 5 #include "core/HTMLNames.h" | 7 #include "core/HTMLNames.h" |
| 6 #include "core/editing/FrameSelection.h" | 8 #include "core/editing/FrameSelection.h" |
| 7 #include "core/frame/FrameView.h" | 9 #include "core/frame/FrameView.h" |
| 8 #include "core/layout/LayoutTestHelper.h" | 10 #include "core/layout/LayoutTestHelper.h" |
| 9 #include "core/layout/LayoutView.h" | 11 #include "core/layout/LayoutView.h" |
| 10 #include "core/page/FocusController.h" | 12 #include "core/page/FocusController.h" |
| 11 #include "core/paint/PaintLayer.h" | 13 #include "core/paint/PaintLayer.h" |
| 12 #include "platform/graphics/GraphicsLayer.h" | 14 #include "platform/graphics/GraphicsLayer.h" |
| 13 #include "platform/graphics/paint/RasterInvalidationTracking.h" | 15 #include "platform/graphics/paint/RasterInvalidationTracking.h" |
| 14 | 16 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 31 } | 33 } |
| 32 | 34 |
| 33 FrameSelection& selection() const { | 35 FrameSelection& selection() const { |
| 34 return document().view()->frame().selection(); | 36 return document().view()->frame().selection(); |
| 35 } | 37 } |
| 36 | 38 |
| 37 const DisplayItemClient& caretDisplayItemClient() const { | 39 const DisplayItemClient& caretDisplayItemClient() const { |
| 38 return selection().caretDisplayItemClientForTesting(); | 40 return selection().caretDisplayItemClientForTesting(); |
| 39 } | 41 } |
| 40 | 42 |
| 43 const LayoutBlock* caretLayoutBlock() const { |
| 44 return static_cast<const CaretDisplayItemClient&>(caretDisplayItemClient()) |
| 45 .m_layoutBlock; |
| 46 } |
| 47 |
| 48 const LayoutBlock* previousCaretLayoutBlock() const { |
| 49 return static_cast<const CaretDisplayItemClient&>(caretDisplayItemClient()) |
| 50 .m_previousLayoutBlock; |
| 51 } |
| 52 |
| 41 Text* appendTextNode(const String& data) { | 53 Text* appendTextNode(const String& data) { |
| 42 Text* text = document().createTextNode(data); | 54 Text* text = document().createTextNode(data); |
| 43 document().body()->appendChild(text); | 55 document().body()->appendChild(text); |
| 44 return text; | 56 return text; |
| 45 } | 57 } |
| 46 | 58 |
| 47 Element* appendBlock(const String& data) { | 59 Element* appendBlock(const String& data) { |
| 48 Element* block = document().createElement("div"); | 60 Element* block = document().createElement("div"); |
| 49 Text* text = document().createTextNode(data); | 61 Text* text = document().createTextNode(data); |
| 50 block->appendChild(text); | 62 block->appendChild(text); |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 EXPECT_EQ(enclosingIntRect(caretVisualRect2), (*rasterInvalidations)[1].rect); | 228 EXPECT_EQ(enclosingIntRect(caretVisualRect2), (*rasterInvalidations)[1].rect); |
| 217 EXPECT_EQ(block2, (*rasterInvalidations)[1].client); | 229 EXPECT_EQ(block2, (*rasterInvalidations)[1].client); |
| 218 EXPECT_EQ(PaintInvalidationCaret, (*rasterInvalidations)[1].reason); | 230 EXPECT_EQ(PaintInvalidationCaret, (*rasterInvalidations)[1].reason); |
| 219 | 231 |
| 220 objectInvalidations = | 232 objectInvalidations = |
| 221 document().view()->trackedObjectPaintInvalidationsAsJSON(); | 233 document().view()->trackedObjectPaintInvalidationsAsJSON(); |
| 222 ASSERT_EQ(2u, objectInvalidations->size()); | 234 ASSERT_EQ(2u, objectInvalidations->size()); |
| 223 document().view()->setTracksPaintInvalidations(false); | 235 document().view()->setTracksPaintInvalidations(false); |
| 224 } | 236 } |
| 225 | 237 |
| 238 TEST_F(CaretDisplayItemClientTest, UpdatePreviousLayoutBlock) { |
| 239 document().body()->setContentEditable("true", ASSERT_NO_EXCEPTION); |
| 240 document().page()->focusController().setActive(true); |
| 241 document().page()->focusController().setFocused(true); |
| 242 auto* blockElement1 = appendBlock("Block1"); |
| 243 auto* blockElement2 = appendBlock("Block2"); |
| 244 updateAllLifecyclePhases(); |
| 245 auto* block1 = toLayoutBlock(blockElement1->layoutObject()); |
| 246 auto* block2 = toLayoutBlock(blockElement2->layoutObject()); |
| 247 |
| 248 // Set caret into block2. |
| 249 document().body()->focus(); |
| 250 selection().setSelection(SelectionInDOMTree::Builder() |
| 251 .collapse(Position(blockElement2, 0)) |
| 252 .build()); |
| 253 document().view()->updateLifecycleToLayoutClean(); |
| 254 EXPECT_TRUE(block2->shouldPaintCursorCaret()); |
| 255 EXPECT_EQ(block2, caretLayoutBlock()); |
| 256 EXPECT_FALSE(block1->shouldPaintCursorCaret()); |
| 257 EXPECT_FALSE(previousCaretLayoutBlock()); |
| 258 |
| 259 // Move caret into block1. Should set previousCaretLayoutBlock to block2. |
| 260 selection().setSelection(SelectionInDOMTree::Builder() |
| 261 .collapse(Position(blockElement1, 0)) |
| 262 .build()); |
| 263 document().view()->updateLifecycleToLayoutClean(); |
| 264 EXPECT_TRUE(block1->shouldPaintCursorCaret()); |
| 265 EXPECT_EQ(block1, caretLayoutBlock()); |
| 266 EXPECT_FALSE(block2->shouldPaintCursorCaret()); |
| 267 EXPECT_EQ(block2, previousCaretLayoutBlock()); |
| 268 |
| 269 // Move caret into block2. Partial update should not change |
| 270 // previousCaretLayoutBlock. |
| 271 selection().setSelection(SelectionInDOMTree::Builder() |
| 272 .collapse(Position(blockElement2, 0)) |
| 273 .build()); |
| 274 document().view()->updateLifecycleToLayoutClean(); |
| 275 EXPECT_TRUE(block2->shouldPaintCursorCaret()); |
| 276 EXPECT_EQ(block2, caretLayoutBlock()); |
| 277 EXPECT_FALSE(block1->shouldPaintCursorCaret()); |
| 278 EXPECT_EQ(block2, previousCaretLayoutBlock()); |
| 279 |
| 280 // Remove block2. Should clear caretLayoutBlock and previousCaretLayoutBlock. |
| 281 blockElement2->parentNode()->removeChild(blockElement2); |
| 282 EXPECT_FALSE(caretLayoutBlock()); |
| 283 EXPECT_FALSE(previousCaretLayoutBlock()); |
| 284 |
| 285 // Set caret into block1. |
| 286 selection().setSelection(SelectionInDOMTree::Builder() |
| 287 .collapse(Position(blockElement1, 0)) |
| 288 .build()); |
| 289 updateAllLifecyclePhases(); |
| 290 // Remove selection. |
| 291 selection().setSelection(SelectionInDOMTree()); |
| 292 document().view()->updateLifecycleToLayoutClean(); |
| 293 EXPECT_EQ(block1, previousCaretLayoutBlock()); |
| 294 } |
| 295 |
| 226 TEST_F(CaretDisplayItemClientTest, CaretHideMoveAndShow) { | 296 TEST_F(CaretDisplayItemClientTest, CaretHideMoveAndShow) { |
| 227 document().body()->setContentEditable("true", ASSERT_NO_EXCEPTION); | 297 document().body()->setContentEditable("true", ASSERT_NO_EXCEPTION); |
| 228 document().page()->focusController().setActive(true); | 298 document().page()->focusController().setActive(true); |
| 229 document().page()->focusController().setFocused(true); | 299 document().page()->focusController().setFocused(true); |
| 230 | 300 |
| 231 Text* text = appendTextNode("Hello, World!"); | 301 Text* text = appendTextNode("Hello, World!"); |
| 232 document().body()->focus(); | 302 document().body()->focus(); |
| 233 updateAllLifecyclePhases(); | 303 updateAllLifecyclePhases(); |
| 234 const auto* block = toLayoutBlock(document().body()->layoutObject()); | 304 const auto* block = toLayoutBlock(document().body()->layoutObject()); |
| 235 | 305 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 265 auto objectInvalidations = | 335 auto objectInvalidations = |
| 266 document().view()->trackedObjectPaintInvalidationsAsJSON(); | 336 document().view()->trackedObjectPaintInvalidationsAsJSON(); |
| 267 ASSERT_EQ(1u, objectInvalidations->size()); | 337 ASSERT_EQ(1u, objectInvalidations->size()); |
| 268 String s; | 338 String s; |
| 269 JSONObject::cast(objectInvalidations->at(0))->get("object")->asString(&s); | 339 JSONObject::cast(objectInvalidations->at(0))->get("object")->asString(&s); |
| 270 EXPECT_EQ("Caret", s); | 340 EXPECT_EQ("Caret", s); |
| 271 document().view()->setTracksPaintInvalidations(false); | 341 document().view()->setTracksPaintInvalidations(false); |
| 272 } | 342 } |
| 273 | 343 |
| 274 } // namespace blink | 344 } // namespace blink |
| OLD | NEW |