xref: /aosp_15_r20/external/cronet/base/test/with_feature_override.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2020 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 #ifndef BASE_TEST_WITH_FEATURE_OVERRIDE_H_
6 #define BASE_TEST_WITH_FEATURE_OVERRIDE_H_
7 
8 #include "base/feature_list.h"
9 #include "base/test/scoped_feature_list.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 
12 namespace base {
13 namespace test {
14 
15 #define INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(test_name) \
16   INSTANTIATE_TEST_SUITE_P(All, test_name, testing::Values(false, true))
17 
18 // Base class for a test fixture that enables running tests twice, once with a
19 // feature enabled and once with it disabled. Must be the first base class of
20 // the test fixture to take effect during its construction. If
21 // WithFeatureOverride is added as a parent to an existing test fixture
22 // all of its existing tests need to be migrated to TEST_P.
23 //
24 // Example usage:
25 //
26 //  class MyTest : public base::test::WithFeatureOverride, public testing::Test
27 //  {
28 //   public:
29 //    MyTest() : base::test::WithFeatureOverride(kMyFeature){}
30 //  };
31 //
32 //  TEST_P(MyTest, FooBar) {
33 //    This will run with both the kMyFeature enabled and disabled.
34 //  }
35 //
36 //  INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(MyTest);
37 
38 class WithFeatureOverride : public testing::WithParamInterface<bool> {
39  public:
40   explicit WithFeatureOverride(const base::Feature& feature);
41   ~WithFeatureOverride();
42 
43   WithFeatureOverride(const WithFeatureOverride&) = delete;
44   WithFeatureOverride& operator=(const WithFeatureOverride&) = delete;
45 
46   // Use to know if the configured feature provided in the constructor is
47   // enabled or not.
48   bool IsParamFeatureEnabled();
49 
50  private:
51   base::test::ScopedFeatureList scoped_feature_list_;
52 };
53 
54 }  // namespace test
55 }  // namespace base
56 
57 #endif  // BASE_TEST_WITH_FEATURE_OVERRIDE_H_
58