summaryrefslogtreecommitdiff
path: root/chromium/ui/gl/wgl_api_unittest.cc
blob: a0fe216ffba933e697224ef544a7f82ca05f7281 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>

#include "base/command_line.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gl/gl_implementation.h"
#include "ui/gl/gl_surface_wgl.h"
#include "ui/gl/gl_wgl_api_implementation.h"

typedef std::pair<std::string, std::string> stringpair;

namespace gl {

class WGLApiTest : public testing::Test {
 public:
  void SetUp() override {
    GLSurfaceWGL::InitializeOneOffForTesting();
    fake_arb_extension_string_ = "";
    fake_ext_extension_string_ = "";

    g_driver_wgl.ClearBindings();
    g_driver_wgl.fn.wglGetExtensionsStringARBFn = &FakeGetExtensionsStringARB;
    g_driver_wgl.fn.wglGetExtensionsStringEXTFn = &FakeGetExtensionsStringEXT;
    SetGLImplementation(kGLImplementationDesktopGL);
    SetGLGetProcAddressProc(&FakeGLGetProcAddress);
  }

  void TearDown() override {
    g_current_wgl_context = nullptr;
    api_.reset(nullptr);
    g_driver_wgl.ClearBindings();

    fake_ext_extension_string_ = "";
    fake_arb_extension_string_ = "";
  }

  void InitializeAPI(const char* disabled_extensions) {
    api_.reset(new RealWGLApi());
    g_current_wgl_context = api_.get();
    api_->Initialize(&g_driver_wgl);
    if (disabled_extensions) {
      SetDisabledExtensionsWGL(disabled_extensions);
    }
    g_driver_wgl.InitializeExtensionBindings();
  }

  void SetFakeEXTExtensionString(const char* fake_string) {
    fake_ext_extension_string_ = fake_string;
  }

  void SetFakeARBExtensionString(const char* fake_string) {
    fake_arb_extension_string_ = fake_string;
  }

  stringpair GetExtensions() {
    auto stringify = [](const char* str) -> std::string {
      return str ? str : "";
    };
    return stringpair(
        stringify(wglGetExtensionsStringARB(NULL)),
        stringify(wglGetExtensionsStringEXT()));
  }

  static GLFunctionPointerType WINAPI FakeGLGetProcAddress(const char* proc) {
    return NULL;
  }

  static const char* GL_BINDING_CALL FakeGetExtensionsStringARB(HDC dc) {
    return fake_arb_extension_string_;
  }

  static const char* GL_BINDING_CALL FakeGetExtensionsStringEXT() {
    return fake_ext_extension_string_;
  }

 protected:
  static const char* fake_ext_extension_string_;
  static const char* fake_arb_extension_string_;

  std::unique_ptr<RealWGLApi> api_;
};

const char* WGLApiTest::fake_ext_extension_string_ = "";
const char* WGLApiTest::fake_arb_extension_string_ = "";

TEST_F(WGLApiTest, DisabledExtensionBitTest) {
  static const char* kFakeExtensions = "WGL_ARB_extensions_string";
  static const char* kFakeDisabledExtensions = "WGL_ARB_extensions_string";

  InitializeAPI(nullptr);

  EXPECT_FALSE(g_driver_wgl.ext.b_WGL_ARB_extensions_string);

  // NULL simulates not being able to resolve wglGetExtensionsStringARB
  SetFakeARBExtensionString(nullptr);
  SetFakeEXTExtensionString(kFakeExtensions);

  InitializeAPI(nullptr);
  EXPECT_TRUE(g_driver_wgl.ext.b_WGL_ARB_extensions_string);

  InitializeAPI(kFakeExtensions);
  EXPECT_FALSE(g_driver_wgl.ext.b_WGL_ARB_extensions_string);

  SetFakeARBExtensionString("");
  SetFakeEXTExtensionString(kFakeExtensions);

  InitializeAPI(nullptr);
  // We expect false here, because wglGetExtensionsStringARB
  // always takes precedence over wglGetExtensionsStringEXT
  // if it is available.
  EXPECT_FALSE(g_driver_wgl.ext.b_WGL_ARB_extensions_string);

  SetFakeARBExtensionString(kFakeExtensions);
  SetFakeEXTExtensionString("");

  InitializeAPI(nullptr);
  EXPECT_TRUE(g_driver_wgl.ext.b_WGL_ARB_extensions_string);

  InitializeAPI(kFakeDisabledExtensions);
  EXPECT_FALSE(g_driver_wgl.ext.b_WGL_ARB_extensions_string);
}

TEST_F(WGLApiTest, DisabledExtensionStringTest) {
  static const char* kFakeExtensions = "EGL_EXT_1 EGL_EXT_2"
                                       " EGL_EXT_3 EGL_EXT_4";
  static const char* kFakeDisabledExtensions =
      "EGL_EXT_1,EGL_EXT_2,EGL_FAKE";
  static const char* kFilteredExtensions = "EGL_EXT_3 EGL_EXT_4";

  SetFakeARBExtensionString(kFakeExtensions);
  SetFakeEXTExtensionString(kFakeExtensions);

  InitializeAPI(nullptr);
  EXPECT_EQ(stringpair(kFakeExtensions, kFakeExtensions),
            GetExtensions());

  InitializeAPI(kFakeDisabledExtensions);
  EXPECT_EQ(stringpair(kFilteredExtensions, kFilteredExtensions),
            GetExtensions());
}

}  // namespace gl