summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/Source/core/layout/ImageQualityControllerTest.cpp
blob: c9e585a53181716764b22aa3db8880a33067883f (plain)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// 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 "core/layout/ImageQualityController.h"

#include "core/layout/LayoutImage.h"
#include "core/layout/LayoutTestHelper.h"
#include "platform/graphics/GraphicsContext.h"
#include "platform/graphics/paint/PaintController.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace blink {

class ImageQualityControllerTest : public RenderingTest {
protected:
    ImageQualityController* controller() { return m_controller; }

private:
    void SetUp() override
    {
        m_controller = ImageQualityController::imageQualityController();
        RenderingTest::SetUp();
    }
    void TearDown() override
    {
    }
    ImageQualityController* m_controller;
};

TEST_F(ImageQualityControllerTest, RegularImage)
{
    setBodyInnerHTML("<img src='myimage'></img>");
    LayoutObject* obj = document().body()->firstChild()->layoutObject();

    EXPECT_EQ(InterpolationDefault, controller()->chooseInterpolationQuality(*obj, nullptr, nullptr, LayoutSize()));
}

TEST_F(ImageQualityControllerTest, ImageRenderingPixelated)
{
    setBodyInnerHTML("<img src='myimage' style='image-rendering: pixelated'></img>");
    LayoutObject* obj = document().body()->firstChild()->layoutObject();

    EXPECT_EQ(InterpolationNone, controller()->chooseInterpolationQuality(*obj, nullptr, nullptr, LayoutSize()));
}

#if !USE(LOW_QUALITY_IMAGE_INTERPOLATION)

class TestImageAnimated : public Image {
public:
    bool maybeAnimated() override { return true; }
    bool currentFrameKnownToBeOpaque(MetadataMode = UseCurrentMetadata) override { return false; }
    IntSize size() const override { return IntSize(); }
    void destroyDecodedData(bool) override { }
    void draw(SkCanvas*, const SkPaint&, const FloatRect& dstRect, const FloatRect& srcRect, RespectImageOrientationEnum, ImageClampingMode) override { }
    PassRefPtr<SkImage> imageForCurrentFrame() override { return nullptr; }
};

TEST_F(ImageQualityControllerTest, ImageMaybeAnimated)
{
    setBodyInnerHTML("<img src='myimage'></img>");
    LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObject());

    RefPtr<TestImageAnimated> testImage = adoptRef(new TestImageAnimated);
    EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(*img, testImage.get(), nullptr, LayoutSize()));
}

class TestImageWithContrast : public Image {
public:
    bool maybeAnimated() override { return true; }
    bool currentFrameKnownToBeOpaque(MetadataMode = UseCurrentMetadata) override { return false; }
    IntSize size() const override { return IntSize(); }
    void destroyDecodedData(bool) override { }
    void draw(SkCanvas*, const SkPaint&, const FloatRect& dstRect, const FloatRect& srcRect, RespectImageOrientationEnum, ImageClampingMode) override { }

    bool isBitmapImage() const override { return true; }
    PassRefPtr<SkImage> imageForCurrentFrame() override { return nullptr; }
};

TEST_F(ImageQualityControllerTest, LowQualityFilterForContrast)
{
    setBodyInnerHTML("<img src='myimage' style='image-rendering: -webkit-optimize-contrast'></img>");
    LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObject());

    RefPtr<TestImageWithContrast> testImage = adoptRef(new TestImageWithContrast);
    EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(*img, testImage.get(), testImage.get(), LayoutSize()));
}

class TestImageLowQuality : public Image {
public:
    bool maybeAnimated() override { return true; }
    bool currentFrameKnownToBeOpaque(MetadataMode = UseCurrentMetadata) override { return false; }
    IntSize size() const override { return IntSize(1, 1); }
    void destroyDecodedData(bool) override { }
    void draw(SkCanvas*, const SkPaint&, const FloatRect& dstRect, const FloatRect& srcRect, RespectImageOrientationEnum, ImageClampingMode) override { }

    bool isBitmapImage() const override { return true; }
    PassRefPtr<SkImage> imageForCurrentFrame() override { return nullptr; }
};

TEST_F(ImageQualityControllerTest, MediumQualityFilterForUnscaledImage)
{
    setBodyInnerHTML("<img src='myimage'></img>");
    LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObject());

    RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality);
    EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(*img, testImage.get(), testImage.get(), LayoutSize(1, 1)));
}

