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
3 // found in the LICENSE file.
5 // See network_change_notifier_android.h for design explanations.
7 #include "base/basictypes.h"
9 #include "base/callback.h"
10 #include "base/compiler_specific.h"
11 #include "base/message_loop/message_loop.h"
12 #include "net/android/network_change_notifier_android.h"
13 #include "net/android/network_change_notifier_delegate_android.h"
14 #include "net/base/network_change_notifier.h"
15 #include "testing/gtest/include/gtest/gtest.h"
21 class NetworkChangeNotifierDelegateAndroidObserver
22 : public NetworkChangeNotifierDelegateAndroid::Observer
{
24 NetworkChangeNotifierDelegateAndroidObserver()
25 : type_notifications_count_(0), max_bandwidth_notifications_count_(0) {}
27 // NetworkChangeNotifierDelegateAndroid::Observer:
28 virtual void OnConnectionTypeChanged() override
{
29 type_notifications_count_
++;
32 virtual void OnMaxBandwidthChanged(double max_bandwidth_mbps
) override
{
33 max_bandwidth_notifications_count_
++;
36 int type_notifications_count() const { return type_notifications_count_
; }
37 int bandwidth_notifications_count() const {
38 return max_bandwidth_notifications_count_
;
42 int type_notifications_count_
;
43 int max_bandwidth_notifications_count_
;
46 class NetworkChangeNotifierObserver
47 : public NetworkChangeNotifier::ConnectionTypeObserver
{
49 NetworkChangeNotifierObserver() : notifications_count_(0) {}
51 // NetworkChangeNotifier::Observer:
52 virtual void OnConnectionTypeChanged(
53 NetworkChangeNotifier::ConnectionType connection_type
) override
{
54 notifications_count_
++;
57 int notifications_count() const {
58 return notifications_count_
;
62 int notifications_count_
;
67 class BaseNetworkChangeNotifierAndroidTest
: public testing::Test
{
69 typedef NetworkChangeNotifier::ConnectionType ConnectionType
;
71 virtual ~BaseNetworkChangeNotifierAndroidTest() {}
74 const base::Callback
<int(void)>& notifications_count_getter
,
75 const base::Callback
<ConnectionType(void)>& connection_type_getter
) {
76 EXPECT_EQ(0, notifications_count_getter
.Run());
77 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN
,
78 connection_type_getter
.Run());
80 // Changing from online to offline should trigger a notification.
82 EXPECT_EQ(1, notifications_count_getter
.Run());
83 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE
,
84 connection_type_getter
.Run());
86 // No notification should be triggered when the offline state hasn't
89 EXPECT_EQ(1, notifications_count_getter
.Run());
90 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE
,
91 connection_type_getter
.Run());
93 // Going from offline to online should trigger a notification.
95 EXPECT_EQ(2, notifications_count_getter
.Run());
96 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN
,
97 connection_type_getter
.Run());
101 delegate_
.SetOnline();
102 // Note that this is needed because ObserverListThreadSafe uses PostTask().
103 base::MessageLoop::current()->RunUntilIdle();
107 delegate_
.SetOffline();
108 // See comment above.
109 base::MessageLoop::current()->RunUntilIdle();
112 NetworkChangeNotifierDelegateAndroid delegate_
;
115 // Tests that NetworkChangeNotifierDelegateAndroid is initialized with the
116 // actual connection type rather than a hardcoded one (e.g.
117 // CONNECTION_UNKNOWN). Initializing the connection type to CONNECTION_UNKNOWN
118 // and relying on the first network change notification to set it correctly can
119 // be problematic in case there is a long delay between the delegate's
120 // construction and the notification.
121 TEST_F(BaseNetworkChangeNotifierAndroidTest
,
122 DelegateIsInitializedWithCurrentConnectionType
) {
124 ASSERT_EQ(NetworkChangeNotifier::CONNECTION_NONE
,
125 delegate_
.GetCurrentConnectionType());
126 // Instantiate another delegate to validate that it uses the actual
127 // connection type at construction.
128 scoped_ptr
<NetworkChangeNotifierDelegateAndroid
> other_delegate(
129 new NetworkChangeNotifierDelegateAndroid());
130 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE
,
131 other_delegate
->GetCurrentConnectionType());
133 // Toggle the global connectivity state and instantiate another delegate
136 ASSERT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN
,
137 delegate_
.GetCurrentConnectionType());
138 other_delegate
.reset(new NetworkChangeNotifierDelegateAndroid());
139 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN
,
140 other_delegate
->GetCurrentConnectionType());
143 class NetworkChangeNotifierDelegateAndroidTest
144 : public BaseNetworkChangeNotifierAndroidTest
{
146 NetworkChangeNotifierDelegateAndroidTest() {
147 delegate_
.AddObserver(&delegate_observer_
);
148 delegate_
.AddObserver(&other_delegate_observer_
);
151 virtual ~NetworkChangeNotifierDelegateAndroidTest() {
152 delegate_
.RemoveObserver(&delegate_observer_
);
153 delegate_
.RemoveObserver(&other_delegate_observer_
);
156 NetworkChangeNotifierDelegateAndroidObserver delegate_observer_
;
157 NetworkChangeNotifierDelegateAndroidObserver other_delegate_observer_
;
160 // Tests that the NetworkChangeNotifierDelegateAndroid's observers are notified.
161 // A testing-only observer is used here for testing. In production the
162 // delegate's observers are instances of NetworkChangeNotifierAndroid.
163 TEST_F(NetworkChangeNotifierDelegateAndroidTest
, DelegateObserverNotified
) {
164 // Test the logic with a single observer.
165 RunTest(base::Bind(&NetworkChangeNotifierDelegateAndroidObserver::
166 type_notifications_count
,
167 base::Unretained(&delegate_observer_
)),
169 &NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType
,
170 base::Unretained(&delegate_
)));
171 // Check that *all* the observers are notified. Both observers should have the
173 EXPECT_EQ(delegate_observer_
.type_notifications_count(),
174 other_delegate_observer_
.type_notifications_count());
177 class NetworkChangeNotifierAndroidTest
178 : public BaseNetworkChangeNotifierAndroidTest
{
180 NetworkChangeNotifierAndroidTest() : notifier_(&delegate_
) {
181 NetworkChangeNotifier::AddConnectionTypeObserver(
182 &connection_type_observer_
);
183 NetworkChangeNotifier::AddConnectionTypeObserver(
184 &other_connection_type_observer_
);
187 NetworkChangeNotifierObserver connection_type_observer_
;
188 NetworkChangeNotifierObserver other_connection_type_observer_
;
189 NetworkChangeNotifier::DisableForTest disable_for_test_
;
190 NetworkChangeNotifierAndroid notifier_
;
193 // When a NetworkChangeNotifierAndroid is observing a
194 // NetworkChangeNotifierDelegateAndroid for network state changes, and the
195 // NetworkChangeNotifierDelegateAndroid's connectivity state changes, the
196 // NetworkChangeNotifierAndroid should reflect that state.
197 TEST_F(NetworkChangeNotifierAndroidTest
,
198 NotificationsSentToNetworkChangeNotifierAndroid
) {
201 &NetworkChangeNotifierObserver::notifications_count
,
202 base::Unretained(&connection_type_observer_
)),
204 &NetworkChangeNotifierAndroid::GetCurrentConnectionType
,
205 base::Unretained(¬ifier_
)));
208 // When a NetworkChangeNotifierAndroid's connection state changes, it should
209 // notify all of its observers.
210 TEST_F(NetworkChangeNotifierAndroidTest
,
211 NotificationsSentToClientsOfNetworkChangeNotifier
) {
214 &NetworkChangeNotifierObserver::notifications_count
,
215 base::Unretained(&connection_type_observer_
)),
216 base::Bind(&NetworkChangeNotifier::GetConnectionType
));
217 // Check that *all* the observers are notified.
218 EXPECT_EQ(connection_type_observer_
.notifications_count(),
219 other_connection_type_observer_
.notifications_count());
222 TEST_F(NetworkChangeNotifierAndroidTest
, MaxBandwidth
) {
224 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN
,
225 notifier_
.GetConnectionType());
226 EXPECT_EQ(std::numeric_limits
<double>::infinity(),
227 notifier_
.GetMaxBandwidth());
229 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE
,
230 notifier_
.GetConnectionType());
231 EXPECT_EQ(0.0, notifier_
.GetMaxBandwidth());
234 TEST_F(NetworkChangeNotifierDelegateAndroidTest
,
235 MaxBandwidthNotifiedOnConnectionChange
) {
236 EXPECT_EQ(0, delegate_observer_
.bandwidth_notifications_count());
238 EXPECT_EQ(1, delegate_observer_
.bandwidth_notifications_count());
240 EXPECT_EQ(2, delegate_observer_
.bandwidth_notifications_count());
242 EXPECT_EQ(2, delegate_observer_
.bandwidth_notifications_count());