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.
7 #include "base/memory/scoped_ptr.h"
8 #include "gpu/config/gpu_control_list.h"
9 #include "gpu/config/gpu_info.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 const char kOsVersion
[] = "10.6.4";
13 const uint32 kIntelVendorId
= 0x8086;
14 const uint32 kNvidiaVendorId
= 0x10de;
16 #define LONG_STRING_CONST(...) #__VA_ARGS__
18 #define EXPECT_EMPTY_SET(feature_set) EXPECT_EQ(0u, feature_set.size())
19 #define EXPECT_SINGLE_FEATURE(feature_set, feature) \
20 EXPECT_TRUE(feature_set.size() == 1 && feature_set.count(feature) == 1)
24 enum TestFeatureType
{
26 TEST_FEATURE_1
= 1 << 2,
27 TEST_FEATURE_2
= 1 << 3,
30 class GpuControlListTest
: public testing::Test
{
32 GpuControlListTest() { }
34 virtual ~GpuControlListTest() { }
36 const GPUInfo
& gpu_info() const {
40 GpuControlList
* Create() {
41 GpuControlList
* rt
= new GpuControlList();
42 rt
->AddSupportedFeature("test_feature_0", TEST_FEATURE_0
);
43 rt
->AddSupportedFeature("test_feature_1", TEST_FEATURE_1
);
44 rt
->AddSupportedFeature("test_feature_2", TEST_FEATURE_2
);
49 virtual void SetUp() {
50 gpu_info_
.gpu
.vendor_id
= kNvidiaVendorId
;
51 gpu_info_
.gpu
.device_id
= 0x0640;
52 gpu_info_
.driver_vendor
= "NVIDIA";
53 gpu_info_
.driver_version
= "1.6.18";
54 gpu_info_
.driver_date
= "7-14-2009";
55 gpu_info_
.machine_model_name
= "MacBookPro";
56 gpu_info_
.machine_model_version
= "7.1";
57 gpu_info_
.gl_vendor
= "NVIDIA Corporation";
58 gpu_info_
.gl_renderer
= "NVIDIA GeForce GT 120 OpenGL Engine";
59 gpu_info_
.performance_stats
.graphics
= 5.0;
60 gpu_info_
.performance_stats
.gaming
= 5.0;
61 gpu_info_
.performance_stats
.overall
= 5.0;
64 virtual void TearDown() {
71 TEST_F(GpuControlListTest
, DefaultControlListSettings
) {
72 scoped_ptr
<GpuControlList
> control_list(Create());
73 // Default control list settings: all feature are allowed.
74 std::set
<int> features
= control_list
->MakeDecision(
75 GpuControlList::kOsMacosx
, kOsVersion
, gpu_info());
76 EXPECT_EMPTY_SET(features
);
79 TEST_F(GpuControlListTest
, EmptyControlList
) {
80 // Empty list: all features are allowed.
81 const std::string empty_list_json
= LONG_STRING_CONST(
83 "name": "gpu control list",
89 scoped_ptr
<GpuControlList
> control_list(Create());
91 EXPECT_TRUE(control_list
->LoadList(empty_list_json
,
92 GpuControlList::kAllOs
));
93 EXPECT_EQ("2.5", control_list
->version());
94 std::set
<int> features
= control_list
->MakeDecision(
95 GpuControlList::kOsMacosx
, kOsVersion
, gpu_info());
96 EXPECT_EMPTY_SET(features
);
99 TEST_F(GpuControlListTest
, DetailedEntryAndInvalidJson
) {
101 const std::string exact_list_json
= LONG_STRING_CONST(
103 "name": "gpu control list",
115 "vendor_id": "0x10de",
116 "device_id": ["0x0640"],
128 scoped_ptr
<GpuControlList
> control_list(Create());
130 EXPECT_TRUE(control_list
->LoadList(exact_list_json
, GpuControlList::kAllOs
));
131 std::set
<int> features
= control_list
->MakeDecision(
132 GpuControlList::kOsMacosx
, kOsVersion
, gpu_info());
133 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
135 // Invalid json input should not change the current control_list settings.
136 const std::string invalid_json
= "invalid";
138 EXPECT_FALSE(control_list
->LoadList(invalid_json
, GpuControlList::kAllOs
));
139 features
= control_list
->MakeDecision(
140 GpuControlList::kOsMacosx
, kOsVersion
, gpu_info());
141 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
142 std::vector
<uint32
> entries
;
143 control_list
->GetDecisionEntries(&entries
, false);
144 ASSERT_EQ(1u, entries
.size());
145 EXPECT_EQ(5u, entries
[0]);
146 EXPECT_EQ(5u, control_list
->max_entry_id());
149 TEST_F(GpuControlListTest
, VendorOnAllOsEntry
) {
150 // ControlList a vendor on all OS.
151 const std::string vendor_json
= LONG_STRING_CONST(
153 "name": "gpu control list",
158 "vendor_id": "0x10de",
166 scoped_ptr
<GpuControlList
> control_list(Create());
168 // ControlList entries won't be filtered to the current OS only upon loading.
169 EXPECT_TRUE(control_list
->LoadList(vendor_json
, GpuControlList::kAllOs
));
170 std::set
<int> features
= control_list
->MakeDecision(
171 GpuControlList::kOsMacosx
, kOsVersion
, gpu_info());
172 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
173 features
= control_list
->MakeDecision(
174 GpuControlList::kOsWin
, kOsVersion
, gpu_info());
175 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
176 features
= control_list
->MakeDecision(
177 GpuControlList::kOsLinux
, kOsVersion
, gpu_info());
178 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
179 #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_MACOSX) || \
181 // ControlList entries will be filtered to the current OS only upon loading.
182 EXPECT_TRUE(control_list
->LoadList(
183 vendor_json
, GpuControlList::kCurrentOsOnly
));
184 features
= control_list
->MakeDecision(
185 GpuControlList::kOsMacosx
, kOsVersion
, gpu_info());
186 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
187 features
= control_list
->MakeDecision(
188 GpuControlList::kOsWin
, kOsVersion
, gpu_info());
189 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
190 features
= control_list
->MakeDecision(
191 GpuControlList::kOsLinux
, kOsVersion
, gpu_info());
192 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
196 TEST_F(GpuControlListTest
, UnknownField
) {
197 const std::string unknown_field_json
= LONG_STRING_CONST(
199 "name": "gpu control list",
218 scoped_ptr
<GpuControlList
> control_list(Create());
220 EXPECT_FALSE(control_list
->LoadList(
221 unknown_field_json
, GpuControlList::kAllOs
));
224 TEST_F(GpuControlListTest
, UnknownExceptionField
) {
225 const std::string unknown_exception_field_json
= LONG_STRING_CONST(
227 "name": "gpu control list",
257 scoped_ptr
<GpuControlList
> control_list(Create());
259 EXPECT_FALSE(control_list
->LoadList(
260 unknown_exception_field_json
, GpuControlList::kAllOs
));
263 TEST_F(GpuControlListTest
, DisabledEntry
) {
264 const std::string disabled_json
= LONG_STRING_CONST(
266 "name": "gpu control list",
279 scoped_ptr
<GpuControlList
> control_list(Create());
280 EXPECT_TRUE(control_list
->LoadList(disabled_json
, GpuControlList::kAllOs
));
281 std::set
<int> features
= control_list
->MakeDecision(
282 GpuControlList::kOsWin
, kOsVersion
, gpu_info());
283 EXPECT_EMPTY_SET(features
);
284 std::vector
<uint32
> flag_entries
;
285 control_list
->GetDecisionEntries(&flag_entries
, false);
286 EXPECT_EQ(0u, flag_entries
.size());
287 control_list
->GetDecisionEntries(&flag_entries
, true);
288 EXPECT_EQ(1u, flag_entries
.size());
291 TEST_F(GpuControlListTest
, NeedsMoreInfoForExceptions
) {
292 const std::string json
= LONG_STRING_CONST(
294 "name": "gpu control list",
302 "vendor_id": "0x8086",
319 gpu_info
.gpu
.vendor_id
= kIntelVendorId
;
321 scoped_ptr
<GpuControlList
> control_list(Create());
322 EXPECT_TRUE(control_list
->LoadList(json
, GpuControlList::kAllOs
));
324 // The case this entry does not apply.
325 std::set
<int> features
= control_list
->MakeDecision(
326 GpuControlList::kOsMacosx
, kOsVersion
, gpu_info
);
327 EXPECT_EMPTY_SET(features
);
328 EXPECT_FALSE(control_list
->needs_more_info());
330 // The case this entry might apply, but need more info.
331 features
= control_list
->MakeDecision(
332 GpuControlList::kOsLinux
, kOsVersion
, gpu_info
);
333 EXPECT_EMPTY_SET(features
);
334 EXPECT_TRUE(control_list
->needs_more_info());
336 // The case we have full info, and the exception applies (so the entry
338 gpu_info
.gl_renderer
= "mesa";
339 features
= control_list
->MakeDecision(
340 GpuControlList::kOsLinux
, kOsVersion
, gpu_info
);
341 EXPECT_EMPTY_SET(features
);
342 EXPECT_FALSE(control_list
->needs_more_info());
344 // The case we have full info, and this entry applies.
345 gpu_info
.gl_renderer
= "my renderer";
346 features
= control_list
->MakeDecision(GpuControlList::kOsLinux
, kOsVersion
,
348 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
349 EXPECT_FALSE(control_list
->needs_more_info());
352 TEST_F(GpuControlListTest
, IgnorableEntries
) {
353 // If an entry will not change the control_list decisions, then it should not
354 // trigger the needs_more_info flag.
355 const std::string json
= LONG_STRING_CONST(
357 "name": "gpu control list",
365 "vendor_id": "0x8086",
375 "vendor_id": "0x8086",
388 gpu_info
.gpu
.vendor_id
= kIntelVendorId
;
390 scoped_ptr
<GpuControlList
> control_list(Create());
391 EXPECT_TRUE(control_list
->LoadList(json
, GpuControlList::kAllOs
));
392 std::set
<int> features
= control_list
->MakeDecision(
393 GpuControlList::kOsLinux
, kOsVersion
, gpu_info
);
394 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);
395 EXPECT_FALSE(control_list
->needs_more_info());
398 TEST_F(GpuControlListTest
, ExceptionWithoutVendorId
) {
399 const std::string json
= LONG_STRING_CONST(
401 "name": "gpu control list",
409 "vendor_id": "0x8086",
412 "device_id": ["0x2a06"],
419 "device_id": ["0x2a02"],
434 gpu_info
.gpu
.vendor_id
= kIntelVendorId
;
435 gpu_info
.gpu
.device_id
= 0x2a02;
436 gpu_info
.driver_version
= "9.1";
438 scoped_ptr
<GpuControlList
> control_list(Create());
439 EXPECT_TRUE(control_list
->LoadList(json
, GpuControlList::kAllOs
));
441 std::set
<int> features
= control_list
->MakeDecision(
442 GpuControlList::kOsLinux
, kOsVersion
, gpu_info
);
443 EXPECT_EMPTY_SET(features
);
445 gpu_info
.driver_version
= "9.0";
446 features
= control_list
->MakeDecision(
447 GpuControlList::kOsLinux
, kOsVersion
, gpu_info
);
448 EXPECT_SINGLE_FEATURE(features
, TEST_FEATURE_0
);