android-webview: Remove legacy crash handler (### Version)
[chromium-blink-merge.git] / chrome / browser / about_flags_unittest.cc
blobf14651d27fecf310d6802943fbff47e64adc4572
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.
5 #include "chrome/browser/about_flags.h"
7 #include <stdint.h>
8 #include <utility>
10 #include "base/files/file_path.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/path_service.h"
13 #include "base/prefs/pref_registry_simple.h"
14 #include "base/prefs/testing_pref_service.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/values.h"
19 #include "chrome/browser/pref_service_flags_storage.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/pref_names.h"
22 #include "chrome/grit/chromium_strings.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "third_party/libxml/chromium/libxml_utils.h"
26 namespace about_flags {
28 namespace {
30 const char kFlags1[] = "flag1";
31 const char kFlags2[] = "flag2";
32 const char kFlags3[] = "flag3";
33 const char kFlags4[] = "flag4";
34 const char kFlags5[] = "flag5";
36 const char kSwitch1[] = "switch";
37 const char kSwitch2[] = "switch2";
38 const char kSwitch3[] = "switch3";
39 const char kValueForSwitch2[] = "value_for_switch2";
41 const char kMultiSwitch1[] = "multi_switch1";
42 const char kMultiSwitch2[] = "multi_switch2";
43 const char kValueForMultiSwitch2[] = "value_for_multi_switch2";
45 const char kEnableDisableValue1[] = "value1";
46 const char kEnableDisableValue2[] = "value2";
48 typedef base::HistogramBase::Sample Sample;
49 typedef std::map<std::string, Sample> SwitchToIdMap;
51 // This is a helper function to the ReadEnumFromHistogramsXml().
52 // Extracts single enum (with integer values) from histograms.xml.
53 // Expects |reader| to point at given enum.
54 // Returns map { value => label }.
55 // Returns empty map on error.
56 std::map<Sample, std::string> ParseEnumFromHistogramsXml(
57 const std::string& enum_name,
58 XmlReader* reader) {
59 int entries_index = -1;
61 std::map<Sample, std::string> result;
62 bool success = true;
64 while (true) {
65 const std::string node_name = reader->NodeName();
66 if (node_name == "enum" && reader->IsClosingElement())
67 break;
69 if (node_name == "int") {
70 ++entries_index;
71 std::string value_str;
72 std::string label;
73 const bool has_value = reader->NodeAttribute("value", &value_str);
74 const bool has_label = reader->NodeAttribute("label", &label);
75 if (!has_value) {
76 ADD_FAILURE() << "Bad " << enum_name << " enum entry (at index "
77 << entries_index << ", label='" << label
78 << "'): No 'value' attribute.";
79 success = false;
81 if (!has_label) {
82 ADD_FAILURE() << "Bad " << enum_name << " enum entry (at index "
83 << entries_index << ", value_str='" << value_str
84 << "'): No 'label' attribute.";
85 success = false;
88 Sample value;
89 if (has_value && !base::StringToInt(value_str, &value)) {
90 ADD_FAILURE() << "Bad " << enum_name << " enum entry (at index "
91 << entries_index << ", label='" << label
92 << "', value_str='" << value_str
93 << "'): 'value' attribute is not integer.";
94 success = false;
96 if (result.count(value)) {
97 ADD_FAILURE() << "Bad " << enum_name << " enum entry (at index "
98 << entries_index << ", label='" << label
99 << "', value_str='" << value_str
100 << "'): duplicate value '" << value_str
101 << "' found in enum. The previous one has label='"
102 << result[value] << "'.";
103 success = false;
105 if (success) {
106 result[value] = label;
109 // All enum entries are on the same level, so it is enough to iterate
110 // until possible.
111 reader->Next();
113 return (success ? result : std::map<Sample, std::string>());
116 // Find and read given enum (with integer values) from histograms.xml.
117 // |enum_name| - enum name.
118 // |histograms_xml| - must be loaded histograms.xml file.
120 // Returns map { value => label } so that:
121 // <int value="9" label="enable-pinch-virtual-viewport"/>
122 // becomes:
123 // { 9 => "enable-pinch-virtual-viewport" }
124 // Returns empty map on error.
125 std::map<Sample, std::string> ReadEnumFromHistogramsXml(
126 const std::string& enum_name,
127 XmlReader* histograms_xml) {
128 std::map<Sample, std::string> login_custom_flags;
130 // Implement simple depth first search.
131 while (true) {
132 const std::string node_name = histograms_xml->NodeName();
133 if (node_name == "enum") {
134 std::string name;
135 if (histograms_xml->NodeAttribute("name", &name) && name == enum_name) {
136 if (!login_custom_flags.empty()) {
137 EXPECT_TRUE(login_custom_flags.empty())
138 << "Duplicate enum '" << enum_name << "' found in histograms.xml";
139 return std::map<Sample, std::string>();
142 const bool got_into_enum = histograms_xml->Read();
143 if (got_into_enum) {
144 login_custom_flags =
145 ParseEnumFromHistogramsXml(enum_name, histograms_xml);
146 EXPECT_FALSE(login_custom_flags.empty())
147 << "Bad enum '" << enum_name
148 << "' found in histograms.xml (format error).";
149 } else {
150 EXPECT_TRUE(got_into_enum)
151 << "Bad enum '" << enum_name
152 << "' (looks empty) found in histograms.xml.";
154 if (login_custom_flags.empty())
155 return std::map<Sample, std::string>();
158 // Go deeper if possible (stops at the closing tag of the deepest node).
159 if (histograms_xml->Read())
160 continue;
162 // Try next node on the same level (skips closing tag).
163 if (histograms_xml->Next())
164 continue;
166 // Go up until next node on the same level exists.
167 while (histograms_xml->Depth() && !histograms_xml->SkipToElement()) {
170 // Reached top. histograms.xml consists of the single top level node
171 // 'histogram-configuration', so this is the end.
172 if (!histograms_xml->Depth())
173 break;
175 EXPECT_FALSE(login_custom_flags.empty())
176 << "Enum '" << enum_name << "' is not found in histograms.xml.";
177 return login_custom_flags;
180 std::string FilePathStringTypeToString(const base::FilePath::StringType& path) {
181 #if defined(OS_WIN)
182 return base::UTF16ToUTF8(path);
183 #else
184 return path;
185 #endif
188 std::set<std::string> GetAllSwitchesForTesting() {
189 std::set<std::string> result;
191 size_t num_experiments = 0;
192 const Experiment* experiments =
193 testing::GetExperiments(&num_experiments);
195 for (size_t i = 0; i < num_experiments; ++i) {
196 const Experiment& experiment = experiments[i];
197 if (experiment.type == Experiment::SINGLE_VALUE) {
198 result.insert(experiment.command_line_switch);
199 } else if (experiment.type == Experiment::MULTI_VALUE) {
200 for (int j = 0; j < experiment.num_choices; ++j) {
201 result.insert(experiment.choices[j].command_line_switch);
203 } else {
204 DCHECK_EQ(experiment.type, Experiment::ENABLE_DISABLE_VALUE);
205 result.insert(experiment.command_line_switch);
206 result.insert(experiment.disable_command_line_switch);
209 return result;
212 } // anonymous namespace
214 const Experiment::Choice kMultiChoices[] = {
215 { IDS_PRODUCT_NAME, "", "" },
216 { IDS_PRODUCT_NAME, kMultiSwitch1, "" },
217 { IDS_PRODUCT_NAME, kMultiSwitch2, kValueForMultiSwitch2 },
220 // The experiments that are set for these tests. The 3rd experiment is not
221 // supported on the current platform, all others are.
222 static Experiment kExperiments[] = {
224 kFlags1,
225 IDS_PRODUCT_NAME,
226 IDS_PRODUCT_NAME,
227 0, // Ends up being mapped to the current platform.
228 Experiment::SINGLE_VALUE,
229 kSwitch1,
231 NULL,
232 NULL,
233 NULL,
237 kFlags2,
238 IDS_PRODUCT_NAME,
239 IDS_PRODUCT_NAME,
240 0, // Ends up being mapped to the current platform.
241 Experiment::SINGLE_VALUE,
242 kSwitch2,
243 kValueForSwitch2,
244 NULL,
245 NULL,
246 NULL,
250 kFlags3,
251 IDS_PRODUCT_NAME,
252 IDS_PRODUCT_NAME,
253 0, // This ends up enabling for an OS other than the current.
254 Experiment::SINGLE_VALUE,
255 kSwitch3,
257 NULL,
258 NULL,
259 NULL,
263 kFlags4,
264 IDS_PRODUCT_NAME,
265 IDS_PRODUCT_NAME,
266 0, // Ends up being mapped to the current platform.
267 Experiment::MULTI_VALUE,
272 kMultiChoices,
273 arraysize(kMultiChoices)
276 kFlags5,
277 IDS_PRODUCT_NAME,
278 IDS_PRODUCT_NAME,
279 0, // Ends up being mapped to the current platform.
280 Experiment::ENABLE_DISABLE_VALUE,
281 kSwitch1,
282 kEnableDisableValue1,
283 kSwitch2,
284 kEnableDisableValue2,
285 NULL,
290 class AboutFlagsTest : public ::testing::Test {
291 protected:
292 AboutFlagsTest() : flags_storage_(&prefs_) {
293 prefs_.registry()->RegisterListPref(prefs::kEnabledLabsExperiments);
294 testing::ClearState();
297 void SetUp() override {
298 for (size_t i = 0; i < arraysize(kExperiments); ++i)
299 kExperiments[i].supported_platforms = GetCurrentPlatform();
301 int os_other_than_current = 1;
302 while (os_other_than_current == GetCurrentPlatform())
303 os_other_than_current <<= 1;
304 kExperiments[2].supported_platforms = os_other_than_current;
306 testing::SetExperiments(kExperiments, arraysize(kExperiments));
309 void TearDown() override { testing::SetExperiments(NULL, 0); }
311 TestingPrefServiceSimple prefs_;
312 PrefServiceFlagsStorage flags_storage_;
316 TEST_F(AboutFlagsTest, NoChangeNoRestart) {
317 EXPECT_FALSE(IsRestartNeededToCommitChanges());
318 SetExperimentEnabled(&flags_storage_, kFlags1, false);
319 EXPECT_FALSE(IsRestartNeededToCommitChanges());
322 TEST_F(AboutFlagsTest, ChangeNeedsRestart) {
323 EXPECT_FALSE(IsRestartNeededToCommitChanges());
324 SetExperimentEnabled(&flags_storage_, kFlags1, true);
325 EXPECT_TRUE(IsRestartNeededToCommitChanges());
328 TEST_F(AboutFlagsTest, MultiFlagChangeNeedsRestart) {
329 const Experiment& experiment = kExperiments[3];
330 ASSERT_EQ(kFlags4, experiment.internal_name);
331 EXPECT_FALSE(IsRestartNeededToCommitChanges());
332 // Enable the 2nd choice of the multi-value.
333 SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(2), true);
334 EXPECT_TRUE(IsRestartNeededToCommitChanges());
335 testing::ClearState();
336 EXPECT_FALSE(IsRestartNeededToCommitChanges());
337 // Enable the default choice now.
338 SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(0), true);
339 EXPECT_TRUE(IsRestartNeededToCommitChanges());
342 TEST_F(AboutFlagsTest, AddTwoFlagsRemoveOne) {
343 // Add two experiments, check they're there.
344 SetExperimentEnabled(&flags_storage_, kFlags1, true);
345 SetExperimentEnabled(&flags_storage_, kFlags2, true);
347 const base::ListValue* experiments_list = prefs_.GetList(
348 prefs::kEnabledLabsExperiments);
349 ASSERT_TRUE(experiments_list != NULL);
351 ASSERT_EQ(2u, experiments_list->GetSize());
353 std::string s0;
354 ASSERT_TRUE(experiments_list->GetString(0, &s0));
355 std::string s1;
356 ASSERT_TRUE(experiments_list->GetString(1, &s1));
358 EXPECT_TRUE(s0 == kFlags1 || s1 == kFlags1);
359 EXPECT_TRUE(s0 == kFlags2 || s1 == kFlags2);
361 // Remove one experiment, check the other's still around.
362 SetExperimentEnabled(&flags_storage_, kFlags2, false);
364 experiments_list = prefs_.GetList(prefs::kEnabledLabsExperiments);
365 ASSERT_TRUE(experiments_list != NULL);
366 ASSERT_EQ(1u, experiments_list->GetSize());
367 ASSERT_TRUE(experiments_list->GetString(0, &s0));
368 EXPECT_TRUE(s0 == kFlags1);
371 TEST_F(AboutFlagsTest, AddTwoFlagsRemoveBoth) {
372 // Add two experiments, check the pref exists.
373 SetExperimentEnabled(&flags_storage_, kFlags1, true);
374 SetExperimentEnabled(&flags_storage_, kFlags2, true);
375 const base::ListValue* experiments_list = prefs_.GetList(
376 prefs::kEnabledLabsExperiments);
377 ASSERT_TRUE(experiments_list != NULL);
379 // Remove both, the pref should have been removed completely.
380 SetExperimentEnabled(&flags_storage_, kFlags1, false);
381 SetExperimentEnabled(&flags_storage_, kFlags2, false);
382 experiments_list = prefs_.GetList(prefs::kEnabledLabsExperiments);
383 EXPECT_TRUE(experiments_list == NULL || experiments_list->GetSize() == 0);
386 TEST_F(AboutFlagsTest, ConvertFlagsToSwitches) {
387 SetExperimentEnabled(&flags_storage_, kFlags1, true);
389 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
390 command_line.AppendSwitch("foo");
392 EXPECT_TRUE(command_line.HasSwitch("foo"));
393 EXPECT_FALSE(command_line.HasSwitch(kSwitch1));
395 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
397 EXPECT_TRUE(command_line.HasSwitch("foo"));
398 EXPECT_TRUE(command_line.HasSwitch(kSwitch1));
399 EXPECT_TRUE(command_line.HasSwitch(switches::kFlagSwitchesBegin));
400 EXPECT_TRUE(command_line.HasSwitch(switches::kFlagSwitchesEnd));
402 base::CommandLine command_line2(base::CommandLine::NO_PROGRAM);
404 ConvertFlagsToSwitches(&flags_storage_, &command_line2, kNoSentinels);
406 EXPECT_TRUE(command_line2.HasSwitch(kSwitch1));
407 EXPECT_FALSE(command_line2.HasSwitch(switches::kFlagSwitchesBegin));
408 EXPECT_FALSE(command_line2.HasSwitch(switches::kFlagSwitchesEnd));
411 base::CommandLine::StringType CreateSwitch(const std::string& value) {
412 #if defined(OS_WIN)
413 return base::ASCIIToUTF16(value);
414 #else
415 return value;
416 #endif
419 TEST_F(AboutFlagsTest, CompareSwitchesToCurrentCommandLine) {
420 SetExperimentEnabled(&flags_storage_, kFlags1, true);
422 const std::string kDoubleDash("--");
424 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
425 command_line.AppendSwitch("foo");
427 base::CommandLine new_command_line(base::CommandLine::NO_PROGRAM);
428 ConvertFlagsToSwitches(&flags_storage_, &new_command_line, kAddSentinels);
430 EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine(
431 new_command_line, command_line, NULL));
433 std::set<base::CommandLine::StringType> difference;
434 EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine(
435 new_command_line, command_line, &difference));
436 EXPECT_EQ(1U, difference.size());
437 EXPECT_EQ(1U, difference.count(CreateSwitch(kDoubleDash + kSwitch1)));
440 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
442 EXPECT_TRUE(AreSwitchesIdenticalToCurrentCommandLine(
443 new_command_line, command_line, NULL));
445 std::set<base::CommandLine::StringType> difference;
446 EXPECT_TRUE(AreSwitchesIdenticalToCurrentCommandLine(
447 new_command_line, command_line, &difference));
448 EXPECT_TRUE(difference.empty());
451 // Now both have flags but different.
452 SetExperimentEnabled(&flags_storage_, kFlags1, false);
453 SetExperimentEnabled(&flags_storage_, kFlags2, true);
455 base::CommandLine another_command_line(base::CommandLine::NO_PROGRAM);
456 ConvertFlagsToSwitches(&flags_storage_, &another_command_line, kAddSentinels);
458 EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine(
459 new_command_line, another_command_line, NULL));
461 std::set<base::CommandLine::StringType> difference;
462 EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine(
463 new_command_line, another_command_line, &difference));
464 EXPECT_EQ(2U, difference.size());
465 EXPECT_EQ(1U, difference.count(CreateSwitch(kDoubleDash + kSwitch1)));
466 EXPECT_EQ(1U,
467 difference.count(CreateSwitch(kDoubleDash + kSwitch2 + "=" +
468 kValueForSwitch2)));
472 TEST_F(AboutFlagsTest, RemoveFlagSwitches) {
473 std::map<std::string, base::CommandLine::StringType> switch_list;
474 switch_list[kSwitch1] = base::CommandLine::StringType();
475 switch_list[switches::kFlagSwitchesBegin] = base::CommandLine::StringType();
476 switch_list[switches::kFlagSwitchesEnd] = base::CommandLine::StringType();
477 switch_list["foo"] = base::CommandLine::StringType();
479 SetExperimentEnabled(&flags_storage_, kFlags1, true);
481 // This shouldn't do anything before ConvertFlagsToSwitches() wasn't called.
482 RemoveFlagsSwitches(&switch_list);
483 ASSERT_EQ(4u, switch_list.size());
484 EXPECT_TRUE(switch_list.find(kSwitch1) != switch_list.end());
485 EXPECT_TRUE(switch_list.find(switches::kFlagSwitchesBegin) !=
486 switch_list.end());
487 EXPECT_TRUE(switch_list.find(switches::kFlagSwitchesEnd) !=
488 switch_list.end());
489 EXPECT_TRUE(switch_list.find("foo") != switch_list.end());
491 // Call ConvertFlagsToSwitches(), then RemoveFlagsSwitches() again.
492 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
493 command_line.AppendSwitch("foo");
494 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
495 RemoveFlagsSwitches(&switch_list);
497 // Now the about:flags-related switch should have been removed.
498 ASSERT_EQ(1u, switch_list.size());
499 EXPECT_TRUE(switch_list.find("foo") != switch_list.end());
502 // Tests enabling experiments that aren't supported on the current platform.
503 TEST_F(AboutFlagsTest, PersistAndPrune) {
504 // Enable experiments 1 and 3.
505 SetExperimentEnabled(&flags_storage_, kFlags1, true);
506 SetExperimentEnabled(&flags_storage_, kFlags3, true);
507 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
508 EXPECT_FALSE(command_line.HasSwitch(kSwitch1));
509 EXPECT_FALSE(command_line.HasSwitch(kSwitch3));
511 // Convert the flags to switches. Experiment 3 shouldn't be among the switches
512 // as it is not applicable to the current platform.
513 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
514 EXPECT_TRUE(command_line.HasSwitch(kSwitch1));
515 EXPECT_FALSE(command_line.HasSwitch(kSwitch3));
517 // Experiment 3 should show still be persisted in preferences though.
518 const base::ListValue* experiments_list =
519 prefs_.GetList(prefs::kEnabledLabsExperiments);
520 ASSERT_TRUE(experiments_list);
521 EXPECT_EQ(2U, experiments_list->GetSize());
522 std::string s0;
523 ASSERT_TRUE(experiments_list->GetString(0, &s0));
524 EXPECT_EQ(kFlags1, s0);
525 std::string s1;
526 ASSERT_TRUE(experiments_list->GetString(1, &s1));
527 EXPECT_EQ(kFlags3, s1);
530 // Tests that switches which should have values get them in the command
531 // line.
532 TEST_F(AboutFlagsTest, CheckValues) {
533 // Enable experiments 1 and 2.
534 SetExperimentEnabled(&flags_storage_, kFlags1, true);
535 SetExperimentEnabled(&flags_storage_, kFlags2, true);
536 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
537 EXPECT_FALSE(command_line.HasSwitch(kSwitch1));
538 EXPECT_FALSE(command_line.HasSwitch(kSwitch2));
540 // Convert the flags to switches.
541 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
542 EXPECT_TRUE(command_line.HasSwitch(kSwitch1));
543 EXPECT_EQ(std::string(), command_line.GetSwitchValueASCII(kSwitch1));
544 EXPECT_TRUE(command_line.HasSwitch(kSwitch2));
545 EXPECT_EQ(std::string(kValueForSwitch2),
546 command_line.GetSwitchValueASCII(kSwitch2));
548 // Confirm that there is no '=' in the command line for simple switches.
549 std::string switch1_with_equals = std::string("--") +
550 std::string(kSwitch1) +
551 std::string("=");
552 #if defined(OS_WIN)
553 EXPECT_EQ(base::string16::npos,
554 command_line.GetCommandLineString().find(
555 base::ASCIIToUTF16(switch1_with_equals)));
556 #else
557 EXPECT_EQ(std::string::npos,
558 command_line.GetCommandLineString().find(switch1_with_equals));
559 #endif
561 // And confirm there is a '=' for switches with values.
562 std::string switch2_with_equals = std::string("--") +
563 std::string(kSwitch2) +
564 std::string("=");
565 #if defined(OS_WIN)
566 EXPECT_NE(base::string16::npos,
567 command_line.GetCommandLineString().find(
568 base::ASCIIToUTF16(switch2_with_equals)));
569 #else
570 EXPECT_NE(std::string::npos,
571 command_line.GetCommandLineString().find(switch2_with_equals));
572 #endif
574 // And it should persist.
575 const base::ListValue* experiments_list =
576 prefs_.GetList(prefs::kEnabledLabsExperiments);
577 ASSERT_TRUE(experiments_list);
578 EXPECT_EQ(2U, experiments_list->GetSize());
579 std::string s0;
580 ASSERT_TRUE(experiments_list->GetString(0, &s0));
581 EXPECT_EQ(kFlags1, s0);
582 std::string s1;
583 ASSERT_TRUE(experiments_list->GetString(1, &s1));
584 EXPECT_EQ(kFlags2, s1);
587 // Tests multi-value type experiments.
588 TEST_F(AboutFlagsTest, MultiValues) {
589 const Experiment& experiment = kExperiments[3];
590 ASSERT_EQ(kFlags4, experiment.internal_name);
592 // Initially, the first "deactivated" option of the multi experiment should
593 // be set.
595 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
596 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
597 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1));
598 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2));
601 // Enable the 2nd choice of the multi-value.
602 SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(2), true);
604 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
605 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
606 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1));
607 EXPECT_TRUE(command_line.HasSwitch(kMultiSwitch2));
608 EXPECT_EQ(std::string(kValueForMultiSwitch2),
609 command_line.GetSwitchValueASCII(kMultiSwitch2));
612 // Disable the multi-value experiment.
613 SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(0), true);
615 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
616 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
617 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1));
618 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2));
622 TEST_F(AboutFlagsTest, EnableDisableValues) {
623 const Experiment& experiment = kExperiments[4];
624 ASSERT_EQ(kFlags5, experiment.internal_name);
626 // Nothing selected.
628 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
629 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
630 EXPECT_FALSE(command_line.HasSwitch(kSwitch1));
631 EXPECT_FALSE(command_line.HasSwitch(kSwitch2));
634 // "Enable" option selected.
635 SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(1), true);
637 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
638 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
639 EXPECT_TRUE(command_line.HasSwitch(kSwitch1));
640 EXPECT_FALSE(command_line.HasSwitch(kSwitch2));
641 EXPECT_EQ(kEnableDisableValue1, command_line.GetSwitchValueASCII(kSwitch1));
644 // "Disable" option selected.
645 SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(2), true);
647 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
648 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
649 EXPECT_FALSE(command_line.HasSwitch(kSwitch1));
650 EXPECT_TRUE(command_line.HasSwitch(kSwitch2));
651 EXPECT_EQ(kEnableDisableValue2, command_line.GetSwitchValueASCII(kSwitch2));
654 // "Default" option selected, same as nothing selected.
655 SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(0), true);
657 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
658 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
659 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1));
660 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2));
664 // Makes sure there are no separators in any of the experiment names.
665 TEST_F(AboutFlagsTest, NoSeparators) {
666 testing::SetExperiments(NULL, 0);
667 size_t count;
668 const Experiment* experiments = testing::GetExperiments(&count);
669 for (size_t i = 0; i < count; ++i) {
670 std::string name = experiments->internal_name;
671 EXPECT_EQ(std::string::npos, name.find(testing::kMultiSeparator)) << i;
675 class AboutFlagsHistogramTest : public ::testing::Test {
676 protected:
677 // This is a helper function to check that all IDs in enum LoginCustomFlags in
678 // histograms.xml are unique.
679 void SetSwitchToHistogramIdMapping(const std::string& switch_name,
680 const Sample switch_histogram_id,
681 std::map<std::string, Sample>* out_map) {
682 const std::pair<std::map<std::string, Sample>::iterator, bool> status =
683 out_map->insert(std::make_pair(switch_name, switch_histogram_id));
684 if (!status.second) {
685 EXPECT_TRUE(status.first->second == switch_histogram_id)
686 << "Duplicate switch '" << switch_name
687 << "' found in enum 'LoginCustomFlags' in histograms.xml.";
691 // This method generates a hint for the user for what string should be added
692 // to the enum LoginCustomFlags to make in consistent.
693 std::string GetHistogramEnumEntryText(const std::string& switch_name,
694 Sample value) {
695 return base::StringPrintf(
696 "<int value=\"%d\" label=\"%s\"/>", value, switch_name.c_str());
700 TEST_F(AboutFlagsHistogramTest, CheckHistograms) {
701 base::FilePath histograms_xml_file_path;
702 ASSERT_TRUE(
703 PathService::Get(base::DIR_SOURCE_ROOT, &histograms_xml_file_path));
704 histograms_xml_file_path = histograms_xml_file_path.AppendASCII("tools")
705 .AppendASCII("metrics")
706 .AppendASCII("histograms")
707 .AppendASCII("histograms.xml");
709 XmlReader histograms_xml;
710 ASSERT_TRUE(histograms_xml.LoadFile(
711 FilePathStringTypeToString(histograms_xml_file_path.value())));
712 std::map<Sample, std::string> login_custom_flags =
713 ReadEnumFromHistogramsXml("LoginCustomFlags", &histograms_xml);
714 ASSERT_TRUE(login_custom_flags.size())
715 << "Error reading enum 'LoginCustomFlags' from histograms.xml.";
717 // Build reverse map {switch_name => id} from login_custom_flags.
718 SwitchToIdMap histograms_xml_switches_ids;
720 EXPECT_TRUE(login_custom_flags.count(testing::kBadSwitchFormatHistogramId))
721 << "Entry for UMA ID of incorrect command-line flag is not found in "
722 "histograms.xml enum LoginCustomFlags. "
723 "Consider adding entry:\n"
724 << " " << GetHistogramEnumEntryText("BAD_FLAG_FORMAT", 0);
725 // Check that all LoginCustomFlags entries have correct values.
726 for (const auto& entry : login_custom_flags) {
727 if (entry.first == testing::kBadSwitchFormatHistogramId) {
728 // Add error value with empty name.
729 SetSwitchToHistogramIdMapping(std::string(), entry.first,
730 &histograms_xml_switches_ids);
731 continue;
733 const Sample uma_id = GetSwitchUMAId(entry.second);
734 EXPECT_EQ(uma_id, entry.first)
735 << "histograms.xml enum LoginCustomFlags "
736 "entry '" << entry.second << "' has incorrect value=" << entry.first
737 << ", but " << uma_id << " is expected. Consider changing entry to:\n"
738 << " " << GetHistogramEnumEntryText(entry.second, uma_id);
739 SetSwitchToHistogramIdMapping(entry.second, entry.first,
740 &histograms_xml_switches_ids);
743 // Check that all flags in about_flags.cc have entries in login_custom_flags.
744 std::set<std::string> all_switches = GetAllSwitchesForTesting();
745 for (const std::string& flag : all_switches) {
746 // Skip empty placeholders.
747 if (flag.empty())
748 continue;
749 const Sample uma_id = GetSwitchUMAId(flag);
750 EXPECT_NE(testing::kBadSwitchFormatHistogramId, uma_id)
751 << "Command-line switch '" << flag
752 << "' from about_flags.cc has UMA ID equal to reserved value "
753 "kBadSwitchFormatHistogramId="
754 << testing::kBadSwitchFormatHistogramId
755 << ". Please modify switch name.";
756 SwitchToIdMap::iterator enum_entry =
757 histograms_xml_switches_ids.lower_bound(flag);
759 // Ignore case here when switch ID is incorrect - it has already been
760 // reported in the previous loop.
761 EXPECT_TRUE(enum_entry != histograms_xml_switches_ids.end() &&
762 enum_entry->first == flag)
763 << "histograms.xml enum LoginCustomFlags doesn't contain switch '"
764 << flag << "' (value=" << uma_id
765 << " expected). Consider adding entry:\n"
766 << " " << GetHistogramEnumEntryText(flag, uma_id);
770 } // namespace about_flags