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 | |
| 7 | #include "base/run_loop.h" |
| 8 | |
| 9 | namespace net { |
| 10 | namespace test { |
| 11 | |
| 12 | MockNetworkChangeNotifier::MockNetworkChangeNotifier() |
rch | d6163f3 | 2017-01-30 23:50:38 | [diff] [blame] | 13 | : force_network_handles_supported_(false), |
| 14 | connection_type_(CONNECTION_UNKNOWN) {} |
rch | e11300ef | 2016-09-02 01:44:28 | [diff] [blame] | 15 | MockNetworkChangeNotifier::~MockNetworkChangeNotifier() {} |
| 16 | |
| 17 | MockNetworkChangeNotifier::ConnectionType |
| 18 | MockNetworkChangeNotifier::GetCurrentConnectionType() const { |
rch | d6163f3 | 2017-01-30 23:50:38 | [diff] [blame] | 19 | return connection_type_; |
rch | e11300ef | 2016-09-02 01:44:28 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | void MockNetworkChangeNotifier::ForceNetworkHandlesSupported() { |
| 23 | force_network_handles_supported_ = true; |
| 24 | } |
| 25 | |
| 26 | bool MockNetworkChangeNotifier::AreNetworkHandlesCurrentlySupported() const { |
| 27 | return force_network_handles_supported_; |
| 28 | } |
| 29 | |
| 30 | void MockNetworkChangeNotifier::SetConnectedNetworksList( |
| 31 | const NetworkList& network_list) { |
| 32 | connected_networks_ = network_list; |
| 33 | } |
| 34 | |
| 35 | void MockNetworkChangeNotifier::GetCurrentConnectedNetworks( |
| 36 | NetworkList* network_list) const { |
| 37 | network_list->clear(); |
| 38 | *network_list = connected_networks_; |
| 39 | } |
| 40 | |
| 41 | void MockNetworkChangeNotifier::NotifyNetworkMadeDefault( |
| 42 | NetworkChangeNotifier::NetworkHandle network) { |
jri | 9f30371 | 2016-09-13 01:10:22 | [diff] [blame] | 43 | QueueNetworkMadeDefault(network); |
rch | e11300ef | 2016-09-02 01:44:28 | [diff] [blame] | 44 | // Spin the message loop so the notification is delivered. |
| 45 | base::RunLoop().RunUntilIdle(); |
| 46 | } |
| 47 | |
jri | 9f30371 | 2016-09-13 01:10:22 | [diff] [blame] | 48 | void MockNetworkChangeNotifier::QueueNetworkMadeDefault( |
| 49 | NetworkChangeNotifier::NetworkHandle network) { |
| 50 | NetworkChangeNotifier::NotifyObserversOfSpecificNetworkChange( |
| 51 | NetworkChangeNotifier::MADE_DEFAULT, network); |
| 52 | } |
| 53 | |
rch | e11300ef | 2016-09-02 01:44:28 | [diff] [blame] | 54 | void MockNetworkChangeNotifier::NotifyNetworkDisconnected( |
| 55 | NetworkChangeNotifier::NetworkHandle network) { |
jri | 9f30371 | 2016-09-13 01:10:22 | [diff] [blame] | 56 | QueueNetworkDisconnected(network); |
| 57 | // Spin the message loop so the notification is delivered. |
| 58 | base::RunLoop().RunUntilIdle(); |
| 59 | } |
| 60 | |
| 61 | void MockNetworkChangeNotifier::QueueNetworkDisconnected( |
| 62 | NetworkChangeNotifier::NetworkHandle network) { |
rch | e11300ef | 2016-09-02 01:44:28 | [diff] [blame] | 63 | NetworkChangeNotifier::NotifyObserversOfSpecificNetworkChange( |
| 64 | NetworkChangeNotifier::DISCONNECTED, network); |
rch | e11300ef | 2016-09-02 01:44:28 | [diff] [blame] | 65 | } |
| 66 | |
jri | 5b78551 | 2016-09-13 04:29:11 | [diff] [blame] | 67 | void MockNetworkChangeNotifier::NotifyNetworkConnected( |
| 68 | NetworkChangeNotifier::NetworkHandle network) { |
| 69 | NetworkChangeNotifier::NotifyObserversOfSpecificNetworkChange( |
| 70 | NetworkChangeNotifier::CONNECTED, network); |
| 71 | // Spin the message loop so the notification is delivered. |
| 72 | base::RunLoop().RunUntilIdle(); |
| 73 | } |
| 74 | |
rch | e11300ef | 2016-09-02 01:44:28 | [diff] [blame] | 75 | ScopedMockNetworkChangeNotifier::ScopedMockNetworkChangeNotifier() |
| 76 | : disable_network_change_notifier_for_tests_( |
| 77 | new NetworkChangeNotifier::DisableForTest()), |
| 78 | mock_network_change_notifier_(new MockNetworkChangeNotifier()) {} |
| 79 | |
| 80 | ScopedMockNetworkChangeNotifier::~ScopedMockNetworkChangeNotifier() {} |
| 81 | |
| 82 | MockNetworkChangeNotifier* |
| 83 | ScopedMockNetworkChangeNotifier::mock_network_change_notifier() { |
| 84 | return mock_network_change_notifier_.get(); |
| 85 | } |
| 86 | |
| 87 | } // namespace test |
| 88 | } // namespace net |