| Index: third_party/WebKit/Source/core/paint/BlockPaintInvalidatorTest.cpp
 | 
| diff --git a/third_party/WebKit/Source/core/paint/BlockPaintInvalidatorTest.cpp b/third_party/WebKit/Source/core/paint/BlockPaintInvalidatorTest.cpp
 | 
| new file mode 100644
 | 
| index 0000000000000000000000000000000000000000..422491bf014c207d5740bbe1367a5c83327dceae
 | 
| --- /dev/null
 | 
| +++ b/third_party/WebKit/Source/core/paint/BlockPaintInvalidatorTest.cpp
 | 
| @@ -0,0 +1,138 @@
 | 
| +// Copyright 2017 The Chromium Authors. All rights reserved.
 | 
| +// Use of this source code is governed by a BSD-style license that can be
 | 
| +// found in the LICENSE file.
 | 
| +
 | 
| +#include "core/HTMLNames.h"
 | 
| +#include "core/editing/FrameSelection.h"
 | 
| +#include "core/editing/SelectionTemplate.h"
 | 
| +#include "core/frame/FrameView.h"
 | 
| +#include "core/layout/LayoutTestHelper.h"
 | 
| +#include "core/layout/LayoutView.h"
 | 
| +#include "core/paint/PaintLayer.h"
 | 
| +#include "platform/graphics/GraphicsLayer.h"
 | 
| +#include "platform/graphics/paint/RasterInvalidationTracking.h"
 | 
| +
 | 
