blob: a666043a1d230cf277f15f679466ecd1536a973c [file] [log] [blame]
[email protected]1bc78422011-03-31 08:41:381// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]e2ddbc92010-10-15 20:02:072// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4b66a7d2014-08-15 17:13:315#include <stdint.h>
6
7#include "base/files/file_path.h"
8#include "base/memory/scoped_vector.h"
9#include "base/path_service.h"
[email protected]3853a4c2013-02-11 17:15:5710#include "base/prefs/pref_registry_simple.h"
[email protected]e5ba874f2013-02-14 17:20:1911#include "base/prefs/testing_pref_service.h"
[email protected]3ea1b182013-02-08 22:38:4112#include "base/strings/string_number_conversions.h"
[email protected]4b66a7d2014-08-15 17:13:3113#include "base/strings/stringprintf.h"
[email protected]135cb802013-06-09 16:44:2014#include "base/strings/utf_string_conversions.h"
[email protected]a314ee5a2010-10-26 21:23:2815#include "base/values.h"
[email protected]8a6ff28d2010-12-02 16:35:1916#include "chrome/browser/about_flags.h"
[email protected]e6d1c4f2013-06-12 17:37:4017#include "chrome/browser/pref_service_flags_storage.h"
[email protected]e2ddbc92010-10-15 20:02:0718#include "chrome/common/chrome_switches.h"
19#include "chrome/common/pref_names.h"
[email protected]af39f002014-08-22 10:18:1820#include "chrome/grit/chromium_strings.h"
[email protected]e2ddbc92010-10-15 20:02:0721#include "testing/gtest/include/gtest/gtest.h"
[email protected]4b66a7d2014-08-15 17:13:3122#include "third_party/libxml/chromium/libxml_utils.h"
23
24namespace {
[email protected]e2ddbc92010-10-15 20:02:0725
[email protected]a314ee5a2010-10-26 21:23:2826const char kFlags1[] = "flag1";
27const char kFlags2[] = "flag2";
28const char kFlags3[] = "flag3";
[email protected]8a6ff28d2010-12-02 16:35:1929const char kFlags4[] = "flag4";
[email protected]83e9fa702013-02-25 19:30:4430const char kFlags5[] = "flag5";
[email protected]e2ddbc92010-10-15 20:02:0731
[email protected]a314ee5a2010-10-26 21:23:2832const char kSwitch1[] = "switch";
33const char kSwitch2[] = "switch2";
34const char kSwitch3[] = "switch3";
[email protected]a82744532011-02-11 16:15:5335const char kValueForSwitch2[] = "value_for_switch2";
[email protected]e2ddbc92010-10-15 20:02:0736
[email protected]8a6ff28d2010-12-02 16:35:1937const char kMultiSwitch1[] = "multi_switch1";
38const char kMultiSwitch2[] = "multi_switch2";
[email protected]a82744532011-02-11 16:15:5339const char kValueForMultiSwitch2[] = "value_for_multi_switch2";
[email protected]8a6ff28d2010-12-02 16:35:1940
[email protected]83e9fa702013-02-25 19:30:4441const char kEnableDisableValue1[] = "value1";
42const char kEnableDisableValue2[] = "value2";
43
alemate0107c3272014-09-03 04:30:0444typedef base::HistogramBase::Sample Sample;
45typedef std::map<std::string, Sample> SwitchToIdMap;
[email protected]4b66a7d2014-08-15 17:13:3146
47// This is a helper function to the ReadEnumFromHistogramsXml().
48// Extracts single enum (with integer values) from histograms.xml.
49// Expects |reader| to point at given enum.
50// Returns map { value => label }.
51// Returns empty map on error.
alemate0107c3272014-09-03 04:30:0452std::map<Sample, std::string> ParseEnumFromHistogramsXml(
[email protected]4b66a7d2014-08-15 17:13:3153 const std::string& enum_name,
54 XmlReader* reader) {
55 int entries_index = -1;
56
alemate0107c3272014-09-03 04:30:0457 std::map<Sample, std::string> result;
[email protected]4b66a7d2014-08-15 17:13:3158 bool success = true;
59
60 while (true) {
61 const std::string node_name = reader->NodeName();
62 if (node_name == "enum" && reader->IsClosingElement())
63 break;
64
65 if (node_name == "int") {
66 ++entries_index;
67 std::string value_str;
68 std::string label;
69 const bool has_value = reader->NodeAttribute("value", &value_str);
70 const bool has_label = reader->NodeAttribute("label", &label);
71 if (!has_value) {
72 ADD_FAILURE() << "Bad " << enum_name << " enum entry (at index "
73 << entries_index << ", label='" << label
74 << "'): No 'value' attribute.";
75 success = false;
76 }
77 if (!has_label) {
78 ADD_FAILURE() << "Bad " << enum_name << " enum entry (at index "
79 << entries_index << ", value_str='" << value_str
80 << "'): No 'label' attribute.";
81 success = false;
82 }
83
alemate0107c3272014-09-03 04:30:0484 Sample value;
85 if (has_value && !base::StringToInt(value_str, &value)) {
[email protected]4b66a7d2014-08-15 17:13:3186 ADD_FAILURE() << "Bad " << enum_name << " enum entry (at index "
87 << entries_index << ", label='" << label
88 << "', value_str='" << value_str
89 << "'): 'value' attribute is not integer.";
90 success = false;
91 }
92 if (result.count(value)) {
93 ADD_FAILURE() << "Bad " << enum_name << " enum entry (at index "
94 << entries_index << ", label='" << label
95 << "', value_str='" << value_str
96 << "'): duplicate value '" << value_str
97 << "' found in enum. The previous one has label='"
98 << result[value] << "'.";
99 success = false;
100 }
101 if (success) {
102 result[value] = label;
103 }
104 }
105 // All enum entries are on the same level, so it is enough to iterate
106 // until possible.
107 reader->Next();
108 }
alemate0107c3272014-09-03 04:30:04109 return (success ? result : std::map<Sample, std::string>());
[email protected]4b66a7d2014-08-15 17:13:31110}
111
112// Find and read given enum (with integer values) from histograms.xml.
113// |enum_name| - enum name.
114// |histograms_xml| - must be loaded histograms.xml file.
115//
116// Returns map { value => label } so that:
117// <int value="9" label="enable-pinch-virtual-viewport"/>
118// becomes:
119// { 9 => "enable-pinch-virtual-viewport" }
120// Returns empty map on error.
alemate0107c3272014-09-03 04:30:04121std::map<Sample, std::string> ReadEnumFromHistogramsXml(
[email protected]4b66a7d2014-08-15 17:13:31122 const std::string& enum_name,
123 XmlReader* histograms_xml) {
alemate0107c3272014-09-03 04:30:04124 std::map<Sample, std::string> login_custom_flags;
[email protected]4b66a7d2014-08-15 17:13:31125
126 // Implement simple depth first search.
127 while (true) {
128 const std::string node_name = histograms_xml->NodeName();
129 if (node_name == "enum") {
130 std::string name;
131 if (histograms_xml->NodeAttribute("name", &name) && name == enum_name) {
132 if (!login_custom_flags.empty()) {
133 EXPECT_TRUE(login_custom_flags.empty())
134 << "Duplicate enum '" << enum_name << "' found in histograms.xml";
alemate0107c3272014-09-03 04:30:04135 return std::map<Sample, std::string>();
[email protected]4b66a7d2014-08-15 17:13:31136 }
137
138 const bool got_into_enum = histograms_xml->Read();
139 if (got_into_enum) {
140 login_custom_flags =
141 ParseEnumFromHistogramsXml(enum_name, histograms_xml);
142 EXPECT_FALSE(login_custom_flags.empty())
143 << "Bad enum '" << enum_name
144 << "' found in histograms.xml (format error).";
145 } else {
146 EXPECT_TRUE(got_into_enum)
147 << "Bad enum '" << enum_name
148 << "' (looks empty) found in histograms.xml.";
149 }
150 if (login_custom_flags.empty())
alemate0107c3272014-09-03 04:30:04151 return std::map<Sample, std::string>();
[email protected]4b66a7d2014-08-15 17:13:31152 }
153 }
154 // Go deeper if possible (stops at the closing tag of the deepest node).
155 if (histograms_xml->Read())
156 continue;
157
158 // Try next node on the same level (skips closing tag).
159 if (histograms_xml->Next())
160 continue;
161
162 // Go up until next node on the same level exists.
163 while (histograms_xml->Depth() && !histograms_xml->SkipToElement()) {
164 }
165
166 // Reached top. histograms.xml consists of the single top level node
167 // 'histogram-configuration', so this is the end.
168 if (!histograms_xml->Depth())
169 break;
170 }
171 EXPECT_FALSE(login_custom_flags.empty())
172 << "Enum '" << enum_name << "' is not found in histograms.xml.";
173 return login_custom_flags;
174}
175
176std::string FilePathStringTypeToString(const base::FilePath::StringType& path) {
177#if defined(OS_WIN)
178 return base::UTF16ToUTF8(path);
179#else
180 return path;
181#endif
182}
183
184std::set<std::string> GetAllSwitchesForTesting() {
185 std::set<std::string> result;
186
187 size_t num_experiments = 0;
188 const about_flags::Experiment* experiments =
189 about_flags::testing::GetExperiments(&num_experiments);
190
191 for (size_t i = 0; i < num_experiments; ++i) {
192 const about_flags::Experiment& experiment = experiments[i];
193 if (experiment.type == about_flags::Experiment::SINGLE_VALUE) {
194 result.insert(experiment.command_line_switch);
195 } else if (experiment.type == about_flags::Experiment::MULTI_VALUE) {
196 for (int j = 0; j < experiment.num_choices; ++j) {
197 result.insert(experiment.choices[j].command_line_switch);
198 }
199 } else {
200 DCHECK_EQ(experiment.type, about_flags::Experiment::ENABLE_DISABLE_VALUE);
201 result.insert(experiment.command_line_switch);
202 result.insert(experiment.disable_command_line_switch);
203 }
204 }
205 return result;
206}
207
208} // anonymous namespace
209
[email protected]e2ddbc92010-10-15 20:02:07210namespace about_flags {
211
[email protected]8a6ff28d2010-12-02 16:35:19212const Experiment::Choice kMultiChoices[] = {
[email protected]a82744532011-02-11 16:15:53213 { IDS_PRODUCT_NAME, "", "" },
214 { IDS_PRODUCT_NAME, kMultiSwitch1, "" },
215 { IDS_PRODUCT_NAME, kMultiSwitch2, kValueForMultiSwitch2 },
[email protected]8a6ff28d2010-12-02 16:35:19216};
217
218// The experiments that are set for these tests. The 3rd experiment is not
219// supported on the current platform, all others are.
[email protected]a314ee5a2010-10-26 21:23:28220static Experiment kExperiments[] = {
221 {
222 kFlags1,
223 IDS_PRODUCT_NAME,
224 IDS_PRODUCT_NAME,
225 0, // Ends up being mapped to the current platform.
[email protected]8a6ff28d2010-12-02 16:35:19226 Experiment::SINGLE_VALUE,
227 kSwitch1,
[email protected]a82744532011-02-11 16:15:53228 "",
[email protected]8a6ff28d2010-12-02 16:35:19229 NULL,
[email protected]83e9fa702013-02-25 19:30:44230 NULL,
231 NULL,
[email protected]8a6ff28d2010-12-02 16:35:19232 0
[email protected]a314ee5a2010-10-26 21:23:28233 },
234 {
235 kFlags2,
236 IDS_PRODUCT_NAME,
237 IDS_PRODUCT_NAME,
238 0, // Ends up being mapped to the current platform.
[email protected]8a6ff28d2010-12-02 16:35:19239 Experiment::SINGLE_VALUE,
240 kSwitch2,
[email protected]a82744532011-02-11 16:15:53241 kValueForSwitch2,
[email protected]8a6ff28d2010-12-02 16:35:19242 NULL,
[email protected]83e9fa702013-02-25 19:30:44243 NULL,
244 NULL,
[email protected]8a6ff28d2010-12-02 16:35:19245 0
[email protected]a314ee5a2010-10-26 21:23:28246 },
247 {
248 kFlags3,
249 IDS_PRODUCT_NAME,
250 IDS_PRODUCT_NAME,
251 0, // This ends up enabling for an OS other than the current.
[email protected]8a6ff28d2010-12-02 16:35:19252 Experiment::SINGLE_VALUE,
253 kSwitch3,
[email protected]a82744532011-02-11 16:15:53254 "",
[email protected]8a6ff28d2010-12-02 16:35:19255 NULL,
[email protected]83e9fa702013-02-25 19:30:44256 NULL,
257 NULL,
[email protected]8a6ff28d2010-12-02 16:35:19258 0
259 },
260 {
261 kFlags4,
262 IDS_PRODUCT_NAME,
263 IDS_PRODUCT_NAME,
264 0, // Ends up being mapped to the current platform.
265 Experiment::MULTI_VALUE,
266 "",
[email protected]a82744532011-02-11 16:15:53267 "",
[email protected]83e9fa702013-02-25 19:30:44268 "",
269 "",
[email protected]8a6ff28d2010-12-02 16:35:19270 kMultiChoices,
271 arraysize(kMultiChoices)
[email protected]a314ee5a2010-10-26 21:23:28272 },
[email protected]83e9fa702013-02-25 19:30:44273 {
274 kFlags5,
275 IDS_PRODUCT_NAME,
276 IDS_PRODUCT_NAME,
277 0, // Ends up being mapped to the current platform.
278 Experiment::ENABLE_DISABLE_VALUE,
279 kSwitch1,
280 kEnableDisableValue1,
281 kSwitch2,
282 kEnableDisableValue2,
283 NULL,
284 3
285 },
[email protected]a314ee5a2010-10-26 21:23:28286};
287
[email protected]e2ddbc92010-10-15 20:02:07288class AboutFlagsTest : public ::testing::Test {
289 protected:
[email protected]e6d1c4f2013-06-12 17:37:40290 AboutFlagsTest() : flags_storage_(&prefs_) {
[email protected]b1de2c72013-02-06 02:45:47291 prefs_.registry()->RegisterListPref(prefs::kEnabledLabsExperiments);
[email protected]e2ddbc92010-10-15 20:02:07292 testing::ClearState();
293 }
294
[email protected]83e9fa702013-02-25 19:30:44295 virtual void SetUp() OVERRIDE {
[email protected]a314ee5a2010-10-26 21:23:28296 for (size_t i = 0; i < arraysize(kExperiments); ++i)
297 kExperiments[i].supported_platforms = GetCurrentPlatform();
298
299 int os_other_than_current = 1;
300 while (os_other_than_current == GetCurrentPlatform())
301 os_other_than_current <<= 1;
302 kExperiments[2].supported_platforms = os_other_than_current;
303
304 testing::SetExperiments(kExperiments, arraysize(kExperiments));
305 }
306
[email protected]83e9fa702013-02-25 19:30:44307 virtual void TearDown() OVERRIDE {
[email protected]a314ee5a2010-10-26 21:23:28308 testing::SetExperiments(NULL, 0);
309 }
310
[email protected]5b199522012-12-22 17:24:44311 TestingPrefServiceSimple prefs_;
[email protected]e6d1c4f2013-06-12 17:37:40312 PrefServiceFlagsStorage flags_storage_;
[email protected]e2ddbc92010-10-15 20:02:07313};
314
[email protected]147492b2013-03-19 23:52:08315
316TEST_F(AboutFlagsTest, NoChangeNoRestart) {
317 EXPECT_FALSE(IsRestartNeededToCommitChanges());
[email protected]e6d1c4f2013-06-12 17:37:40318 SetExperimentEnabled(&flags_storage_, kFlags1, false);
[email protected]147492b2013-03-19 23:52:08319 EXPECT_FALSE(IsRestartNeededToCommitChanges());
320}
321
[email protected]e2ddbc92010-10-15 20:02:07322TEST_F(AboutFlagsTest, ChangeNeedsRestart) {
[email protected]e2ddbc92010-10-15 20:02:07323 EXPECT_FALSE(IsRestartNeededToCommitChanges());
[email protected]e6d1c4f2013-06-12 17:37:40324 SetExperimentEnabled(&flags_storage_, kFlags1, true);
[email protected]e2ddbc92010-10-15 20:02:07325 EXPECT_TRUE(IsRestartNeededToCommitChanges());
326}
327
[email protected]147492b2013-03-19 23:52:08328TEST_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.
[email protected]e6d1c4f2013-06-12 17:37:40333 SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(2), true);
[email protected]147492b2013-03-19 23:52:08334 EXPECT_TRUE(IsRestartNeededToCommitChanges());
335 testing::ClearState();
336 EXPECT_FALSE(IsRestartNeededToCommitChanges());
337 // Enable the default choice now.
[email protected]e6d1c4f2013-06-12 17:37:40338 SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(0), true);
[email protected]147492b2013-03-19 23:52:08339 EXPECT_TRUE(IsRestartNeededToCommitChanges());
340}
341
[email protected]e2ddbc92010-10-15 20:02:07342TEST_F(AboutFlagsTest, AddTwoFlagsRemoveOne) {
[email protected]e2ddbc92010-10-15 20:02:07343 // Add two experiments, check they're there.
[email protected]e6d1c4f2013-06-12 17:37:40344 SetExperimentEnabled(&flags_storage_, kFlags1, true);
345 SetExperimentEnabled(&flags_storage_, kFlags2, true);
[email protected]e2ddbc92010-10-15 20:02:07346
[email protected]5bcdd99d2013-12-23 18:28:30347 const base::ListValue* experiments_list = prefs_.GetList(
[email protected]e2ddbc92010-10-15 20:02:07348 prefs::kEnabledLabsExperiments);
349 ASSERT_TRUE(experiments_list != NULL);
350
351 ASSERT_EQ(2u, experiments_list->GetSize());
352
353 std::string s0;
354 ASSERT_TRUE(experiments_list->GetString(0, &s0));
355 std::string s1;
356 ASSERT_TRUE(experiments_list->GetString(1, &s1));
357
358 EXPECT_TRUE(s0 == kFlags1 || s1 == kFlags1);
359 EXPECT_TRUE(s0 == kFlags2 || s1 == kFlags2);
360
361 // Remove one experiment, check the other's still around.
[email protected]e6d1c4f2013-06-12 17:37:40362 SetExperimentEnabled(&flags_storage_, kFlags2, false);
[email protected]e2ddbc92010-10-15 20:02:07363
[email protected]1bc78422011-03-31 08:41:38364 experiments_list = prefs_.GetList(prefs::kEnabledLabsExperiments);
[email protected]e2ddbc92010-10-15 20:02:07365 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);
369}
370
371TEST_F(AboutFlagsTest, AddTwoFlagsRemoveBoth) {
[email protected]e2ddbc92010-10-15 20:02:07372 // Add two experiments, check the pref exists.
[email protected]e6d1c4f2013-06-12 17:37:40373 SetExperimentEnabled(&flags_storage_, kFlags1, true);
374 SetExperimentEnabled(&flags_storage_, kFlags2, true);
[email protected]5bcdd99d2013-12-23 18:28:30375 const base::ListValue* experiments_list = prefs_.GetList(
[email protected]e2ddbc92010-10-15 20:02:07376 prefs::kEnabledLabsExperiments);
377 ASSERT_TRUE(experiments_list != NULL);
378
379 // Remove both, the pref should have been removed completely.
[email protected]e6d1c4f2013-06-12 17:37:40380 SetExperimentEnabled(&flags_storage_, kFlags1, false);
381 SetExperimentEnabled(&flags_storage_, kFlags2, false);
[email protected]1bc78422011-03-31 08:41:38382 experiments_list = prefs_.GetList(prefs::kEnabledLabsExperiments);
[email protected]e2ddbc92010-10-15 20:02:07383 EXPECT_TRUE(experiments_list == NULL || experiments_list->GetSize() == 0);
384}
385
386TEST_F(AboutFlagsTest, ConvertFlagsToSwitches) {
[email protected]e6d1c4f2013-06-12 17:37:40387 SetExperimentEnabled(&flags_storage_, kFlags1, true);
[email protected]e2ddbc92010-10-15 20:02:07388
[email protected]947446b2010-10-21 03:36:31389 CommandLine command_line(CommandLine::NO_PROGRAM);
[email protected]e2ddbc92010-10-15 20:02:07390 command_line.AppendSwitch("foo");
391
392 EXPECT_TRUE(command_line.HasSwitch("foo"));
[email protected]a314ee5a2010-10-26 21:23:28393 EXPECT_FALSE(command_line.HasSwitch(kSwitch1));
[email protected]e2ddbc92010-10-15 20:02:07394
[email protected]578f2092013-09-16 17:16:21395 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
[email protected]e2ddbc92010-10-15 20:02:07396
397 EXPECT_TRUE(command_line.HasSwitch("foo"));
[email protected]cd7fa99f2011-09-07 01:24:55398 EXPECT_TRUE(command_line.HasSwitch(kSwitch1));
[email protected]578f2092013-09-16 17:16:21399 EXPECT_TRUE(command_line.HasSwitch(switches::kFlagSwitchesBegin));
400 EXPECT_TRUE(command_line.HasSwitch(switches::kFlagSwitchesEnd));
401
402 CommandLine command_line2(CommandLine::NO_PROGRAM);
403
404 ConvertFlagsToSwitches(&flags_storage_, &command_line2, kNoSentinels);
405
406 EXPECT_TRUE(command_line2.HasSwitch(kSwitch1));
407 EXPECT_FALSE(command_line2.HasSwitch(switches::kFlagSwitchesBegin));
408 EXPECT_FALSE(command_line2.HasSwitch(switches::kFlagSwitchesEnd));
[email protected]e2ddbc92010-10-15 20:02:07409}
410
[email protected]4b66a7d2014-08-15 17:13:31411CommandLine::StringType CreateSwitch(const std::string& value) {
412#if defined(OS_WIN)
413 return base::ASCIIToUTF16(value);
414#else
415 return value;
416#endif
417}
418
[email protected]6d98abf2013-06-17 23:35:51419TEST_F(AboutFlagsTest, CompareSwitchesToCurrentCommandLine) {
420 SetExperimentEnabled(&flags_storage_, kFlags1, true);
421
[email protected]4b66a7d2014-08-15 17:13:31422 const std::string kDoubleDash("--");
423
[email protected]6d98abf2013-06-17 23:35:51424 CommandLine command_line(CommandLine::NO_PROGRAM);
425 command_line.AppendSwitch("foo");
426
427 CommandLine new_command_line(CommandLine::NO_PROGRAM);
[email protected]578f2092013-09-16 17:16:21428 ConvertFlagsToSwitches(&flags_storage_, &new_command_line, kAddSentinels);
[email protected]6d98abf2013-06-17 23:35:51429
[email protected]4b66a7d2014-08-15 17:13:31430 EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine(
431 new_command_line, command_line, NULL));
432 {
433 std::set<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)));
438 }
[email protected]6d98abf2013-06-17 23:35:51439
[email protected]578f2092013-09-16 17:16:21440 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
[email protected]6d98abf2013-06-17 23:35:51441
[email protected]4b66a7d2014-08-15 17:13:31442 EXPECT_TRUE(AreSwitchesIdenticalToCurrentCommandLine(
443 new_command_line, command_line, NULL));
444 {
445 std::set<CommandLine::StringType> difference;
446 EXPECT_TRUE(AreSwitchesIdenticalToCurrentCommandLine(
447 new_command_line, command_line, &difference));
448 EXPECT_TRUE(difference.empty());
449 }
[email protected]6d98abf2013-06-17 23:35:51450
451 // Now both have flags but different.
452 SetExperimentEnabled(&flags_storage_, kFlags1, false);
453 SetExperimentEnabled(&flags_storage_, kFlags2, true);
454
455 CommandLine another_command_line(CommandLine::NO_PROGRAM);
[email protected]578f2092013-09-16 17:16:21456 ConvertFlagsToSwitches(&flags_storage_, &another_command_line, kAddSentinels);
[email protected]6d98abf2013-06-17 23:35:51457
[email protected]4b66a7d2014-08-15 17:13:31458 EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine(
459 new_command_line, another_command_line, NULL));
460 {
461 std::set<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)));
469 }
[email protected]6d98abf2013-06-17 23:35:51470}
471
[email protected]e2ddbc92010-10-15 20:02:07472TEST_F(AboutFlagsTest, RemoveFlagSwitches) {
[email protected]e2ddbc92010-10-15 20:02:07473 std::map<std::string, CommandLine::StringType> switch_list;
[email protected]a314ee5a2010-10-26 21:23:28474 switch_list[kSwitch1] = CommandLine::StringType();
[email protected]e2ddbc92010-10-15 20:02:07475 switch_list[switches::kFlagSwitchesBegin] = CommandLine::StringType();
476 switch_list[switches::kFlagSwitchesEnd] = CommandLine::StringType();
477 switch_list["foo"] = CommandLine::StringType();
478
[email protected]e6d1c4f2013-06-12 17:37:40479 SetExperimentEnabled(&flags_storage_, kFlags1, true);
[email protected]e2ddbc92010-10-15 20:02:07480
481 // This shouldn't do anything before ConvertFlagsToSwitches() wasn't called.
482 RemoveFlagsSwitches(&switch_list);
483 ASSERT_EQ(4u, switch_list.size());
[email protected]a314ee5a2010-10-26 21:23:28484 EXPECT_TRUE(switch_list.find(kSwitch1) != switch_list.end());
[email protected]e2ddbc92010-10-15 20:02:07485 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());
490
491 // Call ConvertFlagsToSwitches(), then RemoveFlagsSwitches() again.
[email protected]947446b2010-10-21 03:36:31492 CommandLine command_line(CommandLine::NO_PROGRAM);
[email protected]e2ddbc92010-10-15 20:02:07493 command_line.AppendSwitch("foo");
[email protected]578f2092013-09-16 17:16:21494 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
[email protected]e2ddbc92010-10-15 20:02:07495 RemoveFlagsSwitches(&switch_list);
496
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());
500}
501
[email protected]a314ee5a2010-10-26 21:23:28502// Tests enabling experiments that aren't supported on the current platform.
503TEST_F(AboutFlagsTest, PersistAndPrune) {
[email protected]8a6ff28d2010-12-02 16:35:19504 // Enable experiments 1 and 3.
[email protected]e6d1c4f2013-06-12 17:37:40505 SetExperimentEnabled(&flags_storage_, kFlags1, true);
506 SetExperimentEnabled(&flags_storage_, kFlags3, true);
[email protected]a314ee5a2010-10-26 21:23:28507 CommandLine command_line(CommandLine::NO_PROGRAM);
508 EXPECT_FALSE(command_line.HasSwitch(kSwitch1));
509 EXPECT_FALSE(command_line.HasSwitch(kSwitch3));
510
511 // Convert the flags to switches. Experiment 3 shouldn't be among the switches
512 // as it is not applicable to the current platform.
[email protected]578f2092013-09-16 17:16:21513 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
[email protected]cd7fa99f2011-09-07 01:24:55514 EXPECT_TRUE(command_line.HasSwitch(kSwitch1));
[email protected]a314ee5a2010-10-26 21:23:28515 EXPECT_FALSE(command_line.HasSwitch(kSwitch3));
516
517 // Experiment 3 should show still be persisted in preferences though.
[email protected]5bcdd99d2013-12-23 18:28:30518 const base::ListValue* experiments_list =
[email protected]ee28495a2013-05-20 04:10:52519 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);
[email protected]8a6ff28d2010-12-02 16:35:19528}
529
[email protected]a82744532011-02-11 16:15:53530// Tests that switches which should have values get them in the command
531// line.
532TEST_F(AboutFlagsTest, CheckValues) {
533 // Enable experiments 1 and 2.
[email protected]e6d1c4f2013-06-12 17:37:40534 SetExperimentEnabled(&flags_storage_, kFlags1, true);
535 SetExperimentEnabled(&flags_storage_, kFlags2, true);
[email protected]a82744532011-02-11 16:15:53536 CommandLine command_line(CommandLine::NO_PROGRAM);
537 EXPECT_FALSE(command_line.HasSwitch(kSwitch1));
538 EXPECT_FALSE(command_line.HasSwitch(kSwitch2));
539
540 // Convert the flags to switches.
[email protected]578f2092013-09-16 17:16:21541 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
[email protected]cd7fa99f2011-09-07 01:24:55542 EXPECT_TRUE(command_line.HasSwitch(kSwitch1));
[email protected]007b3f82013-04-09 08:46:45543 EXPECT_EQ(std::string(), command_line.GetSwitchValueASCII(kSwitch1));
[email protected]cd7fa99f2011-09-07 01:24:55544 EXPECT_TRUE(command_line.HasSwitch(kSwitch2));
545 EXPECT_EQ(std::string(kValueForSwitch2),
[email protected]a82744532011-02-11 16:15:53546 command_line.GetSwitchValueASCII(kSwitch2));
547
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(std::wstring::npos,
[email protected]61a4c6f2011-07-20 04:54:52554 command_line.GetCommandLineString().find(
[email protected]670d3232013-12-24 17:58:58555 base::ASCIIToWide(switch1_with_equals)));
[email protected]a82744532011-02-11 16:15:53556#else
557 EXPECT_EQ(std::string::npos,
[email protected]61a4c6f2011-07-20 04:54:52558 command_line.GetCommandLineString().find(switch1_with_equals));
[email protected]a82744532011-02-11 16:15:53559#endif
560
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("=");
[email protected]5b199522012-12-22 17:24:44565#if defined(OS_WIN)
[email protected]cd7fa99f2011-09-07 01:24:55566 EXPECT_NE(std::wstring::npos,
[email protected]61a4c6f2011-07-20 04:54:52567 command_line.GetCommandLineString().find(
[email protected]670d3232013-12-24 17:58:58568 base::ASCIIToWide(switch2_with_equals)));
[email protected]a82744532011-02-11 16:15:53569#else
[email protected]cd7fa99f2011-09-07 01:24:55570 EXPECT_NE(std::string::npos,
[email protected]61a4c6f2011-07-20 04:54:52571 command_line.GetCommandLineString().find(switch2_with_equals));
[email protected]a82744532011-02-11 16:15:53572#endif
573
[email protected]ee28495a2013-05-20 04:10:52574 // And it should persist.
[email protected]5bcdd99d2013-12-23 18:28:30575 const base::ListValue* experiments_list =
[email protected]ee28495a2013-05-20 04:10:52576 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);
[email protected]a82744532011-02-11 16:15:53585}
586
[email protected]28e35af2011-02-09 12:56:22587// Tests multi-value type experiments.
[email protected]8a6ff28d2010-12-02 16:35:19588TEST_F(AboutFlagsTest, MultiValues) {
[email protected]83e9fa702013-02-25 19:30:44589 const Experiment& experiment = kExperiments[3];
590 ASSERT_EQ(kFlags4, experiment.internal_name);
591
[email protected]28e35af2011-02-09 12:56:22592 // Initially, the first "deactivated" option of the multi experiment should
593 // be set.
[email protected]8a6ff28d2010-12-02 16:35:19594 {
595 CommandLine command_line(CommandLine::NO_PROGRAM);
[email protected]578f2092013-09-16 17:16:21596 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
[email protected]28e35af2011-02-09 12:56:22597 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1));
[email protected]8a6ff28d2010-12-02 16:35:19598 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2));
599 }
600
[email protected]28e35af2011-02-09 12:56:22601 // Enable the 2nd choice of the multi-value.
[email protected]e6d1c4f2013-06-12 17:37:40602 SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(2), true);
[email protected]8a6ff28d2010-12-02 16:35:19603 {
604 CommandLine command_line(CommandLine::NO_PROGRAM);
[email protected]578f2092013-09-16 17:16:21605 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
[email protected]8a6ff28d2010-12-02 16:35:19606 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1));
[email protected]cd7fa99f2011-09-07 01:24:55607 EXPECT_TRUE(command_line.HasSwitch(kMultiSwitch2));
608 EXPECT_EQ(std::string(kValueForMultiSwitch2),
[email protected]a82744532011-02-11 16:15:53609 command_line.GetSwitchValueASCII(kMultiSwitch2));
[email protected]8a6ff28d2010-12-02 16:35:19610 }
611
612 // Disable the multi-value experiment.
[email protected]e6d1c4f2013-06-12 17:37:40613 SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(0), true);
[email protected]83e9fa702013-02-25 19:30:44614 {
615 CommandLine command_line(CommandLine::NO_PROGRAM);
[email protected]578f2092013-09-16 17:16:21616 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
[email protected]83e9fa702013-02-25 19:30:44617 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1));
618 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2));
619 }
620}
621
622TEST_F(AboutFlagsTest, EnableDisableValues) {
623 const Experiment& experiment = kExperiments[4];
624 ASSERT_EQ(kFlags5, experiment.internal_name);
625
626 // Nothing selected.
627 {
628 CommandLine command_line(CommandLine::NO_PROGRAM);
[email protected]578f2092013-09-16 17:16:21629 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
[email protected]83e9fa702013-02-25 19:30:44630 EXPECT_FALSE(command_line.HasSwitch(kSwitch1));
631 EXPECT_FALSE(command_line.HasSwitch(kSwitch2));
632 }
633
634 // "Enable" option selected.
[email protected]e6d1c4f2013-06-12 17:37:40635 SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(1), true);
[email protected]83e9fa702013-02-25 19:30:44636 {
637 CommandLine command_line(CommandLine::NO_PROGRAM);
[email protected]578f2092013-09-16 17:16:21638 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
[email protected]83e9fa702013-02-25 19:30:44639 EXPECT_TRUE(command_line.HasSwitch(kSwitch1));
640 EXPECT_FALSE(command_line.HasSwitch(kSwitch2));
641 EXPECT_EQ(kEnableDisableValue1, command_line.GetSwitchValueASCII(kSwitch1));
642 }
643
644 // "Disable" option selected.
[email protected]e6d1c4f2013-06-12 17:37:40645 SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(2), true);
[email protected]83e9fa702013-02-25 19:30:44646 {
647 CommandLine command_line(CommandLine::NO_PROGRAM);
[email protected]578f2092013-09-16 17:16:21648 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
[email protected]83e9fa702013-02-25 19:30:44649 EXPECT_FALSE(command_line.HasSwitch(kSwitch1));
650 EXPECT_TRUE(command_line.HasSwitch(kSwitch2));
651 EXPECT_EQ(kEnableDisableValue2, command_line.GetSwitchValueASCII(kSwitch2));
652 }
653
654 // "Default" option selected, same as nothing selected.
[email protected]e6d1c4f2013-06-12 17:37:40655 SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(0), true);
[email protected]8a6ff28d2010-12-02 16:35:19656 {
657 CommandLine command_line(CommandLine::NO_PROGRAM);
[email protected]578f2092013-09-16 17:16:21658 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels);
[email protected]8a6ff28d2010-12-02 16:35:19659 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1));
660 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2));
661 }
662}
663
664// Makes sure there are no separators in any of the experiment names.
665TEST_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;
672 }
[email protected]a314ee5a2010-10-26 21:23:28673}
674
[email protected]4b66a7d2014-08-15 17:13:31675class 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,
alemate0107c3272014-09-03 04:30:04680 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 =
[email protected]4b66a7d2014-08-15 17:13:31683 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.";
688 }
689 }
690
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,
alemate0107c3272014-09-03 04:30:04694 Sample value) {
[email protected]4b66a7d2014-08-15 17:13:31695 return base::StringPrintf(
alemate0107c3272014-09-03 04:30:04696 "<int value=\"%d\" label=\"%s\"/>", value, switch_name.c_str());
[email protected]4b66a7d2014-08-15 17:13:31697 }
698};
699
700TEST_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");
708
709 XmlReader histograms_xml;
710 ASSERT_TRUE(histograms_xml.LoadFile(
711 FilePathStringTypeToString(histograms_xml_file_path.value())));
alemate0107c3272014-09-03 04:30:04712 std::map<Sample, std::string> login_custom_flags =
[email protected]4b66a7d2014-08-15 17:13:31713 ReadEnumFromHistogramsXml("LoginCustomFlags", &histograms_xml);
714 ASSERT_TRUE(login_custom_flags.size())
715 << "Error reading enum 'LoginCustomFlags' from histograms.xml.";
716
717 // Build reverse map {switch_name => id} from login_custom_flags.
718 SwitchToIdMap histograms_xml_switches_ids;
719
720 EXPECT_TRUE(login_custom_flags.count(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.
alemate0107c3272014-09-03 04:30:04726 for (std::map<Sample, std::string>::const_iterator it =
[email protected]4b66a7d2014-08-15 17:13:31727 login_custom_flags.begin();
728 it != login_custom_flags.end();
729 ++it) {
730 if (it->first == kBadSwitchFormatHistogramId) {
731 // Add eror value with empty name.
732 SetSwitchToHistogramIdMapping(
733 "", it->first, &histograms_xml_switches_ids);
734 continue;
735 }
alemate0107c3272014-09-03 04:30:04736 const Sample uma_id = GetSwitchUMAId(it->second);
[email protected]4b66a7d2014-08-15 17:13:31737 EXPECT_EQ(uma_id, it->first)
738 << "histograms.xml enum LoginCustomFlags "
739 "entry '" << it->second << "' has incorrect value=" << it->first
740 << ", but " << uma_id << " is expected. Consider changing entry to:\n"
741 << " " << GetHistogramEnumEntryText(it->second, uma_id);
742 SetSwitchToHistogramIdMapping(
743 it->second, it->first, &histograms_xml_switches_ids);
744 }
745
746 // Check that all flags in about_flags.cc have entries in login_custom_flags.
747 std::set<std::string> all_switches = GetAllSwitchesForTesting();
748 for (std::set<std::string>::const_iterator it = all_switches.begin();
749 it != all_switches.end();
750 ++it) {
751 // Skip empty placeholders.
752 if (it->empty())
753 continue;
alemate0107c3272014-09-03 04:30:04754 const Sample uma_id = GetSwitchUMAId(*it);
[email protected]4b66a7d2014-08-15 17:13:31755 EXPECT_NE(kBadSwitchFormatHistogramId, uma_id)
756 << "Command-line switch '" << *it
757 << "' from about_flags.cc has UMA ID equal to reserved value "
758 "kBadSwitchFormatHistogramId=" << kBadSwitchFormatHistogramId
759 << ". Please modify switch name.";
760 SwitchToIdMap::iterator enum_entry =
761 histograms_xml_switches_ids.lower_bound(*it);
762
763 // Ignore case here when switch ID is incorrect - it has already been
764 // reported in the previous loop.
765 EXPECT_TRUE(enum_entry != histograms_xml_switches_ids.end() &&
766 enum_entry->first == *it)
767 << "histograms.xml enum LoginCustomFlags doesn't contain switch '"
768 << *it << "' (value=" << uma_id
769 << " expected). Consider adding entry:\n"
770 << " " << GetHistogramEnumEntryText(*it, uma_id);
771 }
772}
773
[email protected]e2ddbc92010-10-15 20:02:07774} // namespace about_flags