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_service.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 "chrome/browser/net/chrome_url_request_context.h" | 9 #include "chrome/browser/net/chrome_url_request_context.h" |
10 #include "chrome/browser/prefs/pref_service_mock_builder.h" | 10 #include "chrome/browser/prefs/pref_service_mock_builder.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 // Testing proxy config service that allows us to fire notifications at will. | 26 // Testing proxy config service that allows us to fire notifications at will. |
27 class TestProxyConfigService : public net::ProxyConfigService { | 27 class TestProxyConfigService : public net::ProxyConfigService { |
28 public: | 28 public: |
29 explicit TestProxyConfigService(const net::ProxyConfig& config) | 29 explicit TestProxyConfigService(const net::ProxyConfig& config) |
30 : config_(config) { | 30 : config_(config) { |
31 } | 31 } |
32 | 32 |
33 void SetProxyConfig(const net::ProxyConfig config) { | 33 void SetProxyConfig(const net::ProxyConfig config) { |
34 config_ = config; | 34 config_ = config; |
35 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_, | 35 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_, |
36 OnProxyConfigChanged(config_)); | 36 OnProxyConfigChanged(config, CONFIG_VALID)); |
37 } | 37 } |
38 | 38 |
39 private: | 39 private: |
40 virtual void AddObserver(net::ProxyConfigService::Observer* observer) { | 40 virtual void AddObserver(net::ProxyConfigService::Observer* observer) { |
41 observers_.AddObserver(observer); | 41 observers_.AddObserver(observer); |
42 } | 42 } |
43 | 43 |
44 virtual void RemoveObserver(net::ProxyConfigService::Observer* observer) { | 44 virtual void RemoveObserver(net::ProxyConfigService::Observer* observer) { |
45 observers_.RemoveObserver(observer); | 45 observers_.RemoveObserver(observer); |
46 } | 46 } |
47 | 47 |
48 virtual bool GetLatestProxyConfig(net::ProxyConfig* config) { | 48 virtual net::ProxyConfigService::ConfigAvailability GetLatestProxyConfig( |
| 49 net::ProxyConfig* config) { |
49 *config = config_; | 50 *config = config_; |
50 return true; | 51 return CONFIG_VALID; |
51 } | 52 } |
52 | 53 |
53 net::ProxyConfig config_; | 54 net::ProxyConfig config_; |
54 ObserverList<net::ProxyConfigService::Observer, true> observers_; | 55 ObserverList<net::ProxyConfigService::Observer, true> observers_; |
55 }; | 56 }; |
56 | 57 |
57 // A mock observer for capturing callbacks. | 58 // A mock observer for capturing callbacks. |
58 class MockObserver : public net::ProxyConfigService::Observer { | 59 class MockObserver : public net::ProxyConfigService::Observer { |
59 public: | 60 public: |
60 MOCK_METHOD1(OnProxyConfigChanged, void(const net::ProxyConfig&)); | 61 MOCK_METHOD2(OnProxyConfigChanged, |
| 62 void(const net::ProxyConfig&, |
| 63 net::ProxyConfigService::ConfigAvailability)); |
61 }; | 64 }; |
62 | 65 |
63 template<typename TESTBASE> | 66 template<typename TESTBASE> |
64 class PrefProxyConfigServiceTestBase : public TESTBASE { | 67 class PrefProxyConfigServiceTestBase : public TESTBASE { |
65 protected: | 68 protected: |
66 PrefProxyConfigServiceTestBase() | 69 PrefProxyConfigServiceTestBase() |
67 : ui_thread_(BrowserThread::UI, &loop_), | 70 : ui_thread_(BrowserThread::UI, &loop_), |
68 io_thread_(BrowserThread::IO, &loop_) {} | 71 io_thread_(BrowserThread::IO, &loop_) {} |
69 | 72 |
70 virtual void Init(PrefService* pref_service) { | 73 virtual void Init(PrefService* pref_service) { |
(...skipping 30 matching lines...) Expand all Loading... |
101 virtual void SetUp() { | 104 virtual void SetUp() { |
102 pref_service_.reset(new TestingPrefService()); | 105 pref_service_.reset(new TestingPrefService()); |
103 Init(pref_service_.get()); | 106 Init(pref_service_.get()); |
104 } | 107 } |
105 | 108 |
106 scoped_ptr<TestingPrefService> pref_service_; | 109 scoped_ptr<TestingPrefService> pref_service_; |
107 }; | 110 }; |
108 | 111 |
109 TEST_F(PrefProxyConfigServiceTest, BaseConfiguration) { | 112 TEST_F(PrefProxyConfigServiceTest, BaseConfiguration) { |
110 net::ProxyConfig actual_config; | 113 net::ProxyConfig actual_config; |
111 proxy_config_service_->GetLatestProxyConfig(&actual_config); | 114 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, |
| 115 proxy_config_service_->GetLatestProxyConfig(&actual_config)); |
112 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url()); | 116 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url()); |
113 } | 117 } |
114 | 118 |
115 TEST_F(PrefProxyConfigServiceTest, DynamicPrefOverrides) { | 119 TEST_F(PrefProxyConfigServiceTest, DynamicPrefOverrides) { |
116 pref_service_->SetManagedPref( | 120 pref_service_->SetManagedPref( |
117 prefs::kProxy, | 121 prefs::kProxy, |
118 ProxyConfigDictionary::CreateFixedServers("http://example.com:3128", "")); | 122 ProxyConfigDictionary::CreateFixedServers("http://example.com:3128", "")); |
119 loop_.RunAllPending(); | 123 loop_.RunAllPending(); |
120 | 124 |
121 net::ProxyConfig actual_config; | 125 net::ProxyConfig actual_config; |
122 proxy_config_service_->GetLatestProxyConfig(&actual_config); | 126 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, |
| 127 proxy_config_service_->GetLatestProxyConfig(&actual_config)); |
123 EXPECT_FALSE(actual_config.auto_detect()); | 128 EXPECT_FALSE(actual_config.auto_detect()); |
124 EXPECT_EQ(net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY, | 129 EXPECT_EQ(net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY, |
125 actual_config.proxy_rules().type); | 130 actual_config.proxy_rules().type); |
126 EXPECT_EQ(actual_config.proxy_rules().single_proxy, | 131 EXPECT_EQ(actual_config.proxy_rules().single_proxy, |
127 net::ProxyServer::FromURI("http://example.com:3128", | 132 net::ProxyServer::FromURI("http://example.com:3128", |
128 net::ProxyServer::SCHEME_HTTP)); | 133 net::ProxyServer::SCHEME_HTTP)); |
129 | 134 |
130 pref_service_->SetManagedPref(prefs::kProxy, | 135 pref_service_->SetManagedPref(prefs::kProxy, |
131 ProxyConfigDictionary::CreateAutoDetect()); | 136 ProxyConfigDictionary::CreateAutoDetect()); |
132 loop_.RunAllPending(); | 137 loop_.RunAllPending(); |
133 | 138 |
134 proxy_config_service_->GetLatestProxyConfig(&actual_config); | 139 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, |
| 140 proxy_config_service_->GetLatestProxyConfig(&actual_config)); |
135 EXPECT_TRUE(actual_config.auto_detect()); | 141 EXPECT_TRUE(actual_config.auto_detect()); |
136 } | 142 } |
137 | 143 |
138 // Compares proxy configurations, but allows different identifiers. | 144 // Compares proxy configurations, but allows different identifiers. |
139 MATCHER_P(ProxyConfigMatches, config, "") { | 145 MATCHER_P(ProxyConfigMatches, config, "") { |
140 net::ProxyConfig reference(config); | 146 net::ProxyConfig reference(config); |
141 reference.set_id(arg.id()); | 147 reference.set_id(arg.id()); |
142 return reference.Equals(arg); | 148 return reference.Equals(arg); |
143 } | 149 } |
144 | 150 |
145 TEST_F(PrefProxyConfigServiceTest, Observers) { | 151 TEST_F(PrefProxyConfigServiceTest, Observers) { |
| 152 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID = |
| 153 net::ProxyConfigService::CONFIG_VALID; |
146 MockObserver observer; | 154 MockObserver observer; |
147 proxy_config_service_->AddObserver(&observer); | 155 proxy_config_service_->AddObserver(&observer); |
148 | 156 |
149 // Firing the observers in the delegate should trigger a notification. | 157 // Firing the observers in the delegate should trigger a notification. |
150 net::ProxyConfig config2; | 158 net::ProxyConfig config2; |
151 config2.set_auto_detect(true); | 159 config2.set_auto_detect(true); |
152 EXPECT_CALL(observer, | 160 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config2), |
153 OnProxyConfigChanged(ProxyConfigMatches(config2))).Times(1); | 161 CONFIG_VALID)).Times(1); |
154 delegate_service_->SetProxyConfig(config2); | 162 delegate_service_->SetProxyConfig(config2); |
155 loop_.RunAllPending(); | 163 loop_.RunAllPending(); |
156 Mock::VerifyAndClearExpectations(&observer); | 164 Mock::VerifyAndClearExpectations(&observer); |
157 | 165 |
158 // Override configuration, this should trigger a notification. | 166 // Override configuration, this should trigger a notification. |
159 net::ProxyConfig pref_config; | 167 net::ProxyConfig pref_config; |
160 pref_config.set_pac_url(GURL(kFixedPacUrl)); | 168 pref_config.set_pac_url(GURL(kFixedPacUrl)); |
161 | 169 |
162 EXPECT_CALL(observer, | 170 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(pref_config), |
163 OnProxyConfigChanged(ProxyConfigMatches(pref_config))).Times(1); | 171 CONFIG_VALID)).Times(1); |
164 pref_service_->SetManagedPref( | 172 pref_service_->SetManagedPref( |
165 prefs::kProxy, | 173 prefs::kProxy, |
166 ProxyConfigDictionary::CreatePacScript(kFixedPacUrl)); | 174 ProxyConfigDictionary::CreatePacScript(kFixedPacUrl)); |
167 loop_.RunAllPending(); | 175 loop_.RunAllPending(); |
168 Mock::VerifyAndClearExpectations(&observer); | 176 Mock::VerifyAndClearExpectations(&observer); |
169 | 177 |
170 // Since there are pref overrides, delegate changes should be ignored. | 178 // Since there are pref overrides, delegate changes should be ignored. |
171 net::ProxyConfig config3; | 179 net::ProxyConfig config3; |
172 config3.proxy_rules().ParseFromString("http=config3:80"); | 180 config3.proxy_rules().ParseFromString("http=config3:80"); |
173 EXPECT_CALL(observer, OnProxyConfigChanged(_)).Times(0); | 181 EXPECT_CALL(observer, OnProxyConfigChanged(_, _)).Times(0); |
174 fixed_config_.set_auto_detect(true); | 182 fixed_config_.set_auto_detect(true); |
175 delegate_service_->SetProxyConfig(config3); | 183 delegate_service_->SetProxyConfig(config3); |
176 loop_.RunAllPending(); | 184 loop_.RunAllPending(); |
177 Mock::VerifyAndClearExpectations(&observer); | 185 Mock::VerifyAndClearExpectations(&observer); |
178 | 186 |
179 // Clear the override should switch back to the fixed configuration. | 187 // Clear the override should switch back to the fixed configuration. |
180 EXPECT_CALL(observer, | 188 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config3), |
181 OnProxyConfigChanged(ProxyConfigMatches(config3))).Times(1); | 189 CONFIG_VALID)).Times(1); |
182 pref_service_->RemoveManagedPref(prefs::kProxy); | 190 pref_service_->RemoveManagedPref(prefs::kProxy); |
183 loop_.RunAllPending(); | 191 loop_.RunAllPending(); |
184 Mock::VerifyAndClearExpectations(&observer); | 192 Mock::VerifyAndClearExpectations(&observer); |
185 | 193 |
186 // Delegate service notifications should show up again. | 194 // Delegate service notifications should show up again. |
187 net::ProxyConfig config4; | 195 net::ProxyConfig config4; |
188 config4.proxy_rules().ParseFromString("socks:config4"); | 196 config4.proxy_rules().ParseFromString("socks:config4"); |
189 EXPECT_CALL(observer, | 197 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config4), |
190 OnProxyConfigChanged(ProxyConfigMatches(config4))).Times(1); | 198 CONFIG_VALID)).Times(1); |
191 delegate_service_->SetProxyConfig(config4); | 199 delegate_service_->SetProxyConfig(config4); |
192 loop_.RunAllPending(); | 200 loop_.RunAllPending(); |
193 Mock::VerifyAndClearExpectations(&observer); | 201 Mock::VerifyAndClearExpectations(&observer); |
194 | 202 |
195 proxy_config_service_->RemoveObserver(&observer); | 203 proxy_config_service_->RemoveObserver(&observer); |
196 } | 204 } |
197 | 205 |
198 // Test parameter object for testing command line proxy configuration. | 206 // Test parameter object for testing command line proxy configuration. |
199 struct CommandLineTestParams { | 207 struct CommandLineTestParams { |
200 // Explicit assignment operator, so testing::TestWithParam works with MSVC. | 208 // Explicit assignment operator, so testing::TestWithParam works with MSVC. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 Init(pref_service_.get()); | 258 Init(pref_service_.get()); |
251 } | 259 } |
252 | 260 |
253 private: | 261 private: |
254 CommandLine command_line_; | 262 CommandLine command_line_; |
255 scoped_ptr<PrefService> pref_service_; | 263 scoped_ptr<PrefService> pref_service_; |
256 }; | 264 }; |
257 | 265 |
258 TEST_P(PrefProxyConfigServiceCommandLineTest, CommandLine) { | 266 TEST_P(PrefProxyConfigServiceCommandLineTest, CommandLine) { |
259 net::ProxyConfig config; | 267 net::ProxyConfig config; |
260 proxy_config_service_->GetLatestProxyConfig(&config); | 268 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, |
| 269 proxy_config_service_->GetLatestProxyConfig(&config)); |
261 | 270 |
262 if (GetParam().is_null) { | 271 if (GetParam().is_null) { |
263 EXPECT_EQ(GURL(kFixedPacUrl), config.pac_url()); | 272 EXPECT_EQ(GURL(kFixedPacUrl), config.pac_url()); |
264 } else { | 273 } else { |
265 EXPECT_NE(GURL(kFixedPacUrl), config.pac_url()); | 274 EXPECT_NE(GURL(kFixedPacUrl), config.pac_url()); |
266 EXPECT_EQ(GetParam().auto_detect, config.auto_detect()); | 275 EXPECT_EQ(GetParam().auto_detect, config.auto_detect()); |
267 EXPECT_EQ(GetParam().pac_url, config.pac_url()); | 276 EXPECT_EQ(GetParam().pac_url, config.pac_url()); |
268 EXPECT_TRUE(GetParam().proxy_rules.Matches(config.proxy_rules())); | 277 EXPECT_TRUE(GetParam().proxy_rules.Matches(config.proxy_rules())); |
269 } | 278 } |
270 } | 279 } |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 net::ProxyRulesExpectation::Empty(), | 387 net::ProxyRulesExpectation::Empty(), |
379 }, | 388 }, |
380 }; | 389 }; |
381 | 390 |
382 INSTANTIATE_TEST_CASE_P( | 391 INSTANTIATE_TEST_CASE_P( |
383 PrefProxyConfigServiceCommandLineTestInstance, | 392 PrefProxyConfigServiceCommandLineTestInstance, |
384 PrefProxyConfigServiceCommandLineTest, | 393 PrefProxyConfigServiceCommandLineTest, |
385 testing::ValuesIn(kCommandLineTestParams)); | 394 testing::ValuesIn(kCommandLineTestParams)); |
386 | 395 |
387 } // namespace | 396 } // namespace |
OLD | NEW |