xref: /aosp_15_r20/external/cronet/net/android/network_change_notifier_android_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // See network_change_notifier_android.h for design explanations.
6 
7 #include "net/android/network_change_notifier_android.h"
8 
9 #include <memory>
10 
11 #include "base/compiler_specific.h"
12 #include "base/functional/bind.h"
13 #include "base/functional/callback.h"
14 #include "base/run_loop.h"
15 #include "net/android/network_change_notifier_delegate_android.h"
16 #include "net/base/ip_address.h"
17 #include "net/base/network_change_notifier.h"
18 #include "net/test/test_with_task_environment.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 
21 namespace net {
22 
23 namespace {
24 
25 // Types of network changes. See similarly named functions in
26 // NetworkChangeNotifier::NetworkObserver for descriptions.
27 enum ChangeType {
28   NONE,
29   CONNECTED,
30   SOON_TO_DISCONNECT,
31   DISCONNECTED,
32   MADE_DEFAULT,
33 };
34 
35 class NetworkChangeNotifierDelegateAndroidObserver
36     : public NetworkChangeNotifierDelegateAndroid::Observer {
37  public:
38   typedef NetworkChangeNotifier::ConnectionCost ConnectionCost;
39   typedef NetworkChangeNotifier::ConnectionType ConnectionType;
40   typedef NetworkChangeNotifier::NetworkList NetworkList;
41 
42   NetworkChangeNotifierDelegateAndroidObserver() = default;
43 
44   // NetworkChangeNotifierDelegateAndroid::Observer:
OnConnectionTypeChanged()45   void OnConnectionTypeChanged() override { type_notifications_count_++; }
46 
OnConnectionCostChanged()47   void OnConnectionCostChanged() override { cost_notifications_count_++; }
48 
OnMaxBandwidthChanged(double max_bandwidth_mbps,net::NetworkChangeNotifier::ConnectionType type)49   void OnMaxBandwidthChanged(
50       double max_bandwidth_mbps,
51       net::NetworkChangeNotifier::ConnectionType type) override {
52     max_bandwidth_notifications_count_++;
53   }
54 
OnNetworkConnected(handles::NetworkHandle network)55   void OnNetworkConnected(handles::NetworkHandle network) override {}
56 
OnNetworkSoonToDisconnect(handles::NetworkHandle network)57   void OnNetworkSoonToDisconnect(handles::NetworkHandle network) override {}
58 
OnNetworkDisconnected(handles::NetworkHandle network)59   void OnNetworkDisconnected(handles::NetworkHandle network) override {}
60 
OnNetworkMadeDefault(handles::NetworkHandle network)61   void OnNetworkMadeDefault(handles::NetworkHandle network) override {}
62 
OnDefaultNetworkActive()63   void OnDefaultNetworkActive() override {
64     default_network_active_notifications_count_++;
65   }
66 
type_notifications_count() const67   int type_notifications_count() const { return type_notifications_count_; }
cost_notifications_count() const68   int cost_notifications_count() const { return cost_notifications_count_; }
bandwidth_notifications_count() const69   int bandwidth_notifications_count() const {
70     return max_bandwidth_notifications_count_;
71   }
default_network_active_notifications_count() const72   int default_network_active_notifications_count() const {
73     return default_network_active_notifications_count_;
74   }
75 
76  private:
77   int type_notifications_count_ = 0;
78   int cost_notifications_count_ = 0;
79   int max_bandwidth_notifications_count_ = 0;
80   int default_network_active_notifications_count_ = 0;
81 };
82 
83 class NetworkChangeNotifierObserver
84     : public NetworkChangeNotifier::ConnectionTypeObserver {
85  public:
86   NetworkChangeNotifierObserver() = default;
87 
88   // NetworkChangeNotifier::ConnectionTypeObserver:
OnConnectionTypeChanged(NetworkChangeNotifier::ConnectionType connection_type)89   void OnConnectionTypeChanged(
90       NetworkChangeNotifier::ConnectionType connection_type) override {
91     notifications_count_++;
92   }
93 
notifications_count() const94   int notifications_count() const {
95     return notifications_count_;
96   }
97 
98  private:
99   int notifications_count_ = 0;
100 };
101 
102 class NetworkChangeNotifierConnectionCostObserver
103     : public NetworkChangeNotifier::ConnectionCostObserver {
104  public:
105   // NetworkChangeNotifier::ConnectionCostObserver:
OnConnectionCostChanged(NetworkChangeNotifier::ConnectionCost cost)106   void OnConnectionCostChanged(
107       NetworkChangeNotifier::ConnectionCost cost) override {
108     notifications_count_++;
109   }
110 
notifications_count() const111   int notifications_count() const { return notifications_count_; }
112 
113  private:
114   int notifications_count_ = 0;
115 };
116 
117 class NetworkChangeNotifierMaxBandwidthObserver
118     : public NetworkChangeNotifier::MaxBandwidthObserver {
119  public:
120   // NetworkChangeNotifier::MaxBandwidthObserver:
OnMaxBandwidthChanged(double max_bandwidth_mbps,NetworkChangeNotifier::ConnectionType type)121   void OnMaxBandwidthChanged(
122       double max_bandwidth_mbps,
123       NetworkChangeNotifier::ConnectionType type) override {
124     notifications_count_++;
125   }
126 
notifications_count() const127   int notifications_count() const { return notifications_count_; }
128 
129  private:
130   int notifications_count_ = 0;
131 };
132 
133 // A NetworkObserver used for verifying correct notifications are sent.
134 class TestNetworkObserver : public NetworkChangeNotifier::NetworkObserver {
135  public:
TestNetworkObserver()136   TestNetworkObserver() { Clear(); }
137 
ExpectChange(ChangeType change,handles::NetworkHandle network)138   void ExpectChange(ChangeType change, handles::NetworkHandle network) {
139     EXPECT_EQ(last_change_type_, change);
140     EXPECT_EQ(last_network_changed_, network);
141     Clear();
142   }
143 
144  private:
Clear()145   void Clear() {
146     last_change_type_ = NONE;
147     last_network_changed_ = handles::kInvalidNetworkHandle;
148   }
149 
150   // NetworkChangeNotifier::NetworkObserver implementation:
OnNetworkConnected(handles::NetworkHandle network)151   void OnNetworkConnected(handles::NetworkHandle network) override {
152     ExpectChange(NONE, handles::kInvalidNetworkHandle);
153     last_change_type_ = CONNECTED;
154     last_network_changed_ = network;
155   }
OnNetworkSoonToDisconnect(handles::NetworkHandle network)156   void OnNetworkSoonToDisconnect(handles::NetworkHandle network) override {
157     ExpectChange(NONE, handles::kInvalidNetworkHandle);
158     last_change_type_ = SOON_TO_DISCONNECT;
159     last_network_changed_ = network;
160   }
OnNetworkDisconnected(handles::NetworkHandle network)161   void OnNetworkDisconnected(handles::NetworkHandle network) override {
162     ExpectChange(NONE, handles::kInvalidNetworkHandle);
163     last_change_type_ = DISCONNECTED;
164     last_network_changed_ = network;
165   }
OnNetworkMadeDefault(handles::NetworkHandle network)166   void OnNetworkMadeDefault(handles::NetworkHandle network) override {
167     // Cannot test for Clear()ed state as we receive CONNECTED immediately prior
168     // to MADE_DEFAULT.
169     last_change_type_ = MADE_DEFAULT;
170     last_network_changed_ = network;
171   }
172 
173   ChangeType last_change_type_;
174   handles::NetworkHandle last_network_changed_;
175 };
176 
177 }  // namespace
178 
179 class BaseNetworkChangeNotifierAndroidTest : public TestWithTaskEnvironment {
180  protected:
181   typedef NetworkChangeNotifier::ConnectionType ConnectionType;
182   typedef NetworkChangeNotifier::ConnectionCost ConnectionCost;
183   typedef NetworkChangeNotifier::ConnectionSubtype ConnectionSubtype;
184 
185   ~BaseNetworkChangeNotifierAndroidTest() override = default;
186 
RunTest(const base::RepeatingCallback<int (void)> & notifications_count_getter,const base::RepeatingCallback<ConnectionType (void)> & connection_type_getter)187   void RunTest(
188       const base::RepeatingCallback<int(void)>& notifications_count_getter,
189       const base::RepeatingCallback<ConnectionType(void)>&
190           connection_type_getter) {
191     EXPECT_EQ(0, notifications_count_getter.Run());
192     EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN,
193               connection_type_getter.Run());
194 
195     // Changing from online to offline should trigger a notification.
196     SetOffline();
197     EXPECT_EQ(1, notifications_count_getter.Run());
198     EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
199               connection_type_getter.Run());
200 
201     // No notification should be triggered when the offline state hasn't
202     // changed.
203     SetOffline();
204     EXPECT_EQ(1, notifications_count_getter.Run());
205     EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
206               connection_type_getter.Run());
207 
208     // Going from offline to online should trigger a notification.
209     SetOnline();
210     EXPECT_EQ(2, notifications_count_getter.Run());
211     EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN,
212               connection_type_getter.Run());
213   }
214 
SetOnline(bool drain_run_loop=true)215   void SetOnline(bool drain_run_loop = true) {
216     delegate_.SetOnline();
217     if (drain_run_loop) {
218       // Note that this is needed because base::ObserverListThreadSafe uses
219       // PostTask().
220       base::RunLoop().RunUntilIdle();
221     }
222   }
223 
SetOffline(bool drain_run_loop=true)224   void SetOffline(bool drain_run_loop = true) {
225     delegate_.SetOffline();
226     if (drain_run_loop) {
227       // See comment above.
228       base::RunLoop().RunUntilIdle();
229     }
230   }
231 
FakeConnectionCostChange(ConnectionCost cost)232   void FakeConnectionCostChange(ConnectionCost cost) {
233     delegate_.FakeConnectionCostChanged(cost);
234     base::RunLoop().RunUntilIdle();
235   }
236 
FakeConnectionSubtypeChange(ConnectionSubtype subtype)237   void FakeConnectionSubtypeChange(ConnectionSubtype subtype) {
238     delegate_.FakeConnectionSubtypeChanged(subtype);
239     base::RunLoop().RunUntilIdle();
240   }
241 
FakeNetworkChange(ChangeType change,handles::NetworkHandle network,ConnectionType type)242   void FakeNetworkChange(ChangeType change,
243                          handles::NetworkHandle network,
244                          ConnectionType type) {
245     switch (change) {
246       case CONNECTED:
247         delegate_.FakeNetworkConnected(network, type);
248         break;
249       case SOON_TO_DISCONNECT:
250         delegate_.FakeNetworkSoonToBeDisconnected(network);
251         break;
252       case DISCONNECTED:
253         delegate_.FakeNetworkDisconnected(network);
254         break;
255       case MADE_DEFAULT:
256         delegate_.FakeDefaultNetwork(network, type);
257         break;
258       case NONE:
259         NOTREACHED();
260         break;
261     }
262     // See comment above.
263     base::RunLoop().RunUntilIdle();
264   }
265 
FakeDefaultNetworkActive()266   void FakeDefaultNetworkActive() {
267     delegate_.FakeDefaultNetworkActive();
268     // See comment above.
269     base::RunLoop().RunUntilIdle();
270   }
271 
FakePurgeActiveNetworkList(NetworkChangeNotifier::NetworkList networks)272   void FakePurgeActiveNetworkList(NetworkChangeNotifier::NetworkList networks) {
273     delegate_.FakePurgeActiveNetworkList(networks);
274     // See comment above.
275     base::RunLoop().RunUntilIdle();
276   }
277 
278   NetworkChangeNotifierDelegateAndroid delegate_;
279 };
280 
281 // Tests that NetworkChangeNotifierDelegateAndroid is initialized with the
282 // actual connection type rather than a hardcoded one (e.g.
283 // CONNECTION_UNKNOWN). Initializing the connection type to CONNECTION_UNKNOWN
284 // and relying on the first network change notification to set it correctly can
285 // be problematic in case there is a long delay between the delegate's
286 // construction and the notification.
TEST_F(BaseNetworkChangeNotifierAndroidTest,DelegateIsInitializedWithCurrentConnectionType)287 TEST_F(BaseNetworkChangeNotifierAndroidTest,
288        DelegateIsInitializedWithCurrentConnectionType) {
289   SetOffline();
290   ASSERT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
291             delegate_.GetCurrentConnectionType());
292   // Instantiate another delegate to validate that it uses the actual
293   // connection type at construction.
294   auto other_delegate =
295       std::make_unique<NetworkChangeNotifierDelegateAndroid>();
296   EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
297             other_delegate->GetCurrentConnectionType());
298 
299   // Toggle the global connectivity state and instantiate another delegate
300   // again.
301   SetOnline();
302   ASSERT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN,
303             delegate_.GetCurrentConnectionType());
304   other_delegate = std::make_unique<NetworkChangeNotifierDelegateAndroid>();
305   EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN,
306             other_delegate->GetCurrentConnectionType());
307 }
308 
309 class NetworkChangeNotifierAndroidTest
310     : public BaseNetworkChangeNotifierAndroidTest {
311  protected:
NetworkChangeNotifierAndroidTest()312   NetworkChangeNotifierAndroidTest() : notifier_(&delegate_) {
313     NetworkChangeNotifier::AddConnectionTypeObserver(
314         &connection_type_observer_);
315     NetworkChangeNotifier::AddConnectionTypeObserver(
316         &other_connection_type_observer_);
317     NetworkChangeNotifier::AddConnectionCostObserver(
318         &connection_cost_observer_);
319     NetworkChangeNotifier::AddMaxBandwidthObserver(&max_bandwidth_observer_);
320   }
321 
ForceNetworkHandlesSupportedForTesting()322   void ForceNetworkHandlesSupportedForTesting() {
323     notifier_.ForceNetworkHandlesSupportedForTesting();
324   }
325 
326   NetworkChangeNotifierObserver connection_type_observer_;
327   NetworkChangeNotifierConnectionCostObserver connection_cost_observer_;
328   NetworkChangeNotifierMaxBandwidthObserver max_bandwidth_observer_;
329   NetworkChangeNotifierObserver other_connection_type_observer_;
330   NetworkChangeNotifier::DisableForTest disable_for_test_;
331   NetworkChangeNotifierAndroid notifier_;
332 };
333 
334 class NetworkChangeNotifierDelegateAndroidTest
335     : public BaseNetworkChangeNotifierAndroidTest {
336  protected:
NetworkChangeNotifierDelegateAndroidTest()337   NetworkChangeNotifierDelegateAndroidTest() {
338     delegate_.RegisterObserver(&delegate_observer_);
339   }
340 
~NetworkChangeNotifierDelegateAndroidTest()341   ~NetworkChangeNotifierDelegateAndroidTest() override {
342     delegate_.UnregisterObserver(&delegate_observer_);
343   }
344 
345   NetworkChangeNotifierDelegateAndroidObserver delegate_observer_;
346 };
347 
348 // Tests that the NetworkChangeNotifierDelegateAndroid's observer is notified.
349 // A testing-only observer is used here for testing. In production the
350 // delegate's observers are instances of NetworkChangeNotifierAndroid.
TEST_F(NetworkChangeNotifierDelegateAndroidTest,DelegateObserverNotified)351 TEST_F(NetworkChangeNotifierDelegateAndroidTest, DelegateObserverNotified) {
352   RunTest(base::BindRepeating(&NetworkChangeNotifierDelegateAndroidObserver::
353                                   type_notifications_count,
354                               base::Unretained(&delegate_observer_)),
355           base::BindRepeating(
356               &NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType,
357               base::Unretained(&delegate_)));
358 }
359 
360 // When a NetworkChangeNotifierAndroid is observing a
361 // NetworkChangeNotifierDelegateAndroid for network state changes, and the
362 // NetworkChangeNotifierDelegateAndroid's connectivity state changes, the
363 // NetworkChangeNotifierAndroid should reflect that state.
TEST_F(NetworkChangeNotifierAndroidTest,DISABLED_NotificationsSentToNetworkChangeNotifierAndroid)364 TEST_F(NetworkChangeNotifierAndroidTest,
365        DISABLED_NotificationsSentToNetworkChangeNotifierAndroid) {
366   RunTest(
367       base::BindRepeating(&NetworkChangeNotifierObserver::notifications_count,
368                           base::Unretained(&connection_type_observer_)),
369       base::BindRepeating(
370           &NetworkChangeNotifierAndroid::GetCurrentConnectionType,
371           base::Unretained(&notifier_)));
372 }
373 
374 // When a NetworkChangeNotifierAndroid's connection state changes, it should
375 // notify all of its observers.
TEST_F(NetworkChangeNotifierAndroidTest,DISABLED_NotificationsSentToClientsOfNetworkChangeNotifier)376 TEST_F(NetworkChangeNotifierAndroidTest,
377        DISABLED_NotificationsSentToClientsOfNetworkChangeNotifier) {
378   RunTest(
379       base::BindRepeating(&NetworkChangeNotifierObserver::notifications_count,
380                           base::Unretained(&connection_type_observer_)),
381       base::BindRepeating(&NetworkChangeNotifier::GetConnectionType));
382   // Check that *all* the observers are notified.
383   EXPECT_EQ(connection_type_observer_.notifications_count(),
384             other_connection_type_observer_.notifications_count());
385 }
386 
TEST_F(NetworkChangeNotifierAndroidTest,ConnectionCost)387 TEST_F(NetworkChangeNotifierAndroidTest, ConnectionCost) {
388   FakeConnectionCostChange(ConnectionCost::CONNECTION_COST_UNMETERED);
389   EXPECT_EQ(NetworkChangeNotifier::CONNECTION_COST_UNMETERED,
390             notifier_.GetConnectionCost());
391   FakeConnectionCostChange(ConnectionCost::CONNECTION_COST_METERED);
392   EXPECT_EQ(NetworkChangeNotifier::CONNECTION_COST_METERED,
393             notifier_.GetConnectionCost());
394 }
395 
TEST_F(NetworkChangeNotifierAndroidTest,ConnectionCostCallbackNotifier)396 TEST_F(NetworkChangeNotifierAndroidTest, ConnectionCostCallbackNotifier) {
397   FakeConnectionCostChange(ConnectionCost::CONNECTION_COST_UNMETERED);
398   EXPECT_EQ(1, connection_cost_observer_.notifications_count());
399 
400   FakeConnectionCostChange(ConnectionCost::CONNECTION_COST_METERED);
401   EXPECT_EQ(2, connection_cost_observer_.notifications_count());
402 }
403 
TEST_F(NetworkChangeNotifierDelegateAndroidTest,ConnectionCostCallbackNotifier)404 TEST_F(NetworkChangeNotifierDelegateAndroidTest,
405        ConnectionCostCallbackNotifier) {
406   EXPECT_EQ(0, delegate_observer_.cost_notifications_count());
407 
408   FakeConnectionCostChange(ConnectionCost::CONNECTION_COST_UNMETERED);
409   EXPECT_EQ(1, delegate_observer_.cost_notifications_count());
410 
411   FakeConnectionCostChange(ConnectionCost::CONNECTION_COST_METERED);
412   EXPECT_EQ(2, delegate_observer_.cost_notifications_count());
413 }
414 
TEST_F(NetworkChangeNotifierAndroidTest,MaxBandwidth)415 TEST_F(NetworkChangeNotifierAndroidTest, MaxBandwidth) {
416   SetOnline();
417   double max_bandwidth_mbps = 0.0;
418   NetworkChangeNotifier::ConnectionType connection_type =
419       NetworkChangeNotifier::CONNECTION_NONE;
420   notifier_.GetMaxBandwidthAndConnectionType(&max_bandwidth_mbps,
421                                              &connection_type);
422   EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, connection_type);
423   EXPECT_EQ(std::numeric_limits<double>::infinity(), max_bandwidth_mbps);
424   SetOffline();
425   notifier_.GetMaxBandwidthAndConnectionType(&max_bandwidth_mbps,
426                                              &connection_type);
427   EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, connection_type);
428   EXPECT_EQ(0.0, max_bandwidth_mbps);
429 }
430 
TEST_F(NetworkChangeNotifierAndroidTest,MaxBandwidthCallbackNotifier)431 TEST_F(NetworkChangeNotifierAndroidTest, MaxBandwidthCallbackNotifier) {
432   // The bandwidth notification should always be forwarded, even if the value
433   // doesn't change (because the type might have changed).
434   FakeConnectionSubtypeChange(ConnectionSubtype::SUBTYPE_CDMA);
435   EXPECT_EQ(1, max_bandwidth_observer_.notifications_count());
436 
437   FakeConnectionSubtypeChange(ConnectionSubtype::SUBTYPE_CDMA);
438   EXPECT_EQ(2, max_bandwidth_observer_.notifications_count());
439 
440   FakeConnectionSubtypeChange(ConnectionSubtype::SUBTYPE_LTE);
441   EXPECT_EQ(3, max_bandwidth_observer_.notifications_count());
442 }
443 
TEST_F(NetworkChangeNotifierDelegateAndroidTest,MaxBandwidthNotifiedOnConnectionChange)444 TEST_F(NetworkChangeNotifierDelegateAndroidTest,
445        MaxBandwidthNotifiedOnConnectionChange) {
446   EXPECT_EQ(0, delegate_observer_.bandwidth_notifications_count());
447   SetOffline();
448   EXPECT_EQ(1, delegate_observer_.bandwidth_notifications_count());
449   SetOnline();
450   EXPECT_EQ(2, delegate_observer_.bandwidth_notifications_count());
451   SetOnline();
452   EXPECT_EQ(2, delegate_observer_.bandwidth_notifications_count());
453 }
454 
TEST_F(NetworkChangeNotifierAndroidTest,NetworkCallbacks)455 TEST_F(NetworkChangeNotifierAndroidTest, NetworkCallbacks) {
456   ForceNetworkHandlesSupportedForTesting();
457 
458   TestNetworkObserver network_observer;
459   NetworkChangeNotifier::AddNetworkObserver(&network_observer);
460 
461   // Test empty values
462   EXPECT_EQ(handles::kInvalidNetworkHandle,
463             NetworkChangeNotifier::GetDefaultNetwork());
464   EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN,
465             NetworkChangeNotifier::GetNetworkConnectionType(100));
466   NetworkChangeNotifier::NetworkList network_list;
467   NetworkChangeNotifier::GetConnectedNetworks(&network_list);
468   EXPECT_EQ(0u, network_list.size());
469   // Test connecting network
470   FakeNetworkChange(CONNECTED, 100, NetworkChangeNotifier::CONNECTION_WIFI);
471   network_observer.ExpectChange(CONNECTED, 100);
472   EXPECT_EQ(handles::kInvalidNetworkHandle,
473             NetworkChangeNotifier::GetDefaultNetwork());
474   // Test GetConnectedNetworks()
475   NetworkChangeNotifier::GetConnectedNetworks(&network_list);
476   EXPECT_EQ(1u, network_list.size());
477   EXPECT_EQ(100, network_list[0]);
478   // Test GetNetworkConnectionType()
479   EXPECT_EQ(NetworkChangeNotifier::CONNECTION_WIFI,
480             NetworkChangeNotifier::GetNetworkConnectionType(100));
481   // Test deduplication of connecting signal
482   FakeNetworkChange(CONNECTED, 100, NetworkChangeNotifier::CONNECTION_WIFI);
483   network_observer.ExpectChange(NONE, handles::kInvalidNetworkHandle);
484   // Test connecting another network
485   FakeNetworkChange(CONNECTED, 101, NetworkChangeNotifier::CONNECTION_3G);
486   network_observer.ExpectChange(CONNECTED, 101);
487   NetworkChangeNotifier::GetConnectedNetworks(&network_list);
488   EXPECT_EQ(2u, network_list.size());
489   EXPECT_EQ(100, network_list[0]);
490   EXPECT_EQ(101, network_list[1]);
491   EXPECT_EQ(NetworkChangeNotifier::CONNECTION_WIFI,
492             NetworkChangeNotifier::GetNetworkConnectionType(100));
493   EXPECT_EQ(NetworkChangeNotifier::CONNECTION_3G,
494             NetworkChangeNotifier::GetNetworkConnectionType(101));
495   // Test lingering network
496   FakeNetworkChange(SOON_TO_DISCONNECT, 100,
497                     NetworkChangeNotifier::CONNECTION_WIFI);
498   network_observer.ExpectChange(SOON_TO_DISCONNECT, 100);
499   NetworkChangeNotifier::GetConnectedNetworks(&network_list);
500   EXPECT_EQ(2u, network_list.size());
501   EXPECT_EQ(100, network_list[0]);
502   EXPECT_EQ(101, network_list[1]);
503   // Test disconnecting network
504   FakeNetworkChange(DISCONNECTED, 100, NetworkChangeNotifier::CONNECTION_WIFI);
505   network_observer.ExpectChange(DISCONNECTED, 100);
506   NetworkChangeNotifier::GetConnectedNetworks(&network_list);
507   EXPECT_EQ(1u, network_list.size());
508   EXPECT_EQ(101, network_list[0]);
509   // Test deduplication of disconnecting signal
510   FakeNetworkChange(DISCONNECTED, 100, NetworkChangeNotifier::CONNECTION_WIFI);
511   network_observer.ExpectChange(NONE, handles::kInvalidNetworkHandle);
512   // Test delay of default network signal until connect signal
513   FakeNetworkChange(MADE_DEFAULT, 100, NetworkChangeNotifier::CONNECTION_WIFI);
514   network_observer.ExpectChange(NONE, handles::kInvalidNetworkHandle);
515   FakeNetworkChange(CONNECTED, 100, NetworkChangeNotifier::CONNECTION_WIFI);
516   network_observer.ExpectChange(MADE_DEFAULT, 100);
517   EXPECT_EQ(100, NetworkChangeNotifier::GetDefaultNetwork());
518   // Test change of default
519   FakeNetworkChange(MADE_DEFAULT, 101, NetworkChangeNotifier::CONNECTION_3G);
520   network_observer.ExpectChange(MADE_DEFAULT, 101);
521   EXPECT_EQ(101, NetworkChangeNotifier::GetDefaultNetwork());
522   // Test deduplication default signal
523   FakeNetworkChange(MADE_DEFAULT, 101, NetworkChangeNotifier::CONNECTION_3G);
524   network_observer.ExpectChange(NONE, handles::kInvalidNetworkHandle);
525   // Test that networks can change type
526   FakeNetworkChange(CONNECTED, 101, NetworkChangeNotifier::CONNECTION_4G);
527   network_observer.ExpectChange(NONE, handles::kInvalidNetworkHandle);
528   EXPECT_EQ(NetworkChangeNotifier::CONNECTION_4G,
529             NetworkChangeNotifier::GetNetworkConnectionType(101));
530   // Test purging the network list
531   NetworkChangeNotifier::GetConnectedNetworks(&network_list);
532   EXPECT_EQ(2u, network_list.size());
533   EXPECT_EQ(100, network_list[0]);
534   EXPECT_EQ(101, network_list[1]);
535   network_list.erase(network_list.begin() + 1);  // Remove network 101
536   FakePurgeActiveNetworkList(network_list);
537   network_observer.ExpectChange(DISCONNECTED, 101);
538   NetworkChangeNotifier::GetConnectedNetworks(&network_list);
539   EXPECT_EQ(1u, network_list.size());
540   EXPECT_EQ(100, network_list[0]);
541   EXPECT_EQ(handles::kInvalidNetworkHandle,
542             NetworkChangeNotifier::GetDefaultNetwork());
543 
544   NetworkChangeNotifier::RemoveNetworkObserver(&network_observer);
545 }
546 
547 // Tests that network type changes happen synchronously. Otherwise the type
548 // "change" at browser startup leaves tasks on the queue that will later
549 // invalidate any network requests that have been started.
TEST_F(NetworkChangeNotifierDelegateAndroidTest,TypeChangeIsSynchronous)550 TEST_F(NetworkChangeNotifierDelegateAndroidTest, TypeChangeIsSynchronous) {
551   const int initial_value = delegate_observer_.type_notifications_count();
552   SetOffline(/*drain_run_loop=*/false);
553   // Note that there's no call to |base::RunLoop::RunUntilIdle| here. The
554   // update must happen synchronously.
555   EXPECT_EQ(initial_value + 1, delegate_observer_.type_notifications_count());
556 }
557 
TEST_F(NetworkChangeNotifierDelegateAndroidTest,DefaultNetworkActive)558 TEST_F(NetworkChangeNotifierDelegateAndroidTest, DefaultNetworkActive) {
559   // No notifications should be received when there are no observers.
560   EXPECT_EQ(0, delegate_observer_.default_network_active_notifications_count());
561   FakeDefaultNetworkActive();
562   EXPECT_EQ(0, delegate_observer_.default_network_active_notifications_count());
563 
564   // Simulate calls to NetworkChangeNotifier::AddDefaultNetworkObserver().
565   // Notifications should be received now.
566   delegate_.DefaultNetworkActiveObserverAdded();
567   FakeDefaultNetworkActive();
568   EXPECT_EQ(1, delegate_observer_.default_network_active_notifications_count());
569   delegate_.DefaultNetworkActiveObserverAdded();
570   FakeDefaultNetworkActive();
571   EXPECT_EQ(2, delegate_observer_.default_network_active_notifications_count());
572 
573   // Simulate call to NetworkChangeNotifier::AddDefaultNetworkObserver().
574   // Notifications should be received until the last observer has been
575   // removed.
576   delegate_.DefaultNetworkActiveObserverRemoved();
577   FakeDefaultNetworkActive();
578   EXPECT_EQ(3, delegate_observer_.default_network_active_notifications_count());
579   delegate_.DefaultNetworkActiveObserverRemoved();
580   FakeDefaultNetworkActive();
581   EXPECT_EQ(3, delegate_observer_.default_network_active_notifications_count());
582 
583   // Double check that things keep working as expected after re-adding an
584   // observer.
585   delegate_.DefaultNetworkActiveObserverAdded();
586   FakeDefaultNetworkActive();
587   EXPECT_EQ(4, delegate_observer_.default_network_active_notifications_count());
588 
589   // Cleanup: delegate destructor DCHECKS that all observers have been
590   // removed.
591   delegate_.DefaultNetworkActiveObserverRemoved();
592 }
593 
594 }  // namespace net
595