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 "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 namespace net { | 17 namespace net { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 class NetworkChangeNotifierDelegateAndroidObserver | 21 class NetworkChangeNotifierDelegateAndroidObserver |
22 : public NetworkChangeNotifierDelegateAndroid::Observer { | 22 : public NetworkChangeNotifierDelegateAndroid::Observer { |
23 public: | 23 public: |
24 NetworkChangeNotifierDelegateAndroidObserver() : notifications_count_(0) {} | 24 NetworkChangeNotifierDelegateAndroidObserver() |
| 25 : type_notifications_count_(0), max_bandwidth_notifications_count_(0) {} |
25 | 26 |
26 // NetworkChangeNotifierDelegateAndroid::Observer: | 27 // NetworkChangeNotifierDelegateAndroid::Observer: |
27 virtual void OnConnectionTypeChanged() override { | 28 virtual void OnConnectionTypeChanged() override { |
28 notifications_count_++; | 29 type_notifications_count_++; |
29 } | 30 } |
30 | 31 |
31 int notifications_count() const { | 32 virtual void OnMaxBandwidthChanged(double max_bandwidth_mbps) override { |
32 return notifications_count_; | 33 max_bandwidth_notifications_count_++; |
| 34 } |
| 35 |
| 36 int type_notifications_count() const { return type_notifications_count_; } |
| 37 int bandwidth_notifications_count() const { |
| 38 return max_bandwidth_notifications_count_; |
33 } | 39 } |
34 | 40 |
35 private: | 41 private: |
36 int notifications_count_; | 42 int type_notifications_count_; |
| 43 int max_bandwidth_notifications_count_; |
37 }; | 44 }; |
38 | 45 |
39 class NetworkChangeNotifierObserver | 46 class NetworkChangeNotifierObserver |
40 : public NetworkChangeNotifier::ConnectionTypeObserver { | 47 : public NetworkChangeNotifier::ConnectionTypeObserver { |
41 public: | 48 public: |
42 NetworkChangeNotifierObserver() : notifications_count_(0) {} | 49 NetworkChangeNotifierObserver() : notifications_count_(0) {} |
43 | 50 |
44 // NetworkChangeNotifier::Observer: | 51 // NetworkChangeNotifier::Observer: |
45 virtual void OnConnectionTypeChanged( | 52 virtual void OnConnectionTypeChanged( |
46 NetworkChangeNotifier::ConnectionType connection_type) override { | 53 NetworkChangeNotifier::ConnectionType connection_type) override { |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 | 155 |
149 NetworkChangeNotifierDelegateAndroidObserver delegate_observer_; | 156 NetworkChangeNotifierDelegateAndroidObserver delegate_observer_; |
150 NetworkChangeNotifierDelegateAndroidObserver other_delegate_observer_; | 157 NetworkChangeNotifierDelegateAndroidObserver other_delegate_observer_; |
151 }; | 158 }; |
152 | 159 |
153 // Tests that the NetworkChangeNotifierDelegateAndroid's observers are notified. | 160 // Tests that the NetworkChangeNotifierDelegateAndroid's observers are notified. |
154 // A testing-only observer is used here for testing. In production the | 161 // A testing-only observer is used here for testing. In production the |
155 // delegate's observers are instances of NetworkChangeNotifierAndroid. | 162 // delegate's observers are instances of NetworkChangeNotifierAndroid. |
156 TEST_F(NetworkChangeNotifierDelegateAndroidTest, DelegateObserverNotified) { | 163 TEST_F(NetworkChangeNotifierDelegateAndroidTest, DelegateObserverNotified) { |
157 // Test the logic with a single observer. | 164 // Test the logic with a single observer. |
158 RunTest( | 165 RunTest(base::Bind(&NetworkChangeNotifierDelegateAndroidObserver:: |
159 base::Bind( | 166 type_notifications_count, |
160 &NetworkChangeNotifierDelegateAndroidObserver::notifications_count, | 167 base::Unretained(&delegate_observer_)), |
161 base::Unretained(&delegate_observer_)), | 168 base::Bind( |
162 base::Bind( | 169 &NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType, |
163 &NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType, | 170 base::Unretained(&delegate_))); |
164 base::Unretained(&delegate_))); | |
165 // Check that *all* the observers are notified. Both observers should have the | 171 // Check that *all* the observers are notified. Both observers should have the |
166 // same state. | 172 // same state. |
167 EXPECT_EQ(delegate_observer_.notifications_count(), | 173 EXPECT_EQ(delegate_observer_.type_notifications_count(), |
168 other_delegate_observer_.notifications_count()); | 174 other_delegate_observer_.type_notifications_count()); |
169 } | 175 } |
170 | 176 |
171 class NetworkChangeNotifierAndroidTest | 177 class NetworkChangeNotifierAndroidTest |
172 : public BaseNetworkChangeNotifierAndroidTest { | 178 : public BaseNetworkChangeNotifierAndroidTest { |
173 protected: | 179 protected: |
174 NetworkChangeNotifierAndroidTest() : notifier_(&delegate_) { | 180 NetworkChangeNotifierAndroidTest() : notifier_(&delegate_) { |
175 NetworkChangeNotifier::AddConnectionTypeObserver( | 181 NetworkChangeNotifier::AddConnectionTypeObserver( |
176 &connection_type_observer_); | 182 &connection_type_observer_); |
177 NetworkChangeNotifier::AddConnectionTypeObserver( | 183 NetworkChangeNotifier::AddConnectionTypeObserver( |
178 &other_connection_type_observer_); | 184 &other_connection_type_observer_); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 EXPECT_EQ(connection_type_observer_.notifications_count(), | 218 EXPECT_EQ(connection_type_observer_.notifications_count(), |
213 other_connection_type_observer_.notifications_count()); | 219 other_connection_type_observer_.notifications_count()); |
214 } | 220 } |
215 | 221 |
216 TEST_F(NetworkChangeNotifierAndroidTest, MaxBandwidth) { | 222 TEST_F(NetworkChangeNotifierAndroidTest, MaxBandwidth) { |
217 SetOnline(); | 223 SetOnline(); |
218 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, | 224 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, |
219 notifier_.GetConnectionType()); | 225 notifier_.GetConnectionType()); |
220 EXPECT_EQ(std::numeric_limits<double>::infinity(), | 226 EXPECT_EQ(std::numeric_limits<double>::infinity(), |
221 notifier_.GetMaxBandwidth()); | 227 notifier_.GetMaxBandwidth()); |
222 | |
223 SetOffline(); | 228 SetOffline(); |
224 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, | 229 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, |
225 notifier_.GetConnectionType()); | 230 notifier_.GetConnectionType()); |
226 EXPECT_EQ(0.0, notifier_.GetMaxBandwidth()); | 231 EXPECT_EQ(0.0, notifier_.GetMaxBandwidth()); |
227 } | 232 } |
228 | 233 |
| 234 TEST_F(NetworkChangeNotifierDelegateAndroidTest, |
| 235 MaxBandwidthNotifiedOnConnectionChange) { |
| 236 EXPECT_EQ(0, delegate_observer_.bandwidth_notifications_count()); |
| 237 SetOffline(); |
| 238 EXPECT_EQ(1, delegate_observer_.bandwidth_notifications_count()); |
| 239 SetOnline(); |
| 240 EXPECT_EQ(2, delegate_observer_.bandwidth_notifications_count()); |
| 241 SetOnline(); |
| 242 EXPECT_EQ(2, delegate_observer_.bandwidth_notifications_count()); |
| 243 } |
| 244 |
229 } // namespace net | 245 } // namespace net |
OLD | NEW |