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. 4 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" 9 10 #define LONG_STRING_CONST(...) #__VA_ARGS__ 11 12 namespace gpu { 13 14 enum TestFeatureType { 15 TEST_FEATURE_0 = 0, 16 TEST_FEATURE_1, 17 TEST_FEATURE_2 18 }; 19 20 class GpuControlListEntryTest : public testing::Test { 21 public: 22 GpuControlListEntryTest() { } 23 virtual ~GpuControlListEntryTest() { } 24 25 const GPUInfo& gpu_info() const { 26 return gpu_info_; 27 } 28 29 typedef GpuControlList::ScopedGpuControlListEntry ScopedEntry; 30 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 base::DictionaryValue* value = NULL; 36 if (root.get() == NULL || !root->GetAsDictionary(&value)) 37 return NULL; 38 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; 43 44 return GpuControlList::GpuControlListEntry::GetEntryFromValue( 45 value, true, feature_map, supports_feature_type_all); 46 } 47 48 static ScopedEntry GetEntryFromString(const std::string& json) { 49 return GetEntryFromString(json, false); 50 } 51 52 virtual void SetUp() { 53 gpu_info_.gpu.vendor_id = 0x10de; 54 gpu_info_.gpu.device_id = 0x0640; 55 gpu_info_.gpu.active = true; 56 gpu_info_.driver_vendor = "NVIDIA"; 57 gpu_info_.driver_version = "1.6.18"; 58 gpu_info_.driver_date = "7-14-2009"; 59 gpu_info_.gl_version = "2.1 NVIDIA-8.24.11 310.90.9b01"; 60 gpu_info_.gl_vendor = "NVIDIA Corporation"; 61 gpu_info_.gl_renderer = "NVIDIA GeForce GT 120 OpenGL Engine"; 62 gpu_info_.performance_stats.graphics = 5.0; 63 gpu_info_.performance_stats.gaming = 5.0; 64 gpu_info_.performance_stats.overall = 5.0; 65 } 66 67 protected: 68 GPUInfo gpu_info_; 69 }; 70 71 TEST_F(GpuControlListEntryTest, DetailedEntry) { 72 const std::string json = LONG_STRING_CONST( 73 { 74 "id": 5, 75 "description": "test entry", 76 "cr_bugs": [1024, 678], 77 "webkit_bugs": [1950], 78 "os": { 79 "type": "macosx", 80 "version": { 81 "op": "=", 82 "value": "10.6.4" 83 } 84 }, 85 "vendor_id": "0x10de", 86 "device_id": ["0x0640"], 87 "driver_version": { 88 "op": "=", 89 "value": "1.6.18" 90 }, 91 "features": [ 92 "test_feature_0" 93 ] 94 } 95 ); 96 97 ScopedEntry entry(GetEntryFromString(json)); 98 EXPECT_TRUE(entry.get() != NULL); 99 EXPECT_EQ(GpuControlList::kOsMacosx, entry->GetOsType()); 100 EXPECT_FALSE(entry->disabled()); 101 EXPECT_EQ(5u, entry->id()); 102 EXPECT_STREQ("test entry", entry->description().c_str()); 103 EXPECT_EQ(2u, entry->cr_bugs().size()); 104 EXPECT_EQ(1024, entry->cr_bugs()[0]); 105 EXPECT_EQ(678, entry->cr_bugs()[1]); 106 EXPECT_EQ(1u, entry->webkit_bugs().size()); 107 EXPECT_EQ(1950, entry->webkit_bugs()[0]); 108 EXPECT_EQ(1u, entry->features().size()); 109 EXPECT_EQ(1u, entry->features().count(TEST_FEATURE_0)); 110 EXPECT_FALSE(entry->NeedsMoreInfo(gpu_info())); 111 EXPECT_TRUE(entry->Contains( 112 GpuControlList::kOsMacosx, "10.6.4", gpu_info())); 113 } 114 115 TEST_F(GpuControlListEntryTest, VendorOnAllOsEntry) { 116 const std::string json = LONG_STRING_CONST( 117 { 118 "id": 1, 119 "vendor_id": "0x10de", 120 "features": [ 121 "test_feature_0" 122 ] 123 } 124 ); 125 ScopedEntry entry(GetEntryFromString(json)); 126 EXPECT_TRUE(entry.get() != NULL); 127 EXPECT_EQ(GpuControlList::kOsAny, entry->GetOsType()); 128 129 const GpuControlList::OsType os_type[] = { 130 GpuControlList::kOsMacosx, 131 GpuControlList::kOsWin, 132 GpuControlList::kOsLinux, 133 GpuControlList::kOsChromeOS, 134 GpuControlList::kOsAndroid 135 }; 136 for (size_t i = 0; i < arraysize(os_type); ++i) 137 EXPECT_TRUE(entry->Contains(os_type[i], "10.6", gpu_info())); 138 } 139 140 TEST_F(GpuControlListEntryTest, VendorOnLinuxEntry) { 141 const std::string json = LONG_STRING_CONST( 142 { 143 "id": 1, 144 "os": { 145 "type": "linux" 146 }, 147 "vendor_id": "0x10de", 148 "features": [ 149 "test_feature_0" 150 ] 151 } 152 ); 153 ScopedEntry entry(GetEntryFromString(json)); 154 EXPECT_TRUE(entry.get() != NULL); 155 EXPECT_EQ(GpuControlList::kOsLinux, entry->GetOsType()); 156 157 const GpuControlList::OsType os_type[] = { 158 GpuControlList::kOsMacosx, 159 GpuControlList::kOsWin, 160 GpuControlList::kOsChromeOS, 161 GpuControlList::kOsAndroid 162 }; 163 for (size_t i = 0; i < arraysize(os_type); ++i) 164 EXPECT_FALSE(entry->Contains(os_type[i], "10.6", gpu_info())); 165 EXPECT_TRUE(entry->Contains( 166 GpuControlList::kOsLinux, "10.6", gpu_info())); 167 } 168 169 TEST_F(GpuControlListEntryTest, AllExceptNVidiaOnLinuxEntry) { 170 const std::string json = LONG_STRING_CONST( 171 { 172 "id": 1, 173 "os": { 174 "type": "linux" 175 }, 176 "exceptions": [ 177 { 178 "vendor_id": "0x10de" 179 } 180 ], 181 "features": [ 182 "test_feature_0" 183 ] 184 } 185 ); 186 ScopedEntry entry(GetEntryFromString(json)); 187 EXPECT_TRUE(entry.get() != NULL); 188 EXPECT_EQ(GpuControlList::kOsLinux, entry->GetOsType()); 189 190 const GpuControlList::OsType os_type[] = { 191 GpuControlList::kOsMacosx, 192 GpuControlList::kOsWin, 193 GpuControlList::kOsLinux, 194 GpuControlList::kOsChromeOS, 195 GpuControlList::kOsAndroid 196 }; 197 for (size_t i = 0; i < arraysize(os_type); ++i) 198 EXPECT_FALSE(entry->Contains(os_type[i], "10.6", gpu_info())); 199 } 200 201 TEST_F(GpuControlListEntryTest, AllExceptIntelOnLinuxEntry) { 202 const std::string json = LONG_STRING_CONST( 203 { 204 "id": 1, 205 "os": { 206 "type": "linux" 207 }, 208 "exceptions": [ 209 { 210 "vendor_id": "0x8086" 211 } 212 ], 213 "features": [ 214 "test_feature_0" 215 ] 216 } 217 ); 218 ScopedEntry entry(GetEntryFromString(json)); 219 EXPECT_TRUE(entry.get() != NULL); 220 EXPECT_EQ(GpuControlList::kOsLinux, entry->GetOsType()); 221 222 const GpuControlList::OsType os_type[] = { 223 GpuControlList::kOsMacosx, 224 GpuControlList::kOsWin, 225 GpuControlList::kOsChromeOS, 226 GpuControlList::kOsAndroid 227 }; 228 for (size_t i = 0; i < arraysize(os_type); ++i) 229 EXPECT_FALSE(entry->Contains(os_type[i], "10.6", gpu_info())); 230 EXPECT_TRUE(entry->Contains( 231 GpuControlList::kOsLinux, "10.6", gpu_info())); 232 } 233 234 TEST_F(GpuControlListEntryTest, DateOnWindowsEntry) { 235 const std::string json = LONG_STRING_CONST( 236 { 237 "id": 1, 238 "os": { 239 "type": "win" 240 }, 241 "driver_date": { 242 "op": "<", 243 "value": "2010.5.8" 244 }, 245 "features": [ 246 "test_feature_0" 247 ] 248 } 249 ); 250 ScopedEntry entry(GetEntryFromString(json)); 251 EXPECT_TRUE(entry.get() != NULL); 252 EXPECT_EQ(GpuControlList::kOsWin, entry->GetOsType()); 253 254 GPUInfo gpu_info; 255 gpu_info.driver_date = "4-12-2010"; 256 EXPECT_TRUE(entry->Contains( 257 GpuControlList::kOsWin, "10.6", gpu_info)); 258 gpu_info.driver_date = "5-8-2010"; 259 EXPECT_FALSE(entry->Contains( 260 GpuControlList::kOsWin, "10.6", gpu_info)); 261 gpu_info.driver_date = "5-9-2010"; 262 EXPECT_FALSE(entry->Contains( 263 GpuControlList::kOsWin, "10.6", gpu_info)); 264 } 265 266 TEST_F(GpuControlListEntryTest, MultipleDevicesEntry) { 267 const std::string json = LONG_STRING_CONST( 268 { 269 "id": 1, 270 "vendor_id": "0x10de", 271 "device_id": ["0x1023", "0x0640"], 272 "features": [ 273 "test_feature_0" 274 ] 275 } 276 ); 277 ScopedEntry entry(GetEntryFromString(json)); 278 EXPECT_TRUE(entry.get() != NULL); 279 EXPECT_EQ(GpuControlList::kOsAny, entry->GetOsType()); 280 281 const GpuControlList::OsType os_type[] = { 282 GpuControlList::kOsMacosx, 283 GpuControlList::kOsWin, 284 GpuControlList::kOsLinux, 285 GpuControlList::kOsChromeOS, 286 GpuControlList::kOsAndroid 287 }; 288 for (size_t i = 0; i < arraysize(os_type); ++i) 289 EXPECT_TRUE(entry->Contains(os_type[i], "10.6", gpu_info())); 290 } 291 292 TEST_F(GpuControlListEntryTest, ChromeOSEntry) { 293 const std::string json = LONG_STRING_CONST( 294 { 295 "id": 1, 296 "os": { 297 "type": "chromeos" 298 }, 299 "features": [ 300 "test_feature_0" 301 ] 302 } 303 ); 304 ScopedEntry entry(GetEntryFromString(json)); 305 EXPECT_TRUE(entry.get() != NULL); 306 EXPECT_EQ(GpuControlList::kOsChromeOS, entry->GetOsType()); 307 308 const GpuControlList::OsType os_type[] = { 309 GpuControlList::kOsMacosx, 310 GpuControlList::kOsWin, 311 GpuControlList::kOsLinux, 312 GpuControlList::kOsAndroid 313 }; 314 for (size_t i = 0; i < arraysize(os_type); ++i) 315 EXPECT_FALSE(entry->Contains(os_type[i], "10.6", gpu_info())); 316 EXPECT_TRUE(entry->Contains( 317 GpuControlList::kOsChromeOS, "10.6", gpu_info())); 318 } 319 320 TEST_F(GpuControlListEntryTest, MalformedVendor) { 321 const std::string json = LONG_STRING_CONST( 322 { 323 "id": 1, 324 "vendor_id": "[0x10de]", 325 "features": [ 326 "test_feature_0" 327 ] 328 } 329 ); 330 ScopedEntry entry(GetEntryFromString(json)); 331 EXPECT_TRUE(entry.get() == NULL); 332 } 333 334 TEST_F(GpuControlListEntryTest, UnknownFieldEntry) { 335 const std::string json = LONG_STRING_CONST( 336 { 337 "id": 1, 338 "unknown_field": 0, 339 "features": [ 340 "test_feature_0" 341 ] 342 } 343 ); 344 ScopedEntry entry(GetEntryFromString(json)); 345 EXPECT_TRUE(entry.get() == NULL); 346 } 347 348 TEST_F(GpuControlListEntryTest, UnknownExceptionFieldEntry) { 349 const std::string json = LONG_STRING_CONST( 350 { 351 "id": 2, 352 "exceptions": [ 353 { 354 "unknown_field": 0 355 } 356 ], 357 "features": [ 358 "test_feature_0" 359 ] 360 } 361 ); 362 ScopedEntry entry(GetEntryFromString(json)); 363 EXPECT_TRUE(entry.get() == NULL); 364 } 365 366 TEST_F(GpuControlListEntryTest, UnknownFeatureEntry) { 367 const std::string json = LONG_STRING_CONST( 368 { 369 "id": 1, 370 "features": [ 371 "some_unknown_feature", 372 "test_feature_0" 373 ] 374 } 375 ); 376 ScopedEntry entry(GetEntryFromString(json)); 377 EXPECT_TRUE(entry.get() == NULL); 378 } 379 380 TEST_F(GpuControlListEntryTest, GlVersionGLESEntry) { 381 const std::string json = LONG_STRING_CONST( 382 { 383 "id": 1, 384 "gl_type": "gles", 385 "gl_version": { 386 "op": "=", 387 "value": "3.0" 388 }, 389 "features": [ 390 "test_feature_0" 391 ] 392 } 393 ); 394 ScopedEntry entry(GetEntryFromString(json)); 395 EXPECT_TRUE(entry.get() != NULL); 396 397 GPUInfo gpu_info; 398 gpu_info.gl_version = "OpenGL ES 3.0 V (at) 66.0 AU@ (CL@)"; 399 EXPECT_TRUE(entry->Contains(GpuControlList::kOsAndroid, "4.4.2", gpu_info)); 400 401 gpu_info.gl_version = "OpenGL ES 3.1 V (at) 66.0 AU@ (CL@)"; 402 EXPECT_FALSE(entry->Contains(GpuControlList::kOsAndroid, "4.4.2", gpu_info)); 403 404 gpu_info.gl_version = "3.0 NVIDIA-8.24.11 310.90.9b01"; 405 EXPECT_FALSE(entry->Contains(GpuControlList::kOsMacosx, "10.9", gpu_info)); 406 407 gpu_info.gl_version = "OpenGL ES 3.0 (ANGLE 1.2.0.2450)"; 408 EXPECT_FALSE(entry->Contains(GpuControlList::kOsWin, "6.1", gpu_info)); 409 } 410 411 TEST_F(GpuControlListEntryTest, GlVersionANGLEEntry) { 412 const std::string json = LONG_STRING_CONST( 413 { 414 "id": 1, 415 "gl_type": "angle", 416 "gl_version": { 417 "op": ">", 418 "value": "2.0" 419 }, 420 "features": [ 421 "test_feature_0" 422 ] 423 } 424 ); 425 ScopedEntry entry(GetEntryFromString(json)); 426 EXPECT_TRUE(entry.get() != NULL); 427 428 GPUInfo gpu_info; 429 gpu_info.gl_version = "OpenGL ES 3.0 V (at) 66.0 AU@ (CL@)"; 430 EXPECT_FALSE(entry->Contains(GpuControlList::kOsAndroid, "4.4.2", gpu_info)); 431 432 gpu_info.gl_version = "3.0 NVIDIA-8.24.11 310.90.9b01"; 433 EXPECT_FALSE(entry->Contains(GpuControlList::kOsMacosx, "10.9", gpu_info)); 434 435 gpu_info.gl_version = "OpenGL ES 3.0 (ANGLE 1.2.0.2450)"; 436 EXPECT_TRUE(entry->Contains(GpuControlList::kOsWin, "6.1", gpu_info)); 437 438 gpu_info.gl_version = "OpenGL ES 2.0 (ANGLE 1.2.0.2450)"; 439 EXPECT_FALSE(entry->Contains(GpuControlList::kOsWin, "6.1", gpu_info)); 440 } 441 442 TEST_F(GpuControlListEntryTest, GlVersionGLEntry) { 443 const std::string json = LONG_STRING_CONST( 444 { 445 "id": 1, 446 "gl_type": "gl", 447 "gl_version": { 448 "op": "<", 449 "value": "4.0" 450 }, 451 "features": [ 452 "test_feature_0" 453 ] 454 } 455 ); 456 ScopedEntry entry(GetEntryFromString(json)); 457 EXPECT_TRUE(entry.get() != NULL); 458 459 GPUInfo gpu_info; 460 gpu_info.gl_version = "OpenGL ES 3.0 V (at) 66.0 AU@ (CL@)"; 461 EXPECT_FALSE(entry->Contains(GpuControlList::kOsAndroid, "4.4.2", gpu_info)); 462 463 gpu_info.gl_version = "3.0 NVIDIA-8.24.11 310.90.9b01"; 464 EXPECT_TRUE(entry->Contains(GpuControlList::kOsMacosx, "10.9", gpu_info)); 465 466 gpu_info.gl_version = "4.0 NVIDIA-8.24.11 310.90.9b01"; 467 EXPECT_FALSE(entry->Contains(GpuControlList::kOsMacosx, "10.9", gpu_info)); 468 469 gpu_info.gl_version = "OpenGL ES 3.0 (ANGLE 1.2.0.2450)"; 470 EXPECT_FALSE(entry->Contains(GpuControlList::kOsWin, "6.1", gpu_info)); 471 } 472 473 TEST_F(GpuControlListEntryTest, GlVendorEqual) { 474 const std::string json = LONG_STRING_CONST( 475 { 476 "id": 1, 477 "gl_vendor": "NVIDIA", 478 "features": [ 479 "test_feature_0" 480 ] 481 } 482 ); 483 ScopedEntry entry(GetEntryFromString(json)); 484 EXPECT_TRUE(entry.get() != NULL); 485 486 GPUInfo gpu_info; 487 gpu_info.gl_vendor = "NVIDIA"; 488 EXPECT_TRUE(entry->Contains( 489 GpuControlList::kOsMacosx, "10.9", gpu_info)); 490 491 // Case sensitive. 492 gpu_info.gl_vendor = "NVidia"; 493 EXPECT_FALSE(entry->Contains( 494 GpuControlList::kOsMacosx, "10.9", gpu_info)); 495 496 gpu_info.gl_vendor = "NVIDIA-x"; 497 EXPECT_FALSE(entry->Contains( 498 GpuControlList::kOsMacosx, "10.9", gpu_info)); 499 } 500 501 TEST_F(GpuControlListEntryTest, GlVendorWithDot) { 502 const std::string json = LONG_STRING_CONST( 503 { 504 "id": 1, 505 "gl_vendor": "X\\.Org.*", 506 "features": [ 507 "test_feature_0" 508 ] 509 } 510 ); 511 ScopedEntry entry(GetEntryFromString(json)); 512 EXPECT_TRUE(entry.get() != NULL); 513 514 GPUInfo gpu_info; 515 gpu_info.gl_vendor = "X.Org R300 Project"; 516 EXPECT_TRUE(entry->Contains( 517 GpuControlList::kOsLinux, "", gpu_info)); 518 519 gpu_info.gl_vendor = "X.Org"; 520 EXPECT_TRUE(entry->Contains( 521 GpuControlList::kOsLinux, "", gpu_info)); 522 } 523 524 TEST_F(GpuControlListEntryTest, GlRendererContains) { 525 const std::string json = LONG_STRING_CONST( 526 { 527 "id": 1, 528 "gl_renderer": ".*GeForce.*", 529 "features": [ 530 "test_feature_0" 531 ] 532 } 533 ); 534 ScopedEntry entry(GetEntryFromString(json)); 535 EXPECT_TRUE(entry.get() != NULL); 536 537 GPUInfo gpu_info; 538 gpu_info.gl_renderer = "NVIDIA GeForce GT 120 OpenGL Engine"; 539 EXPECT_TRUE(entry->Contains( 540 GpuControlList::kOsMacosx, "10.9", gpu_info)); 541 542 // Case sensitive. 543 gpu_info.gl_renderer = "NVIDIA GEFORCE GT 120 OpenGL Engine"; 544 EXPECT_FALSE(entry->Contains( 545 GpuControlList::kOsMacosx, "10.9", gpu_info)); 546 547 gpu_info.gl_renderer = "GeForce GT 120 OpenGL Engine"; 548 EXPECT_TRUE(entry->Contains( 549 GpuControlList::kOsMacosx, "10.9", gpu_info)); 550 551 gpu_info.gl_renderer = "NVIDIA GeForce"; 552 EXPECT_TRUE(entry->Contains( 553 GpuControlList::kOsMacosx, "10.9", gpu_info)); 554 555 gpu_info.gl_renderer = "NVIDIA Ge Force"; 556 EXPECT_FALSE(entry->Contains( 557 GpuControlList::kOsMacosx, "10.9", gpu_info)); 558 } 559 560 TEST_F(GpuControlListEntryTest, GlRendererCaseInsensitive) { 561 const std::string json = LONG_STRING_CONST( 562 { 563 "id": 1, 564 "gl_renderer": "(?i).*software.*", 565 "features": [ 566 "test_feature_0" 567 ] 568 } 569 ); 570 ScopedEntry entry(GetEntryFromString(json)); 571 EXPECT_TRUE(entry.get() != NULL); 572 573 GPUInfo gpu_info; 574 gpu_info.gl_renderer = "software rasterizer"; 575 EXPECT_TRUE(entry->Contains( 576 GpuControlList::kOsMacosx, "10.9", gpu_info)); 577 578 gpu_info.gl_renderer = "Software Rasterizer"; 579 EXPECT_TRUE(entry->Contains( 580 GpuControlList::kOsMacosx, "10.9", gpu_info)); 581 } 582 583 TEST_F(GpuControlListEntryTest, GlExtensionsEndWith) { 584 const std::string json = LONG_STRING_CONST( 585 { 586 "id": 1, 587 "gl_extensions": ".*GL_SUN_slice_accum", 588 "features": [ 589 "test_feature_0" 590 ] 591 } 592 ); 593 ScopedEntry entry(GetEntryFromString(json)); 594 EXPECT_TRUE(entry.get() != NULL); 595 596 GPUInfo gpu_info; 597 gpu_info.gl_extensions = "GL_SGIS_generate_mipmap " 598 "GL_SGIX_shadow " 599 "GL_SUN_slice_accum"; 600 EXPECT_TRUE(entry->Contains( 601 GpuControlList::kOsMacosx, "10.9", gpu_info)); 602 603 gpu_info.gl_extensions = "GL_SGIS_generate_mipmap " 604 "GL_SUN_slice_accum " 605 "GL_SGIX_shadow"; 606 EXPECT_FALSE(entry->Contains( 607 GpuControlList::kOsMacosx, "10.9", gpu_info)); 608 } 609 610 TEST_F(GpuControlListEntryTest, PerfGraphicsEntry) { 611 const std::string json = LONG_STRING_CONST( 612 { 613 "id": 1, 614 "perf_graphics": { 615 "op": "<", 616 "value": "6.0" 617 }, 618 "features": [ 619 "test_feature_0" 620 ] 621 } 622 ); 623 ScopedEntry entry(GetEntryFromString(json)); 624 EXPECT_TRUE(entry.get() != NULL); 625 EXPECT_TRUE(entry->Contains(GpuControlList::kOsWin, "10.6", gpu_info())); 626 } 627 628 TEST_F(GpuControlListEntryTest, PerfGamingEntry) { 629 const std::string json = LONG_STRING_CONST( 630 { 631 "id": 1, 632 "perf_graphics": { 633 "op": "<=", 634 "value": "4.0" 635 }, 636 "features": [ 637 "test_feature_0" 638 ] 639 } 640 ); 641 ScopedEntry entry(GetEntryFromString(json)); 642 EXPECT_TRUE(entry.get() != NULL); 643 EXPECT_FALSE(entry->Contains(GpuControlList::kOsWin, "10.6", gpu_info())); 644 } 645 646 TEST_F(GpuControlListEntryTest, PerfOverallEntry) { 647 const std::string json = LONG_STRING_CONST( 648 { 649 "id": 1, 650 "perf_overall": { 651 "op": "between", 652 "value": "1.0", 653 "value2": "9.0" 654 }, 655 "features": [ 656 "test_feature_0" 657 ] 658 } 659 ); 660 ScopedEntry entry(GetEntryFromString(json)); 661 EXPECT_TRUE(entry.get() != NULL); 662 EXPECT_TRUE(entry->Contains(GpuControlList::kOsWin, "10.6", gpu_info())); 663 } 664 665 TEST_F(GpuControlListEntryTest, DisabledEntry) { 666 const std::string json = LONG_STRING_CONST( 667 { 668 "id": 1, 669 "disabled": true, 670 "features": [ 671 "test_feature_0" 672 ] 673 } 674 ); 675 ScopedEntry entry(GetEntryFromString(json)); 676 EXPECT_TRUE(entry.get() != NULL); 677 EXPECT_TRUE(entry->disabled()); 678 } 679 680 TEST_F(GpuControlListEntryTest, OptimusEntry) { 681 const std::string json = LONG_STRING_CONST( 682 { 683 "id": 1, 684 "os": { 685 "type": "linux" 686 }, 687 "multi_gpu_style": "optimus", 688 "features": [ 689 "test_feature_0" 690 ] 691 } 692 ); 693 GPUInfo gpu_info; 694 gpu_info.optimus = true; 695 696 ScopedEntry entry(GetEntryFromString(json)); 697 EXPECT_TRUE(entry.get() != NULL); 698 EXPECT_EQ(GpuControlList::kOsLinux, entry->GetOsType()); 699 EXPECT_TRUE(entry->Contains( 700 GpuControlList::kOsLinux, "10.6", gpu_info)); 701 } 702 703 TEST_F(GpuControlListEntryTest, AMDSwitchableEntry) { 704 const std::string json = LONG_STRING_CONST( 705 { 706 "id": 1, 707 "os": { 708 "type": "macosx" 709 }, 710 "multi_gpu_style": "amd_switchable", 711 "features": [ 712 "test_feature_0" 713 ] 714 } 715 ); 716 GPUInfo gpu_info; 717 gpu_info.amd_switchable = true; 718 719 ScopedEntry entry(GetEntryFromString(json)); 720 EXPECT_TRUE(entry.get() != NULL); 721 EXPECT_EQ(GpuControlList::kOsMacosx, entry->GetOsType()); 722 EXPECT_TRUE(entry->Contains( 723 GpuControlList::kOsMacosx, "10.6", gpu_info)); 724 } 725 726 TEST_F(GpuControlListEntryTest, DriverVendorBeginWith) { 727 const std::string json = LONG_STRING_CONST( 728 { 729 "id": 1, 730 "driver_vendor": "NVIDIA.*", 731 "features": [ 732 "test_feature_0" 733 ] 734 } 735 ); 736 ScopedEntry entry(GetEntryFromString(json)); 737 EXPECT_TRUE(entry.get() != NULL); 738 739 GPUInfo gpu_info; 740 gpu_info.driver_vendor = "NVIDIA Corporation"; 741 EXPECT_TRUE(entry->Contains( 742 GpuControlList::kOsMacosx, "10.9", gpu_info)); 743 744 // Case sensitive. 745 gpu_info.driver_vendor = "NVidia Corporation"; 746 EXPECT_FALSE(entry->Contains( 747 GpuControlList::kOsMacosx, "10.9", gpu_info)); 748 749 gpu_info.driver_vendor = "NVIDIA"; 750 EXPECT_TRUE(entry->Contains( 751 GpuControlList::kOsMacosx, "10.9", gpu_info)); 752 753 gpu_info.driver_vendor = "USA NVIDIA"; 754 EXPECT_FALSE(entry->Contains( 755 GpuControlList::kOsMacosx, "10.9", gpu_info)); 756 } 757 758 TEST_F(GpuControlListEntryTest, LexicalDriverVersionEntry) { 759 const std::string json = LONG_STRING_CONST( 760 { 761 "id": 1, 762 "os": { 763 "type": "linux" 764 }, 765 "vendor_id": "0x1002", 766 "driver_version": { 767 "op": "=", 768 "style": "lexical", 769 "value": "8.76" 770 }, 771 "features": [ 772 "test_feature_0" 773 ] 774 } 775 ); 776 GPUInfo gpu_info; 777 gpu_info.gpu.vendor_id = 0x1002; 778 779 ScopedEntry entry(GetEntryFromString(json)); 780 EXPECT_TRUE(entry.get() != NULL); 781 EXPECT_EQ(GpuControlList::kOsLinux, entry->GetOsType()); 782 783 gpu_info.driver_version = "8.76"; 784 EXPECT_TRUE(entry->Contains( 785 GpuControlList::kOsLinux, "10.6", gpu_info)); 786 787 gpu_info.driver_version = "8.768"; 788 EXPECT_TRUE(entry->Contains( 789 GpuControlList::kOsLinux, "10.6", gpu_info)); 790 791 gpu_info.driver_version = "8.76.8"; 792 EXPECT_TRUE(entry->Contains( 793 GpuControlList::kOsLinux, "10.6", gpu_info)); 794 } 795 796 TEST_F(GpuControlListEntryTest, NeedsMoreInfoEntry) { 797 const std::string json = LONG_STRING_CONST( 798 { 799 "id": 1, 800 "vendor_id": "0x8086", 801 "driver_version": { 802 "op": "<", 803 "value": "10.7" 804 }, 805 "features": [ 806 "test_feature_1" 807 ] 808 } 809 ); 810 ScopedEntry entry(GetEntryFromString(json)); 811 EXPECT_TRUE(entry.get() != NULL); 812 813 GPUInfo gpu_info; 814 gpu_info.gpu.vendor_id = 0x8086; 815 EXPECT_TRUE(entry->NeedsMoreInfo(gpu_info)); 816 817 gpu_info.driver_version = "10.6"; 818 EXPECT_FALSE(entry->NeedsMoreInfo(gpu_info)); 819 } 820 821 TEST_F(GpuControlListEntryTest, NeedsMoreInfoForExceptionsEntry) { 822 const std::string json = LONG_STRING_CONST( 823 { 824 "id": 1, 825 "vendor_id": "0x8086", 826 "exceptions": [ 827 { 828 "gl_renderer": ".*mesa.*" 829 } 830 ], 831 "features": [ 832 "test_feature_1" 833 ] 834 } 835 ); 836 ScopedEntry entry(GetEntryFromString(json)); 837 EXPECT_TRUE(entry.get() != NULL); 838 839 GPUInfo gpu_info; 840 gpu_info.gpu.vendor_id = 0x8086; 841 EXPECT_TRUE(entry->NeedsMoreInfo(gpu_info)); 842 843 gpu_info.gl_renderer = "mesa"; 844 EXPECT_FALSE(entry->NeedsMoreInfo(gpu_info)); 845 } 846 847 TEST_F(GpuControlListEntryTest, FeatureTypeAllEntry) { 848 const std::string json = LONG_STRING_CONST( 849 { 850 "id": 1, 851 "features": [ 852 "all" 853 ] 854 } 855 ); 856 ScopedEntry entry(GetEntryFromString(json, true)); 857 EXPECT_TRUE(entry.get() != NULL); 858 EXPECT_EQ(3u, entry->features().size()); 859 EXPECT_EQ(1u, entry->features().count(TEST_FEATURE_0)); 860 EXPECT_EQ(1u, entry->features().count(TEST_FEATURE_1)); 861 EXPECT_EQ(1u, entry->features().count(TEST_FEATURE_2)); 862 } 863 864 TEST_F(GpuControlListEntryTest, InvalidVendorIdEntry) { 865 const std::string json = LONG_STRING_CONST( 866 { 867 "id": 1, 868 "vendor_id": "0x0000", 869 "features": [ 870 "test_feature_1" 871 ] 872 } 873 ); 874 ScopedEntry entry(GetEntryFromString(json)); 875 EXPECT_TRUE(entry.get() == NULL); 876 } 877 878 TEST_F(GpuControlListEntryTest, InvalidDeviceIdEntry) { 879 const std::string json = LONG_STRING_CONST( 880 { 881 "id": 1, 882 "vendor_id": "0x10de", 883 "device_id": ["0x1023", "0x0000"], 884 "features": [ 885 "test_feature_1" 886 ] 887 } 888 ); 889 ScopedEntry entry(GetEntryFromString(json)); 890 EXPECT_TRUE(entry.get() == NULL); 891 } 892 893 TEST_F(GpuControlListEntryTest, SingleActiveGPU) { 894 const std::string json = LONG_STRING_CONST( 895 { 896 "id": 1, 897 "os": { 898 "type": "macosx" 899 }, 900 "vendor_id": "0x10de", 901 "device_id": ["0x0640"], 902 "multi_gpu_category": "active", 903 "features": [ 904 "test_feature_0" 905 ] 906 } 907 ); 908 ScopedEntry entry(GetEntryFromString(json)); 909 EXPECT_TRUE(entry.get() != NULL); 910 EXPECT_EQ(GpuControlList::kOsMacosx, entry->GetOsType()); 911 EXPECT_TRUE(entry->Contains( 912 GpuControlList::kOsMacosx, "10.6", gpu_info())); 913 } 914 915 TEST_F(GpuControlListEntryTest, MachineModelName) { 916 const std::string json = LONG_STRING_CONST( 917 { 918 "id": 1, 919 "os": { 920 "type": "android" 921 }, 922 "machine_model_name": [ 923 "Nexus 4", "XT1032", "GT-.*", "SCH-.*" 924 ], 925 "features": [ 926 "test_feature_0" 927 ] 928 } 929 ); 930 ScopedEntry entry(GetEntryFromString(json)); 931 EXPECT_TRUE(entry.get() != NULL); 932 EXPECT_EQ(GpuControlList::kOsAndroid, entry->GetOsType()); 933 GPUInfo gpu_info; 934 935 gpu_info.machine_model_name = "Nexus 4"; 936 EXPECT_TRUE(entry->Contains( 937 GpuControlList::kOsAndroid, "4.1", gpu_info)); 938 939 gpu_info.machine_model_name = "XT1032"; 940 EXPECT_TRUE(entry->Contains( 941 GpuControlList::kOsAndroid, "4.1", gpu_info)); 942 943 gpu_info.machine_model_name = "XT1032i"; 944 EXPECT_FALSE(entry->Contains( 945 GpuControlList::kOsAndroid, "4.1", gpu_info)); 946 947 gpu_info.machine_model_name = "Nexus 5"; 948 EXPECT_FALSE(entry->Contains( 949 GpuControlList::kOsAndroid, "4.1", gpu_info)); 950 951 gpu_info.machine_model_name = "Nexus"; 952 EXPECT_FALSE(entry->Contains( 953 GpuControlList::kOsAndroid, "4.1", gpu_info)); 954 955 gpu_info.machine_model_name = ""; 956 EXPECT_FALSE(entry->Contains( 957 GpuControlList::kOsAndroid, "4.1", gpu_info)); 958 959 gpu_info.machine_model_name = "GT-N7100"; 960 EXPECT_TRUE(entry->Contains( 961 GpuControlList::kOsAndroid, "4.1", gpu_info)); 962 963 gpu_info.machine_model_name = "GT-I9300"; 964 EXPECT_TRUE(entry->Contains( 965 GpuControlList::kOsAndroid, "4.1", gpu_info)); 966 967 gpu_info.machine_model_name = "SCH-I545"; 968 EXPECT_TRUE(entry->Contains( 969 GpuControlList::kOsAndroid, "4.1", gpu_info)); 970 } 971 972 TEST_F(GpuControlListEntryTest, MachineModelNameException) { 973 const std::string json = LONG_STRING_CONST( 974 { 975 "id": 1, 976 "exceptions": [ 977 { 978 "os": { 979 "type": "android" 980 }, 981 "machine_model_name": ["Nexus.*"] 982 } 983 ], 984 "features": [ 985 "test_feature_0" 986 ] 987 } 988 ); 989 ScopedEntry entry(GetEntryFromString(json)); 990 EXPECT_TRUE(entry.get() != NULL); 991 EXPECT_EQ(GpuControlList::kOsAny, entry->GetOsType()); 992 GPUInfo gpu_info; 993 994 gpu_info.machine_model_name = "Nexus 4"; 995 EXPECT_FALSE(entry->Contains( 996 GpuControlList::kOsAndroid, "4.1", gpu_info)); 997 EXPECT_TRUE(entry->Contains( 998 GpuControlList::kOsLinux, "4.1", gpu_info)); 999 1000 gpu_info.machine_model_name = "Nexus 7"; 1001 EXPECT_FALSE(entry->Contains( 1002 GpuControlList::kOsAndroid, "4.1", gpu_info)); 1003 EXPECT_TRUE(entry->Contains( 1004 GpuControlList::kOsLinux, "4.1", gpu_info)); 1005 1006 gpu_info.machine_model_name = ""; 1007 EXPECT_TRUE(entry->Contains( 1008 GpuControlList::kOsAndroid, "4.1", gpu_info)); 1009 EXPECT_TRUE(entry->Contains( 1010 GpuControlList::kOsLinux, "4.1", gpu_info)); 1011 } 1012 1013 TEST_F(GpuControlListEntryTest, MachineModelVersion) { 1014 const std::string json = LONG_STRING_CONST( 1015 { 1016 "id": 1, 1017 "os": { 1018 "type": "macosx" 1019 }, 1020 "machine_model_name": ["MacBookPro"], 1021 "machine_model_version": { 1022 "op": "=", 1023 "value": "7.1" 1024 }, 1025 "features": [ 1026 "test_feature_0" 1027 ] 1028 } 1029 ); 1030 ScopedEntry entry(GetEntryFromString(json)); 1031 EXPECT_TRUE(entry.get() != NULL); 1032 GPUInfo gpu_info; 1033 gpu_info.machine_model_name = "MacBookPro"; 1034 gpu_info.machine_model_version = "7.1"; 1035 EXPECT_EQ(GpuControlList::kOsMacosx, entry->GetOsType()); 1036 EXPECT_TRUE(entry->Contains( 1037 GpuControlList::kOsMacosx, "10.6", gpu_info)); 1038 } 1039 1040 TEST_F(GpuControlListEntryTest, MachineModelVersionException) { 1041 const std::string json = LONG_STRING_CONST( 1042 { 1043 "id": 1, 1044 "os": { 1045 "type": "macosx" 1046 }, 1047 "machine_model_name": ["MacBookPro"], 1048 "exceptions": [ 1049 { 1050 "machine_model_version": { 1051 "op": ">", 1052 "value": "7.1" 1053 } 1054 } 1055 ], 1056 "features": [ 1057 "test_feature_0" 1058 ] 1059 } 1060 ); 1061 ScopedEntry entry(GetEntryFromString(json)); 1062 EXPECT_TRUE(entry.get() != NULL); 1063 EXPECT_EQ(GpuControlList::kOsMacosx, entry->GetOsType()); 1064 1065 GPUInfo gpu_info; 1066 gpu_info.machine_model_name = "MacBookPro"; 1067 gpu_info.machine_model_version = "7.0"; 1068 EXPECT_TRUE(entry->Contains( 1069 GpuControlList::kOsMacosx, "10.6", gpu_info)); 1070 1071 gpu_info.machine_model_version = "7.2"; 1072 EXPECT_FALSE(entry->Contains( 1073 GpuControlList::kOsMacosx, "10.6", gpu_info)); 1074 1075 gpu_info.machine_model_version = ""; 1076 EXPECT_TRUE(entry->Contains( 1077 GpuControlList::kOsMacosx, "10.6", gpu_info)); 1078 } 1079 1080 class GpuControlListEntryDualGPUTest : public GpuControlListEntryTest { 1081 public: 1082 GpuControlListEntryDualGPUTest() { } 1083 virtual ~GpuControlListEntryDualGPUTest() { } 1084 1085 virtual void SetUp() OVERRIDE { 1086 // Set up a NVIDIA/Intel dual, with NVIDIA as primary and Intel as 1087 // secondary, and initially Intel is active. 1088 gpu_info_.gpu.vendor_id = 0x10de; 1089 gpu_info_.gpu.device_id = 0x0640; 1090 gpu_info_.gpu.active = false; 1091 GPUInfo::GPUDevice second_gpu; 1092 second_gpu.vendor_id = 0x8086; 1093 second_gpu.device_id = 0x0166; 1094 second_gpu.active = true; 1095 gpu_info_.secondary_gpus.push_back(second_gpu); 1096 } 1097 1098 void ActivatePrimaryGPU() { 1099 gpu_info_.gpu.active = true; 1100 gpu_info_.secondary_gpus[0].active = false; 1101 } 1102 1103 void EntryShouldApply(const std::string& entry_json) const { 1104 EXPECT_TRUE(EntryApplies(entry_json)); 1105 } 1106 1107 void EntryShouldNotApply(const std::string& entry_json) const { 1108 EXPECT_FALSE(EntryApplies(entry_json)); 1109 } 1110 1111 private: 1112 bool EntryApplies(const std::string& entry_json) const { 1113 ScopedEntry entry(GetEntryFromString(entry_json)); 1114 EXPECT_TRUE(entry.get()); 1115 EXPECT_EQ(GpuControlList::kOsMacosx, entry->GetOsType()); 1116 return entry->Contains(GpuControlList::kOsMacosx, "10.6", gpu_info()); 1117 } 1118 }; 1119 1120 TEST_F(GpuControlListEntryDualGPUTest, CategoryAny) { 1121 const std::string json_intel = LONG_STRING_CONST( 1122 { 1123 "id": 1, 1124 "os": { 1125 "type": "macosx" 1126 }, 1127 "vendor_id": "0x8086", 1128 "device_id": ["0x0166"], 1129 "multi_gpu_category": "any", 1130 "features": [ 1131 "test_feature_0" 1132 ] 1133 } 1134 ); 1135 EntryShouldApply(json_intel); 1136 1137 const std::string json_nvidia = LONG_STRING_CONST( 1138 { 1139 "id": 1, 1140 "os": { 1141 "type": "macosx" 1142 }, 1143 "vendor_id": "0x10de", 1144 "device_id": ["0x0640"], 1145 "multi_gpu_category": "any", 1146 "features": [ 1147 "test_feature_0" 1148 ] 1149 } 1150 ); 1151 EntryShouldApply(json_nvidia); 1152 } 1153 1154 TEST_F(GpuControlListEntryDualGPUTest, CategoryPrimarySecondary) { 1155 const std::string json_secondary = LONG_STRING_CONST( 1156 { 1157 "id": 1, 1158 "os": { 1159 "type": "macosx" 1160 }, 1161 "vendor_id": "0x8086", 1162 "device_id": ["0x0166"], 1163 "multi_gpu_category": "secondary", 1164 "features": [ 1165 "test_feature_0" 1166 ] 1167 } 1168 ); 1169 EntryShouldApply(json_secondary); 1170 1171 const std::string json_primary = LONG_STRING_CONST( 1172 { 1173 "id": 1, 1174 "os": { 1175 "type": "macosx" 1176 }, 1177 "vendor_id": "0x8086", 1178 "device_id": ["0x0166"], 1179 "multi_gpu_category": "primary", 1180 "features": [ 1181 "test_feature_0" 1182 ] 1183 } 1184 ); 1185 EntryShouldNotApply(json_primary); 1186 1187 const std::string json_default = LONG_STRING_CONST( 1188 { 1189 "id": 1, 1190 "os": { 1191 "type": "macosx" 1192 }, 1193 "vendor_id": "0x8086", 1194 "device_id": ["0x0166"], 1195 "features": [ 1196 "test_feature_0" 1197 ] 1198 } 1199 ); 1200 // Default is primary. 1201 EntryShouldNotApply(json_default); 1202 } 1203 1204 TEST_F(GpuControlListEntryDualGPUTest, ActiveSecondaryGPU) { 1205 const std::string json = LONG_STRING_CONST( 1206 { 1207 "id": 1, 1208 "os": { 1209 "type": "macosx" 1210 }, 1211 "vendor_id": "0x8086", 1212 "device_id": ["0x0166", "0x0168"], 1213 "multi_gpu_category": "active", 1214 "features": [ 1215 "test_feature_0" 1216 ] 1217 } 1218 ); 1219 // By default, secondary GPU is active. 1220 EntryShouldApply(json); 1221 1222 ActivatePrimaryGPU(); 1223 EntryShouldNotApply(json); 1224 } 1225 1226 TEST_F(GpuControlListEntryDualGPUTest, VendorOnlyActiveSecondaryGPU) { 1227 const std::string json = LONG_STRING_CONST( 1228 { 1229 "id": 1, 1230 "os": { 1231 "type": "macosx" 1232 }, 1233 "vendor_id": "0x8086", 1234 "multi_gpu_category": "active", 1235 "features": [ 1236 "test_feature_0" 1237 ] 1238 } 1239 ); 1240 // By default, secondary GPU is active. 1241 EntryShouldApply(json); 1242 1243 ActivatePrimaryGPU(); 1244 EntryShouldNotApply(json); 1245 } 1246 1247 TEST_F(GpuControlListEntryDualGPUTest, ActivePrimaryGPU) { 1248 const std::string json = LONG_STRING_CONST( 1249 { 1250 "id": 1, 1251 "os": { 1252 "type": "macosx" 1253 }, 1254 "vendor_id": "0x10de", 1255 "device_id": ["0x0640"], 1256 "multi_gpu_category": "active", 1257 "features": [ 1258 "test_feature_0" 1259 ] 1260 } 1261 ); 1262 // By default, secondary GPU is active. 1263 EntryShouldNotApply(json); 1264 1265 ActivatePrimaryGPU(); 1266 EntryShouldApply(json); 1267 } 1268 1269 TEST_F(GpuControlListEntryDualGPUTest, VendorOnlyActivePrimaryGPU) { 1270 const std::string json = LONG_STRING_CONST( 1271 { 1272 "id": 1, 1273 "os": { 1274 "type": "macosx" 1275 }, 1276 "vendor_id": "0x10de", 1277 "multi_gpu_category": "active", 1278 "features": [ 1279 "test_feature_0" 1280 ] 1281 } 1282 ); 1283 // By default, secondary GPU is active. 1284 EntryShouldNotApply(json); 1285 1286 ActivatePrimaryGPU(); 1287 EntryShouldApply(json); 1288 } 1289 1290 } // namespace gpu 1291 1292