scoped_add_feature_flags_unittest.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/scoped_add_feature_flags.h"
  5. #include <cstring>
  6. #include <string>
  7. #include "base/base_switches.h"
  8. #include "base/command_line.h"
  9. #include "base/feature_list.h"
  10. #include "base/strings/string_util.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace base {
  13. namespace {
  14. // Converts a string to CommandLine::StringType, which is std::wstring on
  15. // Windows and std::string on other platforms.
  16. CommandLine::StringType ToCommandLineStringType(StringPiece s) {
  17. return CommandLine::StringType(s.begin(), s.end());
  18. }
  19. } // namespace
  20. TEST(ScopedAddFeatureFlags, ConflictWithExistingFlags) {
  21. CommandLine command_line(CommandLine::NO_PROGRAM);
  22. command_line.AppendSwitchASCII(switches::kEnableFeatures,
  23. "ExistingEnabledFoo,ExistingEnabledBar");
  24. command_line.AppendSwitchASCII(switches::kDisableFeatures,
  25. "ExistingDisabledFoo,ExistingDisabledBar");
  26. const Feature kExistingEnabledFoo{"ExistingEnabledFoo",
  27. FEATURE_DISABLED_BY_DEFAULT};
  28. const Feature kExistingDisabledFoo{"ExistingDisabledFoo",
  29. FEATURE_DISABLED_BY_DEFAULT};
  30. const Feature kEnabledBaz{"EnabledBaz", FEATURE_DISABLED_BY_DEFAULT};
  31. const Feature kDisabledBaz{"DisabledBaz", FEATURE_DISABLED_BY_DEFAULT};
  32. {
  33. ScopedAddFeatureFlags scoped_add(&command_line);
  34. scoped_add.EnableIfNotSet(kExistingEnabledFoo);
  35. scoped_add.EnableIfNotSet(kExistingDisabledFoo);
  36. scoped_add.EnableIfNotSet(kEnabledBaz);
  37. scoped_add.DisableIfNotSet(kExistingEnabledFoo);
  38. scoped_add.DisableIfNotSet(kExistingDisabledFoo);
  39. scoped_add.DisableIfNotSet(kDisabledBaz);
  40. }
  41. EXPECT_EQ(std::string("ExistingEnabledFoo,ExistingEnabledBar,EnabledBaz"),
  42. command_line.GetSwitchValueASCII(switches::kEnableFeatures));
  43. EXPECT_EQ(std::string("ExistingDisabledFoo,ExistingDisabledBar,DisabledBaz"),
  44. command_line.GetSwitchValueASCII(switches::kDisableFeatures));
  45. // There should not be duplicate --enable-features or --disable-features flags
  46. EXPECT_EQ(
  47. ToCommandLineStringType(
  48. " --enable-features=ExistingEnabledFoo,ExistingEnabledBar,EnabledBaz"
  49. " --disable-features=ExistingDisabledFoo,ExistingDisabledBar,"
  50. "DisabledBaz"),
  51. JoinString(command_line.argv(), ToCommandLineStringType(" ")));
  52. }
  53. TEST(ScopedAddFeatureFlags, FlagWithParameter) {
  54. CommandLine command_line(CommandLine::NO_PROGRAM);
  55. command_line.AppendSwitchASCII(switches::kEnableFeatures,
  56. "ExistingEnabledFoo");
  57. const Feature kExistingEnabledFoo{"ExistingEnabledFoo",
  58. FEATURE_DISABLED_BY_DEFAULT};
  59. const Feature kFeatureWithParameter{"FeatureWithParam",
  60. FEATURE_DISABLED_BY_DEFAULT};
  61. {
  62. ScopedAddFeatureFlags scoped_add(&command_line);
  63. scoped_add.EnableIfNotSet(kExistingEnabledFoo);
  64. scoped_add.EnableIfNotSetWithParameter(kFeatureWithParameter, "name",
  65. "value");
  66. EXPECT_TRUE(scoped_add.IsEnabledWithParameter(kFeatureWithParameter, "name",
  67. "value"));
  68. }
  69. EXPECT_EQ(std::string("ExistingEnabledFoo,FeatureWithParam:name/value"),
  70. command_line.GetSwitchValueASCII(switches::kEnableFeatures));
  71. }
  72. } // namespace base