Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / gpu / config / gpu_control_list_unittest.cc
blob856eb4ac02b1992209ac6732475f5f7c46ef66ca
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 <vector>
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;
15 const uint32 kAmdVendorId = 0x10de;
17 #define LONG_STRING_CONST(...) #__VA_ARGS__
19 #define EXPECT_EMPTY_SET(feature_set) EXPECT_EQ(0u, feature_set.size())
20 #define EXPECT_SINGLE_FEATURE(feature_set, feature) \
21 EXPECT_TRUE(feature_set.size() == 1 && feature_set.count(feature) == 1)
23 namespace gpu {
25 enum TestFeatureType {
26 TEST_FEATURE_0 = 1,
27 TEST_FEATURE_1 = 1 << 2,
28 TEST_FEATURE_2 = 1 << 3,
31 class GpuControlListTest : public testing::Test {
32 public:
33 GpuControlListTest() { }
35 virtual ~GpuControlListTest() { }
37 const GPUInfo& gpu_info() const {
38 return gpu_info_;
41 GpuControlList* Create() {
42 GpuControlList* rt = new GpuControlList();
43 rt->AddSupportedFeature("test_feature_0", TEST_FEATURE_0);
44 rt->AddSupportedFeature("test_feature_1", TEST_FEATURE_1);
45 rt->AddSupportedFeature("test_feature_2", TEST_FEATURE_2);
46 return rt;
49 protected:
50 virtual void SetUp() {
51 gpu_info_.gpu.vendor_id = kNvidiaVendorId;
52 gpu_info_.gpu.device_id = 0x0640;
53 gpu_info_.driver_vendor = "NVIDIA";
54 gpu_info_.driver_version = "1.6.18";
55 gpu_info_.driver_date = "7-14-2009";
56 gpu_info_.machine_model_name = "MacBookPro";
57 gpu_info_.machine_model_version = "7.1";
58 gpu_info_.gl_vendor = "NVIDIA Corporation";
59 gpu_info_.gl_renderer = "NVIDIA GeForce GT 120 OpenGL Engine";
60 gpu_info_.performance_stats.graphics = 5.0;
61 gpu_info_.performance_stats.gaming = 5.0;
62 gpu_info_.performance_stats.overall = 5.0;
65 virtual void TearDown() {
68 private:
69 GPUInfo gpu_info_;
72 TEST_F(GpuControlListTest, DefaultControlListSettings) {
73 scoped_ptr<GpuControlList> control_list(Create());
74 // Default control list settings: all feature are allowed.
75 std::set<int> features = control_list->MakeDecision(
76 GpuControlList::kOsMacosx, kOsVersion, gpu_info());
77 EXPECT_EMPTY_SET(features);
80 TEST_F(GpuControlListTest, EmptyControlList) {
81 // Empty list: all features are allowed.
82 const std::string empty_list_json = LONG_STRING_CONST(
84 "name": "gpu control list",
85 "version": "2.5",
86 "entries": [
90 scoped_ptr<GpuControlList> control_list(Create());
92 EXPECT_TRUE(control_list->LoadList(empty_list_json,
93 GpuControlList::kAllOs));
94 EXPECT_EQ("2.5", control_list->version());
95 std::set<int> features = control_list->MakeDecision(
96 GpuControlList::kOsMacosx, kOsVersion, gpu_info());
97 EXPECT_EMPTY_SET(features);
100 TEST_F(GpuControlListTest, DetailedEntryAndInvalidJson) {
101 // exact setting.
102 const std::string exact_list_json = LONG_STRING_CONST(
104 "name": "gpu control list",
105 "version": "0.1",
106 "entries": [
108 "id": 5,
109 "os": {
110 "type": "macosx",
111 "version": {
112 "op": "=",
113 "value": "10.6.4"
116 "vendor_id": "0x10de",
117 "device_id": ["0x0640"],
118 "driver_version": {
119 "op": "=",
120 "value": "1.6.18"
122 "features": [
123 "test_feature_0"
129 scoped_ptr<GpuControlList> control_list(Create());
131 EXPECT_TRUE(control_list->LoadList(exact_list_json, GpuControlList::kAllOs));
132 std::set<int> features = control_list->MakeDecision(
133 GpuControlList::kOsMacosx, kOsVersion, gpu_info());
134 EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
136 // Invalid json input should not change the current control_list settings.
137 const std::string invalid_json = "invalid";
139 EXPECT_FALSE(control_list->LoadList(invalid_json, GpuControlList::kAllOs));
140 features = control_list->MakeDecision(
141 GpuControlList::kOsMacosx, kOsVersion, gpu_info());
142 EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
143 std::vector<uint32> entries;
144 control_list->GetDecisionEntries(&entries, false);
145 ASSERT_EQ(1u, entries.size());
146 EXPECT_EQ(5u, entries[0]);
147 EXPECT_EQ(5u, control_list->max_entry_id());
150 TEST_F(GpuControlListTest, VendorOnAllOsEntry) {
151 // ControlList a vendor on all OS.
152 const std::string vendor_json = LONG_STRING_CONST(
154 "name": "gpu control list",
155 "version": "0.1",
156 "entries": [
158 "id": 1,
159 "vendor_id": "0x10de",
160 "features": [
161 "test_feature_0"
167 scoped_ptr<GpuControlList> control_list(Create());
169 // ControlList entries won't be filtered to the current OS only upon loading.
170 EXPECT_TRUE(control_list->LoadList(vendor_json, GpuControlList::kAllOs));
171 std::set<int> features = control_list->MakeDecision(
172 GpuControlList::kOsMacosx, kOsVersion, gpu_info());
173 EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
174 features = control_list->MakeDecision(
175 GpuControlList::kOsWin, kOsVersion, gpu_info());
176 EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
177 features = control_list->MakeDecision(
178 GpuControlList::kOsLinux, kOsVersion, gpu_info());
179 EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
180 #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_MACOSX) || \
181 defined(OS_OPENBSD)
182 // ControlList entries will be filtered to the current OS only upon loading.
183 EXPECT_TRUE(control_list->LoadList(
184 vendor_json, GpuControlList::kCurrentOsOnly));
185 features = control_list->MakeDecision(
186 GpuControlList::kOsMacosx, kOsVersion, gpu_info());
187 EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
188 features = control_list->MakeDecision(
189 GpuControlList::kOsWin, kOsVersion, gpu_info());
190 EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
191 features = control_list->MakeDecision(
192 GpuControlList::kOsLinux, kOsVersion, gpu_info());
193 EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
194 #endif
197 TEST_F(GpuControlListTest, UnknownField) {
198 const std::string unknown_field_json = LONG_STRING_CONST(
200 "name": "gpu control list",
201 "version": "0.1",
202 "entries": [
204 "id": 1,
205 "unknown_field": 0,
206 "features": [
207 "test_feature_1"
211 "id": 2,
212 "features": [
213 "test_feature_0"
219 scoped_ptr<GpuControlList> control_list(Create());
221 EXPECT_FALSE(control_list->LoadList(
222 unknown_field_json, GpuControlList::kAllOs));
225 TEST_F(GpuControlListTest, UnknownExceptionField) {
226 const std::string unknown_exception_field_json = LONG_STRING_CONST(
228 "name": "gpu control list",
229 "version": "0.1",
230 "entries": [
232 "id": 1,
233 "unknown_field": 0,
234 "features": [
235 "test_feature_2"
239 "id": 2,
240 "exceptions": [
242 "unknown_field": 0
245 "features": [
246 "test_feature_1"
250 "id": 3,
251 "features": [
252 "test_feature_0"
258 scoped_ptr<GpuControlList> control_list(Create());
260 EXPECT_FALSE(control_list->LoadList(
261 unknown_exception_field_json, GpuControlList::kAllOs));
264 TEST_F(GpuControlListTest, DisabledEntry) {
265 const std::string disabled_json = LONG_STRING_CONST(
267 "name": "gpu control list",
268 "version": "0.1",
269 "entries": [
271 "id": 1,
272 "disabled": true,
273 "features": [
274 "test_feature_0"
280 scoped_ptr<GpuControlList> control_list(Create());
281 EXPECT_TRUE(control_list->LoadList(disabled_json, GpuControlList::kAllOs));
282 std::set<int> features = control_list->MakeDecision(
283 GpuControlList::kOsWin, kOsVersion, gpu_info());
284 EXPECT_EMPTY_SET(features);
285 std::vector<uint32> flag_entries;
286 control_list->GetDecisionEntries(&flag_entries, false);
287 EXPECT_EQ(0u, flag_entries.size());
288 control_list->GetDecisionEntries(&flag_entries, true);
289 EXPECT_EQ(1u, flag_entries.size());
292 TEST_F(GpuControlListTest, NeedsMoreInfo) {
293 const std::string json = LONG_STRING_CONST(
295 "name": "gpu control list",
296 "version": "0.1",
297 "entries": [
299 "id": 1,
300 "os": {
301 "type": "win"
303 "vendor_id": "0x10de",
304 "driver_version": {
305 "op": "<",
306 "value": "12"
308 "features": [
309 "test_feature_0"
315 GPUInfo gpu_info;
316 gpu_info.gpu.vendor_id = kNvidiaVendorId;
318 scoped_ptr<GpuControlList> control_list(Create());
319 EXPECT_TRUE(control_list->LoadList(json, GpuControlList::kAllOs));
321 std::set<int> features = control_list->MakeDecision(
322 GpuControlList::kOsWin, kOsVersion, gpu_info);
323 EXPECT_EMPTY_SET(features);
324 EXPECT_TRUE(control_list->needs_more_info());
325 std::vector<uint32> decision_entries;
326 control_list->GetDecisionEntries(&decision_entries, false);
327 EXPECT_EQ(0u, decision_entries.size());
329 gpu_info.driver_version = "11";
330 features = control_list->MakeDecision(
331 GpuControlList::kOsWin, kOsVersion, gpu_info);
332 EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
333 EXPECT_FALSE(control_list->needs_more_info());
334 control_list->GetDecisionEntries(&decision_entries, false);
335 EXPECT_EQ(1u, decision_entries.size());
338 TEST_F(GpuControlListTest, NeedsMoreInfoForExceptions) {
339 const std::string json = LONG_STRING_CONST(
341 "name": "gpu control list",
342 "version": "0.1",
343 "entries": [
345 "id": 1,
346 "os": {
347 "type": "linux"
349 "vendor_id": "0x8086",
350 "exceptions": [
352 "gl_renderer": ".*mesa.*"
355 "features": [
356 "test_feature_0"
362 GPUInfo gpu_info;
363 gpu_info.gpu.vendor_id = kIntelVendorId;
365 scoped_ptr<GpuControlList> control_list(Create());
366 EXPECT_TRUE(control_list->LoadList(json, GpuControlList::kAllOs));
368 // The case this entry does not apply.
369 std::set<int> features = control_list->MakeDecision(
370 GpuControlList::kOsMacosx, kOsVersion, gpu_info);
371 EXPECT_EMPTY_SET(features);
372 EXPECT_FALSE(control_list->needs_more_info());
374 // The case this entry might apply, but need more info.
375 features = control_list->MakeDecision(
376 GpuControlList::kOsLinux, kOsVersion, gpu_info);
377 EXPECT_EMPTY_SET(features);
378 EXPECT_TRUE(control_list->needs_more_info());
380 // The case we have full info, and the exception applies (so the entry
381 // does not apply).
382 gpu_info.gl_renderer = "mesa";
383 features = control_list->MakeDecision(
384 GpuControlList::kOsLinux, kOsVersion, gpu_info);
385 EXPECT_EMPTY_SET(features);
386 EXPECT_FALSE(control_list->needs_more_info());
388 // The case we have full info, and this entry applies.
389 gpu_info.gl_renderer = "my renderer";
390 features = control_list->MakeDecision(GpuControlList::kOsLinux, kOsVersion,
391 gpu_info);
392 EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
393 EXPECT_FALSE(control_list->needs_more_info());
396 TEST_F(GpuControlListTest, IgnorableEntries) {
397 // If an entry will not change the control_list decisions, then it should not
398 // trigger the needs_more_info flag.
399 const std::string json = LONG_STRING_CONST(
401 "name": "gpu control list",
402 "version": "0.1",
403 "entries": [
405 "id": 1,
406 "os": {
407 "type": "linux"
409 "vendor_id": "0x8086",
410 "features": [
411 "test_feature_0"
415 "id": 2,
416 "os": {
417 "type": "linux"
419 "vendor_id": "0x8086",
420 "driver_version": {
421 "op": "<",
422 "value": "10.7"
424 "features": [
425 "test_feature_0"
431 GPUInfo gpu_info;
432 gpu_info.gpu.vendor_id = kIntelVendorId;
434 scoped_ptr<GpuControlList> control_list(Create());
435 EXPECT_TRUE(control_list->LoadList(json, GpuControlList::kAllOs));
436 std::set<int> features = control_list->MakeDecision(
437 GpuControlList::kOsLinux, kOsVersion, gpu_info);
438 EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
439 EXPECT_FALSE(control_list->needs_more_info());
442 TEST_F(GpuControlListTest, ExceptionWithoutVendorId) {
443 const std::string json = LONG_STRING_CONST(
445 "name": "gpu control list",
446 "version": "0.1",
447 "entries": [
449 "id": 1,
450 "os": {
451 "type": "linux"
453 "vendor_id": "0x8086",
454 "exceptions": [
456 "device_id": ["0x2a06"],
457 "driver_version": {
458 "op": ">=",
459 "value": "8.1"
463 "device_id": ["0x2a02"],
464 "driver_version": {
465 "op": ">=",
466 "value": "9.1"
470 "features": [
471 "test_feature_0"
477 GPUInfo gpu_info;
478 gpu_info.gpu.vendor_id = kIntelVendorId;
479 gpu_info.gpu.device_id = 0x2a02;
480 gpu_info.driver_version = "9.1";
482 scoped_ptr<GpuControlList> control_list(Create());
483 EXPECT_TRUE(control_list->LoadList(json, GpuControlList::kAllOs));
485 std::set<int> features = control_list->MakeDecision(
486 GpuControlList::kOsLinux, kOsVersion, gpu_info);
487 EXPECT_EMPTY_SET(features);
489 gpu_info.driver_version = "9.0";
490 features = control_list->MakeDecision(
491 GpuControlList::kOsLinux, kOsVersion, gpu_info);
492 EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
495 TEST_F(GpuControlListTest, AMDSwitchable) {
496 GPUInfo gpu_info;
497 gpu_info.amd_switchable = true;
498 gpu_info.gpu.vendor_id = kAmdVendorId;
499 gpu_info.gpu.device_id = 0x6760;
500 GPUInfo::GPUDevice integrated_gpu;
501 integrated_gpu.vendor_id = kIntelVendorId;
502 integrated_gpu.device_id = 0x0116;
503 gpu_info.secondary_gpus.push_back(integrated_gpu);
505 { // amd_switchable_discrete entry
506 const std::string json= LONG_STRING_CONST(
508 "name": "gpu control list",
509 "version": "0.1",
510 "entries": [
512 "id": 1,
513 "os": {
514 "type": "win"
516 "multi_gpu_style": "amd_switchable_discrete",
517 "features": [
518 "test_feature_0"
525 scoped_ptr<GpuControlList> control_list(Create());
526 EXPECT_TRUE(control_list->LoadList(json, GpuControlList::kAllOs));
528 // Integrated GPU is active
529 gpu_info.gpu.active = false;
530 gpu_info.secondary_gpus[0].active = true;
531 std::set<int> features = control_list->MakeDecision(
532 GpuControlList::kOsWin, kOsVersion, gpu_info);
533 EXPECT_EMPTY_SET(features);
535 // Discrete GPU is active
536 gpu_info.gpu.active = true;
537 gpu_info.secondary_gpus[0].active = false;
538 features = control_list->MakeDecision(
539 GpuControlList::kOsWin, kOsVersion, gpu_info);
540 EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
543 { // amd_switchable_integrated entry
544 const std::string json= LONG_STRING_CONST(
546 "name": "gpu control list",
547 "version": "0.1",
548 "entries": [
550 "id": 1,
551 "os": {
552 "type": "win"
554 "multi_gpu_style": "amd_switchable_integrated",
555 "features": [
556 "test_feature_0"
563 scoped_ptr<GpuControlList> control_list(Create());
564 EXPECT_TRUE(control_list->LoadList(json, GpuControlList::kAllOs));
566 // Discrete GPU is active
567 gpu_info.gpu.active = true;
568 gpu_info.secondary_gpus[0].active = false;
569 std::set<int> features = control_list->MakeDecision(
570 GpuControlList::kOsWin, kOsVersion, gpu_info);
571 EXPECT_EMPTY_SET(features);
573 // Integrated GPU is active
574 gpu_info.gpu.active = false;
575 gpu_info.secondary_gpus[0].active = true;
576 features = control_list->MakeDecision(
577 GpuControlList::kOsWin, kOsVersion, gpu_info);
578 EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
580 // For non AMD switchable
581 gpu_info.amd_switchable = false;
582 features = control_list->MakeDecision(
583 GpuControlList::kOsWin, kOsVersion, gpu_info);
584 EXPECT_EMPTY_SET(features);
588 } // namespace gpu