OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 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" |
| 22 |
| 23 using chrome_browser_net::NetworkPredictionOptions; |
| 24 using net::NetworkChangeNotifier; |
| 25 |
| 26 namespace { |
| 27 |
| 28 class MockNetworkChangeNotifierWIFI : public NetworkChangeNotifier { |
| 29 public: |
| 30 virtual ConnectionType GetCurrentConnectionType() const OVERRIDE { |
| 31 return NetworkChangeNotifier::CONNECTION_WIFI; |
| 32 } |
| 33 }; |
| 34 |
| 35 class MockNetworkChangeNotifier4G : public NetworkChangeNotifier { |
| 36 public: |
| 37 virtual ConnectionType GetCurrentConnectionType() const OVERRIDE { |
| 38 return NetworkChangeNotifier::CONNECTION_4G; |
| 39 } |
| 40 }; |
| 41 |
| 42 } // namespace |
| 43 |
| 44 namespace predictors { |
| 45 |
| 46 class ResourcePrefetchCommonTest : public testing::Test { |
| 47 public: |
| 48 ResourcePrefetchCommonTest(); |
| 49 virtual void SetUp() OVERRIDE; |
| 50 |
| 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(); |
| 56 } |
| 57 |
| 58 void SetPreference(NetworkPredictionOptions value) { |
| 59 profile_->GetPrefs()->SetInteger(prefs::kNetworkPredictionOptions, value); |
| 60 } |
| 61 |
| 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())); |
| 69 } |
| 70 |
| 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())); |
| 78 } |
| 79 |
| 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())); |
| 87 } |
| 88 |
| 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()); |
| 94 } |
| 95 |
| 96 protected: |
| 97 base::MessageLoop loop_; |
| 98 content::TestBrowserThread ui_thread_; |
| 99 scoped_ptr<TestingProfile> profile_; |
| 100 |
| 101 private: |
| 102 scoped_ptr<base::FieldTrialList> field_trial_list_; |
| 103 }; |
| 104 |
| 105 ResourcePrefetchCommonTest::ResourcePrefetchCommonTest() |
| 106 : loop_(base::MessageLoop::TYPE_DEFAULT), |
| 107 ui_thread_(content::BrowserThread::UI, &loop_), |
| 108 profile_(new TestingProfile()) { } |
| 109 |
| 110 void ResourcePrefetchCommonTest::SetUp() { |
| 111 field_trial_list_.reset(new base::FieldTrialList( |
| 112 new metrics::SHA1EntropyProvider("ResourcePrefetchCommonTest"))); |
| 113 base::StatisticsRecorder::Initialize(); |
| 114 } |
| 115 |
| 116 TEST_F(ResourcePrefetchCommonTest, FieldTrialNotSpecified) { |
| 117 ResourcePrefetchPredictorConfig config; |
| 118 EXPECT_FALSE( |
| 119 IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); |
| 120 TestIsPrefetchDisabled(config); |
| 121 } |
| 122 |
| 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); |
| 130 } |
| 131 |
| 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); |
| 144 } |
| 145 |
| 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); |
| 158 } |
| 159 |
| 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); |
| 167 } |
| 168 |
| 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); |
| 181 } |
| 182 |
| 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); |
| 195 } |
| 196 |
| 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); |
| 203 } |
| 204 |
| 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()); |
| 215 } |
| 216 |
| 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()); |
| 227 } |
| 228 |
| 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()); |
| 239 } |
| 240 |
| 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()); |
| 251 } |
| 252 |
| 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()); |
| 263 } |
| 264 |
| 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()); |
| 275 } |
| 276 |
| 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()); |
| 287 } |
| 288 |
| 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); |
| 297 |
| 298 // Set preference to WIFI_ONLY: prefetch when not on cellular. |
| 299 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_WIFI_ONLY); |
| 300 { |
| 301 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifierWIFI); |
| 302 TestIsPrefetchEnabled(config); |
| 303 } |
| 304 { |
| 305 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifier4G); |
| 306 TestIsPrefetchLearning(config); |
| 307 } |
| 308 |
| 309 // Set preference to ALWAYS: always prefetch. |
| 310 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_ALWAYS); |
| 311 { |
| 312 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifierWIFI); |
| 313 TestIsPrefetchEnabled(config); |
| 314 } |
| 315 { |
| 316 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifier4G); |
| 317 TestIsPrefetchEnabled(config); |
| 318 } |
| 319 |
| 320 // Set preference to NEVER: never prefetch. |
| 321 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_NEVER); |
| 322 { |
| 323 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifierWIFI); |
| 324 TestIsPrefetchLearning(config); |
| 325 } |
| 326 { |
| 327 scoped_ptr<NetworkChangeNotifier> mock(new MockNetworkChangeNotifier4G); |
| 328 TestIsPrefetchLearning(config); |
| 329 } |
| 330 } |
| 331 |
| 332 } // namespace predictors |
OLD | NEW |