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