• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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 
5 #include "base/test/scoped_feature_list.h"
6 
7 #include <string>
8 
9 namespace base {
10 namespace test {
11 
12 namespace {
13 
GetFeatureString(const std::initializer_list<base::Feature> & features)14 static std::string GetFeatureString(
15     const std::initializer_list<base::Feature>& features) {
16   std::string output;
17   for (const base::Feature& feature : features) {
18     if (!output.empty())
19       output += ",";
20     output += feature.name;
21   }
22   return output;
23 }
24 
25 }  // namespace
26 
ScopedFeatureList()27 ScopedFeatureList::ScopedFeatureList() {}
28 
~ScopedFeatureList()29 ScopedFeatureList::~ScopedFeatureList() {
30   if (original_feature_list_) {
31     base::FeatureList::ClearInstanceForTesting();
32     base::FeatureList::RestoreInstanceForTesting(
33         std::move(original_feature_list_));
34   }
35 }
36 
Init()37 void ScopedFeatureList::Init() {
38   std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
39   feature_list->InitializeFromCommandLine(std::string(), std::string());
40   InitWithFeatureList(std::move(feature_list));
41 }
42 
InitWithFeatures(const std::initializer_list<base::Feature> & enabled_features,const std::initializer_list<base::Feature> & disabled_features)43 void ScopedFeatureList::InitWithFeatures(
44     const std::initializer_list<base::Feature>& enabled_features,
45     const std::initializer_list<base::Feature>& disabled_features) {
46   InitFromCommandLine(GetFeatureString(enabled_features),
47                       GetFeatureString(disabled_features));
48 }
49 
InitWithFeatureList(std::unique_ptr<FeatureList> feature_list)50 void ScopedFeatureList::InitWithFeatureList(
51     std::unique_ptr<FeatureList> feature_list) {
52   DCHECK(!original_feature_list_);
53   original_feature_list_ = base::FeatureList::ClearInstanceForTesting();
54   base::FeatureList::SetInstance(std::move(feature_list));
55 }
56 
InitFromCommandLine(const std::string & enable_features,const std::string & disable_features)57 void ScopedFeatureList::InitFromCommandLine(
58     const std::string& enable_features,
59     const std::string& disable_features) {
60   std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
61   feature_list->InitializeFromCommandLine(enable_features, disable_features);
62   InitWithFeatureList(std::move(feature_list));
63 }
64 
InitAndEnableFeature(const base::Feature & feature)65 void ScopedFeatureList::InitAndEnableFeature(const base::Feature& feature) {
66   InitFromCommandLine(feature.name, std::string());
67 }
68 
InitAndDisableFeature(const base::Feature & feature)69 void ScopedFeatureList::InitAndDisableFeature(const base::Feature& feature) {
70   InitFromCommandLine(std::string(), feature.name);
71 }
72 
73 }  // namespace test
74 }  // namespace base
75