1 // Copyright 2012 The Chromium Authors 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 "net/base/mock_network_change_notifier.h" 6 7 #include <utility> 8 9 #include "base/memory/ptr_util.h" 10 #include "base/run_loop.h" 11 #include "net/dns/dns_config_service.h" 12 #include "net/dns/system_dns_config_change_notifier.h" 13 14 namespace net::test { 15 16 // static Create()17std::unique_ptr<MockNetworkChangeNotifier> MockNetworkChangeNotifier::Create() { 18 // Use an empty noop SystemDnsConfigChangeNotifier to disable actual system 19 // DNS configuration notifications. 20 return base::WrapUnique(new MockNetworkChangeNotifier( 21 std::make_unique<SystemDnsConfigChangeNotifier>( 22 nullptr /* task_runner */, nullptr /* dns_config_service */))); 23 } 24 ~MockNetworkChangeNotifier()25MockNetworkChangeNotifier::~MockNetworkChangeNotifier() { 26 StopSystemDnsConfigNotifier(); 27 } 28 29 MockNetworkChangeNotifier::ConnectionType GetCurrentConnectionType() const30MockNetworkChangeNotifier::GetCurrentConnectionType() const { 31 return connection_type_; 32 } 33 ForceNetworkHandlesSupported()34void MockNetworkChangeNotifier::ForceNetworkHandlesSupported() { 35 force_network_handles_supported_ = true; 36 } 37 AreNetworkHandlesCurrentlySupported() const38bool MockNetworkChangeNotifier::AreNetworkHandlesCurrentlySupported() const { 39 return force_network_handles_supported_; 40 } 41 SetConnectedNetworksList(const NetworkList & network_list)42void MockNetworkChangeNotifier::SetConnectedNetworksList( 43 const NetworkList& network_list) { 44 connected_networks_ = network_list; 45 } 46 GetCurrentConnectedNetworks(NetworkList * network_list) const47void MockNetworkChangeNotifier::GetCurrentConnectedNetworks( 48 NetworkList* network_list) const { 49 network_list->clear(); 50 *network_list = connected_networks_; 51 } 52 NotifyNetworkMadeDefault(handles::NetworkHandle network)53void MockNetworkChangeNotifier::NotifyNetworkMadeDefault( 54 handles::NetworkHandle network) { 55 QueueNetworkMadeDefault(network); 56 // Spin the message loop so the notification is delivered. 57 base::RunLoop().RunUntilIdle(); 58 } 59 QueueNetworkMadeDefault(handles::NetworkHandle network)60void MockNetworkChangeNotifier::QueueNetworkMadeDefault( 61 handles::NetworkHandle network) { 62 NetworkChangeNotifier::NotifyObserversOfSpecificNetworkChange( 63 NetworkChangeNotifier::NetworkChangeType::kMadeDefault, network); 64 } 65 NotifyNetworkDisconnected(handles::NetworkHandle network)66void MockNetworkChangeNotifier::NotifyNetworkDisconnected( 67 handles::NetworkHandle network) { 68 QueueNetworkDisconnected(network); 69 // Spin the message loop so the notification is delivered. 70 base::RunLoop().RunUntilIdle(); 71 } 72 QueueNetworkDisconnected(handles::NetworkHandle network)73void MockNetworkChangeNotifier::QueueNetworkDisconnected( 74 handles::NetworkHandle network) { 75 NetworkChangeNotifier::NotifyObserversOfSpecificNetworkChange( 76 NetworkChangeNotifier::NetworkChangeType::kDisconnected, network); 77 } 78 NotifyNetworkConnected(handles::NetworkHandle network)79void MockNetworkChangeNotifier::NotifyNetworkConnected( 80 handles::NetworkHandle network) { 81 NetworkChangeNotifier::NotifyObserversOfSpecificNetworkChange( 82 NetworkChangeNotifier::NetworkChangeType::kConnected, network); 83 // Spin the message loop so the notification is delivered. 84 base::RunLoop().RunUntilIdle(); 85 } 86 IsDefaultNetworkActiveInternal()87bool MockNetworkChangeNotifier::IsDefaultNetworkActiveInternal() { 88 return is_default_network_active_; 89 } 90 SetConnectionTypeAndNotifyObservers(ConnectionType connection_type)91void MockNetworkChangeNotifier::SetConnectionTypeAndNotifyObservers( 92 ConnectionType connection_type) { 93 SetConnectionType(connection_type); 94 NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange(); 95 // Spin the message loop so the notification is delivered. 96 base::RunLoop().RunUntilIdle(); 97 } 98 99 MockNetworkChangeNotifier::ConnectionCost GetCurrentConnectionCost()100MockNetworkChangeNotifier::GetCurrentConnectionCost() { 101 if (use_default_connection_cost_implementation_) 102 return NetworkChangeNotifier::GetCurrentConnectionCost(); 103 return connection_cost_; 104 } 105 106 #if BUILDFLAG(IS_LINUX) GetAddressMapOwnerInternal()107AddressMapOwnerLinux* MockNetworkChangeNotifier::GetAddressMapOwnerInternal() { 108 return address_map_owner_; 109 } 110 #endif // BUILDFLAG(IS_LINUX) 111 MockNetworkChangeNotifier(std::unique_ptr<SystemDnsConfigChangeNotifier> dns_config_notifier)112MockNetworkChangeNotifier::MockNetworkChangeNotifier( 113 std::unique_ptr<SystemDnsConfigChangeNotifier> dns_config_notifier) 114 : NetworkChangeNotifier(NetworkChangeCalculatorParams(), 115 dns_config_notifier.get()), 116 dns_config_notifier_(std::move(dns_config_notifier)) {} 117 ScopedMockNetworkChangeNotifier()118ScopedMockNetworkChangeNotifier::ScopedMockNetworkChangeNotifier() 119 : disable_network_change_notifier_for_tests_( 120 std::make_unique<NetworkChangeNotifier::DisableForTest>()), 121 mock_network_change_notifier_(MockNetworkChangeNotifier::Create()) {} 122 123 ScopedMockNetworkChangeNotifier::~ScopedMockNetworkChangeNotifier() = default; 124 125 MockNetworkChangeNotifier* mock_network_change_notifier()126ScopedMockNetworkChangeNotifier::mock_network_change_notifier() { 127 return mock_network_change_notifier_.get(); 128 } 129 130 } // namespace net::test 131