1 // Copyright (c) 2013 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/json/json_reader.h"
6 #include "gpu/config/gpu_control_list.h"
7 #include "gpu/config/gpu_info.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 #define LONG_STRING_CONST(...) #__VA_ARGS__
14 enum TestFeatureType
{
20 class GpuControlListEntryTest
: public testing::Test
{
22 GpuControlListEntryTest() { }
23 virtual ~GpuControlListEntryTest() { }
25 const GPUInfo
& gpu_info() const {
29 typedef GpuControlList::ScopedGpuControlListEntry ScopedEntry
;
31 static ScopedEntry
GetEntryFromString(
32 const std::string
& json
, bool supports_feature_type_all
) {
33 scoped_ptr
<base::Value
> root
;
34 root
.reset(base::JSONReader::Read(json
));
35 DictionaryValue
* value
= NULL
;
36 if (root
.get() == NULL
|| !root
->GetAsDictionary(&value
))
39 GpuControlList::FeatureMap feature_map
;
40 feature_map
["test_feature_0"] = TEST_FEATURE_0
;
41 feature_map
["test_feature_1"] = TEST_FEATURE_1
;
42 feature_map
["test_feature_2"] = TEST_FEATURE_2
;
44 return GpuControlList::GpuControlListEntry::GetEntryFromValue(
45 value
, true, feature_map
, supports_feature_type_all
);
48 static ScopedEntry
GetEntryFromString(const std::string
& json
) {
49 return GetEntryFromString(json
, false);
52 virtual void SetUp() {
53 gpu_info_
.gpu
.vendor_id
= 0x10de;
54 gpu_info_
.gpu
.device_id
= 0x0640;
55 gpu_info_
.driver_vendor
= "NVIDIA";
56 gpu_info_
.driver_version
= "1.6.18";
57 gpu_info_
.driver_date
= "7-14-2009";
58 gpu_info_
.machine_model
= "MacBookPro 7.1";
59 gpu_info_
.gl_vendor
= "NVIDIA Corporation";
60 gpu_info_
.gl_renderer
= "NVIDIA GeForce GT 120 OpenGL Engine";
61 gpu_info_
.performance_stats
.graphics
= 5.0;
62 gpu_info_
.performance_stats
.gaming
= 5.0;
63 gpu_info_
.performance_stats
.overall
= 5.0;
70 TEST_F(GpuControlListEntryTest
, DetailedEntry
) {
71 const std::string json
= LONG_STRING_CONST(
74 "description": "test entry",
75 "cr_bugs": [1024, 678],
76 "webkit_bugs": [1950],
84 "vendor_id": "0x10de",
85 "device_id": ["0x0640"],
96 ScopedEntry
entry(GetEntryFromString(json
));
97 EXPECT_TRUE(entry
.get() != NULL
);
98 EXPECT_EQ(GpuControlList::kOsMacosx
, entry
->GetOsType());
99 EXPECT_FALSE(entry
->disabled());
100 EXPECT_EQ(5u, entry
->id());
101 EXPECT_STREQ("test entry", entry
->description().c_str());
102 EXPECT_EQ(2u, entry
->cr_bugs().size());
103 EXPECT_EQ(1024, entry
->cr_bugs()[0]);
104 EXPECT_EQ(678, entry
->cr_bugs()[1]);
105 EXPECT_EQ(1u, entry
->webkit_bugs().size());
106 EXPECT_EQ(1950, entry
->webkit_bugs()[0]);
107 EXPECT_EQ(1u, entry
->features().size());
108 EXPECT_EQ(1u, entry
->features().count(TEST_FEATURE_0
));
109 EXPECT_FALSE(entry
->contains_unknown_fields());
110 EXPECT_FALSE(entry
->contains_unknown_features());
111 EXPECT_FALSE(entry
->NeedsMoreInfo(gpu_info()));
112 EXPECT_TRUE(entry
->Contains(
113 GpuControlList::kOsMacosx
, "10.6.4", gpu_info()));
116 TEST_F(GpuControlListEntryTest
, VendorOnAllOsEntry
) {
117 const std::string json
= LONG_STRING_CONST(
120 "vendor_id": "0x10de",
126 ScopedEntry
entry(GetEntryFromString(json
));
127 EXPECT_TRUE(entry
.get() != NULL
);
128 EXPECT_EQ(GpuControlList::kOsAny
, entry
->GetOsType());
130 const GpuControlList::OsType os_type
[] = {
131 GpuControlList::kOsMacosx
,
132 GpuControlList::kOsWin
,
133 GpuControlList::kOsLinux
,
134 GpuControlList::kOsChromeOS
,
135 GpuControlList::kOsAndroid
137 for (size_t i
= 0; i
< arraysize(os_type
); ++i
)
138 EXPECT_TRUE(entry
->Contains(os_type
[i
], "10.6", gpu_info()));
141 TEST_F(GpuControlListEntryTest
, VendorOnLinuxEntry
) {
142 const std::string json
= LONG_STRING_CONST(
148 "vendor_id": "0x10de",
154 ScopedEntry
entry(GetEntryFromString(json
));
155 EXPECT_TRUE(entry
.get() != NULL
);
156 EXPECT_EQ(GpuControlList::kOsLinux
, entry
->GetOsType());
158 const GpuControlList::OsType os_type
[] = {
159 GpuControlList::kOsMacosx
,
160 GpuControlList::kOsWin
,
161 GpuControlList::kOsChromeOS
,
162 GpuControlList::kOsAndroid
164 for (size_t i
= 0; i
< arraysize(os_type
); ++i
)
165 EXPECT_FALSE(entry
->Contains(os_type
[i
], "10.6", gpu_info()));
166 EXPECT_TRUE(entry
->Contains(
167 GpuControlList::kOsLinux
, "10.6", gpu_info()));
170 TEST_F(GpuControlListEntryTest
, AllExceptNVidiaOnLinuxEntry
) {
171 const std::string json
= LONG_STRING_CONST(
179 "vendor_id": "0x10de"
187 ScopedEntry
entry(GetEntryFromString(json
));
188 EXPECT_TRUE(entry
.get() != NULL
);
189 EXPECT_EQ(GpuControlList::kOsLinux
, entry
->GetOsType());
191 const GpuControlList::OsType os_type
[] = {
192 GpuControlList::kOsMacosx
,
193 GpuControlList::kOsWin
,
194 GpuControlList::kOsLinux
,
195 GpuControlList::kOsChromeOS
,
196 GpuControlList::kOsAndroid
198 for (size_t i
= 0; i
< arraysize(os_type
); ++i
)
199 EXPECT_FALSE(entry
->Contains(os_type
[i
], "10.6", gpu_info()));
202 TEST_F(GpuControlListEntryTest
, AllExceptIntelOnLinuxEntry
) {
203 const std::string json
= LONG_STRING_CONST(
211 "vendor_id": "0x8086"
219 ScopedEntry
entry(GetEntryFromString(json
));
220 EXPECT_TRUE(entry
.get() != NULL
);
221 EXPECT_EQ(GpuControlList::kOsLinux
, entry
->GetOsType());
223 const GpuControlList::OsType os_type
[] = {
224 GpuControlList::kOsMacosx
,
225 GpuControlList::kOsWin
,
226 GpuControlList::kOsChromeOS
,
227 GpuControlList::kOsAndroid
229 for (size_t i
= 0; i
< arraysize(os_type
); ++i
)
230 EXPECT_FALSE(entry
->Contains(os_type
[i
], "10.6", gpu_info()));
231 EXPECT_TRUE(entry
->Contains(
232 GpuControlList::kOsLinux
, "10.6", gpu_info()));
235 TEST_F(GpuControlListEntryTest
, DateOnWindowsEntry
) {
236 const std::string json
= LONG_STRING_CONST(
251 ScopedEntry
entry(GetEntryFromString(json
));
252 EXPECT_TRUE(entry
.get() != NULL
);
253 EXPECT_EQ(GpuControlList::kOsWin
, entry
->GetOsType());
256 gpu_info
.driver_date
= "4-12-2010";
257 EXPECT_TRUE(entry
->Contains(
258 GpuControlList::kOsWin
, "10.6", gpu_info
));
259 gpu_info
.driver_date
= "5-8-2010";
260 EXPECT_FALSE(entry
->Contains(
261 GpuControlList::kOsWin
, "10.6", gpu_info
));
262 gpu_info
.driver_date
= "5-9-2010";
263 EXPECT_FALSE(entry
->Contains(
264 GpuControlList::kOsWin
, "10.6", gpu_info
));
267 TEST_F(GpuControlListEntryTest
, MultipleDevicesEntry
) {
268 const std::string json
= LONG_STRING_CONST(
271 "vendor_id": "0x10de",
272 "device_id": ["0x1023", "0x0640"],
278 ScopedEntry
entry(GetEntryFromString(json
));
279 EXPECT_TRUE(entry
.get() != NULL
);
280 EXPECT_EQ(GpuControlList::kOsAny
, entry
->GetOsType());
282 const GpuControlList::OsType os_type
[] = {
283 GpuControlList::kOsMacosx
,
284 GpuControlList::kOsWin
,
285 GpuControlList::kOsLinux
,
286 GpuControlList::kOsChromeOS
,
287 GpuControlList::kOsAndroid
289 for (size_t i
= 0; i
< arraysize(os_type
); ++i
)
290 EXPECT_TRUE(entry
->Contains(os_type
[i
], "10.6", gpu_info()));
293 TEST_F(GpuControlListEntryTest
, ChromeOSEntry
) {
294 const std::string json
= LONG_STRING_CONST(
305 ScopedEntry
entry(GetEntryFromString(json
));
306 EXPECT_TRUE(entry
.get() != NULL
);
307 EXPECT_EQ(GpuControlList::kOsChromeOS
, entry
->GetOsType());
309 const GpuControlList::OsType os_type
[] = {
310 GpuControlList::kOsMacosx
,
311 GpuControlList::kOsWin
,
312 GpuControlList::kOsLinux
,
313 GpuControlList::kOsAndroid
315 for (size_t i
= 0; i
< arraysize(os_type
); ++i
)
316 EXPECT_FALSE(entry
->Contains(os_type
[i
], "10.6", gpu_info()));
317 EXPECT_TRUE(entry
->Contains(
318 GpuControlList::kOsChromeOS
, "10.6", gpu_info()));
321 TEST_F(GpuControlListEntryTest
, MalformedVendor
) {
322 const std::string json
= LONG_STRING_CONST(
325 "vendor_id": "[0x10de]",
331 ScopedEntry
entry(GetEntryFromString(json
));
332 EXPECT_TRUE(entry
.get() == NULL
);
335 TEST_F(GpuControlListEntryTest
, UnknownFieldEntry
) {
336 const std::string json
= LONG_STRING_CONST(
345 ScopedEntry
entry(GetEntryFromString(json
));
346 EXPECT_TRUE(entry
.get() != NULL
);
347 EXPECT_TRUE(entry
->contains_unknown_fields());
348 EXPECT_FALSE(entry
->contains_unknown_features());
351 TEST_F(GpuControlListEntryTest
, UnknownExceptionFieldEntry
) {
352 const std::string json
= LONG_STRING_CONST(
365 ScopedEntry
entry(GetEntryFromString(json
));
366 EXPECT_TRUE(entry
.get() != NULL
);
367 EXPECT_TRUE(entry
->contains_unknown_fields());
368 EXPECT_FALSE(entry
->contains_unknown_features());
371 TEST_F(GpuControlListEntryTest
, UnknownFeatureEntry
) {
372 const std::string json
= LONG_STRING_CONST(
376 "some_unknown_feature",
381 ScopedEntry
entry(GetEntryFromString(json
));
382 EXPECT_TRUE(entry
.get() != NULL
);
383 EXPECT_FALSE(entry
->contains_unknown_fields());
384 EXPECT_TRUE(entry
->contains_unknown_features());
385 EXPECT_EQ(1u, entry
->features().size());
386 EXPECT_EQ(1u, entry
->features().count(TEST_FEATURE_0
));
388 const GpuControlList::OsType os_type
[] = {
389 GpuControlList::kOsMacosx
,
390 GpuControlList::kOsWin
,
391 GpuControlList::kOsLinux
,
392 GpuControlList::kOsChromeOS
,
393 GpuControlList::kOsAndroid
395 for (size_t i
= 0; i
< arraysize(os_type
); ++i
)
396 EXPECT_TRUE(entry
->Contains(os_type
[i
], "10.6", gpu_info()));
399 TEST_F(GpuControlListEntryTest
, GlVendorEntry
) {
400 const std::string json
= LONG_STRING_CONST(
412 ScopedEntry
entry(GetEntryFromString(json
));
413 EXPECT_TRUE(entry
.get() != NULL
);
415 const GpuControlList::OsType os_type
[] = {
416 GpuControlList::kOsMacosx
,
417 GpuControlList::kOsWin
,
418 GpuControlList::kOsLinux
,
419 GpuControlList::kOsChromeOS
,
420 GpuControlList::kOsAndroid
422 for (size_t i
= 0; i
< arraysize(os_type
); ++i
)
423 EXPECT_TRUE(entry
->Contains(os_type
[i
], "10.6", gpu_info()));
426 TEST_F(GpuControlListEntryTest
, GlRendererEntry
) {
427 const std::string json
= LONG_STRING_CONST(
439 ScopedEntry
entry(GetEntryFromString(json
));
440 EXPECT_TRUE(entry
.get() != NULL
);
442 const GpuControlList::OsType os_type
[] = {
443 GpuControlList::kOsMacosx
,
444 GpuControlList::kOsWin
,
445 GpuControlList::kOsLinux
,
446 GpuControlList::kOsChromeOS
,
447 GpuControlList::kOsAndroid
449 for (size_t i
= 0; i
< arraysize(os_type
); ++i
)
450 EXPECT_TRUE(entry
->Contains(os_type
[i
], "10.6", gpu_info()));
453 TEST_F(GpuControlListEntryTest
, PerfGraphicsEntry
) {
454 const std::string json
= LONG_STRING_CONST(
466 ScopedEntry
entry(GetEntryFromString(json
));
467 EXPECT_TRUE(entry
.get() != NULL
);
468 EXPECT_TRUE(entry
->Contains(GpuControlList::kOsWin
, "10.6", gpu_info()));
471 TEST_F(GpuControlListEntryTest
, PerfGamingEntry
) {
472 const std::string json
= LONG_STRING_CONST(
484 ScopedEntry
entry(GetEntryFromString(json
));
485 EXPECT_TRUE(entry
.get() != NULL
);
486 EXPECT_FALSE(entry
->Contains(GpuControlList::kOsWin
, "10.6", gpu_info()));
489 TEST_F(GpuControlListEntryTest
, PerfOverallEntry
) {
490 const std::string json
= LONG_STRING_CONST(
503 ScopedEntry
entry(GetEntryFromString(json
));
504 EXPECT_TRUE(entry
.get() != NULL
);
505 EXPECT_TRUE(entry
->Contains(GpuControlList::kOsWin
, "10.6", gpu_info()));
508 TEST_F(GpuControlListEntryTest
, DisabledEntry
) {
509 const std::string json
= LONG_STRING_CONST(
518 ScopedEntry
entry(GetEntryFromString(json
));
519 EXPECT_TRUE(entry
.get() != NULL
);
520 EXPECT_TRUE(entry
->disabled());
523 TEST_F(GpuControlListEntryTest
, OptimusEntry
) {
524 const std::string json
= LONG_STRING_CONST(
530 "multi_gpu_style": "optimus",
537 gpu_info
.optimus
= true;
539 ScopedEntry
entry(GetEntryFromString(json
));
540 EXPECT_TRUE(entry
.get() != NULL
);
541 EXPECT_EQ(GpuControlList::kOsLinux
, entry
->GetOsType());
542 EXPECT_TRUE(entry
->Contains(
543 GpuControlList::kOsLinux
, "10.6", gpu_info
));
546 TEST_F(GpuControlListEntryTest
, AMDSwitchableEntry
) {
547 const std::string json
= LONG_STRING_CONST(
553 "multi_gpu_style": "amd_switchable",
560 gpu_info
.amd_switchable
= true;
562 ScopedEntry
entry(GetEntryFromString(json
));
563 EXPECT_TRUE(entry
.get() != NULL
);
564 EXPECT_EQ(GpuControlList::kOsMacosx
, entry
->GetOsType());
565 EXPECT_TRUE(entry
->Contains(
566 GpuControlList::kOsMacosx
, "10.6", gpu_info
));
569 TEST_F(GpuControlListEntryTest
, LexicalDriverVersionEntry
) {
570 const std::string json
= LONG_STRING_CONST(
576 "vendor_id": "0x1002",
588 gpu_info
.gpu
.vendor_id
= 0x1002;
590 ScopedEntry
entry(GetEntryFromString(json
));
591 EXPECT_TRUE(entry
.get() != NULL
);
592 EXPECT_EQ(GpuControlList::kOsLinux
, entry
->GetOsType());
594 gpu_info
.driver_version
= "8.76";
595 EXPECT_TRUE(entry
->Contains(
596 GpuControlList::kOsLinux
, "10.6", gpu_info
));
598 gpu_info
.driver_version
= "8.768";
599 EXPECT_TRUE(entry
->Contains(
600 GpuControlList::kOsLinux
, "10.6", gpu_info
));
602 gpu_info
.driver_version
= "8.76.8";
603 EXPECT_TRUE(entry
->Contains(
604 GpuControlList::kOsLinux
, "10.6", gpu_info
));
607 TEST_F(GpuControlListEntryTest
, MultipleGPUsAnyEntry
) {
608 const std::string json
= LONG_STRING_CONST(
614 "vendor_id": "0x8086",
615 "device_id": ["0x0166"],
616 "multi_gpu_category": "any",
622 ScopedEntry
entry(GetEntryFromString(json
));
623 EXPECT_TRUE(entry
.get() != NULL
);
624 EXPECT_EQ(GpuControlList::kOsMacosx
, entry
->GetOsType());
627 gpu_info
.gpu
.vendor_id
= 0x10de;
628 gpu_info
.gpu
.device_id
= 0x1976;
629 EXPECT_FALSE(entry
->Contains(
630 GpuControlList::kOsMacosx
, "10.6", gpu_info
));
632 GPUInfo::GPUDevice gpu_device
;
633 gpu_device
.vendor_id
= 0x8086;
634 gpu_device
.device_id
= 0x0166;
635 gpu_info
.secondary_gpus
.push_back(gpu_device
);
636 EXPECT_TRUE(entry
->Contains(
637 GpuControlList::kOsMacosx
, "10.6", gpu_info
));
640 TEST_F(GpuControlListEntryTest
, MultipleGPUsSecondaryEntry
) {
641 const std::string json
= LONG_STRING_CONST(
647 "vendor_id": "0x8086",
648 "device_id": ["0x0166"],
649 "multi_gpu_category": "secondary",
655 ScopedEntry
entry(GetEntryFromString(json
));
656 EXPECT_TRUE(entry
.get() != NULL
);
657 EXPECT_EQ(GpuControlList::kOsMacosx
, entry
->GetOsType());
660 gpu_info
.gpu
.vendor_id
= 0x10de;
661 gpu_info
.gpu
.device_id
= 0x1976;
662 EXPECT_FALSE(entry
->Contains(
663 GpuControlList::kOsMacosx
, "10.6", gpu_info
));
665 GPUInfo::GPUDevice gpu_device
;
666 gpu_device
.vendor_id
= 0x8086;
667 gpu_device
.device_id
= 0x0166;
668 gpu_info
.secondary_gpus
.push_back(gpu_device
);
669 EXPECT_TRUE(entry
->Contains(
670 GpuControlList::kOsMacosx
, "10.6", gpu_info
));
673 TEST_F(GpuControlListEntryTest
, NeedsMoreInfoEntry
) {
674 const std::string json
= LONG_STRING_CONST(
677 "vendor_id": "0x8086",
687 ScopedEntry
entry(GetEntryFromString(json
));
688 EXPECT_TRUE(entry
.get() != NULL
);
691 gpu_info
.gpu
.vendor_id
= 0x8086;
692 EXPECT_TRUE(entry
->NeedsMoreInfo(gpu_info
));
694 gpu_info
.driver_version
= "10.6";
695 EXPECT_FALSE(entry
->NeedsMoreInfo(gpu_info
));
698 TEST_F(GpuControlListEntryTest
, NeedsMoreInfoForExceptionsEntry
) {
699 const std::string json
= LONG_STRING_CONST(
702 "vendor_id": "0x8086",
716 ScopedEntry
entry(GetEntryFromString(json
));
717 EXPECT_TRUE(entry
.get() != NULL
);
720 gpu_info
.gpu
.vendor_id
= 0x8086;
721 EXPECT_TRUE(entry
->NeedsMoreInfo(gpu_info
));
723 gpu_info
.gl_renderer
= "mesa";
724 EXPECT_FALSE(entry
->NeedsMoreInfo(gpu_info
));
727 TEST_F(GpuControlListEntryTest
, FeatureTypeAllEntry
) {
728 const std::string json
= LONG_STRING_CONST(
736 ScopedEntry
entry(GetEntryFromString(json
, true));
737 EXPECT_TRUE(entry
.get() != NULL
);
738 EXPECT_EQ(3u, entry
->features().size());
739 EXPECT_EQ(1u, entry
->features().count(TEST_FEATURE_0
));
740 EXPECT_EQ(1u, entry
->features().count(TEST_FEATURE_1
));
741 EXPECT_EQ(1u, entry
->features().count(TEST_FEATURE_2
));