Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / gpu / config / gpu_control_list_entry_unittest.cc
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_.machine_model = "MacBookPro 7.1";
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, GlVendorEntry) {
381   const std::string json = LONG_STRING_CONST(
382       {
383         "id": 1,
384         "gl_vendor": {
385           "op": "beginwith",
386           "value": "NVIDIA"
387         },
388         "features": [
389           "test_feature_0"
390         ]
391       }
392   );
393   ScopedEntry entry(GetEntryFromString(json));
394   EXPECT_TRUE(entry.get() != NULL);
395
396   const GpuControlList::OsType os_type[] = {
397     GpuControlList::kOsMacosx,
398     GpuControlList::kOsWin,
399     GpuControlList::kOsLinux,
400     GpuControlList::kOsChromeOS,
401     GpuControlList::kOsAndroid
402   };
403   for (size_t i = 0; i < arraysize(os_type); ++i)
404     EXPECT_TRUE(entry->Contains(os_type[i], "10.6", gpu_info()));
405 }
406
407 TEST_F(GpuControlListEntryTest, GlRendererEntry) {
408   const std::string json = LONG_STRING_CONST(
409       {
410         "id": 1,
411         "gl_renderer": {
412           "op": "contains",
413           "value": "GeForce"
414         },
415         "features": [
416           "test_feature_0"
417         ]
418       }
419   );
420   ScopedEntry entry(GetEntryFromString(json));
421   EXPECT_TRUE(entry.get() != NULL);
422
423   const GpuControlList::OsType os_type[] = {
424     GpuControlList::kOsMacosx,
425     GpuControlList::kOsWin,
426     GpuControlList::kOsLinux,
427     GpuControlList::kOsChromeOS,
428     GpuControlList::kOsAndroid
429   };
430   for (size_t i = 0; i < arraysize(os_type); ++i)
431     EXPECT_TRUE(entry->Contains(os_type[i], "10.6", gpu_info()));
432 }
433
434 TEST_F(GpuControlListEntryTest, PerfGraphicsEntry) {
435   const std::string json = LONG_STRING_CONST(
436       {
437         "id": 1,
438         "perf_graphics": {
439           "op": "<",
440           "value": "6.0"
441         },
442         "features": [
443           "test_feature_0"
444         ]
445       }
446   );
447   ScopedEntry entry(GetEntryFromString(json));
448   EXPECT_TRUE(entry.get() != NULL);
449   EXPECT_TRUE(entry->Contains(GpuControlList::kOsWin, "10.6", gpu_info()));
450 }
451
452 TEST_F(GpuControlListEntryTest, PerfGamingEntry) {
453   const std::string json = LONG_STRING_CONST(
454       {
455         "id": 1,
456         "perf_graphics": {
457           "op": "<=",
458           "value": "4.0"
459         },
460         "features": [
461           "test_feature_0"
462         ]
463       }
464   );
465   ScopedEntry entry(GetEntryFromString(json));
466   EXPECT_TRUE(entry.get() != NULL);
467   EXPECT_FALSE(entry->Contains(GpuControlList::kOsWin, "10.6", gpu_info()));
468 }
469
470 TEST_F(GpuControlListEntryTest, PerfOverallEntry) {
471   const std::string json = LONG_STRING_CONST(
472       {
473         "id": 1,
474         "perf_overall": {
475           "op": "between",
476           "value": "1.0",
477           "value2": "9.0"
478         },
479         "features": [
480           "test_feature_0"
481         ]
482       }
483   );
484   ScopedEntry entry(GetEntryFromString(json));
485   EXPECT_TRUE(entry.get() != NULL);
486   EXPECT_TRUE(entry->Contains(GpuControlList::kOsWin, "10.6", gpu_info()));
487 }
488
489 TEST_F(GpuControlListEntryTest, DisabledEntry) {
490   const std::string json = LONG_STRING_CONST(
491       {
492         "id": 1,
493         "disabled": true,
494         "features": [
495           "test_feature_0"
496         ]
497       }
498   );
499   ScopedEntry entry(GetEntryFromString(json));
500   EXPECT_TRUE(entry.get() != NULL);
501   EXPECT_TRUE(entry->disabled());
502 }
503
504 TEST_F(GpuControlListEntryTest, OptimusEntry) {
505   const std::string json = LONG_STRING_CONST(
506       {
507         "id": 1,
508         "os": {
509           "type": "linux"
510         },
511         "multi_gpu_style": "optimus",
512         "features": [
513           "test_feature_0"
514         ]
515       }
516   );
517   GPUInfo gpu_info;
518   gpu_info.optimus = true;
519
520   ScopedEntry entry(GetEntryFromString(json));
521   EXPECT_TRUE(entry.get() != NULL);
522   EXPECT_EQ(GpuControlList::kOsLinux, entry->GetOsType());
523   EXPECT_TRUE(entry->Contains(
524       GpuControlList::kOsLinux, "10.6", gpu_info));
525 }
526
527 TEST_F(GpuControlListEntryTest, AMDSwitchableEntry) {
528   const std::string json = LONG_STRING_CONST(
529       {
530         "id": 1,
531         "os": {
532           "type": "macosx"
533         },
534         "multi_gpu_style": "amd_switchable",
535         "features": [
536           "test_feature_0"
537         ]
538       }
539   );
540   GPUInfo gpu_info;
541   gpu_info.amd_switchable = true;
542
543   ScopedEntry entry(GetEntryFromString(json));
544   EXPECT_TRUE(entry.get() != NULL);
545   EXPECT_EQ(GpuControlList::kOsMacosx, entry->GetOsType());
546   EXPECT_TRUE(entry->Contains(
547       GpuControlList::kOsMacosx, "10.6", gpu_info));
548 }
549
550 TEST_F(GpuControlListEntryTest, LexicalDriverVersionEntry) {
551   const std::string json = LONG_STRING_CONST(
552       {
553         "id": 1,
554         "os": {
555           "type": "linux"
556         },
557         "vendor_id": "0x1002",
558         "driver_version": {
559           "op": "=",
560           "style": "lexical",
561           "value": "8.76"
562         },
563         "features": [
564           "test_feature_0"
565         ]
566       }
567   );
568   GPUInfo gpu_info;
569   gpu_info.gpu.vendor_id = 0x1002;
570
571   ScopedEntry entry(GetEntryFromString(json));
572   EXPECT_TRUE(entry.get() != NULL);
573   EXPECT_EQ(GpuControlList::kOsLinux, entry->GetOsType());
574
575   gpu_info.driver_version = "8.76";
576   EXPECT_TRUE(entry->Contains(
577       GpuControlList::kOsLinux, "10.6", gpu_info));
578
579   gpu_info.driver_version = "8.768";
580   EXPECT_TRUE(entry->Contains(
581       GpuControlList::kOsLinux, "10.6", gpu_info));
582
583   gpu_info.driver_version = "8.76.8";
584   EXPECT_TRUE(entry->Contains(
585       GpuControlList::kOsLinux, "10.6", gpu_info));
586 }
587
588 TEST_F(GpuControlListEntryTest, NeedsMoreInfoEntry) {
589   const std::string json = LONG_STRING_CONST(
590       {
591         "id": 1,
592         "vendor_id": "0x8086",
593         "driver_version": {
594           "op": "<",
595           "value": "10.7"
596         },
597         "features": [
598           "test_feature_1"
599         ]
600       }
601   );
602   ScopedEntry entry(GetEntryFromString(json));
603   EXPECT_TRUE(entry.get() != NULL);
604
605   GPUInfo gpu_info;
606   gpu_info.gpu.vendor_id = 0x8086;
607   EXPECT_TRUE(entry->NeedsMoreInfo(gpu_info));
608
609   gpu_info.driver_version = "10.6";
610   EXPECT_FALSE(entry->NeedsMoreInfo(gpu_info));
611 }
612
613 TEST_F(GpuControlListEntryTest, NeedsMoreInfoForExceptionsEntry) {
614   const std::string json = LONG_STRING_CONST(
615       {
616         "id": 1,
617         "vendor_id": "0x8086",
618         "exceptions": [
619           {
620             "gl_renderer": {
621               "op": "contains",
622               "value": "mesa"
623             }
624           }
625         ],
626         "features": [
627           "test_feature_1"
628         ]
629       }
630   );
631   ScopedEntry entry(GetEntryFromString(json));
632   EXPECT_TRUE(entry.get() != NULL);
633
634   GPUInfo gpu_info;
635   gpu_info.gpu.vendor_id = 0x8086;
636   EXPECT_TRUE(entry->NeedsMoreInfo(gpu_info));
637
638   gpu_info.gl_renderer = "mesa";
639   EXPECT_FALSE(entry->NeedsMoreInfo(gpu_info));
640 }
641
642 TEST_F(GpuControlListEntryTest, FeatureTypeAllEntry) {
643   const std::string json = LONG_STRING_CONST(
644       {
645         "id": 1,
646         "features": [
647           "all"
648         ]
649       }
650   );
651   ScopedEntry entry(GetEntryFromString(json, true));
652   EXPECT_TRUE(entry.get() != NULL);
653   EXPECT_EQ(3u, entry->features().size());
654   EXPECT_EQ(1u, entry->features().count(TEST_FEATURE_0));
655   EXPECT_EQ(1u, entry->features().count(TEST_FEATURE_1));
656   EXPECT_EQ(1u, entry->features().count(TEST_FEATURE_2));
657 }
658
659 TEST_F(GpuControlListEntryTest, InvalidVendorIdEntry) {
660   const std::string json = LONG_STRING_CONST(
661       {
662         "id": 1,
663         "vendor_id": "0x0000",
664         "features": [
665           "test_feature_1"
666         ]
667       }
668   );
669   ScopedEntry entry(GetEntryFromString(json));
670   EXPECT_TRUE(entry.get() == NULL);
671 }
672
673 TEST_F(GpuControlListEntryTest, InvalidDeviceIdEntry) {
674   const std::string json = LONG_STRING_CONST(
675       {
676         "id": 1,
677         "vendor_id": "0x10de",
678         "device_id": ["0x1023", "0x0000"],
679         "features": [
680           "test_feature_1"
681         ]
682       }
683   );
684   ScopedEntry entry(GetEntryFromString(json));
685   EXPECT_TRUE(entry.get() == NULL);
686 }
687
688 TEST_F(GpuControlListEntryTest, SingleActiveGPU) {
689   const std::string json = LONG_STRING_CONST(
690       {
691         "id": 1,
692         "os": {
693           "type": "macosx"
694         },
695         "vendor_id": "0x10de",
696         "device_id": ["0x0640"],
697         "multi_gpu_category": "active",
698         "features": [
699           "test_feature_0"
700         ]
701       }
702   );
703   ScopedEntry entry(GetEntryFromString(json));
704   EXPECT_TRUE(entry.get() != NULL);
705   EXPECT_EQ(GpuControlList::kOsMacosx, entry->GetOsType());
706   EXPECT_TRUE(entry->Contains(
707       GpuControlList::kOsMacosx, "10.6", gpu_info()));
708 }
709
710 class GpuControlListEntryDualGPUTest : public GpuControlListEntryTest {
711  public:
712   GpuControlListEntryDualGPUTest() { }
713   virtual ~GpuControlListEntryDualGPUTest() { }
714
715   virtual void SetUp() OVERRIDE {
716     // Set up a NVIDIA/Intel dual, with NVIDIA as primary and Intel as
717     // secondary, and initially Intel is active.
718     gpu_info_.gpu.vendor_id = 0x10de;
719     gpu_info_.gpu.device_id = 0x0640;
720     gpu_info_.gpu.active = false;
721     GPUInfo::GPUDevice second_gpu;
722     second_gpu.vendor_id = 0x8086;
723     second_gpu.device_id = 0x0166;
724     second_gpu.active = true;
725     gpu_info_.secondary_gpus.push_back(second_gpu);
726   }
727
728   void ActivatePrimaryGPU() {
729     gpu_info_.gpu.active = true;
730     gpu_info_.secondary_gpus[0].active = false;
731   }
732
733   void EntryShouldApply(const std::string& entry_json) const {
734     EXPECT_TRUE(EntryApplies(entry_json));
735   }
736
737   void EntryShouldNotApply(const std::string& entry_json) const {
738     EXPECT_FALSE(EntryApplies(entry_json));
739   }
740
741  private:
742   bool EntryApplies(const std::string& entry_json) const {
743     ScopedEntry entry(GetEntryFromString(entry_json));
744     EXPECT_TRUE(entry.get());
745     EXPECT_EQ(GpuControlList::kOsMacosx, entry->GetOsType());
746     return entry->Contains(GpuControlList::kOsMacosx, "10.6", gpu_info());
747   }
748 };
749
750 TEST_F(GpuControlListEntryDualGPUTest, CategoryAny) {
751   const std::string json_intel = LONG_STRING_CONST(
752       {
753         "id": 1,
754         "os": {
755           "type": "macosx"
756         },
757         "vendor_id": "0x8086",
758         "device_id": ["0x0166"],
759         "multi_gpu_category": "any",
760         "features": [
761           "test_feature_0"
762         ]
763       }
764   );
765   EntryShouldApply(json_intel);
766
767   const std::string json_nvidia = LONG_STRING_CONST(
768       {
769         "id": 1,
770         "os": {
771           "type": "macosx"
772         },
773         "vendor_id": "0x10de",
774         "device_id": ["0x0640"],
775         "multi_gpu_category": "any",
776         "features": [
777           "test_feature_0"
778         ]
779       }
780   );
781   EntryShouldApply(json_nvidia);
782 }
783
784 TEST_F(GpuControlListEntryDualGPUTest, CategoryPrimarySecondary) {
785   const std::string json_secondary = LONG_STRING_CONST(
786       {
787         "id": 1,
788         "os": {
789           "type": "macosx"
790         },
791         "vendor_id": "0x8086",
792         "device_id": ["0x0166"],
793         "multi_gpu_category": "secondary",
794         "features": [
795           "test_feature_0"
796         ]
797       }
798   );
799   EntryShouldApply(json_secondary);
800
801   const std::string json_primary = LONG_STRING_CONST(
802       {
803         "id": 1,
804         "os": {
805           "type": "macosx"
806         },
807         "vendor_id": "0x8086",
808         "device_id": ["0x0166"],
809         "multi_gpu_category": "primary",
810         "features": [
811           "test_feature_0"
812         ]
813       }
814   );
815   EntryShouldNotApply(json_primary);
816
817   const std::string json_default = LONG_STRING_CONST(
818       {
819         "id": 1,
820         "os": {
821           "type": "macosx"
822         },
823         "vendor_id": "0x8086",
824         "device_id": ["0x0166"],
825         "features": [
826           "test_feature_0"
827         ]
828       }
829   );
830   // Default is primary.
831   EntryShouldNotApply(json_default);
832 }
833
834 TEST_F(GpuControlListEntryDualGPUTest, ActiveSecondaryGPU) {
835   const std::string json = LONG_STRING_CONST(
836       {
837         "id": 1,
838         "os": {
839           "type": "macosx"
840         },
841         "vendor_id": "0x8086",
842         "device_id": ["0x0166", "0x0168"],
843         "multi_gpu_category": "active",
844         "features": [
845           "test_feature_0"
846         ]
847       }
848   );
849   // By default, secondary GPU is active.
850   EntryShouldApply(json);
851
852   ActivatePrimaryGPU();
853   EntryShouldNotApply(json);
854 }
855
856 TEST_F(GpuControlListEntryDualGPUTest, VendorOnlyActiveSecondaryGPU) {
857   const std::string json = LONG_STRING_CONST(
858       {
859         "id": 1,
860         "os": {
861           "type": "macosx"
862         },
863         "vendor_id": "0x8086",
864         "multi_gpu_category": "active",
865         "features": [
866           "test_feature_0"
867         ]
868       }
869   );
870   // By default, secondary GPU is active.
871   EntryShouldApply(json);
872
873   ActivatePrimaryGPU();
874   EntryShouldNotApply(json);
875 }
876
877 TEST_F(GpuControlListEntryDualGPUTest, ActivePrimaryGPU) {
878   const std::string json = LONG_STRING_CONST(
879       {
880         "id": 1,
881         "os": {
882           "type": "macosx"
883         },
884         "vendor_id": "0x10de",
885         "device_id": ["0x0640"],
886         "multi_gpu_category": "active",
887         "features": [
888           "test_feature_0"
889         ]
890       }
891   );
892   // By default, secondary GPU is active.
893   EntryShouldNotApply(json);
894
895   ActivatePrimaryGPU();
896   EntryShouldApply(json);
897 }
898
899 TEST_F(GpuControlListEntryDualGPUTest, VendorOnlyActivePrimaryGPU) {
900   const std::string json = LONG_STRING_CONST(
901       {
902         "id": 1,
903         "os": {
904           "type": "macosx"
905         },
906         "vendor_id": "0x10de",
907         "multi_gpu_category": "active",
908         "features": [
909           "test_feature_0"
910         ]
911       }
912   );
913   // By default, secondary GPU is active.
914   EntryShouldNotApply(json);
915
916   ActivatePrimaryGPU();
917   EntryShouldApply(json);
918 }
919
920 }  // namespace gpu
921