OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/paint/SVGInlineTextBoxPainter.h" |
| 6 |
| 7 #include "core/dom/Document.h" |
| 8 #include "core/dom/Range.h" |
| 9 #include "core/editing/DOMSelection.h" |
| 10 #include "core/frame/LocalDOMWindow.h" |
| 11 #include "core/layout/LayoutTestHelper.h" |
| 12 #include "core/layout/line/InlineTextBox.h" |
| 13 #include "core/layout/svg/LayoutSVGInlineText.h" |
| 14 #include "core/layout/svg/LayoutSVGText.h" |
| 15 #include "core/paint/PaintLayer.h" |
| 16 #include "platform/geometry/IntRectOutsets.h" |
| 17 #include "platform/graphics/GraphicsLayer.h" |
| 18 #include "platform/graphics/paint/DisplayItemList.h" |
| 19 #include "platform/graphics/paint/DrawingDisplayItem.h" |
| 20 #include "platform/graphics/paint/PaintController.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 |
| 23 namespace blink { |
| 24 namespace { |
| 25 |
| 26 class SVGInlineTextBoxPainterTest : public RenderingTest { |
| 27 public: |
| 28 const DrawingDisplayItem* getDrawingForSVGTextById(const char* elementName) |
| 29 { |
| 30 // Look up the inline text box that serves as the display item client fo
r the painted text. |
| 31 LayoutSVGText* targetSVGText = toLayoutSVGText( |
| 32 document().getElementById(AtomicString(elementName))->layoutObject()
); |
| 33 LayoutSVGInlineText* targetInlineText = targetSVGText->descendantTextNod
es()[0]; |
| 34 const DisplayItemClient* targetClient = static_cast<const DisplayItemCli
ent*>(targetInlineText->firstTextBox()); |
| 35 |
| 36 // Find the appropriate drawing in the display item list. |
| 37 const DisplayItemList& displayItemList = rootPaintController().getDispla
yItemList(); |
| 38 for (size_t i = 0; i < displayItemList.size(); i++) { |
| 39 if (displayItemList[i].client() == *targetClient) |
| 40 return static_cast<const DrawingDisplayItem*>(&displayItemList[i
]); |
| 41 } |
| 42 |
| 43 return nullptr; |
| 44 } |
| 45 |
| 46 void selectAllText() |
| 47 { |
| 48 Range* range = document().createRange(); |
| 49 range->selectNode(document().documentElement()); |
| 50 LocalDOMWindow* window = document().domWindow(); |
| 51 DOMSelection* selection = window->getSelection(); |
| 52 selection->removeAllRanges(); |
| 53 selection->addRange(range); |
| 54 } |
| 55 |
| 56 private: |
| 57 PaintController& rootPaintController() |
| 58 { |
| 59 return document().view()->layoutView()->layer()->graphicsLayerBacking()-
>getPaintController(); |
| 60 } |
| 61 |
| 62 void SetUp() override |
| 63 { |
| 64 RenderingTest::SetUp(); |
| 65 enableCompositing(); |
| 66 } |
| 67 }; |
| 68 |
| 69 static void assertTextDrawingEquals(const DrawingDisplayItem* drawingDisplayItem
, const char* str) |
| 70 { |
| 71 ASSERT_EQ(str, static_cast<const InlineTextBox*>(&drawingDisplayItem->client
())->text()); |
| 72 } |
| 73 |
| 74 const static int kAllowedPixelDelta = 8; |
| 75 |
| 76 static void assertCullRectEquals(const DrawingDisplayItem* drawingDisplayItem, c
onst IntRect& expectedRect) |
| 77 { |
| 78 // Text metrics can vary slightly across platforms, so we allow for a small
pixel difference. |
| 79 IntRect outerRect(expectedRect); |
| 80 int delta = kAllowedPixelDelta / 2; |
| 81 outerRect.expand(IntRectOutsets(delta, delta, delta, delta)); |
| 82 IntRect innerRect(expectedRect); |
| 83 // Rect contains is inclusive of edge, so shrink by one extra pixel. |
| 84 int contractDelta = -delta - 1; |
| 85 innerRect.expand(IntRectOutsets(contractDelta, contractDelta, contractDelta,
contractDelta)); |
| 86 |
| 87 IntRect actualRect(IntRect(drawingDisplayItem->picture()->cullRect())); |
| 88 ASSERT_TRUE(outerRect.contains(actualRect) && !innerRect.contains(actualRect
)) |
| 89 << "Cull rect not approximately equal [expected=(" |
| 90 << expectedRect.x() << "," << expectedRect.y() << " " << expectedRect.wi
dth() << "x" << expectedRect.height() << "), actual=(" |
| 91 << actualRect.x() << "," << actualRect.y() << " " << actualRect.width()
<< "x" << actualRect.height() << ")]."; |
| 92 } |
| 93 |
| 94 TEST_F(SVGInlineTextBoxPainterTest, TextCullRect_DefaultWritingMode) |
| 95 { |
| 96 setBodyInnerHTML( |
| 97 "<svg width='400px' height='400px' font-family='Arial' font-size='30'>" |
| 98 "<text id='target' x='50' y='30'>x</text>" |
| 99 "</svg>"); |
| 100 document().view()->updateAllLifecyclePhases(); |
| 101 |
| 102 const DrawingDisplayItem* drawingDisplayItem = getDrawingForSVGTextById("tar
get"); |
| 103 assertTextDrawingEquals(drawingDisplayItem, "x"); |
| 104 assertCullRectEquals(drawingDisplayItem, IntRect(50, 3, 15, 33)); |
| 105 |
| 106 selectAllText(); |
| 107 document().view()->updateAllLifecyclePhases(); |
| 108 |
| 109 drawingDisplayItem = getDrawingForSVGTextById("target"); |
| 110 assertTextDrawingEquals(drawingDisplayItem, "x"); |
| 111 assertCullRectEquals(drawingDisplayItem, IntRect(50, 3, 15, 33)); |
| 112 } |
| 113 |
| 114 TEST_F(SVGInlineTextBoxPainterTest, TextCullRect_WritingModeTopToBottom) |
| 115 { |
| 116 setBodyInnerHTML( |
| 117 "<svg width='400px' height='400px' font-family='Arial' font-size='30'>" |
| 118 "<text id='target' x='50' y='30' writing-mode='tb'>x</text>" |
| 119 "</svg>"); |
| 120 document().view()->updateAllLifecyclePhases(); |
| 121 |
| 122 const DrawingDisplayItem* drawingDisplayItem = getDrawingForSVGTextById("tar
get"); |
| 123 assertTextDrawingEquals(drawingDisplayItem, "x"); |
| 124 assertCullRectEquals(drawingDisplayItem, IntRect(33, 30, 34, 15)); |
| 125 |
| 126 selectAllText(); |
| 127 document().view()->updateAllLifecyclePhases(); |
| 128 |
| 129 // The selection rect is one pixel taller due to sub-pixel difference |
| 130 // between the text bounds and selection bounds in combination with use of |
| 131 // enclosingIntRect() in SVGInlineTextBox::localSelectionRect(). |
| 132 drawingDisplayItem = getDrawingForSVGTextById("target"); |
| 133 assertTextDrawingEquals(drawingDisplayItem, "x"); |
| 134 assertCullRectEquals(drawingDisplayItem, IntRect(33, 30, 34, 16)); |
| 135 } |
| 136 |
| 137 } // namespace |
| 138 } // namespace blink |
OLD | NEW |