Chromium Code Reviews| Index: chrome/browser/predictors/resource_prefetch_common_unittest.cc |
| diff --git a/chrome/browser/predictors/resource_prefetch_common_unittest.cc b/chrome/browser/predictors/resource_prefetch_common_unittest.cc |
| index 792f2d1f7a00b475408556ca77ccf23a0210eebb..8fe135f7c60e05a2df3ae6545bc32dcc261e865a 100644 |
| --- a/chrome/browser/predictors/resource_prefetch_common_unittest.cc |
| +++ b/chrome/browser/predictors/resource_prefetch_common_unittest.cc |
| @@ -2,20 +2,20 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| +#include <memory> |
| +#include <string> |
| + |
| +#include "base/command_line.h" |
| #include "base/memory/ptr_util.h" |
| #include "base/message_loop/message_loop.h" |
| -#include "base/metrics/field_trial.h" |
| -#include "base/metrics/statistics_recorder.h" |
| #include "chrome/browser/net/prediction_options.h" |
| #include "chrome/browser/predictors/resource_prefetch_common.h" |
| #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
| -#include "chrome/browser/predictors/resource_prefetch_predictor_factory.h" |
| #include "chrome/browser/profiles/profile.h" |
| -#include "chrome/browser/ui/browser.h" |
| +#include "chrome/common/chrome_switches.h" |
| #include "chrome/common/pref_names.h" |
| #include "chrome/test/base/testing_profile.h" |
| #include "components/prefs/pref_service.h" |
| -#include "components/variations/entropy_provider.h" |
| #include "content/public/test/test_browser_thread.h" |
| #include "net/base/network_change_notifier.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| @@ -39,27 +39,24 @@ class MockNetworkChangeNotifier4G : public NetworkChangeNotifier { |
| } |
| }; |
| -} // namespace |
| +} // namespace |
| namespace predictors { |
| class ResourcePrefetchCommonTest : public testing::Test { |
| public: |
| ResourcePrefetchCommonTest(); |
| - void SetUp() override; |
| - |
| - void CreateTestFieldTrial(const std::string& name, |
| - const std::string& group_name) { |
| - base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial( |
| - name, group_name); |
| - trial->group(); |
| - } |
| void SetPreference(NetworkPredictionOptions value) { |
| profile_->GetPrefs()->SetInteger(prefs::kNetworkPredictionOptions, value); |
| } |
| - void TestIsPrefetchDisabled(ResourcePrefetchPredictorConfig& config) { |
| + void SetCommandLineValue(const std::string& value) { |
| + base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| + switches::kSpeculativeResourcePrefetching, value); |
| + } |
| + |
| + void TestIsPrefetchDisabled(const ResourcePrefetchPredictorConfig& config) { |
| EXPECT_FALSE(config.IsLearningEnabled()); |
| EXPECT_FALSE(config.IsPrefetchingEnabled(profile_.get())); |
| EXPECT_FALSE(config.IsURLLearningEnabled()); |
| @@ -68,7 +65,7 @@ class ResourcePrefetchCommonTest : public testing::Test { |
| EXPECT_FALSE(config.IsHostPrefetchingEnabled(profile_.get())); |
| } |
| - void TestIsPrefetchEnabled(ResourcePrefetchPredictorConfig& config) { |
| + void TestIsPrefetchEnabled(const ResourcePrefetchPredictorConfig& config) { |
| EXPECT_TRUE(config.IsLearningEnabled()); |
| EXPECT_TRUE(config.IsPrefetchingEnabled(profile_.get())); |
| EXPECT_TRUE(config.IsURLLearningEnabled()); |
| @@ -77,7 +74,7 @@ class ResourcePrefetchCommonTest : public testing::Test { |
| EXPECT_TRUE(config.IsHostPrefetchingEnabled(profile_.get())); |
| } |
| - void TestIsPrefetchLearning(ResourcePrefetchPredictorConfig& config) { |
| + void TestIsPrefetchLearning(const ResourcePrefetchPredictorConfig& config) { |
| EXPECT_TRUE(config.IsLearningEnabled()); |
| EXPECT_FALSE(config.IsPrefetchingEnabled(profile_.get())); |
| EXPECT_TRUE(config.IsURLLearningEnabled()); |
| @@ -86,7 +83,7 @@ class ResourcePrefetchCommonTest : public testing::Test { |
| EXPECT_FALSE(config.IsHostPrefetchingEnabled(profile_.get())); |
| } |
| - void TestIsDefaultExtraConfig(ResourcePrefetchPredictorConfig& config) { |
| + void TestIsDefaultExtraConfig(const ResourcePrefetchPredictorConfig& config) { |
| EXPECT_FALSE(config.IsLowConfidenceForTest()); |
| EXPECT_FALSE(config.IsHighConfidenceForTest()); |
| EXPECT_FALSE(config.IsMoreResourcesEnabledForTest()); |
| @@ -97,9 +94,6 @@ class ResourcePrefetchCommonTest : public testing::Test { |
| base::MessageLoop loop_; |
| content::TestBrowserThread ui_thread_; |
| std::unique_ptr<TestingProfile> profile_; |
| - |
| - private: |
| - std::unique_ptr<base::FieldTrialList> field_trial_list_; |
| }; |
| ResourcePrefetchCommonTest::ResourcePrefetchCommonTest() |
| @@ -107,194 +101,39 @@ ResourcePrefetchCommonTest::ResourcePrefetchCommonTest() |
| ui_thread_(content::BrowserThread::UI, &loop_), |
| profile_(new TestingProfile()) { } |
| -void ResourcePrefetchCommonTest::SetUp() { |
| - field_trial_list_.reset(new base::FieldTrialList( |
| - base::MakeUnique<metrics::SHA1EntropyProvider>( |
| - "ResourcePrefetchCommonTest"))); |
| - base::StatisticsRecorder::Initialize(); |
| -} |
| - |
| -TEST_F(ResourcePrefetchCommonTest, FieldTrialNotSpecified) { |
| +TEST_F(ResourcePrefetchCommonTest, IsDisabledByDefault) { |
|
pasko
2016/11/28 16:31:13
On related note: I am not sure the clutter around
Benoit L
2016/11/28 17:09:55
This will become a bit more complex with the exter
|
| ResourcePrefetchPredictorConfig config; |
| EXPECT_FALSE( |
| IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); |
| TestIsPrefetchDisabled(config); |
|
pasko
2016/11/28 16:31:13
This is becoming the single callsite for this func
Benoit L
2016/11/28 17:09:55
Done.
|
| -} |
| - |
| -TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingDisabled) { |
| - CreateTestFieldTrial("SpeculativeResourcePrefetching", |
| - "Prefetching=Disabled"); |
| - ResourcePrefetchPredictorConfig config; |
| - EXPECT_FALSE( |
| - IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); |
| - TestIsPrefetchDisabled(config); |
| -} |
| - |
| -TEST_F(ResourcePrefetchCommonTest, FieldTrialLearningHost) { |
| - CreateTestFieldTrial("SpeculativeResourcePrefetching", |
| - "Prefetching=Learning:Predictor=Host"); |
| - ResourcePrefetchPredictorConfig config; |
| - EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); |
| - EXPECT_TRUE(config.IsLearningEnabled()); |
| - EXPECT_FALSE(config.IsPrefetchingEnabled(profile_.get())); |
| - EXPECT_FALSE(config.IsURLLearningEnabled()); |
| - EXPECT_TRUE(config.IsHostLearningEnabled()); |
| - EXPECT_FALSE(config.IsURLPrefetchingEnabled(profile_.get())); |
| - EXPECT_FALSE(config.IsHostPrefetchingEnabled(profile_.get())); |
| TestIsDefaultExtraConfig(config); |
| } |
| -TEST_F(ResourcePrefetchCommonTest, FieldTrialLearningURL) { |
| - CreateTestFieldTrial("SpeculativeResourcePrefetching", |
| - "Prefetching=Learning:Predictor=Url"); |
| - ResourcePrefetchPredictorConfig config; |
| - EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); |
| - EXPECT_TRUE(config.IsLearningEnabled()); |
| - EXPECT_FALSE(config.IsPrefetchingEnabled(profile_.get())); |
| - EXPECT_TRUE(config.IsURLLearningEnabled()); |
| - EXPECT_FALSE(config.IsHostLearningEnabled()); |
| - EXPECT_FALSE(config.IsURLPrefetchingEnabled(profile_.get())); |
| - EXPECT_FALSE(config.IsHostPrefetchingEnabled(profile_.get())); |
| - TestIsDefaultExtraConfig(config); |
| -} |
| - |
| -TEST_F(ResourcePrefetchCommonTest, FieldTrialLearning) { |
| - CreateTestFieldTrial("SpeculativeResourcePrefetching", |
| - "Prefetching=Learning"); |
| +TEST_F(ResourcePrefetchCommonTest, EnableLearning) { |
| + SetCommandLineValue("learning"); |
| ResourcePrefetchPredictorConfig config; |
| EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); |
| TestIsPrefetchLearning(config); |
| TestIsDefaultExtraConfig(config); |
| } |
| -TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingHost) { |
| - CreateTestFieldTrial("SpeculativeResourcePrefetching", |
| - "Prefetching=Enabled:Predictor=Host"); |
| - ResourcePrefetchPredictorConfig config; |
| - EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); |
| - EXPECT_TRUE(config.IsLearningEnabled()); |
| - EXPECT_TRUE(config.IsPrefetchingEnabled(profile_.get())); |
| - EXPECT_FALSE(config.IsURLLearningEnabled()); |
| - EXPECT_TRUE(config.IsHostLearningEnabled()); |
| - EXPECT_FALSE(config.IsURLPrefetchingEnabled(profile_.get())); |
| - EXPECT_TRUE(config.IsHostPrefetchingEnabled(profile_.get())); |
| - TestIsDefaultExtraConfig(config); |
| -} |
| - |
| -TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingURL) { |
| - CreateTestFieldTrial("SpeculativeResourcePrefetching", |
| - "Prefetching=Enabled:Predictor=Url"); |
| - ResourcePrefetchPredictorConfig config; |
| - EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); |
| - EXPECT_TRUE(config.IsLearningEnabled()); |
| - EXPECT_TRUE(config.IsPrefetchingEnabled(profile_.get())); |
| - EXPECT_TRUE(config.IsURLLearningEnabled()); |
| - EXPECT_FALSE(config.IsHostLearningEnabled()); |
| - EXPECT_TRUE(config.IsURLPrefetchingEnabled(profile_.get())); |
| - EXPECT_FALSE(config.IsHostPrefetchingEnabled(profile_.get())); |
| - TestIsDefaultExtraConfig(config); |
| -} |
| - |
| -TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetching) { |
| - CreateTestFieldTrial("SpeculativeResourcePrefetching", "Prefetching=Enabled"); |
| +TEST_F(ResourcePrefetchCommonTest, EnablePrefetch) { |
| + SetCommandLineValue("enabled"); |
| ResourcePrefetchPredictorConfig config; |
| EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); |
| TestIsPrefetchEnabled(config); |
| TestIsDefaultExtraConfig(config); |
| } |
| -TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingLowConfidence) { |
| - CreateTestFieldTrial("SpeculativeResourcePrefetching", |
| - "Prefetching=Enabled:Confidence=Low"); |
| - ResourcePrefetchPredictorConfig config; |
| - EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); |
| - TestIsPrefetchEnabled(config); |
| - EXPECT_TRUE(config.IsLowConfidenceForTest()); |
| - EXPECT_FALSE(config.IsHighConfidenceForTest()); |
| - EXPECT_FALSE(config.IsMoreResourcesEnabledForTest()); |
| - EXPECT_FALSE(config.IsSmallDBEnabledForTest()); |
| -} |
| - |
| -TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingHighConfidence) { |
| - CreateTestFieldTrial("SpeculativeResourcePrefetching", |
| - "Prefetching=Enabled:Confidence=High"); |
| - ResourcePrefetchPredictorConfig config; |
| - EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); |
| - TestIsPrefetchEnabled(config); |
| - EXPECT_FALSE(config.IsLowConfidenceForTest()); |
| - EXPECT_TRUE(config.IsHighConfidenceForTest()); |
| - EXPECT_FALSE(config.IsMoreResourcesEnabledForTest()); |
| - EXPECT_FALSE(config.IsSmallDBEnabledForTest()); |
| -} |
| - |
| -TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingMoreResources) { |
| - CreateTestFieldTrial("SpeculativeResourcePrefetching", |
| - "Prefetching=Learning:MoreResources=Enabled"); |
| - ResourcePrefetchPredictorConfig config; |
| - EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); |
| - TestIsPrefetchLearning(config); |
| - EXPECT_FALSE(config.IsLowConfidenceForTest()); |
| - EXPECT_FALSE(config.IsHighConfidenceForTest()); |
| - EXPECT_TRUE(config.IsMoreResourcesEnabledForTest()); |
| - EXPECT_FALSE(config.IsSmallDBEnabledForTest()); |
| -} |
| - |
| -TEST_F(ResourcePrefetchCommonTest, FieldTrialLearningSmallDB) { |
| - CreateTestFieldTrial("SpeculativeResourcePrefetching", |
| - "Prefetching=Learning:SmallDB=Enabled"); |
| - ResourcePrefetchPredictorConfig config; |
| - EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); |
| - TestIsPrefetchLearning(config); |
| - EXPECT_FALSE(config.IsLowConfidenceForTest()); |
| - EXPECT_FALSE(config.IsHighConfidenceForTest()); |
| - EXPECT_FALSE(config.IsMoreResourcesEnabledForTest()); |
| - EXPECT_TRUE(config.IsSmallDBEnabledForTest()); |
| -} |
| - |
| -TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingSmallDB) { |
| - CreateTestFieldTrial("SpeculativeResourcePrefetching", |
| - "Prefetching=Enabled:SmallDB=Enabled"); |
| - ResourcePrefetchPredictorConfig config; |
| - EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); |
| - TestIsPrefetchEnabled(config); |
| - EXPECT_FALSE(config.IsLowConfidenceForTest()); |
| - EXPECT_FALSE(config.IsHighConfidenceForTest()); |
| - EXPECT_FALSE(config.IsMoreResourcesEnabledForTest()); |
| - EXPECT_TRUE(config.IsSmallDBEnabledForTest()); |
| -} |
| - |
| -TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingSmallDBLowConfidence) { |
| - CreateTestFieldTrial("SpeculativeResourcePrefetching", |
| - "Prefetching=Enabled:SmallDB=Enabled:Confidence=Low"); |
| - ResourcePrefetchPredictorConfig config; |
| - EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); |
| - TestIsPrefetchEnabled(config); |
| - EXPECT_TRUE(config.IsLowConfidenceForTest()); |
| - EXPECT_FALSE(config.IsHighConfidenceForTest()); |
| - EXPECT_FALSE(config.IsMoreResourcesEnabledForTest()); |
| - EXPECT_TRUE(config.IsSmallDBEnabledForTest()); |
| -} |
| - |
| -TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingSmallDBHighConfidence) { |
| - CreateTestFieldTrial("SpeculativeResourcePrefetching", |
| - "Prefetching=Enabled:SmallDB=Enabled:Confidence=High"); |
| - ResourcePrefetchPredictorConfig config; |
| - EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); |
| - TestIsPrefetchEnabled(config); |
| - EXPECT_FALSE(config.IsLowConfidenceForTest()); |
| - EXPECT_TRUE(config.IsHighConfidenceForTest()); |
| - EXPECT_FALSE(config.IsMoreResourcesEnabledForTest()); |
| - EXPECT_TRUE(config.IsSmallDBEnabledForTest()); |
| -} |
| // Verifies whether prefetching in the field trial is disabled according to |
|
pasko
2016/11/28 16:31:13
Should we remove the mention of the field trial?
Benoit L
2016/11/28 17:09:55
Done.
|
| // the network type. But learning should not be disabled by network. |
| -TEST_F(ResourcePrefetchCommonTest, FieldTrialPrefetchingDisabledByNetwork) { |
| - CreateTestFieldTrial("SpeculativeResourcePrefetching", |
| - "Prefetching=Enabled"); |
| +TEST_F(ResourcePrefetchCommonTest, RespectsNetworkSettings) { |
| + SetCommandLineValue("enabled"); |
| ResourcePrefetchPredictorConfig config; |
| EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); |
| TestIsPrefetchEnabled(config); |
| + TestIsDefaultExtraConfig(config); |
| // Set preference to WIFI_ONLY: prefetch when not on cellular. |
| SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_WIFI_ONLY); |