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.
5 #include "base/command_line.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/gl/gl_context.h"
9 #include "ui/gl/gl_gl_api_implementation.h"
10 #include "ui/gl/gl_implementation.h"
11 #include "ui/gl/gl_switches.h"
12 #include "ui/gl/gpu_timing.h"
16 class GLContextFake
: public GLContext
{
18 bool Initialize(GLSurface
* compatible_surface
,
19 GpuPreference gpu_preference
) override
{
22 void Destroy() override
{}
23 bool MakeCurrent(GLSurface
* surface
) override
{ return true; }
24 void ReleaseCurrent(GLSurface
* surface
) override
{}
25 bool IsCurrent(GLSurface
* surface
) override
{ return true; }
26 void* GetHandle() override
{ return NULL
; }
27 scoped_refptr
<gfx::GPUTimingClient
> CreateGPUTimingClient() override
{
30 void OnSetSwapInterval(int interval
) override
{}
31 GLContextFake() : GLContext(NULL
) {}
33 ~GLContextFake() override
{}
36 class GLApiTest
: public testing::Test
{
38 void SetUp() override
{
39 fake_extension_string_
= "";
40 num_fake_extension_strings_
= 0;
41 fake_extension_strings_
= nullptr;
43 g_current_gl_context_tls
= new base::ThreadLocalPointer
<GLApi
>;
44 api_
.reset(new RealGLApi());
46 g_driver_gl
.ClearBindings();
47 g_driver_gl
.fn
.glGetStringFn
= &FakeGetString
;
48 g_driver_gl
.fn
.glGetStringiFn
= &FakeGetStringi
;
49 g_driver_gl
.fn
.glGetIntegervFn
= &FakeGetIntegervFn
;
51 SetGLGetProcAddressProc(
52 static_cast<GLGetProcAddressProc
>(&FakeGLGetProcAddress
));
55 static void* GL_BINDING_CALL
FakeGLGetProcAddress(const char *proc
) {
56 return reinterpret_cast<void*>(0x1);
59 void TearDown() override
{
61 delete g_current_gl_context_tls
;
63 SetGLImplementation(kGLImplementationNone
);
64 fake_extension_string_
= "";
65 num_fake_extension_strings_
= 0;
66 fake_extension_strings_
= nullptr;
69 void InitializeAPI(base::CommandLine
* command_line
) {
70 api_
.reset(new RealGLApi());
71 g_current_gl_context_tls
->Set(api_
.get());
73 fake_context_
= new GLContextFake();
75 api_
->InitializeWithCommandLine(&g_driver_gl
, command_line
);
77 api_
->Initialize(&g_driver_gl
);
78 api_
->InitializeFilteredExtensions();
79 g_driver_gl
.InitializeCustomDynamicBindings(fake_context_
.get());
82 void SetFakeExtensionString(const char* fake_string
) {
83 SetGLImplementation(kGLImplementationDesktopGL
);
84 fake_extension_string_
= fake_string
;
87 void SetFakeExtensionStrings(const char** fake_strings
, uint32_t count
) {
88 SetGLImplementation(kGLImplementationDesktopGLCoreProfile
);
89 num_fake_extension_strings_
= count
;
90 fake_extension_strings_
= fake_strings
;
93 static const GLubyte
* GL_BINDING_CALL
FakeGetString(GLenum name
) {
94 return reinterpret_cast<const GLubyte
*>(fake_extension_string_
);
97 static void GL_BINDING_CALL
FakeGetIntegervFn(GLenum pname
, GLint
* params
) {
98 *params
= num_fake_extension_strings_
;
101 static const GLubyte
* GL_BINDING_CALL
FakeGetStringi(GLenum name
,
103 return (index
< num_fake_extension_strings_
) ?
104 reinterpret_cast<const GLubyte
*>(fake_extension_strings_
[index
]) :
108 const char* GetExtensions() {
109 return reinterpret_cast<const char*>(api_
->glGetStringFn(GL_EXTENSIONS
));
112 uint32_t GetNumExtensions() {
113 GLint num_extensions
= 0;
114 api_
->glGetIntegervFn(GL_NUM_EXTENSIONS
, &num_extensions
);
115 return static_cast<uint32_t>(num_extensions
);
118 const char* GetExtensioni(uint32_t index
) {
119 return reinterpret_cast<const char*>(api_
->glGetStringiFn(GL_EXTENSIONS
,
124 static const char* fake_extension_string_
;
126 static uint32_t num_fake_extension_strings_
;
127 static const char** fake_extension_strings_
;
129 scoped_refptr
<GLContext
> fake_context_
;
130 scoped_ptr
<DriverGL
> driver_
;
131 scoped_ptr
<RealGLApi
> api_
;
134 const char* GLApiTest::fake_extension_string_
= "";
136 uint32_t GLApiTest::num_fake_extension_strings_
= 0;
137 const char** GLApiTest::fake_extension_strings_
= nullptr;
139 TEST_F(GLApiTest
, DisabledExtensionStringTest
) {
140 static const char* kFakeExtensions
= "GL_EXT_1 GL_EXT_2 GL_EXT_3 GL_EXT_4";
141 static const char* kFakeDisabledExtensions
= "GL_EXT_1,GL_EXT_2,GL_FAKE";
142 static const char* kFilteredExtensions
= "GL_EXT_3 GL_EXT_4";
144 SetFakeExtensionString(kFakeExtensions
);
145 InitializeAPI(nullptr);
147 EXPECT_STREQ(kFakeExtensions
, GetExtensions());
149 base::CommandLine
command_line(base::CommandLine::NO_PROGRAM
);
150 command_line
.AppendSwitchASCII(switches::kDisableGLExtensions
,
151 kFakeDisabledExtensions
);
152 InitializeAPI(&command_line
);
154 EXPECT_STREQ(kFilteredExtensions
, GetExtensions());
157 TEST_F(GLApiTest
, DisabledExtensionBitTest
) {
158 static const char* kFakeExtensions
[] = {
161 static const char* kFakeDisabledExtensions
= "GL_ARB_timer_query";
163 SetFakeExtensionStrings(kFakeExtensions
, arraysize(kFakeExtensions
));
164 InitializeAPI(nullptr);
166 EXPECT_TRUE(g_driver_gl
.ext
.b_GL_ARB_timer_query
);
168 base::CommandLine
command_line(base::CommandLine::NO_PROGRAM
);
169 command_line
.AppendSwitchASCII(switches::kDisableGLExtensions
,
170 kFakeDisabledExtensions
);
171 InitializeAPI(&command_line
);
173 EXPECT_FALSE(g_driver_gl
.ext
.b_GL_ARB_timer_query
);
176 TEST_F(GLApiTest
, DisabledExtensionStringIndexTest
) {
177 static const char* kFakeExtensions
[] = {
183 static const char* kFakeDisabledExtensions
= "GL_EXT_1,GL_EXT_2,GL_FAKE";
184 static const char* kFilteredExtensions
[] = {
189 SetFakeExtensionStrings(kFakeExtensions
, arraysize(kFakeExtensions
));
190 InitializeAPI(nullptr);
192 EXPECT_EQ(arraysize(kFakeExtensions
), GetNumExtensions());
193 for (uint32_t i
= 0; i
< arraysize(kFakeExtensions
); ++i
) {
194 EXPECT_STREQ(kFakeExtensions
[i
], GetExtensioni(i
));
197 base::CommandLine
command_line(base::CommandLine::NO_PROGRAM
);
198 command_line
.AppendSwitchASCII(switches::kDisableGLExtensions
,
199 kFakeDisabledExtensions
);
200 InitializeAPI(&command_line
);
202 EXPECT_EQ(arraysize(kFilteredExtensions
), GetNumExtensions());
203 for (uint32_t i
= 0; i
< arraysize(kFilteredExtensions
); ++i
) {
204 EXPECT_STREQ(kFilteredExtensions
[i
], GetExtensioni(i
));