Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / ui / gl / gl_api_unittest.cc
blob66c938021c65420b70d00e91049a57ba0b1955e7
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"
14 namespace gfx {
16 class GLContextFake : public GLContext {
17 public:
18 bool Initialize(GLSurface* compatible_surface,
19 GpuPreference gpu_preference) override {
20 return true;
22 bool MakeCurrent(GLSurface* surface) override { return true; }
23 void ReleaseCurrent(GLSurface* surface) override {}
24 bool IsCurrent(GLSurface* surface) override { return true; }
25 void* GetHandle() override { return NULL; }
26 scoped_refptr<gfx::GPUTimingClient> CreateGPUTimingClient() override {
27 return NULL;
29 void OnSetSwapInterval(int interval) override {}
30 GLContextFake() : GLContext(NULL) {}
31 protected:
32 ~GLContextFake() override {}
35 class GLApiTest : public testing::Test {
36 public:
37 void SetUp() override {
38 fake_extension_string_ = "";
39 fake_version_string_ = "";
40 num_fake_extension_strings_ = 0;
41 fake_extension_strings_ = nullptr;
43 g_current_gl_context_tls = new base::ThreadLocalPointer<GLApi>;
45 SetGLGetProcAddressProc(
46 static_cast<GLGetProcAddressProc>(&FakeGLGetProcAddress));
49 static void* GL_BINDING_CALL FakeGLGetProcAddress(const char *proc) {
50 return reinterpret_cast<void*>(0x1);
53 void TearDown() override {
54 api_.reset(nullptr);
55 delete g_current_gl_context_tls;
57 SetGLImplementation(kGLImplementationNone);
58 fake_extension_string_ = "";
59 fake_version_string_ = "";
60 num_fake_extension_strings_ = 0;
61 fake_extension_strings_ = nullptr;
64 void InitializeAPI(base::CommandLine* command_line) {
65 api_.reset(new RealGLApi());
66 g_current_gl_context_tls->Set(api_.get());
68 g_driver_gl.ClearBindings();
69 g_driver_gl.fn.glGetStringFn = &FakeGetString;
70 g_driver_gl.fn.glGetStringiFn = &FakeGetStringi;
71 g_driver_gl.fn.glGetIntegervFn = &FakeGetIntegervFn;
73 fake_context_ = new GLContextFake();
74 if (command_line)
75 api_->InitializeWithCommandLine(&g_driver_gl, command_line);
76 else
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;
85 fake_version_string_ = "2.1";
88 void SetFakeExtensionStrings(const char** fake_strings, uint32_t count) {
89 SetGLImplementation(kGLImplementationDesktopGL);
90 num_fake_extension_strings_ = count;
91 fake_extension_strings_ = fake_strings;
92 fake_version_string_ = "3.0";
95 static const GLubyte* GL_BINDING_CALL FakeGetString(GLenum name) {
96 if (name == GL_VERSION)
97 return reinterpret_cast<const GLubyte*>(fake_version_string_);
98 return reinterpret_cast<const GLubyte*>(fake_extension_string_);
101 static void GL_BINDING_CALL FakeGetIntegervFn(GLenum pname, GLint* params) {
102 *params = num_fake_extension_strings_;
105 static const GLubyte* GL_BINDING_CALL FakeGetStringi(GLenum name,
106 GLuint index) {
107 return (index < num_fake_extension_strings_) ?
108 reinterpret_cast<const GLubyte*>(fake_extension_strings_[index]) :
109 nullptr;
112 const char* GetExtensions() {
113 return reinterpret_cast<const char*>(api_->glGetStringFn(GL_EXTENSIONS));
116 uint32_t GetNumExtensions() {
117 GLint num_extensions = 0;
118 api_->glGetIntegervFn(GL_NUM_EXTENSIONS, &num_extensions);
119 return static_cast<uint32_t>(num_extensions);
122 const char* GetExtensioni(uint32_t index) {
123 return reinterpret_cast<const char*>(api_->glGetStringiFn(GL_EXTENSIONS,
124 index));
127 protected:
128 static const char* fake_extension_string_;
129 static const char* fake_version_string_;
131 static uint32_t num_fake_extension_strings_;
132 static const char** fake_extension_strings_;
134 scoped_refptr<GLContext> fake_context_;
135 scoped_ptr<DriverGL> driver_;
136 scoped_ptr<RealGLApi> api_;
139 const char* GLApiTest::fake_extension_string_ = "";
140 const char* GLApiTest::fake_version_string_ = "";
142 uint32_t GLApiTest::num_fake_extension_strings_ = 0;
143 const char** GLApiTest::fake_extension_strings_ = nullptr;
145 TEST_F(GLApiTest, DisabledExtensionStringTest) {
146 static const char* kFakeExtensions = "GL_EXT_1 GL_EXT_2 GL_EXT_3 GL_EXT_4";
147 static const char* kFakeDisabledExtensions = "GL_EXT_1,GL_EXT_2,GL_FAKE";
148 static const char* kFilteredExtensions = "GL_EXT_3 GL_EXT_4";
150 SetFakeExtensionString(kFakeExtensions);
151 InitializeAPI(nullptr);
153 EXPECT_STREQ(kFakeExtensions, GetExtensions());
155 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
156 command_line.AppendSwitchASCII(switches::kDisableGLExtensions,
157 kFakeDisabledExtensions);
158 InitializeAPI(&command_line);
160 EXPECT_STREQ(kFilteredExtensions, GetExtensions());
163 TEST_F(GLApiTest, DisabledExtensionBitTest) {
164 static const char* kFakeExtensions[] = {
165 "GL_ARB_timer_query"
167 static const char* kFakeDisabledExtensions = "GL_ARB_timer_query";
169 SetFakeExtensionStrings(kFakeExtensions, arraysize(kFakeExtensions));
170 InitializeAPI(nullptr);
172 EXPECT_TRUE(g_driver_gl.ext.b_GL_ARB_timer_query);
174 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
175 command_line.AppendSwitchASCII(switches::kDisableGLExtensions,
176 kFakeDisabledExtensions);
177 InitializeAPI(&command_line);
179 EXPECT_FALSE(g_driver_gl.ext.b_GL_ARB_timer_query);
182 TEST_F(GLApiTest, DisabledExtensionStringIndexTest) {
183 static const char* kFakeExtensions[] = {
184 "GL_EXT_1",
185 "GL_EXT_2",
186 "GL_EXT_3",
187 "GL_EXT_4"
189 static const char* kFakeDisabledExtensions = "GL_EXT_1,GL_EXT_2,GL_FAKE";
190 static const char* kFilteredExtensions[] = {
191 "GL_EXT_3",
192 "GL_EXT_4"
195 SetFakeExtensionStrings(kFakeExtensions, arraysize(kFakeExtensions));
196 InitializeAPI(nullptr);
198 EXPECT_EQ(arraysize(kFakeExtensions), GetNumExtensions());
199 for (uint32_t i = 0; i < arraysize(kFakeExtensions); ++i) {
200 EXPECT_STREQ(kFakeExtensions[i], GetExtensioni(i));
203 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
204 command_line.AppendSwitchASCII(switches::kDisableGLExtensions,
205 kFakeDisabledExtensions);
206 InitializeAPI(&command_line);
208 EXPECT_EQ(arraysize(kFilteredExtensions), GetNumExtensions());
209 for (uint32_t i = 0; i < arraysize(kFilteredExtensions); ++i) {
210 EXPECT_STREQ(kFilteredExtensions[i], GetExtensioni(i));
214 } // namespace gfx