Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / graphics / OpaqueRectTrackingContentLayerDelegateTest.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
23  */
24
25 #include "config.h"
26
27 #include "platform/graphics/OpaqueRectTrackingContentLayerDelegate.h"
28
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"
35
36 #include <gtest/gtest.h>
37
38 using namespace blink;
39
40 namespace {
41
42 struct PaintCallback {
43     virtual void operator()(GraphicsContext&, const IntRect&) = 0;
44 };
45
46 class TestLayerPainterChromium : public GraphicsContextPainter {
47 public:
48     TestLayerPainterChromium(PaintCallback& callback) : m_callback(callback) { }
49
50     virtual void paint(GraphicsContext& context, const IntRect& contentRect) OVERRIDE
51     {
52         m_callback(context, contentRect);
53     }
54
55 private:
56     PaintCallback& m_callback;
57 };
58
59 // Paint callback functions
60
61 struct PaintFillOpaque : public PaintCallback {
62     virtual void operator()(GraphicsContext& context, const IntRect& contentRect) OVERRIDE
63     {
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);
69     }
70 };
71
72 struct PaintFillAlpha : public PaintCallback {
73     virtual void operator()(GraphicsContext& context, const IntRect& contentRect)
74     {
75         Color alpha(0, 0, 0, 0);
76         context.fillRect(contentRect, alpha);
77     }
78 };
79
80 struct PaintFillPartialOpaque : public PaintCallback {
81     PaintFillPartialOpaque(IntRect opaqueRect)
82         : m_opaqueRect(opaqueRect)
83     {
84     }
85
86     virtual void operator()(GraphicsContext& context, const IntRect& contentRect)
87     {
88         Color alpha(0, 0, 0, 0);
89         context.fillRect(contentRect, alpha);
90
91         IntRect fillOpaque = m_opaqueRect;
92         fillOpaque.intersect(contentRect);
93
94         Color opaque(255, 255, 255, 255);
95         context.fillRect(fillOpaque, opaque);
96     }
97
98     IntRect m_opaqueRect;
99 };
100
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);
106
107 class OpaqueRectTrackingContentLayerDelegateTest : public testing::Test {
108 public:
109     OpaqueRectTrackingContentLayerDelegateTest()
110         : m_skCanvas(adoptPtr(skia::CreateBitmapCanvas(canvasRect().width, canvasRect().height, false)))
111     {
112     }
113
114     SkCanvas* skCanvas() { return m_skCanvas.get(); }
115     WebRect canvasRect() { return WebRect(0, 0, 400, 400); }
116
117 private:
118     OwnPtr<SkCanvas> m_skCanvas;
119 };
120
121 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testOpaqueRectPresentAfterOpaquePaint)
122 {
123     PaintFillOpaque fillOpaque;
124     TestLayerPainterChromium painter(fillOpaque);
125
126     OpaqueRectTrackingContentLayerDelegate delegate(&painter);
127
128     WebFloatRect opaqueRect;
129     delegate.paintContents(skCanvas(), canvasRect(), false, opaqueRect);
130     EXPECT_EQ_RECT(WebFloatRect(0, 0, 400, 400), opaqueRect);
131 }
132
133 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testOpaqueRectNotPresentAfterNonOpaquePaint)
134 {
135     PaintFillAlpha fillAlpha;
136     TestLayerPainterChromium painter(fillAlpha);
137     OpaqueRectTrackingContentLayerDelegate delegate(&painter);
138
139     WebFloatRect opaqueRect;
140     delegate.paintContents(skCanvas(), canvasRect(), false, opaqueRect);
141     EXPECT_EQ_RECT(WebFloatRect(0, 0, 0, 0), opaqueRect);
142 }
143
144 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testOpaqueRectNotPresentForOpaqueLayerWithOpaquePaint)
145 {
146     PaintFillOpaque fillOpaque;
147     TestLayerPainterChromium painter(fillOpaque);
148     OpaqueRectTrackingContentLayerDelegate delegate(&painter);
149
150     delegate.setOpaque(true);
151
152     WebFloatRect opaqueRect;
153     delegate.paintContents(skCanvas(), canvasRect(), false, opaqueRect);
154     EXPECT_EQ_RECT(WebFloatRect(0, 0, 0, 0), opaqueRect);
155 }
156
157 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testOpaqueRectNotPresentForOpaqueLayerWithNonOpaquePaint)
158 {
159     PaintFillOpaque fillAlpha;
160     TestLayerPainterChromium painter(fillAlpha);
161     OpaqueRectTrackingContentLayerDelegate delegate(&painter);
162
163     delegate.setOpaque(true);
164
165     WebFloatRect opaqueRect;
166     delegate.paintContents(skCanvas(), canvasRect(), false, opaqueRect);
167     EXPECT_EQ_RECT(WebFloatRect(0, 0, 0, 0), opaqueRect);
168 }
169
170 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testPartialOpaqueRectNoTransform)
171 {
172     IntRect partialRect(100, 200, 50, 75);
173     PaintFillPartialOpaque fillPartial(partialRect);
174     TestLayerPainterChromium painter(fillPartial);
175     OpaqueRectTrackingContentLayerDelegate delegate(&painter);
176
177     WebFloatRect opaqueRect;
178     delegate.paintContents(skCanvas(), canvasRect(), false, opaqueRect);
179     EXPECT_EQ_RECT(WebFloatRect(partialRect.x(), partialRect.y(), partialRect.width(), partialRect.height()), opaqueRect);
180 }
181
182 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testPartialOpaqueRectTranslation)
183 {
184     IntRect partialRect(100, 200, 50, 75);
185     PaintFillPartialOpaque fillPartial(partialRect);
186     TestLayerPainterChromium painter(fillPartial);
187     OpaqueRectTrackingContentLayerDelegate delegate(&painter);
188
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);
193 }
194
195 } // namespace