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 // See network_change_notifier_android.h for design explanations. | 5 // See network_change_notifier_android.h for design explanations. |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "net/android/network_change_notifier_android.h" | 12 #include "net/android/network_change_notifier_android.h" |
13 #include "net/android/network_change_notifier_delegate_android.h" | 13 #include "net/android/network_change_notifier_delegate_android.h" |
14 #include "net/base/network_change_notifier.h" | 14 #include "net/base/network_change_notifier.h" |
| 15 #include "net/dns/dns_config_service.h" |
| 16 #include "net/dns/dns_protocol.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
16 | 18 |
17 namespace net { | 19 namespace net { |
18 | 20 |
19 namespace { | 21 namespace { |
20 | 22 |
21 class NetworkChangeNotifierDelegateAndroidObserver | 23 class NetworkChangeNotifierDelegateAndroidObserver |
22 : public NetworkChangeNotifierDelegateAndroid::Observer { | 24 : public NetworkChangeNotifierDelegateAndroid::Observer { |
23 public: | 25 public: |
24 NetworkChangeNotifierDelegateAndroidObserver() | 26 NetworkChangeNotifierDelegateAndroidObserver() |
(...skipping 28 matching lines...) Expand all Loading... |
53 } | 55 } |
54 | 56 |
55 int notifications_count() const { | 57 int notifications_count() const { |
56 return notifications_count_; | 58 return notifications_count_; |
57 } | 59 } |
58 | 60 |
59 private: | 61 private: |
60 int notifications_count_; | 62 int notifications_count_; |
61 }; | 63 }; |
62 | 64 |
| 65 class DNSChangeObserver : public NetworkChangeNotifier::DNSObserver { |
| 66 public: |
| 67 DNSChangeObserver() |
| 68 : change_notifications_count_(0), initial_notifications_count_(0) {} |
| 69 |
| 70 // NetworkChangeNotifier::DNSObserver: |
| 71 void OnDNSChanged() override { change_notifications_count_++; } |
| 72 |
| 73 void OnInitialDNSConfigRead() override { |
| 74 initial_notifications_count_++; |
| 75 base::MessageLoop::current()->Quit(); |
| 76 } |
| 77 |
| 78 int change_notifications_count() const { return change_notifications_count_; } |
| 79 |
| 80 int initial_notifications_count() const { |
| 81 return initial_notifications_count_; |
| 82 } |
| 83 |
| 84 private: |
| 85 int change_notifications_count_; |
| 86 int initial_notifications_count_; |
| 87 }; |
| 88 |
63 } // namespace | 89 } // namespace |
64 | 90 |
65 class BaseNetworkChangeNotifierAndroidTest : public testing::Test { | 91 class BaseNetworkChangeNotifierAndroidTest : public testing::Test { |
66 protected: | 92 protected: |
67 typedef NetworkChangeNotifier::ConnectionType ConnectionType; | 93 typedef NetworkChangeNotifier::ConnectionType ConnectionType; |
68 | 94 |
69 ~BaseNetworkChangeNotifierAndroidTest() override {} | 95 ~BaseNetworkChangeNotifierAndroidTest() override {} |
70 | 96 |
71 void RunTest( | 97 void RunTest( |
72 const base::Callback<int(void)>& notifications_count_getter, | 98 const base::Callback<int(void)>& notifications_count_getter, |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 base::Unretained(&delegate_))); | 194 base::Unretained(&delegate_))); |
169 // Check that *all* the observers are notified. Both observers should have the | 195 // Check that *all* the observers are notified. Both observers should have the |
170 // same state. | 196 // same state. |
171 EXPECT_EQ(delegate_observer_.type_notifications_count(), | 197 EXPECT_EQ(delegate_observer_.type_notifications_count(), |
172 other_delegate_observer_.type_notifications_count()); | 198 other_delegate_observer_.type_notifications_count()); |
173 } | 199 } |
174 | 200 |
175 class NetworkChangeNotifierAndroidTest | 201 class NetworkChangeNotifierAndroidTest |
176 : public BaseNetworkChangeNotifierAndroidTest { | 202 : public BaseNetworkChangeNotifierAndroidTest { |
177 protected: | 203 protected: |
178 NetworkChangeNotifierAndroidTest() : notifier_(&delegate_) { | 204 void SetUp() override { |
| 205 IPAddressNumber dns_number; |
| 206 ASSERT_TRUE(ParseIPLiteralToNumber("8.8.8.8", &dns_number)); |
| 207 dns_config_.nameservers.push_back( |
| 208 IPEndPoint(dns_number, dns_protocol::kDefaultPort)); |
| 209 notifier_.reset(new NetworkChangeNotifierAndroid(&delegate_, &dns_config_)); |
179 NetworkChangeNotifier::AddConnectionTypeObserver( | 210 NetworkChangeNotifier::AddConnectionTypeObserver( |
180 &connection_type_observer_); | 211 &connection_type_observer_); |
181 NetworkChangeNotifier::AddConnectionTypeObserver( | 212 NetworkChangeNotifier::AddConnectionTypeObserver( |
182 &other_connection_type_observer_); | 213 &other_connection_type_observer_); |
183 } | 214 } |
184 | 215 |
185 NetworkChangeNotifierObserver connection_type_observer_; | 216 NetworkChangeNotifierObserver connection_type_observer_; |
186 NetworkChangeNotifierObserver other_connection_type_observer_; | 217 NetworkChangeNotifierObserver other_connection_type_observer_; |
187 NetworkChangeNotifier::DisableForTest disable_for_test_; | 218 NetworkChangeNotifier::DisableForTest disable_for_test_; |
188 NetworkChangeNotifierAndroid notifier_; | 219 DnsConfig dns_config_; |
| 220 scoped_ptr<NetworkChangeNotifierAndroid> notifier_; |
189 }; | 221 }; |
190 | 222 |
191 // When a NetworkChangeNotifierAndroid is observing a | 223 // When a NetworkChangeNotifierAndroid is observing a |
192 // NetworkChangeNotifierDelegateAndroid for network state changes, and the | 224 // NetworkChangeNotifierDelegateAndroid for network state changes, and the |
193 // NetworkChangeNotifierDelegateAndroid's connectivity state changes, the | 225 // NetworkChangeNotifierDelegateAndroid's connectivity state changes, the |
194 // NetworkChangeNotifierAndroid should reflect that state. | 226 // NetworkChangeNotifierAndroid should reflect that state. |
195 TEST_F(NetworkChangeNotifierAndroidTest, | 227 TEST_F(NetworkChangeNotifierAndroidTest, |
196 NotificationsSentToNetworkChangeNotifierAndroid) { | 228 NotificationsSentToNetworkChangeNotifierAndroid) { |
197 RunTest( | 229 RunTest(base::Bind(&NetworkChangeNotifierObserver::notifications_count, |
198 base::Bind( | 230 base::Unretained(&connection_type_observer_)), |
199 &NetworkChangeNotifierObserver::notifications_count, | 231 base::Bind(&NetworkChangeNotifierAndroid::GetCurrentConnectionType, |
200 base::Unretained(&connection_type_observer_)), | 232 base::Unretained(notifier_.get()))); |
201 base::Bind( | |
202 &NetworkChangeNotifierAndroid::GetCurrentConnectionType, | |
203 base::Unretained(¬ifier_))); | |
204 } | 233 } |
205 | 234 |
206 // When a NetworkChangeNotifierAndroid's connection state changes, it should | 235 // When a NetworkChangeNotifierAndroid's connection state changes, it should |
207 // notify all of its observers. | 236 // notify all of its observers. |
208 TEST_F(NetworkChangeNotifierAndroidTest, | 237 TEST_F(NetworkChangeNotifierAndroidTest, |
209 NotificationsSentToClientsOfNetworkChangeNotifier) { | 238 NotificationsSentToClientsOfNetworkChangeNotifier) { |
210 RunTest( | 239 RunTest( |
211 base::Bind( | 240 base::Bind( |
212 &NetworkChangeNotifierObserver::notifications_count, | 241 &NetworkChangeNotifierObserver::notifications_count, |
213 base::Unretained(&connection_type_observer_)), | 242 base::Unretained(&connection_type_observer_)), |
214 base::Bind(&NetworkChangeNotifier::GetConnectionType)); | 243 base::Bind(&NetworkChangeNotifier::GetConnectionType)); |
215 // Check that *all* the observers are notified. | 244 // Check that *all* the observers are notified. |
216 EXPECT_EQ(connection_type_observer_.notifications_count(), | 245 EXPECT_EQ(connection_type_observer_.notifications_count(), |
217 other_connection_type_observer_.notifications_count()); | 246 other_connection_type_observer_.notifications_count()); |
218 } | 247 } |
219 | 248 |
220 TEST_F(NetworkChangeNotifierAndroidTest, MaxBandwidth) { | 249 TEST_F(NetworkChangeNotifierAndroidTest, MaxBandwidth) { |
221 SetOnline(); | 250 SetOnline(); |
222 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, | 251 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, |
223 notifier_.GetConnectionType()); | 252 notifier_->GetConnectionType()); |
224 EXPECT_EQ(std::numeric_limits<double>::infinity(), | 253 EXPECT_EQ(std::numeric_limits<double>::infinity(), |
225 notifier_.GetMaxBandwidth()); | 254 notifier_->GetMaxBandwidth()); |
226 SetOffline(); | 255 SetOffline(); |
227 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, | 256 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, |
228 notifier_.GetConnectionType()); | 257 notifier_->GetConnectionType()); |
229 EXPECT_EQ(0.0, notifier_.GetMaxBandwidth()); | 258 EXPECT_EQ(0.0, notifier_->GetMaxBandwidth()); |
230 } | 259 } |
231 | 260 |
232 TEST_F(NetworkChangeNotifierDelegateAndroidTest, | 261 TEST_F(NetworkChangeNotifierDelegateAndroidTest, |
233 MaxBandwidthNotifiedOnConnectionChange) { | 262 MaxBandwidthNotifiedOnConnectionChange) { |
234 EXPECT_EQ(0, delegate_observer_.bandwidth_notifications_count()); | 263 EXPECT_EQ(0, delegate_observer_.bandwidth_notifications_count()); |
235 SetOffline(); | 264 SetOffline(); |
236 EXPECT_EQ(1, delegate_observer_.bandwidth_notifications_count()); | 265 EXPECT_EQ(1, delegate_observer_.bandwidth_notifications_count()); |
237 SetOnline(); | 266 SetOnline(); |
238 EXPECT_EQ(2, delegate_observer_.bandwidth_notifications_count()); | 267 EXPECT_EQ(2, delegate_observer_.bandwidth_notifications_count()); |
239 SetOnline(); | 268 SetOnline(); |
240 EXPECT_EQ(2, delegate_observer_.bandwidth_notifications_count()); | 269 EXPECT_EQ(2, delegate_observer_.bandwidth_notifications_count()); |
241 } | 270 } |
242 | 271 |
| 272 TEST_F(NetworkChangeNotifierAndroidTest, InitialSignal) { |
| 273 DNSChangeObserver dns_change_observer; |
| 274 NetworkChangeNotifier::AddDNSObserver(&dns_change_observer); |
| 275 base::MessageLoop::current()->Run(); |
| 276 EXPECT_EQ(1, dns_change_observer.initial_notifications_count()); |
| 277 EXPECT_EQ(0, dns_change_observer.change_notifications_count()); |
| 278 NetworkChangeNotifier::RemoveDNSObserver(&dns_change_observer); |
| 279 } |
| 280 |
243 } // namespace net | 281 } // namespace net |
OLD | NEW |