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.
5 #include "base/scoped_add_feature_flags.h"
10 #include "base/base_switches.h"
11 #include "base/command_line.h"
12 #include "base/feature_list.h"
13 #include "base/strings/string_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
20 // Converts a string to CommandLine::StringType, which is std::wstring on
21 // Windows and std::string on other platforms.
22 CommandLine::StringType ToCommandLineStringType(StringPiece s) {
23 return CommandLine::StringType(s.begin(), s.end());
28 TEST(ScopedAddFeatureFlags, ConflictWithExistingFlags) {
29 CommandLine command_line(CommandLine::NO_PROGRAM);
30 command_line.AppendSwitchASCII(switches::kEnableFeatures,
31 "ExistingEnabledFoo,ExistingEnabledBar");
32 command_line.AppendSwitchASCII(switches::kDisableFeatures,
33 "ExistingDisabledFoo,ExistingDisabledBar");
35 static BASE_FEATURE(kExistingEnabledFoo, "ExistingEnabledFoo",
36 FEATURE_DISABLED_BY_DEFAULT);
37 static BASE_FEATURE(kExistingDisabledFoo, "ExistingDisabledFoo",
38 FEATURE_DISABLED_BY_DEFAULT);
39 static BASE_FEATURE(kEnabledBaz, "EnabledBaz", FEATURE_DISABLED_BY_DEFAULT);
40 static BASE_FEATURE(kDisabledBaz, "DisabledBaz", FEATURE_DISABLED_BY_DEFAULT);
42 ScopedAddFeatureFlags scoped_add(&command_line);
43 scoped_add.EnableIfNotSet(kExistingEnabledFoo);
44 scoped_add.EnableIfNotSet(kExistingDisabledFoo);
45 scoped_add.EnableIfNotSet(kEnabledBaz);
46 scoped_add.DisableIfNotSet(kExistingEnabledFoo);
47 scoped_add.DisableIfNotSet(kExistingDisabledFoo);
48 scoped_add.DisableIfNotSet(kDisabledBaz);
51 EXPECT_EQ(std::string("ExistingEnabledFoo,ExistingEnabledBar,EnabledBaz"),
52 command_line.GetSwitchValueASCII(switches::kEnableFeatures));
53 EXPECT_EQ(std::string("ExistingDisabledFoo,ExistingDisabledBar,DisabledBaz"),
54 command_line.GetSwitchValueASCII(switches::kDisableFeatures));
56 // There should not be duplicate --enable-features or --disable-features flags
58 ToCommandLineStringType(
59 " --enable-features=ExistingEnabledFoo,ExistingEnabledBar,EnabledBaz"
60 " --disable-features=ExistingDisabledFoo,ExistingDisabledBar,"
62 JoinString(command_line.argv(), ToCommandLineStringType(" ")));
65 TEST(ScopedAddFeatureFlags, FlagWithParameter) {
66 CommandLine command_line(CommandLine::NO_PROGRAM);
67 command_line.AppendSwitchASCII(switches::kEnableFeatures,
68 "ExistingEnabledFoo");
69 static BASE_FEATURE(kExistingEnabledFoo, "ExistingEnabledFoo",
70 FEATURE_DISABLED_BY_DEFAULT);
71 static BASE_FEATURE(kFeatureWithParameter, "FeatureWithParam",
72 FEATURE_DISABLED_BY_DEFAULT);
75 ScopedAddFeatureFlags scoped_add(&command_line);
76 scoped_add.EnableIfNotSet(kExistingEnabledFoo);
77 scoped_add.EnableIfNotSetWithParameter(kFeatureWithParameter, "name",
79 EXPECT_TRUE(scoped_add.IsEnabledWithParameter(kFeatureWithParameter, "name",
83 EXPECT_EQ(std::string("ExistingEnabledFoo,FeatureWithParam:name/value"),
84 command_line.GetSwitchValueASCII(switches::kEnableFeatures));