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

Side by Side Diff: components/proxy_config/pref_proxy_config_tracker_impl_unittest.cc

Issue 2860033003: Don't delay creation of system URLRequestContext until first use (Closed)
Patch Set: Remove comment Created 3 years, 7 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
« no previous file with comments | « components/proxy_config/pref_proxy_config_tracker_impl.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "components/proxy_config/pref_proxy_config_tracker_impl.h" 5 #include "components/proxy_config/pref_proxy_config_tracker_impl.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 9
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 // A mock observer for capturing callbacks. 67 // A mock observer for capturing callbacks.
68 class MockObserver : public net::ProxyConfigService::Observer { 68 class MockObserver : public net::ProxyConfigService::Observer {
69 public: 69 public:
70 MOCK_METHOD2(OnProxyConfigChanged, 70 MOCK_METHOD2(OnProxyConfigChanged,
71 void(const net::ProxyConfig&, 71 void(const net::ProxyConfig&,
72 net::ProxyConfigService::ConfigAvailability)); 72 net::ProxyConfigService::ConfigAvailability));
73 }; 73 };
74 74
75 class PrefProxyConfigTrackerImplTest : public testing::Test { 75 class PrefProxyConfigTrackerImplTest : public testing::Test {
76 protected: 76 protected:
77 PrefProxyConfigTrackerImplTest() { 77 PrefProxyConfigTrackerImplTest() {}
78
79 // Initializes the proxy config service. The delegate config service has the
80 // specified initial config availability.
81 void InitConfigService(net::ProxyConfigService::ConfigAvailability
82 delegate_config_availability) {
78 pref_service_.reset(new TestingPrefServiceSimple()); 83 pref_service_.reset(new TestingPrefServiceSimple());
79 PrefProxyConfigTrackerImpl::RegisterPrefs(pref_service_->registry()); 84 PrefProxyConfigTrackerImpl::RegisterPrefs(pref_service_->registry());
80 fixed_config_.set_pac_url(GURL(kFixedPacUrl)); 85 fixed_config_.set_pac_url(GURL(kFixedPacUrl));
81 delegate_service_ = 86 delegate_service_ =
82 new TestProxyConfigService(fixed_config_, 87 new TestProxyConfigService(fixed_config_, delegate_config_availability);
83 net::ProxyConfigService::CONFIG_VALID);
84 proxy_config_tracker_.reset(new PrefProxyConfigTrackerImpl( 88 proxy_config_tracker_.reset(new PrefProxyConfigTrackerImpl(
85 pref_service_.get(), base::ThreadTaskRunnerHandle::Get())); 89 pref_service_.get(), base::ThreadTaskRunnerHandle::Get()));
86 proxy_config_service_ = 90 proxy_config_service_ =
87 proxy_config_tracker_->CreateTrackingProxyConfigService( 91 proxy_config_tracker_->CreateTrackingProxyConfigService(
88 std::unique_ptr<net::ProxyConfigService>(delegate_service_)); 92 std::unique_ptr<net::ProxyConfigService>(delegate_service_));
89 // SetProxyConfigServiceImpl triggers update of initial prefs proxy
90 // config by tracker to chrome proxy config service, so flush all pending
91 // tasks so that tests start fresh.
92 base::RunLoop().RunUntilIdle();
93 } 93 }
94 94
95 ~PrefProxyConfigTrackerImplTest() override { 95 ~PrefProxyConfigTrackerImplTest() override {
96 proxy_config_tracker_->DetachFromPrefService(); 96 proxy_config_tracker_->DetachFromPrefService();
97 base::RunLoop().RunUntilIdle(); 97 base::RunLoop().RunUntilIdle();
98 proxy_config_tracker_.reset(); 98 proxy_config_tracker_.reset();
99 proxy_config_service_.reset(); 99 proxy_config_service_.reset();
100 } 100 }
101 101
102 base::MessageLoop loop_; 102 base::MessageLoop loop_;
103 std::unique_ptr<TestingPrefServiceSimple> pref_service_; 103 std::unique_ptr<TestingPrefServiceSimple> pref_service_;
104 TestProxyConfigService* delegate_service_; // weak 104 TestProxyConfigService* delegate_service_; // weak
105 std::unique_ptr<net::ProxyConfigService> proxy_config_service_; 105 std::unique_ptr<net::ProxyConfigService> proxy_config_service_;
106 net::ProxyConfig fixed_config_; 106 net::ProxyConfig fixed_config_;
107
108 private:
109 std::unique_ptr<PrefProxyConfigTrackerImpl> proxy_config_tracker_; 107 std::unique_ptr<PrefProxyConfigTrackerImpl> proxy_config_tracker_;
110 }; 108 };
111 109
112 TEST_F(PrefProxyConfigTrackerImplTest, BaseConfiguration) { 110 TEST_F(PrefProxyConfigTrackerImplTest, BaseConfiguration) {
111 InitConfigService(net::ProxyConfigService::CONFIG_VALID);
113 net::ProxyConfig actual_config; 112 net::ProxyConfig actual_config;
114 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, 113 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID,
115 proxy_config_service_->GetLatestProxyConfig(&actual_config)); 114 proxy_config_service_->GetLatestProxyConfig(&actual_config));
116 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url()); 115 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url());
117 } 116 }
118 117
119 TEST_F(PrefProxyConfigTrackerImplTest, DynamicPrefOverrides) { 118 TEST_F(PrefProxyConfigTrackerImplTest, DynamicPrefOverrides) {
119 InitConfigService(net::ProxyConfigService::CONFIG_VALID);
120 pref_service_->SetManagedPref(proxy_config::prefs::kProxy, 120 pref_service_->SetManagedPref(proxy_config::prefs::kProxy,
121 ProxyConfigDictionary::CreateFixedServers( 121 ProxyConfigDictionary::CreateFixedServers(
122 "http://example.com:3128", std::string())); 122 "http://example.com:3128", std::string()));
123 base::RunLoop().RunUntilIdle(); 123 base::RunLoop().RunUntilIdle();
124 124
125 net::ProxyConfig actual_config; 125 net::ProxyConfig actual_config;
126 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, 126 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID,
127 proxy_config_service_->GetLatestProxyConfig(&actual_config)); 127 proxy_config_service_->GetLatestProxyConfig(&actual_config));
128 EXPECT_FALSE(actual_config.auto_detect()); 128 EXPECT_FALSE(actual_config.auto_detect());
129 EXPECT_EQ(net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY, 129 EXPECT_EQ(net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY,
(...skipping 12 matching lines...) Expand all
142 } 142 }
143 143
144 // Compares proxy configurations, but allows different identifiers. 144 // Compares proxy configurations, but allows different identifiers.
145 MATCHER_P(ProxyConfigMatches, config, "") { 145 MATCHER_P(ProxyConfigMatches, config, "") {
146 net::ProxyConfig reference(config); 146 net::ProxyConfig reference(config);
147 reference.set_id(arg.id()); 147 reference.set_id(arg.id());
148 return reference.Equals(arg); 148 return reference.Equals(arg);
149 } 149 }
150 150
151 TEST_F(PrefProxyConfigTrackerImplTest, Observers) { 151 TEST_F(PrefProxyConfigTrackerImplTest, Observers) {
152 InitConfigService(net::ProxyConfigService::CONFIG_VALID);
152 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID = 153 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID =
153 net::ProxyConfigService::CONFIG_VALID; 154 net::ProxyConfigService::CONFIG_VALID;
154 MockObserver observer; 155 MockObserver observer;
155 proxy_config_service_->AddObserver(&observer); 156 proxy_config_service_->AddObserver(&observer);
156 157
157 // Firing the observers in the delegate should trigger a notification. 158 // Firing the observers in the delegate should trigger a notification.
158 net::ProxyConfig config2; 159 net::ProxyConfig config2;
159 config2.set_auto_detect(true); 160 config2.set_auto_detect(true);
160 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config2), 161 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config2),
161 CONFIG_VALID)).Times(1); 162 CONFIG_VALID)).Times(1);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config4), 198 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config4),
198 CONFIG_VALID)).Times(1); 199 CONFIG_VALID)).Times(1);
199 delegate_service_->SetProxyConfig(config4, CONFIG_VALID); 200 delegate_service_->SetProxyConfig(config4, CONFIG_VALID);
200 base::RunLoop().RunUntilIdle(); 201 base::RunLoop().RunUntilIdle();
201 Mock::VerifyAndClearExpectations(&observer); 202 Mock::VerifyAndClearExpectations(&observer);
202 203
203 proxy_config_service_->RemoveObserver(&observer); 204 proxy_config_service_->RemoveObserver(&observer);
204 } 205 }
205 206
206 TEST_F(PrefProxyConfigTrackerImplTest, Fallback) { 207 TEST_F(PrefProxyConfigTrackerImplTest, Fallback) {
208 InitConfigService(net::ProxyConfigService::CONFIG_VALID);
207 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID = 209 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID =
208 net::ProxyConfigService::CONFIG_VALID; 210 net::ProxyConfigService::CONFIG_VALID;
209 MockObserver observer; 211 MockObserver observer;
210 net::ProxyConfig actual_config; 212 net::ProxyConfig actual_config;
211 delegate_service_->SetProxyConfig(net::ProxyConfig::CreateDirect(), 213 delegate_service_->SetProxyConfig(net::ProxyConfig::CreateDirect(),
212 net::ProxyConfigService::CONFIG_UNSET); 214 net::ProxyConfigService::CONFIG_UNSET);
213 proxy_config_service_->AddObserver(&observer); 215 proxy_config_service_->AddObserver(&observer);
214 216
215 // Prepare test data. 217 // Prepare test data.
216 net::ProxyConfig recommended_config = net::ProxyConfig::CreateAutoDetect(); 218 net::ProxyConfig recommended_config = net::ProxyConfig::CreateAutoDetect();
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 base::RunLoop().RunUntilIdle(); 252 base::RunLoop().RunUntilIdle();
251 Mock::VerifyAndClearExpectations(&observer); 253 Mock::VerifyAndClearExpectations(&observer);
252 EXPECT_EQ(CONFIG_VALID, 254 EXPECT_EQ(CONFIG_VALID,
253 proxy_config_service_->GetLatestProxyConfig(&actual_config)); 255 proxy_config_service_->GetLatestProxyConfig(&actual_config));
254 EXPECT_TRUE(actual_config.Equals(recommended_config)); 256 EXPECT_TRUE(actual_config.Equals(recommended_config));
255 257
256 proxy_config_service_->RemoveObserver(&observer); 258 proxy_config_service_->RemoveObserver(&observer);
257 } 259 }
258 260
259 TEST_F(PrefProxyConfigTrackerImplTest, ExplicitSystemSettings) { 261 TEST_F(PrefProxyConfigTrackerImplTest, ExplicitSystemSettings) {
262 InitConfigService(net::ProxyConfigService::CONFIG_VALID);
260 pref_service_->SetRecommendedPref(proxy_config::prefs::kProxy, 263 pref_service_->SetRecommendedPref(proxy_config::prefs::kProxy,
261 ProxyConfigDictionary::CreateAutoDetect()); 264 ProxyConfigDictionary::CreateAutoDetect());
262 pref_service_->SetUserPref(proxy_config::prefs::kProxy, 265 pref_service_->SetUserPref(proxy_config::prefs::kProxy,
263 ProxyConfigDictionary::CreateSystem()); 266 ProxyConfigDictionary::CreateSystem());
264 base::RunLoop().RunUntilIdle(); 267 base::RunLoop().RunUntilIdle();
265 268
266 // Test if we actually use the system setting, which is |kFixedPacUrl|. 269 // Test if we actually use the system setting, which is |kFixedPacUrl|.
267 net::ProxyConfig actual_config; 270 net::ProxyConfig actual_config;
268 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, 271 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID,
269 proxy_config_service_->GetLatestProxyConfig(&actual_config)); 272 proxy_config_service_->GetLatestProxyConfig(&actual_config));
270 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url()); 273 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url());
271 } 274 }
272 275
276 // Test the case where the delegate service gets a config only after the service
277 // is created.
278 TEST_F(PrefProxyConfigTrackerImplTest, DelegateConfigServiceGetsConfigLate) {
279 InitConfigService(net::ProxyConfigService::CONFIG_PENDING);
280
281 testing::StrictMock<MockObserver> observer;
282 proxy_config_service_->AddObserver(&observer);
283
284 net::ProxyConfig actual_config;
285 EXPECT_EQ(net::ProxyConfigService::CONFIG_PENDING,
286 proxy_config_service_->GetLatestProxyConfig(&actual_config));
287
288 // When the delegate service gets the config, the other service should update
289 // its observers.
290 EXPECT_CALL(observer,
291 OnProxyConfigChanged(ProxyConfigMatches(fixed_config_),
292 net::ProxyConfigService::CONFIG_VALID))
293 .Times(1);
294 delegate_service_->SetProxyConfig(fixed_config_,
295 net::ProxyConfigService::CONFIG_VALID);
296
297 // Since no prefs were set, should just use the delegated config service's
298 // settings.
299 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID,
300 proxy_config_service_->GetLatestProxyConfig(&actual_config));
301 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url());
302
303 proxy_config_service_->RemoveObserver(&observer);
304 }
305
273 } // namespace 306 } // namespace
OLDNEW
« no previous file with comments | « components/proxy_config/pref_proxy_config_tracker_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698