Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(165)

Side by Side Diff: third_party/WebKit/Source/core/paint/SVGInlineTextBoxPainterTest.cpp

Issue 2137753002: Calculate correct cull rect for SVG inline text boxes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Approximate expected rects. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/core/paint/SVGInlineTextBoxPainter.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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/graphics/GraphicsLayer.h"
17 #include "platform/graphics/paint/DisplayItemList.h"
18 #include "platform/graphics/paint/DrawingDisplayItem.h"
19 #include "platform/graphics/paint/PaintController.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 namespace blink {
23 namespace {
24
25 class SVGInlineTextBoxPainterTest : public RenderingTest {
26 public:
27 const DrawingDisplayItem* getDrawingForSVGTextById(const char* elementName)
28 {
29 // Look up the inline text box that serves as the display item client fo r the painted text.
30 LayoutSVGText* targetSVGText = toLayoutSVGText(
31 document().getElementById(AtomicString(elementName))->layoutObject() );
32 LayoutSVGInlineText* targetInlineText = targetSVGText->descendantTextNod es()[0];
33 const DisplayItemClient* targetClient = static_cast<const DisplayItemCli ent*>(targetInlineText->firstTextBox());
34
35 // Find the appropriate drawing in the display item list.
36 const DisplayItemList& displayItemList = rootPaintController().getDispla yItemList();
37 for (size_t i = 0; i < displayItemList.size(); i++) {
38 if (displayItemList[i].client() == *targetClient)
39 return static_cast<const DrawingDisplayItem*>(&displayItemList[i ]);
40 }
41
42 return nullptr;
43 }
44
45 void selectAllText()
46 {
47 Range* range = document().createRange();
48 range->selectNode(document().documentElement());
49 LocalDOMWindow* window = document().domWindow();
50 DOMSelection* selection = window->getSelection();
51 selection->removeAllRanges();
52 selection->addRange(range);
53 }
54
55 private:
56 PaintController& rootPaintController()
57 {
58 return document().view()->layoutView()->layer()->graphicsLayerBacking()- >getPaintController();
59 }
60
61 void SetUp() override
62 {
63 RenderingTest::SetUp();
64 enableCompositing();
65 }
66 };
67
68 static void assertTextDrawingEquals(const DrawingDisplayItem* drawingDisplayItem , const char* str)
69 {
70 ASSERT_EQ(str, static_cast<const InlineTextBox*>(&drawingDisplayItem->client ())->text());
71 }
72
73 const static int kAllowedPixelDelta = 4;
74
75 static void assertCullRectEquals(const DrawingDisplayItem* drawingDisplayItem, c onst IntRect& expectedRect)
76 {
77 // Text metrics can vary slightly across platforms, so we allow for a small pixel difference.
78 IntRect outerRect(expectedRect);
79 int delta = kAllowedPixelDelta / 2;
80 outerRect.expand(delta, delta);
81 IntRect innerRect(expectedRect);
82 // Rect contains is inclusive of edge, so shrink by one extra pixel.
83 innerRect.contract(delta + 1, delta + 1);
84
85 IntRect actualRect(IntRect(drawingDisplayItem->picture()->cullRect()));
86 ASSERT_TRUE(outerRect.contains(actualRect) && !innerRect.contains(actualRect ))
87 << "Cull rect not approximately equal to " << expectedRect.toString();
88 }
89
90 TEST_F(SVGInlineTextBoxPainterTest, TextCullRect_DefaultWritingMode)
91 {
92 setBodyInnerHTML(
93 "<svg width='400px' height='400px' font-family='Arial' font-size='30'>"
94 "<text id='target' x='50' y='30'>x</text>"
95 "</svg>");
96 document().view()->updateAllLifecyclePhases();
97
98 const DrawingDisplayItem* drawingDisplayItem = getDrawingForSVGTextById("tar get");
99 assertTextDrawingEquals(drawingDisplayItem, "x");
100 assertCullRectEquals(drawingDisplayItem, IntRect(50, 3, 15, 33));
101
102 selectAllText();
103 document().view()->updateAllLifecyclePhases();
104
105 drawingDisplayItem = getDrawingForSVGTextById("target");
106 assertTextDrawingEquals(drawingDisplayItem, "x");
107 assertCullRectEquals(drawingDisplayItem, IntRect(50, 3, 15, 33));
108 }
109
110 TEST_F(SVGInlineTextBoxPainterTest, TextCullRect_WritingModeTopToBottom)
111 {
112 setBodyInnerHTML(
113 "<svg width='400px' height='400px' font-family='Arial' font-size='30'>"
114 "<text id='target' x='50' y='30' writing-mode='tb'>x</text>"
115 "</svg>");
116 document().view()->updateAllLifecyclePhases();
117
118 const DrawingDisplayItem* drawingDisplayItem = getDrawingForSVGTextById("tar get");
119 assertTextDrawingEquals(drawingDisplayItem, "x");
120 assertCullRectEquals(drawingDisplayItem, IntRect(33, 30, 34, 15));
121
122 selectAllText();
123 document().view()->updateAllLifecyclePhases();
124
125 // The selection rect is one pixel taller due to sub-pixel difference
126 // between the text bounds and selection bounds in combination with use of
127 // enclosingIntRect() in SVGInlineTextBox::localSelectionRect().
128 drawingDisplayItem = getDrawingForSVGTextById("target");
129 assertTextDrawingEquals(drawingDisplayItem, "x");
130 assertCullRectEquals(drawingDisplayItem, IntRect(33, 30, 34, 16));
131 }
132
133 } // namespace
134 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/paint/SVGInlineTextBoxPainter.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698