Sophie Chang | a98063c | 2017-11-17 21:20:49 | [diff] [blame] | 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 | |
| 5 | #include "components/optimization_guide/test_component_creator.h" |
| 6 | |
| 7 | #include "base/files/file_util.h" |
| 8 | #include "base/strings/string_number_conversions.h" |
| 9 | #include "base/threading/thread_restrictions.h" |
| 10 | #include "base/version.h" |
| 11 | #include "components/optimization_guide/proto/hints.pb.h" |
| 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
| 14 | namespace optimization_guide { |
| 15 | namespace testing { |
| 16 | |
| 17 | TestComponentCreator::TestComponentCreator() |
| 18 | : scoped_temp_dir_(std::make_unique<base::ScopedTempDir>()), |
| 19 | next_component_version_(1) {} |
| 20 | |
| 21 | TestComponentCreator::~TestComponentCreator() { |
| 22 | base::ScopedAllowBlockingForTesting allow_blocking; |
| 23 | scoped_temp_dir_.reset(); |
| 24 | } |
| 25 | |
| 26 | optimization_guide::ComponentInfo |
Jered Gray | 68b5a64 | 2018-10-04 00:54:59 | [diff] [blame] | 27 | TestComponentCreator::CreateComponentInfoWithTopLevelWhitelist( |
Tarun Bansal | dc97029 | 2018-07-12 00:02:58 | [diff] [blame] | 28 | optimization_guide::proto::OptimizationType optimization_type, |
| 29 | const std::vector<std::string>& whitelisted_host_suffixes) { |
Jered Gray | 68b5a64 | 2018-10-04 00:54:59 | [diff] [blame] | 30 | optimization_guide::proto::Configuration config; |
| 31 | for (const auto& whitelisted_site : whitelisted_host_suffixes) { |
| 32 | optimization_guide::proto::Hint* hint = config.add_hints(); |
| 33 | hint->set_key(whitelisted_site); |
| 34 | hint->set_key_representation(optimization_guide::proto::HOST_SUFFIX); |
| 35 | |
| 36 | optimization_guide::proto::Optimization* optimization = |
| 37 | hint->add_whitelisted_optimizations(); |
| 38 | optimization->set_optimization_type(optimization_type); |
| 39 | } |
| 40 | |
| 41 | return WriteConfigToFileAndReturnComponentInfo(config); |
| 42 | } |
| 43 | |
| 44 | optimization_guide::ComponentInfo |
| 45 | TestComponentCreator::CreateComponentInfoWithPageHints( |
| 46 | optimization_guide::proto::OptimizationType optimization_type, |
| 47 | const std::vector<std::string>& page_hint_host_suffixes, |
| 48 | const std::vector<std::string>& resource_blocking_patterns) { |
| 49 | optimization_guide::proto::Configuration config; |
| 50 | for (const auto& page_hint_site : page_hint_host_suffixes) { |
| 51 | optimization_guide::proto::Hint* hint = config.add_hints(); |
| 52 | hint->set_key(page_hint_site); |
| 53 | hint->set_key_representation(optimization_guide::proto::HOST_SUFFIX); |
| 54 | |
| 55 | optimization_guide::proto::PageHint* page_hint = hint->add_page_hints(); |
| 56 | page_hint->set_page_pattern("*"); |
| 57 | |
| 58 | optimization_guide::proto::Optimization* optimization = |
| 59 | page_hint->add_whitelisted_optimizations(); |
| 60 | optimization->set_optimization_type(optimization_type); |
| 61 | |
| 62 | for (auto resource_blocking_pattern : resource_blocking_patterns) { |
| 63 | optimization_guide::proto::ResourceLoadingHint* resource_loading_hint = |
| 64 | optimization->add_resource_loading_hints(); |
| 65 | resource_loading_hint->set_loading_optimization_type( |
| 66 | optimization_guide::proto::LOADING_BLOCK_RESOURCE); |
| 67 | resource_loading_hint->set_resource_pattern(resource_blocking_pattern); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return WriteConfigToFileAndReturnComponentInfo(config); |
Sophie Chang | a98063c | 2017-11-17 21:20:49 | [diff] [blame] | 72 | } |
| 73 | |
Tarun Bansal | 2c28f7c1 | 2018-11-05 17:57:16 | [diff] [blame] | 74 | optimization_guide::ComponentInfo |
| 75 | TestComponentCreator::CreateComponentInfoWithExperimentalPageHints( |
| 76 | optimization_guide::proto::OptimizationType optimization_type, |
| 77 | const std::vector<std::string>& page_hint_host_suffixes, |
| 78 | const std::vector<std::string>& experimental_resource_patterns) { |
| 79 | optimization_guide::proto::Configuration config; |
| 80 | for (const auto& page_hint_site : page_hint_host_suffixes) { |
| 81 | optimization_guide::proto::Hint* hint = config.add_hints(); |
| 82 | hint->set_key(page_hint_site); |
| 83 | hint->set_key_representation(optimization_guide::proto::HOST_SUFFIX); |
| 84 | |
| 85 | optimization_guide::proto::PageHint* page_hint = hint->add_page_hints(); |
| 86 | page_hint->set_page_pattern("*"); |
| 87 | |
| 88 | optimization_guide::proto::Optimization* optimization = |
| 89 | page_hint->add_whitelisted_optimizations(); |
| 90 | optimization->set_optimization_type(optimization_type); |
| 91 | optimization->set_experiment_name(kFooExperimentName); |
| 92 | |
| 93 | for (auto resource_blocking_pattern : experimental_resource_patterns) { |
| 94 | optimization_guide::proto::ResourceLoadingHint* resource_loading_hint = |
| 95 | optimization->add_resource_loading_hints(); |
| 96 | resource_loading_hint->set_loading_optimization_type( |
| 97 | optimization_guide::proto::LOADING_BLOCK_RESOURCE); |
| 98 | resource_loading_hint->set_resource_pattern(resource_blocking_pattern); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return WriteConfigToFileAndReturnComponentInfo(config); |
| 103 | } |
| 104 | |
| 105 | optimization_guide::ComponentInfo |
| 106 | TestComponentCreator::CreateComponentInfoWithMixPageHints( |
| 107 | optimization_guide::proto::OptimizationType optimization_type, |
| 108 | const std::vector<std::string>& page_hint_host_suffixes, |
| 109 | const std::vector<std::string>& experimental_resource_patterns, |
| 110 | const std::vector<std::string>& default_resource_patterns) { |
| 111 | optimization_guide::proto::Configuration config; |
| 112 | for (const auto& page_hint_site : page_hint_host_suffixes) { |
| 113 | optimization_guide::proto::Hint* hint = config.add_hints(); |
| 114 | hint->set_key(page_hint_site); |
| 115 | hint->set_key_representation(optimization_guide::proto::HOST_SUFFIX); |
| 116 | |
| 117 | optimization_guide::proto::PageHint* page_hint = hint->add_page_hints(); |
| 118 | page_hint->set_page_pattern("*"); |
| 119 | |
| 120 | // Add experimental patterns first so they get higher priority. |
| 121 | { |
| 122 | optimization_guide::proto::Optimization* optimization = |
| 123 | page_hint->add_whitelisted_optimizations(); |
| 124 | optimization->set_optimization_type(optimization_type); |
| 125 | optimization->set_experiment_name(kFooExperimentName); |
| 126 | |
| 127 | for (auto resource_blocking_pattern : experimental_resource_patterns) { |
| 128 | optimization_guide::proto::ResourceLoadingHint* resource_loading_hint = |
| 129 | optimization->add_resource_loading_hints(); |
| 130 | resource_loading_hint->set_loading_optimization_type( |
| 131 | optimization_guide::proto::LOADING_BLOCK_RESOURCE); |
| 132 | resource_loading_hint->set_resource_pattern(resource_blocking_pattern); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | { |
| 137 | optimization_guide::proto::Optimization* optimization = |
| 138 | page_hint->add_whitelisted_optimizations(); |
| 139 | optimization->set_optimization_type(optimization_type); |
| 140 | |
| 141 | for (auto resource_blocking_pattern : default_resource_patterns) { |
| 142 | optimization_guide::proto::ResourceLoadingHint* resource_loading_hint = |
| 143 | optimization->add_resource_loading_hints(); |
| 144 | resource_loading_hint->set_loading_optimization_type( |
| 145 | optimization_guide::proto::LOADING_BLOCK_RESOURCE); |
| 146 | resource_loading_hint->set_resource_pattern(resource_blocking_pattern); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return WriteConfigToFileAndReturnComponentInfo(config); |
| 152 | } |
| 153 | |
Sophie Chang | a98063c | 2017-11-17 21:20:49 | [diff] [blame] | 154 | base::FilePath TestComponentCreator::GetFilePath(std::string file_path_suffix) { |
| 155 | base::ScopedAllowBlockingForTesting allow_blocking; |
| 156 | EXPECT_TRUE(scoped_temp_dir_->IsValid() || |
| 157 | scoped_temp_dir_->CreateUniqueTempDir()); |
| 158 | return scoped_temp_dir_->GetPath().AppendASCII(file_path_suffix); |
| 159 | } |
| 160 | |
| 161 | void TestComponentCreator::WriteConfigToFile( |
Jered Gray | 68b5a64 | 2018-10-04 00:54:59 | [diff] [blame] | 162 | const base::FilePath& file_path, |
| 163 | const optimization_guide::proto::Configuration& config) { |
Sophie Chang | a98063c | 2017-11-17 21:20:49 | [diff] [blame] | 164 | base::ScopedAllowBlockingForTesting allow_blocking; |
| 165 | |
Sophie Chang | a98063c | 2017-11-17 21:20:49 | [diff] [blame] | 166 | std::string serialized_config; |
| 167 | ASSERT_TRUE(config.SerializeToString(&serialized_config)); |
| 168 | |
| 169 | ASSERT_EQ(static_cast<int32_t>(serialized_config.length()), |
| 170 | base::WriteFile(file_path, serialized_config.data(), |
| 171 | serialized_config.length())); |
| 172 | } |
| 173 | |
Jered Gray | 68b5a64 | 2018-10-04 00:54:59 | [diff] [blame] | 174 | optimization_guide::ComponentInfo |
| 175 | TestComponentCreator::WriteConfigToFileAndReturnComponentInfo( |
| 176 | const optimization_guide::proto::Configuration& config) { |
| 177 | std::string version_string = base::IntToString(next_component_version_++); |
| 178 | base::FilePath file_path = GetFilePath(version_string); |
| 179 | WriteConfigToFile(file_path, config); |
| 180 | return optimization_guide::ComponentInfo(base::Version(version_string), |
| 181 | file_path); |
| 182 | } |
| 183 | |
Sophie Chang | a98063c | 2017-11-17 21:20:49 | [diff] [blame] | 184 | } // namespace testing |
| 185 | } // namespace optimization_guide |