tuneable_unittest.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #include "media/base/tuneable.h"
  5. #include "base/strings/string_number_conversions.h"
  6. #include "base/test/scoped_feature_list.h"
  7. #include "media/base/media_switches.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace media {
  10. class TuneableTest : public ::testing::Test {
  11. public:
  12. TuneableTest() = default;
  13. TuneableTest(const TuneableTest&) = delete;
  14. TuneableTest& operator=(const TuneableTest&) = delete;
  15. void SetUp() override {
  16. // Note that we might need to call value() to cache `tuneable_cached_` here.
  17. // We don't currently, since it's not needed.
  18. // Set everything else to non-default values. We do this because we can't
  19. // do it in the tests without it doing odd things.
  20. // params[kTuneableIntSetToNot123] = "124"; // Not 123.
  21. // params[kTuneableInt0] = "0";
  22. // params[kTuneableInt5] = "5";
  23. // params[kTuneableInt10] = "10";
  24. // Set some tuneables to fixed values.
  25. SetFinchParameters(kTuneableIntSetToNot123, 124);
  26. SetFinchParameters(kTuneableInt0, 0);
  27. SetFinchParameters(kTuneableInt5, 5);
  28. SetFinchParameters(kTuneableInt10, 100);
  29. // TimeDelta should be given in milliseconds.
  30. SetFinchParameters(kTuneableTimeDeltaFiveSeconds, 5000);
  31. scoped_feature_list_.InitAndEnableFeatureWithParameters(kMediaOptimizer,
  32. params_);
  33. }
  34. // Set the finch-chosen parameters for tuneable `name`.
  35. void SetFinchParameters(const char* name, int value) {
  36. params_[name] = base::NumberToString(value);
  37. }
  38. // Return the tuneable name for the `x`-th numbered tuneable.
  39. std::string GetNameForNumberedTuneable(const char* basename, int x) {
  40. std::string name(basename);
  41. name[0] = "0123456789ABCDEF"[x & 15];
  42. name[1] = "0123456789ABCDEF"[(x >> 4) & 15];
  43. return name;
  44. }
  45. base::test::ScopedFeatureList scoped_feature_list_;
  46. base::FieldTrialParams params_;
  47. // Params will set this to not 123. We default it to 123 before Setup runs,
  48. // so we make sure that it uses the hardcoded defaults properly.
  49. static constexpr const char* kTuneableIntSetToNot123 = "t_int_not_123";
  50. Tuneable<int> tuneable_cached_{kTuneableIntSetToNot123, 0, 123, 200};
  51. static constexpr const char* kTuneableIntUnset = "t_int_unset";
  52. static constexpr const char* kTuneableInt0 = "t_int_0";
  53. static constexpr const char* kTuneableInt5 = "t_int_5";
  54. static constexpr const char* kTuneableInt10 = "t_int_10";
  55. static constexpr const char* kTuneableTimeDeltaFiveSeconds = "t_time_5s";
  56. };
  57. TEST_F(TuneableTest, IntTuneableCached) {
  58. // Verify that `tuneable_cached_` is, in fact, 123 even though the params try
  59. // to set it to something else. This kind of, sort of, guarantees that it's
  60. // cached properly.
  61. EXPECT_EQ(tuneable_cached_.value(), 123);
  62. }
  63. TEST_F(TuneableTest, IntTuneableFromDefaultWithClamps) {
  64. // The default value should be used, and correctly clamped.
  65. constexpr int min_value = 0;
  66. constexpr int default_value = 4;
  67. constexpr int max_value = 10;
  68. Tuneable<int> t_min(kTuneableIntUnset, min_value, min_value - 1, max_value);
  69. EXPECT_EQ(t_min.value(), min_value);
  70. Tuneable<int> t_default(kTuneableIntUnset, min_value, default_value,
  71. max_value);
  72. EXPECT_EQ(t_default.value(), default_value);
  73. Tuneable<int> t_max(kTuneableIntUnset, min_value, max_value + 1, max_value);
  74. EXPECT_EQ(t_max.value(), max_value);
  75. }
  76. TEST_F(TuneableTest, IntTuneableFromParams) {
  77. // Verify that params override the defaults, and are clamped correctly.
  78. constexpr int min_value = 1;
  79. constexpr int default_value = 4; // That's not the same as the param.
  80. constexpr int max_value = 9;
  81. Tuneable<int> t_min(kTuneableInt0, min_value, default_value, max_value);
  82. EXPECT_EQ(t_min.value(), min_value);
  83. Tuneable<int> t_param(kTuneableInt5, min_value, default_value, max_value);
  84. EXPECT_EQ(t_param.value(), 5);
  85. Tuneable<int> t_max(kTuneableInt10, min_value, default_value, max_value);
  86. EXPECT_EQ(t_max.value(), max_value);
  87. }
  88. TEST_F(TuneableTest, OtherSpecializationsCompile) {
  89. // Since it's all templated, just be happy if it compiles and does something
  90. // somewhat sane.
  91. constexpr base::TimeDelta min_value = base::Seconds(0);
  92. constexpr base::TimeDelta default_value = base::Seconds(5);
  93. constexpr base::TimeDelta max_value = base::Seconds(10);
  94. Tuneable<base::TimeDelta> time_delta_tuneable("whatever", min_value,
  95. default_value, max_value);
  96. // Since the tuneable is not provided in the finch parameters, it should
  97. // equal the default.
  98. EXPECT_EQ(time_delta_tuneable.value(), default_value);
  99. Tuneable<size_t> size_t_tuneable("whatever_else", 0u, 100u, 500u);
  100. EXPECT_EQ(size_t_tuneable.value(), 100u);
  101. }
  102. TEST_F(TuneableTest, TimeDeltaIsSpecifiedInMilliseconds) {
  103. // Since the finch params are constructed with the assumption that the value
  104. // will be interpreted as milliseconds, make sure that the Tuneable actually
  105. // does interpret it that way.
  106. constexpr base::TimeDelta min_value = base::Seconds(0);
  107. constexpr base::TimeDelta max_value = base::Seconds(100);
  108. Tuneable<base::TimeDelta> t(kTuneableTimeDeltaFiveSeconds, min_value,
  109. min_value, max_value);
  110. EXPECT_EQ(t.value(), base::Seconds(5));
  111. }
  112. } // namespace media