variations_params_manager.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #ifndef COMPONENTS_VARIATIONS_VARIATIONS_PARAMS_MANAGER_H_
  5. #define COMPONENTS_VARIATIONS_VARIATIONS_PARAMS_MANAGER_H_
  6. #include <map>
  7. #include <memory>
  8. #include <set>
  9. #include <string>
  10. namespace base {
  11. class CommandLine;
  12. class FieldTrialList;
  13. namespace test {
  14. class ScopedFeatureList;
  15. } // namespace test
  16. } // namespace base
  17. namespace variations {
  18. namespace testing {
  19. // NOTE: THIS CLASS IS DEPRECATED. Please use ScopedFeatureList instead, which
  20. // provides equivalent functionality.
  21. // TODO(asvitkine): Migrate callers and remove this class.
  22. //
  23. // Use this class as a member in your test class to set variation params for
  24. // your tests. You can directly set the parameters in the constructor (if they
  25. // are used by other members upon construction). You can change them later
  26. // arbitrarily many times using the SetVariationParams function. Internally, it
  27. // creates a FieldTrialList as a member. It works well for multiple tests of a
  28. // given test class, as it clears the parameters when this class is destructed.
  29. // Note that it clears all parameters (not just those registered here).
  30. class VariationParamsManager {
  31. public:
  32. // Does not associate any parameters.
  33. VariationParamsManager();
  34. // Calls directly SetVariationParams with the provided arguments.
  35. VariationParamsManager(
  36. const std::string& trial_name,
  37. const std::map<std::string, std::string>& param_values);
  38. // Calls directly SetVariationParamsWithFeatures with the provided arguments.
  39. VariationParamsManager(
  40. const std::string& trial_name,
  41. const std::map<std::string, std::string>& param_values,
  42. const std::set<std::string>& associated_features);
  43. VariationParamsManager(const VariationParamsManager&) = delete;
  44. VariationParamsManager& operator=(const VariationParamsManager&) = delete;
  45. ~VariationParamsManager();
  46. // Associates |param_values| with the given |trial_name|. |param_values| maps
  47. // parameter names to their values. The function creates a new trial group,
  48. // used only for testing. Between two calls of this function,
  49. // ClearAllVariationParams() has to be called.
  50. void SetVariationParams(
  51. const std::string& trial_name,
  52. const std::map<std::string, std::string>& param_values);
  53. // Like SetVariationParams(). |associated_features| lists names of features
  54. // to be associated to the newly created trial group. As a result, all
  55. // parameters from |param_values| can be accessed via any of the feature from
  56. // |associated_features|.
  57. void SetVariationParamsWithFeatureAssociations(
  58. const std::string& trial_name,
  59. const std::map<std::string, std::string>& param_values,
  60. const std::set<std::string>& associated_features);
  61. // Clears all of the mapped associations.
  62. void ClearAllVariationIDs();
  63. // Clears all of the associated params.
  64. void ClearAllVariationParams();
  65. // Appends command line switches to |command_line| in a way that mimics
  66. // SetVariationParams.
  67. //
  68. // This static method is useful in situations where using
  69. // VariationParamsManager directly would have resulted in initializing
  70. // FieldTrialList twice (once from ChromeBrowserMainParts::SetUpFieldTrials
  71. // and once from VariationParamsManager).
  72. static void AppendVariationParams(
  73. const std::string& trial_name,
  74. const std::string& trial_group_name,
  75. const std::map<std::string, std::string>& param_values,
  76. base::CommandLine* command_line);
  77. private:
  78. std::unique_ptr<base::test::ScopedFeatureList> scoped_feature_list_;
  79. };
  80. } // namespace testing
  81. } // namespace variations
  82. #endif // COMPONENTS_VARIATIONS_VARIATIONS_PARAMS_MANAGER_H_