Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(389)

Side by Side Diff: chrome/browser/predictors/resource_prefetch_common_unittest.cc

Issue 2652313002: predictors: Move the configuration of speculative prefetch to field trials. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <memory> 5 #include <memory>
6 #include <string> 6 #include <string>
7 7
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
11 #include "chrome/browser/net/prediction_options.h" 11 #include "chrome/browser/net/prediction_options.h"
12 #include "chrome/browser/predictors/resource_prefetch_common.h" 12 #include "chrome/browser/predictors/resource_prefetch_common.h"
13 #include "chrome/browser/predictors/resource_prefetch_predictor.h" 13 #include "chrome/browser/predictors/resource_prefetch_predictor.h"
14 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/pref_names.h" 15 #include "chrome/common/pref_names.h"
17 #include "chrome/test/base/testing_profile.h" 16 #include "chrome/test/base/testing_profile.h"
18 #include "components/prefs/pref_service.h" 17 #include "components/prefs/pref_service.h"
18 #include "components/variations/variations_params_manager.h"
19 #include "content/public/test/test_browser_thread.h" 19 #include "content/public/test/test_browser_thread.h"
20 #include "net/base/network_change_notifier.h" 20 #include "net/base/network_change_notifier.h"
21 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
22 22
23 using chrome_browser_net::NetworkPredictionOptions; 23 using chrome_browser_net::NetworkPredictionOptions;
24 using net::NetworkChangeNotifier; 24 using net::NetworkChangeNotifier;
25 25
26 namespace { 26 namespace {
27 27
28 class MockNetworkChangeNotifierWIFI : public NetworkChangeNotifier { 28 class MockNetworkChangeNotifierWIFI : public NetworkChangeNotifier {
(...skipping 15 matching lines...) Expand all
44 namespace predictors { 44 namespace predictors {
45 45
46 class ResourcePrefetchCommonTest : public testing::Test { 46 class ResourcePrefetchCommonTest : public testing::Test {
47 public: 47 public:
48 ResourcePrefetchCommonTest(); 48 ResourcePrefetchCommonTest();
49 49
50 void SetPreference(NetworkPredictionOptions value) { 50 void SetPreference(NetworkPredictionOptions value) {
51 profile_->GetPrefs()->SetInteger(prefs::kNetworkPredictionOptions, value); 51 profile_->GetPrefs()->SetInteger(prefs::kNetworkPredictionOptions, value);
52 } 52 }
53 53
54 void SetCommandLineValue(const std::string& value) {
55 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
56 switches::kSpeculativeResourcePrefetching, value);
57 }
58
59 void TestIsPrefetchEnabledForOrigin( 54 void TestIsPrefetchEnabledForOrigin(
60 const ResourcePrefetchPredictorConfig& config, 55 const ResourcePrefetchPredictorConfig& config,
61 PrefetchOrigin origin) { 56 PrefetchOrigin origin) {
62 EXPECT_TRUE(config.IsLearningEnabled()); 57 EXPECT_TRUE(config.IsLearningEnabled());
63 EXPECT_TRUE(config.IsPrefetchingEnabledForOrigin(profile_.get(), origin)); 58 EXPECT_TRUE(config.IsPrefetchingEnabledForOrigin(profile_.get(), origin));
64 } 59 }
65 60
66 void TestIsPrefetchLearning(const ResourcePrefetchPredictorConfig& config) { 61 void TestIsPrefetchLearning(const ResourcePrefetchPredictorConfig& config) {
67 EXPECT_TRUE(config.IsLearningEnabled()); 62 EXPECT_TRUE(config.IsLearningEnabled());
68 EXPECT_FALSE(config.IsPrefetchingEnabledForOrigin( 63 EXPECT_FALSE(config.IsPrefetchingEnabledForOrigin(
(...skipping 27 matching lines...) Expand all
96 91
97 EXPECT_FALSE(config.IsLearningEnabled()); 92 EXPECT_FALSE(config.IsLearningEnabled());
98 EXPECT_FALSE(config.IsPrefetchingEnabledForOrigin(profile_.get(), 93 EXPECT_FALSE(config.IsPrefetchingEnabledForOrigin(profile_.get(),
99 PrefetchOrigin::EXTERNAL)); 94 PrefetchOrigin::EXTERNAL));
100 EXPECT_FALSE(config.IsLearningEnabled()); 95 EXPECT_FALSE(config.IsLearningEnabled());
101 96
102 TestIsDefaultExtraConfig(config); 97 TestIsDefaultExtraConfig(config);
103 } 98 }
104 99
105 TEST_F(ResourcePrefetchCommonTest, EnableLearning) { 100 TEST_F(ResourcePrefetchCommonTest, EnableLearning) {
106 SetCommandLineValue(switches::kSpeculativeResourcePrefetchingLearning); 101 variations::testing::VariationParamsManager params_manager(
102 "dummy-trial", {{kModeParamName, kLearningMode}},
103 {kSpeculativeResourcePrefetchingFeatureName});
104
107 ResourcePrefetchPredictorConfig config; 105 ResourcePrefetchPredictorConfig config;
108 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); 106 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
109 TestIsPrefetchLearning(config); 107 TestIsPrefetchLearning(config);
110 TestIsDefaultExtraConfig(config); 108 TestIsDefaultExtraConfig(config);
111 } 109 }
112 110
113 TEST_F(ResourcePrefetchCommonTest, EnablePrefetch) { 111 TEST_F(ResourcePrefetchCommonTest, EnablePrefetch) {
114 SetCommandLineValue(switches::kSpeculativeResourcePrefetchingEnabled); 112 variations::testing::VariationParamsManager params_manager(
113 "dummy-trial", {{kModeParamName, kPrefetchingMode}},
114 {kSpeculativeResourcePrefetchingFeatureName});
115
115 ResourcePrefetchPredictorConfig config; 116 ResourcePrefetchPredictorConfig config;
116 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); 117 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
117 TestIsPrefetchEnabledForOrigin(config, PrefetchOrigin::EXTERNAL); 118 TestIsPrefetchEnabledForOrigin(config, PrefetchOrigin::EXTERNAL);
118 TestIsPrefetchEnabledForOrigin(config, PrefetchOrigin::NAVIGATION); 119 TestIsPrefetchEnabledForOrigin(config, PrefetchOrigin::NAVIGATION);
119 TestIsDefaultExtraConfig(config); 120 TestIsDefaultExtraConfig(config);
120 } 121 }
121 122
122 TEST_F(ResourcePrefetchCommonTest, EnablePrefetchExternalOnly) { 123 TEST_F(ResourcePrefetchCommonTest, EnablePrefetchExternalOnly) {
123 SetCommandLineValue(switches::kSpeculativeResourcePrefetchingEnabledExternal); 124 variations::testing::VariationParamsManager params_manager(
125 "dummy-trial", {{kModeParamName, kExternalPrefetchingMode}},
126 {kSpeculativeResourcePrefetchingFeatureName});
127
124 ResourcePrefetchPredictorConfig config; 128 ResourcePrefetchPredictorConfig config;
125 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); 129 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
126 TestIsPrefetchEnabledForOrigin(config, PrefetchOrigin::EXTERNAL); 130 TestIsPrefetchEnabledForOrigin(config, PrefetchOrigin::EXTERNAL);
127 EXPECT_FALSE(config.IsPrefetchingEnabledForOrigin( 131 EXPECT_FALSE(config.IsPrefetchingEnabledForOrigin(
128 profile_.get(), PrefetchOrigin::NAVIGATION)); 132 profile_.get(), PrefetchOrigin::NAVIGATION));
129 TestIsDefaultExtraConfig(config); 133 TestIsDefaultExtraConfig(config);
130 } 134 }
131 135
droger 2017/01/25 17:27:11 Do we need a test with the feature disabled? I th
Benoit L 2017/01/26 09:58:15 There is TEST_F(ResourcePrefetchCommonTest, IsDi
132 // Verifies whether prefetching is disabled according to the network type. But 136 // Verifies whether prefetching is disabled according to the network type. But
133 // learning should not be disabled by network. 137 // learning should not be disabled by network.
134 TEST_F(ResourcePrefetchCommonTest, RespectsNetworkSettings) { 138 TEST_F(ResourcePrefetchCommonTest, RespectsNetworkSettings) {
135 SetCommandLineValue(switches::kSpeculativeResourcePrefetchingEnabled); 139 variations::testing::VariationParamsManager params_manager(
140 "dummy-trial", {{kModeParamName, kPrefetchingMode}},
141 {kSpeculativeResourcePrefetchingFeatureName});
142
136 ResourcePrefetchPredictorConfig config; 143 ResourcePrefetchPredictorConfig config;
137 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config)); 144 EXPECT_TRUE(IsSpeculativeResourcePrefetchingEnabled(profile_.get(), &config));
138 TestIsPrefetchEnabledForOrigin(config, PrefetchOrigin::EXTERNAL); 145 TestIsPrefetchEnabledForOrigin(config, PrefetchOrigin::EXTERNAL);
139 TestIsDefaultExtraConfig(config); 146 TestIsDefaultExtraConfig(config);
140 147
141 // Set preference to WIFI_ONLY: prefetch when not on cellular. 148 // Set preference to WIFI_ONLY: prefetch when not on cellular.
142 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_WIFI_ONLY); 149 SetPreference(NetworkPredictionOptions::NETWORK_PREDICTION_WIFI_ONLY);
143 { 150 {
144 std::unique_ptr<NetworkChangeNotifier> mock( 151 std::unique_ptr<NetworkChangeNotifier> mock(
145 new MockNetworkChangeNotifierWIFI); 152 new MockNetworkChangeNotifierWIFI);
(...skipping 13 matching lines...) Expand all
159 TestIsPrefetchLearning(config); 166 TestIsPrefetchLearning(config);
160 } 167 }
161 { 168 {
162 std::unique_ptr<NetworkChangeNotifier> mock( 169 std::unique_ptr<NetworkChangeNotifier> mock(
163 new MockNetworkChangeNotifier4G); 170 new MockNetworkChangeNotifier4G);
164 TestIsPrefetchLearning(config); 171 TestIsPrefetchLearning(config);
165 } 172 }
166 } 173 }
167 174
168 } // namespace predictors 175 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698