OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/about_flags.h" | 5 #include "chrome/browser/about_flags.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/format_macros.h" |
11 #include "base/path_service.h" | 12 #include "base/path_service.h" |
12 #include "base/prefs/pref_registry_simple.h" | 13 #include "base/prefs/pref_registry_simple.h" |
13 #include "base/prefs/testing_pref_service.h" | 14 #include "base/prefs/testing_pref_service.h" |
14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/string_split.h" |
15 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
16 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
17 #include "base/values.h" | 19 #include "base/values.h" |
18 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
19 #include "chrome/grit/chromium_strings.h" | 21 #include "chrome/grit/chromium_strings.h" |
20 #include "components/flags_ui/flags_ui_pref_names.h" | 22 #include "components/flags_ui/flags_ui_pref_names.h" |
21 #include "components/flags_ui/pref_service_flags_storage.h" | 23 #include "components/flags_ui/pref_service_flags_storage.h" |
| 24 #include "content/public/common/content_switches.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
23 #include "third_party/libxml/chromium/libxml_utils.h" | 26 #include "third_party/libxml/chromium/libxml_utils.h" |
24 | 27 |
25 namespace about_flags { | 28 namespace about_flags { |
26 | 29 |
27 namespace { | 30 namespace { |
28 | 31 |
29 const char kFlags1[] = "flag1"; | 32 const char kFlags1[] = "flag1"; |
30 const char kFlags2[] = "flag2"; | 33 const char kFlags2[] = "flag2"; |
31 const char kFlags3[] = "flag3"; | 34 const char kFlags3[] = "flag3"; |
32 const char kFlags4[] = "flag4"; | 35 const char kFlags4[] = "flag4"; |
33 const char kFlags5[] = "flag5"; | 36 const char kFlags5[] = "flag5"; |
34 const char kFlags6[] = "flag6"; | 37 const char kFlags6[] = "flag6"; |
| 38 const char kFlags7[] = "flag7"; |
35 | 39 |
36 const char kSwitch1[] = "switch"; | 40 const char kSwitch1[] = "switch"; |
37 const char kSwitch2[] = "switch2"; | 41 const char kSwitch2[] = "switch2"; |
38 const char kSwitch3[] = "switch3"; | 42 const char kSwitch3[] = "switch3"; |
39 const char kSwitch6[] = "switch6"; | 43 const char kSwitch6[] = "switch6"; |
40 const char kValueForSwitch2[] = "value_for_switch2"; | 44 const char kValueForSwitch2[] = "value_for_switch2"; |
41 | 45 |
42 const char kMultiSwitch1[] = "multi_switch1"; | 46 const char kMultiSwitch1[] = "multi_switch1"; |
43 const char kMultiSwitch2[] = "multi_switch2"; | 47 const char kMultiSwitch2[] = "multi_switch2"; |
44 const char kValueForMultiSwitch2[] = "value_for_multi_switch2"; | 48 const char kValueForMultiSwitch2[] = "value_for_multi_switch2"; |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 } | 183 } |
180 | 184 |
181 std::string FilePathStringTypeToString(const base::FilePath::StringType& path) { | 185 std::string FilePathStringTypeToString(const base::FilePath::StringType& path) { |
182 #if defined(OS_WIN) | 186 #if defined(OS_WIN) |
183 return base::UTF16ToUTF8(path); | 187 return base::UTF16ToUTF8(path); |
184 #else | 188 #else |
185 return path; | 189 return path; |
186 #endif | 190 #endif |
187 } | 191 } |
188 | 192 |
| 193 // Get all associated switches corresponding to defined about_flags.cc entries. |
| 194 // Does not include information about FEATURE_VALUE entries. |
189 std::set<std::string> GetAllSwitchesForTesting() { | 195 std::set<std::string> GetAllSwitchesForTesting() { |
190 std::set<std::string> result; | 196 std::set<std::string> result; |
191 | 197 |
192 size_t num_entries = 0; | 198 size_t num_entries = 0; |
193 const FeatureEntry* entries = | 199 const FeatureEntry* entries = |
194 testing::GetFeatureEntries(&num_entries); | 200 testing::GetFeatureEntries(&num_entries); |
195 | 201 |
196 for (size_t i = 0; i < num_entries; ++i) { | 202 for (size_t i = 0; i < num_entries; ++i) { |
197 const FeatureEntry& entry = entries[i]; | 203 const FeatureEntry& entry = entries[i]; |
198 if (entry.type == FeatureEntry::SINGLE_VALUE || | 204 switch (entry.type) { |
199 entry.type == FeatureEntry::SINGLE_DISABLE_VALUE) { | 205 case FeatureEntry::SINGLE_VALUE: |
200 result.insert(entry.command_line_switch); | 206 case FeatureEntry::SINGLE_DISABLE_VALUE: |
201 } else if (entry.type == FeatureEntry::MULTI_VALUE) { | 207 result.insert(entry.command_line_switch); |
202 for (int j = 0; j < entry.num_choices; ++j) { | 208 break; |
203 result.insert(entry.choices[j].command_line_switch); | 209 case FeatureEntry::MULTI_VALUE: |
204 } | 210 for (int j = 0; j < entry.num_choices; ++j) { |
205 } else { | 211 result.insert(entry.choices[j].command_line_switch); |
206 DCHECK_EQ(entry.type, FeatureEntry::ENABLE_DISABLE_VALUE); | 212 } |
207 result.insert(entry.command_line_switch); | 213 break; |
208 result.insert(entry.disable_command_line_switch); | 214 case FeatureEntry::ENABLE_DISABLE_VALUE: |
| 215 result.insert(entry.command_line_switch); |
| 216 result.insert(entry.disable_command_line_switch); |
| 217 break; |
| 218 case FeatureEntry::FEATURE_VALUE: |
| 219 break; |
209 } | 220 } |
210 } | 221 } |
211 return result; | 222 return result; |
212 } | 223 } |
213 | 224 |
214 } // anonymous namespace | 225 } // anonymous namespace |
215 | 226 |
216 const FeatureEntry::Choice kMultiChoices[] = { | 227 const FeatureEntry::Choice kMultiChoices[] = { |
217 { IDS_PRODUCT_NAME, "", "" }, | 228 { IDS_PRODUCT_NAME, "", "" }, |
218 { IDS_PRODUCT_NAME, kMultiSwitch1, "" }, | 229 { IDS_PRODUCT_NAME, kMultiSwitch1, "" }, |
219 { IDS_PRODUCT_NAME, kMultiSwitch2, kValueForMultiSwitch2 }, | 230 { IDS_PRODUCT_NAME, kMultiSwitch2, kValueForMultiSwitch2 }, |
220 }; | 231 }; |
221 | 232 |
222 // The entries that are set for these tests. The 3rd entry is not supported on | 233 // The entries that are set for these tests. The 3rd entry is not supported on |
223 // the current platform, all others are. | 234 // the current platform, all others are. |
224 static FeatureEntry kEntries[] = { | 235 static FeatureEntry kEntries[] = { |
225 { | 236 {kFlags1, IDS_PRODUCT_NAME, IDS_PRODUCT_NAME, |
226 kFlags1, | 237 0, // Ends up being mapped to the current platform. |
227 IDS_PRODUCT_NAME, | 238 FeatureEntry::SINGLE_VALUE, kSwitch1, "", nullptr, nullptr, nullptr, |
228 IDS_PRODUCT_NAME, | 239 nullptr, 0}, |
229 0, // Ends up being mapped to the current platform. | 240 {kFlags2, IDS_PRODUCT_NAME, IDS_PRODUCT_NAME, |
230 FeatureEntry::SINGLE_VALUE, | 241 0, // Ends up being mapped to the current platform. |
231 kSwitch1, | 242 FeatureEntry::SINGLE_VALUE, kSwitch2, kValueForSwitch2, nullptr, nullptr, |
232 "", | 243 nullptr, nullptr, 0}, |
233 NULL, | 244 {kFlags3, IDS_PRODUCT_NAME, IDS_PRODUCT_NAME, |
234 NULL, | 245 0, // This ends up enabling for an OS other than the current. |
235 NULL, | 246 FeatureEntry::SINGLE_VALUE, kSwitch3, "", nullptr, nullptr, nullptr, |
236 0 | 247 nullptr, 0}, |
237 }, | 248 {kFlags4, IDS_PRODUCT_NAME, IDS_PRODUCT_NAME, |
238 { | 249 0, // Ends up being mapped to the current platform. |
239 kFlags2, | 250 FeatureEntry::MULTI_VALUE, "", "", "", "", nullptr, kMultiChoices, |
240 IDS_PRODUCT_NAME, | 251 arraysize(kMultiChoices)}, |
241 IDS_PRODUCT_NAME, | 252 {kFlags5, IDS_PRODUCT_NAME, IDS_PRODUCT_NAME, |
242 0, // Ends up being mapped to the current platform. | 253 0, // Ends up being mapped to the current platform. |
243 FeatureEntry::SINGLE_VALUE, | 254 FeatureEntry::ENABLE_DISABLE_VALUE, kSwitch1, kEnableDisableValue1, |
244 kSwitch2, | 255 kSwitch2, kEnableDisableValue2, nullptr, nullptr, 3}, |
245 kValueForSwitch2, | 256 {kFlags6, IDS_PRODUCT_NAME, IDS_PRODUCT_NAME, 0, |
246 NULL, | 257 FeatureEntry::SINGLE_DISABLE_VALUE, kSwitch6, "", nullptr, nullptr, |
247 NULL, | 258 nullptr, nullptr, 0}, |
248 NULL, | 259 {kFlags7, IDS_PRODUCT_NAME, IDS_PRODUCT_NAME, |
249 0 | 260 0, // Ends up being mapped to the current platform. |
250 }, | 261 FeatureEntry::FEATURE_VALUE, nullptr, nullptr, nullptr, nullptr, |
251 { | 262 "FeatureName", nullptr, 3}, |
252 kFlags3, | |
253 IDS_PRODUCT_NAME, | |
254 IDS_PRODUCT_NAME, | |
255 0, // This ends up enabling for an OS other than the current. | |
256 FeatureEntry::SINGLE_VALUE, | |
257 kSwitch3, | |
258 "", | |
259 NULL, | |
260 NULL, | |
261 NULL, | |
262 0 | |
263 }, | |
264 { | |
265 kFlags4, | |
266 IDS_PRODUCT_NAME, | |
267 IDS_PRODUCT_NAME, | |
268 0, // Ends up being mapped to the current platform. | |
269 FeatureEntry::MULTI_VALUE, | |
270 "", | |
271 "", | |
272 "", | |
273 "", | |
274 kMultiChoices, | |
275 arraysize(kMultiChoices) | |
276 }, | |
277 { | |
278 kFlags5, | |
279 IDS_PRODUCT_NAME, | |
280 IDS_PRODUCT_NAME, | |
281 0, // Ends up being mapped to the current platform. | |
282 FeatureEntry::ENABLE_DISABLE_VALUE, | |
283 kSwitch1, | |
284 kEnableDisableValue1, | |
285 kSwitch2, | |
286 kEnableDisableValue2, | |
287 NULL, | |
288 3 | |
289 }, | |
290 { | |
291 kFlags6, | |
292 IDS_PRODUCT_NAME, | |
293 IDS_PRODUCT_NAME, | |
294 0, | |
295 FeatureEntry::SINGLE_DISABLE_VALUE, | |
296 kSwitch6, | |
297 "", | |
298 NULL, | |
299 NULL, | |
300 NULL, | |
301 0 | |
302 }, | |
303 }; | 263 }; |
304 | 264 |
305 class AboutFlagsTest : public ::testing::Test { | 265 class AboutFlagsTest : public ::testing::Test { |
306 protected: | 266 protected: |
307 AboutFlagsTest() : flags_storage_(&prefs_) { | 267 AboutFlagsTest() : flags_storage_(&prefs_) { |
308 prefs_.registry()->RegisterListPref( | 268 prefs_.registry()->RegisterListPref( |
309 flags_ui::prefs::kEnabledLabsExperiments); | 269 flags_ui::prefs::kEnabledLabsExperiments); |
310 testing::ClearState(); | 270 testing::ClearState(); |
311 } | 271 } |
312 | 272 |
313 void SetUp() override { | 273 void SetUp() override { |
314 for (size_t i = 0; i < arraysize(kEntries); ++i) | 274 for (size_t i = 0; i < arraysize(kEntries); ++i) |
315 kEntries[i].supported_platforms = GetCurrentPlatform(); | 275 kEntries[i].supported_platforms = GetCurrentPlatform(); |
316 | 276 |
317 int os_other_than_current = 1; | 277 int os_other_than_current = 1; |
318 while (os_other_than_current == GetCurrentPlatform()) | 278 while (os_other_than_current == GetCurrentPlatform()) |
319 os_other_than_current <<= 1; | 279 os_other_than_current <<= 1; |
320 kEntries[2].supported_platforms = os_other_than_current; | 280 kEntries[2].supported_platforms = os_other_than_current; |
321 | 281 |
322 testing::SetFeatureEntries(kEntries, arraysize(kEntries)); | 282 testing::SetFeatureEntries(kEntries, arraysize(kEntries)); |
323 } | 283 } |
324 | 284 |
325 void TearDown() override { testing::SetFeatureEntries(NULL, 0); } | 285 void TearDown() override { testing::SetFeatureEntries(nullptr, 0); } |
326 | 286 |
327 TestingPrefServiceSimple prefs_; | 287 TestingPrefServiceSimple prefs_; |
328 flags_ui::PrefServiceFlagsStorage flags_storage_; | 288 flags_ui::PrefServiceFlagsStorage flags_storage_; |
329 }; | 289 }; |
330 | 290 |
331 | 291 |
332 TEST_F(AboutFlagsTest, NoChangeNoRestart) { | 292 TEST_F(AboutFlagsTest, NoChangeNoRestart) { |
333 EXPECT_FALSE(IsRestartNeededToCommitChanges()); | 293 EXPECT_FALSE(IsRestartNeededToCommitChanges()); |
334 SetFeatureEntryEnabled(&flags_storage_, kFlags1, false); | 294 SetFeatureEntryEnabled(&flags_storage_, kFlags1, false); |
335 EXPECT_FALSE(IsRestartNeededToCommitChanges()); | 295 EXPECT_FALSE(IsRestartNeededToCommitChanges()); |
(...skipping 24 matching lines...) Expand all Loading... |
360 SetFeatureEntryEnabled(&flags_storage_, entry.NameForChoice(2), true); | 320 SetFeatureEntryEnabled(&flags_storage_, entry.NameForChoice(2), true); |
361 EXPECT_TRUE(IsRestartNeededToCommitChanges()); | 321 EXPECT_TRUE(IsRestartNeededToCommitChanges()); |
362 testing::ClearState(); | 322 testing::ClearState(); |
363 EXPECT_FALSE(IsRestartNeededToCommitChanges()); | 323 EXPECT_FALSE(IsRestartNeededToCommitChanges()); |
364 // Enable the default choice now. | 324 // Enable the default choice now. |
365 SetFeatureEntryEnabled(&flags_storage_, entry.NameForChoice(0), true); | 325 SetFeatureEntryEnabled(&flags_storage_, entry.NameForChoice(0), true); |
366 EXPECT_TRUE(IsRestartNeededToCommitChanges()); | 326 EXPECT_TRUE(IsRestartNeededToCommitChanges()); |
367 } | 327 } |
368 | 328 |
369 TEST_F(AboutFlagsTest, AddTwoFlagsRemoveOne) { | 329 TEST_F(AboutFlagsTest, AddTwoFlagsRemoveOne) { |
370 // Add two entriess, check they're there. | 330 // Add two entries, check they're there. |
371 SetFeatureEntryEnabled(&flags_storage_, kFlags1, true); | 331 SetFeatureEntryEnabled(&flags_storage_, kFlags1, true); |
372 SetFeatureEntryEnabled(&flags_storage_, kFlags2, true); | 332 SetFeatureEntryEnabled(&flags_storage_, kFlags2, true); |
373 | 333 |
374 const base::ListValue* entries_list = | 334 const base::ListValue* entries_list = |
375 prefs_.GetList(flags_ui::prefs::kEnabledLabsExperiments); | 335 prefs_.GetList(flags_ui::prefs::kEnabledLabsExperiments); |
376 ASSERT_TRUE(entries_list != NULL); | 336 ASSERT_TRUE(entries_list != nullptr); |
377 | 337 |
378 ASSERT_EQ(2u, entries_list->GetSize()); | 338 ASSERT_EQ(2u, entries_list->GetSize()); |
379 | 339 |
380 std::string s0; | 340 std::string s0; |
381 ASSERT_TRUE(entries_list->GetString(0, &s0)); | 341 ASSERT_TRUE(entries_list->GetString(0, &s0)); |
382 std::string s1; | 342 std::string s1; |
383 ASSERT_TRUE(entries_list->GetString(1, &s1)); | 343 ASSERT_TRUE(entries_list->GetString(1, &s1)); |
384 | 344 |
385 EXPECT_TRUE(s0 == kFlags1 || s1 == kFlags1); | 345 EXPECT_TRUE(s0 == kFlags1 || s1 == kFlags1); |
386 EXPECT_TRUE(s0 == kFlags2 || s1 == kFlags2); | 346 EXPECT_TRUE(s0 == kFlags2 || s1 == kFlags2); |
387 | 347 |
388 // Remove one entry, check the other's still around. | 348 // Remove one entry, check the other's still around. |
389 SetFeatureEntryEnabled(&flags_storage_, kFlags2, false); | 349 SetFeatureEntryEnabled(&flags_storage_, kFlags2, false); |
390 | 350 |
391 entries_list = prefs_.GetList(flags_ui::prefs::kEnabledLabsExperiments); | 351 entries_list = prefs_.GetList(flags_ui::prefs::kEnabledLabsExperiments); |
392 ASSERT_TRUE(entries_list != NULL); | 352 ASSERT_TRUE(entries_list != nullptr); |
393 ASSERT_EQ(1u, entries_list->GetSize()); | 353 ASSERT_EQ(1u, entries_list->GetSize()); |
394 ASSERT_TRUE(entries_list->GetString(0, &s0)); | 354 ASSERT_TRUE(entries_list->GetString(0, &s0)); |
395 EXPECT_TRUE(s0 == kFlags1); | 355 EXPECT_TRUE(s0 == kFlags1); |
396 } | 356 } |
397 | 357 |
398 TEST_F(AboutFlagsTest, AddTwoFlagsRemoveBoth) { | 358 TEST_F(AboutFlagsTest, AddTwoFlagsRemoveBoth) { |
399 // Add two entries, check the pref exists. | 359 // Add two entries, check the pref exists. |
400 SetFeatureEntryEnabled(&flags_storage_, kFlags1, true); | 360 SetFeatureEntryEnabled(&flags_storage_, kFlags1, true); |
401 SetFeatureEntryEnabled(&flags_storage_, kFlags2, true); | 361 SetFeatureEntryEnabled(&flags_storage_, kFlags2, true); |
402 const base::ListValue* entries_list = | 362 const base::ListValue* entries_list = |
403 prefs_.GetList(flags_ui::prefs::kEnabledLabsExperiments); | 363 prefs_.GetList(flags_ui::prefs::kEnabledLabsExperiments); |
404 ASSERT_TRUE(entries_list != NULL); | 364 ASSERT_TRUE(entries_list != nullptr); |
405 | 365 |
406 // Remove both, the pref should have been removed completely. | 366 // Remove both, the pref should have been removed completely. |
407 SetFeatureEntryEnabled(&flags_storage_, kFlags1, false); | 367 SetFeatureEntryEnabled(&flags_storage_, kFlags1, false); |
408 SetFeatureEntryEnabled(&flags_storage_, kFlags2, false); | 368 SetFeatureEntryEnabled(&flags_storage_, kFlags2, false); |
409 entries_list = prefs_.GetList(flags_ui::prefs::kEnabledLabsExperiments); | 369 entries_list = prefs_.GetList(flags_ui::prefs::kEnabledLabsExperiments); |
410 EXPECT_TRUE(entries_list == NULL || entries_list->GetSize() == 0); | 370 EXPECT_TRUE(entries_list == nullptr || entries_list->GetSize() == 0); |
411 } | 371 } |
412 | 372 |
413 TEST_F(AboutFlagsTest, ConvertFlagsToSwitches) { | 373 TEST_F(AboutFlagsTest, ConvertFlagsToSwitches) { |
414 SetFeatureEntryEnabled(&flags_storage_, kFlags1, true); | 374 SetFeatureEntryEnabled(&flags_storage_, kFlags1, true); |
415 | 375 |
416 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); | 376 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
417 command_line.AppendSwitch("foo"); | 377 command_line.AppendSwitch("foo"); |
418 | 378 |
419 EXPECT_TRUE(command_line.HasSwitch("foo")); | 379 EXPECT_TRUE(command_line.HasSwitch("foo")); |
420 EXPECT_FALSE(command_line.HasSwitch(kSwitch1)); | 380 EXPECT_FALSE(command_line.HasSwitch(kSwitch1)); |
(...skipping 26 matching lines...) Expand all Loading... |
447 SetFeatureEntryEnabled(&flags_storage_, kFlags1, true); | 407 SetFeatureEntryEnabled(&flags_storage_, kFlags1, true); |
448 | 408 |
449 const std::string kDoubleDash("--"); | 409 const std::string kDoubleDash("--"); |
450 | 410 |
451 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); | 411 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
452 command_line.AppendSwitch("foo"); | 412 command_line.AppendSwitch("foo"); |
453 | 413 |
454 base::CommandLine new_command_line(base::CommandLine::NO_PROGRAM); | 414 base::CommandLine new_command_line(base::CommandLine::NO_PROGRAM); |
455 ConvertFlagsToSwitches(&flags_storage_, &new_command_line, kAddSentinels); | 415 ConvertFlagsToSwitches(&flags_storage_, &new_command_line, kAddSentinels); |
456 | 416 |
457 EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine( | 417 EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine(new_command_line, |
458 new_command_line, command_line, NULL)); | 418 command_line, nullptr)); |
459 { | 419 { |
460 std::set<base::CommandLine::StringType> difference; | 420 std::set<base::CommandLine::StringType> difference; |
461 EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine( | 421 EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine( |
462 new_command_line, command_line, &difference)); | 422 new_command_line, command_line, &difference)); |
463 EXPECT_EQ(1U, difference.size()); | 423 EXPECT_EQ(1U, difference.size()); |
464 EXPECT_EQ(1U, difference.count(CreateSwitch(kDoubleDash + kSwitch1))); | 424 EXPECT_EQ(1U, difference.count(CreateSwitch(kDoubleDash + kSwitch1))); |
465 } | 425 } |
466 | 426 |
467 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); | 427 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
468 | 428 |
469 EXPECT_TRUE(AreSwitchesIdenticalToCurrentCommandLine( | 429 EXPECT_TRUE(AreSwitchesIdenticalToCurrentCommandLine(new_command_line, |
470 new_command_line, command_line, NULL)); | 430 command_line, nullptr)); |
471 { | 431 { |
472 std::set<base::CommandLine::StringType> difference; | 432 std::set<base::CommandLine::StringType> difference; |
473 EXPECT_TRUE(AreSwitchesIdenticalToCurrentCommandLine( | 433 EXPECT_TRUE(AreSwitchesIdenticalToCurrentCommandLine( |
474 new_command_line, command_line, &difference)); | 434 new_command_line, command_line, &difference)); |
475 EXPECT_TRUE(difference.empty()); | 435 EXPECT_TRUE(difference.empty()); |
476 } | 436 } |
477 | 437 |
478 // Now both have flags but different. | 438 // Now both have flags but different. |
479 SetFeatureEntryEnabled(&flags_storage_, kFlags1, false); | 439 SetFeatureEntryEnabled(&flags_storage_, kFlags1, false); |
480 SetFeatureEntryEnabled(&flags_storage_, kFlags2, true); | 440 SetFeatureEntryEnabled(&flags_storage_, kFlags2, true); |
481 | 441 |
482 base::CommandLine another_command_line(base::CommandLine::NO_PROGRAM); | 442 base::CommandLine another_command_line(base::CommandLine::NO_PROGRAM); |
483 ConvertFlagsToSwitches(&flags_storage_, &another_command_line, kAddSentinels); | 443 ConvertFlagsToSwitches(&flags_storage_, &another_command_line, kAddSentinels); |
484 | 444 |
485 EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine( | 445 EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine( |
486 new_command_line, another_command_line, NULL)); | 446 new_command_line, another_command_line, nullptr)); |
487 { | 447 { |
488 std::set<base::CommandLine::StringType> difference; | 448 std::set<base::CommandLine::StringType> difference; |
489 EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine( | 449 EXPECT_FALSE(AreSwitchesIdenticalToCurrentCommandLine( |
490 new_command_line, another_command_line, &difference)); | 450 new_command_line, another_command_line, &difference)); |
491 EXPECT_EQ(2U, difference.size()); | 451 EXPECT_EQ(2U, difference.size()); |
492 EXPECT_EQ(1U, difference.count(CreateSwitch(kDoubleDash + kSwitch1))); | 452 EXPECT_EQ(1U, difference.count(CreateSwitch(kDoubleDash + kSwitch1))); |
493 EXPECT_EQ(1U, | 453 EXPECT_EQ(1U, |
494 difference.count(CreateSwitch(kDoubleDash + kSwitch2 + "=" + | 454 difference.count(CreateSwitch(kDoubleDash + kSwitch2 + "=" + |
495 kValueForSwitch2))); | 455 kValueForSwitch2))); |
496 } | 456 } |
497 } | 457 } |
498 | 458 |
499 TEST_F(AboutFlagsTest, RemoveFlagSwitches) { | 459 TEST_F(AboutFlagsTest, RemoveFlagSwitches) { |
500 std::map<std::string, base::CommandLine::StringType> switch_list; | 460 std::map<std::string, base::CommandLine::StringType> switch_list; |
501 switch_list[kSwitch1] = base::CommandLine::StringType(); | 461 switch_list[kSwitch1] = base::CommandLine::StringType(); |
502 switch_list[switches::kFlagSwitchesBegin] = base::CommandLine::StringType(); | 462 switch_list[switches::kFlagSwitchesBegin] = base::CommandLine::StringType(); |
503 switch_list[switches::kFlagSwitchesEnd] = base::CommandLine::StringType(); | 463 switch_list[switches::kFlagSwitchesEnd] = base::CommandLine::StringType(); |
504 switch_list["foo"] = base::CommandLine::StringType(); | 464 switch_list["foo"] = base::CommandLine::StringType(); |
505 | 465 |
506 SetFeatureEntryEnabled(&flags_storage_, kFlags1, true); | 466 SetFeatureEntryEnabled(&flags_storage_, kFlags1, true); |
507 | 467 |
508 // This shouldn't do anything before ConvertFlagsToSwitches() wasn't called. | 468 // This shouldn't do anything before ConvertFlagsToSwitches() wasn't called. |
509 RemoveFlagsSwitches(&switch_list); | 469 RemoveFlagsSwitches(&switch_list); |
510 ASSERT_EQ(4u, switch_list.size()); | 470 ASSERT_EQ(4u, switch_list.size()); |
511 EXPECT_TRUE(switch_list.find(kSwitch1) != switch_list.end()); | 471 EXPECT_TRUE(ContainsKey(switch_list, kSwitch1)); |
512 EXPECT_TRUE(switch_list.find(switches::kFlagSwitchesBegin) != | 472 EXPECT_TRUE(ContainsKey(switch_list, switches::kFlagSwitchesBegin)); |
513 switch_list.end()); | 473 EXPECT_TRUE(ContainsKey(switch_list, switches::kFlagSwitchesEnd)); |
514 EXPECT_TRUE(switch_list.find(switches::kFlagSwitchesEnd) != | 474 EXPECT_TRUE(ContainsKey(switch_list, "foo")); |
515 switch_list.end()); | |
516 EXPECT_TRUE(switch_list.find("foo") != switch_list.end()); | |
517 | 475 |
518 // Call ConvertFlagsToSwitches(), then RemoveFlagsSwitches() again. | 476 // Call ConvertFlagsToSwitches(), then RemoveFlagsSwitches() again. |
519 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); | 477 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
520 command_line.AppendSwitch("foo"); | 478 command_line.AppendSwitch("foo"); |
521 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); | 479 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
522 RemoveFlagsSwitches(&switch_list); | 480 RemoveFlagsSwitches(&switch_list); |
523 | 481 |
524 // Now the about:flags-related switch should have been removed. | 482 // Now the about:flags-related switch should have been removed. |
525 ASSERT_EQ(1u, switch_list.size()); | 483 ASSERT_EQ(1u, switch_list.size()); |
526 EXPECT_TRUE(switch_list.find("foo") != switch_list.end()); | 484 EXPECT_TRUE(ContainsKey(switch_list, "foo")); |
| 485 } |
| 486 |
| 487 TEST_F(AboutFlagsTest, RemoveFlagSwitches_Features) { |
| 488 struct { |
| 489 int enabled_choice; // 0: default, 1: enabled, 2: disabled. |
| 490 const char* existing_enable_features; |
| 491 const char* existing_disable_features; |
| 492 const char* expected_enable_features; |
| 493 const char* expected_disable_features; |
| 494 } cases[] = { |
| 495 // Default value: Should not affect existing flags. |
| 496 {0, nullptr, nullptr, nullptr, nullptr}, |
| 497 {0, "A,B", "C", "A,B", "C"}, |
| 498 // "Enable" option: should only affect enabled list. |
| 499 {1, nullptr, nullptr, "FeatureName", nullptr}, |
| 500 {1, "A,B", "C", "A,B,FeatureName", "C"}, |
| 501 // "Disable" option: should only affect disabled list. |
| 502 {2, nullptr, nullptr, nullptr, "FeatureName"}, |
| 503 {2, "A,B", "C", "A,B", "C,FeatureName"}, |
| 504 }; |
| 505 |
| 506 for (size_t i = 0; i < arraysize(cases); ++i) { |
| 507 SCOPED_TRACE(base::StringPrintf( |
| 508 "Test[%" PRIuS "]: %d [%s] [%s]", i, cases[i].enabled_choice, |
| 509 cases[i].existing_enable_features ? cases[i].existing_enable_features |
| 510 : "null", |
| 511 cases[i].existing_disable_features ? cases[i].existing_disable_features |
| 512 : "null")); |
| 513 |
| 514 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 515 if (cases[i].existing_enable_features) { |
| 516 command_line.AppendSwitchASCII(switches::kEnableFeatures, |
| 517 cases[i].existing_enable_features); |
| 518 } |
| 519 if (cases[i].existing_disable_features) { |
| 520 command_line.AppendSwitchASCII(switches::kDisableFeatures, |
| 521 cases[i].existing_disable_features); |
| 522 } |
| 523 |
| 524 testing::ClearState(); |
| 525 |
| 526 const std::string entry_name = base::StringPrintf( |
| 527 "%s%s%d", kFlags7, testing::kMultiSeparator, cases[i].enabled_choice); |
| 528 SetFeatureEntryEnabled(&flags_storage_, entry_name, true); |
| 529 |
| 530 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
| 531 auto switch_list = command_line.GetSwitches(); |
| 532 EXPECT_EQ(cases[i].expected_enable_features != nullptr, |
| 533 ContainsKey(switch_list, switches::kEnableFeatures)); |
| 534 if (cases[i].expected_enable_features) |
| 535 EXPECT_EQ(CreateSwitch(cases[i].expected_enable_features), |
| 536 switch_list[switches::kEnableFeatures]); |
| 537 |
| 538 EXPECT_EQ(cases[i].expected_disable_features != nullptr, |
| 539 ContainsKey(switch_list, switches::kDisableFeatures)); |
| 540 if (cases[i].expected_disable_features) |
| 541 EXPECT_EQ(CreateSwitch(cases[i].expected_disable_features), |
| 542 switch_list[switches::kDisableFeatures]); |
| 543 |
| 544 // RemoveFlagsSwitches() should result in the original values for these |
| 545 // switches. |
| 546 switch_list = command_line.GetSwitches(); |
| 547 RemoveFlagsSwitches(&switch_list); |
| 548 EXPECT_EQ(cases[i].existing_enable_features != nullptr, |
| 549 ContainsKey(switch_list, switches::kEnableFeatures)); |
| 550 if (cases[i].existing_enable_features) |
| 551 EXPECT_EQ(CreateSwitch(cases[i].existing_enable_features), |
| 552 switch_list[switches::kEnableFeatures]); |
| 553 EXPECT_EQ(cases[i].existing_disable_features != nullptr, |
| 554 ContainsKey(switch_list, switches::kEnableFeatures)); |
| 555 if (cases[i].existing_disable_features) |
| 556 EXPECT_EQ(CreateSwitch(cases[i].existing_disable_features), |
| 557 switch_list[switches::kDisableFeatures]); |
| 558 } |
527 } | 559 } |
528 | 560 |
529 // Tests enabling entries that aren't supported on the current platform. | 561 // Tests enabling entries that aren't supported on the current platform. |
530 TEST_F(AboutFlagsTest, PersistAndPrune) { | 562 TEST_F(AboutFlagsTest, PersistAndPrune) { |
531 // Enable entries 1 and 3. | 563 // Enable entries 1 and 3. |
532 SetFeatureEntryEnabled(&flags_storage_, kFlags1, true); | 564 SetFeatureEntryEnabled(&flags_storage_, kFlags1, true); |
533 SetFeatureEntryEnabled(&flags_storage_, kFlags3, true); | 565 SetFeatureEntryEnabled(&flags_storage_, kFlags3, true); |
534 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); | 566 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
535 EXPECT_FALSE(command_line.HasSwitch(kSwitch1)); | 567 EXPECT_FALSE(command_line.HasSwitch(kSwitch1)); |
536 EXPECT_FALSE(command_line.HasSwitch(kSwitch3)); | 568 EXPECT_FALSE(command_line.HasSwitch(kSwitch3)); |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
707 // "Default" option selected, same as nothing selected. | 739 // "Default" option selected, same as nothing selected. |
708 SetFeatureEntryEnabled(&flags_storage_, entry.NameForChoice(0), true); | 740 SetFeatureEntryEnabled(&flags_storage_, entry.NameForChoice(0), true); |
709 { | 741 { |
710 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); | 742 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
711 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); | 743 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
712 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1)); | 744 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch1)); |
713 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2)); | 745 EXPECT_FALSE(command_line.HasSwitch(kMultiSwitch2)); |
714 } | 746 } |
715 } | 747 } |
716 | 748 |
| 749 TEST_F(AboutFlagsTest, FeatureValues) { |
| 750 const FeatureEntry& entry = kEntries[6]; |
| 751 ASSERT_EQ(kFlags7, entry.internal_name); |
| 752 |
| 753 struct { |
| 754 int enabled_choice; |
| 755 const char* existing_enable_features; |
| 756 const char* existing_disable_features; |
| 757 const char* expected_enable_features; |
| 758 const char* expected_disable_features; |
| 759 } cases[] = { |
| 760 // Nothing selected. |
| 761 {-1, nullptr, nullptr, "", ""}, |
| 762 // "Default" option selected, same as nothing selected. |
| 763 {0, nullptr, nullptr, "", ""}, |
| 764 // "Enable" option selected. |
| 765 {1, nullptr, nullptr, "FeatureName", ""}, |
| 766 // "Disable" option selected. |
| 767 {2, nullptr, nullptr, "", "FeatureName"}, |
| 768 // "Enable" option should get added to the existing list. |
| 769 {1, "Foo,Bar", nullptr, "Foo,Bar,FeatureName", ""}, |
| 770 // "Disable" option should get added to the existing list. |
| 771 {2, nullptr, "Foo,Bar", "", "Foo,Bar,FeatureName"}, |
| 772 }; |
| 773 |
| 774 for (size_t i = 0; i < arraysize(cases); ++i) { |
| 775 SCOPED_TRACE(base::StringPrintf( |
| 776 "Test[%" PRIuS "]: %d [%s] [%s]", i, cases[i].enabled_choice, |
| 777 cases[i].existing_enable_features ? cases[i].existing_enable_features |
| 778 : "null", |
| 779 cases[i].existing_disable_features ? cases[i].existing_disable_features |
| 780 : "null")); |
| 781 |
| 782 if (cases[i].enabled_choice != -1) { |
| 783 SetFeatureEntryEnabled( |
| 784 &flags_storage_, entry.NameForChoice(cases[i].enabled_choice), true); |
| 785 } |
| 786 |
| 787 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 788 if (cases[i].existing_enable_features) { |
| 789 command_line.AppendSwitchASCII(switches::kEnableFeatures, |
| 790 cases[i].existing_enable_features); |
| 791 } |
| 792 if (cases[i].existing_disable_features) { |
| 793 command_line.AppendSwitchASCII(switches::kDisableFeatures, |
| 794 cases[i].existing_disable_features); |
| 795 } |
| 796 |
| 797 ConvertFlagsToSwitches(&flags_storage_, &command_line, kAddSentinels); |
| 798 EXPECT_EQ(cases[i].expected_enable_features, |
| 799 command_line.GetSwitchValueASCII(switches::kEnableFeatures)); |
| 800 EXPECT_EQ(cases[i].expected_disable_features, |
| 801 command_line.GetSwitchValueASCII(switches::kDisableFeatures)); |
| 802 } |
| 803 } |
| 804 |
717 // Makes sure there are no separators in any of the entry names. | 805 // Makes sure there are no separators in any of the entry names. |
718 TEST_F(AboutFlagsTest, NoSeparators) { | 806 TEST_F(AboutFlagsTest, NoSeparators) { |
719 testing::SetFeatureEntries(NULL, 0); | 807 testing::SetFeatureEntries(nullptr, 0); |
720 size_t count; | 808 size_t count; |
721 const FeatureEntry* entries = testing::GetFeatureEntries(&count); | 809 const FeatureEntry* entries = testing::GetFeatureEntries(&count); |
722 for (size_t i = 0; i < count; ++i) { | 810 for (size_t i = 0; i < count; ++i) { |
723 std::string name = entries[i].internal_name; | 811 std::string name = entries[i].internal_name; |
724 EXPECT_EQ(std::string::npos, name.find(testing::kMultiSeparator)) << i; | 812 EXPECT_EQ(std::string::npos, name.find(testing::kMultiSeparator)) << i; |
725 } | 813 } |
726 } | 814 } |
727 | 815 |
728 class AboutFlagsHistogramTest : public ::testing::Test { | 816 class AboutFlagsHistogramTest : public ::testing::Test { |
729 protected: | 817 protected: |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
814 EXPECT_TRUE(enum_entry != histograms_xml_switches_ids.end() && | 902 EXPECT_TRUE(enum_entry != histograms_xml_switches_ids.end() && |
815 enum_entry->first == flag) | 903 enum_entry->first == flag) |
816 << "histograms.xml enum LoginCustomFlags doesn't contain switch '" | 904 << "histograms.xml enum LoginCustomFlags doesn't contain switch '" |
817 << flag << "' (value=" << uma_id | 905 << flag << "' (value=" << uma_id |
818 << " expected). Consider adding entry:\n" | 906 << " expected). Consider adding entry:\n" |
819 << " " << GetHistogramEnumEntryText(flag, uma_id); | 907 << " " << GetHistogramEnumEntryText(flag, uma_id); |
820 } | 908 } |
821 } | 909 } |
822 | 910 |
823 } // namespace about_flags | 911 } // namespace about_flags |
OLD | NEW |