network_change_notifier_android_unittest.cc 23 KB

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