Elim cr-checkbox
[chromium-blink-merge.git] / gpu / config / gpu_info_collector_unittest.cc
blob0f9b306f3e121c9688e4e6eb265dbbcb0313a3f1
1 // Copyright (c) 2012 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/memory/scoped_ptr.h"
6 #include "base/strings/string_split.h"
7 #include "gpu/config/gpu_info.h"
8 #include "gpu/config/gpu_info_collector.h"
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/gl/gl_implementation.h"
12 #include "ui/gl/gl_mock.h"
13 #include "ui/gl/test/gl_surface_test_support.h"
15 using ::gfx::MockGLInterface;
16 using ::testing::Return;
17 using ::testing::SetArgPointee;
18 using ::testing::_;
20 namespace {
22 // Allows testing of all configurations on all operating systems.
23 enum MockedOperatingSystemKind {
24 kMockedAndroid,
25 kMockedLinux,
26 kMockedMacOSX,
27 kMockedWindows
30 } // anonymous namespace
32 namespace gpu {
34 static const MockedOperatingSystemKind kMockedOperatingSystemKinds[] = {
35 kMockedAndroid,
36 kMockedLinux,
37 kMockedMacOSX,
38 kMockedWindows
41 class GPUInfoCollectorTest
42 : public testing::Test,
43 public ::testing::WithParamInterface<MockedOperatingSystemKind> {
44 public:
45 GPUInfoCollectorTest() {}
46 ~GPUInfoCollectorTest() override {}
48 void SetUp() override {
49 testing::Test::SetUp();
50 gfx::SetGLGetProcAddressProc(gfx::MockGLInterface::GetGLProcAddress);
51 gfx::GLSurfaceTestSupport::InitializeOneOffWithMockBindings();
52 gl_.reset(new ::testing::StrictMock< ::gfx::MockGLInterface>());
53 ::gfx::MockGLInterface::SetGLInterface(gl_.get());
54 switch (GetParam()) {
55 case kMockedAndroid: {
56 test_values_.gpu.vendor_id = 0; // not implemented
57 test_values_.gpu.device_id = 0; // not implemented
58 test_values_.driver_vendor = ""; // not implemented
59 test_values_.driver_version = "14.0";
60 test_values_.pixel_shader_version = "1.00";
61 test_values_.vertex_shader_version = "1.00";
62 test_values_.gl_renderer = "Adreno (TM) 320";
63 test_values_.gl_vendor = "Qualcomm";
64 test_values_.gl_version = "OpenGL ES 2.0 V@14.0 AU@04.02 (CL@3206)";
65 test_values_.gl_extensions =
66 "GL_OES_packed_depth_stencil GL_EXT_texture_format_BGRA8888 "
67 "GL_EXT_read_format_bgra";
68 gl_shading_language_version_ = "1.00";
69 break;
71 case kMockedLinux: {
72 test_values_.gpu.vendor_id = 0x10de;
73 test_values_.gpu.device_id = 0x0658;
74 test_values_.driver_vendor = "NVIDIA";
75 test_values_.driver_version = "195.36.24";
76 test_values_.pixel_shader_version = "1.50";
77 test_values_.vertex_shader_version = "1.50";
78 test_values_.gl_renderer = "Quadro FX 380/PCI/SSE2";
79 test_values_.gl_vendor = "NVIDIA Corporation";
80 test_values_.gl_version = "3.2.0 NVIDIA 195.36.24";
81 test_values_.gl_extensions =
82 "GL_OES_packed_depth_stencil GL_EXT_texture_format_BGRA8888 "
83 "GL_EXT_read_format_bgra";
84 gl_shading_language_version_ = "1.50 NVIDIA via Cg compiler";
85 break;
87 case kMockedMacOSX: {
88 test_values_.gpu.vendor_id = 0x10de;
89 test_values_.gpu.device_id = 0x0640;
90 test_values_.driver_vendor = ""; // not implemented
91 test_values_.driver_version = "1.6.18";
92 test_values_.pixel_shader_version = "1.20";
93 test_values_.vertex_shader_version = "1.20";
94 test_values_.gl_renderer = "NVIDIA GeForce GT 120 OpenGL Engine";
95 test_values_.gl_vendor = "NVIDIA Corporation";
96 test_values_.gl_version = "2.1 NVIDIA-1.6.18";
97 test_values_.gl_extensions =
98 "GL_OES_packed_depth_stencil GL_EXT_texture_format_BGRA8888 "
99 "GL_EXT_read_format_bgra";
100 gl_shading_language_version_ = "1.20 ";
101 break;
103 case kMockedWindows: {
104 test_values_.gpu.vendor_id = 0x10de;
105 test_values_.gpu.device_id = 0x0658;
106 test_values_.driver_vendor = ""; // not implemented
107 test_values_.driver_version = "";
108 test_values_.pixel_shader_version = "1.40";
109 test_values_.vertex_shader_version = "1.40";
110 test_values_.gl_renderer = "Quadro FX 380/PCI/SSE2";
111 test_values_.gl_vendor = "NVIDIA Corporation";
112 test_values_.gl_version = "3.1.0";
113 test_values_.gl_extensions =
114 "GL_OES_packed_depth_stencil GL_EXT_texture_format_BGRA8888 "
115 "GL_EXT_read_format_bgra";
116 gl_shading_language_version_ = "1.40 NVIDIA via Cg compiler";
117 break;
119 default: {
120 NOTREACHED();
121 break;
125 test_values_.can_lose_context = false;
127 EXPECT_CALL(*gl_, GetString(GL_VERSION))
128 .WillRepeatedly(Return(reinterpret_cast<const GLubyte*>(
129 test_values_.gl_version.c_str())));
131 // Now that that expectation is set up, we can call this helper function.
132 if (gfx::WillUseGLGetStringForExtensions()) {
133 EXPECT_CALL(*gl_, GetString(GL_EXTENSIONS))
134 .WillRepeatedly(Return(reinterpret_cast<const GLubyte*>(
135 test_values_.gl_extensions.c_str())));
136 } else {
137 split_extensions_.clear();
138 split_extensions_ = base::SplitString(
139 test_values_.gl_extensions, " ",
140 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
141 EXPECT_CALL(*gl_, GetIntegerv(GL_NUM_EXTENSIONS, _))
142 .WillRepeatedly(SetArgPointee<1>(split_extensions_.size()));
143 for (size_t ii = 0; ii < split_extensions_.size(); ++ii) {
144 EXPECT_CALL(*gl_, GetStringi(GL_EXTENSIONS, ii))
145 .WillRepeatedly(Return(reinterpret_cast<const uint8*>(
146 split_extensions_[ii].c_str())));
149 EXPECT_CALL(*gl_, GetString(GL_SHADING_LANGUAGE_VERSION))
150 .WillRepeatedly(Return(reinterpret_cast<const GLubyte*>(
151 gl_shading_language_version_)));
152 EXPECT_CALL(*gl_, GetString(GL_VENDOR))
153 .WillRepeatedly(Return(reinterpret_cast<const GLubyte*>(
154 test_values_.gl_vendor.c_str())));
155 EXPECT_CALL(*gl_, GetString(GL_RENDERER))
156 .WillRepeatedly(Return(reinterpret_cast<const GLubyte*>(
157 test_values_.gl_renderer.c_str())));
158 EXPECT_CALL(*gl_, GetIntegerv(GL_MAX_SAMPLES, _))
159 .WillOnce(SetArgPointee<1>(8))
160 .RetiresOnSaturation();
163 void TearDown() override {
164 ::gfx::MockGLInterface::SetGLInterface(NULL);
165 gl_.reset();
166 gfx::ClearGLBindings();
168 testing::Test::TearDown();
171 public:
172 // Use StrictMock to make 100% sure we know how GL will be called.
173 scoped_ptr< ::testing::StrictMock< ::gfx::MockGLInterface> > gl_;
174 GPUInfo test_values_;
175 const char* gl_shading_language_version_ = nullptr;
177 // Persistent storage is needed for the split extension string.
178 std::vector<std::string> split_extensions_;
181 INSTANTIATE_TEST_CASE_P(GPUConfig,
182 GPUInfoCollectorTest,
183 ::testing::ValuesIn(kMockedOperatingSystemKinds));
185 // TODO(rlp): Test the vendor and device id collection if deemed necessary as
186 // it involves several complicated mocks for each platform.
188 // TODO(kbr): This test still has platform-dependent behavior because
189 // CollectDriverInfoGL behaves differently per platform. This should
190 // be fixed.
191 TEST_P(GPUInfoCollectorTest, CollectGraphicsInfoGL) {
192 GPUInfo gpu_info;
193 CollectGraphicsInfoGL(&gpu_info);
194 #if defined(OS_WIN)
195 if (GetParam() == kMockedWindows) {
196 EXPECT_EQ(test_values_.driver_vendor,
197 gpu_info.driver_vendor);
198 // Skip testing the driver version on Windows because it's
199 // obtained from the bot's registry.
201 #elif defined(OS_MACOSX)
202 if (GetParam() == kMockedMacOSX) {
203 EXPECT_EQ(test_values_.driver_vendor,
204 gpu_info.driver_vendor);
205 EXPECT_EQ(test_values_.driver_version,
206 gpu_info.driver_version);
208 #elif defined(OS_ANDROID)
209 if (GetParam() == kMockedAndroid) {
210 EXPECT_EQ(test_values_.driver_vendor,
211 gpu_info.driver_vendor);
212 EXPECT_EQ(test_values_.driver_version,
213 gpu_info.driver_version);
215 #else // defined (OS_LINUX)
216 if (GetParam() == kMockedLinux) {
217 EXPECT_EQ(test_values_.driver_vendor,
218 gpu_info.driver_vendor);
219 EXPECT_EQ(test_values_.driver_version,
220 gpu_info.driver_version);
222 #endif
224 EXPECT_EQ(test_values_.pixel_shader_version,
225 gpu_info.pixel_shader_version);
226 EXPECT_EQ(test_values_.vertex_shader_version,
227 gpu_info.vertex_shader_version);
228 EXPECT_EQ(test_values_.gl_version, gpu_info.gl_version);
229 EXPECT_EQ(test_values_.gl_renderer, gpu_info.gl_renderer);
230 EXPECT_EQ(test_values_.gl_vendor, gpu_info.gl_vendor);
231 EXPECT_EQ(test_values_.gl_extensions, gpu_info.gl_extensions);
234 class CollectDriverInfoGLTest : public testing::Test {
235 public:
236 CollectDriverInfoGLTest() {}
237 ~CollectDriverInfoGLTest() override {}
239 void SetUp() override {}
240 void TearDown() override {}
243 TEST_F(CollectDriverInfoGLTest, CollectDriverInfoGL) {
244 const struct {
245 const char* gl_renderer;
246 const char* gl_vendor;
247 const char* gl_version;
248 const char* expected_driver_version;
249 } kTestStrings[] = {
250 #if defined(OS_ANDROID)
251 {"Adreno (TM) 320",
252 "Qualcomm",
253 "OpenGL ES 2.0 V@14.0 AU@04.02 (CL@3206)",
254 "14.0"},
255 {"Adreno (TM) 420", "Qualcomm", "OpenGL ES 3.0 V@84.0 AU@ (CL@)", "84.0"},
256 {"PowerVR Rogue G6430",
257 "Imagination Technologies",
258 "OpenGL ES 3.1 build 1.4@3283119",
259 "1.4"},
260 {"Mali-T604", "ARM", "OpenGL ES 3.1", "0"},
261 {"NVIDIA Tegra",
262 "NVIDIA Corporation",
263 "OpenGL ES 3.1 NVIDIA 343.00",
264 "343.00"},
265 {"NVIDIA Tegra 3",
266 "NVIDIA Corporation",
267 "OpenGL ES 2.0 14.01003",
268 "14.01003"},
269 {"random GPU",
270 "random vendor",
271 "OpenGL ES 2.0 with_long_version_string=1.2.3.4",
272 "1.2"},
273 {"random GPU",
274 "random vendor",
275 "OpenGL ES 2.0 with_short_version_string=1",
276 "0"},
277 {"random GPU",
278 "random vendor",
279 "OpenGL ES 2.0 with_no_version_string",
280 "0"},
281 #elif defined(OS_MACOSX)
282 {"Intel Iris Pro OpenGL Engine",
283 "Intel Inc.",
284 "2.1 INTEL-10.6.20",
285 "10.6.20"},
286 #elif defined(OS_LINUX)
287 {"Quadro K2000/PCIe/SSE2",
288 "NVIDIA Corporation",
289 "4.4.0 NVIDIA 331.79",
290 "331.79"},
291 #endif
292 {NULL, NULL, NULL, NULL}
295 GPUInfo gpu_info;
296 for (int i = 0; kTestStrings[i].gl_renderer != NULL; ++i) {
297 gpu_info.gl_renderer = kTestStrings[i].gl_renderer;
298 gpu_info.gl_vendor = kTestStrings[i].gl_vendor;
299 gpu_info.gl_version = kTestStrings[i].gl_version;
300 EXPECT_EQ(CollectDriverInfoGL(&gpu_info), kCollectInfoSuccess);
301 EXPECT_EQ(gpu_info.driver_version, kTestStrings[i].expected_driver_version);
305 } // namespace gpu