blob: ac159f8a32b29e1e4ea0d21f4d3066930ac1e351 [file] [log] [blame]
Sophie Changa98063c2017-11-17 21:20:491// 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
14namespace optimization_guide {
15namespace testing {
16
17TestComponentCreator::TestComponentCreator()
18 : scoped_temp_dir_(std::make_unique<base::ScopedTempDir>()),
19 next_component_version_(1) {}
20
21TestComponentCreator::~TestComponentCreator() {
22 base::ScopedAllowBlockingForTesting allow_blocking;
23 scoped_temp_dir_.reset();
24}
25
26optimization_guide::ComponentInfo
Jered Gray68b5a642018-10-04 00:54:5927TestComponentCreator::CreateComponentInfoWithTopLevelWhitelist(
Tarun Bansaldc970292018-07-12 00:02:5828 optimization_guide::proto::OptimizationType optimization_type,
29 const std::vector<std::string>& whitelisted_host_suffixes) {
Jered Gray68b5a642018-10-04 00:54:5930 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
44optimization_guide::ComponentInfo
45TestComponentCreator::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 Changa98063c2017-11-17 21:20:4972}
73
Tarun Bansal2c28f7c12018-11-05 17:57:1674optimization_guide::ComponentInfo
75TestComponentCreator::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
105optimization_guide::ComponentInfo
106TestComponentCreator::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 Changa98063c2017-11-17 21:20:49154base::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
161void TestComponentCreator::WriteConfigToFile(
Jered Gray68b5a642018-10-04 00:54:59162 const base::FilePath& file_path,
163 const optimization_guide::proto::Configuration& config) {
Sophie Changa98063c2017-11-17 21:20:49164 base::ScopedAllowBlockingForTesting allow_blocking;
165
Sophie Changa98063c2017-11-17 21:20:49166 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 Gray68b5a642018-10-04 00:54:59174optimization_guide::ComponentInfo
175TestComponentCreator::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 Changa98063c2017-11-17 21:20:49184} // namespace testing
185} // namespace optimization_guide