| 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_observer_proxy.h" | |
| 6 | |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/ref_counted.h" | |
| 10 #include "chrome/common/net/fake_network_change_notifier_thread.h" | |
| 11 #include "chrome/common/net/mock_network_change_observer.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace chrome_common_net { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // Version of NetworkChangeObserverProxy that records on what thread | |
| 19 // it was deleted. | |
| 20 class DeleteCheckingNetworkChangeObserverProxy | |
| 21 : public NetworkChangeObserverProxy { | |
| 22 public: | |
| 23 // *deleting_message_loop_ must be NULL. It is set to a non-NULL | |
| 24 // *value when this object is deleted. | |
| 25 DeleteCheckingNetworkChangeObserverProxy( | |
| 26 NetworkChangeNotifierThread* source_thread, | |
| 27 MessageLoop* target_message_loop, | |
| 28 MessageLoop** deleting_message_loop) | |
| 29 : NetworkChangeObserverProxy(source_thread, target_message_loop), | |
| 30 deleting_message_loop_(deleting_message_loop) { | |
| 31 CHECK(deleting_message_loop_); | |
| 32 EXPECT_TRUE(*deleting_message_loop_ == NULL); | |
| 33 } | |
| 34 | |
| 35 private: | |
| 36 virtual ~DeleteCheckingNetworkChangeObserverProxy() { | |
| 37 *deleting_message_loop_ = MessageLoop::current(); | |
| 38 } | |
| 39 | |
| 40 MessageLoop** deleting_message_loop_; | |
| 41 | |
| 42 DISALLOW_COPY_AND_ASSIGN(DeleteCheckingNetworkChangeObserverProxy); | |
| 43 }; | |
| 44 | |
| 45 class NetworkChangeObserverProxyTest : public testing::Test { | |
| 46 protected: | |
| 47 NetworkChangeObserverProxyTest() : proxy_deleting_message_loop_(NULL) {} | |
| 48 | |
| 49 virtual ~NetworkChangeObserverProxyTest() {} | |
| 50 | |
| 51 virtual void SetUp() { | |
| 52 source_thread_.Start(); | |
| 53 proxy_deleting_message_loop_ = NULL; | |
| 54 proxy_ = new DeleteCheckingNetworkChangeObserverProxy( | |
| 55 &source_thread_, &target_message_loop_, | |
| 56 &proxy_deleting_message_loop_); | |
| 57 } | |
| 58 | |
| 59 // On TearDown, |proxy_| must be released and both source and target | |
| 60 // must be pumped. | |
| 61 virtual void TearDown() { | |
| 62 EXPECT_TRUE(proxy_ == NULL); | |
| 63 EXPECT_TRUE(proxy_deleting_message_loop_ != NULL); | |
| 64 source_thread_.Stop(); | |
| 65 } | |
| 66 | |
| 67 // Pump any events posted on the source thread. | |
| 68 void PumpSource() { | |
| 69 source_thread_.Pump(); | |
| 70 } | |
| 71 | |
| 72 // Pump any events posted on the target thread (which is just the | |
| 73 // main test thread). | |
| 74 void PumpTarget() { | |
| 75 target_message_loop_.RunAllPending(); | |
| 76 } | |
| 77 | |
| 78 // Trigger an "IP address changed" event on the source network | |
| 79 // change notifier on the source thread. | |
| 80 void NotifyIPAddressChange() { | |
| 81 source_thread_.NotifyIPAddressChange(); | |
| 82 } | |
| 83 | |
| 84 FakeNetworkChangeNotifierThread source_thread_; | |
| 85 | |
| 86 MessageLoop target_message_loop_; | |
| 87 MockNetworkChangeObserver target_observer_; | |
| 88 | |
| 89 MessageLoop* proxy_deleting_message_loop_; | |
| 90 scoped_refptr<NetworkChangeObserverProxy> proxy_; | |
| 91 | |
| 92 private: | |
| 93 DISALLOW_COPY_AND_ASSIGN(NetworkChangeObserverProxyTest); | |
| 94 }; | |
| 95 | |
| 96 // Make sure nothing blows up if we don't attach the proxy. | |
| 97 TEST_F(NetworkChangeObserverProxyTest, DoNothing) { | |
| 98 EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(0); | |
| 99 | |
| 100 proxy_ = NULL; | |
| 101 // No need to pump. | |
| 102 | |
| 103 EXPECT_EQ(proxy_deleting_message_loop_, &target_message_loop_); | |
| 104 } | |
| 105 | |
| 106 TEST_F(NetworkChangeObserverProxyTest, AttachDetach) { | |
| 107 EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(0); | |
| 108 | |
| 109 proxy_->Attach(&target_observer_); | |
| 110 proxy_->Detach(); | |
| 111 proxy_ = NULL; | |
| 112 PumpSource(); | |
| 113 | |
| 114 EXPECT_EQ(proxy_deleting_message_loop_, source_thread_.GetMessageLoop()); | |
| 115 } | |
| 116 | |
| 117 TEST_F(NetworkChangeObserverProxyTest, AttachDetachReleaseAfterPump) { | |
| 118 EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(0); | |
| 119 | |
| 120 proxy_->Attach(&target_observer_); | |
| 121 proxy_->Detach(); | |
| 122 PumpSource(); | |
| 123 proxy_ = NULL; | |
| 124 | |
| 125 EXPECT_EQ(proxy_deleting_message_loop_, &target_message_loop_); | |
| 126 } | |
| 127 | |
| 128 TEST_F(NetworkChangeObserverProxyTest, Basic) { | |
| 129 EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(1); | |
| 130 | |
| 131 proxy_->Attach(&target_observer_); | |
| 132 NotifyIPAddressChange(); | |
| 133 PumpSource(); | |
| 134 PumpTarget(); | |
| 135 proxy_->Detach(); | |
| 136 proxy_ = NULL; | |
| 137 PumpSource(); | |
| 138 | |
| 139 EXPECT_EQ(proxy_deleting_message_loop_, source_thread_.GetMessageLoop()); | |
| 140 } | |
| 141 | |
| 142 TEST_F(NetworkChangeObserverProxyTest, Multiple) { | |
| 143 const int kTimes = 5; | |
| 144 EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(kTimes); | |
| 145 | |
| 146 proxy_->Attach(&target_observer_); | |
| 147 for (int i = 0; i < kTimes; ++i) { | |
| 148 NotifyIPAddressChange(); | |
| 149 } | |
| 150 PumpSource(); | |
| 151 PumpTarget(); | |
| 152 proxy_->Detach(); | |
| 153 proxy_ = NULL; | |
| 154 PumpSource(); | |
| 155 | |
| 156 EXPECT_EQ(proxy_deleting_message_loop_, source_thread_.GetMessageLoop()); | |
| 157 } | |
| 158 | |
| 159 TEST_F(NetworkChangeObserverProxyTest, MultipleAttachDetach) { | |
| 160 const int kTimes = 5; | |
| 161 EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(kTimes); | |
| 162 | |
| 163 for (int i = 0; i < kTimes; ++i) { | |
| 164 proxy_->Attach(&target_observer_); | |
| 165 NotifyIPAddressChange(); | |
| 166 PumpSource(); | |
| 167 PumpTarget(); | |
| 168 proxy_->Detach(); | |
| 169 } | |
| 170 proxy_ = NULL; | |
| 171 PumpSource(); | |
| 172 | |
| 173 EXPECT_EQ(proxy_deleting_message_loop_, source_thread_.GetMessageLoop()); | |
| 174 } | |
| 175 | |
| 176 TEST_F(NetworkChangeObserverProxyTest, IgnoresEventPostedAfterDetach) { | |
| 177 EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(0); | |
| 178 | |
| 179 proxy_->Attach(&target_observer_); | |
| 180 NotifyIPAddressChange(); | |
| 181 proxy_->Detach(); | |
| 182 proxy_ = NULL; | |
| 183 PumpSource(); | |
| 184 PumpTarget(); | |
| 185 | |
| 186 EXPECT_EQ(proxy_deleting_message_loop_, &target_message_loop_); | |
| 187 } | |
| 188 | |
| 189 TEST_F(NetworkChangeObserverProxyTest, IgnoresEventPostedBeforeDetach) { | |
| 190 EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(0); | |
| 191 | |
| 192 proxy_->Attach(&target_observer_); | |
| 193 NotifyIPAddressChange(); | |
| 194 PumpSource(); | |
| 195 proxy_->Detach(); | |
| 196 proxy_ = NULL; | |
| 197 PumpTarget(); | |
| 198 PumpSource(); | |
| 199 | |
| 200 EXPECT_EQ(proxy_deleting_message_loop_, source_thread_.GetMessageLoop()); | |
| 201 } | |
| 202 | |
| 203 TEST_F(NetworkChangeObserverProxyTest, IgnoresEventAfterDetach) { | |
| 204 EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(0); | |
| 205 | |
| 206 proxy_->Attach(&target_observer_); | |
| 207 proxy_->Detach(); | |
| 208 proxy_ = NULL; | |
| 209 NotifyIPAddressChange(); | |
| 210 PumpSource(); | |
| 211 | |
| 212 EXPECT_EQ(proxy_deleting_message_loop_, source_thread_.GetMessageLoop()); | |
| 213 } | |
| 214 | |
| 215 TEST_F(NetworkChangeObserverProxyTest, IgnoresEventBeforeAttach) { | |
| 216 EXPECT_CALL(target_observer_, OnIPAddressChanged()).Times(0); | |
| 217 | |
| 218 NotifyIPAddressChange(); | |
| 219 PumpSource(); | |
| 220 proxy_->Attach(&target_observer_); | |
| 221 proxy_->Detach(); | |
| 222 proxy_ = NULL; | |
| 223 PumpSource(); | |
| 224 | |
| 225 EXPECT_EQ(proxy_deleting_message_loop_, source_thread_.GetMessageLoop()); | |
| 226 } | |
| 227 | |
| 228 } // namespace | |
| 229 | |
| 230 } // namespace chrome_common_net | |
| OLD | NEW |