OLD | NEW |
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 "chrome/browser/net/pref_proxy_config_service.h" | 5 #include "chrome/browser/net/pref_proxy_config_tracker.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "chrome/browser/net/chrome_url_request_context.h" | 10 #include "chrome/browser/net/chrome_url_request_context.h" |
11 #include "chrome/browser/prefs/pref_service_mock_builder.h" | 11 #include "chrome/browser/prefs/pref_service_mock_builder.h" |
12 #include "chrome/browser/prefs/proxy_config_dictionary.h" | 12 #include "chrome/browser/prefs/proxy_config_dictionary.h" |
13 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
14 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
15 #include "chrome/test/base/testing_pref_service.h" | 15 #include "chrome/test/base/testing_pref_service.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 | 63 |
64 // A mock observer for capturing callbacks. | 64 // A mock observer for capturing callbacks. |
65 class MockObserver : public net::ProxyConfigService::Observer { | 65 class MockObserver : public net::ProxyConfigService::Observer { |
66 public: | 66 public: |
67 MOCK_METHOD2(OnProxyConfigChanged, | 67 MOCK_METHOD2(OnProxyConfigChanged, |
68 void(const net::ProxyConfig&, | 68 void(const net::ProxyConfig&, |
69 net::ProxyConfigService::ConfigAvailability)); | 69 net::ProxyConfigService::ConfigAvailability)); |
70 }; | 70 }; |
71 | 71 |
72 template<typename TESTBASE> | 72 template<typename TESTBASE> |
73 class PrefProxyConfigServiceTestBase : public TESTBASE { | 73 class PrefProxyConfigTrackerTestBase : public TESTBASE { |
74 protected: | 74 protected: |
75 PrefProxyConfigServiceTestBase() | 75 PrefProxyConfigTrackerTestBase() |
76 : ui_thread_(BrowserThread::UI, &loop_), | 76 : ui_thread_(BrowserThread::UI, &loop_), |
77 io_thread_(BrowserThread::IO, &loop_) {} | 77 io_thread_(BrowserThread::IO, &loop_) {} |
78 | 78 |
79 virtual void Init(PrefService* pref_service) { | 79 virtual void Init(PrefService* pref_service) { |
80 ASSERT_TRUE(pref_service); | 80 ASSERT_TRUE(pref_service); |
81 PrefProxyConfigService::RegisterPrefs(pref_service); | 81 PrefProxyConfigTracker::RegisterPrefs(pref_service); |
82 fixed_config_.set_pac_url(GURL(kFixedPacUrl)); | 82 fixed_config_.set_pac_url(GURL(kFixedPacUrl)); |
83 delegate_service_ = | 83 delegate_service_ = |
84 new TestProxyConfigService(fixed_config_, | 84 new TestProxyConfigService(fixed_config_, |
85 net::ProxyConfigService::CONFIG_VALID); | 85 net::ProxyConfigService::CONFIG_VALID); |
86 proxy_config_tracker_ = new PrefProxyConfigTracker(pref_service); | |
87 proxy_config_service_.reset( | 86 proxy_config_service_.reset( |
88 new PrefProxyConfigService(proxy_config_tracker_.get(), | 87 new ChromeProxyConfigService(delegate_service_)); |
89 delegate_service_)); | 88 proxy_config_tracker_.reset(new PrefProxyConfigTracker(pref_service)); |
| 89 proxy_config_tracker_->SetChromeProxyConfigService( |
| 90 proxy_config_service_.get()); |
| 91 // SetChromeProxyConfigService triggers update of initial prefs proxy |
| 92 // config by tracker to chrome proxy config service, so flush all pending |
| 93 // tasks so that tests start fresh. |
| 94 loop_.RunAllPending(); |
90 } | 95 } |
91 | 96 |
92 virtual void TearDown() { | 97 virtual void TearDown() { |
93 proxy_config_tracker_->DetachFromPrefService(); | 98 proxy_config_tracker_->DetachFromPrefService(); |
94 loop_.RunAllPending(); | 99 loop_.RunAllPending(); |
| 100 proxy_config_tracker_.reset(); |
95 proxy_config_service_.reset(); | 101 proxy_config_service_.reset(); |
96 } | 102 } |
97 | 103 |
98 MessageLoop loop_; | 104 MessageLoop loop_; |
99 TestProxyConfigService* delegate_service_; // weak | 105 TestProxyConfigService* delegate_service_; // weak |
100 scoped_ptr<PrefProxyConfigService> proxy_config_service_; | 106 scoped_ptr<ChromeProxyConfigService> proxy_config_service_; |
101 net::ProxyConfig fixed_config_; | 107 net::ProxyConfig fixed_config_; |
102 | 108 |
103 private: | 109 private: |
104 scoped_refptr<PrefProxyConfigTracker> proxy_config_tracker_; | 110 scoped_ptr<PrefProxyConfigTracker> proxy_config_tracker_; |
105 BrowserThread ui_thread_; | 111 BrowserThread ui_thread_; |
106 BrowserThread io_thread_; | 112 BrowserThread io_thread_; |
107 }; | 113 }; |
108 | 114 |
109 class PrefProxyConfigServiceTest | 115 class PrefProxyConfigTrackerTest |
110 : public PrefProxyConfigServiceTestBase<testing::Test> { | 116 : public PrefProxyConfigTrackerTestBase<testing::Test> { |
111 protected: | 117 protected: |
112 virtual void SetUp() { | 118 virtual void SetUp() { |
113 pref_service_.reset(new TestingPrefService()); | 119 pref_service_.reset(new TestingPrefService()); |
114 Init(pref_service_.get()); | 120 Init(pref_service_.get()); |
115 } | 121 } |
116 | 122 |
117 scoped_ptr<TestingPrefService> pref_service_; | 123 scoped_ptr<TestingPrefService> pref_service_; |
118 }; | 124 }; |
119 | 125 |
120 TEST_F(PrefProxyConfigServiceTest, BaseConfiguration) { | 126 TEST_F(PrefProxyConfigTrackerTest, BaseConfiguration) { |
121 net::ProxyConfig actual_config; | 127 net::ProxyConfig actual_config; |
122 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, | 128 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, |
123 proxy_config_service_->GetLatestProxyConfig(&actual_config)); | 129 proxy_config_service_->GetLatestProxyConfig(&actual_config)); |
124 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url()); | 130 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url()); |
125 } | 131 } |
126 | 132 |
127 TEST_F(PrefProxyConfigServiceTest, DynamicPrefOverrides) { | 133 TEST_F(PrefProxyConfigTrackerTest, DynamicPrefOverrides) { |
128 pref_service_->SetManagedPref( | 134 pref_service_->SetManagedPref( |
129 prefs::kProxy, | 135 prefs::kProxy, |
130 ProxyConfigDictionary::CreateFixedServers("http://example.com:3128", "")); | 136 ProxyConfigDictionary::CreateFixedServers("http://example.com:3128", "")); |
131 loop_.RunAllPending(); | 137 loop_.RunAllPending(); |
132 | 138 |
133 net::ProxyConfig actual_config; | 139 net::ProxyConfig actual_config; |
134 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, | 140 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, |
135 proxy_config_service_->GetLatestProxyConfig(&actual_config)); | 141 proxy_config_service_->GetLatestProxyConfig(&actual_config)); |
136 EXPECT_FALSE(actual_config.auto_detect()); | 142 EXPECT_FALSE(actual_config.auto_detect()); |
137 EXPECT_EQ(net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY, | 143 EXPECT_EQ(net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY, |
(...skipping 11 matching lines...) Expand all Loading... |
149 EXPECT_TRUE(actual_config.auto_detect()); | 155 EXPECT_TRUE(actual_config.auto_detect()); |
150 } | 156 } |
151 | 157 |
152 // Compares proxy configurations, but allows different identifiers. | 158 // Compares proxy configurations, but allows different identifiers. |
153 MATCHER_P(ProxyConfigMatches, config, "") { | 159 MATCHER_P(ProxyConfigMatches, config, "") { |
154 net::ProxyConfig reference(config); | 160 net::ProxyConfig reference(config); |
155 reference.set_id(arg.id()); | 161 reference.set_id(arg.id()); |
156 return reference.Equals(arg); | 162 return reference.Equals(arg); |
157 } | 163 } |
158 | 164 |
159 TEST_F(PrefProxyConfigServiceTest, Observers) { | 165 TEST_F(PrefProxyConfigTrackerTest, Observers) { |
160 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID = | 166 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID = |
161 net::ProxyConfigService::CONFIG_VALID; | 167 net::ProxyConfigService::CONFIG_VALID; |
162 MockObserver observer; | 168 MockObserver observer; |
163 proxy_config_service_->AddObserver(&observer); | 169 proxy_config_service_->AddObserver(&observer); |
164 | 170 |
165 // Firing the observers in the delegate should trigger a notification. | 171 // Firing the observers in the delegate should trigger a notification. |
166 net::ProxyConfig config2; | 172 net::ProxyConfig config2; |
167 config2.set_auto_detect(true); | 173 config2.set_auto_detect(true); |
168 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config2), | 174 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config2), |
169 CONFIG_VALID)).Times(1); | 175 CONFIG_VALID)).Times(1); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 config4.proxy_rules().ParseFromString("socks:config4"); | 210 config4.proxy_rules().ParseFromString("socks:config4"); |
205 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config4), | 211 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config4), |
206 CONFIG_VALID)).Times(1); | 212 CONFIG_VALID)).Times(1); |
207 delegate_service_->SetProxyConfig(config4, CONFIG_VALID); | 213 delegate_service_->SetProxyConfig(config4, CONFIG_VALID); |
208 loop_.RunAllPending(); | 214 loop_.RunAllPending(); |
209 Mock::VerifyAndClearExpectations(&observer); | 215 Mock::VerifyAndClearExpectations(&observer); |
210 | 216 |
211 proxy_config_service_->RemoveObserver(&observer); | 217 proxy_config_service_->RemoveObserver(&observer); |
212 } | 218 } |
213 | 219 |
214 TEST_F(PrefProxyConfigServiceTest, Fallback) { | 220 TEST_F(PrefProxyConfigTrackerTest, Fallback) { |
215 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID = | 221 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID = |
216 net::ProxyConfigService::CONFIG_VALID; | 222 net::ProxyConfigService::CONFIG_VALID; |
217 MockObserver observer; | 223 MockObserver observer; |
218 net::ProxyConfig actual_config; | 224 net::ProxyConfig actual_config; |
219 delegate_service_->SetProxyConfig(net::ProxyConfig::CreateDirect(), | 225 delegate_service_->SetProxyConfig(net::ProxyConfig::CreateDirect(), |
220 net::ProxyConfigService::CONFIG_UNSET); | 226 net::ProxyConfigService::CONFIG_UNSET); |
221 proxy_config_service_->AddObserver(&observer); | 227 proxy_config_service_->AddObserver(&observer); |
222 | 228 |
223 // Prepare test data. | 229 // Prepare test data. |
224 net::ProxyConfig recommended_config = net::ProxyConfig::CreateAutoDetect(); | 230 net::ProxyConfig recommended_config = net::ProxyConfig::CreateAutoDetect(); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 pref_service_->RemoveManagedPref(prefs::kProxy); | 264 pref_service_->RemoveManagedPref(prefs::kProxy); |
259 loop_.RunAllPending(); | 265 loop_.RunAllPending(); |
260 Mock::VerifyAndClearExpectations(&observer); | 266 Mock::VerifyAndClearExpectations(&observer); |
261 EXPECT_EQ(CONFIG_VALID, | 267 EXPECT_EQ(CONFIG_VALID, |
262 proxy_config_service_->GetLatestProxyConfig(&actual_config)); | 268 proxy_config_service_->GetLatestProxyConfig(&actual_config)); |
263 EXPECT_TRUE(actual_config.Equals(recommended_config)); | 269 EXPECT_TRUE(actual_config.Equals(recommended_config)); |
264 | 270 |
265 proxy_config_service_->RemoveObserver(&observer); | 271 proxy_config_service_->RemoveObserver(&observer); |
266 } | 272 } |
267 | 273 |
268 TEST_F(PrefProxyConfigServiceTest, ExplicitSystemSettings) { | 274 TEST_F(PrefProxyConfigTrackerTest, ExplicitSystemSettings) { |
269 pref_service_->SetRecommendedPref( | 275 pref_service_->SetRecommendedPref( |
270 prefs::kProxy, | 276 prefs::kProxy, |
271 ProxyConfigDictionary::CreateAutoDetect()); | 277 ProxyConfigDictionary::CreateAutoDetect()); |
272 pref_service_->SetUserPref( | 278 pref_service_->SetUserPref( |
273 prefs::kProxy, | 279 prefs::kProxy, |
274 ProxyConfigDictionary::CreateSystem()); | 280 ProxyConfigDictionary::CreateSystem()); |
275 loop_.RunAllPending(); | 281 loop_.RunAllPending(); |
276 | 282 |
277 // Test if we actually use the system setting, which is |kFixedPacUrl|. | 283 // Test if we actually use the system setting, which is |kFixedPacUrl|. |
278 net::ProxyConfig actual_config; | 284 net::ProxyConfig actual_config; |
(...skipping 29 matching lines...) Expand all Loading... |
308 bool is_null; | 314 bool is_null; |
309 bool auto_detect; | 315 bool auto_detect; |
310 GURL pac_url; | 316 GURL pac_url; |
311 net::ProxyRulesExpectation proxy_rules; | 317 net::ProxyRulesExpectation proxy_rules; |
312 }; | 318 }; |
313 | 319 |
314 void PrintTo(const CommandLineTestParams& params, std::ostream* os) { | 320 void PrintTo(const CommandLineTestParams& params, std::ostream* os) { |
315 *os << params.description; | 321 *os << params.description; |
316 } | 322 } |
317 | 323 |
318 class PrefProxyConfigServiceCommandLineTest | 324 class PrefProxyConfigTrackerCommandLineTest |
319 : public PrefProxyConfigServiceTestBase< | 325 : public PrefProxyConfigTrackerTestBase< |
320 testing::TestWithParam<CommandLineTestParams> > { | 326 testing::TestWithParam<CommandLineTestParams> > { |
321 protected: | 327 protected: |
322 PrefProxyConfigServiceCommandLineTest() | 328 PrefProxyConfigTrackerCommandLineTest() |
323 : command_line_(CommandLine::NO_PROGRAM) {} | 329 : command_line_(CommandLine::NO_PROGRAM) {} |
324 | 330 |
325 virtual void SetUp() { | 331 virtual void SetUp() { |
326 for (size_t i = 0; i < arraysize(GetParam().switches); i++) { | 332 for (size_t i = 0; i < arraysize(GetParam().switches); i++) { |
327 const char* name = GetParam().switches[i].name; | 333 const char* name = GetParam().switches[i].name; |
328 const char* value = GetParam().switches[i].value; | 334 const char* value = GetParam().switches[i].value; |
329 if (name && value) | 335 if (name && value) |
330 command_line_.AppendSwitchASCII(name, value); | 336 command_line_.AppendSwitchASCII(name, value); |
331 else if (name) | 337 else if (name) |
332 command_line_.AppendSwitch(name); | 338 command_line_.AppendSwitch(name); |
333 } | 339 } |
334 pref_service_.reset( | 340 pref_service_.reset( |
335 PrefServiceMockBuilder().WithCommandLine(&command_line_).Create()); | 341 PrefServiceMockBuilder().WithCommandLine(&command_line_).Create()); |
336 Init(pref_service_.get()); | 342 Init(pref_service_.get()); |
337 } | 343 } |
338 | 344 |
339 private: | 345 private: |
340 CommandLine command_line_; | 346 CommandLine command_line_; |
341 scoped_ptr<PrefService> pref_service_; | 347 scoped_ptr<PrefService> pref_service_; |
342 }; | 348 }; |
343 | 349 |
344 TEST_P(PrefProxyConfigServiceCommandLineTest, CommandLine) { | 350 TEST_P(PrefProxyConfigTrackerCommandLineTest, CommandLine) { |
345 net::ProxyConfig config; | 351 net::ProxyConfig config; |
346 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, | 352 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, |
347 proxy_config_service_->GetLatestProxyConfig(&config)); | 353 proxy_config_service_->GetLatestProxyConfig(&config)); |
348 | 354 |
349 if (GetParam().is_null) { | 355 if (GetParam().is_null) { |
350 EXPECT_EQ(GURL(kFixedPacUrl), config.pac_url()); | 356 EXPECT_EQ(GURL(kFixedPacUrl), config.pac_url()); |
351 } else { | 357 } else { |
352 EXPECT_NE(GURL(kFixedPacUrl), config.pac_url()); | 358 EXPECT_NE(GURL(kFixedPacUrl), config.pac_url()); |
353 EXPECT_EQ(GetParam().auto_detect, config.auto_detect()); | 359 EXPECT_EQ(GetParam().auto_detect, config.auto_detect()); |
354 EXPECT_EQ(GetParam().pac_url, config.pac_url()); | 360 EXPECT_EQ(GetParam().pac_url, config.pac_url()); |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 }, | 466 }, |
461 // Expected result | 467 // Expected result |
462 false, // is_null | 468 false, // is_null |
463 true, // auto_detect | 469 true, // auto_detect |
464 GURL(), // pac_url | 470 GURL(), // pac_url |
465 net::ProxyRulesExpectation::Empty(), | 471 net::ProxyRulesExpectation::Empty(), |
466 }, | 472 }, |
467 }; | 473 }; |
468 | 474 |
469 INSTANTIATE_TEST_CASE_P( | 475 INSTANTIATE_TEST_CASE_P( |
470 PrefProxyConfigServiceCommandLineTestInstance, | 476 PrefProxyConfigTrackerCommandLineTestInstance, |
471 PrefProxyConfigServiceCommandLineTest, | 477 PrefProxyConfigTrackerCommandLineTest, |
472 testing::ValuesIn(kCommandLineTestParams)); | 478 testing::ValuesIn(kCommandLineTestParams)); |
473 | 479 |
474 } // namespace | 480 } // namespace |
OLD | NEW |