with_feature_override.h 1.8 KB

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