OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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 | |
5 #include "chrome/common/net/network_change_notifier_proxy.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/logging.h" | |
9 #include "base/ref_counted.h" | |
10 #include "base/scoped_ptr.h" | |
11 #include "chrome/common/net/fake_network_change_notifier_thread.h" | |
12 #include "chrome/common/net/mock_network_change_observer.h" | |
13 #include "net/base/network_change_notifier.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 namespace chrome_common_net { | |
17 | |
18 namespace { | |
19 | |
20 class NetworkChangeNotifierProxyTest : public testing::Test { | |
21 protected: | |
22 NetworkChangeNotifierProxyTest() {} | |
23 | |
24 virtual ~NetworkChangeNotifierProxyTest() {} | |
25 | |
26 virtual void SetUp() { | |
27 source_thread_.Start(); | |
28 notifier_proxy_.reset(new NetworkChangeNotifierProxy(&source_thread_)); | |
29 } | |
30 | |
31 virtual void TearDown() { | |
32 // Posts a task to the source thread. | |
33 notifier_proxy_.reset(); | |
34 source_thread_.Stop(); | |
35 } | |
36 | |
37 // Trigger an "IP address changed" event on the source network | |
38 // change notifier on the source thread and propagate any generated | |
39 // notifications to the target thread. | |
40 void NotifyIPAddressChange() { | |
41 source_thread_.NotifyIPAddressChange(); | |
42 source_thread_.Pump(); | |
43 target_message_loop_.RunAllPending(); | |
44 } | |
45 | |
46 FakeNetworkChangeNotifierThread source_thread_; | |
47 | |
48 MessageLoop target_message_loop_; | |
49 MockNetworkChangeObserver target_observer_; | |
50 | |
51 scoped_ptr<net::NetworkChangeNotifier> notifier_proxy_; | |
52 | |
53 private: | |
54 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierProxyTest); | |
55 }; | |
56 | |
57 TEST_F(NetworkChangeNotifierProxyTest, Basic) { | |
58 EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(1); | |
59 | |
60 notifier_proxy_->AddObserver(&target_observer_); | |
61 NotifyIPAddressChange(); | |
62 notifier_proxy_->RemoveObserver(&target_observer_); | |
63 } | |
64 | |
65 TEST_F(NetworkChangeNotifierProxyTest, IgnoresEventAfterRemoveObserver) { | |
66 EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(0); | |
67 | |
68 notifier_proxy_->AddObserver(&target_observer_); | |
69 notifier_proxy_->RemoveObserver(&target_observer_); | |
70 NotifyIPAddressChange(); | |
71 } | |
72 | |
73 TEST_F(NetworkChangeNotifierProxyTest, IgnoresEventBeforeRemoveObserver) { | |
74 EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(0); | |
75 | |
76 NotifyIPAddressChange(); | |
77 notifier_proxy_->AddObserver(&target_observer_); | |
78 notifier_proxy_->RemoveObserver(&target_observer_); | |
79 } | |
80 | |
81 TEST_F(NetworkChangeNotifierProxyTest, Multiple) { | |
82 EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(0); | |
83 | |
84 const int kNumObservers = 5; | |
85 MockNetworkChangeObserver extra_observers[kNumObservers]; | |
86 for (int i = 0; i < kNumObservers; ++i) { | |
87 EXPECT_CALL(extra_observers[i], OnIPAddressChanged()).Times(1); | |
88 } | |
89 | |
90 for (int i = 0; i < kNumObservers; ++i) { | |
91 notifier_proxy_->AddObserver(&extra_observers[i]); | |
92 } | |
93 NotifyIPAddressChange(); | |
94 for (int i = 0; i < kNumObservers; ++i) { | |
95 notifier_proxy_->RemoveObserver(&extra_observers[i]); | |
96 } | |
97 } | |
98 | |
99 } // namespace | |
100 | |
101 } // namespace chrome_common_net | |
OLD | NEW |