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 "skia/ext/platform_canvas.h"
33 #include "public/platform/WebFloatRect.h"
34 #include "public/platform/WebRect.h"
36 #include <gtest/gtest.h>
39 using blink::WebFloatRect;
40 using namespace WebCore;
44 struct PaintCallback {
45 virtual void operator()(GraphicsContext&, const IntRect&) = 0;
48 class TestLayerPainterChromium : public GraphicsContextPainter {
50 TestLayerPainterChromium(PaintCallback& callback) : m_callback(callback) { }
52 virtual void paint(GraphicsContext& context, const IntRect& contentRect) OVERRIDE
54 m_callback(context, contentRect);
58 PaintCallback& m_callback;
61 // Paint callback functions
63 struct PaintFillOpaque : public PaintCallback {
64 virtual void operator()(GraphicsContext& context, const IntRect& contentRect) OVERRIDE
66 Color opaque(255, 0, 0, 255);
67 IntRect top(contentRect.x(), contentRect.y(), contentRect.width(), contentRect.height() / 2);
68 IntRect bottom(contentRect.x(), contentRect.y() + contentRect.height() / 2, contentRect.width(), contentRect.height() / 2);
69 context.fillRect(top, opaque);
70 context.fillRect(bottom, opaque);
74 struct PaintFillAlpha : public PaintCallback {
75 virtual void operator()(GraphicsContext& context, const IntRect& contentRect)
77 Color alpha(0, 0, 0, 0);
78 context.fillRect(contentRect, alpha);
82 struct PaintFillPartialOpaque : public PaintCallback {
83 PaintFillPartialOpaque(IntRect opaqueRect)
84 : m_opaqueRect(opaqueRect)
88 virtual void operator()(GraphicsContext& context, const IntRect& contentRect)
90 Color alpha(0, 0, 0, 0);
91 context.fillRect(contentRect, alpha);
93 IntRect fillOpaque = m_opaqueRect;
94 fillOpaque.intersect(contentRect);
96 Color opaque(255, 255, 255, 255);
97 context.fillRect(fillOpaque, opaque);
100 IntRect m_opaqueRect;
103 #define EXPECT_EQ_RECT(a, b) \
104 EXPECT_EQ(a.x, b.x); \
105 EXPECT_EQ(a.width, b.width); \
106 EXPECT_EQ(a.y, b.y); \
107 EXPECT_EQ(a.height, b.height);
109 class OpaqueRectTrackingContentLayerDelegateTest : public testing::Test {
111 OpaqueRectTrackingContentLayerDelegateTest()
112 : m_skCanvas(adoptPtr(skia::CreateBitmapCanvas(canvasRect().width, canvasRect().height, false)))
116 SkCanvas* skCanvas() { return m_skCanvas.get(); }
117 WebRect canvasRect() { return WebRect(0, 0, 400, 400); }
120 OwnPtr<SkCanvas> m_skCanvas;
123 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testOpaqueRectPresentAfterOpaquePaint)
125 PaintFillOpaque fillOpaque;
126 TestLayerPainterChromium painter(fillOpaque);
128 OpaqueRectTrackingContentLayerDelegate delegate(&painter);
130 WebFloatRect opaqueRect;
131 delegate.paintContents(skCanvas(), canvasRect(), false, opaqueRect);
132 EXPECT_EQ_RECT(WebFloatRect(0, 0, 400, 400), opaqueRect);
135 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testOpaqueRectNotPresentAfterNonOpaquePaint)
137 PaintFillAlpha fillAlpha;
138 TestLayerPainterChromium painter(fillAlpha);
139 OpaqueRectTrackingContentLayerDelegate delegate(&painter);
141 WebFloatRect opaqueRect;
142 delegate.paintContents(skCanvas(), canvasRect(), false, opaqueRect);
143 EXPECT_EQ_RECT(WebFloatRect(0, 0, 0, 0), opaqueRect);
146 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testOpaqueRectNotPresentForOpaqueLayerWithOpaquePaint)
148 PaintFillOpaque fillOpaque;
149 TestLayerPainterChromium painter(fillOpaque);
150 OpaqueRectTrackingContentLayerDelegate delegate(&painter);
152 delegate.setOpaque(true);
154 WebFloatRect opaqueRect;
155 delegate.paintContents(skCanvas(), canvasRect(), false, opaqueRect);
156 EXPECT_EQ_RECT(WebFloatRect(0, 0, 0, 0), opaqueRect);
159 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testOpaqueRectNotPresentForOpaqueLayerWithNonOpaquePaint)
161 PaintFillOpaque fillAlpha;
162 TestLayerPainterChromium painter(fillAlpha);
163 OpaqueRectTrackingContentLayerDelegate delegate(&painter);
165 delegate.setOpaque(true);
167 WebFloatRect opaqueRect;
168 delegate.paintContents(skCanvas(), canvasRect(), false, opaqueRect);
169 EXPECT_EQ_RECT(WebFloatRect(0, 0, 0, 0), opaqueRect);
172 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testPartialOpaqueRectNoTransform)
174 IntRect partialRect(100, 200, 50, 75);
175 PaintFillPartialOpaque fillPartial(partialRect);
176 TestLayerPainterChromium painter(fillPartial);
177 OpaqueRectTrackingContentLayerDelegate delegate(&painter);
179 WebFloatRect opaqueRect;
180 delegate.paintContents(skCanvas(), canvasRect(), false, opaqueRect);
181 EXPECT_EQ_RECT(WebFloatRect(partialRect.x(), partialRect.y(), partialRect.width(), partialRect.height()), opaqueRect);
184 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testPartialOpaqueRectTranslation)
186 IntRect partialRect(100, 200, 50, 75);
187 PaintFillPartialOpaque fillPartial(partialRect);
188 TestLayerPainterChromium painter(fillPartial);
189 OpaqueRectTrackingContentLayerDelegate delegate(&painter);
191 WebFloatRect opaqueRect;
192 WebRect contentRect(11, 12, 389, 388);
193 delegate.paintContents(skCanvas(), contentRect, false, opaqueRect);
194 EXPECT_EQ_RECT(WebFloatRect(partialRect.x(), partialRect.y(), partialRect.width(), partialRect.height()), opaqueRect);