OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chromeos/network/proxy/proxy_config_service_impl.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/test/scoped_task_environment.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "base/values.h" |
| 13 #include "chromeos/dbus/dbus_thread_manager.h" |
| 14 #include "chromeos/network/network_handler.h" |
| 15 #include "chromeos/network/network_state_test.h" |
| 16 #include "components/prefs/pref_service.h" |
| 17 #include "components/prefs/testing_pref_service.h" |
| 18 #include "components/proxy_config/pref_proxy_config_tracker_impl.h" |
| 19 #include "components/proxy_config/proxy_config_pref_names.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 |
| 22 namespace chromeos { |
| 23 namespace { |
| 24 |
| 25 const char kFixedPacUrl[] = "http://fixed/"; |
| 26 |
| 27 class TestProxyConfigService : public net::ProxyConfigService { |
| 28 public: |
| 29 TestProxyConfigService(const net::ProxyConfig& config, |
| 30 ConfigAvailability availability) |
| 31 : config_(config), availability_(availability) {} |
| 32 |
| 33 private: |
| 34 void AddObserver(net::ProxyConfigService::Observer* observer) override {} |
| 35 void RemoveObserver(net::ProxyConfigService::Observer* observer) override {} |
| 36 |
| 37 net::ProxyConfigService::ConfigAvailability GetLatestProxyConfig( |
| 38 net::ProxyConfig* config) override { |
| 39 *config = config_; |
| 40 return availability_; |
| 41 } |
| 42 |
| 43 net::ProxyConfig config_; |
| 44 ConfigAvailability availability_; |
| 45 }; |
| 46 |
| 47 } // namespace |
| 48 |
| 49 class ProxyConfigServiceImplTest : public NetworkStateTest { |
| 50 void SetUp() override { |
| 51 DBusThreadManager::Initialize(); |
| 52 chromeos::NetworkHandler::Initialize(); |
| 53 NetworkStateTest::SetUp(); |
| 54 } |
| 55 |
| 56 void TearDown() override { |
| 57 NetworkStateTest::TearDown(); |
| 58 chromeos::NetworkHandler::Shutdown(); |
| 59 DBusThreadManager::Shutdown(); |
| 60 } |
| 61 |
| 62 protected: |
| 63 base::test::ScopedTaskEnvironment environment_; |
| 64 }; |
| 65 |
| 66 // By default, ProxyConfigServiceImpl should ignore the state of the nested |
| 67 // ProxyConfigService. |
| 68 TEST_F(ProxyConfigServiceImplTest, IgnoresNestedProxyConfigServiceByDefault) { |
| 69 TestingPrefServiceSimple profile_prefs; |
| 70 PrefProxyConfigTrackerImpl::RegisterProfilePrefs(profile_prefs.registry()); |
| 71 TestingPrefServiceSimple local_state_prefs; |
| 72 |
| 73 net::ProxyConfig fixed_config; |
| 74 fixed_config.set_pac_url(GURL(kFixedPacUrl)); |
| 75 std::unique_ptr<TestProxyConfigService> nested_service = |
| 76 base::MakeUnique<TestProxyConfigService>( |
| 77 fixed_config, net::ProxyConfigService::CONFIG_VALID); |
| 78 |
| 79 ProxyConfigServiceImpl proxy_tracker(&profile_prefs, &local_state_prefs, |
| 80 base::ThreadTaskRunnerHandle::Get()); |
| 81 |
| 82 std::unique_ptr<net::ProxyConfigService> proxy_service = |
| 83 proxy_tracker.CreateTrackingProxyConfigService(std::move(nested_service)); |
| 84 |
| 85 net::ProxyConfig config; |
| 86 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, |
| 87 proxy_service->GetLatestProxyConfig(&config)); |
| 88 EXPECT_TRUE(config.Equals(net::ProxyConfig::CreateDirect())); |
| 89 |
| 90 environment_.RunUntilIdle(); |
| 91 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, |
| 92 proxy_service->GetLatestProxyConfig(&config)); |
| 93 EXPECT_TRUE(config.Equals(net::ProxyConfig::CreateDirect())); |
| 94 |
| 95 proxy_tracker.DetachFromPrefService(); |
| 96 } |
| 97 |
| 98 // Sets proxy_config::prefs::kUseSharedProxies to true, and makes sure the |
| 99 // nested ProxyConfigService is used. |
| 100 TEST_F(ProxyConfigServiceImplTest, UsesNestedProxyConfigService) { |
| 101 TestingPrefServiceSimple profile_prefs; |
| 102 PrefProxyConfigTrackerImpl::RegisterProfilePrefs(profile_prefs.registry()); |
| 103 TestingPrefServiceSimple local_state_prefs; |
| 104 profile_prefs.SetUserPref(proxy_config::prefs::kUseSharedProxies, |
| 105 base::MakeUnique<base::Value>(true)); |
| 106 |
| 107 net::ProxyConfig fixed_config; |
| 108 fixed_config.set_pac_url(GURL(kFixedPacUrl)); |
| 109 std::unique_ptr<TestProxyConfigService> nested_service = |
| 110 base::MakeUnique<TestProxyConfigService>( |
| 111 fixed_config, net::ProxyConfigService::CONFIG_VALID); |
| 112 |
| 113 ProxyConfigServiceImpl proxy_tracker(&profile_prefs, &local_state_prefs, |
| 114 base::ThreadTaskRunnerHandle::Get()); |
| 115 |
| 116 std::unique_ptr<net::ProxyConfigService> proxy_service = |
| 117 proxy_tracker.CreateTrackingProxyConfigService(std::move(nested_service)); |
| 118 |
| 119 net::ProxyConfig config; |
| 120 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, |
| 121 proxy_service->GetLatestProxyConfig(&config)); |
| 122 EXPECT_TRUE(config.Equals(fixed_config)); |
| 123 |
| 124 environment_.RunUntilIdle(); |
| 125 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, |
| 126 proxy_service->GetLatestProxyConfig(&config)); |
| 127 EXPECT_TRUE(config.Equals(fixed_config)); |
| 128 |
| 129 proxy_tracker.DetachFromPrefService(); |
| 130 } |
| 131 |
| 132 } // namespace chromeos |
OLD | NEW |