rch | e11300ef | 2016-09-02 01:44:28 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
mgersh | af9a923 | 2017-04-13 20:19:03 | [diff] [blame] | 5 | #include "net/base/mock_network_change_notifier.h" |
rch | e11300ef | 2016-09-02 01:44:28 | [diff] [blame] | 6 | |
Eric Orth | c398f1e | 2019-07-09 21:54:55 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
| 9 | #include "base/memory/ptr_util.h" |
rch | e11300ef | 2016-09-02 01:44:28 | [diff] [blame] | 10 | #include "base/run_loop.h" |
Eric Orth | c398f1e | 2019-07-09 21:54:55 | [diff] [blame] | 11 | #include "net/dns/dns_config_service.h" |
| 12 | #include "net/dns/system_dns_config_change_notifier.h" |
rch | e11300ef | 2016-09-02 01:44:28 | [diff] [blame] | 13 | |
| 14 | namespace net { |
| 15 | namespace test { |
| 16 | |
Eric Orth | c398f1e | 2019-07-09 21:54:55 | [diff] [blame] | 17 | // static |
| 18 | std::unique_ptr<MockNetworkChangeNotifier> MockNetworkChangeNotifier::Create() { |
| 19 | // Use an empty noop SystemDnsConfigChangeNotifier to disable actual system |
| 20 | // DNS configuration notifications. |
| 21 | return base::WrapUnique(new MockNetworkChangeNotifier( |
| 22 | std::make_unique<SystemDnsConfigChangeNotifier>( |
| 23 | nullptr /* task_runner */, nullptr /* dns_config_service */))); |
| 24 | } |
| 25 | |
| 26 | MockNetworkChangeNotifier::~MockNetworkChangeNotifier() { |
| 27 | StopSystemDnsConfigNotifier(); |
| 28 | } |
rch | e11300ef | 2016-09-02 01:44:28 | [diff] [blame] | 29 | |
| 30 | MockNetworkChangeNotifier::ConnectionType |
| 31 | MockNetworkChangeNotifier::GetCurrentConnectionType() const { |
rch | d6163f3 | 2017-01-30 23:50:38 | [diff] [blame] | 32 | return connection_type_; |
rch | e11300ef | 2016-09-02 01:44:28 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | void MockNetworkChangeNotifier::ForceNetworkHandlesSupported() { |
| 36 | force_network_handles_supported_ = true; |
| 37 | } |
| 38 | |
| 39 | bool MockNetworkChangeNotifier::AreNetworkHandlesCurrentlySupported() const { |
| 40 | return force_network_handles_supported_; |
| 41 | } |
| 42 | |
| 43 | void MockNetworkChangeNotifier::SetConnectedNetworksList( |
| 44 | const NetworkList& network_list) { |
| 45 | connected_networks_ = network_list; |
| 46 | } |
| 47 | |
| 48 | void MockNetworkChangeNotifier::GetCurrentConnectedNetworks( |
| 49 | NetworkList* network_list) const { |
| 50 | network_list->clear(); |
| 51 | *network_list = connected_networks_; |
| 52 | } |
| 53 | |
| 54 | void MockNetworkChangeNotifier::NotifyNetworkMadeDefault( |
| 55 | NetworkChangeNotifier::NetworkHandle network) { |
jri | 9f30371 | 2016-09-13 01:10:22 | [diff] [blame] | 56 | QueueNetworkMadeDefault(network); |
rch | e11300ef | 2016-09-02 01:44:28 | [diff] [blame] | 57 | // Spin the message loop so the notification is delivered. |
| 58 | base::RunLoop().RunUntilIdle(); |
| 59 | } |
| 60 | |
jri | 9f30371 | 2016-09-13 01:10:22 | [diff] [blame] | 61 | void MockNetworkChangeNotifier::QueueNetworkMadeDefault( |
| 62 | NetworkChangeNotifier::NetworkHandle network) { |
| 63 | NetworkChangeNotifier::NotifyObserversOfSpecificNetworkChange( |
| 64 | NetworkChangeNotifier::MADE_DEFAULT, network); |
| 65 | } |
| 66 | |
rch | e11300ef | 2016-09-02 01:44:28 | [diff] [blame] | 67 | void MockNetworkChangeNotifier::NotifyNetworkDisconnected( |
| 68 | NetworkChangeNotifier::NetworkHandle network) { |
jri | 9f30371 | 2016-09-13 01:10:22 | [diff] [blame] | 69 | QueueNetworkDisconnected(network); |
| 70 | // Spin the message loop so the notification is delivered. |
| 71 | base::RunLoop().RunUntilIdle(); |
| 72 | } |
| 73 | |
| 74 | void MockNetworkChangeNotifier::QueueNetworkDisconnected( |
| 75 | NetworkChangeNotifier::NetworkHandle network) { |
rch | e11300ef | 2016-09-02 01:44:28 | [diff] [blame] | 76 | NetworkChangeNotifier::NotifyObserversOfSpecificNetworkChange( |
| 77 | NetworkChangeNotifier::DISCONNECTED, network); |
rch | e11300ef | 2016-09-02 01:44:28 | [diff] [blame] | 78 | } |
| 79 | |
jri | 5b78551 | 2016-09-13 04:29:11 | [diff] [blame] | 80 | void MockNetworkChangeNotifier::NotifyNetworkConnected( |
| 81 | NetworkChangeNotifier::NetworkHandle network) { |
| 82 | NetworkChangeNotifier::NotifyObserversOfSpecificNetworkChange( |
| 83 | NetworkChangeNotifier::CONNECTED, network); |
| 84 | // Spin the message loop so the notification is delivered. |
| 85 | base::RunLoop().RunUntilIdle(); |
| 86 | } |
| 87 | |
Eric Orth | 2393586 | 2020-01-10 00:42:24 | [diff] [blame] | 88 | void MockNetworkChangeNotifier::SetConnectionTypeAndNotifyObservers( |
| 89 | ConnectionType connection_type) { |
| 90 | SetConnectionType(connection_type); |
| 91 | NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange(); |
| 92 | // Spin the message loop so the notification is delivered. |
| 93 | base::RunLoop().RunUntilIdle(); |
| 94 | } |
| 95 | |
Eric Orth | c398f1e | 2019-07-09 21:54:55 | [diff] [blame] | 96 | MockNetworkChangeNotifier::MockNetworkChangeNotifier( |
| 97 | std::unique_ptr<SystemDnsConfigChangeNotifier> dns_config_notifier) |
| 98 | : NetworkChangeNotifier(NetworkChangeCalculatorParams(), |
| 99 | dns_config_notifier.get()), |
| 100 | force_network_handles_supported_(false), |
| 101 | connection_type_(CONNECTION_UNKNOWN), |
| 102 | dns_config_notifier_(std::move(dns_config_notifier)) {} |
| 103 | |
rch | e11300ef | 2016-09-02 01:44:28 | [diff] [blame] | 104 | ScopedMockNetworkChangeNotifier::ScopedMockNetworkChangeNotifier() |
| 105 | : disable_network_change_notifier_for_tests_( |
| 106 | new NetworkChangeNotifier::DisableForTest()), |
Eric Orth | c398f1e | 2019-07-09 21:54:55 | [diff] [blame] | 107 | mock_network_change_notifier_(MockNetworkChangeNotifier::Create()) {} |
rch | e11300ef | 2016-09-02 01:44:28 | [diff] [blame] | 108 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 109 | ScopedMockNetworkChangeNotifier::~ScopedMockNetworkChangeNotifier() = default; |
rch | e11300ef | 2016-09-02 01:44:28 | [diff] [blame] | 110 | |
| 111 | MockNetworkChangeNotifier* |
| 112 | ScopedMockNetworkChangeNotifier::mock_network_change_notifier() { |
| 113 | return mock_network_change_notifier_.get(); |
| 114 | } |
| 115 | |
| 116 | } // namespace test |
| 117 | } // namespace net |