| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 #include "net/base/network_change_notifier.h" | 6 #include "net/base/network_change_notifier.h" |
| 7 #include "net/base/network_change_notifier_factory.h" | 7 #include "net/base/network_change_notifier_factory.h" |
| 8 #include "net/base/network_change_notifier_win.h" | 8 #include "net/base/network_change_notifier_win.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 using ::testing::AtLeast; | 12 using ::testing::AtLeast; |
| 13 using ::testing::Invoke; | 13 using ::testing::Invoke; |
| 14 using ::testing::InvokeWithoutArgs; |
| 14 using ::testing::Return; | 15 using ::testing::Return; |
| 15 using ::testing::StrictMock; | 16 using ::testing::StrictMock; |
| 17 using ::testing::_; |
| 16 | 18 |
| 17 namespace net { | 19 namespace net { |
| 18 | 20 |
| 19 namespace { | 21 namespace { |
| 20 | 22 |
| 21 // Subclass of NetworkChangeNotifierWin that overrides functions so that no | 23 // Subclass of NetworkChangeNotifierWin that overrides functions so that no |
| 22 // Windows API networking functions are ever called. | 24 // Windows API networking functions are ever called. |
| 23 class TestNetworkChangeNotifierWin : public NetworkChangeNotifierWin { | 25 class TestNetworkChangeNotifierWin : public NetworkChangeNotifierWin { |
| 24 public: | 26 public: |
| 25 TestNetworkChangeNotifierWin() {} | 27 TestNetworkChangeNotifierWin() {} |
| (...skipping 10 matching lines...) Expand all Loading... |
| 36 return NetworkChangeNotifier::CONNECTION_UNKNOWN; | 38 return NetworkChangeNotifier::CONNECTION_UNKNOWN; |
| 37 } | 39 } |
| 38 | 40 |
| 39 // From NetworkChangeNotifierWin. | 41 // From NetworkChangeNotifierWin. |
| 40 MOCK_METHOD0(WatchForAddressChangeInternal, bool()); | 42 MOCK_METHOD0(WatchForAddressChangeInternal, bool()); |
| 41 | 43 |
| 42 private: | 44 private: |
| 43 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifierWin); | 45 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifierWin); |
| 44 }; | 46 }; |
| 45 | 47 |
| 46 class TestIPAddressObserver | 48 class TestNetworkChangeObserver |
| 47 : public net::NetworkChangeNotifier::IPAddressObserver { | 49 : public net::NetworkChangeNotifier::NetworkChangeObserver { |
| 48 public: | 50 public: |
| 49 TestIPAddressObserver() { | 51 TestNetworkChangeObserver() { |
| 50 NetworkChangeNotifier::AddIPAddressObserver(this); | 52 NetworkChangeNotifier::AddNetworkChangeObserver(this); |
| 51 } | 53 } |
| 52 | 54 |
| 53 ~TestIPAddressObserver() { | 55 ~TestNetworkChangeObserver() { |
| 54 NetworkChangeNotifier::RemoveIPAddressObserver(this); | 56 NetworkChangeNotifier::RemoveNetworkChangeObserver(this); |
| 55 } | 57 } |
| 56 | 58 |
| 57 MOCK_METHOD0(OnIPAddressChanged, void()); | 59 MOCK_METHOD1(OnNetworkChanged, void(NetworkChangeNotifier::ConnectionType)); |
| 58 | 60 |
| 59 private: | 61 private: |
| 60 DISALLOW_COPY_AND_ASSIGN(TestIPAddressObserver); | 62 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeObserver); |
| 61 }; | 63 }; |
| 62 | 64 |
| 63 bool ExitMessageLoopAndReturnFalse() { | 65 bool ExitMessageLoopAndReturnFalse() { |
| 64 MessageLoop::current()->Quit(); | 66 MessageLoop::current()->Quit(); |
| 65 return false; | 67 return false; |
| 66 } | 68 } |
| 67 | 69 |
| 68 } // namespace | 70 } // namespace |
| 69 | 71 |
| 70 class NetworkChangeNotifierWinTest : public testing::Test { | 72 class NetworkChangeNotifierWinTest : public testing::Test { |
| 71 public: | 73 public: |
| 74 NetworkChangeNotifierWinTest() { |
| 75 NetworkChangeNotifier::NetworkChangeCalculatorParams params; |
| 76 params.ip_address_offline_delay_ = base::TimeDelta::FromSeconds(0); |
| 77 params.ip_address_online_delay_ = base::TimeDelta::FromSeconds(0); |
| 78 params.connection_type_offline_delay_ = base::TimeDelta::FromDays(1); |
| 79 params.connection_type_online_delay_ = base::TimeDelta::FromDays(1); |
| 80 network_change_notifier_.ResetNetworkChangeCalculatorParamsForTest(params); |
| 81 } |
| 82 |
| 72 // Calls WatchForAddressChange, and simulates a WatchForAddressChangeInternal | 83 // Calls WatchForAddressChange, and simulates a WatchForAddressChangeInternal |
| 73 // success. Expects that |network_change_notifier_| has just been created, so | 84 // success. Expects that |network_change_notifier_| has just been created, so |
| 74 // it's not watching anything yet, and there have been no previous | 85 // it's not watching anything yet, and there have been no previous |
| 75 // WatchForAddressChangeInternal failures. | 86 // WatchForAddressChangeInternal failures. |
| 76 void StartWatchingAndSucceed() { | 87 void StartWatchingAndSucceed() { |
| 77 EXPECT_FALSE(network_change_notifier_.is_watching()); | 88 EXPECT_FALSE(network_change_notifier_.is_watching()); |
| 78 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); | 89 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); |
| 79 | 90 |
| 80 EXPECT_CALL(test_ip_address_observer_, OnIPAddressChanged()).Times(0); | 91 EXPECT_CALL(test_network_change_observer_, OnNetworkChanged(_)).Times(0); |
| 81 EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal()) | 92 EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal()) |
| 82 .Times(1) | 93 .Times(1) |
| 83 .WillOnce(Return(true)); | 94 .WillOnce(Return(true)); |
| 84 | 95 |
| 85 network_change_notifier_.WatchForAddressChange(); | 96 network_change_notifier_.WatchForAddressChange(); |
| 86 | 97 |
| 87 EXPECT_TRUE(network_change_notifier_.is_watching()); | 98 EXPECT_TRUE(network_change_notifier_.is_watching()); |
| 88 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); | 99 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); |
| 89 | 100 |
| 90 // If a task to notify observers of the IP address change event was | 101 // If a task to notify observers of the IP address change event was |
| 91 // incorrectly posted, make sure it gets run to trigger a failure. | 102 // incorrectly posted, make sure it gets run to trigger a failure. |
| 92 MessageLoop::current()->RunUntilIdle(); | 103 MessageLoop::current()->RunUntilIdle(); |
| 93 } | 104 } |
| 94 | 105 |
| 95 // Calls WatchForAddressChange, and simulates a WatchForAddressChangeInternal | 106 // Calls WatchForAddressChange, and simulates a WatchForAddressChangeInternal |
| 96 // failure. | 107 // failure. |
| 97 void StartWatchingAndFail() { | 108 void StartWatchingAndFail() { |
| 98 EXPECT_FALSE(network_change_notifier_.is_watching()); | 109 EXPECT_FALSE(network_change_notifier_.is_watching()); |
| 99 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); | 110 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); |
| 100 | 111 |
| 101 EXPECT_CALL(test_ip_address_observer_, OnIPAddressChanged()).Times(0); | 112 EXPECT_CALL(test_network_change_observer_, OnNetworkChanged(_)).Times(0); |
| 102 EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal()) | 113 EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal()) |
| 103 // Due to an expected race, it's theoretically possible for more than | 114 // Due to an expected race, it's theoretically possible for more than |
| 104 // one call to occur, though unlikely. | 115 // one call to occur, though unlikely. |
| 105 .Times(AtLeast(1)) | 116 .Times(AtLeast(1)) |
| 106 .WillRepeatedly(Return(false)); | 117 .WillRepeatedly(Return(false)); |
| 107 | 118 |
| 108 network_change_notifier_.WatchForAddressChange(); | 119 network_change_notifier_.WatchForAddressChange(); |
| 109 | 120 |
| 110 EXPECT_FALSE(network_change_notifier_.is_watching()); | 121 EXPECT_FALSE(network_change_notifier_.is_watching()); |
| 111 EXPECT_LT(0, network_change_notifier_.sequential_failures()); | 122 EXPECT_LT(0, network_change_notifier_.sequential_failures()); |
| 112 | 123 |
| 113 // If a task to notify observers of the IP address change event was | 124 // If a task to notify observers of the IP address change event was |
| 114 // incorrectly posted, make sure it gets run. | 125 // incorrectly posted, make sure it gets run. |
| 115 MessageLoop::current()->RunUntilIdle(); | 126 MessageLoop::current()->RunUntilIdle(); |
| 116 } | 127 } |
| 117 | 128 |
| 118 // Simulates a network change event, resulting in a call to OnObjectSignaled. | 129 // Simulates a network change event, resulting in a call to OnObjectSignaled. |
| 119 // The resulting call to WatchForAddressChangeInternal then succeeds. | 130 // The resulting call to WatchForAddressChangeInternal then succeeds. |
| 120 void SignalAndSucceed() { | 131 void SignalAndSucceed() { |
| 121 EXPECT_TRUE(network_change_notifier_.is_watching()); | 132 EXPECT_TRUE(network_change_notifier_.is_watching()); |
| 122 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); | 133 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); |
| 123 | 134 |
| 124 EXPECT_CALL(test_ip_address_observer_, OnIPAddressChanged()).Times(1); | 135 EXPECT_CALL(test_network_change_observer_, OnNetworkChanged(_)).Times(2); |
| 125 EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal()) | 136 EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal()) |
| 126 .Times(1) | 137 .Times(1) |
| 127 .WillOnce(Return(true)); | 138 .WillOnce(Return(true)); |
| 128 | 139 |
| 129 network_change_notifier_.OnObjectSignaled(INVALID_HANDLE_VALUE); | 140 network_change_notifier_.OnObjectSignaled(INVALID_HANDLE_VALUE); |
| 130 | 141 |
| 131 EXPECT_TRUE(network_change_notifier_.is_watching()); | 142 EXPECT_TRUE(network_change_notifier_.is_watching()); |
| 132 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); | 143 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); |
| 133 | 144 |
| 134 // Run the task to notify observers of the IP address change event. | 145 // Run the task to notify observers of the IP address change event. |
| 135 MessageLoop::current()->RunUntilIdle(); | 146 MessageLoop::current()->RunUntilIdle(); |
| 136 } | 147 } |
| 137 | 148 |
| 138 // Simulates a network change event, resulting in a call to OnObjectSignaled. | 149 // Simulates a network change event, resulting in a call to OnObjectSignaled. |
| 139 // The resulting call to WatchForAddressChangeInternal then fails. | 150 // The resulting call to WatchForAddressChangeInternal then fails. |
| 140 void SignalAndFail() { | 151 void SignalAndFail() { |
| 141 EXPECT_TRUE(network_change_notifier_.is_watching()); | 152 EXPECT_TRUE(network_change_notifier_.is_watching()); |
| 142 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); | 153 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); |
| 143 | 154 |
| 144 EXPECT_CALL(test_ip_address_observer_, OnIPAddressChanged()).Times(1); | 155 EXPECT_CALL(test_network_change_observer_, OnNetworkChanged(_)).Times(2); |
| 145 EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal()) | 156 EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal()) |
| 146 // Due to an expected race, it's theoretically possible for more than | 157 // Due to an expected race, it's theoretically possible for more than |
| 147 // one call to occur, though unlikely. | 158 // one call to occur, though unlikely. |
| 148 .Times(AtLeast(1)) | 159 .Times(AtLeast(1)) |
| 149 .WillRepeatedly(Return(false)); | 160 .WillRepeatedly(Return(false)); |
| 150 | 161 |
| 151 network_change_notifier_.OnObjectSignaled(INVALID_HANDLE_VALUE); | 162 network_change_notifier_.OnObjectSignaled(INVALID_HANDLE_VALUE); |
| 152 | 163 |
| 153 EXPECT_FALSE(network_change_notifier_.is_watching()); | 164 EXPECT_FALSE(network_change_notifier_.is_watching()); |
| 154 EXPECT_LT(0, network_change_notifier_.sequential_failures()); | 165 EXPECT_LT(0, network_change_notifier_.sequential_failures()); |
| 155 | 166 |
| 156 // Run the task to notify observers of the IP address change event. | 167 // Run the task to notify observers of the IP address change event. |
| 157 MessageLoop::current()->RunUntilIdle(); | 168 MessageLoop::current()->RunUntilIdle(); |
| 158 } | 169 } |
| 159 | 170 |
| 160 // Runs the message loop until WatchForAddressChange is called again, as a | 171 // Runs the message loop until WatchForAddressChange is called again, as a |
| 161 // result of the already posted task after a WatchForAddressChangeInternal | 172 // result of the already posted task after a WatchForAddressChangeInternal |
| 162 // failure. Simulates a success on the resulting call to | 173 // failure. Simulates a success on the resulting call to |
| 163 // WatchForAddressChangeInternal. | 174 // WatchForAddressChangeInternal. |
| 164 void RetryAndSucceed() { | 175 void RetryAndSucceed() { |
| 165 EXPECT_FALSE(network_change_notifier_.is_watching()); | 176 EXPECT_FALSE(network_change_notifier_.is_watching()); |
| 166 EXPECT_LT(0, network_change_notifier_.sequential_failures()); | 177 EXPECT_LT(0, network_change_notifier_.sequential_failures()); |
| 167 | 178 |
| 168 EXPECT_CALL(test_ip_address_observer_, OnIPAddressChanged()) | 179 EXPECT_CALL(test_network_change_observer_, OnNetworkChanged(_)) |
| 169 .Times(1) | 180 .Times(2) |
| 170 .WillOnce(Invoke(MessageLoop::current(), &MessageLoop::Quit)); | 181 .WillRepeatedly(InvokeWithoutArgs(MessageLoop::current(), |
| 182 &MessageLoop::Quit)); |
| 171 EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal()) | 183 EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal()) |
| 172 .Times(1) | 184 .Times(1) |
| 173 .WillOnce(Return(true)); | 185 .WillOnce(Return(true)); |
| 174 | 186 |
| 175 MessageLoop::current()->Run(); | 187 MessageLoop::current()->Run(); |
| 176 | 188 |
| 177 EXPECT_TRUE(network_change_notifier_.is_watching()); | 189 EXPECT_TRUE(network_change_notifier_.is_watching()); |
| 178 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); | 190 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); |
| 179 } | 191 } |
| 180 | 192 |
| 181 // Runs the message loop until WatchForAddressChange is called again, as a | 193 // Runs the message loop until WatchForAddressChange is called again, as a |
| 182 // result of the already posted task after a WatchForAddressChangeInternal | 194 // result of the already posted task after a WatchForAddressChangeInternal |
| 183 // failure. Simulates a failure on the resulting call to | 195 // failure. Simulates a failure on the resulting call to |
| 184 // WatchForAddressChangeInternal. | 196 // WatchForAddressChangeInternal. |
| 185 void RetryAndFail() { | 197 void RetryAndFail() { |
| 186 EXPECT_FALSE(network_change_notifier_.is_watching()); | 198 EXPECT_FALSE(network_change_notifier_.is_watching()); |
| 187 EXPECT_LT(0, network_change_notifier_.sequential_failures()); | 199 EXPECT_LT(0, network_change_notifier_.sequential_failures()); |
| 188 | 200 |
| 189 int initial_sequential_failures = | 201 int initial_sequential_failures = |
| 190 network_change_notifier_.sequential_failures(); | 202 network_change_notifier_.sequential_failures(); |
| 191 | 203 |
| 192 EXPECT_CALL(test_ip_address_observer_, OnIPAddressChanged()).Times(0); | 204 EXPECT_CALL(test_network_change_observer_, OnNetworkChanged(_)).Times(0); |
| 193 EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal()) | 205 EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal()) |
| 194 // Due to an expected race, it's theoretically possible for more than | 206 // Due to an expected race, it's theoretically possible for more than |
| 195 // one call to occur, though unlikely. | 207 // one call to occur, though unlikely. |
| 196 .Times(AtLeast(1)) | 208 .Times(AtLeast(1)) |
| 197 .WillRepeatedly(Invoke(ExitMessageLoopAndReturnFalse)); | 209 .WillRepeatedly(Invoke(ExitMessageLoopAndReturnFalse)); |
| 198 | 210 |
| 199 MessageLoop::current()->Run(); | 211 MessageLoop::current()->Run(); |
| 200 | 212 |
| 201 EXPECT_FALSE(network_change_notifier_.is_watching()); | 213 EXPECT_FALSE(network_change_notifier_.is_watching()); |
| 202 EXPECT_LT(initial_sequential_failures, | 214 EXPECT_LT(initial_sequential_failures, |
| 203 network_change_notifier_.sequential_failures()); | 215 network_change_notifier_.sequential_failures()); |
| 204 | 216 |
| 205 // If a task to notify observers of the IP address change event was | 217 // If a task to notify observers of the IP address change event was |
| 206 // incorrectly posted, make sure it gets run. | 218 // incorrectly posted, make sure it gets run. |
| 207 MessageLoop::current()->RunUntilIdle(); | 219 MessageLoop::current()->RunUntilIdle(); |
| 208 } | 220 } |
| 209 | 221 |
| 210 private: | 222 private: |
| 211 // Note that the order of declaration here is important. | 223 // Note that the order of declaration here is important. |
| 212 | 224 |
| 213 // Allows creating a new NetworkChangeNotifier. Must be created before | 225 // Allows creating a new NetworkChangeNotifier. Must be created before |
| 214 // |network_change_notifier_| and destroyed after it to avoid DCHECK failures. | 226 // |network_change_notifier_| and destroyed after it to avoid DCHECK failures. |
| 215 NetworkChangeNotifier::DisableForTest disable_for_test_; | 227 NetworkChangeNotifier::DisableForTest disable_for_test_; |
| 216 | 228 |
| 217 StrictMock<TestNetworkChangeNotifierWin> network_change_notifier_; | 229 StrictMock<TestNetworkChangeNotifierWin> network_change_notifier_; |
| 218 | 230 |
| 219 // Must be created after |network_change_notifier_|, so it can add itself as | 231 // Must be created after |network_change_notifier_|, so it can add itself as |
| 220 // an IPAddressObserver. | 232 // a NetworkChangeObserver. |
| 221 StrictMock<TestIPAddressObserver> test_ip_address_observer_; | 233 StrictMock<TestNetworkChangeObserver> test_network_change_observer_; |
| 222 }; | 234 }; |
| 223 | 235 |
| 224 TEST_F(NetworkChangeNotifierWinTest, NetChangeWinBasic) { | 236 TEST_F(NetworkChangeNotifierWinTest, NetChangeWinBasic) { |
| 225 StartWatchingAndSucceed(); | 237 StartWatchingAndSucceed(); |
| 226 } | 238 } |
| 227 | 239 |
| 228 TEST_F(NetworkChangeNotifierWinTest, NetChangeWinFailStart) { | 240 TEST_F(NetworkChangeNotifierWinTest, NetChangeWinFailStart) { |
| 229 StartWatchingAndFail(); | 241 StartWatchingAndFail(); |
| 230 } | 242 } |
| 231 | 243 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 252 } | 264 } |
| 253 | 265 |
| 254 TEST_F(NetworkChangeNotifierWinTest, NetChangeWinFailSignalTwice) { | 266 TEST_F(NetworkChangeNotifierWinTest, NetChangeWinFailSignalTwice) { |
| 255 StartWatchingAndSucceed(); | 267 StartWatchingAndSucceed(); |
| 256 SignalAndFail(); | 268 SignalAndFail(); |
| 257 RetryAndFail(); | 269 RetryAndFail(); |
| 258 RetryAndSucceed(); | 270 RetryAndSucceed(); |
| 259 } | 271 } |
| 260 | 272 |
| 261 } // namespace net | 273 } // namespace net |
| OLD | NEW |