Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / chrome / browser / predictors / resource_prefetch_common_unittest.cc
blob59ef5c4f58501d351c066adc9cc73c1e5bb12de7
1 // Copyright 2014 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.
5 #include "base/message_loop/message_loop.h"
6 #include "base/metrics/field_trial.h"
7 #include "base/metrics/statistics_recorder.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/net/prediction_options.h"
10 #include "chrome/browser/predictors/resource_prefetch_common.h"
11 #include "chrome/browser/predictors/resource_prefetch_predictor.h"
12 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/test/base/in_process_browser_test.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "components/variations/entropy_provider.h"
19 #include "content/public/test/test_browser_thread.h"
20 #include "net/base/network_change_notifier.h"
22 using chrome_browser_net::NetworkPredictionOptions;
23 using net::NetworkChangeNotifier;
25 namespace {
27 class MockNetworkChangeNotifierWIFI : public NetworkChangeNotifier {
28 public:
29 ConnectionType GetCurrentConnectionType() const override {
30 return NetworkChangeNotifier::CONNECTION_WIFI;
34 class MockNetworkChangeNotifier4G : public NetworkChangeNotifier {
35 public:
36 ConnectionType GetCurrentConnectionType() const override {
37 return NetworkChangeNotifier::CONNECTION_4G;
41 } // namespace
43 namespace predictors {
45 class ResourcePrefetchCommonTest : public testing::Test {
46 public:
47 ResourcePrefetchCommonTest();
48 void SetUp() override;
50 void CreateTestFieldTrial(const std::string& name,
51 const std::string& group_name) {
52 base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial(
53 name, group_name);
54 trial->group();
57 void SetPreference(NetworkPredictionOptions value) {
58 profile_->GetPrefs()->SetInteger(prefs::kNetworkPredictionOptions, value);
61 void TestIsPrefetchDisabled(ResourcePrefetchPredictorConfig& config) {
62 EXPECT_FALSE(config.IsLearningEnabled());
63 EXPECT_FALSE(config.IsPrefetchingEnabled(profile_.get()));
64 EXPECT_FALSE(config.IsURLLearningEnabled());
65 EXPECT_FALSE(config.IsHostLearningEnabled());
66 EXPECT_FALSE(config.IsURLPrefetchingEnabled(profile_.get()));
67 EXPECT_FALSE(config.IsHostPrefetchingEnabled(profile_.get()));
70 void TestIsPrefetchEnabled(ResourcePrefetchPredictorConfig& config) {
71 EXPECT_TRUE(config.IsLearningEnabled());
72 EXPECT_TRUE(config.IsPrefetchingEnabled(profile_.get()));
73 EXPECT_TRUE(config.IsURLLearningEnabled());
74 EXPECT_TRUE(config.IsHostLearningEnabled());
75 EXPECT_TRUE(config.IsURLPrefetchingEnabled(profile_.get()));
76 EXPECT_TRUE(config.IsHostPrefetchingEnabled(profile_.get()));
79 void TestIsPrefetchLearning(ResourcePrefetchPredictorConfig& config) {
80 EXPECT_TRUE(config.IsLearningEnabled());
81 EXPECT_FALSE(config.IsPrefetchingEnabled(profile_.get()));
82 EXPECT_TRUE(config.IsURLLearningEnabled());
83 EXPECT_TRUE(config.IsHostLearningEnabled());
84 EXPECT_FALSE(config.IsURLPrefetchingEnabled(profile_.get()));
85 EXPECT_FALSE(config.IsHostPrefetchingEnabled(profile_.get()));
88 void TestIsDefaultExtraConfig(ResourcePrefetchPredictorConfig& config) {
89 EXPECT_FALSE(config.IsLowConfidenceForTest());
90 EXPECT_FALSE(config.IsHighConfidenceForTest());
91 EXPECT_FALSE(config.IsMoreResourcesEnabledForTest());
92 EXPECT_FALSE(config.IsSmallDBEnabledForTest());
95 protected:
96 base::MessageLoop loop_;
97 content::TestBrowserThread ui_thread_;
98 scoped_ptr<TestingProfile> profile_;
100 private:
101 scoped_ptr<base::FieldTrialList> field_trial_list_;
104 ResourcePrefetchCommonTest::ResourcePrefetchCommonTest()
105 : loop_(base::MessageLoop::TYPE_DEFAULT),
106 ui_thread_(content::BrowserThread::UI, &loop_),
107 profile_(new TestingProfile()) { }
109 void ResourcePrefetchCommonTest::SetUp() {
110 field_trial_list_.reset(new base::FieldTrialList(
111 new metrics::SHA1EntropyProvider("ResourcePrefetchCommonTest")));
112 base::StatisticsRecorder::Initialize();
115 TEST_F(ResourcePrefetchCommonTest, FieldTrialNotSpecified) {
116 ResourcePrefetchPredictorConfig config;
117 EXPECT_FALSE(
118 IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
119 TestIsPrefetchDisabled(config);
122 TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingDisabled) {
123 CreateTestFieldTrial("SpeculativeResourcePrefetching",
124 "Prefetching=Disabled");
125 ResourcePrefetchPredictorConfig config;
126 EXPECT_FALSE(
127 IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
128 TestIsPrefetchDisabled(config);
131 TEST_F(ResourcePrefetchCommonTest, FieldTrialLearningHost) {
132 CreateTestFieldTrial("SpeculativeResourcePrefetching",
133 "Prefetching=Learning:Predictor=Host");
134 ResourcePrefetchPredictorConfig config;
135 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
136 EXPECT_TRUE(config.IsLearningEnabled());
137 EXPECT_FALSE(config.IsPrefetchingEnabled(profile_.get()));
138 EXPECT_FALSE(config.IsURLLearningEnabled());
139 EXPECT_TRUE(config.IsHostLearningEnabled());
140 EXPECT_FALSE(config.IsURLPrefetchingEnabled(profile_.get()));
141 EXPECT_FALSE(config.IsHostPrefetchingEnabled(profile_.get()));
142 TestIsDefaultExtraConfig(config);
145 TEST_F(ResourcePrefetchCommonTest, FieldTrialLearningURL) {
146 CreateTestFieldTrial("SpeculativeResourcePrefetching",
147 "Prefetching=Learning:Predictor=Url");
148 ResourcePrefetchPredictorConfig config;
149 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
150 EXPECT_TRUE(config.IsLearningEnabled());
151 EXPECT_FALSE(config.IsPrefetchingEnabled(profile_.get()));
152 EXPECT_TRUE(config.IsURLLearningEnabled());
153 EXPECT_FALSE(config.IsHostLearningEnabled());
154 EXPECT_FALSE(config.IsURLPrefetchingEnabled(profile_.get()));
155 EXPECT_FALSE(config.IsHostPrefetchingEnabled(profile_.get()));
156 TestIsDefaultExtraConfig(config);
159 TEST_F(ResourcePrefetchCommonTest, FieldTrialLearning) {
160 CreateTestFieldTrial("SpeculativeResourcePrefetching",
161 "Prefetching=Learning");
162 ResourcePrefetchPredictorConfig config;
163 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
164 TestIsPrefetchLearning(config);
165 TestIsDefaultExtraConfig(config);
168 TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingHost) {
169 CreateTestFieldTrial("SpeculativeResourcePrefetching",
170 "Prefetching=Enabled:Predictor=Host");
171 ResourcePrefetchPredictorConfig config;
172 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
173 EXPECT_TRUE(config.IsLearningEnabled());
174 EXPECT_TRUE(config.IsPrefetchingEnabled(profile_.get()));
175 EXPECT_FALSE(config.IsURLLearningEnabled());
176 EXPECT_TRUE(config.IsHostLearningEnabled());
177 EXPECT_FALSE(config.IsURLPrefetchingEnabled(profile_.get()));
178 EXPECT_TRUE(config.IsHostPrefetchingEnabled(profile_.get()));
179 TestIsDefaultExtraConfig(config);
182 TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingURL) {
183 CreateTestFieldTrial("SpeculativeResourcePrefetching",
184 "Prefetching=Enabled:Predictor=Url");
185 ResourcePrefetchPredictorConfig config;
186 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
187 EXPECT_TRUE(config.IsLearningEnabled());
188 EXPECT_TRUE(config.IsPrefetchingEnabled(profile_.get()));
189 EXPECT_TRUE(config.IsURLLearningEnabled());
190 EXPECT_FALSE(config.IsHostLearningEnabled());
191 EXPECT_TRUE(config.IsURLPrefetchingEnabled(profile_.get()));
192 EXPECT_FALSE(config.IsHostPrefetchingEnabled(profile_.get()));
193 TestIsDefaultExtraConfig(config);
196 TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetching) {
197 CreateTestFieldTrial("SpeculativeResourcePrefetching", "Prefetching=Enabled");
198 ResourcePrefetchPredictorConfig config;
199 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
200 TestIsPrefetchEnabled(config);
201 TestIsDefaultExtraConfig(config);
204 TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingLowConfidence) {
205 CreateTestFieldTrial("SpeculativeResourcePrefetching",
206 "Prefetching=Enabled:Confidence=Low");
207 ResourcePrefetchPredictorConfig config;
208 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
209 TestIsPrefetchEnabled(config);
210 EXPECT_TRUE(config.IsLowConfidenceForTest());
211 EXPECT_FALSE(config.IsHighConfidenceForTest());
212 EXPECT_FALSE(config.IsMoreResourcesEnabledForTest());
213 EXPECT_FALSE(config.IsSmallDBEnabledForTest());
216 TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingHighConfidence) {
217 CreateTestFieldTrial("SpeculativeResourcePrefetching",
218 "Prefetching=Enabled:Confidence=High");
219 ResourcePrefetchPredictorConfig config;
220 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
221 TestIsPrefetchEnabled(config);
222 EXPECT_FALSE(config.IsLowConfidenceForTest());
223 EXPECT_TRUE(config.IsHighConfidenceForTest());
224 EXPECT_FALSE(config.IsMoreResourcesEnabledForTest());
225 EXPECT_FALSE(config.IsSmallDBEnabledForTest());
228 TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingMoreResources) {
229 CreateTestFieldTrial("SpeculativeResourcePrefetching",
230 "Prefetching=Learning:MoreResources=Enabled");
231 ResourcePrefetchPredictorConfig config;
232 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
233 TestIsPrefetchLearning(config);
234 EXPECT_FALSE(config.IsLowConfidenceForTest());
235 EXPECT_FALSE(config.IsHighConfidenceForTest());
236 EXPECT_TRUE(config.IsMoreResourcesEnabledForTest());
237 EXPECT_FALSE(config.IsSmallDBEnabledForTest());
240 TEST_F(ResourcePrefetchCommonTest, FieldTrialLearningSmallDB) {
241 CreateTestFieldTrial("SpeculativeResourcePrefetching",
242 "Prefetching=Learning:SmallDB=Enabled");
243 ResourcePrefetchPredictorConfig config;
244 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
245 TestIsPrefetchLearning(config);
246 EXPECT_FALSE(config.IsLowConfidenceForTest());
247 EXPECT_FALSE(config.IsHighConfidenceForTest());
248 EXPECT_FALSE(config.IsMoreResourcesEnabledForTest());
249 EXPECT_TRUE(config.IsSmallDBEnabledForTest());
252 TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingSmallDB) {
253 CreateTestFieldTrial("SpeculativeResourcePrefetching",
254 "Prefetching=Enabled:SmallDB=Enabled");
255 ResourcePrefetchPredictorConfig config;
256 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
257 TestIsPrefetchEnabled(config);
258 EXPECT_FALSE(config.IsLowConfidenceForTest());
259 EXPECT_FALSE(config.IsHighConfidenceForTest());
260 EXPECT_FALSE(config.IsMoreResourcesEnabledForTest());
261 EXPECT_TRUE(config.IsSmallDBEnabledForTest());
264 TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingSmallDBLowConfidence) {
265 CreateTestFieldTrial("SpeculativeResourcePrefetching",
266 "Prefetching=Enabled:SmallDB=Enabled:Confidence=Low");
267 ResourcePrefetchPredictorConfig config;
268 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
269 TestIsPrefetchEnabled(config);
270 EXPECT_TRUE(config.IsLowConfidenceForTest());
271 EXPECT_FALSE(config.IsHighConfidenceForTest());
272 EXPECT_FALSE(config.IsMoreResourcesEnabledForTest());
273 EXPECT_TRUE(config.IsSmallDBEnabledForTest());
276 TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingSmallDBHighConfidence) {
277 CreateTestFieldTrial("SpeculativeResourcePrefetching",
278 "Prefetching=Enabled:SmallDB=Enabled:Confidence=High");
279 ResourcePrefetchPredictorConfig config;
280 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
281 TestIsPrefetchEnabled(config);
282 EXPECT_FALSE(config.IsLowConfidenceForTest());
283 EXPECT_TRUE(config.IsHighConfidenceForTest());
284 EXPECT_FALSE(config.IsMoreResourcesEnabledForTest());
285 EXPECT_TRUE(config.IsSmallDBEnabledForTest());
288 // Verifies whether prefetching in the field trial is disabled according to
289 // the network type. But learning should not be disabled by network.
290 TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingDisabledByNetwork) {
291 CreateTestFieldTrial("SpeculativeResourcePrefetching",
292 "Prefetching=Enabled");
293 ResourcePrefetchPredictorConfig config;
294 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
295 TestIsPrefetchEnabled(config);
297 // Set preference to WIFI_ONLY: prefetch when not on cellular.
298 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_WIFI_ONLY);
300 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifierWIFI);
301 TestIsPrefetchEnabled(config);
304 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifier4G);
305 TestIsPrefetchLearning(config);
308 // Set preference to ALWAYS: always prefetch.
309 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_ALWAYS);
311 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifierWIFI);
312 TestIsPrefetchEnabled(config);
315 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifier4G);
316 TestIsPrefetchEnabled(config);
319 // Set preference to NEVER: never prefetch.
320 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_NEVER);
322 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifierWIFI);
323 TestIsPrefetchLearning(config);
326 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifier4G);
327 TestIsPrefetchLearning(config);
331 } // namespace predictors