[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 | |
asvitkine | 26e4032 | 2015-05-21 21:28:30 | [diff] [blame] | 5 | #include "chrome/browser/about_flags.h" |
| 6 | |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 7 | #include <stdint.h> |
asvitkine | bff63b2 | 2014-11-25 01:03:52 | [diff] [blame] | 8 | #include <utility> |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 9 | |
| 10 | #include "base/files/file_path.h" |
| 11 | #include "base/memory/scoped_vector.h" |
| 12 | #include "base/path_service.h" |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 13 | #include "base/prefs/pref_registry_simple.h" |
[email protected] | e5ba874f | 2013-02-14 17:20:19 | [diff] [blame] | 14 | #include "base/prefs/testing_pref_service.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 15 | #include "base/strings/string_number_conversions.h" |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 16 | #include "base/strings/stringprintf.h" |
[email protected] | 135cb80 | 2013-06-09 16:44:20 | [diff] [blame] | 17 | #include "base/strings/utf_string_conversions.h" |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 18 | #include "base/values.h" |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 19 | #include "chrome/browser/pref_service_flags_storage.h" |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 20 | #include "chrome/common/chrome_switches.h" |
| 21 | #include "chrome/common/pref_names.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 22 | #include "chrome/grit/chromium_strings.h" |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 23 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 24 | #include "third_party/libxml/chromium/libxml_utils.h" |
| 25 | |
asvitkine | 26e4032 | 2015-05-21 21:28:30 | [diff] [blame] | 26 | namespace about_flags { |
| 27 | |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 28 | namespace { |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 29 | |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 30 | const char kFlags1[] = "flag1"; |
| 31 | const char kFlags2[] = "flag2"; |
| 32 | const char kFlags3[] = "flag3"; |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 33 | const char kFlags4[] = "flag4"; |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 34 | const char kFlags5[] = "flag5"; |
flackr | 0a5eb156 | 2015-06-12 18:50:14 | [diff] [blame] | 35 | const char kFlags6[] = "flag6"; |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 36 | |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 37 | const char kSwitch1[] = "switch"; |
| 38 | const char kSwitch2[] = "switch2"; |
| 39 | const char kSwitch3[] = "switch3"; |
flackr | 0a5eb156 | 2015-06-12 18:50:14 | [diff] [blame] | 40 | const char kSwitch6[] = "switch6"; |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 41 | const char kValueForSwitch2[] = "value_for_switch2"; |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 42 | |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 43 | const char kMultiSwitch1[] = "multi_switch1"; |
| 44 | const char kMultiSwitch2[] = "multi_switch2"; |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 45 | const char kValueForMultiSwitch2[] = "value_for_multi_switch2"; |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 46 | |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 47 | const char kEnableDisableValue1[] = "value1"; |
| 48 | const char kEnableDisableValue2[] = "value2"; |
| 49 | |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 50 | typedef base::HistogramBase::Sample Sample; |
| 51 | typedef std::map<std::string, Sample> SwitchToIdMap; |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 52 | |
| 53 | // This is a helper function to the ReadEnumFromHistogramsXml(). |
| 54 | // Extracts single enum (with integer values) from histograms.xml. |
| 55 | // Expects |reader| to point at given enum. |
| 56 | // Returns map { value => label }. |
| 57 | // Returns empty map on error. |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 58 | std::map<Sample, std::string> ParseEnumFromHistogramsXml( |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 59 | const std::string& enum_name, |
| 60 | XmlReader* reader) { |
| 61 | int entries_index = -1; |
| 62 | |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 63 | std::map<Sample, std::string> result; |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 64 | bool success = true; |
| 65 | |
| 66 | while (true) { |
| 67 | const std::string node_name = reader->NodeName(); |
| 68 | if (node_name == "enum" && reader->IsClosingElement()) |
| 69 | break; |
| 70 | |
| 71 | if (node_name == "int") { |
| 72 | ++entries_index; |
| 73 | std::string value_str; |
| 74 | std::string label; |
| 75 | const bool has_value = reader->NodeAttribute("value", &value_str); |
| 76 | const bool has_label = reader->NodeAttribute("label", &label); |
| 77 | if (!has_value) { |
| 78 | ADD_FAILURE() << "Bad " << enum_name << " enum entry (at index " |
| 79 | << entries_index << ", label='" << label |
| 80 | << "'): No 'value' attribute."; |
| 81 | success = false; |
| 82 | } |
| 83 | if (!has_label) { |
| 84 | ADD_FAILURE() << "Bad " << enum_name << " enum entry (at index " |
| 85 | << entries_index << ", value_str='" << value_str |
| 86 | << "'): No 'label' attribute."; |
| 87 | success = false; |
| 88 | } |
| 89 | |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 90 | Sample value; |
| 91 | if (has_value && !base::StringToInt(value_str, &value)) { |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 92 | ADD_FAILURE() << "Bad " << enum_name << " enum entry (at index " |
| 93 | << entries_index << ", label='" << label |
| 94 | << "', value_str='" << value_str |
| 95 | << "'): 'value' attribute is not integer."; |
| 96 | success = false; |
| 97 | } |
| 98 | if (result.count(value)) { |
| 99 | ADD_FAILURE() << "Bad " << enum_name << " enum entry (at index " |
| 100 | << entries_index << ", label='" << label |
| 101 | << "', value_str='" << value_str |
| 102 | << "'): duplicate value '" << value_str |
| 103 | << "' found in enum. The previous one has label='" |
| 104 | << result[value] << "'."; |
| 105 | success = false; |
| 106 | } |
| 107 | if (success) { |
| 108 | result[value] = label; |
| 109 | } |
| 110 | } |
| 111 | // All enum entries are on the same level, so it is enough to iterate |
| 112 | // until possible. |
| 113 | reader->Next(); |
| 114 | } |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 115 | return (success ? result : std::map<Sample, std::string>()); |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // Find and read given enum (with integer values) from histograms.xml. |
| 119 | // |enum_name| - enum name. |
| 120 | // |histograms_xml| - must be loaded histograms.xml file. |
| 121 | // |
| 122 | // Returns map { value => label } so that: |
| 123 | // <int value="9" label="enable-pinch-virtual-viewport"/> |
| 124 | // becomes: |
| 125 | // { 9 => "enable-pinch-virtual-viewport" } |
| 126 | // Returns empty map on error. |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 127 | std::map<Sample, std::string> ReadEnumFromHistogramsXml( |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 128 | const std::string& enum_name, |
| 129 | XmlReader* histograms_xml) { |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 130 | std::map<Sample, std::string> login_custom_flags; |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 131 | |
| 132 | // Implement simple depth first search. |
| 133 | while (true) { |
| 134 | const std::string node_name = histograms_xml->NodeName(); |
| 135 | if (node_name == "enum") { |
| 136 | std::string name; |
| 137 | if (histograms_xml->NodeAttribute("name", &name) && name == enum_name) { |
| 138 | if (!login_custom_flags.empty()) { |
| 139 | EXPECT_TRUE(login_custom_flags.empty()) |
| 140 | << "Duplicate enum '" << enum_name << "' found in histograms.xml"; |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 141 | return std::map<Sample, std::string>(); |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | const bool got_into_enum = histograms_xml->Read(); |
| 145 | if (got_into_enum) { |
| 146 | login_custom_flags = |
| 147 | ParseEnumFromHistogramsXml(enum_name, histograms_xml); |
| 148 | EXPECT_FALSE(login_custom_flags.empty()) |
| 149 | << "Bad enum '" << enum_name |
| 150 | << "' found in histograms.xml (format error)."; |
| 151 | } else { |
| 152 | EXPECT_TRUE(got_into_enum) |
| 153 | << "Bad enum '" << enum_name |
| 154 | << "' (looks empty) found in histograms.xml."; |
| 155 | } |
| 156 | if (login_custom_flags.empty()) |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 157 | return std::map<Sample, std::string>(); |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | // Go deeper if possible (stops at the closing tag of the deepest node). |
| 161 | if (histograms_xml->Read()) |
| 162 | continue; |
| 163 | |
| 164 | // Try next node on the same level (skips closing tag). |
| 165 | if (histograms_xml->Next()) |
| 166 | continue; |
| 167 | |
| 168 | // Go up until next node on the same level exists. |
| 169 | while (histograms_xml->Depth() && !histograms_xml->SkipToElement()) { |
| 170 | } |
| 171 | |
| 172 | // Reached top. histograms.xml consists of the single top level node |
| 173 | // 'histogram-configuration', so this is the end. |
| 174 | if (!histograms_xml->Depth()) |
| 175 | break; |
| 176 | } |
| 177 | EXPECT_FALSE(login_custom_flags.empty()) |
| 178 | << "Enum '" << enum_name << "' is not found in histograms.xml."; |
| 179 | return login_custom_flags; |
| 180 | } |
| 181 | |
| 182 | std::string FilePathStringTypeToString(const base::FilePath::StringType& path) { |
| 183 | #if defined(OS_WIN) |
| 184 | return base::UTF16ToUTF8(path); |
| 185 | #else |
| 186 | return path; |
| 187 | #endif |
| 188 | } |
| 189 | |
| 190 | std::set<std::string> GetAllSwitchesForTesting() { |
| 191 | std::set<std::string> result; |
| 192 | |
| 193 | size_t num_experiments = 0; |
asvitkine | 26e4032 | 2015-05-21 21:28:30 | [diff] [blame] | 194 | const Experiment* experiments = |
| 195 | testing::GetExperiments(&num_experiments); |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 196 | |
| 197 | for (size_t i = 0; i < num_experiments; ++i) { |
asvitkine | 26e4032 | 2015-05-21 21:28:30 | [diff] [blame] | 198 | const Experiment& experiment = experiments[i]; |
flackr | 0a5eb156 | 2015-06-12 18:50:14 | [diff] [blame] | 199 | if (experiment.type == Experiment::SINGLE_VALUE || |
| 200 | experiment.type == Experiment::SINGLE_DISABLE_VALUE) { |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 201 | result.insert(experiment.command_line_switch); |
asvitkine | 26e4032 | 2015-05-21 21:28:30 | [diff] [blame] | 202 | } else if (experiment.type == Experiment::MULTI_VALUE) { |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 203 | for (int j = 0; j < experiment.num_choices; ++j) { |
| 204 | result.insert(experiment.choices[j].command_line_switch); |
| 205 | } |
| 206 | } else { |
asvitkine | 26e4032 | 2015-05-21 21:28:30 | [diff] [blame] | 207 | DCHECK_EQ(experiment.type, Experiment::ENABLE_DISABLE_VALUE); |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 208 | result.insert(experiment.command_line_switch); |
| 209 | result.insert(experiment.disable_command_line_switch); |
| 210 | } |
| 211 | } |
| 212 | return result; |
| 213 | } |
| 214 | |
| 215 | } // anonymous namespace |
| 216 | |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 217 | const Experiment::Choice kMultiChoices[] = { |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 218 | { IDS_PRODUCT_NAME, "", "" }, |
| 219 | { IDS_PRODUCT_NAME, kMultiSwitch1, "" }, |
| 220 | { IDS_PRODUCT_NAME, kMultiSwitch2, kValueForMultiSwitch2 }, |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | // The experiments that are set for these tests. The 3rd experiment is not |
| 224 | // supported on the current platform, all others are. |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 225 | static Experiment kExperiments[] = { |
| 226 | { |
| 227 | kFlags1, |
| 228 | IDS_PRODUCT_NAME, |
| 229 | IDS_PRODUCT_NAME, |
| 230 | 0, // Ends up being mapped to the current platform. |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 231 | Experiment::SINGLE_VALUE, |
| 232 | kSwitch1, |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 233 | "", |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 234 | NULL, |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 235 | NULL, |
| 236 | NULL, |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 237 | 0 |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 238 | }, |
| 239 | { |
| 240 | kFlags2, |
| 241 | IDS_PRODUCT_NAME, |
| 242 | IDS_PRODUCT_NAME, |
| 243 | 0, // Ends up being mapped to the current platform. |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 244 | Experiment::SINGLE_VALUE, |
| 245 | kSwitch2, |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 246 | kValueForSwitch2, |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 247 | NULL, |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 248 | NULL, |
| 249 | NULL, |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 250 | 0 |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 251 | }, |
| 252 | { |
| 253 | kFlags3, |
| 254 | IDS_PRODUCT_NAME, |
| 255 | IDS_PRODUCT_NAME, |
| 256 | 0, // This ends up enabling for an OS other than the current. |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 257 | Experiment::SINGLE_VALUE, |
| 258 | kSwitch3, |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 259 | "", |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 260 | NULL, |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 261 | NULL, |
| 262 | NULL, |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 263 | 0 |
| 264 | }, |
| 265 | { |
| 266 | kFlags4, |
| 267 | IDS_PRODUCT_NAME, |
| 268 | IDS_PRODUCT_NAME, |
| 269 | 0, // Ends up being mapped to the current platform. |
| 270 | Experiment::MULTI_VALUE, |
| 271 | "", |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 272 | "", |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 273 | "", |
| 274 | "", |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 275 | kMultiChoices, |
| 276 | arraysize(kMultiChoices) |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 277 | }, |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 278 | { |
| 279 | kFlags5, |
| 280 | IDS_PRODUCT_NAME, |
| 281 | IDS_PRODUCT_NAME, |
| 282 | 0, // Ends up being mapped to the current platform. |
| 283 | Experiment::ENABLE_DISABLE_VALUE, |
| 284 | kSwitch1, |
| 285 | kEnableDisableValue1, |
| 286 | kSwitch2, |
| 287 | kEnableDisableValue2, |
| 288 | NULL, |
| 289 | 3 |
| 290 | }, |
flackr | 0a5eb156 | 2015-06-12 18:50:14 | [diff] [blame] | 291 | { |
| 292 | kFlags6, |
| 293 | IDS_PRODUCT_NAME, |
| 294 | IDS_PRODUCT_NAME, |
| 295 | 0, |
| 296 | Experiment::SINGLE_DISABLE_VALUE, |
| 297 | kSwitch6, |
| 298 | "", |
| 299 | NULL, |
| 300 | NULL, |
| 301 | NULL, |
| 302 | 0 |
| 303 | }, |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 304 | }; |
| 305 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 306 | class AboutFlagsTest : public ::testing::Test { |
| 307 | protected: |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 308 | AboutFlagsTest() : flags_storage_(&prefs_) { |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 309 | prefs_.registry()->RegisterListPref(prefs::kEnabledLabsExperiments); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 310 | testing::ClearState(); |
| 311 | } |
| 312 | |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 313 | void SetUp() override { |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 314 | for (size_t i = 0; i < arraysize(kExperiments); ++i) |
| 315 | kExperiments[i].supported_platforms = GetCurrentPlatform(); |
| 316 | |
| 317 | int os_other_than_current = 1; |
| 318 | while (os_other_than_current == GetCurrentPlatform()) |
| 319 | os_other_than_current <<= 1; |
| 320 | kExperiments[2].supported_platforms = os_other_than_current; |
| 321 | |
| 322 | testing::SetExperiments(kExperiments, arraysize(kExperiments)); |
| 323 | } |
| 324 | |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 325 | void TearDown() override { testing::SetExperiments(NULL, 0); } |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 326 | |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 327 | TestingPrefServiceSimple prefs_; |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 328 | PrefServiceFlagsStorage flags_storage_; |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 329 | }; |
| 330 | |
[email protected] | 147492b | 2013-03-19 23:52:08 | [diff] [blame] | 331 | |
| 332 | TEST_F(AboutFlagsTest, NoChangeNoRestart) { |
| 333 | EXPECT_FALSE(IsRestartNeededToCommitChanges()); |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 334 | SetExperimentEnabled(&flags_storage_, kFlags1, false); |
[email protected] | 147492b | 2013-03-19 23:52:08 | [diff] [blame] | 335 | EXPECT_FALSE(IsRestartNeededToCommitChanges()); |
flackr | 0a5eb156 | 2015-06-12 18:50:14 | [diff] [blame] | 336 | |
| 337 | // kFlags6 is enabled by default, so enabling should not require a restart. |
| 338 | SetExperimentEnabled(&flags_storage_, kFlags6, true); |
| 339 | EXPECT_FALSE(IsRestartNeededToCommitChanges()); |
[email protected] | 147492b | 2013-03-19 23:52:08 | [diff] [blame] | 340 | } |
| 341 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 342 | TEST_F(AboutFlagsTest, ChangeNeedsRestart) { |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 343 | EXPECT_FALSE(IsRestartNeededToCommitChanges()); |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 344 | SetExperimentEnabled(&flags_storage_, kFlags1, true); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 345 | EXPECT_TRUE(IsRestartNeededToCommitChanges()); |
| 346 | } |
| 347 | |
flackr | 0a5eb156 | 2015-06-12 18:50:14 | [diff] [blame] | 348 | // Tests that disabling a default enabled experiment requires a restart. |
| 349 | TEST_F(AboutFlagsTest, DisableChangeNeedsRestart) { |
| 350 | EXPECT_FALSE(IsRestartNeededToCommitChanges()); |
| 351 | SetExperimentEnabled(&flags_storage_, kFlags6, false); |
| 352 | EXPECT_TRUE(IsRestartNeededToCommitChanges()); |
| 353 | } |
| 354 | |
[email protected] | 147492b | 2013-03-19 23:52:08 | [diff] [blame] | 355 | TEST_F(AboutFlagsTest, MultiFlagChangeNeedsRestart) { |
| 356 | const Experiment& experiment = kExperiments[3]; |
| 357 | ASSERT_EQ(kFlags4, experiment.internal_name); |
| 358 | EXPECT_FALSE(IsRestartNeededToCommitChanges()); |
| 359 | // Enable the 2nd choice of the multi-value. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 360 | SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(2), true); |
[email protected] | 147492b | 2013-03-19 23:52:08 | [diff] [blame] | 361 | EXPECT_TRUE(IsRestartNeededToCommitChanges()); |
| 362 | testing::ClearState(); |
| 363 | EXPECT_FALSE(IsRestartNeededToCommitChanges()); |
| 364 | // Enable the default choice now. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 365 | SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(0), true); |
[email protected] | 147492b | 2013-03-19 23:52:08 | [diff] [blame] | 366 | EXPECT_TRUE(IsRestartNeededToCommitChanges()); |
| 367 | } |
| 368 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 369 | TEST_F(AboutFlagsTest, AddTwoFlagsRemoveOne) { |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 370 | // Add two experiments, check they're there. |
[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] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 373 | |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 374 | const base::ListValue* experiments_list = prefs_.GetList( |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 375 | prefs::kEnabledLabsExperiments); |
| 376 | ASSERT_TRUE(experiments_list != NULL); |
| 377 | |
| 378 | ASSERT_EQ(2u, experiments_list->GetSize()); |
| 379 | |
| 380 | std::string s0; |
| 381 | ASSERT_TRUE(experiments_list->GetString(0, &s0)); |
| 382 | std::string s1; |
| 383 | ASSERT_TRUE(experiments_list->GetString(1, &s1)); |
| 384 | |
| 385 | EXPECT_TRUE(s0 == kFlags1 || s1 == kFlags1); |
| 386 | EXPECT_TRUE(s0 == kFlags2 || s1 == kFlags2); |
| 387 | |
| 388 | // Remove one experiment, check the other's still around. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 389 | SetExperimentEnabled(&flags_storage_, kFlags2, false); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 390 | |
[email protected] | 1bc7842 | 2011-03-31 08:41:38 | [diff] [blame] | 391 | experiments_list = prefs_.GetList(prefs::kEnabledLabsExperiments); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 392 | ASSERT_TRUE(experiments_list != NULL); |
| 393 | ASSERT_EQ(1u, experiments_list->GetSize()); |
| 394 | ASSERT_TRUE(experiments_list->GetString(0, &s0)); |
| 395 | EXPECT_TRUE(s0 == kFlags1); |
| 396 | } |
| 397 | |
| 398 | TEST_F(AboutFlagsTest, AddTwoFlagsRemoveBoth) { |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 399 | // Add two experiments, check the pref exists. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 400 | SetExperimentEnabled(&flags_storage_, kFlags1, true); |
| 401 | SetExperimentEnabled(&flags_storage_, kFlags2, true); |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 402 | const base::ListValue* experiments_list = prefs_.GetList( |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 403 | prefs::kEnabledLabsExperiments); |
| 404 | ASSERT_TRUE(experiments_list != NULL); |
| 405 | |
| 406 | // Remove both, the pref should have been removed completely. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 407 | SetExperimentEnabled(&flags_storage_, kFlags1, false); |
| 408 | SetExperimentEnabled(&flags_storage_, kFlags2, false); |
[email protected] | 1bc7842 | 2011-03-31 08:41:38 | [diff] [blame] | 409 | experiments_list = prefs_.GetList(prefs::kEnabledLabsExperiments); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 410 | EXPECT_TRUE(experiments_list == NULL || experiments_list->GetSize() == 0); |
| 411 | } |
| 412 | |
| 413 | TEST_F(AboutFlagsTest, ConvertFlagsToSwitches) { |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 414 | SetExperimentEnabled(&flags_storage_, kFlags1, true); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 415 | |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 416 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 417 | command_line.AppendSwitch("foo"); |
| 418 | |
| 419 | EXPECT_TRUE(command_line.HasSwitch("foo")); |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 420 | EXPECT_FALSE(command_line.HasSwitch(kSwitch1)); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 421 | |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 422 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 423 | |
| 424 | EXPECT_TRUE(command_line.HasSwitch("foo")); |
[email protected] | cd7fa99f | 2011-09-07 01:24:55 | [diff] [blame] | 425 | EXPECT_TRUE(command_line.HasSwitch(kSwitch1)); |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 426 | EXPECT_TRUE(command_line.HasSwitch(switches::kFlagSwitchesBegin)); |
| 427 | EXPECT_TRUE(command_line.HasSwitch(switches::kFlagSwitchesEnd)); |
| 428 | |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 429 | base::CommandLine command_line2(base::CommandLine::NO_PROGRAM); |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 430 | |
| 431 | ConvertFlagsToSwitches(&flags_storage_, &command_line2, kNoSentinels); |
| 432 | |
| 433 | EXPECT_TRUE(command_line2.HasSwitch(kSwitch1)); |
| 434 | EXPECT_FALSE(command_line2.HasSwitch(switches::kFlagSwitchesBegin)); |
| 435 | EXPECT_FALSE(command_line2.HasSwitch(switches::kFlagSwitchesEnd)); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 436 | } |
| 437 | |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 438 | base::CommandLine::StringType CreateSwitch(const std::string& value) { |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 439 | #if defined(OS_WIN) |
| 440 | return base::ASCIIToUTF16(value); |
| 441 | #else |
| 442 | return value; |
| 443 | #endif |
| 444 | } |
| 445 | |
[email protected] | 6d98abf | 2013-06-17 23:35:51 | [diff] [blame] | 446 | TEST_F(AboutFlagsTest, CompareSwitchesToCurrentCommandLine) { |
| 447 | SetExperimentEnabled(&flags_storage_, kFlags1, true); |
| 448 | |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 449 | const std::string kDoubleDash("--"); |
| 450 | |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 451 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
[email protected] | 6d98abf | 2013-06-17 23:35:51 | [diff] [blame] | 452 | command_line.AppendSwitch("foo"); |
| 453 | |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 454 | base::CommandLine new_command_line(base::CommandLine::NO_PROGRAM); |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 455 | ConvertFlagsToSwitches(&flags_storage_, &new_command_line, kAddSentinels); |
[email protected] | 6d98abf | 2013-06-17 23:35:51 | [diff] [blame] | 456 | |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 457 | EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine( |
| 458 | new_command_line, command_line, NULL)); |
| 459 | { |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 460 | std::set<base::CommandLine::StringType> difference; |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 461 | EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine( |
| 462 | new_command_line, command_line, &difference)); |
| 463 | EXPECT_EQ(1U, difference.size()); |
| 464 | EXPECT_EQ(1U, difference.count(CreateSwitch(kDoubleDash + kSwitch1))); |
| 465 | } |
[email protected] | 6d98abf | 2013-06-17 23:35:51 | [diff] [blame] | 466 | |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 467 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | 6d98abf | 2013-06-17 23:35:51 | [diff] [blame] | 468 | |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 469 | EXPECT_TRUE(AreSwitchesIdenticalToCurrentCommandLine( |
| 470 | new_command_line, command_line, NULL)); |
| 471 | { |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 472 | std::set<base::CommandLine::StringType> difference; |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 473 | EXPECT_TRUE(AreSwitchesIdenticalToCurrentCommandLine( |
| 474 | new_command_line, command_line, &difference)); |
| 475 | EXPECT_TRUE(difference.empty()); |
| 476 | } |
[email protected] | 6d98abf | 2013-06-17 23:35:51 | [diff] [blame] | 477 | |
| 478 | // Now both have flags but different. |
| 479 | SetExperimentEnabled(&flags_storage_, kFlags1, false); |
| 480 | SetExperimentEnabled(&flags_storage_, kFlags2, true); |
| 481 | |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 482 | base::CommandLine another_command_line(base::CommandLine::NO_PROGRAM); |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 483 | ConvertFlagsToSwitches(&flags_storage_, &another_command_line, kAddSentinels); |
[email protected] | 6d98abf | 2013-06-17 23:35:51 | [diff] [blame] | 484 | |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 485 | EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine( |
| 486 | new_command_line, another_command_line, NULL)); |
| 487 | { |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 488 | std::set<base::CommandLine::StringType> difference; |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 489 | EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine( |
| 490 | new_command_line, another_command_line, &difference)); |
| 491 | EXPECT_EQ(2U, difference.size()); |
| 492 | EXPECT_EQ(1U, difference.count(CreateSwitch(kDoubleDash + kSwitch1))); |
| 493 | EXPECT_EQ(1U, |
| 494 | difference.count(CreateSwitch(kDoubleDash + kSwitch2 + "=" + |
| 495 | kValueForSwitch2))); |
| 496 | } |
[email protected] | 6d98abf | 2013-06-17 23:35:51 | [diff] [blame] | 497 | } |
| 498 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 499 | TEST_F(AboutFlagsTest, RemoveFlagSwitches) { |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 500 | std::map<std::string, base::CommandLine::StringType> switch_list; |
| 501 | switch_list[kSwitch1] = base::CommandLine::StringType(); |
| 502 | switch_list[switches::kFlagSwitchesBegin] = base::CommandLine::StringType(); |
| 503 | switch_list[switches::kFlagSwitchesEnd] = base::CommandLine::StringType(); |
| 504 | switch_list["foo"] = base::CommandLine::StringType(); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 505 | |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 506 | SetExperimentEnabled(&flags_storage_, kFlags1, true); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 507 | |
| 508 | // This shouldn't do anything before ConvertFlagsToSwitches() wasn't called. |
| 509 | RemoveFlagsSwitches(&switch_list); |
| 510 | ASSERT_EQ(4u, switch_list.size()); |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 511 | EXPECT_TRUE(switch_list.find(kSwitch1) != switch_list.end()); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 512 | EXPECT_TRUE(switch_list.find(switches::kFlagSwitchesBegin) != |
| 513 | switch_list.end()); |
| 514 | EXPECT_TRUE(switch_list.find(switches::kFlagSwitchesEnd) != |
| 515 | switch_list.end()); |
| 516 | EXPECT_TRUE(switch_list.find("foo") != switch_list.end()); |
| 517 | |
| 518 | // Call ConvertFlagsToSwitches(), then RemoveFlagsSwitches() again. |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 519 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 520 | command_line.AppendSwitch("foo"); |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 521 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 522 | RemoveFlagsSwitches(&switch_list); |
| 523 | |
| 524 | // Now the about:flags-related switch should have been removed. |
| 525 | ASSERT_EQ(1u, switch_list.size()); |
| 526 | EXPECT_TRUE(switch_list.find("foo") != switch_list.end()); |
| 527 | } |
| 528 | |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 529 | // Tests enabling experiments that aren't supported on the current platform. |
| 530 | TEST_F(AboutFlagsTest, PersistAndPrune) { |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 531 | // Enable experiments 1 and 3. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 532 | SetExperimentEnabled(&flags_storage_, kFlags1, true); |
| 533 | SetExperimentEnabled(&flags_storage_, kFlags3, true); |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 534 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 535 | EXPECT_FALSE(command_line.HasSwitch(kSwitch1)); |
| 536 | EXPECT_FALSE(command_line.HasSwitch(kSwitch3)); |
| 537 | |
| 538 | // Convert the flags to switches. Experiment 3 shouldn't be among the switches |
| 539 | // as it is not applicable to the current platform. |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 540 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | cd7fa99f | 2011-09-07 01:24:55 | [diff] [blame] | 541 | EXPECT_TRUE(command_line.HasSwitch(kSwitch1)); |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 542 | EXPECT_FALSE(command_line.HasSwitch(kSwitch3)); |
| 543 | |
| 544 | // Experiment 3 should show still be persisted in preferences though. |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 545 | const base::ListValue* experiments_list = |
[email protected] | ee28495a | 2013-05-20 04:10:52 | [diff] [blame] | 546 | prefs_.GetList(prefs::kEnabledLabsExperiments); |
| 547 | ASSERT_TRUE(experiments_list); |
| 548 | EXPECT_EQ(2U, experiments_list->GetSize()); |
| 549 | std::string s0; |
| 550 | ASSERT_TRUE(experiments_list->GetString(0, &s0)); |
| 551 | EXPECT_EQ(kFlags1, s0); |
| 552 | std::string s1; |
| 553 | ASSERT_TRUE(experiments_list->GetString(1, &s1)); |
| 554 | EXPECT_EQ(kFlags3, s1); |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 555 | } |
| 556 | |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 557 | // Tests that switches which should have values get them in the command |
| 558 | // line. |
| 559 | TEST_F(AboutFlagsTest, CheckValues) { |
| 560 | // Enable experiments 1 and 2. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 561 | SetExperimentEnabled(&flags_storage_, kFlags1, true); |
| 562 | SetExperimentEnabled(&flags_storage_, kFlags2, true); |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 563 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 564 | EXPECT_FALSE(command_line.HasSwitch(kSwitch1)); |
| 565 | EXPECT_FALSE(command_line.HasSwitch(kSwitch2)); |
| 566 | |
| 567 | // Convert the flags to switches. |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 568 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | cd7fa99f | 2011-09-07 01:24:55 | [diff] [blame] | 569 | EXPECT_TRUE(command_line.HasSwitch(kSwitch1)); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 570 | EXPECT_EQ(std::string(), command_line.GetSwitchValueASCII(kSwitch1)); |
[email protected] | cd7fa99f | 2011-09-07 01:24:55 | [diff] [blame] | 571 | EXPECT_TRUE(command_line.HasSwitch(kSwitch2)); |
| 572 | EXPECT_EQ(std::string(kValueForSwitch2), |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 573 | command_line.GetSwitchValueASCII(kSwitch2)); |
| 574 | |
| 575 | // Confirm that there is no '=' in the command line for simple switches. |
| 576 | std::string switch1_with_equals = std::string("--") + |
| 577 | std::string(kSwitch1) + |
| 578 | std::string("="); |
| 579 | #if defined(OS_WIN) |
thestig | 11bf74d | 2014-11-24 20:14:42 | [diff] [blame] | 580 | EXPECT_EQ(base::string16::npos, |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 581 | command_line.GetCommandLineString().find( |
thestig | 11bf74d | 2014-11-24 20:14:42 | [diff] [blame] | 582 | base::ASCIIToUTF16(switch1_with_equals))); |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 583 | #else |
| 584 | EXPECT_EQ(std::string::npos, |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 585 | command_line.GetCommandLineString().find(switch1_with_equals)); |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 586 | #endif |
| 587 | |
| 588 | // And confirm there is a '=' for switches with values. |
| 589 | std::string switch2_with_equals = std::string("--") + |
| 590 | std::string(kSwitch2) + |
| 591 | std::string("="); |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 592 | #if defined(OS_WIN) |
thestig | 11bf74d | 2014-11-24 20:14:42 | [diff] [blame] | 593 | EXPECT_NE(base::string16::npos, |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 594 | command_line.GetCommandLineString().find( |
thestig | 11bf74d | 2014-11-24 20:14:42 | [diff] [blame] | 595 | base::ASCIIToUTF16(switch2_with_equals))); |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 596 | #else |
[email protected] | cd7fa99f | 2011-09-07 01:24:55 | [diff] [blame] | 597 | EXPECT_NE(std::string::npos, |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 598 | command_line.GetCommandLineString().find(switch2_with_equals)); |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 599 | #endif |
| 600 | |
[email protected] | ee28495a | 2013-05-20 04:10:52 | [diff] [blame] | 601 | // And it should persist. |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 602 | const base::ListValue* experiments_list = |
[email protected] | ee28495a | 2013-05-20 04:10:52 | [diff] [blame] | 603 | prefs_.GetList(prefs::kEnabledLabsExperiments); |
| 604 | ASSERT_TRUE(experiments_list); |
| 605 | EXPECT_EQ(2U, experiments_list->GetSize()); |
| 606 | std::string s0; |
| 607 | ASSERT_TRUE(experiments_list->GetString(0, &s0)); |
| 608 | EXPECT_EQ(kFlags1, s0); |
| 609 | std::string s1; |
| 610 | ASSERT_TRUE(experiments_list->GetString(1, &s1)); |
| 611 | EXPECT_EQ(kFlags2, s1); |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 612 | } |
| 613 | |
[email protected] | 28e35af | 2011-02-09 12:56:22 | [diff] [blame] | 614 | // Tests multi-value type experiments. |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 615 | TEST_F(AboutFlagsTest, MultiValues) { |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 616 | const Experiment& experiment = kExperiments[3]; |
| 617 | ASSERT_EQ(kFlags4, experiment.internal_name); |
| 618 | |
[email protected] | 28e35af | 2011-02-09 12:56:22 | [diff] [blame] | 619 | // Initially, the first "deactivated" option of the multi experiment should |
| 620 | // be set. |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 621 | { |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 622 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 623 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | 28e35af | 2011-02-09 12:56:22 | [diff] [blame] | 624 | EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1)); |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 625 | EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2)); |
| 626 | } |
| 627 | |
[email protected] | 28e35af | 2011-02-09 12:56:22 | [diff] [blame] | 628 | // Enable the 2nd choice of the multi-value. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 629 | SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(2), true); |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 630 | { |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 631 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 632 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 633 | EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1)); |
[email protected] | cd7fa99f | 2011-09-07 01:24:55 | [diff] [blame] | 634 | EXPECT_TRUE(command_line.HasSwitch(kMultiSwitch2)); |
| 635 | EXPECT_EQ(std::string(kValueForMultiSwitch2), |
[email protected] | a8274453 | 2011-02-11 16:15:53 | [diff] [blame] | 636 | command_line.GetSwitchValueASCII(kMultiSwitch2)); |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | // Disable the multi-value experiment. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 640 | SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(0), true); |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 641 | { |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 642 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 643 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 644 | EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1)); |
| 645 | EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2)); |
| 646 | } |
| 647 | } |
| 648 | |
flackr | 0a5eb156 | 2015-06-12 18:50:14 | [diff] [blame] | 649 | // Tests that disable flags are added when an experiment is disabled. |
| 650 | TEST_F(AboutFlagsTest, DisableFlagCommandLine) { |
| 651 | // Nothing selected. |
| 652 | { |
| 653 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 654 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
| 655 | EXPECT_FALSE(command_line.HasSwitch(kSwitch6)); |
| 656 | } |
| 657 | |
| 658 | // Disable the experiment 6. |
| 659 | SetExperimentEnabled(&flags_storage_, kFlags6, false); |
| 660 | { |
| 661 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 662 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
| 663 | EXPECT_TRUE(command_line.HasSwitch(kSwitch6)); |
| 664 | } |
| 665 | |
| 666 | // Enable experiment 6. |
| 667 | SetExperimentEnabled(&flags_storage_, kFlags6, true); |
| 668 | { |
| 669 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 670 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
| 671 | EXPECT_FALSE(command_line.HasSwitch(kSwitch6)); |
| 672 | } |
| 673 | } |
| 674 | |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 675 | TEST_F(AboutFlagsTest, EnableDisableValues) { |
| 676 | const Experiment& experiment = kExperiments[4]; |
| 677 | ASSERT_EQ(kFlags5, experiment.internal_name); |
| 678 | |
| 679 | // Nothing selected. |
| 680 | { |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 681 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 682 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 683 | EXPECT_FALSE(command_line.HasSwitch(kSwitch1)); |
| 684 | EXPECT_FALSE(command_line.HasSwitch(kSwitch2)); |
| 685 | } |
| 686 | |
| 687 | // "Enable" option selected. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 688 | SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(1), true); |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 689 | { |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 690 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 691 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 692 | EXPECT_TRUE(command_line.HasSwitch(kSwitch1)); |
| 693 | EXPECT_FALSE(command_line.HasSwitch(kSwitch2)); |
| 694 | EXPECT_EQ(kEnableDisableValue1, command_line.GetSwitchValueASCII(kSwitch1)); |
| 695 | } |
| 696 | |
| 697 | // "Disable" option selected. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 698 | SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(2), true); |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 699 | { |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 700 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 701 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | 83e9fa70 | 2013-02-25 19:30:44 | [diff] [blame] | 702 | EXPECT_FALSE(command_line.HasSwitch(kSwitch1)); |
| 703 | EXPECT_TRUE(command_line.HasSwitch(kSwitch2)); |
| 704 | EXPECT_EQ(kEnableDisableValue2, command_line.GetSwitchValueASCII(kSwitch2)); |
| 705 | } |
| 706 | |
| 707 | // "Default" option selected, same as nothing selected. |
[email protected] | e6d1c4f | 2013-06-12 17:37:40 | [diff] [blame] | 708 | SetExperimentEnabled(&flags_storage_, experiment.NameForChoice(0), true); |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 709 | { |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 710 | base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
[email protected] | 578f209 | 2013-09-16 17:16:21 | [diff] [blame] | 711 | ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
[email protected] | 8a6ff28d | 2010-12-02 16:35:19 | [diff] [blame] | 712 | EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1)); |
| 713 | EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2)); |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | // Makes sure there are no separators in any of the experiment names. |
| 718 | TEST_F(AboutFlagsTest, NoSeparators) { |
| 719 | testing::SetExperiments(NULL, 0); |
| 720 | size_t count; |
| 721 | const Experiment* experiments = testing::GetExperiments(&count); |
| 722 | for (size_t i = 0; i < count; ++i) { |
| 723 | std::string name = experiments->internal_name; |
| 724 | EXPECT_EQ(std::string::npos, name.find(testing::kMultiSeparator)) << i; |
| 725 | } |
[email protected] | a314ee5a | 2010-10-26 21:23:28 | [diff] [blame] | 726 | } |
| 727 | |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 728 | class AboutFlagsHistogramTest : public ::testing::Test { |
| 729 | protected: |
| 730 | // This is a helper function to check that all IDs in enum LoginCustomFlags in |
| 731 | // histograms.xml are unique. |
| 732 | void SetSwitchToHistogramIdMapping(const std::string& switch_name, |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 733 | const Sample switch_histogram_id, |
| 734 | std::map<std::string, Sample>* out_map) { |
| 735 | const std::pair<std::map<std::string, Sample>::iterator, bool> status = |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 736 | out_map->insert(std::make_pair(switch_name, switch_histogram_id)); |
| 737 | if (!status.second) { |
| 738 | EXPECT_TRUE(status.first->second == switch_histogram_id) |
| 739 | << "Duplicate switch '" << switch_name |
| 740 | << "' found in enum 'LoginCustomFlags' in histograms.xml."; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | // This method generates a hint for the user for what string should be added |
| 745 | // to the enum LoginCustomFlags to make in consistent. |
| 746 | std::string GetHistogramEnumEntryText(const std::string& switch_name, |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 747 | Sample value) { |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 748 | return base::StringPrintf( |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 749 | "<int value=\"%d\" label=\"%s\"/>", value, switch_name.c_str()); |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 750 | } |
| 751 | }; |
| 752 | |
| 753 | TEST_F(AboutFlagsHistogramTest, CheckHistograms) { |
| 754 | base::FilePath histograms_xml_file_path; |
| 755 | ASSERT_TRUE( |
| 756 | PathService::Get(base::DIR_SOURCE_ROOT, &histograms_xml_file_path)); |
| 757 | histograms_xml_file_path = histograms_xml_file_path.AppendASCII("tools") |
| 758 | .AppendASCII("metrics") |
| 759 | .AppendASCII("histograms") |
| 760 | .AppendASCII("histograms.xml"); |
| 761 | |
| 762 | XmlReader histograms_xml; |
| 763 | ASSERT_TRUE(histograms_xml.LoadFile( |
| 764 | FilePathStringTypeToString(histograms_xml_file_path.value()))); |
alemate | 0107c327 | 2014-09-03 04:30:04 | [diff] [blame] | 765 | std::map<Sample, std::string> login_custom_flags = |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 766 | ReadEnumFromHistogramsXml("LoginCustomFlags", &histograms_xml); |
| 767 | ASSERT_TRUE(login_custom_flags.size()) |
| 768 | << "Error reading enum 'LoginCustomFlags' from histograms.xml."; |
| 769 | |
| 770 | // Build reverse map {switch_name => id} from login_custom_flags. |
| 771 | SwitchToIdMap histograms_xml_switches_ids; |
| 772 | |
asvitkine | bff63b2 | 2014-11-25 01:03:52 | [diff] [blame] | 773 | EXPECT_TRUE(login_custom_flags.count(testing::kBadSwitchFormatHistogramId)) |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 774 | << "Entry for UMA ID of incorrect command-line flag is not found in " |
| 775 | "histograms.xml enum LoginCustomFlags. " |
| 776 | "Consider adding entry:\n" |
| 777 | << " " << GetHistogramEnumEntryText("BAD_FLAG_FORMAT", 0); |
| 778 | // Check that all LoginCustomFlags entries have correct values. |
asvitkine | bff63b2 | 2014-11-25 01:03:52 | [diff] [blame] | 779 | for (const auto& entry : login_custom_flags) { |
| 780 | if (entry.first == testing::kBadSwitchFormatHistogramId) { |
| 781 | // Add error value with empty name. |
| 782 | SetSwitchToHistogramIdMapping(std::string(), entry.first, |
| 783 | &histograms_xml_switches_ids); |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 784 | continue; |
| 785 | } |
asvitkine | bff63b2 | 2014-11-25 01:03:52 | [diff] [blame] | 786 | const Sample uma_id = GetSwitchUMAId(entry.second); |
| 787 | EXPECT_EQ(uma_id, entry.first) |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 788 | << "histograms.xml enum LoginCustomFlags " |
asvitkine | bff63b2 | 2014-11-25 01:03:52 | [diff] [blame] | 789 | "entry '" << entry.second << "' has incorrect value=" << entry.first |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 790 | << ", but " << uma_id << " is expected. Consider changing entry to:\n" |
asvitkine | bff63b2 | 2014-11-25 01:03:52 | [diff] [blame] | 791 | << " " << GetHistogramEnumEntryText(entry.second, uma_id); |
| 792 | SetSwitchToHistogramIdMapping(entry.second, entry.first, |
| 793 | &histograms_xml_switches_ids); |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | // Check that all flags in about_flags.cc have entries in login_custom_flags. |
| 797 | std::set<std::string> all_switches = GetAllSwitchesForTesting(); |
asvitkine | bff63b2 | 2014-11-25 01:03:52 | [diff] [blame] | 798 | for (const std::string& flag : all_switches) { |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 799 | // Skip empty placeholders. |
asvitkine | bff63b2 | 2014-11-25 01:03:52 | [diff] [blame] | 800 | if (flag.empty()) |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 801 | continue; |
asvitkine | bff63b2 | 2014-11-25 01:03:52 | [diff] [blame] | 802 | const Sample uma_id = GetSwitchUMAId(flag); |
| 803 | EXPECT_NE(testing::kBadSwitchFormatHistogramId, uma_id) |
| 804 | << "Command-line switch '" << flag |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 805 | << "' from about_flags.cc has UMA ID equal to reserved value " |
asvitkine | bff63b2 | 2014-11-25 01:03:52 | [diff] [blame] | 806 | "kBadSwitchFormatHistogramId=" |
| 807 | << testing::kBadSwitchFormatHistogramId |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 808 | << ". Please modify switch name."; |
| 809 | SwitchToIdMap::iterator enum_entry = |
asvitkine | bff63b2 | 2014-11-25 01:03:52 | [diff] [blame] | 810 | histograms_xml_switches_ids.lower_bound(flag); |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 811 | |
| 812 | // Ignore case here when switch ID is incorrect - it has already been |
| 813 | // reported in the previous loop. |
| 814 | EXPECT_TRUE(enum_entry != histograms_xml_switches_ids.end() && |
asvitkine | bff63b2 | 2014-11-25 01:03:52 | [diff] [blame] | 815 | enum_entry->first == flag) |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 816 | << "histograms.xml enum LoginCustomFlags doesn't contain switch '" |
asvitkine | bff63b2 | 2014-11-25 01:03:52 | [diff] [blame] | 817 | << flag << "' (value=" << uma_id |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 818 | << " expected). Consider adding entry:\n" |
asvitkine | bff63b2 | 2014-11-25 01:03:52 | [diff] [blame] | 819 | << " " << GetHistogramEnumEntryText(flag, uma_id); |
[email protected] | 4b66a7d | 2014-08-15 17:13:31 | [diff] [blame] | 820 | } |
| 821 | } |
| 822 | |
[email protected] | e2ddbc9 | 2010-10-15 20:02:07 | [diff] [blame] | 823 | } // namespace about_flags |