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

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

Issue 11628008: Provide NetworkChangeNotifierAndroid with the actual initial connection type. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address Ryan's comments Created 7 years, 12 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"
11 #include "base/message_loop.h" 11 #include "base/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 // Template used to generate both the NetworkChangeNotifierDelegateAndroid and 21 class NetworkChangeNotifierDelegateAndroidObserver
22 // NetworkChangeNotifier::ConnectionTypeObserver implementations which have the 22 : public NetworkChangeNotifierDelegateAndroid::Observer {
23 // same interface.
24 template <typename BaseObserver>
25 class ObserverImpl : public BaseObserver {
26 public: 23 public:
27 ObserverImpl() 24 NetworkChangeNotifierDelegateAndroidObserver() : notifications_count_(0) {}
28 : times_connection_type_changed_(0), 25
29 current_connection_(NetworkChangeNotifier::CONNECTION_UNKNOWN) { 26 // NetworkChangeNotifierDelegateAndroid::Observer:
27 virtual void OnConnectionTypeChanged() OVERRIDE {
28 notifications_count_++;
30 } 29 }
31 30
32 // BaseObserver: 31 int notifications_count() const {
33 virtual void OnConnectionTypeChanged( 32 return notifications_count_;
34 NetworkChangeNotifier::ConnectionType type) OVERRIDE {
35 times_connection_type_changed_++;
36 current_connection_ = type;
37 }
38
39 int times_connection_type_changed() const {
40 return times_connection_type_changed_;
41 }
42
43 NetworkChangeNotifier::ConnectionType current_connection() const {
44 return current_connection_;
45 } 33 }
46 34
47 private: 35 private:
48 int times_connection_type_changed_; 36 int notifications_count_;
49 NetworkChangeNotifier::ConnectionType current_connection_; 37 };
50 38
51 DISALLOW_COPY_AND_ASSIGN(ObserverImpl); 39 class NetworkChangeNotifierObserver
40 : public NetworkChangeNotifier::ConnectionTypeObserver {
41 public:
42 NetworkChangeNotifierObserver() : notifications_count_(0) {}
43
44 // NetworkChangeNotifier::Observer:
45 virtual void OnConnectionTypeChanged(
46 NetworkChangeNotifier::ConnectionType connection_type) OVERRIDE {
47 notifications_count_++;
48 }
49
50 int notifications_count() const {
51 return notifications_count_;
52 }
53
54 private:
55 int notifications_count_;
52 }; 56 };
53 57
54 } // namespace 58 } // namespace
55 59
56 class BaseNetworkChangeNotifierAndroidTest : public testing::Test { 60 class BaseNetworkChangeNotifierAndroidTest : public testing::Test {
57 protected: 61 protected:
58 typedef NetworkChangeNotifier::ConnectionType ConnectionType; 62 typedef NetworkChangeNotifier::ConnectionType ConnectionType;
59 63
60 virtual ~BaseNetworkChangeNotifierAndroidTest() {} 64 virtual ~BaseNetworkChangeNotifierAndroidTest() {}
61 65
62 void RunTest( 66 void RunTest(
63 const base::Callback<int(void)>& times_connection_type_changed_callback, 67 const base::Callback<int(void)>& notifications_count_getter,
64 const base::Callback<ConnectionType(void)>& connection_type_getter) { 68 const base::Callback<ConnectionType(void)>& connection_type_getter) {
65 EXPECT_EQ(0, times_connection_type_changed_callback.Run()); 69 EXPECT_EQ(0, notifications_count_getter.Run());
66 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, 70 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN,
67 connection_type_getter.Run()); 71 connection_type_getter.Run());
68 72
69 ForceConnectivityState(NetworkChangeNotifierDelegateAndroid::OFFLINE); 73 SetOffline();
Ryan Sleevi 2013/01/03 18:28:25 nit: Add some comments (*above* line 73/78/83) //
Philippe 2013/01/04 10:13:01 Thanks.
70 EXPECT_EQ(1, times_connection_type_changed_callback.Run()); 74 EXPECT_EQ(1, notifications_count_getter.Run());
71 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, 75 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
72 connection_type_getter.Run()); 76 connection_type_getter.Run());
73 77
74 ForceConnectivityState(NetworkChangeNotifierDelegateAndroid::OFFLINE); 78 SetOffline();
Ryan Sleevi 2013/01/03 18:28:25 // No notification should be triggered when the of
Philippe 2013/01/04 10:13:01 Done.
75 EXPECT_EQ(1, times_connection_type_changed_callback.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 ForceConnectivityState(NetworkChangeNotifierDelegateAndroid::ONLINE); 83 SetOnline();
Ryan Sleevi 2013/01/03 18:28:25 // Going from offline to online should trigger a n
Philippe 2013/01/04 10:13:01 Done.
80 EXPECT_EQ(2, times_connection_type_changed_callback.Run()); 84 EXPECT_EQ(2, notifications_count_getter.Run());
81 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, 85 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN,
82 connection_type_getter.Run()); 86 connection_type_getter.Run());
83 } 87 }
84 88
85 void ForceConnectivityState( 89 void SetOnline() {
86 NetworkChangeNotifierDelegateAndroid::ConnectivityState state) { 90 delegate_.SetOnline();
87 delegate_.ForceConnectivityState(state);
88 // Note that this is needed because ObserverListThreadSafe uses PostTask(). 91 // Note that this is needed because ObserverListThreadSafe uses PostTask().
89 MessageLoop::current()->RunUntilIdle(); 92 MessageLoop::current()->RunUntilIdle();
90 } 93 }
91 94
95 void SetOffline() {
96 delegate_.SetOffline();
97 // See comment above.
98 MessageLoop::current()->RunUntilIdle();
99 }
100
92 NetworkChangeNotifierDelegateAndroid delegate_; 101 NetworkChangeNotifierDelegateAndroid delegate_;
93 }; 102 };
94 103
104 // Tests that NetworkChangeNotifierDelegateAndroid is initialized with the
105 // actual connection type rather than a hardcoded one (e.g.
106 // CONNECTION_UNKNOWN). Initializing the connection type to CONNECTION_UNKNOWN
107 // and relying on the first network change notification to set it correctly can
108 // be problematic in case there is a long delay between the delegate's
109 // construction and the notification.
110 TEST_F(BaseNetworkChangeNotifierAndroidTest,
111 DelegateIsInitializedWithCurrentConnectionType) {
112 SetOffline();
113 ASSERT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
114 delegate_.GetCurrentConnectionType());
115 // Instantiate another delegate to validate that it uses the actual
116 // connection type at construction.
117 scoped_ptr<NetworkChangeNotifierDelegateAndroid> other_delegate(
118 new NetworkChangeNotifierDelegateAndroid());
119 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
120 other_delegate->GetCurrentConnectionType());
121
122 // Toggle the global connectivity state and instantiate another delegate
123 // again.
124 SetOnline();
125 ASSERT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN,
126 delegate_.GetCurrentConnectionType());
127 other_delegate.reset(new NetworkChangeNotifierDelegateAndroid());
128 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN,
129 other_delegate->GetCurrentConnectionType());
130 }
131
95 class NetworkChangeNotifierDelegateAndroidTest 132 class NetworkChangeNotifierDelegateAndroidTest
96 : public BaseNetworkChangeNotifierAndroidTest { 133 : public BaseNetworkChangeNotifierAndroidTest {
97 protected: 134 protected:
98 typedef ObserverImpl<
99 NetworkChangeNotifierDelegateAndroid::Observer> TestDelegateObserver;
100
101 NetworkChangeNotifierDelegateAndroidTest() { 135 NetworkChangeNotifierDelegateAndroidTest() {
102 delegate_.AddObserver(&delegate_observer_); 136 delegate_.AddObserver(&delegate_observer_);
103 delegate_.AddObserver(&other_delegate_observer_); 137 delegate_.AddObserver(&other_delegate_observer_);
104 } 138 }
105 139
106 virtual ~NetworkChangeNotifierDelegateAndroidTest() { 140 virtual ~NetworkChangeNotifierDelegateAndroidTest() {
107 delegate_.RemoveObserver(&delegate_observer_); 141 delegate_.RemoveObserver(&delegate_observer_);
108 delegate_.RemoveObserver(&other_delegate_observer_); 142 delegate_.RemoveObserver(&other_delegate_observer_);
109 } 143 }
110 144
111 TestDelegateObserver delegate_observer_; 145 NetworkChangeNotifierDelegateAndroidObserver delegate_observer_;
112 TestDelegateObserver other_delegate_observer_; 146 NetworkChangeNotifierDelegateAndroidObserver other_delegate_observer_;
113 }; 147 };
114 148
115 // Tests that the NetworkChangeNotifierDelegateAndroid's observers are notified. 149 // Tests that the NetworkChangeNotifierDelegateAndroid's observers are notified.
116 // A testing-only observer is used here for testing. In production the 150 // A testing-only observer is used here for testing. In production the
117 // delegate's observers are instances of NetworkChangeNotifierAndroid. 151 // delegate's observers are instances of NetworkChangeNotifierAndroid.
118 TEST_F(NetworkChangeNotifierDelegateAndroidTest, DelegateObserverNotified) { 152 TEST_F(NetworkChangeNotifierDelegateAndroidTest, DelegateObserverNotified) {
119 // Test the logic with a single observer. 153 // Test the logic with a single observer.
120 RunTest( 154 RunTest(
121 base::Bind( 155 base::Bind(
122 &TestDelegateObserver::times_connection_type_changed, 156 &NetworkChangeNotifierDelegateAndroidObserver::notifications_count,
123 base::Unretained(&delegate_observer_)), 157 base::Unretained(&delegate_observer_)),
124 base::Bind( 158 base::Bind(
125 &TestDelegateObserver::current_connection, 159 &NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType,
126 base::Unretained(&delegate_observer_))); 160 base::Unretained(&delegate_)));
127 // Check that *all* the observers are notified. Both observers should have the 161 // Check that *all* the observers are notified. Both observers should have the
128 // same state. 162 // same state.
129 EXPECT_EQ(delegate_observer_.times_connection_type_changed(), 163 EXPECT_EQ(delegate_observer_.notifications_count(),
130 other_delegate_observer_.times_connection_type_changed()); 164 other_delegate_observer_.notifications_count());
131 EXPECT_EQ(delegate_observer_.current_connection(),
132 other_delegate_observer_.current_connection());
133 } 165 }
134 166
135 class NetworkChangeNotifierAndroidTest 167 class NetworkChangeNotifierAndroidTest
136 : public BaseNetworkChangeNotifierAndroidTest { 168 : public BaseNetworkChangeNotifierAndroidTest {
137 protected: 169 protected:
138 typedef ObserverImpl<
139 NetworkChangeNotifier::ConnectionTypeObserver> TestConnectionTypeObserver;
140
141 NetworkChangeNotifierAndroidTest() : notifier_(&delegate_) { 170 NetworkChangeNotifierAndroidTest() : notifier_(&delegate_) {
142 NetworkChangeNotifier::AddConnectionTypeObserver( 171 NetworkChangeNotifier::AddConnectionTypeObserver(
143 &connection_type_observer_); 172 &connection_type_observer_);
144 NetworkChangeNotifier::AddConnectionTypeObserver( 173 NetworkChangeNotifier::AddConnectionTypeObserver(
145 &other_connection_type_observer_); 174 &other_connection_type_observer_);
146 } 175 }
147 176
148 TestConnectionTypeObserver connection_type_observer_; 177 NetworkChangeNotifierObserver connection_type_observer_;
149 TestConnectionTypeObserver other_connection_type_observer_; 178 NetworkChangeNotifierObserver other_connection_type_observer_;
150 NetworkChangeNotifier::DisableForTest disable_for_test_; 179 NetworkChangeNotifier::DisableForTest disable_for_test_;
151 NetworkChangeNotifierAndroid notifier_; 180 NetworkChangeNotifierAndroid notifier_;
152 }; 181 };
153 182
154 // When a NetworkChangeNotifierAndroid is observing a 183 // When a NetworkChangeNotifierAndroid is observing a
155 // NetworkChangeNotifierDelegateAndroid for network state changes, and the 184 // NetworkChangeNotifierDelegateAndroid for network state changes, and the
156 // NetworkChangeNotifierDelegateAndroid's connectivity state changes, the 185 // NetworkChangeNotifierDelegateAndroid's connectivity state changes, the
157 // NetworkChangeNotifierAndroid should reflect that state. 186 // NetworkChangeNotifierAndroid should reflect that state.
158 TEST_F(NetworkChangeNotifierAndroidTest, 187 TEST_F(NetworkChangeNotifierAndroidTest,
159 NotificationsSentToNetworkChangeNotifierAndroid) { 188 NotificationsSentToNetworkChangeNotifierAndroid) {
160 RunTest( 189 RunTest(
161 base::Bind( 190 base::Bind(
162 &TestConnectionTypeObserver::times_connection_type_changed, 191 &NetworkChangeNotifierObserver::notifications_count,
163 base::Unretained(&connection_type_observer_)), 192 base::Unretained(&connection_type_observer_)),
164 base::Bind( 193 base::Bind(
165 &NetworkChangeNotifierAndroid::GetCurrentConnectionType, 194 &NetworkChangeNotifierAndroid::GetCurrentConnectionType,
166 base::Unretained(&notifier_))); 195 base::Unretained(&notifier_)));
167 } 196 }
168 197
169 // When a NetworkChangeNotifierAndroid's connection state changes, it should 198 // When a NetworkChangeNotifierAndroid's connection state changes, it should
170 // notify all of its observers. 199 // notify all of its observers.
171 TEST_F(NetworkChangeNotifierAndroidTest, 200 TEST_F(NetworkChangeNotifierAndroidTest,
172 NotificationsSentToClientsOfNetworkChangeNotifier) { 201 NotificationsSentToClientsOfNetworkChangeNotifier) {
173 RunTest( 202 RunTest(
174 base::Bind( 203 base::Bind(
175 &TestConnectionTypeObserver::times_connection_type_changed, 204 &NetworkChangeNotifierObserver::notifications_count,
176 base::Unretained(&connection_type_observer_)), 205 base::Unretained(&connection_type_observer_)),
177 base::Bind( 206 base::Bind(&NetworkChangeNotifier::GetConnectionType));
178 &TestConnectionTypeObserver::current_connection,
179 base::Unretained(&connection_type_observer_)));
180 // Check that *all* the observers are notified. 207 // Check that *all* the observers are notified.
181 EXPECT_EQ(connection_type_observer_.times_connection_type_changed(), 208 EXPECT_EQ(connection_type_observer_.notifications_count(),
182 other_connection_type_observer_.times_connection_type_changed()); 209 other_connection_type_observer_.notifications_count());
183 EXPECT_EQ(connection_type_observer_.current_connection(),
184 other_connection_type_observer_.current_connection());
185 } 210 }
186 211
187 } // namespace net 212 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698