Added localized string for "Manage" button.
[chromium-blink-merge.git] / gpu / config / gpu_control_list_unittest.cc
blob33c69734a296d82bef79f363331ea0db7de6206a
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <vector>
7 #include "base/memory/scoped_ptr.h"
8 #include "gpu/config/gpu_control_list.h"
9 #include "gpu/config/gpu_info.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 const char kOsVersion[] = "10.6.4";
13 const uint32 kIntelVendorId = 0x8086;
14 const uint32 kNvidiaVendorId = 0x10de;
16 #define LONG_STRING_CONST(...) #__VA_ARGS__
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)
22 namespace gpu {
24 enum TestFeatureType {
25 TEST_FEATURE_0 = 1,
26 TEST_FEATURE_1 = 1 << 2,
27 TEST_FEATURE_2 = 1 << 3,
30 class GpuControlListTest : public testing::Test {
31 public:
32 GpuControlListTest() { }
34 virtual ~GpuControlListTest() { }
36 const GPUInfo& gpu_info() const {
37 return gpu_info_;
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;
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;
64 virtual void TearDown() {
67 private:
68 GPUInfo gpu_info_;
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);
79 TEST_F(GpuControlListTest, EmptyControlList) {
80 // Empty list: all features are allowed.
81 const std::string empty_list_json = LONG_STRING_CONST(
83 "name": "gpu control list",
84 "version": "2.5",
85 "entries": [
89 scoped_ptr<GpuControlList> control_list(Create());
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);
99 TEST_F(GpuControlListTest, DetailedEntryAndInvalidJson) {
100 // exact setting.
101 const std::string exact_list_json = LONG_STRING_CONST(
103 "name": "gpu control list",
104 "version": "0.1",
105 "entries": [
107 "id": 5,
108 "os": {
109 "type": "macosx",
110 "version": {
111 "op": "=",
112 "value": "10.6.4"
115 "vendor_id": "0x10de",
116 "device_id": ["0x0640"],
117 "driver_version": {
118 "op": "=",
119 "value": "1.6.18"
121 "features": [
122 "test_feature_0"
128 scoped_ptr<GpuControlList> control_list(Create());
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);
135 // Invalid json input should not change the current control_list settings.
136 const std::string invalid_json = "invalid";
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());
149 TEST_F(GpuControlListTest, VendorOnAllOsEntry) {
150 // ControlList a vendor on all OS.
151 const std::string vendor_json = LONG_STRING_CONST(
153 "name": "gpu control list",
154 "version": "0.1",
155 "entries": [
157 "id": 1,
158 "vendor_id": "0x10de",
159 "features": [
160 "test_feature_0"
166 scoped_ptr<GpuControlList> control_list(Create());
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
196 TEST_F(GpuControlListTest, UnknownField) {
197 const std::string unknown_field_json = LONG_STRING_CONST(
199 "name": "gpu control list",
200 "version": "0.1",
201 "entries": [
203 "id": 1,
204 "unknown_field": 0,
205 "features": [
206 "test_feature_1"
210 "id": 2,
211 "features": [
212 "test_feature_0"
218 scoped_ptr<GpuControlList> control_list(Create());
220 EXPECT_FALSE(control_list->LoadList(
221 unknown_field_json, GpuControlList::kAllOs));
224 TEST_F(GpuControlListTest, UnknownExceptionField) {
225 const std::string unknown_exception_field_json = LONG_STRING_CONST(
227 "name": "gpu control list",
228 "version": "0.1",
229 "entries": [
231 "id": 1,
232 "unknown_field": 0,
233 "features": [
234 "test_feature_2"
238 "id": 2,
239 "exceptions": [
241 "unknown_field": 0
244 "features": [
245 "test_feature_1"
249 "id": 3,
250 "features": [
251 "test_feature_0"
257 scoped_ptr<GpuControlList> control_list(Create());
259 EXPECT_FALSE(control_list->LoadList(
260 unknown_exception_field_json, GpuControlList::kAllOs));
263 TEST_F(GpuControlListTest, DisabledEntry) {
264 const std::string disabled_json = LONG_STRING_CONST(
266 "name": "gpu control list",
267 "version": "0.1",
268 "entries": [
270 "id": 1,
271 "disabled": true,
272 "features": [
273 "test_feature_0"
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());
291 TEST_F(GpuControlListTest, NeedsMoreInfoForExceptions) {
292 const std::string json = LONG_STRING_CONST(
294 "name": "gpu control list",
295 "version": "0.1",
296 "entries": [
298 "id": 1,
299 "os": {
300 "type": "linux"
302 "vendor_id": "0x8086",
303 "exceptions": [
305 "gl_renderer": {
306 "op": "contains",
307 "value": "mesa"
311 "features": [
312 "test_feature_0"
318 GPUInfo gpu_info;
319 gpu_info.gpu.vendor_id = kIntelVendorId;
321 scoped_ptr<GpuControlList> control_list(Create());
322 EXPECT_TRUE(control_list->LoadList(json, GpuControlList::kAllOs));
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());
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());
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());
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());
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(
357 "name": "gpu control list",
358 "version": "0.1",
359 "entries": [
361 "id": 1,
362 "os": {
363 "type": "linux"
365 "vendor_id": "0x8086",
366 "features": [
367 "test_feature_0"
371 "id": 2,
372 "os": {
373 "type": "linux"
375 "vendor_id": "0x8086",
376 "driver_version": {
377 "op": "<",
378 "value": "10.7"
380 "features": [
381 "test_feature_0"
387 GPUInfo gpu_info;
388 gpu_info.gpu.vendor_id = kIntelVendorId;
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());
398 TEST_F(GpuControlListTest, ExceptionWithoutVendorId) {
399 const std::string json = LONG_STRING_CONST(
401 "name": "gpu control list",
402 "version": "0.1",
403 "entries": [
405 "id": 1,
406 "os": {
407 "type": "linux"
409 "vendor_id": "0x8086",
410 "exceptions": [
412 "device_id": ["0x2a06"],
413 "driver_version": {
414 "op": ">=",
415 "value": "8.1"
419 "device_id": ["0x2a02"],
420 "driver_version": {
421 "op": ">=",
422 "value": "9.1"
426 "features": [
427 "test_feature_0"
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";
438 scoped_ptr<GpuControlList> control_list(Create());
439 EXPECT_TRUE(control_list->LoadList(json, GpuControlList::kAllOs));
441 std::set<int> features = control_list->MakeDecision(
442 GpuControlList::kOsLinux, kOsVersion, gpu_info);
443 EXPECT_EMPTY_SET(features);
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);
451 } // namespace gpu