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 "base/run_loop.h" |
8 #include "net/base/network_change_notifier.h" | 8 #include "net/base/network_change_notifier.h" |
9 #include "net/base/network_change_notifier_factory.h" | 9 #include "net/base/network_change_notifier_factory.h" |
10 #include "net/base/network_change_notifier_win.h" | 10 #include "net/base/network_change_notifier_win.h" |
11 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 using ::testing::AtLeast; | 14 using ::testing::AtLeast; |
15 using ::testing::Invoke; | 15 using ::testing::Invoke; |
16 using ::testing::Return; | 16 using ::testing::Return; |
17 using ::testing::StrictMock; | 17 using ::testing::StrictMock; |
18 | 18 |
19 namespace net { | 19 namespace net { |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 // Subclass of NetworkChangeNotifierWin that overrides functions so that no | 23 NetworkChangeNotifier::ConnectionType TestRecomputeCurrentConnectionType() { |
24 // Windows API networking functions are ever called. | 24 return NetworkChangeNotifier::CONNECTION_UNKNOWN; |
25 class TestNetworkChangeNotifierWin : public NetworkChangeNotifierWin { | 25 } |
26 public: | |
27 TestNetworkChangeNotifierWin() {} | |
28 | |
29 ~TestNetworkChangeNotifierWin() override { | |
30 // This is needed so we don't try to stop watching for IP address changes, | |
31 // as we never actually started. | |
32 set_is_watching(false); | |
33 } | |
34 | |
35 // From NetworkChangeNotifierWin. | |
36 NetworkChangeNotifier::ConnectionType RecomputeCurrentConnectionType() | |
37 const override { | |
38 return NetworkChangeNotifier::CONNECTION_UNKNOWN; | |
39 } | |
40 | |
41 // From NetworkChangeNotifierWin. | |
42 MOCK_METHOD0(WatchForAddressChangeInternal, bool()); | |
43 | |
44 private: | |
45 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifierWin); | |
46 }; | |
47 | 26 |
48 class TestIPAddressObserver : public NetworkChangeNotifier::IPAddressObserver { | 27 class TestIPAddressObserver : public NetworkChangeNotifier::IPAddressObserver { |
49 public: | 28 public: |
50 TestIPAddressObserver() { | 29 TestIPAddressObserver() { |
51 NetworkChangeNotifier::AddIPAddressObserver(this); | 30 NetworkChangeNotifier::AddIPAddressObserver(this); |
52 } | 31 } |
53 | 32 |
54 ~TestIPAddressObserver() { | 33 ~TestIPAddressObserver() { |
55 NetworkChangeNotifier::RemoveIPAddressObserver(this); | 34 NetworkChangeNotifier::RemoveIPAddressObserver(this); |
56 } | 35 } |
57 | 36 |
58 MOCK_METHOD0(OnIPAddressChanged, void()); | 37 MOCK_METHOD0(OnIPAddressChanged, void()); |
59 | 38 |
60 private: | 39 private: |
61 DISALLOW_COPY_AND_ASSIGN(TestIPAddressObserver); | 40 DISALLOW_COPY_AND_ASSIGN(TestIPAddressObserver); |
62 }; | 41 }; |
63 | 42 |
64 bool ExitMessageLoopAndReturnFalse() { | 43 bool ExitMessageLoopAndReturnFalse() { |
65 base::MessageLoop::current()->QuitWhenIdle(); | 44 base::MessageLoop::current()->QuitWhenIdle(); |
66 return false; | 45 return false; |
67 } | 46 } |
68 | 47 |
69 } // namespace | 48 } // namespace |
70 | 49 |
50 // Subclass of NetworkChangeNotifierWin that overrides functions so that no | |
51 // Windows API networking functions are ever called. | |
52 class TestNetworkChangeNotifierWin : public NetworkChangeNotifierWin { | |
53 public: | |
54 TestNetworkChangeNotifierWin() { | |
55 // Use a mocked connection type callback. | |
56 calculate_connection_type_ = | |
57 base::BindRepeating(&TestRecomputeCurrentConnectionType); | |
58 | |
59 // Use this thread for dns tasks instead of a separate thread. | |
60 dns_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | |
pauljensen
2017/05/23 17:57:40
why do we care what thread this is run on in tests
| |
61 } | |
62 | |
63 ~TestNetworkChangeNotifierWin() override { | |
64 // This is needed so we don't try to stop watching for IP address changes, | |
65 // as we never actually started. | |
66 set_is_watching(false); | |
67 } | |
68 | |
69 // From NetworkChangeNotifierWin. | |
70 MOCK_METHOD0(WatchForAddressChangeInternal, bool()); | |
71 | |
72 private: | |
73 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifierWin); | |
74 }; | |
75 | |
71 class NetworkChangeNotifierWinTest : public testing::Test { | 76 class NetworkChangeNotifierWinTest : public testing::Test { |
72 public: | 77 public: |
73 // Calls WatchForAddressChange, and simulates a WatchForAddressChangeInternal | 78 // Calls WatchForAddressChange, and simulates a WatchForAddressChangeInternal |
74 // success. Expects that |network_change_notifier_| has just been created, so | 79 // success. Expects that |network_change_notifier_| has just been created, so |
75 // it's not watching anything yet, and there have been no previous | 80 // it's not watching anything yet, and there have been no previous |
76 // WatchForAddressChangeInternal failures. | 81 // WatchForAddressChangeInternal failures. |
77 void StartWatchingAndSucceed() { | 82 void StartWatchingAndSucceed() { |
78 EXPECT_FALSE(network_change_notifier_.is_watching()); | 83 EXPECT_FALSE(network_change_notifier_.is_watching()); |
79 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); | 84 EXPECT_EQ(0, network_change_notifier_.sequential_failures()); |
80 | 85 |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
253 } | 258 } |
254 | 259 |
255 TEST_F(NetworkChangeNotifierWinTest, NetChangeWinFailSignalTwice) { | 260 TEST_F(NetworkChangeNotifierWinTest, NetChangeWinFailSignalTwice) { |
256 StartWatchingAndSucceed(); | 261 StartWatchingAndSucceed(); |
257 SignalAndFail(); | 262 SignalAndFail(); |
258 RetryAndFail(); | 263 RetryAndFail(); |
259 RetryAndSucceed(); | 264 RetryAndSucceed(); |
260 } | 265 } |
261 | 266 |
262 } // namespace net | 267 } // namespace net |
OLD | NEW |