with_feature_override.h 1.8 KB

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