2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "platform/graphics/OpaqueRectTrackingContentLayerDelegate.h"
29 #include "platform/geometry/IntRect.h"
30 #include "platform/graphics/Color.h"
31 #include "platform/graphics/GraphicsContext.h"
32 #include "public/platform/WebFloatRect.h"
33 #include "public/platform/WebRect.h"
34 #include "skia/ext/platform_canvas.h"
36 #include <gtest/gtest.h>
38 using namespace blink;
42 struct PaintCallback {
43 virtual void operator()(GraphicsContext&, const IntRect&) = 0;
46 class TestLayerPainterChromium : public GraphicsContextPainter {
48 TestLayerPainterChromium(PaintCallback& callback) : m_callback(callback) { }
50 virtual void paint(GraphicsContext& context, const IntRect& contentRect) OVERRIDE
52 m_callback(context, contentRect);
56 PaintCallback& m_callback;
59 // Paint callback functions
61 struct PaintFillOpaque : public PaintCallback {
62 virtual void operator()(GraphicsContext& context, const IntRect& contentRect) OVERRIDE
64 Color opaque(255, 0, 0, 255);
65 IntRect top(contentRect.x(), contentRect.y(), contentRect.width(), contentRect.height() / 2);
66 IntRect bottom(contentRect.x(), contentRect.y() + contentRect.height() / 2, contentRect.width(), contentRect.height() / 2);
67 context.fillRect(top, opaque);
68 context.fillRect(bottom, opaque);
72 struct PaintFillAlpha : public PaintCallback {
73 virtual void operator()(GraphicsContext& context, const IntRect& contentRect)
75 Color alpha(0, 0, 0, 0);
76 context.fillRect(contentRect, alpha);
80 struct PaintFillPartialOpaque : public PaintCallback {
81 PaintFillPartialOpaque(IntRect opaqueRect)
82 : m_opaqueRect(opaqueRect)
86 virtual void operator()(GraphicsContext& context, const IntRect& contentRect)
88 Color alpha(0, 0, 0, 0);
89 context.fillRect(contentRect, alpha);
91 IntRect fillOpaque = m_opaqueRect;
92 fillOpaque.intersect(contentRect);
94 Color opaque(255, 255, 255, 255);
95 context.fillRect(fillOpaque, opaque);
101 #define EXPECT_EQ_RECT(a, b) \
102 EXPECT_EQ(a.x, b.x); \
103 EXPECT_EQ(a.width, b.width); \
104 EXPECT_EQ(a.y, b.y); \
105 EXPECT_EQ(a.height, b.height);
107 class OpaqueRectTrackingContentLayerDelegateTest : public testing::Test {
109 OpaqueRectTrackingContentLayerDelegateTest()
110 : m_skCanvas(adoptPtr(skia::CreateBitmapCanvas(canvasRect().width, canvasRect().height, false)))
114 SkCanvas* skCanvas() { return m_skCanvas.get(); }
115 WebRect canvasRect() { return WebRect(0, 0, 400, 400); }
118 OwnPtr<SkCanvas> m_skCanvas;
121 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testOpaqueRectPresentAfterOpaquePaint)
123 PaintFillOpaque fillOpaque;
124 TestLayerPainterChromium painter(fillOpaque);
126 OpaqueRectTrackingContentLayerDelegate delegate(&painter);
128 WebFloatRect opaqueRect;
129 delegate.paintContents(skCanvas(), canvasRect(), false, opaqueRect);
130 EXPECT_EQ_RECT(WebFloatRect(0, 0, 400, 400), opaqueRect);
133 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testOpaqueRectNotPresentAfterNonOpaquePaint)
135 PaintFillAlpha fillAlpha;
136 TestLayerPainterChromium painter(fillAlpha);
137 OpaqueRectTrackingContentLayerDelegate delegate(&painter);
139 WebFloatRect opaqueRect;
140 delegate.paintContents(skCanvas(), canvasRect(), false, opaqueRect);
141 EXPECT_EQ_RECT(WebFloatRect(0, 0, 0, 0), opaqueRect);
144 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testOpaqueRectNotPresentForOpaqueLayerWithOpaquePaint)
146 PaintFillOpaque fillOpaque;
147 TestLayerPainterChromium painter(fillOpaque);
148 OpaqueRectTrackingContentLayerDelegate delegate(&painter);
150 delegate.setOpaque(true);
152 WebFloatRect opaqueRect;
153 delegate.paintContents(skCanvas(), canvasRect(), false, opaqueRect);
154 EXPECT_EQ_RECT(WebFloatRect(0, 0, 0, 0), opaqueRect);
157 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testOpaqueRectNotPresentForOpaqueLayerWithNonOpaquePaint)
159 PaintFillOpaque fillAlpha;
160 TestLayerPainterChromium painter(fillAlpha);
161 OpaqueRectTrackingContentLayerDelegate delegate(&painter);
163 delegate.setOpaque(true);
165 WebFloatRect opaqueRect;
166 delegate.paintContents(skCanvas(), canvasRect(), false, opaqueRect);
167 EXPECT_EQ_RECT(WebFloatRect(0, 0, 0, 0), opaqueRect);
170 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testPartialOpaqueRectNoTransform)
172 IntRect partialRect(100, 200, 50, 75);
173 PaintFillPartialOpaque fillPartial(partialRect);
174 TestLayerPainterChromium painter(fillPartial);
175 OpaqueRectTrackingContentLayerDelegate delegate(&painter);
177 WebFloatRect opaqueRect;
178 delegate.paintContents(skCanvas(), canvasRect(), false, opaqueRect);
179 EXPECT_EQ_RECT(WebFloatRect(partialRect.x(), partialRect.y(), partialRect.width(), partialRect.height()), opaqueRect);
182 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testPartialOpaqueRectTranslation)
184 IntRect partialRect(100, 200, 50, 75);
185 PaintFillPartialOpaque fillPartial(partialRect);
186 TestLayerPainterChromium painter(fillPartial);
187 OpaqueRectTrackingContentLayerDelegate delegate(&painter);
189 WebFloatRect opaqueRect;
190 WebRect contentRect(11, 12, 389, 388);
191 delegate.paintContents(skCanvas(), contentRect, false, opaqueRect);
192 EXPECT_EQ_RECT(WebFloatRect(partialRect.x(), partialRect.y(), partialRect.width(), partialRect.height()), opaqueRect);