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 "core/platform/graphics/chromium/OpaqueRectTrackingContentLayerDelegate.h" 28 29 #include "core/platform/graphics/Color.h" 30 #include "core/platform/graphics/GraphicsContext.h" 31 #include "core/platform/graphics/IntRect.h" 32 #include "skia/ext/platform_canvas.h" 33 #include "public/platform/WebFloatRect.h" 34 #include "public/platform/WebRect.h" 35 36 #include <gtest/gtest.h> 37 38 using WebKit::WebRect; 39 using WebKit::WebFloatRect; 40 using namespace WebCore; 41 42 namespace { 43 44 struct PaintCallback { 45 virtual void operator()(GraphicsContext&, const IntRect&) = 0; 46 }; 47 48 class TestLayerPainterChromium : public GraphicsContextPainter { 49 public: 50 TestLayerPainterChromium(PaintCallback& callback) : m_callback(callback) { } 51 52 virtual void paint(GraphicsContext& context, const IntRect& contentRect) OVERRIDE 53 { 54 m_callback(context, contentRect); 55 } 56 57 private: 58 PaintCallback& m_callback; 59 }; 60 61 // Paint callback functions 62 63 struct PaintFillOpaque : public PaintCallback { 64 virtual void operator()(GraphicsContext& context, const IntRect& contentRect) OVERRIDE 65 { 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); 71 } 72 }; 73 74 struct PaintFillAlpha : public PaintCallback { 75 virtual void operator()(GraphicsContext& context, const IntRect& contentRect) 76 { 77 Color alpha(0, 0, 0, 0); 78 context.fillRect(contentRect, alpha); 79 } 80 }; 81 82 struct PaintFillPartialOpaque : public PaintCallback { 83 PaintFillPartialOpaque(IntRect opaqueRect) 84 : m_opaqueRect(opaqueRect) 85 { 86 } 87 88 virtual void operator()(GraphicsContext& context, const IntRect& contentRect) 89 { 90 Color alpha(0, 0, 0, 0); 91 context.fillRect(contentRect, alpha); 92 93 IntRect fillOpaque = m_opaqueRect; 94 fillOpaque.intersect(contentRect); 95 96 Color opaque(255, 255, 255, 255); 97 context.fillRect(fillOpaque, opaque); 98 } 99 100 IntRect m_opaqueRect; 101 }; 102 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); 108 109 class OpaqueRectTrackingContentLayerDelegateTest : public testing::Test { 110 public: 111 OpaqueRectTrackingContentLayerDelegateTest() 112 : m_skCanvas(adoptPtr(skia::CreateBitmapCanvas(canvasRect().width, canvasRect().height, false))) 113 { 114 } 115 116 SkCanvas* skCanvas() { return m_skCanvas.get(); } 117 WebRect canvasRect() { return WebRect(0, 0, 400, 400); } 118 119 private: 120 OwnPtr<SkCanvas> m_skCanvas; 121 }; 122 123 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testOpaqueRectPresentAfterOpaquePaint) 124 { 125 PaintFillOpaque fillOpaque; 126 TestLayerPainterChromium painter(fillOpaque); 127 128 OpaqueRectTrackingContentLayerDelegate delegate(&painter); 129 130 WebFloatRect opaqueRect; 131 delegate.paintContents(skCanvas(), canvasRect(), false, opaqueRect); 132 EXPECT_EQ_RECT(WebFloatRect(0, 0, 400, 400), opaqueRect); 133 } 134 135 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testOpaqueRectNotPresentAfterNonOpaquePaint) 136 { 137 PaintFillAlpha fillAlpha; 138 TestLayerPainterChromium painter(fillAlpha); 139 OpaqueRectTrackingContentLayerDelegate delegate(&painter); 140 141 WebFloatRect opaqueRect; 142 delegate.paintContents(skCanvas(), canvasRect(), false, opaqueRect); 143 EXPECT_EQ_RECT(WebFloatRect(0, 0, 0, 0), opaqueRect); 144 } 145 146 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testOpaqueRectNotPresentForOpaqueLayerWithOpaquePaint) 147 { 148 PaintFillOpaque fillOpaque; 149 TestLayerPainterChromium painter(fillOpaque); 150 OpaqueRectTrackingContentLayerDelegate delegate(&painter); 151 152 delegate.setOpaque(true); 153 154 WebFloatRect opaqueRect; 155 delegate.paintContents(skCanvas(), canvasRect(), false, opaqueRect); 156 EXPECT_EQ_RECT(WebFloatRect(0, 0, 0, 0), opaqueRect); 157 } 158 159 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testOpaqueRectNotPresentForOpaqueLayerWithNonOpaquePaint) 160 { 161 PaintFillOpaque fillAlpha; 162 TestLayerPainterChromium painter(fillAlpha); 163 OpaqueRectTrackingContentLayerDelegate delegate(&painter); 164 165 delegate.setOpaque(true); 166 167 WebFloatRect opaqueRect; 168 delegate.paintContents(skCanvas(), canvasRect(), false, opaqueRect); 169 EXPECT_EQ_RECT(WebFloatRect(0, 0, 0, 0), opaqueRect); 170 } 171 172 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testPartialOpaqueRectNoTransform) 173 { 174 IntRect partialRect(100, 200, 50, 75); 175 PaintFillPartialOpaque fillPartial(partialRect); 176 TestLayerPainterChromium painter(fillPartial); 177 OpaqueRectTrackingContentLayerDelegate delegate(&painter); 178 179 WebFloatRect opaqueRect; 180 delegate.paintContents(skCanvas(), canvasRect(), false, opaqueRect); 181 EXPECT_EQ_RECT(WebFloatRect(partialRect.x(), partialRect.y(), partialRect.width(), partialRect.height()), opaqueRect); 182 } 183 184 TEST_F(OpaqueRectTrackingContentLayerDelegateTest, testPartialOpaqueRectTranslation) 185 { 186 IntRect partialRect(100, 200, 50, 75); 187 PaintFillPartialOpaque fillPartial(partialRect); 188 TestLayerPainterChromium painter(fillPartial); 189 OpaqueRectTrackingContentLayerDelegate delegate(&painter); 190 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); 195 } 196 197 } // namespace 198