optimization_hints_component_installer_unittest.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #include "components/component_updater/installer_policies/optimization_hints_component_installer.h"
  5. #include <utility>
  6. #include "base/files/file_path.h"
  7. #include "base/files/file_util.h"
  8. #include "base/files/scoped_temp_dir.h"
  9. #include "base/run_loop.h"
  10. #include "base/task/single_thread_task_runner.h"
  11. #include "base/test/scoped_feature_list.h"
  12. #include "base/test/task_environment.h"
  13. #include "base/threading/thread_task_runner_handle.h"
  14. #include "base/version.h"
  15. #include "components/component_updater/mock_component_updater_service.h"
  16. #include "components/optimization_guide/core/optimization_guide_constants.h"
  17. #include "components/optimization_guide/core/optimization_guide_features.h"
  18. #include "components/optimization_guide/core/optimization_hints_component_update_listener.h"
  19. #include "testing/gtest/include/gtest/gtest.h"
  20. #include "testing/platform_test.h"
  21. namespace {
  22. static const char kTestHintsVersion[] = "1.2.3";
  23. class OptimizationHintsMockComponentUpdateService
  24. : public component_updater::MockComponentUpdateService {
  25. public:
  26. OptimizationHintsMockComponentUpdateService() = default;
  27. OptimizationHintsMockComponentUpdateService(
  28. const OptimizationHintsMockComponentUpdateService&) = delete;
  29. OptimizationHintsMockComponentUpdateService& operator=(
  30. const OptimizationHintsMockComponentUpdateService&) = delete;
  31. ~OptimizationHintsMockComponentUpdateService() override = default;
  32. };
  33. } // namespace
  34. namespace component_updater {
  35. class OptimizationHintsComponentInstallerTest : public PlatformTest {
  36. public:
  37. OptimizationHintsComponentInstallerTest() = default;
  38. OptimizationHintsComponentInstallerTest(
  39. const OptimizationHintsComponentInstallerTest&) = delete;
  40. OptimizationHintsComponentInstallerTest& operator=(
  41. const OptimizationHintsComponentInstallerTest&) = delete;
  42. ~OptimizationHintsComponentInstallerTest() override = default;
  43. void SetUp() override {
  44. PlatformTest::SetUp();
  45. ASSERT_TRUE(component_install_dir_.CreateUniqueTempDir());
  46. policy_ = std::make_unique<OptimizationHintsComponentInstallerPolicy>();
  47. }
  48. base::FilePath component_install_dir() {
  49. return component_install_dir_.GetPath();
  50. }
  51. base::Version ruleset_format_version() {
  52. return policy_->ruleset_format_version_;
  53. }
  54. void CreateTestOptimizationHints(const std::string& hints_content) {
  55. base::FilePath hints_path = component_install_dir().Append(
  56. optimization_guide::kUnindexedHintsFileName);
  57. ASSERT_TRUE(base::WriteFile(hints_path, hints_content));
  58. }
  59. void LoadOptimizationHints(const base::Version& ruleset_format) {
  60. base::Value manifest(base::Value::Type::DICTIONARY);
  61. if (ruleset_format.IsValid()) {
  62. manifest.SetStringKey(
  63. OptimizationHintsComponentInstallerPolicy::kManifestRulesetFormatKey,
  64. ruleset_format.GetString());
  65. }
  66. ASSERT_TRUE(policy_->VerifyInstallation(manifest, component_install_dir()));
  67. const base::Version expected_version(kTestHintsVersion);
  68. policy_->ComponentReady(expected_version, component_install_dir(),
  69. std::move(manifest));
  70. base::RunLoop().RunUntilIdle();
  71. }
  72. protected:
  73. void RunUntilIdle() {
  74. task_environment_.RunUntilIdle();
  75. base::RunLoop().RunUntilIdle();
  76. }
  77. private:
  78. base::test::TaskEnvironment task_environment_;
  79. base::ScopedTempDir component_install_dir_;
  80. std::unique_ptr<OptimizationHintsComponentInstallerPolicy> policy_;
  81. };
  82. TEST_F(OptimizationHintsComponentInstallerTest,
  83. ComponentRegistrationWhenFeatureDisabled) {
  84. base::test::ScopedFeatureList scoped_list;
  85. scoped_list.InitAndDisableFeature(
  86. optimization_guide::features::kOptimizationHints);
  87. std::unique_ptr<OptimizationHintsMockComponentUpdateService> cus(
  88. new OptimizationHintsMockComponentUpdateService());
  89. EXPECT_CALL(*cus, RegisterComponent(testing::_)).Times(0);
  90. RegisterOptimizationHintsComponent(cus.get());
  91. RunUntilIdle();
  92. }
  93. TEST_F(OptimizationHintsComponentInstallerTest,
  94. ComponentRegistrationWhenFeatureEnabled) {
  95. base::test::ScopedFeatureList scoped_list;
  96. scoped_list.InitAndEnableFeature(
  97. optimization_guide::features::kOptimizationHints);
  98. std::unique_ptr<OptimizationHintsMockComponentUpdateService> cus(
  99. new OptimizationHintsMockComponentUpdateService());
  100. EXPECT_CALL(*cus, RegisterComponent(testing::_))
  101. .Times(1)
  102. .WillOnce(testing::Return(true));
  103. RegisterOptimizationHintsComponent(cus.get());
  104. RunUntilIdle();
  105. }
  106. TEST_F(OptimizationHintsComponentInstallerTest, NoRulesetFormatIgnored) {
  107. ASSERT_NO_FATAL_FAILURE(CreateTestOptimizationHints("some hints"));
  108. ASSERT_NO_FATAL_FAILURE(LoadOptimizationHints(base::Version("")));
  109. EXPECT_FALSE(optimization_guide::OptimizationHintsComponentUpdateListener::
  110. GetInstance()
  111. ->hints_component_info()
  112. .has_value());
  113. }
  114. TEST_F(OptimizationHintsComponentInstallerTest, FutureRulesetFormatIgnored) {
  115. ASSERT_NO_FATAL_FAILURE(CreateTestOptimizationHints("some hints"));
  116. base::Version version = ruleset_format_version();
  117. const std::vector<uint32_t> future_ruleset_components = {
  118. version.components()[0] + 1, version.components()[1],
  119. version.components()[2]};
  120. ASSERT_NO_FATAL_FAILURE(
  121. LoadOptimizationHints(base::Version(future_ruleset_components)));
  122. EXPECT_FALSE(optimization_guide::OptimizationHintsComponentUpdateListener::
  123. GetInstance()
  124. ->hints_component_info()
  125. .has_value());
  126. }
  127. TEST_F(OptimizationHintsComponentInstallerTest, LoadFileWithData) {
  128. const std::string expected_hints = "some hints";
  129. ASSERT_NO_FATAL_FAILURE(CreateTestOptimizationHints(expected_hints));
  130. ASSERT_NO_FATAL_FAILURE(LoadOptimizationHints(ruleset_format_version()));
  131. absl::optional<optimization_guide::HintsComponentInfo> component_info =
  132. optimization_guide::OptimizationHintsComponentUpdateListener::
  133. GetInstance()
  134. ->hints_component_info();
  135. EXPECT_TRUE(component_info.has_value());
  136. EXPECT_EQ(base::Version(kTestHintsVersion), component_info->version);
  137. std::string actual_hints;
  138. ASSERT_TRUE(base::ReadFileToString(component_info->path, &actual_hints));
  139. EXPECT_EQ(expected_hints, actual_hints);
  140. }
  141. } // namespace component_updater