| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/paint/TextPainter.h" | 5 #include "core/style/ComputedStyle.h" |
| 6 | 6 |
| 7 #include "core/CSSPropertyNames.h" | 7 #include <stdio.h> |
| 8 #include "core/CSSValueKeywords.h" | 8 #include <stdlib.h> |
| 9 #include "core/css/CSSPrimitiveValue.h" | |
| 10 #include "core/frame/Settings.h" | |
| 11 #include "core/layout/LayoutTestHelper.h" | |
| 12 #include "core/layout/api/LineLayoutText.h" | |
| 13 #include "core/paint/PaintInfo.h" | |
| 14 #include "core/style/ShadowData.h" | |
| 15 #include "core/style/ShadowList.h" | |
| 16 #include "platform/graphics/paint/PaintController.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 #include <memory> | |
| 19 | 9 |
| 20 namespace blink { | 10 using namespace blink; |
| 21 namespace { | |
| 22 | 11 |
| 23 class TextPainterTest : public RenderingTest { | 12 Color secretColor() { |
| 24 public: | 13 return Color::black; |
| 25 TextPainterTest() | |
| 26 : m_layoutText(nullptr) | |
| 27 , m_paintController(PaintController::create()) | |
| 28 , m_context(*m_paintController) | |
| 29 { } | |
| 30 | |
| 31 protected: | |
| 32 LineLayoutText getLineLayoutText() { return LineLayoutText(m_layoutText); } | |
| 33 | |
| 34 PaintInfo createPaintInfo(bool usesTextAsClip, bool isPrinting) | |
| 35 { | |
| 36 return PaintInfo(m_context, IntRect(), usesTextAsClip ? PaintPhaseTextCl
ip : PaintPhaseSelfBlockBackgroundOnly, isPrinting ? GlobalPaintPrinting : Globa
lPaintNormalPhase, 0); | |
| 37 } | |
| 38 | |
| 39 private: | |
| 40 void SetUp() override | |
| 41 { | |
| 42 RenderingTest::SetUp(); | |
| 43 setBodyInnerHTML("Hello world"); | |
| 44 m_layoutText = toLayoutText(document().body()->firstChild()->layoutObjec
t()); | |
| 45 ASSERT_TRUE(m_layoutText); | |
| 46 ASSERT_EQ("Hello world", m_layoutText->text()); | |
| 47 } | |
| 48 | |
| 49 LayoutText* m_layoutText; | |
| 50 std::unique_ptr<PaintController> m_paintController; | |
| 51 GraphicsContext m_context; | |
| 52 }; | |
| 53 | |
| 54 TEST_F(TextPainterTest, TextPaintingStyle_Simple) | |
| 55 { | |
| 56 document().body()->setInlineStyleProperty(CSSPropertyColor, CSSValueBlue); | |
| 57 document().view()->updateAllLifecyclePhases(); | |
| 58 | |
| 59 TextPainter::Style textStyle = TextPainter::textPaintingStyle( | |
| 60 getLineLayoutText(), getLineLayoutText().styleRef(), createPaintInfo(fal
se /* usesTextAsClip */, false /* isPrinting */)); | |
| 61 EXPECT_EQ(Color(0, 0, 255), textStyle.fillColor); | |
| 62 EXPECT_EQ(Color(0, 0, 255), textStyle.strokeColor); | |
| 63 EXPECT_EQ(Color(0, 0, 255), textStyle.emphasisMarkColor); | |
| 64 EXPECT_EQ(0, textStyle.strokeWidth); | |
| 65 EXPECT_EQ(nullptr, textStyle.shadow); | |
| 66 } | 14 } |
| 67 | 15 |
| 68 TEST_F(TextPainterTest, TextPaintingStyle_AllProperties) | 16 int main(int argc, char* argv[]) { |
| 69 { | 17 ComputedStyle style1; |
| 70 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextFillColor, CS
SValueRed); | 18 StyleColor color(0xffff0000); |
| 71 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeColor,
CSSValueLime); | 19 style1.setVisitedLinkTextFillColor(color); |
| 72 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextEmphasisColor
, CSSValueBlue); | 20 Color c = style1.colorIncludingFallback(CSSPropertyWebkitTextFillColor, fals
e); |
| 73 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeWidth,
4, CSSPrimitiveValue::UnitType::Pixels); | 21 if (Color(255, 0, 0) != c) { |
| 74 document().body()->setInlineStyleProperty(CSSPropertyTextShadow, "1px 2px 3p
x yellow"); | 22 fprintf(stderr, "fail2\n"); |
| 75 document().view()->updateAllLifecyclePhases(); | 23 exit(1); |
| 76 | 24 } else { |
| 77 TextPainter::Style textStyle = TextPainter::textPaintingStyle( | 25 fprintf(stderr, "pass2\n"); |
| 78 getLineLayoutText(), getLineLayoutText().styleRef(), createPaintInfo(fal
se /* usesTextAsClip */, false /* isPrinting */)); | 26 } |
| 79 EXPECT_EQ(Color(255, 0, 0), textStyle.fillColor); | |
| 80 EXPECT_EQ(Color(0, 255, 0), textStyle.strokeColor); | |
| 81 EXPECT_EQ(Color(0, 0, 255), textStyle.emphasisMarkColor); | |
| 82 EXPECT_EQ(4, textStyle.strokeWidth); | |
| 83 ASSERT_NE(nullptr, textStyle.shadow); | |
| 84 EXPECT_EQ(1u, textStyle.shadow->shadows().size()); | |
| 85 EXPECT_EQ(1, textStyle.shadow->shadows()[0].x()); | |
| 86 EXPECT_EQ(2, textStyle.shadow->shadows()[0].y()); | |
| 87 EXPECT_EQ(3, textStyle.shadow->shadows()[0].blur()); | |
| 88 EXPECT_EQ(Color(255, 255, 0), textStyle.shadow->shadows()[0].color().getColo
r()); | |
| 89 } | 27 } |
| 90 | |
| 91 TEST_F(TextPainterTest, TextPaintingStyle_UsesTextAsClip) | |
| 92 { | |
| 93 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextFillColor, CS
SValueRed); | |
| 94 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeColor,
CSSValueLime); | |
| 95 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextEmphasisColor
, CSSValueBlue); | |
| 96 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeWidth,
4, CSSPrimitiveValue::UnitType::Pixels); | |
| 97 document().body()->setInlineStyleProperty(CSSPropertyTextShadow, "1px 2px 3p
x yellow"); | |
| 98 document().view()->updateAllLifecyclePhases(); | |
| 99 | |
| 100 TextPainter::Style textStyle = TextPainter::textPaintingStyle( | |
| 101 getLineLayoutText(), getLineLayoutText().styleRef(), createPaintInfo(tru
e /* usesTextAsClip */, false /* isPrinting */)); | |
| 102 EXPECT_EQ(Color::black, textStyle.fillColor); | |
| 103 EXPECT_EQ(Color::black, textStyle.strokeColor); | |
| 104 EXPECT_EQ(Color::black, textStyle.emphasisMarkColor); | |
| 105 EXPECT_EQ(4, textStyle.strokeWidth); | |
| 106 EXPECT_EQ(nullptr, textStyle.shadow); | |
| 107 } | |
| 108 | |
| 109 TEST_F(TextPainterTest, TextPaintingStyle_ForceBackgroundToWhite_NoAdjustmentNee
ded) | |
| 110 { | |
| 111 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextFillColor, CS
SValueRed); | |
| 112 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeColor,
CSSValueLime); | |
| 113 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextEmphasisColor
, CSSValueBlue); | |
| 114 document().body()->setInlineStyleProperty(CSSPropertyWebkitPrintColorAdjust,
CSSValueEconomy); | |
| 115 document().settings()->setShouldPrintBackgrounds(false); | |
| 116 document().setPrinting(true); | |
| 117 document().view()->updateAllLifecyclePhases(); | |
| 118 | |
| 119 TextPainter::Style textStyle = TextPainter::textPaintingStyle( | |
| 120 getLineLayoutText(), getLineLayoutText().styleRef(), createPaintInfo(fal
se /* usesTextAsClip */, true /* isPrinting */)); | |
| 121 EXPECT_EQ(Color(255, 0, 0), textStyle.fillColor); | |
| 122 EXPECT_EQ(Color(0, 255, 0), textStyle.strokeColor); | |
| 123 EXPECT_EQ(Color(0, 0, 255), textStyle.emphasisMarkColor); | |
| 124 } | |
| 125 | |
| 126 TEST_F(TextPainterTest, TextPaintingStyle_ForceBackgroundToWhite_Darkened) | |
| 127 { | |
| 128 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextFillColor, "r
gb(255, 220, 220)"); | |
| 129 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeColor,
"rgb(220, 255, 220)"); | |
| 130 document().body()->setInlineStyleProperty(CSSPropertyWebkitTextEmphasisColor
, "rgb(220, 220, 255)"); | |
| 131 document().body()->setInlineStyleProperty(CSSPropertyWebkitPrintColorAdjust,
CSSValueEconomy); | |
| 132 document().settings()->setShouldPrintBackgrounds(false); | |
| 133 document().setPrinting(true); | |
| 134 document().view()->updateAllLifecyclePhases(); | |
| 135 | |
| 136 TextPainter::Style textStyle = TextPainter::textPaintingStyle( | |
| 137 getLineLayoutText(), getLineLayoutText().styleRef(), createPaintInfo(fal
se /* usesTextAsClip */, true /* isPrinting */)); | |
| 138 EXPECT_EQ(Color(255, 220, 220).dark(), textStyle.fillColor); | |
| 139 EXPECT_EQ(Color(220, 255, 220).dark(), textStyle.strokeColor); | |
| 140 EXPECT_EQ(Color(220, 220, 255).dark(), textStyle.emphasisMarkColor); | |
| 141 } | |
| 142 | |
| 143 } // namespace | |
| 144 } // namespace blink | |
| OLD | NEW |