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 private: 88 // Create using MockNetworkChangeNotifier::Create(). 89 explicit MockNetworkChangeNotifier( 90 std::unique_ptr<SystemDnsConfigChangeNotifier> dns_config_notifier); 91 92 bool force_network_handles_supported_ = false; 93 bool is_default_network_active_ = true; 94 ConnectionType connection_type_ = CONNECTION_UNKNOWN; 95 ConnectionCost connection_cost_ = CONNECTION_COST_UNKNOWN; 96 bool use_default_connection_cost_implementation_ = false; 97 NetworkChangeNotifier::NetworkList connected_networks_; 98 std::unique_ptr<SystemDnsConfigChangeNotifier> dns_config_notifier_; 99 }; 100 101 // Class to replace existing NetworkChangeNotifier singleton with a 102 // MockNetworkChangeNotifier for a test. To use, simply create a 103 // ScopedMockNetworkChangeNotifier object in the test. 104 class ScopedMockNetworkChangeNotifier { 105 public: 106 ScopedMockNetworkChangeNotifier(); 107 ~ScopedMockNetworkChangeNotifier(); 108 109 MockNetworkChangeNotifier* mock_network_change_notifier(); 110 111 private: 112 std::unique_ptr<NetworkChangeNotifier::DisableForTest> 113 disable_network_change_notifier_for_tests_; 114 std::unique_ptr<MockNetworkChangeNotifier> mock_network_change_notifier_; 115 }; 116 117 } // namespace test 118 } // namespace net 119 120 #endif // NET_BASE_MOCK_NETWORK_CHANGE_NOTIFIER_H_ 121