test_hints_component_creator.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2017 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_OPTIMIZATION_GUIDE_CORE_TEST_HINTS_COMPONENT_CREATOR_H_
  5. #define COMPONENTS_OPTIMIZATION_GUIDE_CORE_TEST_HINTS_COMPONENT_CREATOR_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/files/file_path.h"
  9. #include "base/files/scoped_temp_dir.h"
  10. #include "components/optimization_guide/proto/hints.pb.h"
  11. namespace optimization_guide {
  12. struct HintsComponentInfo;
  13. namespace testing {
  14. // Helper class to create test OptimizationHints components for testing.
  15. //
  16. // All temporary files and paths are cleaned up when this instance goes out of
  17. // scope.
  18. class TestHintsComponentCreator {
  19. public:
  20. TestHintsComponentCreator();
  21. TestHintsComponentCreator(const TestHintsComponentCreator&) = delete;
  22. TestHintsComponentCreator& operator=(const TestHintsComponentCreator&) =
  23. delete;
  24. ~TestHintsComponentCreator();
  25. // Creates component data based on |allowlisted_hosts| and
  26. // |page_pattern| with page hints for type |optimization_type| blocking
  27. // resources specified by |resource_patterns|, and returns the
  28. // HintsComponentInfo for it.
  29. optimization_guide::HintsComponentInfo CreateHintsComponentInfoWithPageHints(
  30. optimization_guide::proto::OptimizationType optimization_type,
  31. const std::vector<std::string>& allowlisted_hosts,
  32. const std::string& page_pattern);
  33. private:
  34. // Returns the scoped temp directory path with the |file_path_suffix| that is
  35. // valid for the lifetime of this instance.
  36. // The file itself will not be automatically created.
  37. base::FilePath GetFilePath(std::string file_path_suffix);
  38. // Writes a configuration of hints to the file path.
  39. void WriteConfigToFile(
  40. const base::FilePath& file_path,
  41. const optimization_guide::proto::Configuration& config);
  42. // Writes a configuration of hints to the file path and returns the
  43. // HintsComponentInfo for it.
  44. optimization_guide::HintsComponentInfo
  45. WriteConfigToFileAndReturnHintsComponentInfo(
  46. const optimization_guide::proto::Configuration& config);
  47. std::unique_ptr<base::ScopedTempDir> scoped_temp_dir_;
  48. int next_component_version_;
  49. };
  50. } // namespace testing
  51. } // namespace optimization_guide
  52. #endif // COMPONENTS_OPTIMIZATION_GUIDE_CORE_TEST_HINTS_COMPONENT_CREATOR_H_