| 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/HTMLNames.h" | 5 #include "core/HTMLNames.h" |
| 6 #include "core/editing/FrameSelection.h" | 6 #include "core/editing/FrameSelection.h" |
| 7 #include "core/frame/FrameView.h" | 7 #include "core/frame/FrameView.h" |
| 8 #include "core/layout/LayoutTestHelper.h" | 8 #include "core/layout/LayoutTestHelper.h" |
| 9 #include "core/layout/LayoutView.h" | 9 #include "core/layout/LayoutView.h" |
| 10 #include "core/page/FocusController.h" | 10 #include "core/page/FocusController.h" |
| 11 #include "core/paint/PaintLayer.h" | 11 #include "core/paint/PaintLayer.h" |
| 12 #include "platform/graphics/GraphicsLayer.h" | 12 #include "platform/graphics/GraphicsLayer.h" |
| 13 #include "platform/graphics/paint/RasterInvalidationTracking.h" | 13 #include "platform/graphics/paint/RasterInvalidationTracking.h" |
| 14 | 14 |
| 15 namespace blink { | 15 namespace blink { |
| 16 | 16 |
| 17 class BlockPaintInvalidatorTest : public RenderingTest { | 17 class CaretDisplayItemClientTest : public RenderingTest { |
| 18 protected: | 18 protected: |
| 19 void SetUp() override { | 19 void SetUp() override { |
| 20 RenderingTest::SetUp(); | 20 RenderingTest::SetUp(); |
| 21 enableCompositing(); | 21 enableCompositing(); |
| 22 } | 22 } |
| 23 | 23 |
| 24 const RasterInvalidationTracking* getRasterInvalidationTracking() const { | 24 const RasterInvalidationTracking* getRasterInvalidationTracking() const { |
| 25 // TODO(wangxianzhu): Test SPv2. | 25 // TODO(wangxianzhu): Test SPv2. |
| 26 return layoutView() | 26 return layoutView() |
| 27 .layer() | 27 .layer() |
| 28 ->graphicsLayerBacking() | 28 ->graphicsLayerBacking() |
| 29 ->getRasterInvalidationTracking(); | 29 ->getRasterInvalidationTracking(); |
| 30 } | 30 } |
| 31 | 31 |
| 32 FrameSelection& selection() const { | 32 FrameSelection& selection() const { |
| 33 return document().view()->frame().selection(); | 33 return document().view()->frame().selection(); |
| 34 } | 34 } |
| 35 | 35 |
| 36 const DisplayItemClient& caretDisplayItemClient() const { | 36 const DisplayItemClient& caretDisplayItemClient() const { |
| 37 return selection().caretDisplayItemClientForTesting(); | 37 return selection().caretDisplayItemClientForTesting(); |
| 38 } | 38 } |
| 39 | 39 |
| 40 Text* appendTextNode(const String& data) { | 40 Text* appendTextNode(const String& data) { |
| 41 Text* text = document().createTextNode(data); | 41 Text* text = document().createTextNode(data); |
| 42 document().body()->appendChild(text); | 42 document().body()->appendChild(text); |
| 43 return text; | 43 return text; |
| 44 } | 44 } |
| 45 |
| 46 Element* appendBlock(const String& data) { |
| 47 Element* block = document().createElement("div"); |
| 48 Text* text = document().createTextNode(data); |
| 49 block->appendChild(text); |
| 50 document().body()->appendChild(block); |
| 51 return block; |
| 52 } |
| 45 }; | 53 }; |
| 46 | 54 |
| 47 TEST_F(BlockPaintInvalidatorTest, CaretPaintInvalidation) { | 55 TEST_F(CaretDisplayItemClientTest, CaretPaintInvalidation) { |
| 48 document().body()->setContentEditable("true", ASSERT_NO_EXCEPTION); | 56 document().body()->setContentEditable("true", ASSERT_NO_EXCEPTION); |
| 49 document().page()->focusController().setActive(true); | 57 document().page()->focusController().setActive(true); |
| 50 document().page()->focusController().setFocused(true); | 58 document().page()->focusController().setFocused(true); |
| 51 | 59 |
| 52 Text* text = appendTextNode("Hello, World!"); | 60 Text* text = appendTextNode("Hello, World!"); |
| 53 document().view()->updateAllLifecyclePhases(); | 61 document().view()->updateAllLifecyclePhases(); |
| 54 const auto* block = toLayoutBlock(document().body()->layoutObject()); | 62 const auto* block = toLayoutBlock(document().body()->layoutObject()); |
| 55 | 63 |
| 56 // Focus the body. Should invalidate the new caret. | 64 // Focus the body. Should invalidate the new caret. |
| 57 document().view()->setTracksPaintInvalidations(true); | 65 document().view()->setTracksPaintInvalidations(true); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 EXPECT_EQ(block, (*rasterInvalidations)[0].client); | 133 EXPECT_EQ(block, (*rasterInvalidations)[0].client); |
| 126 | 134 |
| 127 objectInvalidations = | 135 objectInvalidations = |
| 128 document().view()->trackedObjectPaintInvalidationsAsJSON(); | 136 document().view()->trackedObjectPaintInvalidationsAsJSON(); |
| 129 ASSERT_EQ(1u, objectInvalidations->size()); | 137 ASSERT_EQ(1u, objectInvalidations->size()); |
| 130 JSONObject::cast(objectInvalidations->at(0))->get("object")->asString(&s); | 138 JSONObject::cast(objectInvalidations->at(0))->get("object")->asString(&s); |
| 131 EXPECT_EQ("Caret", s); | 139 EXPECT_EQ("Caret", s); |
| 132 document().view()->setTracksPaintInvalidations(false); | 140 document().view()->setTracksPaintInvalidations(false); |
| 133 } | 141 } |
| 134 | 142 |
| 143 TEST_F(CaretDisplayItemClientTest, CaretMovesBetweenBlocks) { |
| 144 document().body()->setContentEditable("true", ASSERT_NO_EXCEPTION); |
| 145 document().page()->focusController().setActive(true); |
| 146 document().page()->focusController().setFocused(true); |
| 147 auto* blockElement1 = appendBlock("Block1"); |
| 148 auto* blockElement2 = appendBlock("Block2"); |
| 149 document().view()->updateAllLifecyclePhases(); |
| 150 auto* block1 = toLayoutBlock(blockElement1->layoutObject()); |
| 151 auto* block2 = toLayoutBlock(blockElement2->layoutObject()); |
| 152 |
| 153 // Focus the body. |
| 154 document().body()->focus(); |
| 155 document().view()->updateAllLifecyclePhases(); |
| 156 LayoutRect caretVisualRect1 = caretDisplayItemClient().visualRect(); |
| 157 EXPECT_EQ(1, caretVisualRect1.width()); |
| 158 EXPECT_EQ(block1->visualRect().location(), caretVisualRect1.location()); |
| 159 EXPECT_TRUE(block1->shouldPaintCursorCaret()); |
| 160 EXPECT_FALSE(block2->shouldPaintCursorCaret()); |
| 161 |
| 162 // Move the caret into block2. Should invalidate both the old and new carets. |
| 163 document().view()->setTracksPaintInvalidations(true); |
| 164 selection().setSelection(SelectionInDOMTree::Builder() |
| 165 .collapse(Position(blockElement2, 0)) |
| 166 .build()); |
| 167 document().view()->updateAllLifecyclePhases(); |
| 168 |
| 169 LayoutRect caretVisualRect2 = caretDisplayItemClient().visualRect(); |
| 170 EXPECT_EQ(1, caretVisualRect2.width()); |
| 171 EXPECT_EQ(block2->visualRect().location(), caretVisualRect2.location()); |
| 172 EXPECT_FALSE(block1->shouldPaintCursorCaret()); |
| 173 EXPECT_TRUE(block2->shouldPaintCursorCaret()); |
| 174 |
| 175 const auto* rasterInvalidations = |
| 176 &getRasterInvalidationTracking()->trackedRasterInvalidations; |
| 177 ASSERT_EQ(2u, rasterInvalidations->size()); |
| 178 EXPECT_EQ(enclosingIntRect(caretVisualRect1), (*rasterInvalidations)[0].rect); |
| 179 EXPECT_EQ(block1, (*rasterInvalidations)[0].client); |
| 180 EXPECT_EQ(PaintInvalidationCaret, (*rasterInvalidations)[0].reason); |
| 181 EXPECT_EQ(enclosingIntRect(caretVisualRect2), (*rasterInvalidations)[1].rect); |
| 182 EXPECT_EQ(block2, (*rasterInvalidations)[1].client); |
| 183 EXPECT_EQ(PaintInvalidationCaret, (*rasterInvalidations)[1].reason); |
| 184 |
| 185 std::unique_ptr<JSONArray> objectInvalidations = |
| 186 document().view()->trackedObjectPaintInvalidationsAsJSON(); |
| 187 ASSERT_EQ(2u, objectInvalidations->size()); |
| 188 document().view()->setTracksPaintInvalidations(false); |
| 189 |
| 190 // Move the caret back into block1. |
| 191 document().view()->setTracksPaintInvalidations(true); |
| 192 selection().setSelection(SelectionInDOMTree::Builder() |
| 193 .collapse(Position(blockElement1, 0)) |
| 194 .build()); |
| 195 document().view()->updateAllLifecyclePhases(); |
| 196 |
| 197 EXPECT_EQ(caretVisualRect1, caretDisplayItemClient().visualRect()); |
| 198 EXPECT_TRUE(block1->shouldPaintCursorCaret()); |
| 199 EXPECT_FALSE(block2->shouldPaintCursorCaret()); |
| 200 |
| 201 rasterInvalidations = |
| 202 &getRasterInvalidationTracking()->trackedRasterInvalidations; |
| 203 ASSERT_EQ(2u, rasterInvalidations->size()); |
| 204 EXPECT_EQ(enclosingIntRect(caretVisualRect1), (*rasterInvalidations)[0].rect); |
| 205 EXPECT_EQ(block1, (*rasterInvalidations)[0].client); |
| 206 EXPECT_EQ(PaintInvalidationCaret, (*rasterInvalidations)[0].reason); |
| 207 EXPECT_EQ(enclosingIntRect(caretVisualRect2), (*rasterInvalidations)[1].rect); |
| 208 EXPECT_EQ(block2, (*rasterInvalidations)[1].client); |
| 209 EXPECT_EQ(PaintInvalidationCaret, (*rasterInvalidations)[1].reason); |
| 210 |
| 211 objectInvalidations = |
| 212 document().view()->trackedObjectPaintInvalidationsAsJSON(); |
| 213 ASSERT_EQ(2u, objectInvalidations->size()); |
| 214 document().view()->setTracksPaintInvalidations(false); |
| 215 } |
| 216 |
| 135 } // namespace blink | 217 } // namespace blink |
| OLD | NEW |