class MockTimer : public Timer<ImageQualityController> {
    typedef void (ImageQualityController::*TimerFiredFunction)(Timer*);
public:
    MockTimer(ImageQualityController* o, TimerFiredFunction f)
    : Timer<ImageQualityController>(o, f)
    {
    }

    void fire()
    {
        this->Timer<ImageQualityController>::fired();
        stop();
    }
};

TEST_F(ImageQualityControllerTest, LowQualityFilterForResizingImage)
{
    MockTimer* mockTimer = new MockTimer(controller(), &ImageQualityController::highQualityRepaintTimerFired);
    controller()->setTimer(mockTimer);
    setBodyInnerHTML("<img src='myimage'></img>");
    LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObject());

    RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality);
    OwnPtr<PaintController> paintController = PaintController::create();
    GraphicsContext context(*paintController);

    // Paint once. This will kick off a timer to see if we resize it during that timer's execution.
    EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(*img, testImage.get(), testImage.get(), LayoutSize(2, 2)));

    // Go into low-quality mode now that the size changed.
    EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(*img, testImage.get(), testImage.get(), LayoutSize(3, 3)));

    // Stay in low-quality mode since the size changed again.
    EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(*img, testImage.get(), testImage.get(), LayoutSize(4, 4)));

    mockTimer->fire();
    // The timer fired before painting at another size, so this doesn't count as animation. Therefore not painting at low quality.
    EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(*img, testImage.get(), testImage.get(), LayoutSize(4, 4)));
}

TEST_F(ImageQualityControllerTest, MediumQualityFilterForNotAnimatedWhileAnotherAnimates)
{
    MockTimer* mockTimer = new MockTimer(controller(), &ImageQualityController::highQualityRepaintTimerFired);
    controller()->setTimer(mockTimer);
    setBodyInnerHTML("<img id='myAnimatingImage' src='myimage'></img> <img id='myNonAnimatingImage' src='myimage2'></img>");
    LayoutImage* animatingImage = toLayoutImage(document().getElementById("myAnimatingImage")->layoutObject());
    LayoutImage* nonAnimatingImage = toLayoutImage(document().getElementById("myNonAnimatingImage")->layoutObject());

    RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality);
    OwnPtr<PaintController> paintController = PaintController::create();
    GraphicsContext context(*paintController);

    // Paint once. This will kick off a timer to see if we resize it during that timer's execution.
    EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(*animatingImage, testImage.get(), testImage.get(), LayoutSize(2, 2)));

    // Go into low-quality mode now that the size changed.
    EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(*animatingImage, testImage.get(), testImage.get(), LayoutSize(3, 3)));

    // The non-animating image receives a medium-quality filter, even though the other one is animating.
    EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(*nonAnimatingImage, testImage.get(), testImage.get(), LayoutSize(4, 4)));

    // Now the second image has animated, so it also gets painted with a low-quality filter.
    EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(*nonAnimatingImage, testImage.get(), testImage.get(), LayoutSize(3, 3)));

    mockTimer->fire();
    // The timer fired before painting at another size, so this doesn't count as animation. Therefore not painting at low quality for any image.
    EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(*animatingImage, testImage.get(), testImage.get(), LayoutSize(4, 4)));
    EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(*nonAnimatingImage, testImage.get(), testImage.get(), LayoutSize(4, 4)));
}

TEST_F(ImageQualityControllerTest, DontKickTheAnimationTimerWhenPaintingAtTheSameSize)
{
    MockTimer* mockTimer = new MockTimer(controller(), &ImageQualityController::highQualityRepaintTimerFired);
    controller()->setTimer(mockTimer);
    setBodyInnerHTML("<img src='myimage'></img>");
    LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObject());

    RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality);

    // Paint once. This will kick off a timer to see if we resize it during that timer's execution.
    EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(*img, testImage.get(), testImage.get(), LayoutSize(2, 2)));

    // Go into low-quality mode now that the size changed.
    EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(*img, testImage.get(), testImage.get(), LayoutSize(3, 3)));

    // Stay in low-quality mode since the size changed again.
    EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(*img, testImage.get(), testImage.get(), LayoutSize(4, 4)));

    mockTimer->stop();
    EXPECT_FALSE(mockTimer->isActive());
    // Painted at the same size, so even though timer is still executing, don't go to low quality.
    EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(*img, testImage.get(), testImage.get(), LayoutSize(4, 4)));
    // Check that the timer was not kicked. It should not have been, since the image was painted at the same size as last time.
    EXPECT_FALSE(mockTimer->isActive());
}

#endif

} // namespace blink