Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(785)

Side by Side Diff: Source/core/platform/graphics/chromium/Canvas2DLayerBridgeTest.cpp

Issue 21858004: Refactoring Canvas2DLayerBridge to make it easier to write unit tests (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Fixed similarity blunder Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * 12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN Y 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN Y
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 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 AN Y 16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN Y
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 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 O N 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O N
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 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 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. 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */ 23 */
24 24
25 #include "config.h" 25 #include "config.h"
26 26
27 #include "core/platform/graphics/chromium/Canvas2DLayerBridge.h"
28
29 #include "SkDeferredCanvas.h" 27 #include "SkDeferredCanvas.h"
28 #include "SkSurface.h"
30 #include "core/platform/graphics/ImageBuffer.h" 29 #include "core/platform/graphics/ImageBuffer.h"
31 #include "core/tests/FakeWebGraphicsContext3D.h" 30 #include "core/platform/graphics/chromium/Canvas2DLayerBridgeTestHelper.h"
32 #include "public/platform/Platform.h" 31 #include "public/platform/Platform.h"
33 #include "public/platform/WebThread.h" 32 #include "public/platform/WebThread.h"
34 #include "third_party/skia/include/core/SkDevice.h" 33 #include "third_party/skia/include/core/SkDevice.h"
35 #include "wtf/RefPtr.h" 34 #include "wtf/RefPtr.h"
36 35
37 #include <gmock/gmock.h>
38 #include <gtest/gtest.h> 36 #include <gtest/gtest.h>
39 37
40 using namespace WebCore; 38 using namespace WebCore;
41 using namespace WebKit; 39 using namespace WebKit;
42 using testing::InSequence; 40 using testing::InSequence;
43 using testing::Return; 41 using testing::Return;
44 using testing::Test; 42 using testing::Test;
45 43
46 namespace { 44 namespace {
47 45
48 class MockCanvasContext : public FakeWebGraphicsContext3D {
49 public:
50 MOCK_METHOD0(flush, void(void));
51 MOCK_METHOD0(createTexture, unsigned(void));
52 MOCK_METHOD1(deleteTexture, void(unsigned));
53
54 virtual GrGLInterface* onCreateGrGLInterface() OVERRIDE { return 0; }
55 };
56
57 class FakeCanvas2DLayerBridge : public Canvas2DLayerBridge { 46 class FakeCanvas2DLayerBridge : public Canvas2DLayerBridge {
58 public: 47 public:
59 static PassOwnPtr<FakeCanvas2DLayerBridge> create(PassRefPtr<GraphicsContext 3D> context, SkDeferredCanvas* canvas, OpacityMode opacityMode) 48 static PassOwnPtr<FakeCanvas2DLayerBridge> create(PassOwnPtr<Helper> helper, const IntSize& size, OpacityMode opacityMode, bool* success)
60 { 49 {
61 return adoptPtr(new FakeCanvas2DLayerBridge(context, canvas, opacityMode )); 50 return adoptPtr(new FakeCanvas2DLayerBridge(helper, size, opacityMode, s uccess));
62 } 51 }
63 protected: 52 protected:
64 FakeCanvas2DLayerBridge(PassRefPtr<GraphicsContext3D> context, SkDeferredCan vas* canvas, OpacityMode opacityMode) : 53 FakeCanvas2DLayerBridge(PassOwnPtr<Helper> helper, const WebCore::IntSize& s ize, OpacityMode opacityMode, bool* success) :
65 Canvas2DLayerBridge(context, canvas, opacityMode) 54 Canvas2DLayerBridge(helper, size, opacityMode, success)
66 { } 55 { }
67 }; 56 };
68 57
69 } // namespace 58 }
70 59
71 class Canvas2DLayerBridgeTest : public Test { 60 class Canvas2DLayerBridgeTest : public Test {
72 protected: 61 protected:
73 void fullLifecycleTest() 62 void fullLifecycleTest()
74 { 63 {
75 RefPtr<GraphicsContext3D> mainContext = GraphicsContext3D::createGraphic sContextFromWebContext(adoptPtr(new MockCanvasContext)); 64 OwnPtr<Canvas2DLayerBridgeTestHelper> testHelper = adoptPtr(new Canvas2D LayerBridgeTestHelper);
76 65
77 MockCanvasContext& mainMock = *static_cast<MockCanvasContext*>(mainConte xt->webContext()); 66 MockCanvasContext* mockContext = &testHelper->mock();
67 ::testing::Mock::VerifyAndClearExpectations(mockContext);
78 68
79 SkDevice device(SkBitmap::kARGB_8888_Config, 300, 150); 69 IntSize size(300, 150);
80 SkDeferredCanvas canvas(&device); 70 bool success;
71 OwnPtr<Canvas2DLayerBridge> bridge = FakeCanvas2DLayerBridge::create(tes tHelper.release(), size, Canvas2DLayerBridge::NonOpaque, &success);
72 EXPECT_TRUE(success);
81 73
82 ::testing::Mock::VerifyAndClearExpectations(&mainMock); 74 ::testing::Mock::VerifyAndClearExpectations(mockContext);
83 75
84 OwnPtr<Canvas2DLayerBridge> bridge = FakeCanvas2DLayerBridge::create(mai nContext.release(), &canvas, Canvas2DLayerBridge::NonOpaque); 76 EXPECT_CALL(*mockContext, flush());
85
86 ::testing::Mock::VerifyAndClearExpectations(&mainMock);
87
88 EXPECT_CALL(mainMock, flush());
89 unsigned textureId = bridge->backBufferTexture(); 77 unsigned textureId = bridge->backBufferTexture();
90 EXPECT_EQ(textureId, 0u); 78 EXPECT_EQ(textureId, 0u);
91 79
92 ::testing::Mock::VerifyAndClearExpectations(&mainMock); 80 ::testing::Mock::VerifyAndClearExpectations(mockContext);
93 81
94 bridge.clear(); 82 bridge.clear();
95 83
96 ::testing::Mock::VerifyAndClearExpectations(&mainMock); 84 ::testing::Mock::VerifyAndClearExpectations(mockContext);
85 }
86
87 void loseContextTest()
88 {
89 OwnPtr<Canvas2DLayerBridgeTestHelper> testHelper = adoptPtr(new Canvas2D LayerBridgeTestHelper);
90 MockCanvasContext* mockContext = &testHelper->mock();
91
92 IntSize size(300, 150);
93 bool success;
94 OwnPtr<Canvas2DLayerBridge> bridge = FakeCanvas2DLayerBridge::create(tes tHelper.release(), size, Canvas2DLayerBridge::NonOpaque, &success);
95 EXPECT_TRUE(success);
96
97 ::testing::Mock::VerifyAndClearExpectations(mockContext);
98 EXPECT_TRUE(bridge->isValid());
99
100 mockContext->fakeContextLoss();
101
102 ::testing::Mock::VerifyAndClearExpectations(mockContext);
103
104 EXPECT_FALSE(bridge->isValid());
105
106 ::testing::Mock::VerifyAndClearExpectations(mockContext);
107
108 static_cast<Canvas2DLayerBridgeTestHelper*>(bridge->helper())->regenerat eContext();
109 mockContext = &static_cast<Canvas2DLayerBridgeTestHelper*>(bridge->helpe r())->mock();
110
111 ::testing::Mock::VerifyAndClearExpectations(mockContext);
112
113 EXPECT_TRUE(bridge->isValid());
114
115 ::testing::Mock::VerifyAndClearExpectations(mockContext);
116
117 EXPECT_FALSE(bridge->prepareMailbox(0, 0));
118
119 ::testing::Mock::VerifyAndClearExpectations(mockContext);
97 } 120 }
98 }; 121 };
99 122
100 namespace { 123 namespace {
101 124
102 TEST_F(Canvas2DLayerBridgeTest, testFullLifecycleSingleThreaded) 125 TEST_F(Canvas2DLayerBridgeTest, testFullLifecycle)
103 { 126 {
104 fullLifecycleTest(); 127 fullLifecycleTest();
105 } 128 }
106 129
130 TEST_F(Canvas2DLayerBridgeTest, testLoseContext)
131 {
132 loseContextTest();
133 }
134
107 } // namespace 135 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698