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 void OnConnectionTypeChanged() override
{ type_notifications_count_
++; }
30 void OnMaxBandwidthChanged(double max_bandwidth_mbps
) override
{
31 max_bandwidth_notifications_count_
++;
34 int type_notifications_count() const { return type_notifications_count_
; }
35 int bandwidth_notifications_count() const {
36 return max_bandwidth_notifications_count_
;
40 int type_notifications_count_
;
41 int max_bandwidth_notifications_count_
;
44 class NetworkChangeNotifierObserver
45 : public NetworkChangeNotifier::ConnectionTypeObserver
{
47 NetworkChangeNotifierObserver() : notifications_count_(0) {}
49 // NetworkChangeNotifier::Observer:
50 void OnConnectionTypeChanged(
51 NetworkChangeNotifier::ConnectionType connection_type
) override
{
52 notifications_count_
++;
55 int notifications_count() const {
56 return notifications_count_
;
60 int notifications_count_
;
65 class BaseNetworkChangeNotifierAndroidTest
: public testing::Test
{
67 typedef NetworkChangeNotifier::ConnectionType ConnectionType
;
69 ~BaseNetworkChangeNotifierAndroidTest() override
{}
72 const base::Callback
<int(void)>& notifications_count_getter
,
73 const base::Callback
<ConnectionType(void)>& connection_type_getter
) {
74 EXPECT_EQ(0, notifications_count_getter
.Run());
75 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN
,
76 connection_type_getter
.Run());
78 // Changing from online to offline should trigger a notification.
80 EXPECT_EQ(1, notifications_count_getter
.Run());
81 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE
,
82 connection_type_getter
.Run());
84 // No notification should be triggered when the offline state hasn't
87 EXPECT_EQ(1, notifications_count_getter
.Run());
88 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE
,
89 connection_type_getter
.Run());
91 // Going from offline to online should trigger a notification.
93 EXPECT_EQ(2, notifications_count_getter
.Run());
94 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN
,
95 connection_type_getter
.Run());
99 delegate_
.SetOnline();
100 // Note that this is needed because ObserverListThreadSafe uses PostTask().
101 base::MessageLoop::current()->RunUntilIdle();
105 delegate_
.SetOffline();
106 // See comment above.
107 base::MessageLoop::current()->RunUntilIdle();
110 NetworkChangeNotifierDelegateAndroid delegate_
;
113 // Tests that NetworkChangeNotifierDelegateAndroid is initialized with the
114 // actual connection type rather than a hardcoded one (e.g.
115 // CONNECTION_UNKNOWN). Initializing the connection type to CONNECTION_UNKNOWN
116 // and relying on the first network change notification to set it correctly can
117 // be problematic in case there is a long delay between the delegate's
118 // construction and the notification.
119 TEST_F(BaseNetworkChangeNotifierAndroidTest
,
120 DelegateIsInitializedWithCurrentConnectionType
) {
122 ASSERT_EQ(NetworkChangeNotifier::CONNECTION_NONE
,
123 delegate_
.GetCurrentConnectionType());
124 // Instantiate another delegate to validate that it uses the actual
125 // connection type at construction.
126 scoped_ptr
<NetworkChangeNotifierDelegateAndroid
> other_delegate(
127 new NetworkChangeNotifierDelegateAndroid());
128 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE
,
129 other_delegate
->GetCurrentConnectionType());
131 // Toggle the global connectivity state and instantiate another delegate
134 ASSERT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN
,
135 delegate_
.GetCurrentConnectionType());
136 other_delegate
.reset(new NetworkChangeNotifierDelegateAndroid());
137 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN
,
138 other_delegate
->GetCurrentConnectionType());
141 class NetworkChangeNotifierDelegateAndroidTest
142 : public BaseNetworkChangeNotifierAndroidTest
{
144 NetworkChangeNotifierDelegateAndroidTest() {
145 delegate_
.AddObserver(&delegate_observer_
);
146 delegate_
.AddObserver(&other_delegate_observer_
);
149 ~NetworkChangeNotifierDelegateAndroidTest() override
{
150 delegate_
.RemoveObserver(&delegate_observer_
);
151 delegate_
.RemoveObserver(&other_delegate_observer_
);
154 NetworkChangeNotifierDelegateAndroidObserver delegate_observer_
;
155 NetworkChangeNotifierDelegateAndroidObserver other_delegate_observer_
;
158 // Tests that the NetworkChangeNotifierDelegateAndroid's observers are notified.
159 // A testing-only observer is used here for testing. In production the
160 // delegate's observers are instances of NetworkChangeNotifierAndroid.
161 TEST_F(NetworkChangeNotifierDelegateAndroidTest
, DelegateObserverNotified
) {
162 // Test the logic with a single observer.
163 RunTest(base::Bind(&NetworkChangeNotifierDelegateAndroidObserver::
164 type_notifications_count
,
165 base::Unretained(&delegate_observer_
)),
167 &NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType
,
168 base::Unretained(&delegate_
)));
169 // Check that *all* the observers are notified. Both observers should have the
171 EXPECT_EQ(delegate_observer_
.type_notifications_count(),
172 other_delegate_observer_
.type_notifications_count());
175 class NetworkChangeNotifierAndroidTest
176 : public BaseNetworkChangeNotifierAndroidTest
{
178 NetworkChangeNotifierAndroidTest() : notifier_(&delegate_
) {
179 NetworkChangeNotifier::AddConnectionTypeObserver(
180 &connection_type_observer_
);
181 NetworkChangeNotifier::AddConnectionTypeObserver(
182 &other_connection_type_observer_
);
185 NetworkChangeNotifierObserver connection_type_observer_
;
186 NetworkChangeNotifierObserver other_connection_type_observer_
;
187 NetworkChangeNotifier::DisableForTest disable_for_test_
;
188 NetworkChangeNotifierAndroid notifier_
;
191 // When a NetworkChangeNotifierAndroid is observing a
192 // NetworkChangeNotifierDelegateAndroid for network state changes, and the
193 // NetworkChangeNotifierDelegateAndroid's connectivity state changes, the
194 // NetworkChangeNotifierAndroid should reflect that state.
195 TEST_F(NetworkChangeNotifierAndroidTest
,
196 NotificationsSentToNetworkChangeNotifierAndroid
) {
199 &NetworkChangeNotifierObserver::notifications_count
,
200 base::Unretained(&connection_type_observer_
)),
202 &NetworkChangeNotifierAndroid::GetCurrentConnectionType
,
203 base::Unretained(¬ifier_
)));
206 // When a NetworkChangeNotifierAndroid's connection state changes, it should
207 // notify all of its observers.
208 TEST_F(NetworkChangeNotifierAndroidTest
,
209 NotificationsSentToClientsOfNetworkChangeNotifier
) {
212 &NetworkChangeNotifierObserver::notifications_count
,
213 base::Unretained(&connection_type_observer_
)),
214 base::Bind(&NetworkChangeNotifier::GetConnectionType
));
215 // Check that *all* the observers are notified.
216 EXPECT_EQ(connection_type_observer_
.notifications_count(),
217 other_connection_type_observer_
.notifications_count());
220 TEST_F(NetworkChangeNotifierAndroidTest
, MaxBandwidth
) {
222 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN
,
223 notifier_
.GetConnectionType());
224 EXPECT_EQ(std::numeric_limits
<double>::infinity(),
225 notifier_
.GetMaxBandwidth());
227 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE
,
228 notifier_
.GetConnectionType());
229 EXPECT_EQ(0.0, notifier_
.GetMaxBandwidth());
232 TEST_F(NetworkChangeNotifierDelegateAndroidTest
,
233 MaxBandwidthNotifiedOnConnectionChange
) {
234 EXPECT_EQ(0, delegate_observer_
.bandwidth_notifications_count());
236 EXPECT_EQ(1, delegate_observer_
.bandwidth_notifications_count());
238 EXPECT_EQ(2, delegate_observer_
.bandwidth_notifications_count());
240 EXPECT_EQ(2, delegate_observer_
.bandwidth_notifications_count());