| 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/macros.h" | 5 #include "base/macros.h" |
| 6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
| 7 #include "base/run_loop.h" |
| 7 #include "net/base/network_change_notifier.h" | 8 #include "net/base/network_change_notifier.h" |
| 8 #include "net/base/network_change_notifier_factory.h" | 9 #include "net/base/network_change_notifier_factory.h" |
| 9 #include "net/base/network_change_notifier_win.h" | 10 #include "net/base/network_change_notifier_win.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 13 using ::testing::AtLeast; | 14 using ::testing::AtLeast; |
| 14 using ::testing::Invoke; | 15 using ::testing::Invoke; |
| 15 using ::testing::Return; | 16 using ::testing::Return; |
| 16 using ::testing::StrictMock; | 17 using ::testing::StrictMock; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 .Times(1) | 83 .Times(1) |
| 83 .WillOnce(Return(true)); | 84 .WillOnce(Return(true)); |
| 84 | 85 |
| 85 network_change_notifier_.WatchForAddressChange(); | 86 network_change_notifier_.WatchForAddressChange(); |
| 86 | 87 |
| 87 EXPECT_TRUE(network_change_notifier_.is_watching()); | 88 EXPECT_TRUE(network_change_notifier_.is_watching()); |
| 88 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); | 89 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); |
| 89 | 90 |
| 90 // If a task to notify observers of the IP address change event was | 91 // 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. | 92 // incorrectly posted, make sure it gets run to trigger a failure. |
| 92 base::MessageLoop::current()->RunUntilIdle(); | 93 base::RunLoop().RunUntilIdle(); |
| 93 } | 94 } |
| 94 | 95 |
| 95 // Calls WatchForAddressChange, and simulates a WatchForAddressChangeInternal | 96 // Calls WatchForAddressChange, and simulates a WatchForAddressChangeInternal |
| 96 // failure. | 97 // failure. |
| 97 void StartWatchingAndFail() { | 98 void StartWatchingAndFail() { |
| 98 EXPECT_FALSE(network_change_notifier_.is_watching()); | 99 EXPECT_FALSE(network_change_notifier_.is_watching()); |
| 99 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); | 100 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); |
| 100 | 101 |
| 101 EXPECT_CALL(test_ip_address_observer_, OnIPAddressChanged()).Times(0); | 102 EXPECT_CALL(test_ip_address_observer_, OnIPAddressChanged()).Times(0); |
| 102 EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal()) | 103 EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal()) |
| 103 // Due to an expected race, it's theoretically possible for more than | 104 // Due to an expected race, it's theoretically possible for more than |
| 104 // one call to occur, though unlikely. | 105 // one call to occur, though unlikely. |
| 105 .Times(AtLeast(1)) | 106 .Times(AtLeast(1)) |
| 106 .WillRepeatedly(Return(false)); | 107 .WillRepeatedly(Return(false)); |
| 107 | 108 |
| 108 network_change_notifier_.WatchForAddressChange(); | 109 network_change_notifier_.WatchForAddressChange(); |
| 109 | 110 |
| 110 EXPECT_FALSE(network_change_notifier_.is_watching()); | 111 EXPECT_FALSE(network_change_notifier_.is_watching()); |
| 111 EXPECT_LT(0, network_change_notifier_.sequential_failures()); | 112 EXPECT_LT(0, network_change_notifier_.sequential_failures()); |
| 112 | 113 |
| 113 // If a task to notify observers of the IP address change event was | 114 // If a task to notify observers of the IP address change event was |
| 114 // incorrectly posted, make sure it gets run. | 115 // incorrectly posted, make sure it gets run. |
| 115 base::MessageLoop::current()->RunUntilIdle(); | 116 base::RunLoop().RunUntilIdle(); |
| 116 } | 117 } |
| 117 | 118 |
| 118 // Simulates a network change event, resulting in a call to OnObjectSignaled. | 119 // Simulates a network change event, resulting in a call to OnObjectSignaled. |
| 119 // The resulting call to WatchForAddressChangeInternal then succeeds. | 120 // The resulting call to WatchForAddressChangeInternal then succeeds. |
| 120 void SignalAndSucceed() { | 121 void SignalAndSucceed() { |
| 121 EXPECT_TRUE(network_change_notifier_.is_watching()); | 122 EXPECT_TRUE(network_change_notifier_.is_watching()); |
| 122 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); | 123 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); |
| 123 | 124 |
| 124 EXPECT_CALL(test_ip_address_observer_, OnIPAddressChanged()).Times(1); | 125 EXPECT_CALL(test_ip_address_observer_, OnIPAddressChanged()).Times(1); |
| 125 EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal()) | 126 EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal()) |
| 126 .Times(1) | 127 .Times(1) |
| 127 .WillOnce(Return(true)); | 128 .WillOnce(Return(true)); |
| 128 | 129 |
| 129 network_change_notifier_.OnObjectSignaled(INVALID_HANDLE_VALUE); | 130 network_change_notifier_.OnObjectSignaled(INVALID_HANDLE_VALUE); |
| 130 | 131 |
| 131 EXPECT_TRUE(network_change_notifier_.is_watching()); | 132 EXPECT_TRUE(network_change_notifier_.is_watching()); |
| 132 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); | 133 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); |
| 133 | 134 |
| 134 // Run the task to notify observers of the IP address change event. | 135 // Run the task to notify observers of the IP address change event. |
| 135 base::MessageLoop::current()->RunUntilIdle(); | 136 base::RunLoop().RunUntilIdle(); |
| 136 } | 137 } |
| 137 | 138 |
| 138 // Simulates a network change event, resulting in a call to OnObjectSignaled. | 139 // Simulates a network change event, resulting in a call to OnObjectSignaled. |
| 139 // The resulting call to WatchForAddressChangeInternal then fails. | 140 // The resulting call to WatchForAddressChangeInternal then fails. |
| 140 void SignalAndFail() { | 141 void SignalAndFail() { |
| 141 EXPECT_TRUE(network_change_notifier_.is_watching()); | 142 EXPECT_TRUE(network_change_notifier_.is_watching()); |
| 142 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); | 143 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); |
| 143 | 144 |
| 144 EXPECT_CALL(test_ip_address_observer_, OnIPAddressChanged()).Times(1); | 145 EXPECT_CALL(test_ip_address_observer_, OnIPAddressChanged()).Times(1); |
| 145 EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal()) | 146 EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal()) |
| 146 // Due to an expected race, it's theoretically possible for more than | 147 // Due to an expected race, it's theoretically possible for more than |
| 147 // one call to occur, though unlikely. | 148 // one call to occur, though unlikely. |
| 148 .Times(AtLeast(1)) | 149 .Times(AtLeast(1)) |
| 149 .WillRepeatedly(Return(false)); | 150 .WillRepeatedly(Return(false)); |
| 150 | 151 |
| 151 network_change_notifier_.OnObjectSignaled(INVALID_HANDLE_VALUE); | 152 network_change_notifier_.OnObjectSignaled(INVALID_HANDLE_VALUE); |
| 152 | 153 |
| 153 EXPECT_FALSE(network_change_notifier_.is_watching()); | 154 EXPECT_FALSE(network_change_notifier_.is_watching()); |
| 154 EXPECT_LT(0, network_change_notifier_.sequential_failures()); | 155 EXPECT_LT(0, network_change_notifier_.sequential_failures()); |
| 155 | 156 |
| 156 // Run the task to notify observers of the IP address change event. | 157 // Run the task to notify observers of the IP address change event. |
| 157 base::MessageLoop::current()->RunUntilIdle(); | 158 base::RunLoop().RunUntilIdle(); |
| 158 } | 159 } |
| 159 | 160 |
| 160 // Runs the message loop until WatchForAddressChange is called again, as a | 161 // Runs the message loop until WatchForAddressChange is called again, as a |
| 161 // result of the already posted task after a WatchForAddressChangeInternal | 162 // result of the already posted task after a WatchForAddressChangeInternal |
| 162 // failure. Simulates a success on the resulting call to | 163 // failure. Simulates a success on the resulting call to |
| 163 // WatchForAddressChangeInternal. | 164 // WatchForAddressChangeInternal. |
| 164 void RetryAndSucceed() { | 165 void RetryAndSucceed() { |
| 165 EXPECT_FALSE(network_change_notifier_.is_watching()); | 166 EXPECT_FALSE(network_change_notifier_.is_watching()); |
| 166 EXPECT_LT(0, network_change_notifier_.sequential_failures()); | 167 EXPECT_LT(0, network_change_notifier_.sequential_failures()); |
| 167 | 168 |
| 168 EXPECT_CALL(test_ip_address_observer_, OnIPAddressChanged()) | 169 EXPECT_CALL(test_ip_address_observer_, OnIPAddressChanged()) |
| 169 .Times(1) | 170 .Times(1) |
| 170 .WillOnce(Invoke(base::MessageLoop::current(), | 171 .WillOnce(Invoke(base::MessageLoop::current(), |
| 171 &base::MessageLoop::QuitWhenIdle)); | 172 &base::MessageLoop::QuitWhenIdle)); |
| 172 EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal()) | 173 EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal()) |
| 173 .Times(1).WillOnce(Return(true)); | 174 .Times(1).WillOnce(Return(true)); |
| 174 | 175 |
| 175 base::MessageLoop::current()->Run(); | 176 base::RunLoop().Run(); |
| 176 | 177 |
| 177 EXPECT_TRUE(network_change_notifier_.is_watching()); | 178 EXPECT_TRUE(network_change_notifier_.is_watching()); |
| 178 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); | 179 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); |
| 179 } | 180 } |
| 180 | 181 |
| 181 // Runs the message loop until WatchForAddressChange is called again, as a | 182 // Runs the message loop until WatchForAddressChange is called again, as a |
| 182 // result of the already posted task after a WatchForAddressChangeInternal | 183 // result of the already posted task after a WatchForAddressChangeInternal |
| 183 // failure. Simulates a failure on the resulting call to | 184 // failure. Simulates a failure on the resulting call to |
| 184 // WatchForAddressChangeInternal. | 185 // WatchForAddressChangeInternal. |
| 185 void RetryAndFail() { | 186 void RetryAndFail() { |
| 186 EXPECT_FALSE(network_change_notifier_.is_watching()); | 187 EXPECT_FALSE(network_change_notifier_.is_watching()); |
| 187 EXPECT_LT(0, network_change_notifier_.sequential_failures()); | 188 EXPECT_LT(0, network_change_notifier_.sequential_failures()); |
| 188 | 189 |
| 189 int initial_sequential_failures = | 190 int initial_sequential_failures = |
| 190 network_change_notifier_.sequential_failures(); | 191 network_change_notifier_.sequential_failures(); |
| 191 | 192 |
| 192 EXPECT_CALL(test_ip_address_observer_, OnIPAddressChanged()).Times(0); | 193 EXPECT_CALL(test_ip_address_observer_, OnIPAddressChanged()).Times(0); |
| 193 EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal()) | 194 EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal()) |
| 194 // Due to an expected race, it's theoretically possible for more than | 195 // Due to an expected race, it's theoretically possible for more than |
| 195 // one call to occur, though unlikely. | 196 // one call to occur, though unlikely. |
| 196 .Times(AtLeast(1)) | 197 .Times(AtLeast(1)) |
| 197 .WillRepeatedly(Invoke(ExitMessageLoopAndReturnFalse)); | 198 .WillRepeatedly(Invoke(ExitMessageLoopAndReturnFalse)); |
| 198 | 199 |
| 199 base::MessageLoop::current()->Run(); | 200 base::RunLoop().Run(); |
| 200 | 201 |
| 201 EXPECT_FALSE(network_change_notifier_.is_watching()); | 202 EXPECT_FALSE(network_change_notifier_.is_watching()); |
| 202 EXPECT_LT(initial_sequential_failures, | 203 EXPECT_LT(initial_sequential_failures, |
| 203 network_change_notifier_.sequential_failures()); | 204 network_change_notifier_.sequential_failures()); |
| 204 | 205 |
| 205 // If a task to notify observers of the IP address change event was | 206 // If a task to notify observers of the IP address change event was |
| 206 // incorrectly posted, make sure it gets run. | 207 // incorrectly posted, make sure it gets run. |
| 207 base::MessageLoop::current()->RunUntilIdle(); | 208 base::RunLoop().RunUntilIdle(); |
| 208 } | 209 } |
| 209 | 210 |
| 210 private: | 211 private: |
| 211 // Note that the order of declaration here is important. | 212 // Note that the order of declaration here is important. |
| 212 | 213 |
| 213 // Allows creating a new NetworkChangeNotifier. Must be created before | 214 // Allows creating a new NetworkChangeNotifier. Must be created before |
| 214 // |network_change_notifier_| and destroyed after it to avoid DCHECK failures. | 215 // |network_change_notifier_| and destroyed after it to avoid DCHECK failures. |
| 215 NetworkChangeNotifier::DisableForTest disable_for_test_; | 216 NetworkChangeNotifier::DisableForTest disable_for_test_; |
| 216 | 217 |
| 217 StrictMock<TestNetworkChangeNotifierWin> network_change_notifier_; | 218 StrictMock<TestNetworkChangeNotifierWin> network_change_notifier_; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 } | 253 } |
| 253 | 254 |
| 254 TEST_F(NetworkChangeNotifierWinTest, NetChangeWinFailSignalTwice) { | 255 TEST_F(NetworkChangeNotifierWinTest, NetChangeWinFailSignalTwice) { |
| 255 StartWatchingAndSucceed(); | 256 StartWatchingAndSucceed(); |
| 256 SignalAndFail(); | 257 SignalAndFail(); |
| 257 RetryAndFail(); | 258 RetryAndFail(); |
| 258 RetryAndSucceed(); | 259 RetryAndSucceed(); |
| 259 } | 260 } |
| 260 | 261 |
| 261 } // namespace net | 262 } // namespace net |
| OLD | NEW |