• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 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/test/test_suite_helper.h"
6 
7 #include "base/base_switches.h"
8 #include "base/command_line.h"
9 #include "base/test/scoped_feature_list.h"
10 
11 namespace base::test {
12 
InitScopedFeatureListForTesting(ScopedFeatureList & scoped_feature_list)13 void InitScopedFeatureListForTesting(ScopedFeatureList& scoped_feature_list) {
14   const CommandLine* command_line = CommandLine::ForCurrentProcess();
15 
16   // We set up a FeatureList via ScopedFeatureList::InitFromCommandLine().
17   // This ensures that code using that API will not hit an error that it's
18   // not set. It will be cleared by ~ScopedFeatureList().
19 
20   // TestFeatureForBrowserTest1 and TestFeatureForBrowserTest2 used in
21   // ContentBrowserTestScopedFeatureListTest to ensure ScopedFeatureList keeps
22   // features from command line.
23   // TestBlinkFeatureDefault is used in RuntimeEnabledFeaturesTest to test a
24   // behavior with OverrideState::OVERIDE_USE_DEFAULT.
25   std::string enabled =
26       command_line->GetSwitchValueASCII(switches::kEnableFeatures);
27   std::string disabled =
28       command_line->GetSwitchValueASCII(switches::kDisableFeatures);
29   enabled += ",TestFeatureForBrowserTest1,*TestBlinkFeatureDefault";
30   disabled += ",TestFeatureForBrowserTest2";
31   scoped_feature_list.InitFromCommandLine(enabled, disabled);
32 
33   // The enable-features and disable-features flags were just slurped into a
34   // FeatureList, so remove them from the command line. Tests should enable
35   // and disable features via the ScopedFeatureList API rather than
36   // command-line flags.
37   CommandLine new_command_line(command_line->GetProgram());
38   CommandLine::SwitchMap switches = command_line->GetSwitches();
39 
40   switches.erase(switches::kEnableFeatures);
41   switches.erase(switches::kDisableFeatures);
42 
43   for (const auto& iter : switches) {
44     new_command_line.AppendSwitchNative(iter.first, iter.second);
45   }
46 
47   *CommandLine::ForCurrentProcess() = new_command_line;
48 }
49 
50 }  // namespace base::test
51