• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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()17 std::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()25 MockNetworkChangeNotifier::~MockNetworkChangeNotifier() {
26   StopSystemDnsConfigNotifier();
27 }
28 
29 MockNetworkChangeNotifier::ConnectionType
GetCurrentConnectionType() const30 MockNetworkChangeNotifier::GetCurrentConnectionType() const {
31   return connection_type_;
32 }
33 
ForceNetworkHandlesSupported()34 void MockNetworkChangeNotifier::ForceNetworkHandlesSupported() {
35   force_network_handles_supported_ = true;
36 }
37 
AreNetworkHandlesCurrentlySupported() const38 bool MockNetworkChangeNotifier::AreNetworkHandlesCurrentlySupported() const {
39   return force_network_handles_supported_;
40 }
41 
SetConnectedNetworksList(const NetworkList & network_list)42 void MockNetworkChangeNotifier::SetConnectedNetworksList(
43     const NetworkList& network_list) {
44   connected_networks_ = network_list;
45 }
46 
GetCurrentConnectedNetworks(NetworkList * network_list) const47 void MockNetworkChangeNotifier::GetCurrentConnectedNetworks(
48     NetworkList* network_list) const {
49   network_list->clear();
50   *network_list = connected_networks_;
51 }
52 
NotifyNetworkMadeDefault(handles::NetworkHandle network)53 void 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)60 void MockNetworkChangeNotifier::QueueNetworkMadeDefault(
61     handles::NetworkHandle network) {
62   NetworkChangeNotifier::NotifyObserversOfSpecificNetworkChange(
63       NetworkChangeNotifier::NetworkChangeType::kMadeDefault, network);
64 }
65 
NotifyNetworkDisconnected(handles::NetworkHandle network)66 void 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)73 void MockNetworkChangeNotifier::QueueNetworkDisconnected(
74     handles::NetworkHandle network) {
75   NetworkChangeNotifier::NotifyObserversOfSpecificNetworkChange(
76       NetworkChangeNotifier::NetworkChangeType::kDisconnected, network);
77 }
78 
NotifyNetworkConnected(handles::NetworkHandle network)79 void 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()87 bool MockNetworkChangeNotifier::IsDefaultNetworkActiveInternal() {
88   return is_default_network_active_;
89 }
90 
SetConnectionTypeAndNotifyObservers(ConnectionType connection_type)91 void 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()100 MockNetworkChangeNotifier::GetCurrentConnectionCost() {
101   if (use_default_connection_cost_implementation_)
102     return NetworkChangeNotifier::GetCurrentConnectionCost();
103   return connection_cost_;
104 }
105 
MockNetworkChangeNotifier(std::unique_ptr<SystemDnsConfigChangeNotifier> dns_config_notifier)106 MockNetworkChangeNotifier::MockNetworkChangeNotifier(
107     std::unique_ptr<SystemDnsConfigChangeNotifier> dns_config_notifier)
108     : NetworkChangeNotifier(NetworkChangeCalculatorParams(),
109                             dns_config_notifier.get()),
110       dns_config_notifier_(std::move(dns_config_notifier)) {}
111 
ScopedMockNetworkChangeNotifier()112 ScopedMockNetworkChangeNotifier::ScopedMockNetworkChangeNotifier()
113     : disable_network_change_notifier_for_tests_(
114           std::make_unique<NetworkChangeNotifier::DisableForTest>()),
115       mock_network_change_notifier_(MockNetworkChangeNotifier::Create()) {}
116 
117 ScopedMockNetworkChangeNotifier::~ScopedMockNetworkChangeNotifier() = default;
118 
119 MockNetworkChangeNotifier*
mock_network_change_notifier()120 ScopedMockNetworkChangeNotifier::mock_network_change_notifier() {
121   return mock_network_change_notifier_.get();
122 }
123 
124 }  // namespace net::test
125