[email protected] | 1bc7842 | 2011-03-31 08:41:38 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 5 | #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] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 10 | #include "base/prefs/pref_registry_simple.h" |
[email protected] | e5ba874f | 2013-02-14 17:20:19 | [diff] [blame] | 11 | #include "base/prefs/testing_pref_service.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 12 | #include "base/strings/string_number_conversions.h" |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 13 | #include "base/strings/stringprintf.h" |
[email protected] | 135cb80 | 2013-06-09 16:44:20 | [diff] [blame] | 14 | #include "base/strings/utf_string_conversions.h" |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 15 | #include "base/values.h" |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 16 | #include "chrome/browser/about_flags.h" |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 17 | #include "chrome/browser/pref_service_flags_storage.h" |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 18 | #include "chrome/common/chrome_switches.h" |
| 19 | #include "chrome/common/pref_names.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 20 | #include "chrome/grit/chromium_strings.h" |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 21 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 22 | #include "third_party/libxml/chromium/libxml_utils.h" |
| 23 | |
| 24 | namespace { |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 25 | |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 26 | const char kFlags1[] = "flag1"; |
| 27 | const char kFlags2[] = "flag2"; |
| 28 | const char kFlags3[] = "flag3"; |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 29 | const char kFlags4[] = "flag4"; |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 30 | const char kFlags5[] = "flag5"; |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 31 | |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 32 | const char kSwitch1[] = "switch"; |
| 33 | const char kSwitch2[] = "switch2"; |
| 34 | const char kSwitch3[] = "switch3"; |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 35 | const char kValueForSwitch2[] = "value_for_switch2"; |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 36 | |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 37 | const char kMultiSwitch1[] = "multi_switch1"; |
| 38 | const char kMultiSwitch2[] = "multi_switch2"; |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 39 | const char kValueForMultiSwitch2[] = "value_for_multi_switch2"; |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 40 | |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 41 | const char kEnableDisableValue1[] = "value1"; |
| 42 | const char kEnableDisableValue2[] = "value2"; |
| 43 | |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 44 | typedef base::HistogramBase::Sample Sample; |
| 45 | typedef std::map<std::string, Sample> SwitchToIdMap; |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 46 | |
| 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. |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 52 | std::map<Sample, std::string> ParseEnumFromHistogramsXml( |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 53 | const std::string& enum_name, |
| 54 | XmlReader* reader) { |
| 55 | int entries_index = -1; |
| 56 | |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 57 | std::map<Sample, std::string> result; |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 58 | 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 | |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 84 | Sample value; |
| 85 | if (has_value && !base::StringToInt(value_str, &value)) { |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 86 | 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 | } |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 109 | return (success ? result : std::map<Sample, std::string>()); |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 110 | } |
| 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. |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 121 | std::map<Sample, std::string> ReadEnumFromHistogramsXml( |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 122 | const std::string& enum_name, |
| 123 | XmlReader* histograms_xml) { |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 124 | std::map<Sample, std::string> login_custom_flags; |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 125 | |
| 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"; |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 135 | return std::map<Sample, std::string>(); |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 136 | } |
| 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()) |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 151 | return std::map<Sample, std::string>(); |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 152 | } |
| 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 | |
| 176 | std::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 | |
| 184 | std::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] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 210 | namespace about_flags { |
| 211 | |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 212 | const Experiment::Choice kMultiChoices[] = { |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 213 | { IDS_PRODUCT_NAME, "", "" }, |
| 214 | { IDS_PRODUCT_NAME, kMultiSwitch1, "" }, |
| 215 | { IDS_PRODUCT_NAME, kMultiSwitch2, kValueForMultiSwitch2 }, |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 216 | }; |
| 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] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 220 | static 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] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 226 | Experiment::SINGLE_VALUE, |
| 227 | kSwitch1, |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 228 | "", |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 229 | NULL, |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 230 | NULL, |
| 231 | NULL, |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 232 | 0 |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 233 | }, |
| 234 | { |
| 235 | kFlags2, |
| 236 | IDS_PRODUCT_NAME, |
| 237 | IDS_PRODUCT_NAME, |
| 238 | 0, // Ends up being mapped to the current platform. |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 239 | Experiment::SINGLE_VALUE, |
| 240 | kSwitch2, |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 241 | kValueForSwitch2, |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 242 | NULL, |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 243 | NULL, |
| 244 | NULL, |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 245 | 0 |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 246 | }, |
| 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] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 252 | Experiment::SINGLE_VALUE, |
| 253 | kSwitch3, |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 254 | "", |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 255 | NULL, |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 256 | NULL, |
| 257 | NULL, |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 258 | 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] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 267 | "", |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 268 | "", |
| 269 | "", |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 270 | kMultiChoices, |
| 271 | arraysize(kMultiChoices) |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 272 | }, |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 273 | { |
| 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] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 286 | }; |
| 287 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 288 | class AboutFlagsTest : public ::testing::Test { |
| 289 | protected: |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 290 | AboutFlagsTest() : flags_storage_(&prefs_) { |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 291 | prefs_.registry()->RegisterListPref(prefs::kEnabledLabsExperiments); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 292 | testing::ClearState(); |
| 293 | } |
| 294 | |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame^] | 295 | void SetUp() override { |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 296 | 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 | |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame^] | 307 | void TearDown() override { testing::SetExperiments(NULL, 0); } |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 308 | |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 309 | TestingPrefServiceSimple prefs_; |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 310 | PrefServiceFlagsStorage flags_storage_; |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 311 | }; |
| 312 | |
[email protected] | 147492b | 2013-03-19 23:52:08 | [diff] [blame] | 313 | |
| 314 | TEST_F(AboutFlagsTest, NoChangeNoRestart) { |
| 315 | EXPECT_FALSE(IsRestartNeededToCommitChanges()); |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 316 | SetExperimentEnabled(&flags_storage_, kFlags1, false); |
[email protected] | 147492b | 2013-03-19 23:52:08 | [diff] [blame] | 317 | EXPECT_FALSE(IsRestartNeededToCommitChanges()); |
| 318 | } |
| 319 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 320 | TEST_F(AboutFlagsTest, ChangeNeedsRestart) { |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 321 | EXPECT_FALSE(IsRestartNeededToCommitChanges()); |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 322 | SetExperimentEnabled(&flags_storage_, kFlags1, true); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 323 | EXPECT_TRUE(IsRestartNeededToCommitChanges()); |
| 324 | } |
| 325 | |
[email protected] | 147492b | 2013-03-19 23:52:08 | [diff] [blame] | 326 | TEST_F(AboutFlagsTest, MultiFlagChangeNeedsRestart) { |
| 327 | const Experiment& experiment = kExperiments[3]; |
| 328 | ASSERT_EQ(kFlags4, experiment.internal_name); |
| 329 | EXPECT_FALSE(IsRestartNeededToCommitChanges()); |
| 330 | // Enable the 2nd choice of the multi-value. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 331 | SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(2), true); |
[email protected] | 147492b | 2013-03-19 23:52:08 | [diff] [blame] | 332 | EXPECT_TRUE(IsRestartNeededToCommitChanges()); |
| 333 | testing::ClearState(); |
| 334 | EXPECT_FALSE(IsRestartNeededToCommitChanges()); |
| 335 | // Enable the default choice now. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 336 | SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(0), true); |
[email protected] | 147492b | 2013-03-19 23:52:08 | [diff] [blame] | 337 | EXPECT_TRUE(IsRestartNeededToCommitChanges()); |
| 338 | } |
| 339 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 340 | TEST_F(AboutFlagsTest, AddTwoFlagsRemoveOne) { |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 341 | // Add two experiments, check they're there. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 342 | SetExperimentEnabled(&flags_storage_, kFlags1, true); |
| 343 | SetExperimentEnabled(&flags_storage_, kFlags2, true); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 344 | |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 345 | const base::ListValue* experiments_list = prefs_.GetList( |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 346 | prefs::kEnabledLabsExperiments); |
| 347 | ASSERT_TRUE(experiments_list != NULL); |
| 348 | |
| 349 | ASSERT_EQ(2u, experiments_list->GetSize()); |
| 350 | |
| 351 | std::string s0; |
| 352 | ASSERT_TRUE(experiments_list->GetString(0, &s0)); |
| 353 | std::string s1; |
| 354 | ASSERT_TRUE(experiments_list->GetString(1, &s1)); |
| 355 | |
| 356 | EXPECT_TRUE(s0 == kFlags1 || s1 == kFlags1); |
| 357 | EXPECT_TRUE(s0 == kFlags2 || s1 == kFlags2); |
| 358 | |
| 359 | // Remove one experiment, check the other's still around. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 360 | SetExperimentEnabled(&flags_storage_, kFlags2, false); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 361 | |
[email protected] | 1bc7842 | 2011-03-31 08:41:38 | [diff] [blame] | 362 | experiments_list = prefs_.GetList(prefs::kEnabledLabsExperiments); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 363 | ASSERT_TRUE(experiments_list != NULL); |
| 364 | ASSERT_EQ(1u, experiments_list->GetSize()); |
| 365 | ASSERT_TRUE(experiments_list->GetString(0, &s0)); |
| 366 | EXPECT_TRUE(s0 == kFlags1); |
| 367 | } |
| 368 | |
| 369 | TEST_F(AboutFlagsTest, AddTwoFlagsRemoveBoth) { |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 370 | // Add two experiments, check the pref exists. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 371 | SetExperimentEnabled(&flags_storage_, kFlags1, true); |
| 372 | SetExperimentEnabled(&flags_storage_, kFlags2, true); |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 373 | const base::ListValue* experiments_list = prefs_.GetList( |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 374 | prefs::kEnabledLabsExperiments); |
| 375 | ASSERT_TRUE(experiments_list != NULL); |
| 376 | |
| 377 | // Remove both, the pref should have been removed completely. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 378 | SetExperimentEnabled(&flags_storage_, kFlags1, false); |
| 379 | SetExperimentEnabled(&flags_storage_, kFlags2, false); |
[email protected] | 1bc7842 | 2011-03-31 08:41:38 | [diff] [blame] | 380 | experiments_list = prefs_.GetList(prefs::kEnabledLabsExperiments); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 381 | EXPECT_TRUE(experiments_list == NULL || experiments_list->GetSize() == 0); |
| 382 | } |
| 383 | |
| 384 | TEST_F(AboutFlagsTest, ConvertFlagsToSwitches) { |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 385 | SetExperimentEnabled(&flags_storage_, kFlags1, true); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 386 | |
[email protected] | 947446b | 2010-10-21 03:36:31 | [diff] [blame] | 387 | CommandLine command_line(CommandLine::NO_PROGRAM); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 388 | command_line.AppendSwitch("foo"); |
| 389 | |
| 390 | EXPECT_TRUE(command_line.HasSwitch("foo")); |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 391 | EXPECT_FALSE(command_line.HasSwitch(kSwitch1)); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 392 | |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 393 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 394 | |
| 395 | EXPECT_TRUE(command_line.HasSwitch("foo")); |
[email protected] | cd7fa99f | 2011-09-07 01:24:55 | [diff] [blame] | 396 | EXPECT_TRUE(command_line.HasSwitch(kSwitch1)); |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 397 | EXPECT_TRUE(command_line.HasSwitch(switches::kFlagSwitchesBegin)); |
| 398 | EXPECT_TRUE(command_line.HasSwitch(switches::kFlagSwitchesEnd)); |
| 399 | |
| 400 | CommandLine command_line2(CommandLine::NO_PROGRAM); |
| 401 | |
| 402 | ConvertFlagsToSwitches(&flags_storage_, &command_line2, kNoSentinels); |
| 403 | |
| 404 | EXPECT_TRUE(command_line2.HasSwitch(kSwitch1)); |
| 405 | EXPECT_FALSE(command_line2.HasSwitch(switches::kFlagSwitchesBegin)); |
| 406 | EXPECT_FALSE(command_line2.HasSwitch(switches::kFlagSwitchesEnd)); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 407 | } |
| 408 | |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 409 | CommandLine::StringType CreateSwitch(const std::string& value) { |
| 410 | #if defined(OS_WIN) |
| 411 | return base::ASCIIToUTF16(value); |
| 412 | #else |
| 413 | return value; |
| 414 | #endif |
| 415 | } |
| 416 | |
[email protected] | 6d98abf | 2013-06-17 23:35:51 | [diff] [blame] | 417 | TEST_F(AboutFlagsTest, CompareSwitchesToCurrentCommandLine) { |
| 418 | SetExperimentEnabled(&flags_storage_, kFlags1, true); |
| 419 | |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 420 | const std::string kDoubleDash("--"); |
| 421 | |
[email protected] | 6d98abf | 2013-06-17 23:35:51 | [diff] [blame] | 422 | CommandLine command_line(CommandLine::NO_PROGRAM); |
| 423 | command_line.AppendSwitch("foo"); |
| 424 | |
| 425 | CommandLine new_command_line(CommandLine::NO_PROGRAM); |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 426 | ConvertFlagsToSwitches(&flags_storage_, &new_command_line, kAddSentinels); |
[email protected] | 6d98abf | 2013-06-17 23:35:51 | [diff] [blame] | 427 | |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 428 | EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine( |
| 429 | new_command_line, command_line, NULL)); |
| 430 | { |
| 431 | std::set<CommandLine::StringType> difference; |
| 432 | EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine( |
| 433 | new_command_line, command_line, &difference)); |
| 434 | EXPECT_EQ(1U, difference.size()); |
| 435 | EXPECT_EQ(1U, difference.count(CreateSwitch(kDoubleDash + kSwitch1))); |
| 436 | } |
[email protected] | 6d98abf | 2013-06-17 23:35:51 | [diff] [blame] | 437 | |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 438 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | 6d98abf | 2013-06-17 23:35:51 | [diff] [blame] | 439 | |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 440 | EXPECT_TRUE(AreSwitchesIdenticalToCurrentCommandLine( |
| 441 | new_command_line, command_line, NULL)); |
| 442 | { |
| 443 | std::set<CommandLine::StringType> difference; |
| 444 | EXPECT_TRUE(AreSwitchesIdenticalToCurrentCommandLine( |
| 445 | new_command_line, command_line, &difference)); |
| 446 | EXPECT_TRUE(difference.empty()); |
| 447 | } |
[email protected] | 6d98abf | 2013-06-17 23:35:51 | [diff] [blame] | 448 | |
| 449 | // Now both have flags but different. |
| 450 | SetExperimentEnabled(&flags_storage_, kFlags1, false); |
| 451 | SetExperimentEnabled(&flags_storage_, kFlags2, true); |
| 452 | |
| 453 | CommandLine another_command_line(CommandLine::NO_PROGRAM); |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 454 | ConvertFlagsToSwitches(&flags_storage_, &another_command_line, kAddSentinels); |
[email protected] | 6d98abf | 2013-06-17 23:35:51 | [diff] [blame] | 455 | |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 456 | EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine( |
| 457 | new_command_line, another_command_line, NULL)); |
| 458 | { |
| 459 | std::set<CommandLine::StringType> difference; |
| 460 | EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine( |
| 461 | new_command_line, another_command_line, &difference)); |
| 462 | EXPECT_EQ(2U, difference.size()); |
| 463 | EXPECT_EQ(1U, difference.count(CreateSwitch(kDoubleDash + kSwitch1))); |
| 464 | EXPECT_EQ(1U, |
| 465 | difference.count(CreateSwitch(kDoubleDash + kSwitch2 + "=" + |
| 466 | kValueForSwitch2))); |
| 467 | } |
[email protected] | 6d98abf | 2013-06-17 23:35:51 | [diff] [blame] | 468 | } |
| 469 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 470 | TEST_F(AboutFlagsTest, RemoveFlagSwitches) { |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 471 | std::map<std::string, CommandLine::StringType> switch_list; |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 472 | switch_list[kSwitch1] = CommandLine::StringType(); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 473 | switch_list[switches::kFlagSwitchesBegin] = CommandLine::StringType(); |
| 474 | switch_list[switches::kFlagSwitchesEnd] = CommandLine::StringType(); |
| 475 | switch_list["foo"] = CommandLine::StringType(); |
| 476 | |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 477 | SetExperimentEnabled(&flags_storage_, kFlags1, true); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 478 | |
| 479 | // This shouldn't do anything before ConvertFlagsToSwitches() wasn't called. |
| 480 | RemoveFlagsSwitches(&switch_list); |
| 481 | ASSERT_EQ(4u, switch_list.size()); |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 482 | EXPECT_TRUE(switch_list.find(kSwitch1) != switch_list.end()); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 483 | EXPECT_TRUE(switch_list.find(switches::kFlagSwitchesBegin) != |
| 484 | switch_list.end()); |
| 485 | EXPECT_TRUE(switch_list.find(switches::kFlagSwitchesEnd) != |
| 486 | switch_list.end()); |
| 487 | EXPECT_TRUE(switch_list.find("foo") != switch_list.end()); |
| 488 | |
| 489 | // Call ConvertFlagsToSwitches(), then RemoveFlagsSwitches() again. |
[email protected] | 947446b | 2010-10-21 03:36:31 | [diff] [blame] | 490 | CommandLine command_line(CommandLine::NO_PROGRAM); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 491 | command_line.AppendSwitch("foo"); |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 492 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 493 | RemoveFlagsSwitches(&switch_list); |
| 494 | |
| 495 | // Now the about:flags-related switch should have been removed. |
| 496 | ASSERT_EQ(1u, switch_list.size()); |
| 497 | EXPECT_TRUE(switch_list.find("foo") != switch_list.end()); |
| 498 | } |
| 499 | |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 500 | // Tests enabling experiments that aren't supported on the current platform. |
| 501 | TEST_F(AboutFlagsTest, PersistAndPrune) { |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 502 | // Enable experiments 1 and 3. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 503 | SetExperimentEnabled(&flags_storage_, kFlags1, true); |
| 504 | SetExperimentEnabled(&flags_storage_, kFlags3, true); |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 505 | CommandLine command_line(CommandLine::NO_PROGRAM); |
| 506 | EXPECT_FALSE(command_line.HasSwitch(kSwitch1)); |
| 507 | EXPECT_FALSE(command_line.HasSwitch(kSwitch3)); |
| 508 | |
| 509 | // Convert the flags to switches. Experiment 3 shouldn't be among the switches |
| 510 | // as it is not applicable to the current platform. |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 511 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | cd7fa99f | 2011-09-07 01:24:55 | [diff] [blame] | 512 | EXPECT_TRUE(command_line.HasSwitch(kSwitch1)); |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 513 | EXPECT_FALSE(command_line.HasSwitch(kSwitch3)); |
| 514 | |
| 515 | // Experiment 3 should show still be persisted in preferences though. |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 516 | const base::ListValue* experiments_list = |
[email protected] | ee28495a | 2013-05-20 04:10:52 | [diff] [blame] | 517 | prefs_.GetList(prefs::kEnabledLabsExperiments); |
| 518 | ASSERT_TRUE(experiments_list); |
| 519 | EXPECT_EQ(2U, experiments_list->GetSize()); |
| 520 | std::string s0; |
| 521 | ASSERT_TRUE(experiments_list->GetString(0, &s0)); |
| 522 | EXPECT_EQ(kFlags1, s0); |
| 523 | std::string s1; |
| 524 | ASSERT_TRUE(experiments_list->GetString(1, &s1)); |
| 525 | EXPECT_EQ(kFlags3, s1); |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 526 | } |
| 527 | |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 528 | // Tests that switches which should have values get them in the command |
| 529 | // line. |
| 530 | TEST_F(AboutFlagsTest, CheckValues) { |
| 531 | // Enable experiments 1 and 2. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 532 | SetExperimentEnabled(&flags_storage_, kFlags1, true); |
| 533 | SetExperimentEnabled(&flags_storage_, kFlags2, true); |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 534 | CommandLine command_line(CommandLine::NO_PROGRAM); |
| 535 | EXPECT_FALSE(command_line.HasSwitch(kSwitch1)); |
| 536 | EXPECT_FALSE(command_line.HasSwitch(kSwitch2)); |
| 537 | |
| 538 | // Convert the flags to switches. |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 539 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | cd7fa99f | 2011-09-07 01:24:55 | [diff] [blame] | 540 | EXPECT_TRUE(command_line.HasSwitch(kSwitch1)); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 541 | EXPECT_EQ(std::string(), command_line.GetSwitchValueASCII(kSwitch1)); |
[email protected] | cd7fa99f | 2011-09-07 01:24:55 | [diff] [blame] | 542 | EXPECT_TRUE(command_line.HasSwitch(kSwitch2)); |
| 543 | EXPECT_EQ(std::string(kValueForSwitch2), |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 544 | command_line.GetSwitchValueASCII(kSwitch2)); |
| 545 | |
| 546 | // Confirm that there is no '=' in the command line for simple switches. |
| 547 | std::string switch1_with_equals = std::string("--") + |
| 548 | std::string(kSwitch1) + |
| 549 | std::string("="); |
| 550 | #if defined(OS_WIN) |
| 551 | EXPECT_EQ(std::wstring::npos, |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 552 | command_line.GetCommandLineString().find( |
[email protected] | 670d323 | 2013-12-24 17:58:58 | [diff] [blame] | 553 | base::ASCIIToWide(switch1_with_equals))); |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 554 | #else |
| 555 | EXPECT_EQ(std::string::npos, |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 556 | command_line.GetCommandLineString().find(switch1_with_equals)); |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 557 | #endif |
| 558 | |
| 559 | // And confirm there is a '=' for switches with values. |
| 560 | std::string switch2_with_equals = std::string("--") + |
| 561 | std::string(kSwitch2) + |
| 562 | std::string("="); |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 563 | #if defined(OS_WIN) |
[email protected] | cd7fa99f | 2011-09-07 01:24:55 | [diff] [blame] | 564 | EXPECT_NE(std::wstring::npos, |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 565 | command_line.GetCommandLineString().find( |
[email protected] | 670d323 | 2013-12-24 17:58:58 | [diff] [blame] | 566 | base::ASCIIToWide(switch2_with_equals))); |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 567 | #else |
[email protected] | cd7fa99f | 2011-09-07 01:24:55 | [diff] [blame] | 568 | EXPECT_NE(std::string::npos, |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 569 | command_line.GetCommandLineString().find(switch2_with_equals)); |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 570 | #endif |
| 571 | |
[email protected] | ee28495a | 2013-05-20 04:10:52 | [diff] [blame] | 572 | // And it should persist. |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 573 | const base::ListValue* experiments_list = |
[email protected] | ee28495a | 2013-05-20 04:10:52 | [diff] [blame] | 574 | prefs_.GetList(prefs::kEnabledLabsExperiments); |
| 575 | ASSERT_TRUE(experiments_list); |
| 576 | EXPECT_EQ(2U, experiments_list->GetSize()); |
| 577 | std::string s0; |
| 578 | ASSERT_TRUE(experiments_list->GetString(0, &s0)); |
| 579 | EXPECT_EQ(kFlags1, s0); |
| 580 | std::string s1; |
| 581 | ASSERT_TRUE(experiments_list->GetString(1, &s1)); |
| 582 | EXPECT_EQ(kFlags2, s1); |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 583 | } |
| 584 | |
[email protected] | 28e35af | 2011-02-09 12:56:22 | [diff] [blame] | 585 | // Tests multi-value type experiments. |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 586 | TEST_F(AboutFlagsTest, MultiValues) { |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 587 | const Experiment& experiment = kExperiments[3]; |
| 588 | ASSERT_EQ(kFlags4, experiment.internal_name); |
| 589 | |
[email protected] | 28e35af | 2011-02-09 12:56:22 | [diff] [blame] | 590 | // Initially, the first "deactivated" option of the multi experiment should |
| 591 | // be set. |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 592 | { |
| 593 | CommandLine command_line(CommandLine::NO_PROGRAM); |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 594 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | 28e35af | 2011-02-09 12:56:22 | [diff] [blame] | 595 | EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1)); |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 596 | EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2)); |
| 597 | } |
| 598 | |
[email protected] | 28e35af | 2011-02-09 12:56:22 | [diff] [blame] | 599 | // Enable the 2nd choice of the multi-value. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 600 | SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(2), true); |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 601 | { |
| 602 | CommandLine command_line(CommandLine::NO_PROGRAM); |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 603 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 604 | EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1)); |
[email protected] | cd7fa99f | 2011-09-07 01:24:55 | [diff] [blame] | 605 | EXPECT_TRUE(command_line.HasSwitch(kMultiSwitch2)); |
| 606 | EXPECT_EQ(std::string(kValueForMultiSwitch2), |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 607 | command_line.GetSwitchValueASCII(kMultiSwitch2)); |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | // Disable the multi-value experiment. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 611 | SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(0), true); |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 612 | { |
| 613 | CommandLine command_line(CommandLine::NO_PROGRAM); |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 614 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 615 | EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1)); |
| 616 | EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2)); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | TEST_F(AboutFlagsTest, EnableDisableValues) { |
| 621 | const Experiment& experiment = kExperiments[4]; |
| 622 | ASSERT_EQ(kFlags5, experiment.internal_name); |
| 623 | |
| 624 | // Nothing selected. |
| 625 | { |
| 626 | CommandLine command_line(CommandLine::NO_PROGRAM); |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 627 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 628 | EXPECT_FALSE(command_line.HasSwitch(kSwitch1)); |
| 629 | EXPECT_FALSE(command_line.HasSwitch(kSwitch2)); |
| 630 | } |
| 631 | |
| 632 | // "Enable" option selected. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 633 | SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(1), true); |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 634 | { |
| 635 | CommandLine command_line(CommandLine::NO_PROGRAM); |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 636 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 637 | EXPECT_TRUE(command_line.HasSwitch(kSwitch1)); |
| 638 | EXPECT_FALSE(command_line.HasSwitch(kSwitch2)); |
| 639 | EXPECT_EQ(kEnableDisableValue1, command_line.GetSwitchValueASCII(kSwitch1)); |
| 640 | } |
| 641 | |
| 642 | // "Disable" option selected. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 643 | SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(2), true); |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 644 | { |
| 645 | CommandLine command_line(CommandLine::NO_PROGRAM); |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 646 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 647 | EXPECT_FALSE(command_line.HasSwitch(kSwitch1)); |
| 648 | EXPECT_TRUE(command_line.HasSwitch(kSwitch2)); |
| 649 | EXPECT_EQ(kEnableDisableValue2, command_line.GetSwitchValueASCII(kSwitch2)); |
| 650 | } |
| 651 | |
| 652 | // "Default" option selected, same as nothing selected. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 653 | SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(0), true); |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 654 | { |
| 655 | CommandLine command_line(CommandLine::NO_PROGRAM); |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 656 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 657 | EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1)); |
| 658 | EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2)); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | // Makes sure there are no separators in any of the experiment names. |
| 663 | TEST_F(AboutFlagsTest, NoSeparators) { |
| 664 | testing::SetExperiments(NULL, 0); |
| 665 | size_t count; |
| 666 | const Experiment* experiments = testing::GetExperiments(&count); |
| 667 | for (size_t i = 0; i < count; ++i) { |
| 668 | std::string name = experiments->internal_name; |
| 669 | EXPECT_EQ(std::string::npos, name.find(testing::kMultiSeparator)) << i; |
| 670 | } |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 671 | } |
| 672 | |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 673 | class AboutFlagsHistogramTest : public ::testing::Test { |
| 674 | protected: |
| 675 | // This is a helper function to check that all IDs in enum LoginCustomFlags in |
| 676 | // histograms.xml are unique. |
| 677 | void SetSwitchToHistogramIdMapping(const std::string& switch_name, |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 678 | const Sample switch_histogram_id, |
| 679 | std::map<std::string, Sample>* out_map) { |
| 680 | const std::pair<std::map<std::string, Sample>::iterator, bool> status = |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 681 | out_map->insert(std::make_pair(switch_name, switch_histogram_id)); |
| 682 | if (!status.second) { |
| 683 | EXPECT_TRUE(status.first->second == switch_histogram_id) |
| 684 | << "Duplicate switch '" << switch_name |
| 685 | << "' found in enum 'LoginCustomFlags' in histograms.xml."; |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | // This method generates a hint for the user for what string should be added |
| 690 | // to the enum LoginCustomFlags to make in consistent. |
| 691 | std::string GetHistogramEnumEntryText(const std::string& switch_name, |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 692 | Sample value) { |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 693 | return base::StringPrintf( |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 694 | "<int value=\"%d\" label=\"%s\"/>", value, switch_name.c_str()); |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 695 | } |
| 696 | }; |
| 697 | |
| 698 | TEST_F(AboutFlagsHistogramTest, CheckHistograms) { |
| 699 | base::FilePath histograms_xml_file_path; |
| 700 | ASSERT_TRUE( |
| 701 | PathService::Get(base::DIR_SOURCE_ROOT, &histograms_xml_file_path)); |
| 702 | histograms_xml_file_path = histograms_xml_file_path.AppendASCII("tools") |
| 703 | .AppendASCII("metrics") |
| 704 | .AppendASCII("histograms") |
| 705 | .AppendASCII("histograms.xml"); |
| 706 | |
| 707 | XmlReader histograms_xml; |
| 708 | ASSERT_TRUE(histograms_xml.LoadFile( |
| 709 | FilePathStringTypeToString(histograms_xml_file_path.value()))); |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 710 | std::map<Sample, std::string> login_custom_flags = |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 711 | ReadEnumFromHistogramsXml("LoginCustomFlags", &histograms_xml); |
| 712 | ASSERT_TRUE(login_custom_flags.size()) |
| 713 | << "Error reading enum 'LoginCustomFlags' from histograms.xml."; |
| 714 | |
| 715 | // Build reverse map {switch_name => id} from login_custom_flags. |
| 716 | SwitchToIdMap histograms_xml_switches_ids; |
| 717 | |
| 718 | EXPECT_TRUE(login_custom_flags.count(kBadSwitchFormatHistogramId)) |
| 719 | << "Entry for UMA ID of incorrect command-line flag is not found in " |
| 720 | "histograms.xml enum LoginCustomFlags. " |
| 721 | "Consider adding entry:\n" |
| 722 | << " " << GetHistogramEnumEntryText("BAD_FLAG_FORMAT", 0); |
| 723 | // Check that all LoginCustomFlags entries have correct values. |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 724 | for (std::map<Sample, std::string>::const_iterator it = |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 725 | login_custom_flags.begin(); |
| 726 | it != login_custom_flags.end(); |
| 727 | ++it) { |
| 728 | if (it->first == kBadSwitchFormatHistogramId) { |
| 729 | // Add eror value with empty name. |
| 730 | SetSwitchToHistogramIdMapping( |
| 731 | "", it->first, &histograms_xml_switches_ids); |
| 732 | continue; |
| 733 | } |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 734 | const Sample uma_id = GetSwitchUMAId(it->second); |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 735 | EXPECT_EQ(uma_id, it->first) |
| 736 | << "histograms.xml enum LoginCustomFlags " |
| 737 | "entry '" << it->second << "' has incorrect value=" << it->first |
| 738 | << ", but " << uma_id << " is expected. Consider changing entry to:\n" |
| 739 | << " " << GetHistogramEnumEntryText(it->second, uma_id); |
| 740 | SetSwitchToHistogramIdMapping( |
| 741 | it->second, it->first, &histograms_xml_switches_ids); |
| 742 | } |
| 743 | |
| 744 | // Check that all flags in about_flags.cc have entries in login_custom_flags. |
| 745 | std::set<std::string> all_switches = GetAllSwitchesForTesting(); |
| 746 | for (std::set<std::string>::const_iterator it = all_switches.begin(); |
| 747 | it != all_switches.end(); |
| 748 | ++it) { |
| 749 | // Skip empty placeholders. |
| 750 | if (it->empty()) |
| 751 | continue; |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 752 | const Sample uma_id = GetSwitchUMAId(*it); |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 753 | EXPECT_NE(kBadSwitchFormatHistogramId, uma_id) |
| 754 | << "Command-line switch '" << *it |
| 755 | << "' from about_flags.cc has UMA ID equal to reserved value " |
| 756 | "kBadSwitchFormatHistogramId=" << kBadSwitchFormatHistogramId |
| 757 | << ". Please modify switch name."; |
| 758 | SwitchToIdMap::iterator enum_entry = |
| 759 | histograms_xml_switches_ids.lower_bound(*it); |
| 760 | |
| 761 | // Ignore case here when switch ID is incorrect - it has already been |
| 762 | // reported in the previous loop. |
| 763 | EXPECT_TRUE(enum_entry != histograms_xml_switches_ids.end() && |
| 764 | enum_entry->first == *it) |
| 765 | << "histograms.xml enum LoginCustomFlags doesn't contain switch '" |
| 766 | << *it << "' (value=" << uma_id |
| 767 | << " expected). Consider adding entry:\n" |
| 768 | << " " << GetHistogramEnumEntryText(*it, uma_id); |
| 769 | } |
| 770 | } |
| 771 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 772 | } // namespace about_flags |