Add more components tests to GN build.
[chromium-blink-merge.git] / ui / gl / egl_api_unittest.cc
blob8e29277537fca1b8ea8d08a7c6e437b853665ea9
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_egl_api_implementation.h"
9 #include "ui/gl/gl_switches.h"
11 namespace gfx {
13 class EGLApiTest : public testing::Test {
14 public:
15 void SetUp() override {
16 fake_client_extension_string_ = "";
17 fake_extension_string_ = "";
19 // TODO(dyen): Add a way to bind mock drivers for testing.
20 g_driver_egl.ClearBindings();
21 g_driver_egl.fn.eglInitializeFn = &FakeInitialize;
22 g_driver_egl.fn.eglQueryStringFn = &FakeQueryString;
23 g_driver_egl.fn.eglGetCurrentDisplayFn = &FakeGetCurrentDisplay;
24 g_driver_egl.fn.eglGetDisplayFn = &FakeGetDisplay;
25 g_driver_egl.fn.eglGetErrorFn = &FakeGetError;
28 void TearDown() override {
29 g_current_egl_context = nullptr;
30 api_.reset(nullptr);
31 g_driver_egl.ClearBindings();
33 fake_client_extension_string_ = "";
34 fake_extension_string_ = "";
37 void InitializeAPI(base::CommandLine* command_line) {
38 api_.reset(new RealEGLApi());
39 g_current_egl_context = api_.get();
40 if (command_line)
41 api_->InitializeWithCommandLine(&g_driver_egl, command_line);
42 else
43 api_->Initialize(&g_driver_egl);
44 g_driver_egl.InitializeExtensionBindings();
47 void SetFakeExtensionString(const char* fake_string,
48 const char* fake_client_string) {
49 fake_extension_string_ = fake_string;
50 fake_client_extension_string_ = fake_client_string;
53 static EGLBoolean GL_BINDING_CALL FakeInitialize(EGLDisplay display,
54 EGLint * major,
55 EGLint * minor) {
56 return EGL_TRUE;
59 static const char* GL_BINDING_CALL FakeQueryString(EGLDisplay dpy,
60 EGLint name) {
61 if (dpy == EGL_NO_DISPLAY) {
62 return fake_client_extension_string_;
63 } else {
64 return fake_extension_string_;
68 static EGLDisplay GL_BINDING_CALL FakeGetCurrentDisplay() {
69 return reinterpret_cast<EGLDisplay>(0x1);
72 static EGLDisplay GL_BINDING_CALL FakeGetDisplay(
73 EGLNativeDisplayType native_display) {
74 return reinterpret_cast<EGLDisplay>(0x1);
77 static EGLint GL_BINDING_CALL FakeGetError() {
78 return EGL_SUCCESS;
81 std::pair<const char*, const char*> GetExtensions() {
82 return std::make_pair(
83 api_->eglQueryStringFn(EGL_NO_DISPLAY, EGL_EXTENSIONS),
84 api_->eglQueryStringFn(api_->eglGetCurrentDisplayFn(), EGL_EXTENSIONS));
87 protected:
88 static const char* fake_extension_string_;
89 static const char* fake_client_extension_string_;
91 scoped_ptr<RealEGLApi> api_;
94 const char* EGLApiTest::fake_extension_string_ = "";
95 const char* EGLApiTest::fake_client_extension_string_ = "";
97 TEST_F(EGLApiTest, DisabledExtensionBitTest) {
98 static const char* kFakeExtensions = "EGL_KHR_fence_sync";
99 static const char* kFakeClientExtensions = "";
100 static const char* kFakeDisabledExtensions = "EGL_KHR_fence_sync";
102 SetFakeExtensionString(kFakeExtensions, kFakeClientExtensions);
103 InitializeAPI(nullptr);
105 EXPECT_TRUE(g_driver_egl.ext.b_EGL_KHR_fence_sync);
107 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
108 command_line.AppendSwitchASCII(switches::kDisableGLExtensions,
109 kFakeDisabledExtensions);
110 InitializeAPI(&command_line);
112 EXPECT_FALSE(g_driver_egl.ext.b_EGL_KHR_fence_sync);
115 TEST_F(EGLApiTest, DisabledExtensionStringTest) {
116 static const char* kFakeExtensions = "EGL_EXT_1 EGL_EXT_2"
117 " EGL_EXT_3 EGL_EXT_4";
118 static const char* kFakeClientExtensions =
119 "EGL_CLIENT_EXT_1 EGL_CLIENT_EXT_2";
120 static const char* kFakeDisabledExtensions =
121 "EGL_EXT_1,EGL_EXT_2,EGL_FAKE,EGL_CLIENT_EXT_1";
122 static const char* kFilteredExtensions = "EGL_EXT_3 EGL_EXT_4";
123 static const char* kFilteredClientExtensions = "EGL_CLIENT_EXT_2";
125 SetFakeExtensionString(kFakeExtensions, kFakeClientExtensions);
126 InitializeAPI(nullptr);
128 EXPECT_STREQ(kFakeClientExtensions, GetExtensions().first);
129 EXPECT_STREQ(kFakeExtensions, GetExtensions().second);
131 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
132 command_line.AppendSwitchASCII(switches::kDisableGLExtensions,
133 kFakeDisabledExtensions);
134 InitializeAPI(&command_line);
136 EXPECT_STREQ(kFilteredClientExtensions, GetExtensions().first);
137 EXPECT_STREQ(kFilteredExtensions, GetExtensions().second);
140 } // namespace gfx