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