1/*
2 * Copyright (C) 2011 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/Canvas2DLayerBridge.h"
28
29#include "SkDeferredCanvas.h"
30#include "SkSurface.h"
31#include "core/platform/graphics/ImageBuffer.h"
32#include "core/tests/FakeWebGraphicsContext3D.h"
33#include "public/platform/Platform.h"
34#include "public/platform/WebThread.h"
35#include "third_party/skia/include/core/SkDevice.h"
36#include "wtf/RefPtr.h"
37
38#include <gmock/gmock.h>
39#include <gtest/gtest.h>
40
41using namespace WebCore;
42using namespace WebKit;
43using testing::InSequence;
44using testing::Return;
45using testing::Test;
46
47namespace {
48
49class MockCanvasContext : public FakeWebGraphicsContext3D {
50public:
51    MOCK_METHOD0(flush, void(void));
52    MOCK_METHOD0(createTexture, unsigned(void));
53    MOCK_METHOD1(deleteTexture, void(unsigned));
54
55    virtual GrGLInterface* onCreateGrGLInterface() OVERRIDE { return 0; }
56};
57
58class FakeCanvas2DLayerBridge : public Canvas2DLayerBridge {
59public:
60    static PassOwnPtr<FakeCanvas2DLayerBridge> create(PassRefPtr<GraphicsContext3D> context, SkDeferredCanvas* canvas, OpacityMode opacityMode)
61    {
62        return adoptPtr(new FakeCanvas2DLayerBridge(context, canvas, opacityMode));
63    }
64protected:
65    FakeCanvas2DLayerBridge(PassRefPtr<GraphicsContext3D> context, SkDeferredCanvas* canvas, OpacityMode opacityMode) :
66        Canvas2DLayerBridge(context, canvas, opacityMode)
67    { }
68};
69
70} // namespace
71
72class Canvas2DLayerBridgeTest : public Test {
73protected:
74    void fullLifecycleTest()
75    {
76        RefPtr<GraphicsContext3D> mainContext = GraphicsContext3D::createGraphicsContextFromWebContext(adoptPtr(new MockCanvasContext));
77
78        MockCanvasContext& mainMock = *static_cast<MockCanvasContext*>(mainContext->webContext());
79
80        SkImage::Info info = {
81            300,
82            150,
83            SkImage::kPMColor_ColorType,
84            SkImage::kPremul_AlphaType,
85        };
86        SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(SkSurface::NewRaster(info)));
87
88        ::testing::Mock::VerifyAndClearExpectations(&mainMock);
89
90        OwnPtr<Canvas2DLayerBridge> bridge = FakeCanvas2DLayerBridge::create(mainContext.release(), canvas.get(), Canvas2DLayerBridge::NonOpaque);
91
92        ::testing::Mock::VerifyAndClearExpectations(&mainMock);
93
94        EXPECT_CALL(mainMock, flush());
95        unsigned textureId = bridge->backBufferTexture();
96        EXPECT_EQ(textureId, 0u);
97
98        ::testing::Mock::VerifyAndClearExpectations(&mainMock);
99
100        bridge.clear();
101
102        ::testing::Mock::VerifyAndClearExpectations(&mainMock);
103    }
104};
105
106namespace {
107
108TEST_F(Canvas2DLayerBridgeTest, testFullLifecycleSingleThreaded)
109{
110    fullLifecycleTest();
111}
112
113} // namespace
114