2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "GraphicsContext3D.h"
30 #include "GraphicsContext3DPrivate.h"
31 #include "MockWebGraphicsContext3D.h"
33 #include <gmock/gmock.h>
34 #include <gtest/gtest.h>
36 using namespace WebCore;
37 using namespace WebKit;
38 using namespace testing;
40 class FrameCountingContext : public MockWebGraphicsContext3D {
42 FrameCountingContext() : m_frame(0) { }
44 // This method would normally do a glSwapBuffers under the hood.
45 virtual void prepareTexture() { m_frame++; }
47 int frameCount() { return m_frame; }
53 TEST(MockGraphicsContext3DTest, CanOverrideManually)
55 GraphicsContext3D::Attributes attrs;
56 RefPtr<GraphicsContext3D> context = GraphicsContext3DPrivate::createGraphicsContextFromWebContext(adoptPtr(new FrameCountingContext()), attrs, 0, GraphicsContext3D::RenderDirectlyToHostWindow, GraphicsContext3DPrivate::ForUseOnThisThread);
57 FrameCountingContext& mockContext = *static_cast<FrameCountingContext*>(GraphicsContext3DPrivate::extractWebGraphicsContext3D(context.get()));
59 for (int i = 0; i < 10; i++) {
60 context->clearColor(0, 0, 0, 1);
61 context->prepareTexture();
65 EXPECT_EQ(10, mockContext.frameCount());
69 class GMockContext : public MockWebGraphicsContext3D {
71 MOCK_METHOD0(getError, WGC3Denum());
74 TEST(MockGraphicsContext3DTest, CanUseGMock)
76 GraphicsContext3D::Attributes attrs;
77 RefPtr<GraphicsContext3D> context = GraphicsContext3DPrivate::createGraphicsContextFromWebContext(adoptPtr(new GMockContext()), attrs, 0, GraphicsContext3D::RenderDirectlyToHostWindow, GraphicsContext3DPrivate::ForUseOnThisThread);
78 GMockContext& mockContext = *static_cast<GMockContext*>(GraphicsContext3DPrivate::extractWebGraphicsContext3D(context.get()));
80 EXPECT_CALL(mockContext, getError())
81 .WillRepeatedly(Return(314));
83 // It's OK to call methods GMock doesn't know about.
84 context->makeContextCurrent();
86 // Check that the mocked method is returning as intended.
87 for (int i = 0; i < 10; i++)
88 EXPECT_EQ((int)context->getError(), 314);
91 class ContextThatCountsMakeCurrents : public MockWebGraphicsContext3D {
93 ContextThatCountsMakeCurrents() : m_makeCurrentCount(0) { }
94 virtual bool makeContextCurrent()
99 int makeCurrentCount() { return m_makeCurrentCount; }
101 int m_makeCurrentCount;
105 TEST(MockGraphicsContext3DTest, ContextForThisThreadShouldMakeCurrent)
107 GraphicsContext3D::Attributes attrs;
108 RefPtr<GraphicsContext3D> context = GraphicsContext3DPrivate::createGraphicsContextFromWebContext(adoptPtr(new ContextThatCountsMakeCurrents()), attrs, 0, GraphicsContext3D::RenderDirectlyToHostWindow, GraphicsContext3DPrivate::ForUseOnThisThread);
109 EXPECT_TRUE(context);
110 ContextThatCountsMakeCurrents& mockContext = *static_cast<ContextThatCountsMakeCurrents*>(GraphicsContext3DPrivate::extractWebGraphicsContext3D(context.get()));
111 EXPECT_EQ(1, mockContext.makeCurrentCount());
114 TEST(MockGraphicsContext3DTest, ContextForAnotherThreadShouldNotMakeCurrent)
116 GraphicsContext3D::Attributes attrs;
117 RefPtr<GraphicsContext3D> context = GraphicsContext3DPrivate::createGraphicsContextFromWebContext(adoptPtr(new ContextThatCountsMakeCurrents()), attrs, 0, GraphicsContext3D::RenderDirectlyToHostWindow, GraphicsContext3DPrivate::ForUseOnAnotherThread);
118 EXPECT_TRUE(context);
119 ContextThatCountsMakeCurrents& mockContext = *static_cast<ContextThatCountsMakeCurrents*>(GraphicsContext3DPrivate::extractWebGraphicsContext3D(context.get()));
120 EXPECT_EQ(0, mockContext.makeCurrentCount());
123 class ContextWithMakeCurrentThatFails : public MockWebGraphicsContext3D {
125 ContextWithMakeCurrentThatFails() { }
126 virtual bool makeContextCurrent() { return false; }
129 TEST(MockGraphicsContext3DTest, ContextForThisThreadFailsWhenMakeCurrentFails)
131 GraphicsContext3D::Attributes attrs;
132 RefPtr<GraphicsContext3D> context = GraphicsContext3DPrivate::createGraphicsContextFromWebContext(adoptPtr(new ContextWithMakeCurrentThatFails()), attrs, 0, GraphicsContext3D::RenderDirectlyToHostWindow, GraphicsContext3DPrivate::ForUseOnThisThread);
133 EXPECT_FALSE(context);