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_implementation.h"
9 #include "ui/gl/gl_surface_wgl.h"
10 #include "ui/gl/gl_wgl_api_implementation.h"
12 typedef std::pair
<std::string
, std::string
> stringpair
;
16 class WGLApiTest
: public testing::Test
{
18 void SetUp() override
{
19 GLSurfaceWGL::InitializeOneOffForTesting();
20 fake_arb_extension_string_
= "";
21 fake_ext_extension_string_
= "";
23 g_driver_wgl
.ClearBindings();
24 g_driver_wgl
.fn
.wglGetExtensionsStringARBFn
= &FakeGetExtensionsStringARB
;
25 g_driver_wgl
.fn
.wglGetExtensionsStringEXTFn
= &FakeGetExtensionsStringEXT
;
26 SetGLImplementation(kGLImplementationDesktopGL
);
27 SetGLGetProcAddressProc(
28 static_cast<GLGetProcAddressProc
>(&FakeGLGetProcAddress
));
31 void TearDown() override
{
32 g_current_wgl_context
= nullptr;
34 g_driver_wgl
.ClearBindings();
36 fake_ext_extension_string_
= "";
37 fake_arb_extension_string_
= "";
40 void InitializeAPI(base::CommandLine
* command_line
) {
41 api_
.reset(new RealWGLApi());
42 g_current_wgl_context
= api_
.get();
44 api_
->InitializeWithCommandLine(&g_driver_wgl
, command_line
);
46 api_
->Initialize(&g_driver_wgl
);
47 g_driver_wgl
.InitializeExtensionBindings();
50 void SetFakeEXTExtensionString(const char* fake_string
) {
51 fake_ext_extension_string_
= fake_string
;
54 void SetFakeARBExtensionString(const char* fake_string
) {
55 fake_arb_extension_string_
= fake_string
;
58 stringpair
GetExtensions() {
59 auto stringify
= [](const char* str
) -> std::string
{
60 return str
? str
: "";
63 stringify(wglGetExtensionsStringARB(NULL
)),
64 stringify(wglGetExtensionsStringEXT()));
67 static void* GL_BINDING_CALL
FakeGLGetProcAddress(const char *proc
) {
71 static const char* GL_BINDING_CALL
FakeGetExtensionsStringARB(HDC dc
) {
72 return fake_arb_extension_string_
;
75 static const char* GL_BINDING_CALL
FakeGetExtensionsStringEXT() {
76 return fake_ext_extension_string_
;
80 static const char* fake_ext_extension_string_
;
81 static const char* fake_arb_extension_string_
;
83 scoped_ptr
<RealWGLApi
> api_
;
86 const char* WGLApiTest::fake_ext_extension_string_
= "";
87 const char* WGLApiTest::fake_arb_extension_string_
= "";
89 TEST_F(WGLApiTest
, DisabledExtensionBitTest
) {
90 static const char* kFakeExtensions
= "WGL_ARB_extensions_string";
91 static const char* kFakeDisabledExtensions
= "WGL_ARB_extensions_string";
93 InitializeAPI(nullptr);
95 EXPECT_FALSE(g_driver_wgl
.ext
.b_WGL_ARB_extensions_string
);
97 base::CommandLine
command_line(base::CommandLine::NO_PROGRAM
);
98 command_line
.AppendSwitchASCII(switches::kDisableGLExtensions
,
99 kFakeDisabledExtensions
);
101 // NULL simulates not being able to resolve wglGetExtensionsStringARB
102 SetFakeARBExtensionString(NULL
);
103 SetFakeEXTExtensionString(kFakeExtensions
);
105 InitializeAPI(nullptr);
106 EXPECT_TRUE(g_driver_wgl
.ext
.b_WGL_ARB_extensions_string
);
108 InitializeAPI(&command_line
);
109 EXPECT_FALSE(g_driver_wgl
.ext
.b_WGL_ARB_extensions_string
);
111 SetFakeARBExtensionString("");
112 SetFakeEXTExtensionString(kFakeExtensions
);
114 InitializeAPI(nullptr);
115 // We expect false here, because wglGetExtensionsStringARB
116 // always takes precedence over wglGetExtensionsStringEXT
117 // if it is available.
118 EXPECT_FALSE(g_driver_wgl
.ext
.b_WGL_ARB_extensions_string
);
120 SetFakeARBExtensionString(kFakeExtensions
);
121 SetFakeEXTExtensionString("");
123 InitializeAPI(nullptr);
124 EXPECT_TRUE(g_driver_wgl
.ext
.b_WGL_ARB_extensions_string
);
126 InitializeAPI(&command_line
);
127 EXPECT_FALSE(g_driver_wgl
.ext
.b_WGL_ARB_extensions_string
);
130 TEST_F(WGLApiTest
, DisabledExtensionStringTest
) {
131 static const char* kFakeExtensions
= "EGL_EXT_1 EGL_EXT_2"
132 " EGL_EXT_3 EGL_EXT_4";
133 static const char* kFakeDisabledExtensions
=
134 "EGL_EXT_1,EGL_EXT_2,EGL_FAKE";
135 static const char* kFilteredExtensions
= "EGL_EXT_3 EGL_EXT_4";
137 base::CommandLine
command_line(base::CommandLine::NO_PROGRAM
);
138 command_line
.AppendSwitchASCII(switches::kDisableGLExtensions
,
139 kFakeDisabledExtensions
);
141 SetFakeARBExtensionString(kFakeExtensions
);
142 SetFakeEXTExtensionString(kFakeExtensions
);
144 InitializeAPI(nullptr);
145 EXPECT_EQ(stringpair(kFakeExtensions
, kFakeExtensions
),
148 InitializeAPI(&command_line
);
149 EXPECT_EQ(stringpair(kFilteredExtensions
, kFilteredExtensions
),