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 #ifndef NET_BASE_MOCK_NETWORK_CHANGE_NOTIFIER_H_ 6 #define NET_BASE_MOCK_NETWORK_CHANGE_NOTIFIER_H_ 7 8 #include <memory> 9 10 #include "net/base/network_change_notifier.h" 11 #include "net/base/network_handle.h" 12 13 namespace net { 14 15 class SystemDnsConfigChangeNotifier; 16 17 namespace test { 18 19 class MockNetworkChangeNotifier : public NetworkChangeNotifier { 20 public: 21 static std::unique_ptr<MockNetworkChangeNotifier> Create(); 22 ~MockNetworkChangeNotifier() override; 23 24 ConnectionType GetCurrentConnectionType() const override; 25 26 void ForceNetworkHandlesSupported(); 27 28 bool AreNetworkHandlesCurrentlySupported() const override; 29 SetConnectionType(ConnectionType connection_type)30 void SetConnectionType(ConnectionType connection_type) { 31 connection_type_ = connection_type; 32 } 33 34 void SetConnectedNetworksList(const NetworkList& network_list); 35 36 void GetCurrentConnectedNetworks(NetworkList* network_list) const override; 37 38 // Delivers a MADE_DEFAULT notification to observers. 39 void NotifyNetworkMadeDefault(handles::NetworkHandle network); 40 41 // Queues a MADE_DEFAULT notification to be delivered to observers 42 // but does not spin the message loop to actually deliver it. 43 void QueueNetworkMadeDefault(handles::NetworkHandle network); 44 45 // Delivers a DISCONNECTED notification to observers. 46 void NotifyNetworkDisconnected(handles::NetworkHandle network); 47 48 // Queues a DISCONNECTED notification to be delivered to observers 49 // but does not spin the message loop to actually deliver it. 50 void QueueNetworkDisconnected(handles::NetworkHandle network); 51 52 // Delivers a CONNECTED notification to observers. 53 void NotifyNetworkConnected(handles::NetworkHandle network); 54 55 void SetConnectionTypeAndNotifyObservers(ConnectionType connection_type); 56 57 // Sets the cached value of the connection cost. If 58 // use_default_connection_cost_implementation is set to true, this value gets 59 // ignored. SetConnectionCost(ConnectionCost connection_cost)60 void SetConnectionCost(ConnectionCost connection_cost) { 61 connection_cost_ = connection_cost; 62 } 63 64 bool IsDefaultNetworkActiveInternal() override; 65 SetIsDefaultNetworkActiveInternalForTesting(bool is_active)66 void SetIsDefaultNetworkActiveInternalForTesting(bool is_active) { 67 is_default_network_active_ = is_active; 68 } 69 70 // Tells this class to ignore its cached connection cost value and instead 71 // call the base class's implementation. This is intended to allow tests to 72 // mock the product code's fallback to the default implementation in certain 73 // situations. For example, the APIs to support this functionality exist on 74 // Win10 only so it falls back to the default on Win7, so this function allows 75 // tests to validate the default implementation's behavior on Win10 machines. SetUseDefaultConnectionCostImplementation(bool use_default_connection_cost_implementation)76 void SetUseDefaultConnectionCostImplementation( 77 bool use_default_connection_cost_implementation) { 78 use_default_connection_cost_implementation_ = 79 use_default_connection_cost_implementation; 80 } 81 82 // Returns either the cached connection cost value or the default 83 // implementation's result, depending on whether 84 // use_default_connection_cost_implementation is set to true. 85 ConnectionCost GetCurrentConnectionCost() override; 86 87 #if BUILDFLAG(IS_LINUX) SetAddressMapOwnerLinux(AddressMapOwnerLinux * address_map_owner)88 void SetAddressMapOwnerLinux(AddressMapOwnerLinux* address_map_owner) { 89 address_map_owner_ = address_map_owner; 90 } 91 92 AddressMapOwnerLinux* GetAddressMapOwnerInternal() override; 93 #endif 94 95 private: 96 // Create using MockNetworkChangeNotifier::Create(). 97 explicit MockNetworkChangeNotifier( 98 std::unique_ptr<SystemDnsConfigChangeNotifier> dns_config_notifier); 99 100 bool force_network_handles_supported_ = false; 101 bool is_default_network_active_ = true; 102 ConnectionType connection_type_ = CONNECTION_UNKNOWN; 103 ConnectionCost connection_cost_ = CONNECTION_COST_UNKNOWN; 104 bool use_default_connection_cost_implementation_ = false; 105 NetworkChangeNotifier::NetworkList connected_networks_; 106 std::unique_ptr<SystemDnsConfigChangeNotifier> dns_config_notifier_; 107 #if BUILDFLAG(IS_LINUX) 108 raw_ptr<AddressMapOwnerLinux> address_map_owner_ = nullptr; 109 #endif 110 }; 111 112 // Class to replace existing NetworkChangeNotifier singleton with a 113 // MockNetworkChangeNotifier for a test. To use, simply create a 114 // ScopedMockNetworkChangeNotifier object in the test. 115 class ScopedMockNetworkChangeNotifier { 116 public: 117 ScopedMockNetworkChangeNotifier(); 118 ~ScopedMockNetworkChangeNotifier(); 119 120 MockNetworkChangeNotifier* mock_network_change_notifier(); 121 122 private: 123 std::unique_ptr<NetworkChangeNotifier::DisableForTest> 124 disable_network_change_notifier_for_tests_; 125 std::unique_ptr<MockNetworkChangeNotifier> mock_network_change_notifier_; 126 }; 127 128 } // namespace test 129 } // namespace net 130 131 #endif // NET_BASE_MOCK_NETWORK_CHANGE_NOTIFIER_H_ 132