Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / gpu / config / gpu_control_list_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 <vector>
6
7 #include "base/memory/scoped_ptr.h"
8 #include "gpu/config/gpu_control_list.h"
9 #include "gpu/config/gpu_info.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 const char kOsVersion[] = "10.6.4";
13 const uint32 kIntelVendorId = 0x8086;
14 const uint32 kNvidiaVendorId = 0x10de;
15
16 #define LONG_STRING_CONST(...) #__VA_ARGS__
17
18 #define EXPECT_EMPTY_SET(feature_set) EXPECT_EQ(0u, feature_set.size())
19 #define EXPECT_SINGLE_FEATURE(feature_set, feature) \
20     EXPECT_TRUE(feature_set.size() == 1 && feature_set.count(feature) == 1)
21
22 namespace gpu {
23
24 enum TestFeatureType {
25   TEST_FEATURE_0 = 1,
26   TEST_FEATURE_1 = 1 << 2,
27   TEST_FEATURE_2 = 1 << 3,
28 };
29
30 class GpuControlListTest : public testing::Test {
31  public:
32   GpuControlListTest() { }
33
34   virtual ~GpuControlListTest() { }
35
36   const GPUInfo& gpu_info() const {
37     return gpu_info_;
38   }
39
40   GpuControlList* Create() {
41     GpuControlList* rt = new GpuControlList();
42     rt->AddSupportedFeature("test_feature_0", TEST_FEATURE_0);
43     rt->AddSupportedFeature("test_feature_1", TEST_FEATURE_1);
44     rt->AddSupportedFeature("test_feature_2", TEST_FEATURE_2);
45     return rt;
46   }
47
48  protected:
49   virtual void SetUp() {
50     gpu_info_.gpu.vendor_id = kNvidiaVendorId;
51     gpu_info_.gpu.device_id = 0x0640;
52     gpu_info_.driver_vendor = "NVIDIA";
53     gpu_info_.driver_version = "1.6.18";
54     gpu_info_.driver_date = "7-14-2009";
55     gpu_info_.machine_model_name = "MacBookPro";
56     gpu_info_.machine_model_version = "7.1";
57     gpu_info_.gl_vendor = "NVIDIA Corporation";
58     gpu_info_.gl_renderer = "NVIDIA GeForce GT 120 OpenGL Engine";
59     gpu_info_.performance_stats.graphics = 5.0;
60     gpu_info_.performance_stats.gaming = 5.0;
61     gpu_info_.performance_stats.overall = 5.0;
62   }
63
64   virtual void TearDown() {
65   }
66
67  private:
68   GPUInfo gpu_info_;
69 };
70
71 TEST_F(GpuControlListTest, DefaultControlListSettings) {
72   scoped_ptr<GpuControlList> control_list(Create());
73   // Default control list settings: all feature are allowed.
74   std::set<int> features = control_list->MakeDecision(
75       GpuControlList::kOsMacosx, kOsVersion, gpu_info());
76   EXPECT_EMPTY_SET(features);
77 }
78
79 TEST_F(GpuControlListTest, EmptyControlList) {
80   // Empty list: all features are allowed.
81   const std::string empty_list_json = LONG_STRING_CONST(
82       {
83         "name": "gpu control list",
84         "version": "2.5",
85         "entries": [
86         ]
87       }
88   );
89   scoped_ptr<GpuControlList> control_list(Create());
90
91   EXPECT_TRUE(control_list->LoadList(empty_list_json,
92                                      GpuControlList::kAllOs));
93   EXPECT_EQ("2.5", control_list->version());
94   std::set<int> features = control_list->MakeDecision(
95       GpuControlList::kOsMacosx, kOsVersion, gpu_info());
96   EXPECT_EMPTY_SET(features);
97 }
98
99 TEST_F(GpuControlListTest, DetailedEntryAndInvalidJson) {
100   // exact setting.
101   const std::string exact_list_json = LONG_STRING_CONST(
102       {
103         "name": "gpu control list",
104         "version": "0.1",
105         "entries": [
106           {
107             "id": 5,
108             "os": {
109               "type": "macosx",
110               "version": {
111                 "op": "=",
112                 "value": "10.6.4"
113               }
114             },
115             "vendor_id": "0x10de",
116             "device_id": ["0x0640"],
117             "driver_version": {
118               "op": "=",
119               "value": "1.6.18"
120             },
121             "features": [
122               "test_feature_0"
123             ]
124           }
125         ]
126       }
127   );
128   scoped_ptr<GpuControlList> control_list(Create());
129
130   EXPECT_TRUE(control_list->LoadList(exact_list_json, GpuControlList::kAllOs));
131   std::set<int> features = control_list->MakeDecision(
132       GpuControlList::kOsMacosx, kOsVersion, gpu_info());
133   EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
134
135   // Invalid json input should not change the current control_list settings.
136   const std::string invalid_json = "invalid";
137
138   EXPECT_FALSE(control_list->LoadList(invalid_json, GpuControlList::kAllOs));
139   features = control_list->MakeDecision(
140       GpuControlList::kOsMacosx, kOsVersion, gpu_info());
141   EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
142   std::vector<uint32> entries;
143   control_list->GetDecisionEntries(&entries, false);
144   ASSERT_EQ(1u, entries.size());
145   EXPECT_EQ(5u, entries[0]);
146   EXPECT_EQ(5u, control_list->max_entry_id());
147 }
148
149 TEST_F(GpuControlListTest, VendorOnAllOsEntry) {
150   // ControlList a vendor on all OS.
151   const std::string vendor_json = LONG_STRING_CONST(
152       {
153         "name": "gpu control list",
154         "version": "0.1",
155         "entries": [
156           {
157             "id": 1,
158             "vendor_id": "0x10de",
159             "features": [
160               "test_feature_0"
161             ]
162           }
163         ]
164       }
165   );
166   scoped_ptr<GpuControlList> control_list(Create());
167
168   // ControlList entries won't be filtered to the current OS only upon loading.
169   EXPECT_TRUE(control_list->LoadList(vendor_json, GpuControlList::kAllOs));
170   std::set<int> features = control_list->MakeDecision(
171       GpuControlList::kOsMacosx, kOsVersion, gpu_info());
172   EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
173   features = control_list->MakeDecision(
174       GpuControlList::kOsWin, kOsVersion, gpu_info());
175   EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
176   features = control_list->MakeDecision(
177       GpuControlList::kOsLinux, kOsVersion, gpu_info());
178   EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
179 #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_MACOSX) || \
180     defined(OS_OPENBSD)
181   // ControlList entries will be filtered to the current OS only upon loading.
182   EXPECT_TRUE(control_list->LoadList(
183       vendor_json, GpuControlList::kCurrentOsOnly));
184   features = control_list->MakeDecision(
185       GpuControlList::kOsMacosx, kOsVersion, gpu_info());
186   EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
187   features = control_list->MakeDecision(
188       GpuControlList::kOsWin, kOsVersion, gpu_info());
189   EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
190   features = control_list->MakeDecision(
191       GpuControlList::kOsLinux, kOsVersion, gpu_info());
192   EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
193 #endif
194 }
195
196 TEST_F(GpuControlListTest, UnknownField) {
197   const std::string unknown_field_json = LONG_STRING_CONST(
198       {
199         "name": "gpu control list",
200         "version": "0.1",
201         "entries": [
202           {
203             "id": 1,
204             "unknown_field": 0,
205             "features": [
206               "test_feature_1"
207             ]
208           },
209           {
210             "id": 2,
211             "features": [
212               "test_feature_0"
213             ]
214           }
215         ]
216       }
217   );
218   scoped_ptr<GpuControlList> control_list(Create());
219
220   EXPECT_FALSE(control_list->LoadList(
221       unknown_field_json, GpuControlList::kAllOs));
222 }
223
224 TEST_F(GpuControlListTest, UnknownExceptionField) {
225   const std::string unknown_exception_field_json = LONG_STRING_CONST(
226       {
227         "name": "gpu control list",
228         "version": "0.1",
229         "entries": [
230           {
231             "id": 1,
232             "unknown_field": 0,
233             "features": [
234               "test_feature_2"
235             ]
236           },
237           {
238             "id": 2,
239             "exceptions": [
240               {
241                 "unknown_field": 0
242               }
243             ],
244             "features": [
245               "test_feature_1"
246             ]
247           },
248           {
249             "id": 3,
250             "features": [
251               "test_feature_0"
252             ]
253           }
254         ]
255       }
256   );
257   scoped_ptr<GpuControlList> control_list(Create());
258
259   EXPECT_FALSE(control_list->LoadList(
260       unknown_exception_field_json, GpuControlList::kAllOs));
261 }
262
263 TEST_F(GpuControlListTest, DisabledEntry) {
264   const std::string disabled_json = LONG_STRING_CONST(
265       {
266         "name": "gpu control list",
267         "version": "0.1",
268         "entries": [
269           {
270             "id": 1,
271             "disabled": true,
272             "features": [
273               "test_feature_0"
274             ]
275           }
276         ]
277       }
278   );
279   scoped_ptr<GpuControlList> control_list(Create());
280   EXPECT_TRUE(control_list->LoadList(disabled_json, GpuControlList::kAllOs));
281   std::set<int> features = control_list->MakeDecision(
282       GpuControlList::kOsWin, kOsVersion, gpu_info());
283   EXPECT_EMPTY_SET(features);
284   std::vector<uint32> flag_entries;
285   control_list->GetDecisionEntries(&flag_entries, false);
286   EXPECT_EQ(0u, flag_entries.size());
287   control_list->GetDecisionEntries(&flag_entries, true);
288   EXPECT_EQ(1u, flag_entries.size());
289 }
290
291 TEST_F(GpuControlListTest, NeedsMoreInfoForExceptions) {
292   const std::string json = LONG_STRING_CONST(
293       {
294         "name": "gpu control list",
295         "version": "0.1",
296         "entries": [
297           {
298             "id": 1,
299             "os": {
300               "type": "linux"
301             },
302             "vendor_id": "0x8086",
303             "exceptions": [
304               {
305                 "gl_renderer": {
306                   "op": "contains",
307                   "value": "mesa"
308                 }
309               }
310             ],
311             "features": [
312               "test_feature_0"
313             ]
314           }
315         ]
316       }
317   );
318   GPUInfo gpu_info;
319   gpu_info.gpu.vendor_id = kIntelVendorId;
320
321   scoped_ptr<GpuControlList> control_list(Create());
322   EXPECT_TRUE(control_list->LoadList(json, GpuControlList::kAllOs));
323
324   // The case this entry does not apply.
325   std::set<int> features = control_list->MakeDecision(
326       GpuControlList::kOsMacosx, kOsVersion, gpu_info);
327   EXPECT_EMPTY_SET(features);
328   EXPECT_FALSE(control_list->needs_more_info());
329
330   // The case this entry might apply, but need more info.
331   features = control_list->MakeDecision(
332       GpuControlList::kOsLinux, kOsVersion, gpu_info);
333   EXPECT_EMPTY_SET(features);
334   EXPECT_TRUE(control_list->needs_more_info());
335
336   // The case we have full info, and the exception applies (so the entry
337   // does not apply).
338   gpu_info.gl_renderer = "mesa";
339   features = control_list->MakeDecision(
340       GpuControlList::kOsLinux, kOsVersion, gpu_info);
341   EXPECT_EMPTY_SET(features);
342   EXPECT_FALSE(control_list->needs_more_info());
343
344   // The case we have full info, and this entry applies.
345   gpu_info.gl_renderer = "my renderer";
346   features = control_list->MakeDecision(GpuControlList::kOsLinux, kOsVersion,
347       gpu_info);
348   EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
349   EXPECT_FALSE(control_list->needs_more_info());
350 }
351
352 TEST_F(GpuControlListTest, IgnorableEntries) {
353   // If an entry will not change the control_list decisions, then it should not
354   // trigger the needs_more_info flag.
355   const std::string json = LONG_STRING_CONST(
356       {
357         "name": "gpu control list",
358         "version": "0.1",
359         "entries": [
360           {
361             "id": 1,
362             "os": {
363               "type": "linux"
364             },
365             "vendor_id": "0x8086",
366             "features": [
367               "test_feature_0"
368             ]
369           },
370           {
371             "id": 2,
372             "os": {
373               "type": "linux"
374             },
375             "vendor_id": "0x8086",
376             "driver_version": {
377               "op": "<",
378               "value": "10.7"
379             },
380             "features": [
381               "test_feature_0"
382             ]
383           }
384         ]
385       }
386   );
387   GPUInfo gpu_info;
388   gpu_info.gpu.vendor_id = kIntelVendorId;
389
390   scoped_ptr<GpuControlList> control_list(Create());
391   EXPECT_TRUE(control_list->LoadList(json, GpuControlList::kAllOs));
392   std::set<int> features = control_list->MakeDecision(
393       GpuControlList::kOsLinux, kOsVersion, gpu_info);
394   EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
395   EXPECT_FALSE(control_list->needs_more_info());
396 }
397
398 TEST_F(GpuControlListTest, ExceptionWithoutVendorId) {
399   const std::string json = LONG_STRING_CONST(
400       {
401         "name": "gpu control list",
402         "version": "0.1",
403         "entries": [
404           {
405             "id": 1,
406             "os": {
407               "type": "linux"
408             },
409             "vendor_id": "0x8086",
410             "exceptions": [
411               {
412                 "device_id": ["0x2a06"],
413                 "driver_version": {
414                   "op": ">=",
415                   "value": "8.1"
416                 }
417               },
418               {
419                 "device_id": ["0x2a02"],
420                 "driver_version": {
421                   "op": ">=",
422                   "value": "9.1"
423                 }
424               }
425             ],
426             "features": [
427               "test_feature_0"
428             ]
429           }
430         ]
431       }
432   );
433   GPUInfo gpu_info;
434   gpu_info.gpu.vendor_id = kIntelVendorId;
435   gpu_info.gpu.device_id = 0x2a02;
436   gpu_info.driver_version = "9.1";
437
438   scoped_ptr<GpuControlList> control_list(Create());
439   EXPECT_TRUE(control_list->LoadList(json, GpuControlList::kAllOs));
440
441   std::set<int> features = control_list->MakeDecision(
442       GpuControlList::kOsLinux, kOsVersion, gpu_info);
443   EXPECT_EMPTY_SET(features);
444
445   gpu_info.driver_version = "9.0";
446   features = control_list->MakeDecision(
447       GpuControlList::kOsLinux, kOsVersion, gpu_info);
448   EXPECT_SINGLE_FEATURE(features, TEST_FEATURE_0);
449 }
450
451 }  // namespace gpu
452