Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(378)

Side by Side Diff: chrome/browser/about_flags_unittest.cc

Issue 12326019: Refactor about_flags.cc to make defining enabled/disabled/default flags easier. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/about_flags.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/prefs/pref_registry_simple.h" 5 #include "base/prefs/pref_registry_simple.h"
6 #include "base/prefs/testing_pref_service.h" 6 #include "base/prefs/testing_pref_service.h"
7 #include "base/strings/string_number_conversions.h" 7 #include "base/strings/string_number_conversions.h"
8 #include "base/utf_string_conversions.h" 8 #include "base/utf_string_conversions.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "chrome/browser/about_flags.h" 10 #include "chrome/browser/about_flags.h"
11 #include "chrome/common/chrome_switches.h" 11 #include "chrome/common/chrome_switches.h"
12 #include "chrome/common/pref_names.h" 12 #include "chrome/common/pref_names.h"
13 #include "grit/chromium_strings.h" 13 #include "grit/chromium_strings.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 15
16 const char kFlags1[] = "flag1"; 16 const char kFlags1[] = "flag1";
17 const char kFlags2[] = "flag2"; 17 const char kFlags2[] = "flag2";
18 const char kFlags3[] = "flag3"; 18 const char kFlags3[] = "flag3";
19 const char kFlags4[] = "flag4"; 19 const char kFlags4[] = "flag4";
20 const char kFlags5[] = "flag5";
20 21
21 const char kSwitch1[] = "switch"; 22 const char kSwitch1[] = "switch";
22 const char kSwitch2[] = "switch2"; 23 const char kSwitch2[] = "switch2";
23 const char kSwitch3[] = "switch3"; 24 const char kSwitch3[] = "switch3";
24 const char kValueForSwitch2[] = "value_for_switch2"; 25 const char kValueForSwitch2[] = "value_for_switch2";
25 26
26 const char kMultiSwitch1[] = "multi_switch1"; 27 const char kMultiSwitch1[] = "multi_switch1";
27 const char kMultiSwitch2[] = "multi_switch2"; 28 const char kMultiSwitch2[] = "multi_switch2";
28 const char kValueForMultiSwitch2[] = "value_for_multi_switch2"; 29 const char kValueForMultiSwitch2[] = "value_for_multi_switch2";
29 30
31 const char kEnableDisableValue1[] = "value1";
32 const char kEnableDisableValue2[] = "value2";
33
30 namespace about_flags { 34 namespace about_flags {
31 35
32 const Experiment::Choice kMultiChoices[] = { 36 const Experiment::Choice kMultiChoices[] = {
33 { IDS_PRODUCT_NAME, "", "" }, 37 { IDS_PRODUCT_NAME, "", "" },
34 { IDS_PRODUCT_NAME, kMultiSwitch1, "" }, 38 { IDS_PRODUCT_NAME, kMultiSwitch1, "" },
35 { IDS_PRODUCT_NAME, kMultiSwitch2, kValueForMultiSwitch2 }, 39 { IDS_PRODUCT_NAME, kMultiSwitch2, kValueForMultiSwitch2 },
36 }; 40 };
37 41
38 // The experiments that are set for these tests. The 3rd experiment is not 42 // The experiments that are set for these tests. The 3rd experiment is not
39 // supported on the current platform, all others are. 43 // supported on the current platform, all others are.
40 static Experiment kExperiments[] = { 44 static Experiment kExperiments[] = {
41 { 45 {
42 kFlags1, 46 kFlags1,
43 IDS_PRODUCT_NAME, 47 IDS_PRODUCT_NAME,
44 IDS_PRODUCT_NAME, 48 IDS_PRODUCT_NAME,
45 0, // Ends up being mapped to the current platform. 49 0, // Ends up being mapped to the current platform.
46 Experiment::SINGLE_VALUE, 50 Experiment::SINGLE_VALUE,
47 kSwitch1, 51 kSwitch1,
48 "", 52 "",
49 NULL, 53 NULL,
54 NULL,
55 NULL,
50 0 56 0
51 }, 57 },
52 { 58 {
53 kFlags2, 59 kFlags2,
54 IDS_PRODUCT_NAME, 60 IDS_PRODUCT_NAME,
55 IDS_PRODUCT_NAME, 61 IDS_PRODUCT_NAME,
56 0, // Ends up being mapped to the current platform. 62 0, // Ends up being mapped to the current platform.
57 Experiment::SINGLE_VALUE, 63 Experiment::SINGLE_VALUE,
58 kSwitch2, 64 kSwitch2,
59 kValueForSwitch2, 65 kValueForSwitch2,
60 NULL, 66 NULL,
67 NULL,
68 NULL,
61 0 69 0
62 }, 70 },
63 { 71 {
64 kFlags3, 72 kFlags3,
65 IDS_PRODUCT_NAME, 73 IDS_PRODUCT_NAME,
66 IDS_PRODUCT_NAME, 74 IDS_PRODUCT_NAME,
67 0, // This ends up enabling for an OS other than the current. 75 0, // This ends up enabling for an OS other than the current.
68 Experiment::SINGLE_VALUE, 76 Experiment::SINGLE_VALUE,
69 kSwitch3, 77 kSwitch3,
70 "", 78 "",
71 NULL, 79 NULL,
80 NULL,
81 NULL,
72 0 82 0
73 }, 83 },
74 { 84 {
75 kFlags4, 85 kFlags4,
76 IDS_PRODUCT_NAME, 86 IDS_PRODUCT_NAME,
77 IDS_PRODUCT_NAME, 87 IDS_PRODUCT_NAME,
78 0, // Ends up being mapped to the current platform. 88 0, // Ends up being mapped to the current platform.
79 Experiment::MULTI_VALUE, 89 Experiment::MULTI_VALUE,
80 "", 90 "",
81 "", 91 "",
92 "",
93 "",
82 kMultiChoices, 94 kMultiChoices,
83 arraysize(kMultiChoices) 95 arraysize(kMultiChoices)
84 }, 96 },
97 {
98 kFlags5,
99 IDS_PRODUCT_NAME,
100 IDS_PRODUCT_NAME,
101 0, // Ends up being mapped to the current platform.
102 Experiment::ENABLE_DISABLE_VALUE,
103 kSwitch1,
104 kEnableDisableValue1,
105 kSwitch2,
106 kEnableDisableValue2,
107 NULL,
108 3
109 },
85 }; 110 };
86 111
87 class AboutFlagsTest : public ::testing::Test { 112 class AboutFlagsTest : public ::testing::Test {
88 protected: 113 protected:
89 AboutFlagsTest() { 114 AboutFlagsTest() {
90 prefs_.registry()->RegisterListPref(prefs::kEnabledLabsExperiments); 115 prefs_.registry()->RegisterListPref(prefs::kEnabledLabsExperiments);
91 testing::ClearState(); 116 testing::ClearState();
92 } 117 }
93 118
94 virtual void SetUp() { 119 virtual void SetUp() OVERRIDE {
95 for (size_t i = 0; i < arraysize(kExperiments); ++i) 120 for (size_t i = 0; i < arraysize(kExperiments); ++i)
96 kExperiments[i].supported_platforms = GetCurrentPlatform(); 121 kExperiments[i].supported_platforms = GetCurrentPlatform();
97 122
98 int os_other_than_current = 1; 123 int os_other_than_current = 1;
99 while (os_other_than_current == GetCurrentPlatform()) 124 while (os_other_than_current == GetCurrentPlatform())
100 os_other_than_current <<= 1; 125 os_other_than_current <<= 1;
101 kExperiments[2].supported_platforms = os_other_than_current; 126 kExperiments[2].supported_platforms = os_other_than_current;
102 127
103 testing::SetExperiments(kExperiments, arraysize(kExperiments)); 128 testing::SetExperiments(kExperiments, arraysize(kExperiments));
104 } 129 }
105 130
106 virtual void TearDown() { 131 virtual void TearDown() OVERRIDE {
107 testing::SetExperiments(NULL, 0); 132 testing::SetExperiments(NULL, 0);
108 } 133 }
109 134
110 TestingPrefServiceSimple prefs_; 135 TestingPrefServiceSimple prefs_;
111 }; 136 };
112 137
113 TEST_F(AboutFlagsTest, ChangeNeedsRestart) { 138 TEST_F(AboutFlagsTest, ChangeNeedsRestart) {
114 EXPECT_FALSE(IsRestartNeededToCommitChanges()); 139 EXPECT_FALSE(IsRestartNeededToCommitChanges());
115 SetExperimentEnabled(&prefs_, kFlags1, true); 140 SetExperimentEnabled(&prefs_, kFlags1, true);
116 EXPECT_TRUE(IsRestartNeededToCommitChanges()); 141 EXPECT_TRUE(IsRestartNeededToCommitChanges());
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 #endif 296 #endif
272 297
273 // And it should persist 298 // And it should persist
274 scoped_ptr<ListValue> switch_prefs(GetFlagsExperimentsData(&prefs_)); 299 scoped_ptr<ListValue> switch_prefs(GetFlagsExperimentsData(&prefs_));
275 ASSERT_TRUE(switch_prefs.get()); 300 ASSERT_TRUE(switch_prefs.get());
276 EXPECT_EQ(arraysize(kExperiments), switch_prefs->GetSize()); 301 EXPECT_EQ(arraysize(kExperiments), switch_prefs->GetSize());
277 } 302 }
278 303
279 // Tests multi-value type experiments. 304 // Tests multi-value type experiments.
280 TEST_F(AboutFlagsTest, MultiValues) { 305 TEST_F(AboutFlagsTest, MultiValues) {
306 const Experiment& experiment = kExperiments[3];
307 ASSERT_EQ(kFlags4, experiment.internal_name);
308
281 // Initially, the first "deactivated" option of the multi experiment should 309 // Initially, the first "deactivated" option of the multi experiment should
282 // be set. 310 // be set.
283 { 311 {
284 CommandLine command_line(CommandLine::NO_PROGRAM); 312 CommandLine command_line(CommandLine::NO_PROGRAM);
285 ConvertFlagsToSwitches(&prefs_, &command_line); 313 ConvertFlagsToSwitches(&prefs_, &command_line);
286 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1)); 314 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1));
287 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2)); 315 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2));
288 } 316 }
289 317
290 // Enable the 2nd choice of the multi-value. 318 // Enable the 2nd choice of the multi-value.
291 SetExperimentEnabled(&prefs_, std::string(kFlags4) + 319 SetExperimentEnabled(&prefs_, experiment.NameForChoice(2), true);
292 std::string(testing::kMultiSeparator) +
293 base::IntToString(2), true);
294 { 320 {
295 CommandLine command_line(CommandLine::NO_PROGRAM); 321 CommandLine command_line(CommandLine::NO_PROGRAM);
296 ConvertFlagsToSwitches(&prefs_, &command_line); 322 ConvertFlagsToSwitches(&prefs_, &command_line);
297 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1)); 323 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1));
298 EXPECT_TRUE(command_line.HasSwitch(kMultiSwitch2)); 324 EXPECT_TRUE(command_line.HasSwitch(kMultiSwitch2));
299 EXPECT_EQ(std::string(kValueForMultiSwitch2), 325 EXPECT_EQ(std::string(kValueForMultiSwitch2),
300 command_line.GetSwitchValueASCII(kMultiSwitch2)); 326 command_line.GetSwitchValueASCII(kMultiSwitch2));
301 } 327 }
302 328
303 // Disable the multi-value experiment. 329 // Disable the multi-value experiment.
304 SetExperimentEnabled(&prefs_, std::string(kFlags4) + 330 SetExperimentEnabled(&prefs_, experiment.NameForChoice(0), true);
305 std::string(testing::kMultiSeparator) +
306 base::IntToString(0), true);
307 { 331 {
308 CommandLine command_line(CommandLine::NO_PROGRAM); 332 CommandLine command_line(CommandLine::NO_PROGRAM);
309 ConvertFlagsToSwitches(&prefs_, &command_line); 333 ConvertFlagsToSwitches(&prefs_, &command_line);
310 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1)); 334 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1));
311 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2)); 335 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2));
312 } 336 }
313 } 337 }
314 338
339 TEST_F(AboutFlagsTest, EnableDisableValues) {
340 const Experiment& experiment = kExperiments[4];
341 ASSERT_EQ(kFlags5, experiment.internal_name);
342
343 // Nothing selected.
344 {
345 CommandLine command_line(CommandLine::NO_PROGRAM);
346 ConvertFlagsToSwitches(&prefs_, &command_line);
347 EXPECT_FALSE(command_line.HasSwitch(kSwitch1));
348 EXPECT_FALSE(command_line.HasSwitch(kSwitch2));
349 }
350
351 // "Enable" option selected.
352 SetExperimentEnabled(&prefs_, experiment.NameForChoice(1), true);
353 {
354 CommandLine command_line(CommandLine::NO_PROGRAM);
355 ConvertFlagsToSwitches(&prefs_, &command_line);
356 EXPECT_TRUE(command_line.HasSwitch(kSwitch1));
357 EXPECT_FALSE(command_line.HasSwitch(kSwitch2));
358 EXPECT_EQ(kEnableDisableValue1, command_line.GetSwitchValueASCII(kSwitch1));
359 }
360
361 // "Disable" option selected.
362 SetExperimentEnabled(&prefs_, experiment.NameForChoice(2), true);
363 {
364 CommandLine command_line(CommandLine::NO_PROGRAM);
365 ConvertFlagsToSwitches(&prefs_, &command_line);
366 EXPECT_FALSE(command_line.HasSwitch(kSwitch1));
367 EXPECT_TRUE(command_line.HasSwitch(kSwitch2));
368 EXPECT_EQ(kEnableDisableValue2, command_line.GetSwitchValueASCII(kSwitch2));
369 }
370
371 // "Default" option selected, same as nothing selected.
372 SetExperimentEnabled(&prefs_, experiment.NameForChoice(0), true);
373 {
374 CommandLine command_line(CommandLine::NO_PROGRAM);
375 ConvertFlagsToSwitches(&prefs_, &command_line);
376 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1));
377 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2));
378 }
379 }
380
315 // Makes sure there are no separators in any of the experiment names. 381 // Makes sure there are no separators in any of the experiment names.
316 TEST_F(AboutFlagsTest, NoSeparators) { 382 TEST_F(AboutFlagsTest, NoSeparators) {
317 testing::SetExperiments(NULL, 0); 383 testing::SetExperiments(NULL, 0);
318 size_t count; 384 size_t count;
319 const Experiment* experiments = testing::GetExperiments(&count); 385 const Experiment* experiments = testing::GetExperiments(&count);
320 for (size_t i = 0; i < count; ++i) { 386 for (size_t i = 0; i < count; ++i) {
321 std::string name = experiments->internal_name; 387 std::string name = experiments->internal_name;
322 EXPECT_EQ(std::string::npos, name.find(testing::kMultiSeparator)) << i; 388 EXPECT_EQ(std::string::npos, name.find(testing::kMultiSeparator)) << i;
323 } 389 }
324 } 390 }
325 391
326 } // namespace about_flags 392 } // namespace about_flags
OLDNEW
« no previous file with comments | « chrome/browser/about_flags.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698