mock_network_change_notifier.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #ifndef NET_BASE_MOCK_NETWORK_CHANGE_NOTIFIER_H_
  5. #define NET_BASE_MOCK_NETWORK_CHANGE_NOTIFIER_H_
  6. #include <memory>
  7. #include "net/base/network_change_notifier.h"
  8. #include "net/base/network_handle.h"
  9. namespace net {
  10. class SystemDnsConfigChangeNotifier;
  11. namespace test {
  12. class MockNetworkChangeNotifier : public NetworkChangeNotifier {
  13. public:
  14. static std::unique_ptr<MockNetworkChangeNotifier> Create();
  15. ~MockNetworkChangeNotifier() override;
  16. ConnectionType GetCurrentConnectionType() const override;
  17. void ForceNetworkHandlesSupported();
  18. bool AreNetworkHandlesCurrentlySupported() const override;
  19. void SetConnectionType(ConnectionType connection_type) {
  20. connection_type_ = connection_type;
  21. }
  22. void SetConnectedNetworksList(const NetworkList& network_list);
  23. void GetCurrentConnectedNetworks(NetworkList* network_list) const override;
  24. // Delivers a MADE_DEFAULT notification to observers.
  25. void NotifyNetworkMadeDefault(handles::NetworkHandle network);
  26. // Queues a MADE_DEFAULT notification to be delivered to observers
  27. // but does not spin the message loop to actually deliver it.
  28. void QueueNetworkMadeDefault(handles::NetworkHandle network);
  29. // Delivers a DISCONNECTED notification to observers.
  30. void NotifyNetworkDisconnected(handles::NetworkHandle network);
  31. // Queues a DISCONNECTED notification to be delivered to observers
  32. // but does not spin the message loop to actually deliver it.
  33. void QueueNetworkDisconnected(handles::NetworkHandle network);
  34. // Delivers a CONNECTED notification to observers.
  35. void NotifyNetworkConnected(handles::NetworkHandle network);
  36. void SetConnectionTypeAndNotifyObservers(ConnectionType connection_type);
  37. // Sets the cached value of the connection cost. If
  38. // use_default_connection_cost_implementation is set to true, this value gets
  39. // ignored.
  40. void SetConnectionCost(ConnectionCost connection_cost) {
  41. connection_cost_ = connection_cost;
  42. }
  43. // Tells this class to ignore its cached connection cost value and instead
  44. // call the base class's implementation. This is intended to allow tests to
  45. // mock the product code's fallback to the default implementation in certain
  46. // situations. For example, the APIs to support this functionality exist on
  47. // Win10 only so it falls back to the default on Win7, so this function allows
  48. // tests to validate the default implementation's behavior on Win10 machines.
  49. void SetUseDefaultConnectionCostImplementation(
  50. bool use_default_connection_cost_implementation) {
  51. use_default_connection_cost_implementation_ =
  52. use_default_connection_cost_implementation;
  53. }
  54. // Returns either the cached connection cost value or the default
  55. // implementation's result, depending on whether
  56. // use_default_connection_cost_implementation is set to true.
  57. ConnectionCost GetCurrentConnectionCost() override;
  58. private:
  59. // Create using MockNetworkChangeNotifier::Create().
  60. explicit MockNetworkChangeNotifier(
  61. std::unique_ptr<SystemDnsConfigChangeNotifier> dns_config_notifier);
  62. bool force_network_handles_supported_ = false;
  63. ConnectionType connection_type_ = CONNECTION_UNKNOWN;
  64. ConnectionCost connection_cost_ = CONNECTION_COST_UNKNOWN;
  65. bool use_default_connection_cost_implementation_ = false;
  66. NetworkChangeNotifier::NetworkList connected_networks_;
  67. std::unique_ptr<SystemDnsConfigChangeNotifier> dns_config_notifier_;
  68. };
  69. // Class to replace existing NetworkChangeNotifier singleton with a
  70. // MockNetworkChangeNotifier for a test. To use, simply create a
  71. // ScopedMockNetworkChangeNotifier object in the test.
  72. class ScopedMockNetworkChangeNotifier {
  73. public:
  74. ScopedMockNetworkChangeNotifier();
  75. ~ScopedMockNetworkChangeNotifier();
  76. MockNetworkChangeNotifier* mock_network_change_notifier();
  77. private:
  78. std::unique_ptr<NetworkChangeNotifier::DisableForTest>
  79. disable_network_change_notifier_for_tests_;
  80. std::unique_ptr<MockNetworkChangeNotifier> mock_network_change_notifier_;
  81. };
  82. } // namespace test
  83. } // namespace net
  84. #endif // NET_BASE_MOCK_NETWORK_CHANGE_NOTIFIER_H_