[Media Router] Add integration tests and e2e tests for media router and presentation...
[chromium-blink-merge.git] / chrome / browser / predictors / resource_prefetch_common_unittest.cc
blob61089d6168129846ef8c671f509c871f0de10c0c
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/browser_test_utils.h"
20 #include "content/public/test/test_browser_thread.h"
21 #include "net/base/network_change_notifier.h"
23 using chrome_browser_net::NetworkPredictionOptions;
24 using net::NetworkChangeNotifier;
26 namespace {
28 class MockNetworkChangeNotifierWIFI : public NetworkChangeNotifier {
29 public:
30 ConnectionType GetCurrentConnectionType() const override {
31 return NetworkChangeNotifier::CONNECTION_WIFI;
35 class MockNetworkChangeNotifier4G : public NetworkChangeNotifier {
36 public:
37 ConnectionType GetCurrentConnectionType() const override {
38 return NetworkChangeNotifier::CONNECTION_4G;
42 } // namespace
44 namespace predictors {
46 class ResourcePrefetchCommonTest : public testing::Test {
47 public:
48 ResourcePrefetchCommonTest();
49 void SetUp() override;
51 void CreateTestFieldTrial(const std::string& name,
52 const std::string& group_name) {
53 base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial(
54 name, group_name);
55 trial->group();
58 void SetPreference(NetworkPredictionOptions value) {
59 profile_->GetPrefs()->SetInteger(prefs::kNetworkPredictionOptions, value);
62 void TestIsPrefetchDisabled(ResourcePrefetchPredictorConfig& config) {
63 EXPECT_FALSE(config.IsLearningEnabled());
64 EXPECT_FALSE(config.IsPrefetchingEnabled(profile_.get()));
65 EXPECT_FALSE(config.IsURLLearningEnabled());
66 EXPECT_FALSE(config.IsHostLearningEnabled());
67 EXPECT_FALSE(config.IsURLPrefetchingEnabled(profile_.get()));
68 EXPECT_FALSE(config.IsHostPrefetchingEnabled(profile_.get()));
71 void TestIsPrefetchEnabled(ResourcePrefetchPredictorConfig& config) {
72 EXPECT_TRUE(config.IsLearningEnabled());
73 EXPECT_TRUE(config.IsPrefetchingEnabled(profile_.get()));
74 EXPECT_TRUE(config.IsURLLearningEnabled());
75 EXPECT_TRUE(config.IsHostLearningEnabled());
76 EXPECT_TRUE(config.IsURLPrefetchingEnabled(profile_.get()));
77 EXPECT_TRUE(config.IsHostPrefetchingEnabled(profile_.get()));
80 void TestIsPrefetchLearning(ResourcePrefetchPredictorConfig& config) {
81 EXPECT_TRUE(config.IsLearningEnabled());
82 EXPECT_FALSE(config.IsPrefetchingEnabled(profile_.get()));
83 EXPECT_TRUE(config.IsURLLearningEnabled());
84 EXPECT_TRUE(config.IsHostLearningEnabled());
85 EXPECT_FALSE(config.IsURLPrefetchingEnabled(profile_.get()));
86 EXPECT_FALSE(config.IsHostPrefetchingEnabled(profile_.get()));
89 void TestIsDefaultExtraConfig(ResourcePrefetchPredictorConfig& config) {
90 EXPECT_FALSE(config.IsLowConfidenceForTest());
91 EXPECT_FALSE(config.IsHighConfidenceForTest());
92 EXPECT_FALSE(config.IsMoreResourcesEnabledForTest());
93 EXPECT_FALSE(config.IsSmallDBEnabledForTest());
96 protected:
97 base::MessageLoop loop_;
98 content::TestBrowserThread ui_thread_;
99 scoped_ptr<TestingProfile> profile_;
101 private:
102 scoped_ptr<base::FieldTrialList> field_trial_list_;
105 ResourcePrefetchCommonTest::ResourcePrefetchCommonTest()
106 : loop_(base::MessageLoop::TYPE_DEFAULT),
107 ui_thread_(content::BrowserThread::UI, &loop_),
108 profile_(new TestingProfile()) { }
110 void ResourcePrefetchCommonTest::SetUp() {
111 field_trial_list_.reset(new base::FieldTrialList(
112 new metrics::SHA1EntropyProvider("ResourcePrefetchCommonTest")));
113 base::StatisticsRecorder::Initialize();
116 TEST_F(ResourcePrefetchCommonTest, FieldTrialNotSpecified) {
117 ResourcePrefetchPredictorConfig config;
118 EXPECT_FALSE(
119 IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
120 TestIsPrefetchDisabled(config);
123 TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingDisabled) {
124 CreateTestFieldTrial("SpeculativeResourcePrefetching",
125 "Prefetching=Disabled");
126 ResourcePrefetchPredictorConfig config;
127 EXPECT_FALSE(
128 IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
129 TestIsPrefetchDisabled(config);
132 TEST_F(ResourcePrefetchCommonTest, FieldTrialLearningHost) {
133 CreateTestFieldTrial("SpeculativeResourcePrefetching",
134 "Prefetching=Learning:Predictor=Host");
135 ResourcePrefetchPredictorConfig config;
136 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
137 EXPECT_TRUE(config.IsLearningEnabled());
138 EXPECT_FALSE(config.IsPrefetchingEnabled(profile_.get()));
139 EXPECT_FALSE(config.IsURLLearningEnabled());
140 EXPECT_TRUE(config.IsHostLearningEnabled());
141 EXPECT_FALSE(config.IsURLPrefetchingEnabled(profile_.get()));
142 EXPECT_FALSE(config.IsHostPrefetchingEnabled(profile_.get()));
143 TestIsDefaultExtraConfig(config);
146 TEST_F(ResourcePrefetchCommonTest, FieldTrialLearningURL) {
147 CreateTestFieldTrial("SpeculativeResourcePrefetching",
148 "Prefetching=Learning:Predictor=Url");
149 ResourcePrefetchPredictorConfig config;
150 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
151 EXPECT_TRUE(config.IsLearningEnabled());
152 EXPECT_FALSE(config.IsPrefetchingEnabled(profile_.get()));
153 EXPECT_TRUE(config.IsURLLearningEnabled());
154 EXPECT_FALSE(config.IsHostLearningEnabled());
155 EXPECT_FALSE(config.IsURLPrefetchingEnabled(profile_.get()));
156 EXPECT_FALSE(config.IsHostPrefetchingEnabled(profile_.get()));
157 TestIsDefaultExtraConfig(config);
160 TEST_F(ResourcePrefetchCommonTest, FieldTrialLearning) {
161 CreateTestFieldTrial("SpeculativeResourcePrefetching",
162 "Prefetching=Learning");
163 ResourcePrefetchPredictorConfig config;
164 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
165 TestIsPrefetchLearning(config);
166 TestIsDefaultExtraConfig(config);
169 TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingHost) {
170 CreateTestFieldTrial("SpeculativeResourcePrefetching",
171 "Prefetching=Enabled:Predictor=Host");
172 ResourcePrefetchPredictorConfig config;
173 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
174 EXPECT_TRUE(config.IsLearningEnabled());
175 EXPECT_TRUE(config.IsPrefetchingEnabled(profile_.get()));
176 EXPECT_FALSE(config.IsURLLearningEnabled());
177 EXPECT_TRUE(config.IsHostLearningEnabled());
178 EXPECT_FALSE(config.IsURLPrefetchingEnabled(profile_.get()));
179 EXPECT_TRUE(config.IsHostPrefetchingEnabled(profile_.get()));
180 TestIsDefaultExtraConfig(config);
183 TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingURL) {
184 CreateTestFieldTrial("SpeculativeResourcePrefetching",
185 "Prefetching=Enabled:Predictor=Url");
186 ResourcePrefetchPredictorConfig config;
187 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
188 EXPECT_TRUE(config.IsLearningEnabled());
189 EXPECT_TRUE(config.IsPrefetchingEnabled(profile_.get()));
190 EXPECT_TRUE(config.IsURLLearningEnabled());
191 EXPECT_FALSE(config.IsHostLearningEnabled());
192 EXPECT_TRUE(config.IsURLPrefetchingEnabled(profile_.get()));
193 EXPECT_FALSE(config.IsHostPrefetchingEnabled(profile_.get()));
194 TestIsDefaultExtraConfig(config);
197 TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetching) {
198 CreateTestFieldTrial("SpeculativeResourcePrefetching", "Prefetching=Enabled");
199 ResourcePrefetchPredictorConfig config;
200 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
201 TestIsPrefetchEnabled(config);
202 TestIsDefaultExtraConfig(config);
205 TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingLowConfidence) {
206 CreateTestFieldTrial("SpeculativeResourcePrefetching",
207 "Prefetching=Enabled:Confidence=Low");
208 ResourcePrefetchPredictorConfig config;
209 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
210 TestIsPrefetchEnabled(config);
211 EXPECT_TRUE(config.IsLowConfidenceForTest());
212 EXPECT_FALSE(config.IsHighConfidenceForTest());
213 EXPECT_FALSE(config.IsMoreResourcesEnabledForTest());
214 EXPECT_FALSE(config.IsSmallDBEnabledForTest());
217 TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingHighConfidence) {
218 CreateTestFieldTrial("SpeculativeResourcePrefetching",
219 "Prefetching=Enabled:Confidence=High");
220 ResourcePrefetchPredictorConfig config;
221 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
222 TestIsPrefetchEnabled(config);
223 EXPECT_FALSE(config.IsLowConfidenceForTest());
224 EXPECT_TRUE(config.IsHighConfidenceForTest());
225 EXPECT_FALSE(config.IsMoreResourcesEnabledForTest());
226 EXPECT_FALSE(config.IsSmallDBEnabledForTest());
229 TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingMoreResources) {
230 CreateTestFieldTrial("SpeculativeResourcePrefetching",
231 "Prefetching=Learning:MoreResources=Enabled");
232 ResourcePrefetchPredictorConfig config;
233 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
234 TestIsPrefetchLearning(config);
235 EXPECT_FALSE(config.IsLowConfidenceForTest());
236 EXPECT_FALSE(config.IsHighConfidenceForTest());
237 EXPECT_TRUE(config.IsMoreResourcesEnabledForTest());
238 EXPECT_FALSE(config.IsSmallDBEnabledForTest());
241 TEST_F(ResourcePrefetchCommonTest, FieldTrialLearningSmallDB) {
242 CreateTestFieldTrial("SpeculativeResourcePrefetching",
243 "Prefetching=Learning:SmallDB=Enabled");
244 ResourcePrefetchPredictorConfig config;
245 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
246 TestIsPrefetchLearning(config);
247 EXPECT_FALSE(config.IsLowConfidenceForTest());
248 EXPECT_FALSE(config.IsHighConfidenceForTest());
249 EXPECT_FALSE(config.IsMoreResourcesEnabledForTest());
250 EXPECT_TRUE(config.IsSmallDBEnabledForTest());
253 TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingSmallDB) {
254 CreateTestFieldTrial("SpeculativeResourcePrefetching",
255 "Prefetching=Enabled:SmallDB=Enabled");
256 ResourcePrefetchPredictorConfig config;
257 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
258 TestIsPrefetchEnabled(config);
259 EXPECT_FALSE(config.IsLowConfidenceForTest());
260 EXPECT_FALSE(config.IsHighConfidenceForTest());
261 EXPECT_FALSE(config.IsMoreResourcesEnabledForTest());
262 EXPECT_TRUE(config.IsSmallDBEnabledForTest());
265 TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingSmallDBLowConfidence) {
266 CreateTestFieldTrial("SpeculativeResourcePrefetching",
267 "Prefetching=Enabled:SmallDB=Enabled:Confidence=Low");
268 ResourcePrefetchPredictorConfig config;
269 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
270 TestIsPrefetchEnabled(config);
271 EXPECT_TRUE(config.IsLowConfidenceForTest());
272 EXPECT_FALSE(config.IsHighConfidenceForTest());
273 EXPECT_FALSE(config.IsMoreResourcesEnabledForTest());
274 EXPECT_TRUE(config.IsSmallDBEnabledForTest());
277 TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingSmallDBHighConfidence) {
278 CreateTestFieldTrial("SpeculativeResourcePrefetching",
279 "Prefetching=Enabled:SmallDB=Enabled:Confidence=High");
280 ResourcePrefetchPredictorConfig config;
281 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
282 TestIsPrefetchEnabled(config);
283 EXPECT_FALSE(config.IsLowConfidenceForTest());
284 EXPECT_TRUE(config.IsHighConfidenceForTest());
285 EXPECT_FALSE(config.IsMoreResourcesEnabledForTest());
286 EXPECT_TRUE(config.IsSmallDBEnabledForTest());
289 // Verifies whether prefetching in the field trial is disabled according to
290 // the network type. But learning should not be disabled by network.
291 TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingDisabledByNetwork) {
292 CreateTestFieldTrial("SpeculativeResourcePrefetching",
293 "Prefetching=Enabled");
294 ResourcePrefetchPredictorConfig config;
295 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
296 TestIsPrefetchEnabled(config);
298 // Set preference to WIFI_ONLY: prefetch when not on cellular.
299 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_WIFI_ONLY);
301 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifierWIFI);
302 TestIsPrefetchEnabled(config);
305 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifier4G);
306 TestIsPrefetchLearning(config);
309 // Set preference to ALWAYS: always prefetch.
310 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_ALWAYS);
312 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifierWIFI);
313 TestIsPrefetchEnabled(config);
316 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifier4G);
317 TestIsPrefetchEnabled(config);
320 // Set preference to NEVER: never prefetch.
321 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_NEVER);
323 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifierWIFI);
324 TestIsPrefetchLearning(config);
327 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifier4G);
328 TestIsPrefetchLearning(config);
332 } // namespace predictors