Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1080)

Side by Side Diff: net/android/network_change_notifier_android_unittest.cc

Issue 11620007: Switch from OnIPAddressChanged and OnConnectionTypeChange to OnNetworkChanged Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
(...skipping 19 matching lines...) Expand all
30 30
31 int notifications_count() const { 31 int notifications_count() const {
32 return notifications_count_; 32 return notifications_count_;
33 } 33 }
34 34
35 private: 35 private:
36 int notifications_count_; 36 int notifications_count_;
37 }; 37 };
38 38
39 class NetworkChangeNotifierObserver 39 class NetworkChangeNotifierObserver
40 : public NetworkChangeNotifier::ConnectionTypeObserver { 40 : public NetworkChangeNotifier::NetworkChangeObserver {
41 public: 41 public:
42 NetworkChangeNotifierObserver() : notifications_count_(0) {} 42 NetworkChangeNotifierObserver() : notifications_count_(0) {}
43 43
44 // NetworkChangeNotifier::Observer: 44 // NetworkChangeNotifier::NetworkChangeObserver:
45 virtual void OnConnectionTypeChanged( 45 virtual void OnNetworkChanged(
46 NetworkChangeNotifier::ConnectionType connection_type) OVERRIDE { 46 NetworkChangeNotifier::ConnectionType connection_type) OVERRIDE {
47 notifications_count_++; 47 notifications_count_++;
48 } 48 }
49 49
50 int notifications_count() const { 50 int notifications_count() const {
51 return notifications_count_; 51 return notifications_count_;
52 } 52 }
53 53
54 private: 54 private:
55 int notifications_count_; 55 int notifications_count_;
56 }; 56 };
57 57
58 } // namespace 58 } // namespace
59 59
60 class BaseNetworkChangeNotifierAndroidTest : public testing::Test { 60 class BaseNetworkChangeNotifierAndroidTest : public testing::Test {
61 protected: 61 protected:
62 typedef NetworkChangeNotifier::ConnectionType ConnectionType; 62 typedef NetworkChangeNotifier::ConnectionType ConnectionType;
63 63
64 virtual ~BaseNetworkChangeNotifierAndroidTest() {} 64 virtual ~BaseNetworkChangeNotifierAndroidTest() {}
65 65
66 // |extra_online_notification| indicates
67 // NetworkChangeNotifier::OnNetworkChanged() is being tested, which produces
68 // two notifications when transitioning to an online state.
66 void RunTest( 69 void RunTest(
67 const base::Callback<int(void)>& notifications_count_getter, 70 const base::Callback<int(void)>& notifications_count_getter,
68 const base::Callback<ConnectionType(void)>& connection_type_getter) { 71 const base::Callback<ConnectionType(void)>& connection_type_getter,
72 bool extra_online_notification) {
69 EXPECT_EQ(0, notifications_count_getter.Run()); 73 EXPECT_EQ(0, notifications_count_getter.Run());
70 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, 74 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN,
71 connection_type_getter.Run()); 75 connection_type_getter.Run());
72 76
73 // Changing from online to offline should trigger a notification. 77 // Changing from online to offline should trigger a notification.
74 SetOffline(); 78 SetOffline();
75 EXPECT_EQ(1, notifications_count_getter.Run()); 79 EXPECT_EQ(1, notifications_count_getter.Run());
76 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, 80 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
77 connection_type_getter.Run()); 81 connection_type_getter.Run());
78 82
79 // No notification should be triggered when the offline state hasn't 83 // No notification should be triggered when the offline state hasn't
80 // changed. 84 // changed.
81 SetOffline(); 85 SetOffline();
82 EXPECT_EQ(1, notifications_count_getter.Run()); 86 EXPECT_EQ(1, notifications_count_getter.Run());
83 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, 87 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
84 connection_type_getter.Run()); 88 connection_type_getter.Run());
85 89
86 // Going from offline to online should trigger a notification. 90 // Going from offline to online should trigger a notification.
87 SetOnline(); 91 SetOnline();
88 EXPECT_EQ(2, notifications_count_getter.Run()); 92 EXPECT_EQ(extra_online_notification ? 3 : 2,
93 notifications_count_getter.Run());
89 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, 94 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN,
90 connection_type_getter.Run()); 95 connection_type_getter.Run());
91 } 96 }
92 97
93 void SetOnline() { 98 void SetOnline() {
94 delegate_.SetOnline(); 99 delegate_.SetOnline();
95 // Note that this is needed because ObserverListThreadSafe uses PostTask(). 100 // Note that this is needed because ObserverListThreadSafe uses PostTask().
96 MessageLoop::current()->RunUntilIdle(); 101 MessageLoop::current()->RunUntilIdle();
97 } 102 }
98 103
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 // A testing-only observer is used here for testing. In production the 159 // A testing-only observer is used here for testing. In production the
155 // delegate's observers are instances of NetworkChangeNotifierAndroid. 160 // delegate's observers are instances of NetworkChangeNotifierAndroid.
156 TEST_F(NetworkChangeNotifierDelegateAndroidTest, DelegateObserverNotified) { 161 TEST_F(NetworkChangeNotifierDelegateAndroidTest, DelegateObserverNotified) {
157 // Test the logic with a single observer. 162 // Test the logic with a single observer.
158 RunTest( 163 RunTest(
159 base::Bind( 164 base::Bind(
160 &NetworkChangeNotifierDelegateAndroidObserver::notifications_count, 165 &NetworkChangeNotifierDelegateAndroidObserver::notifications_count,
161 base::Unretained(&delegate_observer_)), 166 base::Unretained(&delegate_observer_)),
162 base::Bind( 167 base::Bind(
163 &NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType, 168 &NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType,
164 base::Unretained(&delegate_))); 169 base::Unretained(&delegate_)),
170 false);
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_.notifications_count(),
168 other_delegate_observer_.notifications_count()); 174 other_delegate_observer_.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::AddNetworkChangeObserver(&connection_type_observer_);
176 &connection_type_observer_); 182 NetworkChangeNotifier::AddNetworkChangeObserver(
177 NetworkChangeNotifier::AddConnectionTypeObserver(
178 &other_connection_type_observer_); 183 &other_connection_type_observer_);
179 } 184 }
180 185
181 NetworkChangeNotifierObserver connection_type_observer_; 186 NetworkChangeNotifierObserver connection_type_observer_;
182 NetworkChangeNotifierObserver other_connection_type_observer_; 187 NetworkChangeNotifierObserver other_connection_type_observer_;
183 NetworkChangeNotifier::DisableForTest disable_for_test_; 188 NetworkChangeNotifier::DisableForTest disable_for_test_;
184 NetworkChangeNotifierAndroid notifier_; 189 NetworkChangeNotifierAndroid notifier_;
185 }; 190 };
186 191
187 // When a NetworkChangeNotifierAndroid is observing a 192 // When a NetworkChangeNotifierAndroid is observing a
188 // NetworkChangeNotifierDelegateAndroid for network state changes, and the 193 // NetworkChangeNotifierDelegateAndroid for network state changes, and the
189 // NetworkChangeNotifierDelegateAndroid's connectivity state changes, the 194 // NetworkChangeNotifierDelegateAndroid's connectivity state changes, the
190 // NetworkChangeNotifierAndroid should reflect that state. 195 // NetworkChangeNotifierAndroid should reflect that state.
191 TEST_F(NetworkChangeNotifierAndroidTest, 196 TEST_F(NetworkChangeNotifierAndroidTest,
192 NotificationsSentToNetworkChangeNotifierAndroid) { 197 NotificationsSentToNetworkChangeNotifierAndroid) {
193 RunTest( 198 RunTest(
194 base::Bind( 199 base::Bind(
195 &NetworkChangeNotifierObserver::notifications_count, 200 &NetworkChangeNotifierObserver::notifications_count,
196 base::Unretained(&connection_type_observer_)), 201 base::Unretained(&connection_type_observer_)),
197 base::Bind( 202 base::Bind(
198 &NetworkChangeNotifierAndroid::GetCurrentConnectionType, 203 &NetworkChangeNotifierAndroid::GetCurrentConnectionType,
199 base::Unretained(&notifier_))); 204 base::Unretained(&notifier_)),
205 true);
200 } 206 }
201 207
202 // When a NetworkChangeNotifierAndroid's connection state changes, it should 208 // When a NetworkChangeNotifierAndroid's connection state changes, it should
203 // notify all of its observers. 209 // notify all of its observers.
204 TEST_F(NetworkChangeNotifierAndroidTest, 210 TEST_F(NetworkChangeNotifierAndroidTest,
205 NotificationsSentToClientsOfNetworkChangeNotifier) { 211 NotificationsSentToClientsOfNetworkChangeNotifier) {
206 RunTest( 212 RunTest(
207 base::Bind( 213 base::Bind(
208 &NetworkChangeNotifierObserver::notifications_count, 214 &NetworkChangeNotifierObserver::notifications_count,
209 base::Unretained(&connection_type_observer_)), 215 base::Unretained(&connection_type_observer_)),
210 base::Bind(&NetworkChangeNotifier::GetConnectionType)); 216 base::Bind(&NetworkChangeNotifier::GetConnectionType),
217 true);
211 // Check that *all* the observers are notified. 218 // Check that *all* the observers are notified.
212 EXPECT_EQ(connection_type_observer_.notifications_count(), 219 EXPECT_EQ(connection_type_observer_.notifications_count(),
213 other_connection_type_observer_.notifications_count()); 220 other_connection_type_observer_.notifications_count());
214 } 221 }
215 222
216 } // namespace net 223 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698