OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "web/PageOverlay.h" |
| 7 |
| 8 #include "core/frame/FrameView.h" |
| 9 #include "core/rendering/RenderView.h" |
| 10 #include "platform/graphics/Color.h" |
| 11 #include "platform/graphics/GraphicsContext.h" |
| 12 #include "platform/graphics/paint/DisplayItemList.h" |
| 13 #include "platform/graphics/paint/DrawingRecorder.h" |
| 14 #include "public/platform/Platform.h" |
| 15 #include "public/platform/WebCanvas.h" |
| 16 #include "public/platform/WebThread.h" |
| 17 #include "public/web/WebPageOverlay.h" |
| 18 #include "public/web/WebSettings.h" |
| 19 #include "third_party/skia/include/core/SkBitmap.h" |
| 20 #include "third_party/skia/include/core/SkCanvas.h" |
| 21 #include "third_party/skia/include/core/SkColor.h" |
| 22 #include "third_party/skia/include/core/SkPaint.h" |
| 23 #include "web/WebLocalFrameImpl.h" |
| 24 #include "web/WebViewImpl.h" |
| 25 #include "web/tests/FrameTestHelpers.h" |
| 26 #include <gmock/gmock.h> |
| 27 #include <gtest/gtest.h> |
| 28 |
| 29 using testing::_; |
| 30 using testing::AtLeast; |
| 31 using testing::Property; |
| 32 |
| 33 namespace blink { |
| 34 namespace { |
| 35 |
| 36 static const int viewportWidth = 800; |
| 37 static const int viewportHeight = 600; |
| 38 |
| 39 // These unit tests cover both PageOverlay and PageOverlayList. |
| 40 |
| 41 void enableAcceleratedCompositing(WebSettings* settings) |
| 42 { |
| 43 settings->setAcceleratedCompositingEnabled(true); |
| 44 } |
| 45 |
| 46 void disableAcceleratedCompositing(WebSettings* settings) |
| 47 { |
| 48 settings->setAcceleratedCompositingEnabled(false); |
| 49 } |
| 50 |
| 51 class PageOverlayTest : public ::testing::Test { |
| 52 protected: |
| 53 enum CompositingMode { AcceleratedCompositing, UnacceleratedCompositing }; |
| 54 |
| 55 void initialize(CompositingMode compositingMode) |
| 56 { |
| 57 m_helper.initialize( |
| 58 false /* enableJavascript */, nullptr /* webFrameClient */, nullptr
/* webViewClient */, |
| 59 compositingMode == AcceleratedCompositing ? enableAcceleratedComposi
ting : disableAcceleratedCompositing); |
| 60 webViewImpl()->resize(WebSize(viewportWidth, viewportHeight)); |
| 61 ASSERT_EQ(compositingMode == AcceleratedCompositing, webViewImpl()->isAc
celeratedCompositingActive()); |
| 62 ASSERT_TRUE(!webViewImpl()->pageOverlays() || webViewImpl()->pageOverlay
s()->empty()); |
| 63 } |
| 64 |
| 65 WebViewImpl* webViewImpl() const { return m_helper.webViewImpl(); } |
| 66 |
| 67 template <typename OverlayType> |
| 68 void runPageOverlayTestWithUnacceleratedCompositing(); |
| 69 |
| 70 template <typename OverlayType> |
| 71 void runPageOverlayTestWithAcceleratedCompositing(); |
| 72 |
| 73 private: |
| 74 FrameTestHelpers::WebViewHelper m_helper; |
| 75 }; |
| 76 |
| 77 // WebPageOverlay that uses a WebCanvas to draw a solid color. |
| 78 class TestWebPageOverlay : public WebPageOverlay { |
| 79 public: |
| 80 TestWebPageOverlay(WebView* webView, SkColor color) |
| 81 : m_webView(webView) |
| 82 , m_color(color) |
| 83 { |
| 84 } |
| 85 |
| 86 void paintPageOverlay(WebCanvas* canvas) override |
| 87 { |
| 88 IntSize size = m_webView->size(); |
| 89 SkPaint paint; |
| 90 paint.setColor(m_color); |
| 91 paint.setStyle(SkPaint::kFill_Style); |
| 92 canvas->drawRectCoords(0, 0, size.width(), size.height(), paint); |
| 93 } |
| 94 |
| 95 private: |
| 96 WebView* m_webView; |
| 97 SkColor m_color; |
| 98 }; |
| 99 |
| 100 // PageOverlay::Painter that uses a GraphicsContext to draw a solid color. |
| 101 // This kind of painter can legally produce more than one display item. |
| 102 // For simplicity, we only produce one here, and the particular display item tag |
| 103 // we use is unimportant for the purposes of this test. |
| 104 class TestPageOverlayPainter : public PageOverlay::Painter { |
| 105 public: |
| 106 TestPageOverlayPainter(WebViewImpl* webViewImpl, Color color) |
| 107 : m_webViewImpl(webViewImpl) |
| 108 , m_color(color) |
| 109 { |
| 110 } |
| 111 |
| 112 void paintPageOverlay(GraphicsContext& graphicsContext) override |
| 113 { |
| 114 IntSize size = m_webViewImpl->size(); |
| 115 FloatRect rect(0, 0, size.width(), size.height()); |
| 116 DisplayItemClient client = m_webViewImpl->mainFrameImpl()->frameView()->
renderView()->displayItemClient(); |
| 117 DrawingRecorder drawingRecorder(&graphicsContext, client, DisplayItem::P
ageOverlay, rect); |
| 118 graphicsContext.fillRect(rect, m_color); |
| 119 } |
| 120 |
| 121 private: |
| 122 WebViewImpl* m_webViewImpl; |
| 123 Color m_color; |
| 124 }; |
| 125 |
| 126 template <bool(*getter)(), void(*setter)(bool)> |
| 127 class RuntimeFeatureChange { |
| 128 public: |
| 129 RuntimeFeatureChange(bool newValue) : m_oldValue(getter()) { setter(newValue
); } |
| 130 ~RuntimeFeatureChange() { setter(m_oldValue); } |
| 131 private: |
| 132 bool m_oldValue; |
| 133 }; |
| 134 using SlimmingPaintScope = RuntimeFeatureChange<&RuntimeEnabledFeatures::slimmin
gPaintEnabled, RuntimeEnabledFeatures::setSlimmingPaintEnabled>; |
| 135 |
| 136 class MockCanvas : public SkCanvas { |
| 137 public: |
| 138 MockCanvas(int width, int height) : SkCanvas(width, height) { } |
| 139 MOCK_METHOD2(onDrawRect, void(const SkRect&, const SkPaint&)); |
| 140 }; |
| 141 |
| 142 template <typename OverlayType> |
| 143 void PageOverlayTest::runPageOverlayTestWithUnacceleratedCompositing() |
| 144 { |
| 145 initialize(UnacceleratedCompositing); |
| 146 |
| 147 OverlayType overlay(webViewImpl(), SK_ColorYELLOW); |
| 148 webViewImpl()->addPageOverlay(&overlay, 0 /* zOrder */); |
| 149 EXPECT_TRUE(webViewImpl()->pageOverlays() && !webViewImpl()->pageOverlays()-
>empty()); |
| 150 webViewImpl()->layout(); |
| 151 |
| 152 MockCanvas canvas(viewportWidth, viewportHeight); |
| 153 EXPECT_CALL(canvas, onDrawRect(_, _)).Times(AtLeast(1)); |
| 154 EXPECT_CALL(canvas, onDrawRect(SkRect::MakeWH(viewportWidth, viewportHeight)
, Property(&SkPaint::getColor, SK_ColorYELLOW))); |
| 155 webViewImpl()->paint(&canvas, WebRect(0, 0, viewportWidth, viewportHeight)); |
| 156 } |
| 157 |
| 158 TEST_F(PageOverlayTest, WebPageOverlay_UnacceleratedCompositing_NoSlimmingPaint) |
| 159 { |
| 160 SlimmingPaintScope slimmingPaintEnabled(false); |
| 161 runPageOverlayTestWithUnacceleratedCompositing<TestWebPageOverlay>(); |
| 162 } |
| 163 |
| 164 TEST_F(PageOverlayTest, WebPageOverlay_UnacceleratedCompositing_SlimmingPaint) |
| 165 { |
| 166 SlimmingPaintScope slimmingPaintEnabled(true); |
| 167 runPageOverlayTestWithUnacceleratedCompositing<TestWebPageOverlay>(); |
| 168 } |
| 169 |
| 170 TEST_F(PageOverlayTest, PageOverlayPainter_UnacceleratedCompositing_NoSlimmingPa
int) |
| 171 { |
| 172 SlimmingPaintScope slimmingPaintEnabled(false); |
| 173 runPageOverlayTestWithUnacceleratedCompositing<TestPageOverlayPainter>(); |
| 174 } |
| 175 |
| 176 TEST_F(PageOverlayTest, PageOverlayPainter_UnacceleratedCompositing_SlimmingPain
t) |
| 177 { |
| 178 SlimmingPaintScope slimmingPaintEnabled(true); |
| 179 runPageOverlayTestWithUnacceleratedCompositing<TestPageOverlayPainter>(); |
| 180 } |
| 181 |
| 182 template <typename OverlayType> |
| 183 void PageOverlayTest::runPageOverlayTestWithAcceleratedCompositing() |
| 184 { |
| 185 initialize(AcceleratedCompositing); |
| 186 webViewImpl()->layerTreeView()->setViewportSize(WebSize(viewportWidth, viewp
ortHeight)); |
| 187 |
| 188 OverlayType overlay(webViewImpl(), SK_ColorYELLOW); |
| 189 webViewImpl()->addPageOverlay(&overlay, 0 /* zOrder */); |
| 190 EXPECT_TRUE(webViewImpl()->pageOverlays() && !webViewImpl()->pageOverlays()-
>empty()); |
| 191 webViewImpl()->layout(); |
| 192 |
| 193 // Ideally, we would get results from the compositor that showed that this |
| 194 // page overlay actually winds up getting drawn on top of the rest. |
| 195 // For now, we just check that the GraphicsLayer will draw the right thing. |
| 196 |
| 197 MockCanvas canvas(viewportWidth, viewportHeight); |
| 198 EXPECT_CALL(canvas, onDrawRect(_, _)).Times(AtLeast(0)); |
| 199 EXPECT_CALL(canvas, onDrawRect(SkRect::MakeWH(viewportWidth, viewportHeight)
, Property(&SkPaint::getColor, SK_ColorYELLOW))); |
| 200 |
| 201 GraphicsLayer* graphicsLayer = webViewImpl()->pageOverlays()->graphicsLayerF
orTesting(); |
| 202 if (RuntimeEnabledFeatures::slimmingPaintEnabled()) { |
| 203 // If slimming paint is on, we paint the layer with a null canvas to get |
| 204 // a display list, and then replay that onto the mock canvas for |
| 205 // examination. This is about as close to the real path as we can easily |
| 206 // get. |
| 207 GraphicsContext graphicsContext(nullptr /* canvas */, graphicsLayer->dis
playItemList()); |
| 208 graphicsContext.setCertainlyOpaque(false); |
| 209 graphicsLayer->paint(graphicsContext, WebRect(0, 0, viewportWidth, viewp
ortHeight)); |
| 210 |
| 211 GraphicsContext replayContext(&canvas, nullptr /* displayItemList */); |
| 212 replayContext.setCertainlyOpaque(false); |
| 213 graphicsLayer->displayItemList()->replay(&replayContext); |
| 214 } else { |
| 215 GraphicsContext graphicsContext(&canvas, nullptr /* displayItemList */); |
| 216 graphicsContext.setCertainlyOpaque(false); |
| 217 graphicsLayer->paint(graphicsContext, WebRect(0, 0, viewportWidth, viewp
ortHeight)); |
| 218 } |
| 219 } |
| 220 |
| 221 TEST_F(PageOverlayTest, WebPageOverlay_AcceleratedCompositing_NoSlimmingPaint) |
| 222 { |
| 223 SlimmingPaintScope slimmingPaintEnabled(false); |
| 224 runPageOverlayTestWithAcceleratedCompositing<TestWebPageOverlay>(); |
| 225 } |
| 226 |
| 227 TEST_F(PageOverlayTest, WebPageOverlay_AcceleratedCompositing_SlimmingPaint) |
| 228 { |
| 229 SlimmingPaintScope slimmingPaintEnabled(true); |
| 230 runPageOverlayTestWithAcceleratedCompositing<TestWebPageOverlay>(); |
| 231 } |
| 232 |
| 233 TEST_F(PageOverlayTest, PageOverlayPainter_AcceleratedCompositing_NoSlimmingPain
t) |
| 234 { |
| 235 SlimmingPaintScope slimmingPaintEnabled(false); |
| 236 runPageOverlayTestWithAcceleratedCompositing<TestPageOverlayPainter>(); |
| 237 } |
| 238 |
| 239 TEST_F(PageOverlayTest, PageOverlayPainter_AcceleratedCompositing_SlimmingPaint) |
| 240 { |
| 241 SlimmingPaintScope slimmingPaintEnabled(true); |
| 242 runPageOverlayTestWithAcceleratedCompositing<TestPageOverlayPainter>(); |
| 243 } |
| 244 |
| 245 } // namespace |
| 246 } // namespace blink |
OLD | NEW |