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() : notifications_count_(0) {} |
25 | 25 |
26 // NetworkChangeNotifierDelegateAndroid::Observer: | 26 // NetworkChangeNotifierDelegateAndroid::Observer: |
27 virtual void OnConnectionTypeChanged() OVERRIDE { | 27 virtual void OnConnectionTypeChanged() OVERRIDE { notifications_count_++; } |
28 notifications_count_++; | |
29 } | |
30 | 28 |
31 int notifications_count() const { | 29 int notifications_count() const { return notifications_count_; } |
32 return notifications_count_; | |
33 } | |
34 | 30 |
35 private: | 31 private: |
36 int notifications_count_; | 32 int notifications_count_; |
37 }; | 33 }; |
38 | 34 |
39 class NetworkChangeNotifierObserver | 35 class NetworkChangeNotifierObserver |
40 : public NetworkChangeNotifier::ConnectionTypeObserver { | 36 : public NetworkChangeNotifier::ConnectionTypeObserver { |
41 public: | 37 public: |
42 NetworkChangeNotifierObserver() : notifications_count_(0) {} | 38 NetworkChangeNotifierObserver() : notifications_count_(0) {} |
43 | 39 |
44 // NetworkChangeNotifier::Observer: | 40 // NetworkChangeNotifier::Observer: |
45 virtual void OnConnectionTypeChanged( | 41 virtual void OnConnectionTypeChanged( |
46 NetworkChangeNotifier::ConnectionType connection_type) OVERRIDE { | 42 NetworkChangeNotifier::ConnectionType connection_type) OVERRIDE { |
47 notifications_count_++; | 43 notifications_count_++; |
48 } | 44 } |
49 | 45 |
50 int notifications_count() const { | 46 int notifications_count() const { return notifications_count_; } |
51 return notifications_count_; | |
52 } | |
53 | 47 |
54 private: | 48 private: |
55 int notifications_count_; | 49 int notifications_count_; |
56 }; | 50 }; |
57 | 51 |
58 } // namespace | 52 } // namespace |
59 | 53 |
60 class BaseNetworkChangeNotifierAndroidTest : public testing::Test { | 54 class BaseNetworkChangeNotifierAndroidTest : public testing::Test { |
61 protected: | 55 protected: |
62 typedef NetworkChangeNotifier::ConnectionType ConnectionType; | 56 typedef NetworkChangeNotifier::ConnectionType ConnectionType; |
63 | 57 |
64 virtual ~BaseNetworkChangeNotifierAndroidTest() {} | 58 virtual ~BaseNetworkChangeNotifierAndroidTest() {} |
65 | 59 |
66 void RunTest( | 60 void RunTest( |
67 const base::Callback<int(void)>& notifications_count_getter, | 61 const base::Callback<int(void)>& notifications_count_getter, |
68 const base::Callback<ConnectionType(void)>& connection_type_getter) { | 62 const base::Callback<ConnectionType(void)>& connection_type_getter) { |
69 EXPECT_EQ(0, notifications_count_getter.Run()); | 63 EXPECT_EQ(0, notifications_count_getter.Run()); |
70 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, | 64 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, |
71 connection_type_getter.Run()); | 65 connection_type_getter.Run()); |
72 | 66 |
73 // Changing from online to offline should trigger a notification. | 67 // Changing from online to offline should trigger a notification. |
74 SetOffline(); | 68 SetOffline(); |
75 EXPECT_EQ(1, notifications_count_getter.Run()); | 69 EXPECT_EQ(1, notifications_count_getter.Run()); |
76 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, | 70 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, |
77 connection_type_getter.Run()); | 71 connection_type_getter.Run()); |
78 | 72 |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 NetworkChangeNotifier::DisableForTest disable_for_test_; | 177 NetworkChangeNotifier::DisableForTest disable_for_test_; |
184 NetworkChangeNotifierAndroid notifier_; | 178 NetworkChangeNotifierAndroid notifier_; |
185 }; | 179 }; |
186 | 180 |
187 // When a NetworkChangeNotifierAndroid is observing a | 181 // When a NetworkChangeNotifierAndroid is observing a |
188 // NetworkChangeNotifierDelegateAndroid for network state changes, and the | 182 // NetworkChangeNotifierDelegateAndroid for network state changes, and the |
189 // NetworkChangeNotifierDelegateAndroid's connectivity state changes, the | 183 // NetworkChangeNotifierDelegateAndroid's connectivity state changes, the |
190 // NetworkChangeNotifierAndroid should reflect that state. | 184 // NetworkChangeNotifierAndroid should reflect that state. |
191 TEST_F(NetworkChangeNotifierAndroidTest, | 185 TEST_F(NetworkChangeNotifierAndroidTest, |
192 NotificationsSentToNetworkChangeNotifierAndroid) { | 186 NotificationsSentToNetworkChangeNotifierAndroid) { |
193 RunTest( | 187 RunTest(base::Bind(&NetworkChangeNotifierObserver::notifications_count, |
194 base::Bind( | 188 base::Unretained(&connection_type_observer_)), |
195 &NetworkChangeNotifierObserver::notifications_count, | 189 base::Bind(&NetworkChangeNotifierAndroid::GetCurrentConnectionType, |
196 base::Unretained(&connection_type_observer_)), | 190 base::Unretained(¬ifier_))); |
197 base::Bind( | |
198 &NetworkChangeNotifierAndroid::GetCurrentConnectionType, | |
199 base::Unretained(¬ifier_))); | |
200 } | 191 } |
201 | 192 |
202 // When a NetworkChangeNotifierAndroid's connection state changes, it should | 193 // When a NetworkChangeNotifierAndroid's connection state changes, it should |
203 // notify all of its observers. | 194 // notify all of its observers. |
204 TEST_F(NetworkChangeNotifierAndroidTest, | 195 TEST_F(NetworkChangeNotifierAndroidTest, |
205 NotificationsSentToClientsOfNetworkChangeNotifier) { | 196 NotificationsSentToClientsOfNetworkChangeNotifier) { |
206 RunTest( | 197 RunTest(base::Bind(&NetworkChangeNotifierObserver::notifications_count, |
207 base::Bind( | 198 base::Unretained(&connection_type_observer_)), |
208 &NetworkChangeNotifierObserver::notifications_count, | 199 base::Bind(&NetworkChangeNotifier::GetConnectionType)); |
209 base::Unretained(&connection_type_observer_)), | |
210 base::Bind(&NetworkChangeNotifier::GetConnectionType)); | |
211 // Check that *all* the observers are notified. | 200 // Check that *all* the observers are notified. |
212 EXPECT_EQ(connection_type_observer_.notifications_count(), | 201 EXPECT_EQ(connection_type_observer_.notifications_count(), |
213 other_connection_type_observer_.notifications_count()); | 202 other_connection_type_observer_.notifications_count()); |
214 } | 203 } |
215 | 204 |
216 } // namespace net | 205 } // namespace net |
OLD | NEW |