initial import
[vuplus_webkit] / Source / WebKit / chromium / tests / MockGraphicsContext3DTest.cpp
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  *
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.
13  *
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.
24  */
25
26 #include "config.h"
27
28 #include "GraphicsContext3D.h"
29
30 #include "GraphicsContext3DPrivate.h"
31 #include "MockWebGraphicsContext3D.h"
32
33 #include <gmock/gmock.h>
34 #include <gtest/gtest.h>
35
36 using namespace WebCore;
37 using namespace WebKit;
38 using namespace testing;
39
40 class FrameCountingContext : public MockWebGraphicsContext3D {
41 public:
42     FrameCountingContext() : m_frame(0) { }
43
44     // This method would normally do a glSwapBuffers under the hood.
45     virtual void prepareTexture() { m_frame++; }
46
47     int frameCount() { return m_frame; }
48
49 private:
50     int m_frame;
51 };
52
53 TEST(MockGraphicsContext3DTest, CanOverrideManually)
54 {
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()));
58
59     for (int i = 0; i < 10; i++) {
60         context->clearColor(0, 0, 0, 1);
61         context->prepareTexture();
62     }
63     context->finish();
64
65     EXPECT_EQ(10, mockContext.frameCount());
66 }
67
68
69 class GMockContext : public MockWebGraphicsContext3D {
70 public:
71     MOCK_METHOD0(getError, WGC3Denum());
72 };
73
74 TEST(MockGraphicsContext3DTest, CanUseGMock)
75 {
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()));
79
80     EXPECT_CALL(mockContext, getError())
81             .WillRepeatedly(Return(314));
82
83     // It's OK to call methods GMock doesn't know about.
84     context->makeContextCurrent();
85
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);
89 }
90
91 class ContextThatCountsMakeCurrents : public MockWebGraphicsContext3D {
92 public:
93     ContextThatCountsMakeCurrents() : m_makeCurrentCount(0) { }
94     virtual bool makeContextCurrent()
95     {
96         m_makeCurrentCount++;
97         return true;
98     }
99     int makeCurrentCount() { return m_makeCurrentCount; }
100 private:
101     int m_makeCurrentCount;
102 };
103
104
105 TEST(MockGraphicsContext3DTest, ContextForThisThreadShouldMakeCurrent)
106 {
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());
112 }
113
114 TEST(MockGraphicsContext3DTest, ContextForAnotherThreadShouldNotMakeCurrent)
115 {
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());
121 }
122
123 class ContextWithMakeCurrentThatFails : public MockWebGraphicsContext3D {
124 public:
125     ContextWithMakeCurrentThatFails() { }
126     virtual bool makeContextCurrent() { return false; }
127 };
128
129 TEST(MockGraphicsContext3DTest, ContextForThisThreadFailsWhenMakeCurrentFails)
130 {
131     GraphicsContext3D::Attributes attrs;
132     RefPtr<GraphicsContext3D> context = GraphicsContext3DPrivate::createGraphicsContextFromWebContext(adoptPtr(new ContextWithMakeCurrentThatFails()), attrs, 0, GraphicsContext3D::RenderDirectlyToHostWindow, GraphicsContext3DPrivate::ForUseOnThisThread);
133     EXPECT_FALSE(context);
134 }