| +namespace blink {
 | 
| +
 | 
| +class BlockPaintInvalidatorTest : public RenderingTest {
 | 
| + protected:
 | 
| +  void SetUp() override {
 | 
| +    RenderingTest::SetUp();
 | 
| +    enableCompositing();
 | 
| +  }
 | 
| +
 | 
| +  const RasterInvalidationTracking* getRasterInvalidationTracking() const {
 | 
| +    // TODO(wangxianzhu): Test SPv2.
 | 
| +    return layoutView()
 | 
| +        .layer()
 | 
| +        ->graphicsLayerBacking()
 | 
| +        ->getRasterInvalidationTracking();
 | 
| +  }
 | 
| +
 | 
| +  FrameSelection& selection() const {
 | 
| +    return document().view()->frame().selection();
 | 
| +  }
 | 
| +
 | 
| +  Text* appendTextNode(const String& data) {
 | 
| +    Text* text = document().createTextNode(data);
 | 
| +    document().body()->appendChild(text);
 | 
| +    return text;
 | 
| +  }
 | 
| +};
 | 
| +
 | 
| +TEST_F(BlockPaintInvalidatorTest, CaretPaintInvalidation) {
 | 
| +  document().body()->setContentEditable("true", ASSERT_NO_EXCEPTION);
 | 
| +  Text* text = appendTextNode("Hello, World!");
 | 
| +  document().view()->updateAllLifecyclePhases();
 | 
| +
 | 
| +  // Focus the body. Should invalidate the new caret.
 | 
| +  document().view()->setTracksPaintInvalidations(true);
 | 
| +  document().body()->focus();
 | 
| +  LayoutBlock* caretLayoutBlock = selection().caretLayoutBlock();
 | 
| +  EXPECT_EQ(document().body()->layoutObject(), caretLayoutBlock);
 | 
| +  EXPECT_TRUE(caretLayoutBlock->hasCursorCaret());
 | 
| +  EXPECT_TRUE(caretLayoutBlock->mayNeedPaintInvalidation());
 | 
| +
 | 
| +  document().view()->updateAllLifecyclePhases();
 | 
| +  EXPECT_EQ(caretLayoutBlock, selection().caretLayoutBlock());
 | 
| +  EXPECT_FALSE(caretLayoutBlock->mayNeedPaintInvalidation());
 | 
| +
 | 
| +  LayoutRect caretRect = selection().caretLocalRect();
 | 
| +  caretRect.moveBy(caretLayoutBlock->location());
 | 
| +  EXPECT_EQ(caretRect, selection().getCaretDisplayItemClient().visualRect());
 | 
| +
 | 
| +  const auto* rasterInvalidations =
 | 
| +      &getRasterInvalidationTracking()->trackedRasterInvalidations;
 | 
| +  ASSERT_EQ(1u, rasterInvalidations->size());
 | 
| +  EXPECT_EQ(enclosingIntRect(caretRect), (*rasterInvalidations)[0].rect);
 | 
| +  EXPECT_EQ(caretLayoutBlock, (*rasterInvalidations)[0].client);
 | 
| +  EXPECT_EQ(PaintInvalidationCaret, (*rasterInvalidations)[0].reason);
 | 
| +
 | 
| +  std::unique_ptr<JSONArray> objectInvalidations =
 | 
| +      document().view()->trackedObjectPaintInvalidationsAsJSON();
 | 
| +  ASSERT_EQ(1u, objectInvalidations->size());
 | 
| +  String s;
 | 
| +  JSONObject::cast(objectInvalidations->at(0))->get("object")->asString(&s);
 | 
| +  EXPECT_EQ("Caret", s);
 | 
| +  document().view()->setTracksPaintInvalidations(false);
 | 
| +
 | 
| +  // Move the caret to the end of the text. Should invalidate both the old and
 | 
| +  // new carets.
 | 
| +  document().view()->setTracksPaintInvalidations(true);
 | 
| +  selection().setSelection(
 | 
| +      SelectionInDOMTree::Builder().collapse(Position(text, 5)).build());
 | 
| +  caretLayoutBlock = selection().caretLayoutBlock();
 | 
| +  EXPECT_EQ(document().body()->layoutObject(), caretLayoutBlock);
 | 
| +  EXPECT_TRUE(caretLayoutBlock->hasCursorCaret());
 | 
| +  EXPECT_TRUE(caretLayoutBlock->mayNeedPaintInvalidation());
 | 
| +
 | 
| +  document().view()->updateAllLifecyclePhases();
 | 
| +  EXPECT_EQ(caretLayoutBlock, selection().caretLayoutBlock());
 | 
| +  EXPECT_FALSE(caretLayoutBlock->mayNeedPaintInvalidation());
 | 
| +
 | 
| +  LayoutRect newCaretRect = selection().caretLocalRect();
 | 
| +  newCaretRect.moveBy(caretLayoutBlock->location());
 | 
| +  EXPECT_EQ(newCaretRect, selection().getCaretDisplayItemClient().visualRect());
 | 
| +
 | 
| +  rasterInvalidations =
 | 
| +      &getRasterInvalidationTracking()->trackedRasterInvalidations;
 | 
| +  ASSERT_EQ(2u, rasterInvalidations->size());
 | 
| +  EXPECT_EQ(enclosingIntRect(caretRect), (*rasterInvalidations)[0].rect);
 | 
| +  EXPECT_EQ(caretLayoutBlock, (*rasterInvalidations)[0].client);
 | 
| +  EXPECT_EQ(PaintInvalidationCaret, (*rasterInvalidations)[0].reason);
 | 
| +  EXPECT_EQ(enclosingIntRect(newCaretRect), (*rasterInvalidations)[1].rect);
 | 
| +  EXPECT_EQ(caretLayoutBlock, (*rasterInvalidations)[1].client);
 | 
| +  EXPECT_EQ(PaintInvalidationCaret, (*rasterInvalidations)[1].reason);
 | 
| +
 | 
| +  objectInvalidations =
 | 
| +      document().view()->trackedObjectPaintInvalidationsAsJSON();
 | 
| +  ASSERT_EQ(1u, objectInvalidations->size());
 | 
| +  JSONObject::cast(objectInvalidations->at(0))->get("object")->asString(&s);
 | 
| +  EXPECT_EQ("Caret", s);
 | 
| +  document().view()->setTracksPaintInvalidations(false);
 | 
| +
 | 
| +  // Remove selection. Should invalidate the old caret.
 | 
| +  LayoutRect oldCaretRect = newCaretRect;
 | 
| +  LayoutBlock* oldCaretLayoutBlock = caretLayoutBlock;
 | 
| +  document().view()->setTracksPaintInvalidations(true);
 | 
| +  selection().setSelection(SelectionInDOMTree());
 | 
| +  EXPECT_FALSE(selection().caretLayoutBlock());
 | 
| +
 | 
| +  document().view()->updateAllLifecyclePhases();
 | 
| +  EXPECT_FALSE(selection().caretLayoutBlock());
 | 
| +
 | 
| +  rasterInvalidations =
 | 
| +      &getRasterInvalidationTracking()->trackedRasterInvalidations;
 | 
| +  ASSERT_EQ(1u, rasterInvalidations->size());
 | 
| +  EXPECT_EQ(enclosingIntRect(oldCaretRect), (*rasterInvalidations)[0].rect);
 | 
| +  EXPECT_EQ(oldCaretLayoutBlock, (*rasterInvalidations)[0].client);
 | 
| +
 | 
| +  objectInvalidations =
 | 
| +      document().view()->trackedObjectPaintInvalidationsAsJSON();
 | 
| +  ASSERT_EQ(1u, objectInvalidations->size());
 | 
| +  JSONObject::cast(objectInvalidations->at(0))->get("object")->asString(&s);
 | 
| +  EXPECT_EQ("Caret", s);
 | 
| +  document().view()->setTracksPaintInvalidations(false);
 | 
| +}
 | 
| +
 | 
| +}  // namespace blink
 | 
| 
 |