File: TextPainterTest.cpp

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (129 lines) | stat: -rw-r--r-- 6,037 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2014 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 "config.h"
#include "core/paint/TextPainter.h"

#include "core/CSSPropertyNames.h"
#include "core/CSSValueKeywords.h"
#include "core/css/CSSPrimitiveValue.h"
#include "core/frame/Settings.h"
#include "core/rendering/RenderText.h"
#include "core/rendering/RenderingTestHelper.h"
#include "core/rendering/style/ShadowData.h"
#include "core/rendering/style/ShadowList.h"
#include <gtest/gtest.h>

namespace blink {
namespace {

class TextPainterTest : public RenderingTest {
public:
    TextPainterTest() : m_renderText(nullptr) { }

protected:
    RenderText* renderText() { return m_renderText; }

private:
    virtual void SetUp() override
    {
        RenderingTest::SetUp();
        setBodyInnerHTML("Hello world");
        m_renderText = toRenderText(document().body()->firstChild()->renderer());
        ASSERT_TRUE(m_renderText);
        ASSERT_EQ("Hello world", m_renderText->text());
    }

    RenderText* m_renderText;
};

TEST_F(TextPainterTest, TextPaintingStyle_Simple)
{
    document().body()->setInlineStyleProperty(CSSPropertyColor, CSSValueBlue);
    document().view()->updateLayoutAndStyleIfNeededRecursive();

    TextPainter::Style textStyle = TextPainter::textPaintingStyle(
        *renderText(), renderText()->style(), false /* forceBlackText */, false /* isPrinting */);
    EXPECT_EQ(Color(0, 0, 255), textStyle.fillColor);
    EXPECT_EQ(Color(0, 0, 255), textStyle.strokeColor);
    EXPECT_EQ(Color(0, 0, 255), textStyle.emphasisMarkColor);
    EXPECT_EQ(0, textStyle.strokeWidth);
    EXPECT_EQ(nullptr, textStyle.shadow);
}

TEST_F(TextPainterTest, TextPaintingStyle_AllProperties)
{
    document().body()->setInlineStyleProperty(CSSPropertyWebkitTextFillColor, CSSValueRed);
    document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeColor, CSSValueLime);
    document().body()->setInlineStyleProperty(CSSPropertyWebkitTextEmphasisColor, CSSValueBlue);
    document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeWidth, 4, CSSPrimitiveValue::CSS_PX);
    document().body()->setInlineStyleProperty(CSSPropertyTextShadow, "1px 2px 3px yellow");
    document().view()->updateLayoutAndStyleIfNeededRecursive();

    TextPainter::Style textStyle = TextPainter::textPaintingStyle(
        *renderText(), renderText()->style(), false /* forceBlackText */, false /* isPrinting */);
    EXPECT_EQ(Color(255, 0, 0), textStyle.fillColor);
    EXPECT_EQ(Color(0, 255, 0), textStyle.strokeColor);
    EXPECT_EQ(Color(0, 0, 255), textStyle.emphasisMarkColor);
    EXPECT_EQ(4, textStyle.strokeWidth);
    ASSERT_NE(nullptr, textStyle.shadow);
    EXPECT_EQ(1u, textStyle.shadow->shadows().size());
    EXPECT_EQ(1, textStyle.shadow->shadows()[0].x());
    EXPECT_EQ(2, textStyle.shadow->shadows()[0].y());
    EXPECT_EQ(3, textStyle.shadow->shadows()[0].blur());
    EXPECT_EQ(Color(255, 255, 0), textStyle.shadow->shadows()[0].color());
}

TEST_F(TextPainterTest, TextPaintingStyle_ForceBlackText)
{
    document().body()->setInlineStyleProperty(CSSPropertyWebkitTextFillColor, CSSValueRed);
    document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeColor, CSSValueLime);
    document().body()->setInlineStyleProperty(CSSPropertyWebkitTextEmphasisColor, CSSValueBlue);
    document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeWidth, 4, CSSPrimitiveValue::CSS_PX);
    document().body()->setInlineStyleProperty(CSSPropertyTextShadow, "1px 2px 3px yellow");
    document().view()->updateLayoutAndStyleIfNeededRecursive();

    TextPainter::Style textStyle = TextPainter::textPaintingStyle(
        *renderText(), renderText()->style(), true /* forceBlackText */, false /* isPrinting */);
    EXPECT_EQ(Color::black, textStyle.fillColor);
    EXPECT_EQ(Color::black, textStyle.strokeColor);
    EXPECT_EQ(Color::black, textStyle.emphasisMarkColor);
    EXPECT_EQ(4, textStyle.strokeWidth);
    EXPECT_EQ(nullptr, textStyle.shadow);
}

TEST_F(TextPainterTest, TextPaintingStyle_ForceBackgroundToWhite_NoAdjustmentNeeded)
{
    document().body()->setInlineStyleProperty(CSSPropertyWebkitTextFillColor, CSSValueRed);
    document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeColor, CSSValueLime);
    document().body()->setInlineStyleProperty(CSSPropertyWebkitTextEmphasisColor, CSSValueBlue);
    document().body()->setInlineStyleProperty(CSSPropertyWebkitPrintColorAdjust, CSSValueEconomy);
    document().settings()->setShouldPrintBackgrounds(false);
    document().view()->updateLayoutAndStyleIfNeededRecursive();

    TextPainter::Style textStyle = TextPainter::textPaintingStyle(
        *renderText(), renderText()->style(), false /* forceBlackText */, true /* isPrinting */);
    EXPECT_EQ(Color(255, 0, 0), textStyle.fillColor);
    EXPECT_EQ(Color(0, 255, 0), textStyle.strokeColor);
    EXPECT_EQ(Color(0, 0, 255), textStyle.emphasisMarkColor);
}

TEST_F(TextPainterTest, TextPaintingStyle_ForceBackgroundToWhite_Darkened)
{
    document().body()->setInlineStyleProperty(CSSPropertyWebkitTextFillColor, "rgb(255, 220, 220)");
    document().body()->setInlineStyleProperty(CSSPropertyWebkitTextStrokeColor, "rgb(220, 255, 220)");
    document().body()->setInlineStyleProperty(CSSPropertyWebkitTextEmphasisColor, "rgb(220, 220, 255)");
    document().body()->setInlineStyleProperty(CSSPropertyWebkitPrintColorAdjust, CSSValueEconomy);
    document().settings()->setShouldPrintBackgrounds(false);
    document().view()->updateLayoutAndStyleIfNeededRecursive();

    TextPainter::Style textStyle = TextPainter::textPaintingStyle(
        *renderText(), renderText()->style(), false /* forceBlackText */, true /* isPrinting */);
    EXPECT_EQ(Color(255, 220, 220).dark(), textStyle.fillColor);
    EXPECT_EQ(Color(220, 255, 220).dark(), textStyle.strokeColor);
    EXPECT_EQ(Color(220, 220, 255).dark(), textStyle.emphasisMarkColor);
}

} // namespace
} // namespace blink