OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "core/HTMLNames.h" |
| 6 #include "core/editing/FrameSelection.h" |
| 7 #include "core/frame/FrameView.h" |
| 8 #include "core/layout/LayoutTestHelper.h" |
| 9 #include "core/layout/LayoutView.h" |
| 10 #include "core/page/FocusController.h" |
| 11 #include "core/paint/PaintLayer.h" |
| 12 #include "platform/graphics/GraphicsLayer.h" |
| 13 #include "platform/graphics/paint/RasterInvalidationTracking.h" |
| 14 |
| 15 namespace blink { |
| 16 |
| 17 class BlockPaintInvalidatorTest : public RenderingTest { |
| 18 protected: |
| 19 void SetUp() override { |
| 20 RenderingTest::SetUp(); |
| 21 enableCompositing(); |
| 22 } |
| 23 |
| 24 const RasterInvalidationTracking* getRasterInvalidationTracking() const { |
| 25 // TODO(wangxianzhu): Test SPv2. |
| 26 return layoutView() |
| 27 .layer() |
| 28 ->graphicsLayerBacking() |
| 29 ->getRasterInvalidationTracking(); |
| 30 } |
| 31 |
| 32 FrameSelection& selection() const { |
| 33 return document().view()->frame().selection(); |
| 34 } |
| 35 |
| 36 const DisplayItemClient& caretDisplayItemClient() const { |
| 37 return selection().caretDisplayItemClientForTesting(); |
| 38 } |
| 39 |
| 40 Text* appendTextNode(const String& data) { |
| 41 Text* text = document().createTextNode(data); |
| 42 document().body()->appendChild(text); |
| 43 return text; |
| 44 } |
| 45 }; |
| 46 |
| 47 TEST_F(BlockPaintInvalidatorTest, CaretPaintInvalidation) { |
| 48 document().body()->setContentEditable("true", ASSERT_NO_EXCEPTION); |
| 49 document().page()->focusController().setActive(true); |
| 50 document().page()->focusController().setFocused(true); |
| 51 |
| 52 Text* text = appendTextNode("Hello, World!"); |
| 53 document().view()->updateAllLifecyclePhases(); |
| 54 const auto* block = toLayoutBlock(document().body()->layoutObject()); |
| 55 |
| 56 // Focus the body. Should invalidate the new caret. |
| 57 document().view()->setTracksPaintInvalidations(true); |
| 58 document().body()->focus(); |
| 59 document().view()->updateAllLifecyclePhases(); |
| 60 EXPECT_TRUE(block->shouldPaintCursorCaret()); |
| 61 |
| 62 LayoutRect caretVisualRect = caretDisplayItemClient().visualRect(); |
| 63 EXPECT_EQ(1, caretVisualRect.width()); |
| 64 EXPECT_EQ(block->location(), caretVisualRect.location()); |
| 65 |
| 66 const auto* rasterInvalidations = |
| 67 &getRasterInvalidationTracking()->trackedRasterInvalidations; |
| 68 ASSERT_EQ(1u, rasterInvalidations->size()); |
| 69 EXPECT_EQ(enclosingIntRect(caretVisualRect), (*rasterInvalidations)[0].rect); |
| 70 EXPECT_EQ(block, (*rasterInvalidations)[0].client); |
| 71 EXPECT_EQ(PaintInvalidationCaret, (*rasterInvalidations)[0].reason); |
| 72 |
| 73 std::unique_ptr<JSONArray> objectInvalidations = |
| 74 document().view()->trackedObjectPaintInvalidationsAsJSON(); |
| 75 ASSERT_EQ(1u, objectInvalidations->size()); |
| 76 String s; |
| 77 JSONObject::cast(objectInvalidations->at(0))->get("object")->asString(&s); |
| 78 EXPECT_EQ("Caret", s); |
| 79 document().view()->setTracksPaintInvalidations(false); |
| 80 |
| 81 // Move the caret to the end of the text. Should invalidate both the old and |
| 82 // new carets. |
| 83 document().view()->setTracksPaintInvalidations(true); |
| 84 selection().setSelection( |
| 85 SelectionInDOMTree::Builder().collapse(Position(text, 5)).build()); |
| 86 document().view()->updateAllLifecyclePhases(); |
| 87 EXPECT_TRUE(block->shouldPaintCursorCaret()); |
| 88 |
| 89 LayoutRect newCaretVisualRect = caretDisplayItemClient().visualRect(); |
| 90 EXPECT_EQ(caretVisualRect.size(), newCaretVisualRect.size()); |
| 91 EXPECT_EQ(caretVisualRect.y(), newCaretVisualRect.y()); |
| 92 EXPECT_LT(caretVisualRect.x(), newCaretVisualRect.x()); |
| 93 |
| 94 rasterInvalidations = |
| 95 &getRasterInvalidationTracking()->trackedRasterInvalidations; |
| 96 ASSERT_EQ(2u, rasterInvalidations->size()); |
| 97 EXPECT_EQ(enclosingIntRect(caretVisualRect), (*rasterInvalidations)[0].rect); |
| 98 EXPECT_EQ(block, (*rasterInvalidations)[0].client); |
| 99 EXPECT_EQ(PaintInvalidationCaret, (*rasterInvalidations)[0].reason); |
| 100 EXPECT_EQ(enclosingIntRect(newCaretVisualRect), |
| 101 (*rasterInvalidations)[1].rect); |
| 102 EXPECT_EQ(block, (*rasterInvalidations)[1].client); |
| 103 EXPECT_EQ(PaintInvalidationCaret, (*rasterInvalidations)[1].reason); |
| 104 |
| 105 objectInvalidations = |
| 106 document().view()->trackedObjectPaintInvalidationsAsJSON(); |
| 107 ASSERT_EQ(1u, objectInvalidations->size()); |
| 108 JSONObject::cast(objectInvalidations->at(0))->get("object")->asString(&s); |
| 109 EXPECT_EQ("Caret", s); |
| 110 document().view()->setTracksPaintInvalidations(false); |
| 111 |
| 112 // Remove selection. Should invalidate the old caret. |
| 113 LayoutRect oldCaretVisualRect = newCaretVisualRect; |
| 114 document().view()->setTracksPaintInvalidations(true); |
| 115 selection().setSelection(SelectionInDOMTree()); |
| 116 document().view()->updateAllLifecyclePhases(); |
| 117 EXPECT_FALSE(block->shouldPaintCursorCaret()); |
| 118 EXPECT_EQ(LayoutRect(), caretDisplayItemClient().visualRect()); |
| 119 |
| 120 rasterInvalidations = |
| 121 &getRasterInvalidationTracking()->trackedRasterInvalidations; |
| 122 ASSERT_EQ(1u, rasterInvalidations->size()); |
| 123 EXPECT_EQ(enclosingIntRect(oldCaretVisualRect), |
| 124 (*rasterInvalidations)[0].rect); |
| 125 EXPECT_EQ(block, (*rasterInvalidations)[0].client); |
| 126 |
| 127 objectInvalidations = |
| 128 document().view()->trackedObjectPaintInvalidationsAsJSON(); |
| 129 ASSERT_EQ(1u, objectInvalidations->size()); |
| 130 JSONObject::cast(objectInvalidations->at(0))->get("object")->asString(&s); |
| 131 EXPECT_EQ("Caret", s); |
| 132 document().view()->setTracksPaintInvalidations(false); |
| 133 } |
| 134 |
| 135 } // namespace blink |
OLD | NEW |