• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     }
261     // See comment above.
262     base::RunLoop().RunUntilIdle();
263   }
264 
FakeDefaultNetworkActive()265   void FakeDefaultNetworkActive() {
266     delegate_.FakeDefaultNetworkActive();
267     // See comment above.
268     base::RunLoop().RunUntilIdle();
269   }
270 
FakePurgeActiveNetworkList(NetworkChangeNotifier::NetworkList networks)271   void FakePurgeActiveNetworkList(NetworkChangeNotifier::NetworkList networks) {
272     delegate_.FakePurgeActiveNetworkList(networks);
273     // See comment above.
274     base::RunLoop().RunUntilIdle();
275   }
276 
277   NetworkChangeNotifierDelegateAndroid delegate_;
278 };
279 
280 // Tests that NetworkChangeNotifierDelegateAndroid is initialized with the
281 // actual connection type rather than a hardcoded one (e.g.
282 // CONNECTION_UNKNOWN). Initializing the connection type to CONNECTION_UNKNOWN
283 // and relying on the first network change notification to set it correctly can
284 // be problematic in case there is a long delay between the delegate's
285 // construction and the notification.
TEST_F(BaseNetworkChangeNotifierAndroidTest,DelegateIsInitializedWithCurrentConnectionType)286 TEST_F(BaseNetworkChangeNotifierAndroidTest,
287        DelegateIsInitializedWithCurrentConnectionType) {
288   SetOffline();
289   ASSERT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
290             delegate_.GetCurrentConnectionType());
291   // Instantiate another delegate to validate that it uses the actual
292   // connection type at construction.
293   auto other_delegate =
294       std::make_unique<NetworkChangeNotifierDelegateAndroid>();
295   EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
296             other_delegate->GetCurrentConnectionType());
297 
298   // Toggle the global connectivity state and instantiate another delegate
299   // again.
300   SetOnline();
301   ASSERT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN,
302             delegate_.GetCurrentConnectionType());
303   other_delegate = std::make_unique<NetworkChangeNotifierDelegateAndroid>();
304   EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN,
305             other_delegate->GetCurrentConnectionType());
306 }
307 
308 class NetworkChangeNotifierAndroidTest
309     : public BaseNetworkChangeNotifierAndroidTest {
310  protected:
NetworkChangeNotifierAndroidTest()311   NetworkChangeNotifierAndroidTest() : notifier_(&delegate_) {
312     NetworkChangeNotifier::AddConnectionTypeObserver(
313         &connection_type_observer_);
314     NetworkChangeNotifier::AddConnectionTypeObserver(
315         &other_connection_type_observer_);
316     NetworkChangeNotifier::AddConnectionCostObserver(
317         &connection_cost_observer_);
318     NetworkChangeNotifier::AddMaxBandwidthObserver(&max_bandwidth_observer_);
319   }
320 
ForceNetworkHandlesSupportedForTesting()321   void ForceNetworkHandlesSupportedForTesting() {
322     notifier_.ForceNetworkHandlesSupportedForTesting();
323   }
324 
325   NetworkChangeNotifierObserver connection_type_observer_;
326   NetworkChangeNotifierConnectionCostObserver connection_cost_observer_;
327   NetworkChangeNotifierMaxBandwidthObserver max_bandwidth_observer_;
328   NetworkChangeNotifierObserver other_connection_type_observer_;
329   NetworkChangeNotifier::DisableForTest disable_for_test_;
330   NetworkChangeNotifierAndroid notifier_;
331 };
332 
333 class NetworkChangeNotifierDelegateAndroidTest
334     : public BaseNetworkChangeNotifierAndroidTest {
335  protected:
NetworkChangeNotifierDelegateAndroidTest()336   NetworkChangeNotifierDelegateAndroidTest() {
337     delegate_.RegisterObserver(&delegate_observer_);
338   }
339 
~NetworkChangeNotifierDelegateAndroidTest()340   ~NetworkChangeNotifierDelegateAndroidTest() override {
341     delegate_.UnregisterObserver(&delegate_observer_);
342   }
343 
344   NetworkChangeNotifierDelegateAndroidObserver delegate_observer_;
345 };
346 
347 // Tests that the NetworkChangeNotifierDelegateAndroid's observer is notified.
348 // A testing-only observer is used here for testing. In production the
349 // delegate's observers are instances of NetworkChangeNotifierAndroid.
TEST_F(NetworkChangeNotifierDelegateAndroidTest,DelegateObserverNotified)350 TEST_F(NetworkChangeNotifierDelegateAndroidTest, DelegateObserverNotified) {
351   RunTest(base::BindRepeating(&NetworkChangeNotifierDelegateAndroidObserver::
352                                   type_notifications_count,
353                               base::Unretained(&delegate_observer_)),
354           base::BindRepeating(
355               &NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType,
356               base::Unretained(&delegate_)));
357 }
358 
359 // When a NetworkChangeNotifierAndroid is observing a
360 // NetworkChangeNotifierDelegateAndroid for network state changes, and the
361 // NetworkChangeNotifierDelegateAndroid's connectivity state changes, the
362 // NetworkChangeNotifierAndroid should reflect that state.
TEST_F(NetworkChangeNotifierAndroidTest,DISABLED_NotificationsSentToNetworkChangeNotifierAndroid)363 TEST_F(NetworkChangeNotifierAndroidTest,
364        DISABLED_NotificationsSentToNetworkChangeNotifierAndroid) {
365   RunTest(
366       base::BindRepeating(&NetworkChangeNotifierObserver::notifications_count,
367                           base::Unretained(&connection_type_observer_)),
368       base::BindRepeating(
369           &NetworkChangeNotifierAndroid::GetCurrentConnectionType,
370           base::Unretained(&notifier_)));
371 }
372 
373 // When a NetworkChangeNotifierAndroid's connection state changes, it should
374 // notify all of its observers.
TEST_F(NetworkChangeNotifierAndroidTest,DISABLED_NotificationsSentToClientsOfNetworkChangeNotifier)375 TEST_F(NetworkChangeNotifierAndroidTest,
376        DISABLED_NotificationsSentToClientsOfNetworkChangeNotifier) {
377   RunTest(
378       base::BindRepeating(&NetworkChangeNotifierObserver::notifications_count,
379                           base::Unretained(&connection_type_observer_)),
380       base::BindRepeating(&NetworkChangeNotifier::GetConnectionType));
381   // Check that *all* the observers are notified.
382   EXPECT_EQ(connection_type_observer_.notifications_count(),
383             other_connection_type_observer_.notifications_count());
384 }
385 
TEST_F(NetworkChangeNotifierAndroidTest,ConnectionCost)386 TEST_F(NetworkChangeNotifierAndroidTest, ConnectionCost) {
387   FakeConnectionCostChange(ConnectionCost::CONNECTION_COST_UNMETERED);
388   EXPECT_EQ(NetworkChangeNotifier::CONNECTION_COST_UNMETERED,
389             notifier_.GetConnectionCost());
390   FakeConnectionCostChange(ConnectionCost::CONNECTION_COST_METERED);
391   EXPECT_EQ(NetworkChangeNotifier::CONNECTION_COST_METERED,
392             notifier_.GetConnectionCost());
393 }
394 
TEST_F(NetworkChangeNotifierAndroidTest,ConnectionCostCallbackNotifier)395 TEST_F(NetworkChangeNotifierAndroidTest, ConnectionCostCallbackNotifier) {
396   FakeConnectionCostChange(ConnectionCost::CONNECTION_COST_UNMETERED);
397   EXPECT_EQ(1, connection_cost_observer_.notifications_count());
398 
399   FakeConnectionCostChange(ConnectionCost::CONNECTION_COST_METERED);
400   EXPECT_EQ(2, connection_cost_observer_.notifications_count());
401 }
402 
TEST_F(NetworkChangeNotifierDelegateAndroidTest,ConnectionCostCallbackNotifier)403 TEST_F(NetworkChangeNotifierDelegateAndroidTest,
404        ConnectionCostCallbackNotifier) {
405   EXPECT_EQ(0, delegate_observer_.cost_notifications_count());
406 
407   FakeConnectionCostChange(ConnectionCost::CONNECTION_COST_UNMETERED);
408   EXPECT_EQ(1, delegate_observer_.cost_notifications_count());
409 
410   FakeConnectionCostChange(ConnectionCost::CONNECTION_COST_METERED);
411   EXPECT_EQ(2, delegate_observer_.cost_notifications_count());
412 }
413 
TEST_F(NetworkChangeNotifierAndroidTest,MaxBandwidth)414 TEST_F(NetworkChangeNotifierAndroidTest, MaxBandwidth) {
415   SetOnline();
416   double max_bandwidth_mbps = 0.0;
417   NetworkChangeNotifier::ConnectionType connection_type =
418       NetworkChangeNotifier::CONNECTION_NONE;
419   notifier_.GetMaxBandwidthAndConnectionType(&max_bandwidth_mbps,
420                                              &connection_type);
421   EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, connection_type);
422   EXPECT_EQ(std::numeric_limits<double>::infinity(), max_bandwidth_mbps);
423   SetOffline();
424   notifier_.GetMaxBandwidthAndConnectionType(&max_bandwidth_mbps,
425                                              &connection_type);
426   EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, connection_type);
427   EXPECT_EQ(0.0, max_bandwidth_mbps);
428 }
429 
TEST_F(NetworkChangeNotifierAndroidTest,MaxBandwidthCallbackNotifier)430 TEST_F(NetworkChangeNotifierAndroidTest, MaxBandwidthCallbackNotifier) {
431   // The bandwidth notification should always be forwarded, even if the value
432   // doesn't change (because the type might have changed).
433   FakeConnectionSubtypeChange(ConnectionSubtype::SUBTYPE_CDMA);
434   EXPECT_EQ(1, max_bandwidth_observer_.notifications_count());
435 
436   FakeConnectionSubtypeChange(ConnectionSubtype::SUBTYPE_CDMA);
437   EXPECT_EQ(2, max_bandwidth_observer_.notifications_count());
438 
439   FakeConnectionSubtypeChange(ConnectionSubtype::SUBTYPE_LTE);
440   EXPECT_EQ(3, max_bandwidth_observer_.notifications_count());
441 }
442 
TEST_F(NetworkChangeNotifierDelegateAndroidTest,MaxBandwidthNotifiedOnConnectionChange)443 TEST_F(NetworkChangeNotifierDelegateAndroidTest,
444        MaxBandwidthNotifiedOnConnectionChange) {
445   EXPECT_EQ(0, delegate_observer_.bandwidth_notifications_count());
446   SetOffline();
447   EXPECT_EQ(1, delegate_observer_.bandwidth_notifications_count());
448   SetOnline();
449   EXPECT_EQ(2, delegate_observer_.bandwidth_notifications_count());
450   SetOnline();
451   EXPECT_EQ(2, delegate_observer_.bandwidth_notifications_count());
452 }
453 
TEST_F(NetworkChangeNotifierAndroidTest,NetworkCallbacks)454 TEST_F(NetworkChangeNotifierAndroidTest, NetworkCallbacks) {
455   ForceNetworkHandlesSupportedForTesting();
456 
457   TestNetworkObserver network_observer;
458   NetworkChangeNotifier::AddNetworkObserver(&network_observer);
459 
460   // Test empty values
461   EXPECT_EQ(handles::kInvalidNetworkHandle,
462             NetworkChangeNotifier::GetDefaultNetwork());
463   EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN,
464             NetworkChangeNotifier::GetNetworkConnectionType(100));
465   NetworkChangeNotifier::NetworkList network_list;
466   NetworkChangeNotifier::GetConnectedNetworks(&network_list);
467   EXPECT_EQ(0u, network_list.size());
468   // Test connecting network
469   FakeNetworkChange(CONNECTED, 100, NetworkChangeNotifier::CONNECTION_WIFI);
470   network_observer.ExpectChange(CONNECTED, 100);
471   EXPECT_EQ(handles::kInvalidNetworkHandle,
472             NetworkChangeNotifier::GetDefaultNetwork());
473   // Test GetConnectedNetworks()
474   NetworkChangeNotifier::GetConnectedNetworks(&network_list);
475   EXPECT_EQ(1u, network_list.size());
476   EXPECT_EQ(100, network_list[0]);
477   // Test GetNetworkConnectionType()
478   EXPECT_EQ(NetworkChangeNotifier::CONNECTION_WIFI,
479             NetworkChangeNotifier::GetNetworkConnectionType(100));
480   // Test deduplication of connecting signal
481   FakeNetworkChange(CONNECTED, 100, NetworkChangeNotifier::CONNECTION_WIFI);
482   network_observer.ExpectChange(NONE, handles::kInvalidNetworkHandle);
483   // Test connecting another network
484   FakeNetworkChange(CONNECTED, 101, NetworkChangeNotifier::CONNECTION_3G);
485   network_observer.ExpectChange(CONNECTED, 101);
486   NetworkChangeNotifier::GetConnectedNetworks(&network_list);
487   EXPECT_EQ(2u, network_list.size());
488   EXPECT_EQ(100, network_list[0]);
489   EXPECT_EQ(101, network_list[1]);
490   EXPECT_EQ(NetworkChangeNotifier::CONNECTION_WIFI,
491             NetworkChangeNotifier::GetNetworkConnectionType(100));
492   EXPECT_EQ(NetworkChangeNotifier::CONNECTION_3G,
493             NetworkChangeNotifier::GetNetworkConnectionType(101));
494   // Test lingering network
495   FakeNetworkChange(SOON_TO_DISCONNECT, 100,
496                     NetworkChangeNotifier::CONNECTION_WIFI);
497   network_observer.ExpectChange(SOON_TO_DISCONNECT, 100);
498   NetworkChangeNotifier::GetConnectedNetworks(&network_list);
499   EXPECT_EQ(2u, network_list.size());
500   EXPECT_EQ(100, network_list[0]);
501   EXPECT_EQ(101, network_list[1]);
502   // Test disconnecting network
503   FakeNetworkChange(DISCONNECTED, 100, NetworkChangeNotifier::CONNECTION_WIFI);
504   network_observer.ExpectChange(DISCONNECTED, 100);
505   NetworkChangeNotifier::GetConnectedNetworks(&network_list);
506   EXPECT_EQ(1u, network_list.size());
507   EXPECT_EQ(101, network_list[0]);
508   // Test deduplication of disconnecting signal
509   FakeNetworkChange(DISCONNECTED, 100, NetworkChangeNotifier::CONNECTION_WIFI);
510   network_observer.ExpectChange(NONE, handles::kInvalidNetworkHandle);
511   // Test delay of default network signal until connect signal
512   FakeNetworkChange(MADE_DEFAULT, 100, NetworkChangeNotifier::CONNECTION_WIFI);
513   network_observer.ExpectChange(NONE, handles::kInvalidNetworkHandle);
514   FakeNetworkChange(CONNECTED, 100, NetworkChangeNotifier::CONNECTION_WIFI);
515   network_observer.ExpectChange(MADE_DEFAULT, 100);
516   EXPECT_EQ(100, NetworkChangeNotifier::GetDefaultNetwork());
517   // Test change of default
518   FakeNetworkChange(MADE_DEFAULT, 101, NetworkChangeNotifier::CONNECTION_3G);
519   network_observer.ExpectChange(MADE_DEFAULT, 101);
520   EXPECT_EQ(101, NetworkChangeNotifier::GetDefaultNetwork());
521   // Test deduplication default signal
522   FakeNetworkChange(MADE_DEFAULT, 101, NetworkChangeNotifier::CONNECTION_3G);
523   network_observer.ExpectChange(NONE, handles::kInvalidNetworkHandle);
524   // Test that networks can change type
525   FakeNetworkChange(CONNECTED, 101, NetworkChangeNotifier::CONNECTION_4G);
526   network_observer.ExpectChange(NONE, handles::kInvalidNetworkHandle);
527   EXPECT_EQ(NetworkChangeNotifier::CONNECTION_4G,
528             NetworkChangeNotifier::GetNetworkConnectionType(101));
529   // Test purging the network list
530   NetworkChangeNotifier::GetConnectedNetworks(&network_list);
531   EXPECT_EQ(2u, network_list.size());
532   EXPECT_EQ(100, network_list[0]);
533   EXPECT_EQ(101, network_list[1]);
534   network_list.erase(network_list.begin() + 1);  // Remove network 101
535   FakePurgeActiveNetworkList(network_list);
536   network_observer.ExpectChange(DISCONNECTED, 101);
537   NetworkChangeNotifier::GetConnectedNetworks(&network_list);
538   EXPECT_EQ(1u, network_list.size());
539   EXPECT_EQ(100, network_list[0]);
540   EXPECT_EQ(handles::kInvalidNetworkHandle,
541             NetworkChangeNotifier::GetDefaultNetwork());
542 
543   NetworkChangeNotifier::RemoveNetworkObserver(&network_observer);
544 }
545 
546 // Tests that network type changes happen synchronously. Otherwise the type
547 // "change" at browser startup leaves tasks on the queue that will later
548 // invalidate any network requests that have been started.
TEST_F(NetworkChangeNotifierDelegateAndroidTest,TypeChangeIsSynchronous)549 TEST_F(NetworkChangeNotifierDelegateAndroidTest, TypeChangeIsSynchronous) {
550   const int initial_value = delegate_observer_.type_notifications_count();
551   SetOffline(/*drain_run_loop=*/false);
552   // Note that there's no call to |base::RunLoop::RunUntilIdle| here. The
553   // update must happen synchronously.
554   EXPECT_EQ(initial_value + 1, delegate_observer_.type_notifications_count());
555 }
556 
TEST_F(NetworkChangeNotifierDelegateAndroidTest,DefaultNetworkActive)557 TEST_F(NetworkChangeNotifierDelegateAndroidTest, DefaultNetworkActive) {
558   // No notifications should be received when there are no observers.
559   EXPECT_EQ(0, delegate_observer_.default_network_active_notifications_count());
560   FakeDefaultNetworkActive();
561   EXPECT_EQ(0, delegate_observer_.default_network_active_notifications_count());
562 
563   // Simulate calls to NetworkChangeNotifier::AddDefaultNetworkObserver().
564   // Notifications should be received now.
565   delegate_.DefaultNetworkActiveObserverAdded();
566   FakeDefaultNetworkActive();
567   EXPECT_EQ(1, delegate_observer_.default_network_active_notifications_count());
568   delegate_.DefaultNetworkActiveObserverAdded();
569   FakeDefaultNetworkActive();
570   EXPECT_EQ(2, delegate_observer_.default_network_active_notifications_count());
571 
572   // Simulate call to NetworkChangeNotifier::AddDefaultNetworkObserver().
573   // Notifications should be received until the last observer has been
574   // removed.
575   delegate_.DefaultNetworkActiveObserverRemoved();
576   FakeDefaultNetworkActive();
577   EXPECT_EQ(3, delegate_observer_.default_network_active_notifications_count());
578   delegate_.DefaultNetworkActiveObserverRemoved();
579   FakeDefaultNetworkActive();
580   EXPECT_EQ(3, delegate_observer_.default_network_active_notifications_count());
581 
582   // Double check that things keep working as expected after re-adding an
583   // observer.
584   delegate_.DefaultNetworkActiveObserverAdded();
585   FakeDefaultNetworkActive();
586   EXPECT_EQ(4, delegate_observer_.default_network_active_notifications_count());
587 
588   // Cleanup: delegate destructor DCHECKS that all observers have been
589   // removed.
590   delegate_.DefaultNetworkActiveObserverRemoved();
591 }
592 
593 }  // namespace net
594