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
3 // found in the LICENSE file.
7 #include "base/files/file_path.h"
8 #include "base/memory/scoped_vector.h"
9 #include "base/path_service.h"
10 #include "base/prefs/pref_registry_simple.h"
11 #include "base/prefs/testing_pref_service.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/values.h"
16 #include "chrome/browser/about_flags.h"
17 #include "chrome/browser/pref_service_flags_storage.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/pref_names.h"
20 #include "chrome/grit/chromium_strings.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "third_party/libxml/chromium/libxml_utils.h"
26 const char kFlags1
[] = "flag1";
27 const char kFlags2
[] = "flag2";
28 const char kFlags3
[] = "flag3";
29 const char kFlags4
[] = "flag4";
30 const char kFlags5
[] = "flag5";
32 const char kSwitch1
[] = "switch";
33 const char kSwitch2
[] = "switch2";
34 const char kSwitch3
[] = "switch3";
35 const char kValueForSwitch2
[] = "value_for_switch2";
37 const char kMultiSwitch1
[] = "multi_switch1";
38 const char kMultiSwitch2
[] = "multi_switch2";
39 const char kValueForMultiSwitch2
[] = "value_for_multi_switch2";
41 const char kEnableDisableValue1
[] = "value1";
42 const char kEnableDisableValue2
[] = "value2";
44 typedef std::map
<std::string
, uint32_t> SwitchToIdMap
;
46 // This is a helper function to the ReadEnumFromHistogramsXml().
47 // Extracts single enum (with integer values) from histograms.xml.
48 // Expects |reader| to point at given enum.
49 // Returns map { value => label }.
50 // Returns empty map on error.
51 std::map
<uint32_t, std::string
> ParseEnumFromHistogramsXml(
52 const std::string
& enum_name
,
54 int entries_index
= -1;
56 std::map
<uint32_t, std::string
> result
;
60 const std::string node_name
= reader
->NodeName();
61 if (node_name
== "enum" && reader
->IsClosingElement())
64 if (node_name
== "int") {
66 std::string value_str
;
68 const bool has_value
= reader
->NodeAttribute("value", &value_str
);
69 const bool has_label
= reader
->NodeAttribute("label", &label
);
71 ADD_FAILURE() << "Bad " << enum_name
<< " enum entry (at index "
72 << entries_index
<< ", label='" << label
73 << "'): No 'value' attribute.";
77 ADD_FAILURE() << "Bad " << enum_name
<< " enum entry (at index "
78 << entries_index
<< ", value_str='" << value_str
79 << "'): No 'label' attribute.";
84 if (has_value
&& !base::StringToUint(value_str
, &value
)) {
85 ADD_FAILURE() << "Bad " << enum_name
<< " enum entry (at index "
86 << entries_index
<< ", label='" << label
87 << "', value_str='" << value_str
88 << "'): 'value' attribute is not integer.";
91 if (result
.count(value
)) {
92 ADD_FAILURE() << "Bad " << enum_name
<< " enum entry (at index "
93 << entries_index
<< ", label='" << label
94 << "', value_str='" << value_str
95 << "'): duplicate value '" << value_str
96 << "' found in enum. The previous one has label='"
97 << result
[value
] << "'.";
101 result
[value
] = label
;
104 // All enum entries are on the same level, so it is enough to iterate
108 return (success
? result
: std::map
<uint32_t, std::string
>());
111 // Find and read given enum (with integer values) from histograms.xml.
112 // |enum_name| - enum name.
113 // |histograms_xml| - must be loaded histograms.xml file.
115 // Returns map { value => label } so that:
116 // <int value="9" label="enable-pinch-virtual-viewport"/>
118 // { 9 => "enable-pinch-virtual-viewport" }
119 // Returns empty map on error.
120 std::map
<uint32_t, std::string
> ReadEnumFromHistogramsXml(
121 const std::string
& enum_name
,
122 XmlReader
* histograms_xml
) {
123 std::map
<uint32_t, std::string
> login_custom_flags
;
125 // Implement simple depth first search.
127 const std::string node_name
= histograms_xml
->NodeName();
128 if (node_name
== "enum") {
130 if (histograms_xml
->NodeAttribute("name", &name
) && name
== enum_name
) {
131 if (!login_custom_flags
.empty()) {
132 EXPECT_TRUE(login_custom_flags
.empty())
133 << "Duplicate enum '" << enum_name
<< "' found in histograms.xml";
134 return std::map
<uint32_t, std::string
>();
137 const bool got_into_enum
= histograms_xml
->Read();
140 ParseEnumFromHistogramsXml(enum_name
, histograms_xml
);
141 EXPECT_FALSE(login_custom_flags
.empty())
142 << "Bad enum '" << enum_name
143 << "' found in histograms.xml (format error).";
145 EXPECT_TRUE(got_into_enum
)
146 << "Bad enum '" << enum_name
147 << "' (looks empty) found in histograms.xml.";
149 if (login_custom_flags
.empty())
150 return std::map
<uint32_t, std::string
>();
153 // Go deeper if possible (stops at the closing tag of the deepest node).
154 if (histograms_xml
->Read())
157 // Try next node on the same level (skips closing tag).
158 if (histograms_xml
->Next())
161 // Go up until next node on the same level exists.
162 while (histograms_xml
->Depth() && !histograms_xml
->SkipToElement()) {
165 // Reached top. histograms.xml consists of the single top level node
166 // 'histogram-configuration', so this is the end.
167 if (!histograms_xml
->Depth())
170 EXPECT_FALSE(login_custom_flags
.empty())
171 << "Enum '" << enum_name
<< "' is not found in histograms.xml.";
172 return login_custom_flags
;
175 std::string
FilePathStringTypeToString(const base::FilePath::StringType
& path
) {
177 return base::UTF16ToUTF8(path
);
183 std::set
<std::string
> GetAllSwitchesForTesting() {
184 std::set
<std::string
> result
;
186 size_t num_experiments
= 0;
187 const about_flags::Experiment
* experiments
=
188 about_flags::testing::GetExperiments(&num_experiments
);
190 for (size_t i
= 0; i
< num_experiments
; ++i
) {
191 const about_flags::Experiment
& experiment
= experiments
[i
];
192 if (experiment
.type
== about_flags::Experiment::SINGLE_VALUE
) {
193 result
.insert(experiment
.command_line_switch
);
194 } else if (experiment
.type
== about_flags::Experiment::MULTI_VALUE
) {
195 for (int j
= 0; j
< experiment
.num_choices
; ++j
) {
196 result
.insert(experiment
.choices
[j
].command_line_switch
);
199 DCHECK_EQ(experiment
.type
, about_flags::Experiment::ENABLE_DISABLE_VALUE
);
200 result
.insert(experiment
.command_line_switch
);
201 result
.insert(experiment
.disable_command_line_switch
);
207 } // anonymous namespace
209 namespace about_flags
{
211 const Experiment::Choice kMultiChoices
[] = {
212 { IDS_PRODUCT_NAME
, "", "" },
213 { IDS_PRODUCT_NAME
, kMultiSwitch1
, "" },
214 { IDS_PRODUCT_NAME
, kMultiSwitch2
, kValueForMultiSwitch2
},
217 // The experiments that are set for these tests. The 3rd experiment is not
218 // supported on the current platform, all others are.
219 static Experiment kExperiments
[] = {
224 0, // Ends up being mapped to the current platform.
225 Experiment::SINGLE_VALUE
,
237 0, // Ends up being mapped to the current platform.
238 Experiment::SINGLE_VALUE
,
250 0, // This ends up enabling for an OS other than the current.
251 Experiment::SINGLE_VALUE
,
263 0, // Ends up being mapped to the current platform.
264 Experiment::MULTI_VALUE
,
270 arraysize(kMultiChoices
)
276 0, // Ends up being mapped to the current platform.
277 Experiment::ENABLE_DISABLE_VALUE
,
279 kEnableDisableValue1
,
281 kEnableDisableValue2
,
287 class AboutFlagsTest
: public ::testing::Test
{
289 AboutFlagsTest() : flags_storage_(&prefs_
) {
290 prefs_
.registry()->RegisterListPref(prefs::kEnabledLabsExperiments
);
291 testing::ClearState();
294 virtual void SetUp() OVERRIDE
{
295 for (size_t i
= 0; i
< arraysize(kExperiments
); ++i
)
296 kExperiments
[i
].supported_platforms
= GetCurrentPlatform();
298 int os_other_than_current
= 1;
299 while (os_other_than_current
== GetCurrentPlatform())
300 os_other_than_current
<<= 1;
301 kExperiments
[2].supported_platforms
= os_other_than_current
;
303 testing::SetExperiments(kExperiments
, arraysize(kExperiments
));
306 virtual void TearDown() OVERRIDE
{
307 testing::SetExperiments(NULL
, 0);
310 TestingPrefServiceSimple prefs_
;
311 PrefServiceFlagsStorage flags_storage_
;
315 TEST_F(AboutFlagsTest
, NoChangeNoRestart
) {
316 EXPECT_FALSE(IsRestartNeededToCommitChanges());
317 SetExperimentEnabled(&flags_storage_
, kFlags1
, false);
318 EXPECT_FALSE(IsRestartNeededToCommitChanges());
321 TEST_F(AboutFlagsTest
, ChangeNeedsRestart
) {
322 EXPECT_FALSE(IsRestartNeededToCommitChanges());
323 SetExperimentEnabled(&flags_storage_
, kFlags1
, true);
324 EXPECT_TRUE(IsRestartNeededToCommitChanges());
327 TEST_F(AboutFlagsTest
, MultiFlagChangeNeedsRestart
) {
328 const Experiment
& experiment
= kExperiments
[3];
329 ASSERT_EQ(kFlags4
, experiment
.internal_name
);
330 EXPECT_FALSE(IsRestartNeededToCommitChanges());
331 // Enable the 2nd choice of the multi-value.
332 SetExperimentEnabled(&flags_storage_
, experiment
.NameForChoice(2), true);
333 EXPECT_TRUE(IsRestartNeededToCommitChanges());
334 testing::ClearState();
335 EXPECT_FALSE(IsRestartNeededToCommitChanges());
336 // Enable the default choice now.
337 SetExperimentEnabled(&flags_storage_
, experiment
.NameForChoice(0), true);
338 EXPECT_TRUE(IsRestartNeededToCommitChanges());
341 TEST_F(AboutFlagsTest
, AddTwoFlagsRemoveOne
) {
342 // Add two experiments, check they're there.
343 SetExperimentEnabled(&flags_storage_
, kFlags1
, true);
344 SetExperimentEnabled(&flags_storage_
, kFlags2
, true);
346 const base::ListValue
* experiments_list
= prefs_
.GetList(
347 prefs::kEnabledLabsExperiments
);
348 ASSERT_TRUE(experiments_list
!= NULL
);
350 ASSERT_EQ(2u, experiments_list
->GetSize());
353 ASSERT_TRUE(experiments_list
->GetString(0, &s0
));
355 ASSERT_TRUE(experiments_list
->GetString(1, &s1
));
357 EXPECT_TRUE(s0
== kFlags1
|| s1
== kFlags1
);
358 EXPECT_TRUE(s0
== kFlags2
|| s1
== kFlags2
);
360 // Remove one experiment, check the other's still around.
361 SetExperimentEnabled(&flags_storage_
, kFlags2
, false);
363 experiments_list
= prefs_
.GetList(prefs::kEnabledLabsExperiments
);
364 ASSERT_TRUE(experiments_list
!= NULL
);
365 ASSERT_EQ(1u, experiments_list
->GetSize());
366 ASSERT_TRUE(experiments_list
->GetString(0, &s0
));
367 EXPECT_TRUE(s0
== kFlags1
);
370 TEST_F(AboutFlagsTest
, AddTwoFlagsRemoveBoth
) {
371 // Add two experiments, check the pref exists.
372 SetExperimentEnabled(&flags_storage_
, kFlags1
, true);
373 SetExperimentEnabled(&flags_storage_
, kFlags2
, true);
374 const base::ListValue
* experiments_list
= prefs_
.GetList(
375 prefs::kEnabledLabsExperiments
);
376 ASSERT_TRUE(experiments_list
!= NULL
);
378 // Remove both, the pref should have been removed completely.
379 SetExperimentEnabled(&flags_storage_
, kFlags1
, false);
380 SetExperimentEnabled(&flags_storage_
, kFlags2
, false);
381 experiments_list
= prefs_
.GetList(prefs::kEnabledLabsExperiments
);
382 EXPECT_TRUE(experiments_list
== NULL
|| experiments_list
->GetSize() == 0);
385 TEST_F(AboutFlagsTest
, ConvertFlagsToSwitches
) {
386 SetExperimentEnabled(&flags_storage_
, kFlags1
, true);
388 CommandLine
command_line(CommandLine::NO_PROGRAM
);
389 command_line
.AppendSwitch("foo");
391 EXPECT_TRUE(command_line
.HasSwitch("foo"));
392 EXPECT_FALSE(command_line
.HasSwitch(kSwitch1
));
394 ConvertFlagsToSwitches(&flags_storage_
, &command_line
, kAddSentinels
);
396 EXPECT_TRUE(command_line
.HasSwitch("foo"));
397 EXPECT_TRUE(command_line
.HasSwitch(kSwitch1
));
398 EXPECT_TRUE(command_line
.HasSwitch(switches::kFlagSwitchesBegin
));
399 EXPECT_TRUE(command_line
.HasSwitch(switches::kFlagSwitchesEnd
));
401 CommandLine
command_line2(CommandLine::NO_PROGRAM
);
403 ConvertFlagsToSwitches(&flags_storage_
, &command_line2
, kNoSentinels
);
405 EXPECT_TRUE(command_line2
.HasSwitch(kSwitch1
));
406 EXPECT_FALSE(command_line2
.HasSwitch(switches::kFlagSwitchesBegin
));
407 EXPECT_FALSE(command_line2
.HasSwitch(switches::kFlagSwitchesEnd
));
410 CommandLine::StringType
CreateSwitch(const std::string
& value
) {
412 return base::ASCIIToUTF16(value
);
418 TEST_F(AboutFlagsTest
, CompareSwitchesToCurrentCommandLine
) {
419 SetExperimentEnabled(&flags_storage_
, kFlags1
, true);
421 const std::string
kDoubleDash("--");
423 CommandLine
command_line(CommandLine::NO_PROGRAM
);
424 command_line
.AppendSwitch("foo");
426 CommandLine
new_command_line(CommandLine::NO_PROGRAM
);
427 ConvertFlagsToSwitches(&flags_storage_
, &new_command_line
, kAddSentinels
);
429 EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine(
430 new_command_line
, command_line
, NULL
));
432 std::set
<CommandLine::StringType
> difference
;
433 EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine(
434 new_command_line
, command_line
, &difference
));
435 EXPECT_EQ(1U, difference
.size());
436 EXPECT_EQ(1U, difference
.count(CreateSwitch(kDoubleDash
+ kSwitch1
)));
439 ConvertFlagsToSwitches(&flags_storage_
, &command_line
, kAddSentinels
);
441 EXPECT_TRUE(AreSwitchesIdenticalToCurrentCommandLine(
442 new_command_line
, command_line
, NULL
));
444 std::set
<CommandLine::StringType
> difference
;
445 EXPECT_TRUE(AreSwitchesIdenticalToCurrentCommandLine(
446 new_command_line
, command_line
, &difference
));
447 EXPECT_TRUE(difference
.empty());
450 // Now both have flags but different.
451 SetExperimentEnabled(&flags_storage_
, kFlags1
, false);
452 SetExperimentEnabled(&flags_storage_
, kFlags2
, true);
454 CommandLine
another_command_line(CommandLine::NO_PROGRAM
);
455 ConvertFlagsToSwitches(&flags_storage_
, &another_command_line
, kAddSentinels
);
457 EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine(
458 new_command_line
, another_command_line
, NULL
));
460 std::set
<CommandLine::StringType
> difference
;
461 EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine(
462 new_command_line
, another_command_line
, &difference
));
463 EXPECT_EQ(2U, difference
.size());
464 EXPECT_EQ(1U, difference
.count(CreateSwitch(kDoubleDash
+ kSwitch1
)));
466 difference
.count(CreateSwitch(kDoubleDash
+ kSwitch2
+ "=" +
471 TEST_F(AboutFlagsTest
, RemoveFlagSwitches
) {
472 std::map
<std::string
, CommandLine::StringType
> switch_list
;
473 switch_list
[kSwitch1
] = CommandLine::StringType();
474 switch_list
[switches::kFlagSwitchesBegin
] = CommandLine::StringType();
475 switch_list
[switches::kFlagSwitchesEnd
] = CommandLine::StringType();
476 switch_list
["foo"] = CommandLine::StringType();
478 SetExperimentEnabled(&flags_storage_
, kFlags1
, true);
480 // This shouldn't do anything before ConvertFlagsToSwitches() wasn't called.
481 RemoveFlagsSwitches(&switch_list
);
482 ASSERT_EQ(4u, switch_list
.size());
483 EXPECT_TRUE(switch_list
.find(kSwitch1
) != switch_list
.end());
484 EXPECT_TRUE(switch_list
.find(switches::kFlagSwitchesBegin
) !=
486 EXPECT_TRUE(switch_list
.find(switches::kFlagSwitchesEnd
) !=
488 EXPECT_TRUE(switch_list
.find("foo") != switch_list
.end());
490 // Call ConvertFlagsToSwitches(), then RemoveFlagsSwitches() again.
491 CommandLine
command_line(CommandLine::NO_PROGRAM
);
492 command_line
.AppendSwitch("foo");
493 ConvertFlagsToSwitches(&flags_storage_
, &command_line
, kAddSentinels
);
494 RemoveFlagsSwitches(&switch_list
);
496 // Now the about:flags-related switch should have been removed.
497 ASSERT_EQ(1u, switch_list
.size());
498 EXPECT_TRUE(switch_list
.find("foo") != switch_list
.end());
501 // Tests enabling experiments that aren't supported on the current platform.
502 TEST_F(AboutFlagsTest
, PersistAndPrune
) {
503 // Enable experiments 1 and 3.
504 SetExperimentEnabled(&flags_storage_
, kFlags1
, true);
505 SetExperimentEnabled(&flags_storage_
, kFlags3
, true);
506 CommandLine
command_line(CommandLine::NO_PROGRAM
);
507 EXPECT_FALSE(command_line
.HasSwitch(kSwitch1
));
508 EXPECT_FALSE(command_line
.HasSwitch(kSwitch3
));
510 // Convert the flags to switches. Experiment 3 shouldn't be among the switches
511 // as it is not applicable to the current platform.
512 ConvertFlagsToSwitches(&flags_storage_
, &command_line
, kAddSentinels
);
513 EXPECT_TRUE(command_line
.HasSwitch(kSwitch1
));
514 EXPECT_FALSE(command_line
.HasSwitch(kSwitch3
));
516 // Experiment 3 should show still be persisted in preferences though.
517 const base::ListValue
* experiments_list
=
518 prefs_
.GetList(prefs::kEnabledLabsExperiments
);
519 ASSERT_TRUE(experiments_list
);
520 EXPECT_EQ(2U, experiments_list
->GetSize());
522 ASSERT_TRUE(experiments_list
->GetString(0, &s0
));
523 EXPECT_EQ(kFlags1
, s0
);
525 ASSERT_TRUE(experiments_list
->GetString(1, &s1
));
526 EXPECT_EQ(kFlags3
, s1
);
529 // Tests that switches which should have values get them in the command
531 TEST_F(AboutFlagsTest
, CheckValues
) {
532 // Enable experiments 1 and 2.
533 SetExperimentEnabled(&flags_storage_
, kFlags1
, true);
534 SetExperimentEnabled(&flags_storage_
, kFlags2
, true);
535 CommandLine
command_line(CommandLine::NO_PROGRAM
);
536 EXPECT_FALSE(command_line
.HasSwitch(kSwitch1
));
537 EXPECT_FALSE(command_line
.HasSwitch(kSwitch2
));
539 // Convert the flags to switches.
540 ConvertFlagsToSwitches(&flags_storage_
, &command_line
, kAddSentinels
);
541 EXPECT_TRUE(command_line
.HasSwitch(kSwitch1
));
542 EXPECT_EQ(std::string(), command_line
.GetSwitchValueASCII(kSwitch1
));
543 EXPECT_TRUE(command_line
.HasSwitch(kSwitch2
));
544 EXPECT_EQ(std::string(kValueForSwitch2
),
545 command_line
.GetSwitchValueASCII(kSwitch2
));
547 // Confirm that there is no '=' in the command line for simple switches.
548 std::string switch1_with_equals
= std::string("--") +
549 std::string(kSwitch1
) +
552 EXPECT_EQ(std::wstring::npos
,
553 command_line
.GetCommandLineString().find(
554 base::ASCIIToWide(switch1_with_equals
)));
556 EXPECT_EQ(std::string::npos
,
557 command_line
.GetCommandLineString().find(switch1_with_equals
));
560 // And confirm there is a '=' for switches with values.
561 std::string switch2_with_equals
= std::string("--") +
562 std::string(kSwitch2
) +
565 EXPECT_NE(std::wstring::npos
,
566 command_line
.GetCommandLineString().find(
567 base::ASCIIToWide(switch2_with_equals
)));
569 EXPECT_NE(std::string::npos
,
570 command_line
.GetCommandLineString().find(switch2_with_equals
));
573 // And it should persist.
574 const base::ListValue
* experiments_list
=
575 prefs_
.GetList(prefs::kEnabledLabsExperiments
);
576 ASSERT_TRUE(experiments_list
);
577 EXPECT_EQ(2U, experiments_list
->GetSize());
579 ASSERT_TRUE(experiments_list
->GetString(0, &s0
));
580 EXPECT_EQ(kFlags1
, s0
);
582 ASSERT_TRUE(experiments_list
->GetString(1, &s1
));
583 EXPECT_EQ(kFlags2
, s1
);
586 // Tests multi-value type experiments.
587 TEST_F(AboutFlagsTest
, MultiValues
) {
588 const Experiment
& experiment
= kExperiments
[3];
589 ASSERT_EQ(kFlags4
, experiment
.internal_name
);
591 // Initially, the first "deactivated" option of the multi experiment should
594 CommandLine
command_line(CommandLine::NO_PROGRAM
);
595 ConvertFlagsToSwitches(&flags_storage_
, &command_line
, kAddSentinels
);
596 EXPECT_FALSE(command_line
.HasSwitch(kMultiSwitch1
));
597 EXPECT_FALSE(command_line
.HasSwitch(kMultiSwitch2
));
600 // Enable the 2nd choice of the multi-value.
601 SetExperimentEnabled(&flags_storage_
, experiment
.NameForChoice(2), true);
603 CommandLine
command_line(CommandLine::NO_PROGRAM
);
604 ConvertFlagsToSwitches(&flags_storage_
, &command_line
, kAddSentinels
);
605 EXPECT_FALSE(command_line
.HasSwitch(kMultiSwitch1
));
606 EXPECT_TRUE(command_line
.HasSwitch(kMultiSwitch2
));
607 EXPECT_EQ(std::string(kValueForMultiSwitch2
),
608 command_line
.GetSwitchValueASCII(kMultiSwitch2
));
611 // Disable the multi-value experiment.
612 SetExperimentEnabled(&flags_storage_
, experiment
.NameForChoice(0), true);
614 CommandLine
command_line(CommandLine::NO_PROGRAM
);
615 ConvertFlagsToSwitches(&flags_storage_
, &command_line
, kAddSentinels
);
616 EXPECT_FALSE(command_line
.HasSwitch(kMultiSwitch1
));
617 EXPECT_FALSE(command_line
.HasSwitch(kMultiSwitch2
));
621 TEST_F(AboutFlagsTest
, EnableDisableValues
) {
622 const Experiment
& experiment
= kExperiments
[4];
623 ASSERT_EQ(kFlags5
, experiment
.internal_name
);
627 CommandLine
command_line(CommandLine::NO_PROGRAM
);
628 ConvertFlagsToSwitches(&flags_storage_
, &command_line
, kAddSentinels
);
629 EXPECT_FALSE(command_line
.HasSwitch(kSwitch1
));
630 EXPECT_FALSE(command_line
.HasSwitch(kSwitch2
));
633 // "Enable" option selected.
634 SetExperimentEnabled(&flags_storage_
, experiment
.NameForChoice(1), true);
636 CommandLine
command_line(CommandLine::NO_PROGRAM
);
637 ConvertFlagsToSwitches(&flags_storage_
, &command_line
, kAddSentinels
);
638 EXPECT_TRUE(command_line
.HasSwitch(kSwitch1
));
639 EXPECT_FALSE(command_line
.HasSwitch(kSwitch2
));
640 EXPECT_EQ(kEnableDisableValue1
, command_line
.GetSwitchValueASCII(kSwitch1
));
643 // "Disable" option selected.
644 SetExperimentEnabled(&flags_storage_
, experiment
.NameForChoice(2), true);
646 CommandLine
command_line(CommandLine::NO_PROGRAM
);
647 ConvertFlagsToSwitches(&flags_storage_
, &command_line
, kAddSentinels
);
648 EXPECT_FALSE(command_line
.HasSwitch(kSwitch1
));
649 EXPECT_TRUE(command_line
.HasSwitch(kSwitch2
));
650 EXPECT_EQ(kEnableDisableValue2
, command_line
.GetSwitchValueASCII(kSwitch2
));
653 // "Default" option selected, same as nothing selected.
654 SetExperimentEnabled(&flags_storage_
, experiment
.NameForChoice(0), true);
656 CommandLine
command_line(CommandLine::NO_PROGRAM
);
657 ConvertFlagsToSwitches(&flags_storage_
, &command_line
, kAddSentinels
);
658 EXPECT_FALSE(command_line
.HasSwitch(kMultiSwitch1
));
659 EXPECT_FALSE(command_line
.HasSwitch(kMultiSwitch2
));
663 // Makes sure there are no separators in any of the experiment names.
664 TEST_F(AboutFlagsTest
, NoSeparators
) {
665 testing::SetExperiments(NULL
, 0);
667 const Experiment
* experiments
= testing::GetExperiments(&count
);
668 for (size_t i
= 0; i
< count
; ++i
) {
669 std::string name
= experiments
->internal_name
;
670 EXPECT_EQ(std::string::npos
, name
.find(testing::kMultiSeparator
)) << i
;
674 class AboutFlagsHistogramTest
: public ::testing::Test
{
676 // This is a helper function to check that all IDs in enum LoginCustomFlags in
677 // histograms.xml are unique.
678 void SetSwitchToHistogramIdMapping(const std::string
& switch_name
,
679 const uint32_t switch_histogram_id
,
680 std::map
<std::string
, uint32_t>* out_map
) {
681 const std::pair
<std::map
<std::string
, uint32_t>::iterator
, bool> status
=
682 out_map
->insert(std::make_pair(switch_name
, switch_histogram_id
));
683 if (!status
.second
) {
684 EXPECT_TRUE(status
.first
->second
== switch_histogram_id
)
685 << "Duplicate switch '" << switch_name
686 << "' found in enum 'LoginCustomFlags' in histograms.xml.";
690 // This method generates a hint for the user for what string should be added
691 // to the enum LoginCustomFlags to make in consistent.
692 std::string
GetHistogramEnumEntryText(const std::string
& switch_name
,
694 return base::StringPrintf(
695 "<int value=\"%u\" label=\"%s\"/>", value
, switch_name
.c_str());
699 TEST_F(AboutFlagsHistogramTest
, CheckHistograms
) {
700 base::FilePath histograms_xml_file_path
;
702 PathService::Get(base::DIR_SOURCE_ROOT
, &histograms_xml_file_path
));
703 histograms_xml_file_path
= histograms_xml_file_path
.AppendASCII("tools")
704 .AppendASCII("metrics")
705 .AppendASCII("histograms")
706 .AppendASCII("histograms.xml");
708 XmlReader histograms_xml
;
709 ASSERT_TRUE(histograms_xml
.LoadFile(
710 FilePathStringTypeToString(histograms_xml_file_path
.value())));
711 std::map
<uint32_t, std::string
> login_custom_flags
=
712 ReadEnumFromHistogramsXml("LoginCustomFlags", &histograms_xml
);
713 ASSERT_TRUE(login_custom_flags
.size())
714 << "Error reading enum 'LoginCustomFlags' from histograms.xml.";
716 // Build reverse map {switch_name => id} from login_custom_flags.
717 SwitchToIdMap histograms_xml_switches_ids
;
719 EXPECT_TRUE(login_custom_flags
.count(kBadSwitchFormatHistogramId
))
720 << "Entry for UMA ID of incorrect command-line flag is not found in "
721 "histograms.xml enum LoginCustomFlags. "
722 "Consider adding entry:\n"
723 << " " << GetHistogramEnumEntryText("BAD_FLAG_FORMAT", 0);
724 // Check that all LoginCustomFlags entries have correct values.
725 for (std::map
<uint32_t, std::string
>::const_iterator it
=
726 login_custom_flags
.begin();
727 it
!= login_custom_flags
.end();
729 if (it
->first
== kBadSwitchFormatHistogramId
) {
730 // Add eror value with empty name.
731 SetSwitchToHistogramIdMapping(
732 "", it
->first
, &histograms_xml_switches_ids
);
735 const uint32_t uma_id
= GetSwitchUMAId(it
->second
);
736 EXPECT_EQ(uma_id
, it
->first
)
737 << "histograms.xml enum LoginCustomFlags "
738 "entry '" << it
->second
<< "' has incorrect value=" << it
->first
739 << ", but " << uma_id
<< " is expected. Consider changing entry to:\n"
740 << " " << GetHistogramEnumEntryText(it
->second
, uma_id
);
741 SetSwitchToHistogramIdMapping(
742 it
->second
, it
->first
, &histograms_xml_switches_ids
);
745 // Check that all flags in about_flags.cc have entries in login_custom_flags.
746 std::set
<std::string
> all_switches
= GetAllSwitchesForTesting();
747 for (std::set
<std::string
>::const_iterator it
= all_switches
.begin();
748 it
!= all_switches
.end();
750 // Skip empty placeholders.
753 const uint32_t uma_id
= GetSwitchUMAId(*it
);
754 EXPECT_NE(kBadSwitchFormatHistogramId
, uma_id
)
755 << "Command-line switch '" << *it
756 << "' from about_flags.cc has UMA ID equal to reserved value "
757 "kBadSwitchFormatHistogramId=" << kBadSwitchFormatHistogramId
758 << ". Please modify switch name.";
759 SwitchToIdMap::iterator enum_entry
=
760 histograms_xml_switches_ids
.lower_bound(*it
);
762 // Ignore case here when switch ID is incorrect - it has already been
763 // reported in the previous loop.
764 EXPECT_TRUE(enum_entry
!= histograms_xml_switches_ids
.end() &&
765 enum_entry
->first
== *it
)
766 << "histograms.xml enum LoginCustomFlags doesn't contain switch '"
767 << *it
<< "' (value=" << uma_id
768 << " expected). Consider adding entry:\n"
769 << " " << GetHistogramEnumEntryText(*it
, uma_id
);
773 } // namespace about_flags