Upstream version 7.36.149.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_.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@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@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@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@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, GlVendorEntry) {
474   const std::string json = LONG_STRING_CONST(
475       {
476         "id": 1,
477         "gl_vendor": {
478           "op": "beginwith",
479           "value": "NVIDIA"
480         },
481         "features": [
482           "test_feature_0"
483         ]
484       }
485   );
486   ScopedEntry entry(GetEntryFromString(json));
487   EXPECT_TRUE(entry.get() != NULL);
488
489   const GpuControlList::OsType os_type[] = {
490     GpuControlList::kOsMacosx,
491     GpuControlList::kOsWin,
492     GpuControlList::kOsLinux,
493     GpuControlList::kOsChromeOS,
494     GpuControlList::kOsAndroid
495   };
496   for (size_t i = 0; i < arraysize(os_type); ++i)
497     EXPECT_TRUE(entry->Contains(os_type[i], "10.6", gpu_info()));
498 }
499
500 TEST_F(GpuControlListEntryTest, GlRendererEntry) {
501   const std::string json = LONG_STRING_CONST(
502       {
503         "id": 1,
504         "gl_renderer": {
505           "op": "contains",
506           "value": "GeForce"
507         },
508         "features": [
509           "test_feature_0"
510         ]
511       }
512   );
513   ScopedEntry entry(GetEntryFromString(json));
514   EXPECT_TRUE(entry.get() != NULL);
515
516   const GpuControlList::OsType os_type[] = {
517     GpuControlList::kOsMacosx,
518     GpuControlList::kOsWin,
519     GpuControlList::kOsLinux,
520     GpuControlList::kOsChromeOS,
521     GpuControlList::kOsAndroid
522   };
523   for (size_t i = 0; i < arraysize(os_type); ++i)
524     EXPECT_TRUE(entry->Contains(os_type[i], "10.6", gpu_info()));
525 }
526
527 TEST_F(GpuControlListEntryTest, PerfGraphicsEntry) {
528   const std::string json = LONG_STRING_CONST(
529       {
530         "id": 1,
531         "perf_graphics": {
532           "op": "<",
533           "value": "6.0"
534         },
535         "features": [
536           "test_feature_0"
537         ]
538       }
539   );
540   ScopedEntry entry(GetEntryFromString(json));
541   EXPECT_TRUE(entry.get() != NULL);
542   EXPECT_TRUE(entry->Contains(GpuControlList::kOsWin, "10.6", gpu_info()));
543 }
544
545 TEST_F(GpuControlListEntryTest, PerfGamingEntry) {
546   const std::string json = LONG_STRING_CONST(
547       {
548         "id": 1,
549         "perf_graphics": {
550           "op": "<=",
551           "value": "4.0"
552         },
553         "features": [
554           "test_feature_0"
555         ]
556       }
557   );
558   ScopedEntry entry(GetEntryFromString(json));
559   EXPECT_TRUE(entry.get() != NULL);
560   EXPECT_FALSE(entry->Contains(GpuControlList::kOsWin, "10.6", gpu_info()));
561 }
562
563 TEST_F(GpuControlListEntryTest, PerfOverallEntry) {
564   const std::string json = LONG_STRING_CONST(
565       {
566         "id": 1,
567         "perf_overall": {
568           "op": "between",
569           "value": "1.0",
570           "value2": "9.0"
571         },
572         "features": [
573           "test_feature_0"
574         ]
575       }
576   );
577   ScopedEntry entry(GetEntryFromString(json));
578   EXPECT_TRUE(entry.get() != NULL);
579   EXPECT_TRUE(entry->Contains(GpuControlList::kOsWin, "10.6", gpu_info()));
580 }
581
582 TEST_F(GpuControlListEntryTest, DisabledEntry) {
583   const std::string json = LONG_STRING_CONST(
584       {
585         "id": 1,
586         "disabled": true,
587         "features": [
588           "test_feature_0"
589         ]
590       }
591   );
592   ScopedEntry entry(GetEntryFromString(json));
593   EXPECT_TRUE(entry.get() != NULL);
594   EXPECT_TRUE(entry->disabled());
595 }
596
597 TEST_F(GpuControlListEntryTest, OptimusEntry) {
598   const std::string json = LONG_STRING_CONST(
599       {
600         "id": 1,
601         "os": {
602           "type": "linux"
603         },
604         "multi_gpu_style": "optimus",
605         "features": [
606           "test_feature_0"
607         ]
608       }
609   );
610   GPUInfo gpu_info;
611   gpu_info.optimus = true;
612
613   ScopedEntry entry(GetEntryFromString(json));
614   EXPECT_TRUE(entry.get() != NULL);
615   EXPECT_EQ(GpuControlList::kOsLinux, entry->GetOsType());
616   EXPECT_TRUE(entry->Contains(
617       GpuControlList::kOsLinux, "10.6", gpu_info));
618 }
619
620 TEST_F(GpuControlListEntryTest, AMDSwitchableEntry) {
621   const std::string json = LONG_STRING_CONST(
622       {
623         "id": 1,
624         "os": {
625           "type": "macosx"
626         },
627         "multi_gpu_style": "amd_switchable",
628         "features": [
629           "test_feature_0"
630         ]
631       }
632   );
633   GPUInfo gpu_info;
634   gpu_info.amd_switchable = true;
635
636   ScopedEntry entry(GetEntryFromString(json));
637   EXPECT_TRUE(entry.get() != NULL);
638   EXPECT_EQ(GpuControlList::kOsMacosx, entry->GetOsType());
639   EXPECT_TRUE(entry->Contains(
640       GpuControlList::kOsMacosx, "10.6", gpu_info));
641 }
642
643 TEST_F(GpuControlListEntryTest, LexicalDriverVersionEntry) {
644   const std::string json = LONG_STRING_CONST(
645       {
646         "id": 1,
647         "os": {
648           "type": "linux"
649         },
650         "vendor_id": "0x1002",
651         "driver_version": {
652           "op": "=",
653           "style": "lexical",
654           "value": "8.76"
655         },
656         "features": [
657           "test_feature_0"
658         ]
659       }
660   );
661   GPUInfo gpu_info;
662   gpu_info.gpu.vendor_id = 0x1002;
663
664   ScopedEntry entry(GetEntryFromString(json));
665   EXPECT_TRUE(entry.get() != NULL);
666   EXPECT_EQ(GpuControlList::kOsLinux, entry->GetOsType());
667
668   gpu_info.driver_version = "8.76";
669   EXPECT_TRUE(entry->Contains(
670       GpuControlList::kOsLinux, "10.6", gpu_info));
671
672   gpu_info.driver_version = "8.768";
673   EXPECT_TRUE(entry->Contains(
674       GpuControlList::kOsLinux, "10.6", gpu_info));
675
676   gpu_info.driver_version = "8.76.8";
677   EXPECT_TRUE(entry->Contains(
678       GpuControlList::kOsLinux, "10.6", gpu_info));
679 }
680
681 TEST_F(GpuControlListEntryTest, NeedsMoreInfoEntry) {
682   const std::string json = LONG_STRING_CONST(
683       {
684         "id": 1,
685         "vendor_id": "0x8086",
686         "driver_version": {
687           "op": "<",
688           "value": "10.7"
689         },
690         "features": [
691           "test_feature_1"
692         ]
693       }
694   );
695   ScopedEntry entry(GetEntryFromString(json));
696   EXPECT_TRUE(entry.get() != NULL);
697
698   GPUInfo gpu_info;
699   gpu_info.gpu.vendor_id = 0x8086;
700   EXPECT_TRUE(entry->NeedsMoreInfo(gpu_info));
701
702   gpu_info.driver_version = "10.6";
703   EXPECT_FALSE(entry->NeedsMoreInfo(gpu_info));
704 }
705
706 TEST_F(GpuControlListEntryTest, NeedsMoreInfoForExceptionsEntry) {
707   const std::string json = LONG_STRING_CONST(
708       {
709         "id": 1,
710         "vendor_id": "0x8086",
711         "exceptions": [
712           {
713             "gl_renderer": {
714               "op": "contains",
715               "value": "mesa"
716             }
717           }
718         ],
719         "features": [
720           "test_feature_1"
721         ]
722       }
723   );
724   ScopedEntry entry(GetEntryFromString(json));
725   EXPECT_TRUE(entry.get() != NULL);
726
727   GPUInfo gpu_info;
728   gpu_info.gpu.vendor_id = 0x8086;
729   EXPECT_TRUE(entry->NeedsMoreInfo(gpu_info));
730
731   gpu_info.gl_renderer = "mesa";
732   EXPECT_FALSE(entry->NeedsMoreInfo(gpu_info));
733 }
734
735 TEST_F(GpuControlListEntryTest, FeatureTypeAllEntry) {
736   const std::string json = LONG_STRING_CONST(
737       {
738         "id": 1,
739         "features": [
740           "all"
741         ]
742       }
743   );
744   ScopedEntry entry(GetEntryFromString(json, true));
745   EXPECT_TRUE(entry.get() != NULL);
746   EXPECT_EQ(3u, entry->features().size());
747   EXPECT_EQ(1u, entry->features().count(TEST_FEATURE_0));
748   EXPECT_EQ(1u, entry->features().count(TEST_FEATURE_1));
749   EXPECT_EQ(1u, entry->features().count(TEST_FEATURE_2));
750 }
751
752 TEST_F(GpuControlListEntryTest, InvalidVendorIdEntry) {
753   const std::string json = LONG_STRING_CONST(
754       {
755         "id": 1,
756         "vendor_id": "0x0000",
757         "features": [
758           "test_feature_1"
759         ]
760       }
761   );
762   ScopedEntry entry(GetEntryFromString(json));
763   EXPECT_TRUE(entry.get() == NULL);
764 }
765
766 TEST_F(GpuControlListEntryTest, InvalidDeviceIdEntry) {
767   const std::string json = LONG_STRING_CONST(
768       {
769         "id": 1,
770         "vendor_id": "0x10de",
771         "device_id": ["0x1023", "0x0000"],
772         "features": [
773           "test_feature_1"
774         ]
775       }
776   );
777   ScopedEntry entry(GetEntryFromString(json));
778   EXPECT_TRUE(entry.get() == NULL);
779 }
780
781 TEST_F(GpuControlListEntryTest, SingleActiveGPU) {
782   const std::string json = LONG_STRING_CONST(
783       {
784         "id": 1,
785         "os": {
786           "type": "macosx"
787         },
788         "vendor_id": "0x10de",
789         "device_id": ["0x0640"],
790         "multi_gpu_category": "active",
791         "features": [
792           "test_feature_0"
793         ]
794       }
795   );
796   ScopedEntry entry(GetEntryFromString(json));
797   EXPECT_TRUE(entry.get() != NULL);
798   EXPECT_EQ(GpuControlList::kOsMacosx, entry->GetOsType());
799   EXPECT_TRUE(entry->Contains(
800       GpuControlList::kOsMacosx, "10.6", gpu_info()));
801 }
802
803 TEST_F(GpuControlListEntryTest, MachineModelName) {
804   const std::string json = LONG_STRING_CONST(
805       {
806         "id": 1,
807         "os": {
808           "type": "android"
809         },
810         "machine_model_name": ["Nexus 4", "XT1032"],
811         "features": [
812           "test_feature_0"
813         ]
814       }
815   );
816   ScopedEntry entry(GetEntryFromString(json));
817   EXPECT_TRUE(entry.get() != NULL);
818   EXPECT_EQ(GpuControlList::kOsAndroid, entry->GetOsType());
819   GPUInfo gpu_info;
820
821   gpu_info.machine_model_name = "Nexus 4";
822   EXPECT_TRUE(entry->Contains(
823       GpuControlList::kOsAndroid, "4.1", gpu_info));
824
825   gpu_info.machine_model_name = "XT1032";
826   EXPECT_TRUE(entry->Contains(
827       GpuControlList::kOsAndroid, "4.1", gpu_info));
828
829   gpu_info.machine_model_name = "XT1032i";
830   EXPECT_FALSE(entry->Contains(
831       GpuControlList::kOsAndroid, "4.1", gpu_info));
832
833   gpu_info.machine_model_name = "Nexus 5";
834   EXPECT_FALSE(entry->Contains(
835       GpuControlList::kOsAndroid, "4.1", gpu_info));
836
837   gpu_info.machine_model_name = "Nexus";
838   EXPECT_FALSE(entry->Contains(
839       GpuControlList::kOsAndroid, "4.1", gpu_info));
840
841   gpu_info.machine_model_name = "";
842   EXPECT_FALSE(entry->Contains(
843       GpuControlList::kOsAndroid, "4.1", gpu_info));
844 }
845
846 TEST_F(GpuControlListEntryTest, MachineModelNameException) {
847   const std::string json = LONG_STRING_CONST(
848       {
849         "id": 1,
850         "exceptions": [
851           {
852             "os": {
853               "type": "android"
854             },
855             "machine_model_name": ["Nexus 4"]
856           }
857         ],
858         "features": [
859           "test_feature_0"
860         ]
861       }
862   );
863   ScopedEntry entry(GetEntryFromString(json));
864   EXPECT_TRUE(entry.get() != NULL);
865   EXPECT_EQ(GpuControlList::kOsAny, entry->GetOsType());
866   GPUInfo gpu_info;
867
868   gpu_info.machine_model_name = "Nexus 4";
869   EXPECT_FALSE(entry->Contains(
870       GpuControlList::kOsAndroid, "4.1", gpu_info));
871   EXPECT_TRUE(entry->Contains(
872       GpuControlList::kOsLinux, "4.1", gpu_info));
873
874   gpu_info.machine_model_name = "";
875   EXPECT_TRUE(entry->Contains(
876       GpuControlList::kOsAndroid, "4.1", gpu_info));
877   EXPECT_TRUE(entry->Contains(
878       GpuControlList::kOsLinux, "4.1", gpu_info));
879 }
880
881 TEST_F(GpuControlListEntryTest, MachineModelVersion) {
882   const std::string json = LONG_STRING_CONST(
883       {
884         "id": 1,
885         "os": {
886           "type": "macosx"
887         },
888         "machine_model_name": ["MacBookPro"],
889         "machine_model_version": {
890           "op": "=",
891           "value": "7.1"
892         },
893         "features": [
894           "test_feature_0"
895         ]
896       }
897   );
898   ScopedEntry entry(GetEntryFromString(json));
899   EXPECT_TRUE(entry.get() != NULL);
900   GPUInfo gpu_info;
901   gpu_info.machine_model_name = "MacBookPro";
902   gpu_info.machine_model_version = "7.1";
903   EXPECT_EQ(GpuControlList::kOsMacosx, entry->GetOsType());
904   EXPECT_TRUE(entry->Contains(
905       GpuControlList::kOsMacosx, "10.6", gpu_info));
906 }
907
908 TEST_F(GpuControlListEntryTest, MachineModelVersionException) {
909   const std::string json = LONG_STRING_CONST(
910       {
911         "id": 1,
912         "os": {
913           "type": "macosx"
914         },
915         "machine_model_name": ["MacBookPro"],
916         "exceptions": [
917           {
918             "machine_model_version": {
919               "op": ">",
920               "value": "7.1"
921             }
922           }
923         ],
924         "features": [
925           "test_feature_0"
926         ]
927       }
928   );
929   ScopedEntry entry(GetEntryFromString(json));
930   EXPECT_TRUE(entry.get() != NULL);
931   EXPECT_EQ(GpuControlList::kOsMacosx, entry->GetOsType());
932
933   GPUInfo gpu_info;
934   gpu_info.machine_model_name = "MacBookPro";
935   gpu_info.machine_model_version = "7.0";
936   EXPECT_TRUE(entry->Contains(
937       GpuControlList::kOsMacosx, "10.6", gpu_info));
938
939   gpu_info.machine_model_version = "7.2";
940   EXPECT_FALSE(entry->Contains(
941       GpuControlList::kOsMacosx, "10.6", gpu_info));
942
943   gpu_info.machine_model_version = "";
944   EXPECT_TRUE(entry->Contains(
945       GpuControlList::kOsMacosx, "10.6", gpu_info));
946 }
947
948 class GpuControlListEntryDualGPUTest : public GpuControlListEntryTest {
949  public:
950   GpuControlListEntryDualGPUTest() { }
951   virtual ~GpuControlListEntryDualGPUTest() { }
952
953   virtual void SetUp() OVERRIDE {
954     // Set up a NVIDIA/Intel dual, with NVIDIA as primary and Intel as
955     // secondary, and initially Intel is active.
956     gpu_info_.gpu.vendor_id = 0x10de;
957     gpu_info_.gpu.device_id = 0x0640;
958     gpu_info_.gpu.active = false;
959     GPUInfo::GPUDevice second_gpu;
960     second_gpu.vendor_id = 0x8086;
961     second_gpu.device_id = 0x0166;
962     second_gpu.active = true;
963     gpu_info_.secondary_gpus.push_back(second_gpu);
964   }
965
966   void ActivatePrimaryGPU() {
967     gpu_info_.gpu.active = true;
968     gpu_info_.secondary_gpus[0].active = false;
969   }
970
971   void EntryShouldApply(const std::string& entry_json) const {
972     EXPECT_TRUE(EntryApplies(entry_json));
973   }
974
975   void EntryShouldNotApply(const std::string& entry_json) const {
976     EXPECT_FALSE(EntryApplies(entry_json));
977   }
978
979  private:
980   bool EntryApplies(const std::string& entry_json) const {
981     ScopedEntry entry(GetEntryFromString(entry_json));
982     EXPECT_TRUE(entry.get());
983     EXPECT_EQ(GpuControlList::kOsMacosx, entry->GetOsType());
984     return entry->Contains(GpuControlList::kOsMacosx, "10.6", gpu_info());
985   }
986 };
987
988 TEST_F(GpuControlListEntryDualGPUTest, CategoryAny) {
989   const std::string json_intel = LONG_STRING_CONST(
990       {
991         "id": 1,
992         "os": {
993           "type": "macosx"
994         },
995         "vendor_id": "0x8086",
996         "device_id": ["0x0166"],
997         "multi_gpu_category": "any",
998         "features": [
999           "test_feature_0"
1000         ]
1001       }
1002   );
1003   EntryShouldApply(json_intel);
1004
1005   const std::string json_nvidia = LONG_STRING_CONST(
1006       {
1007         "id": 1,
1008         "os": {
1009           "type": "macosx"
1010         },
1011         "vendor_id": "0x10de",
1012         "device_id": ["0x0640"],
1013         "multi_gpu_category": "any",
1014         "features": [
1015           "test_feature_0"
1016         ]
1017       }
1018   );
1019   EntryShouldApply(json_nvidia);
1020 }
1021
1022 TEST_F(GpuControlListEntryDualGPUTest, CategoryPrimarySecondary) {
1023   const std::string json_secondary = LONG_STRING_CONST(
1024       {
1025         "id": 1,
1026         "os": {
1027           "type": "macosx"
1028         },
1029         "vendor_id": "0x8086",
1030         "device_id": ["0x0166"],
1031         "multi_gpu_category": "secondary",
1032         "features": [
1033           "test_feature_0"
1034         ]
1035       }
1036   );
1037   EntryShouldApply(json_secondary);
1038
1039   const std::string json_primary = LONG_STRING_CONST(
1040       {
1041         "id": 1,
1042         "os": {
1043           "type": "macosx"
1044         },
1045         "vendor_id": "0x8086",
1046         "device_id": ["0x0166"],
1047         "multi_gpu_category": "primary",
1048         "features": [
1049           "test_feature_0"
1050         ]
1051       }
1052   );
1053   EntryShouldNotApply(json_primary);
1054
1055   const std::string json_default = LONG_STRING_CONST(
1056       {
1057         "id": 1,
1058         "os": {
1059           "type": "macosx"
1060         },
1061         "vendor_id": "0x8086",
1062         "device_id": ["0x0166"],
1063         "features": [
1064           "test_feature_0"
1065         ]
1066       }
1067   );
1068   // Default is primary.
1069   EntryShouldNotApply(json_default);
1070 }
1071
1072 TEST_F(GpuControlListEntryDualGPUTest, ActiveSecondaryGPU) {
1073   const std::string json = LONG_STRING_CONST(
1074       {
1075         "id": 1,
1076         "os": {
1077           "type": "macosx"
1078         },
1079         "vendor_id": "0x8086",
1080         "device_id": ["0x0166", "0x0168"],
1081         "multi_gpu_category": "active",
1082         "features": [
1083           "test_feature_0"
1084         ]
1085       }
1086   );
1087   // By default, secondary GPU is active.
1088   EntryShouldApply(json);
1089
1090   ActivatePrimaryGPU();
1091   EntryShouldNotApply(json);
1092 }
1093
1094 TEST_F(GpuControlListEntryDualGPUTest, VendorOnlyActiveSecondaryGPU) {
1095   const std::string json = LONG_STRING_CONST(
1096       {
1097         "id": 1,
1098         "os": {
1099           "type": "macosx"
1100         },
1101         "vendor_id": "0x8086",
1102         "multi_gpu_category": "active",
1103         "features": [
1104           "test_feature_0"
1105         ]
1106       }
1107   );
1108   // By default, secondary GPU is active.
1109   EntryShouldApply(json);
1110
1111   ActivatePrimaryGPU();
1112   EntryShouldNotApply(json);
1113 }
1114
1115 TEST_F(GpuControlListEntryDualGPUTest, ActivePrimaryGPU) {
1116   const std::string json = LONG_STRING_CONST(
1117       {
1118         "id": 1,
1119         "os": {
1120           "type": "macosx"
1121         },
1122         "vendor_id": "0x10de",
1123         "device_id": ["0x0640"],
1124         "multi_gpu_category": "active",
1125         "features": [
1126           "test_feature_0"
1127         ]
1128       }
1129   );
1130   // By default, secondary GPU is active.
1131   EntryShouldNotApply(json);
1132
1133   ActivatePrimaryGPU();
1134   EntryShouldApply(json);
1135 }
1136
1137 TEST_F(GpuControlListEntryDualGPUTest, VendorOnlyActivePrimaryGPU) {
1138   const std::string json = LONG_STRING_CONST(
1139       {
1140         "id": 1,
1141         "os": {
1142           "type": "macosx"
1143         },
1144         "vendor_id": "0x10de",
1145         "multi_gpu_category": "active",
1146         "features": [
1147           "test_feature_0"
1148         ]
1149       }
1150   );
1151   // By default, secondary GPU is active.
1152   EntryShouldNotApply(json);
1153
1154   ActivatePrimaryGPU();
1155   EntryShouldApply(json);
1156 }
1157
1158 }  // namespace gpu
1159