| 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 "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 Loading... |
| 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) { | |
| 83 pref_service_.reset(new TestingPrefServiceSimple()); | 78 pref_service_.reset(new TestingPrefServiceSimple()); |
| 84 PrefProxyConfigTrackerImpl::RegisterPrefs(pref_service_->registry()); | 79 PrefProxyConfigTrackerImpl::RegisterPrefs(pref_service_->registry()); |
| 85 fixed_config_.set_pac_url(GURL(kFixedPacUrl)); | 80 fixed_config_.set_pac_url(GURL(kFixedPacUrl)); |
| 86 delegate_service_ = | 81 delegate_service_ = |
| 87 new TestProxyConfigService(fixed_config_, delegate_config_availability); | 82 new TestProxyConfigService(fixed_config_, |
| 83 net::ProxyConfigService::CONFIG_VALID); |
| 88 proxy_config_tracker_.reset(new PrefProxyConfigTrackerImpl( | 84 proxy_config_tracker_.reset(new PrefProxyConfigTrackerImpl( |
| 89 pref_service_.get(), base::ThreadTaskRunnerHandle::Get())); | 85 pref_service_.get(), base::ThreadTaskRunnerHandle::Get())); |
| 90 proxy_config_service_ = | 86 proxy_config_service_ = |
| 91 proxy_config_tracker_->CreateTrackingProxyConfigService( | 87 proxy_config_tracker_->CreateTrackingProxyConfigService( |
| 92 std::unique_ptr<net::ProxyConfigService>(delegate_service_)); | 88 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: |
| 107 std::unique_ptr<PrefProxyConfigTrackerImpl> proxy_config_tracker_; | 109 std::unique_ptr<PrefProxyConfigTrackerImpl> proxy_config_tracker_; |
| 108 }; | 110 }; |
| 109 | 111 |
| 110 TEST_F(PrefProxyConfigTrackerImplTest, BaseConfiguration) { | 112 TEST_F(PrefProxyConfigTrackerImplTest, BaseConfiguration) { |
| 111 InitConfigService(net::ProxyConfigService::CONFIG_VALID); | |
| 112 net::ProxyConfig actual_config; | 113 net::ProxyConfig actual_config; |
| 113 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, | 114 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, |
| 114 proxy_config_service_->GetLatestProxyConfig(&actual_config)); | 115 proxy_config_service_->GetLatestProxyConfig(&actual_config)); |
| 115 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url()); | 116 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url()); |
| 116 } | 117 } |
| 117 | 118 |
| 118 TEST_F(PrefProxyConfigTrackerImplTest, DynamicPrefOverrides) { | 119 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 Loading... |
| 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); | |
| 153 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID = | 152 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID = |
| 154 net::ProxyConfigService::CONFIG_VALID; | 153 net::ProxyConfigService::CONFIG_VALID; |
| 155 MockObserver observer; | 154 MockObserver observer; |
| 156 proxy_config_service_->AddObserver(&observer); | 155 proxy_config_service_->AddObserver(&observer); |
| 157 | 156 |
| 158 // Firing the observers in the delegate should trigger a notification. | 157 // Firing the observers in the delegate should trigger a notification. |
| 159 net::ProxyConfig config2; | 158 net::ProxyConfig config2; |
| 160 config2.set_auto_detect(true); | 159 config2.set_auto_detect(true); |
| 161 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config2), | 160 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config2), |
| 162 CONFIG_VALID)).Times(1); | 161 CONFIG_VALID)).Times(1); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config4), | 197 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config4), |
| 199 CONFIG_VALID)).Times(1); | 198 CONFIG_VALID)).Times(1); |
| 200 delegate_service_->SetProxyConfig(config4, CONFIG_VALID); | 199 delegate_service_->SetProxyConfig(config4, CONFIG_VALID); |
| 201 base::RunLoop().RunUntilIdle(); | 200 base::RunLoop().RunUntilIdle(); |
| 202 Mock::VerifyAndClearExpectations(&observer); | 201 Mock::VerifyAndClearExpectations(&observer); |
| 203 | 202 |
| 204 proxy_config_service_->RemoveObserver(&observer); | 203 proxy_config_service_->RemoveObserver(&observer); |
| 205 } | 204 } |
| 206 | 205 |
| 207 TEST_F(PrefProxyConfigTrackerImplTest, Fallback) { | 206 TEST_F(PrefProxyConfigTrackerImplTest, Fallback) { |
| 208 InitConfigService(net::ProxyConfigService::CONFIG_VALID); | |
| 209 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID = | 207 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID = |
| 210 net::ProxyConfigService::CONFIG_VALID; | 208 net::ProxyConfigService::CONFIG_VALID; |
| 211 MockObserver observer; | 209 MockObserver observer; |
| 212 net::ProxyConfig actual_config; | 210 net::ProxyConfig actual_config; |
| 213 delegate_service_->SetProxyConfig(net::ProxyConfig::CreateDirect(), | 211 delegate_service_->SetProxyConfig(net::ProxyConfig::CreateDirect(), |
| 214 net::ProxyConfigService::CONFIG_UNSET); | 212 net::ProxyConfigService::CONFIG_UNSET); |
| 215 proxy_config_service_->AddObserver(&observer); | 213 proxy_config_service_->AddObserver(&observer); |
| 216 | 214 |
| 217 // Prepare test data. | 215 // Prepare test data. |
| 218 net::ProxyConfig recommended_config = net::ProxyConfig::CreateAutoDetect(); | 216 net::ProxyConfig recommended_config = net::ProxyConfig::CreateAutoDetect(); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 base::RunLoop().RunUntilIdle(); | 250 base::RunLoop().RunUntilIdle(); |
| 253 Mock::VerifyAndClearExpectations(&observer); | 251 Mock::VerifyAndClearExpectations(&observer); |
| 254 EXPECT_EQ(CONFIG_VALID, | 252 EXPECT_EQ(CONFIG_VALID, |
| 255 proxy_config_service_->GetLatestProxyConfig(&actual_config)); | 253 proxy_config_service_->GetLatestProxyConfig(&actual_config)); |
| 256 EXPECT_TRUE(actual_config.Equals(recommended_config)); | 254 EXPECT_TRUE(actual_config.Equals(recommended_config)); |
| 257 | 255 |
| 258 proxy_config_service_->RemoveObserver(&observer); | 256 proxy_config_service_->RemoveObserver(&observer); |
| 259 } | 257 } |
| 260 | 258 |
| 261 TEST_F(PrefProxyConfigTrackerImplTest, ExplicitSystemSettings) { | 259 TEST_F(PrefProxyConfigTrackerImplTest, ExplicitSystemSettings) { |
| 262 InitConfigService(net::ProxyConfigService::CONFIG_VALID); | |
| 263 pref_service_->SetRecommendedPref(proxy_config::prefs::kProxy, | 260 pref_service_->SetRecommendedPref(proxy_config::prefs::kProxy, |
| 264 ProxyConfigDictionary::CreateAutoDetect()); | 261 ProxyConfigDictionary::CreateAutoDetect()); |
| 265 pref_service_->SetUserPref(proxy_config::prefs::kProxy, | 262 pref_service_->SetUserPref(proxy_config::prefs::kProxy, |
| 266 ProxyConfigDictionary::CreateSystem()); | 263 ProxyConfigDictionary::CreateSystem()); |
| 267 base::RunLoop().RunUntilIdle(); | 264 base::RunLoop().RunUntilIdle(); |
| 268 | 265 |
| 269 // Test if we actually use the system setting, which is |kFixedPacUrl|. | 266 // Test if we actually use the system setting, which is |kFixedPacUrl|. |
| 270 net::ProxyConfig actual_config; | 267 net::ProxyConfig actual_config; |
| 271 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, | 268 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, |
| 272 proxy_config_service_->GetLatestProxyConfig(&actual_config)); | 269 proxy_config_service_->GetLatestProxyConfig(&actual_config)); |
| 273 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url()); | 270 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url()); |
| 274 } | 271 } |
| 275 | 272 |
| 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 | |
| 306 } // namespace | 273 } // namespace |
| OLD | NEW |