OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.net; | 5 package org.chromium.net; |
6 | 6 |
7 import android.annotation.SuppressLint; | 7 import android.annotation.SuppressLint; |
8 import android.content.Context; | 8 import android.content.Context; |
9 import android.content.Intent; | 9 import android.content.Intent; |
10 import android.net.ConnectivityManager; | 10 import android.net.ConnectivityManager; |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 @UiThreadTest | 293 @UiThreadTest |
294 @MediumTest | 294 @MediumTest |
295 @Feature({"Android-AppBase"}) | 295 @Feature({"Android-AppBase"}) |
296 public void testNetworkChangeNotifierRegistersInConstructor() throws Interru
ptedException { | 296 public void testNetworkChangeNotifierRegistersInConstructor() throws Interru
ptedException { |
297 Context context = getInstrumentation().getTargetContext(); | 297 Context context = getInstrumentation().getTargetContext(); |
298 | 298 |
299 NetworkChangeNotifierAutoDetect.Observer observer = | 299 NetworkChangeNotifierAutoDetect.Observer observer = |
300 new TestNetworkChangeNotifierAutoDetectObserver(); | 300 new TestNetworkChangeNotifierAutoDetectObserver(); |
301 | 301 |
302 NetworkChangeNotifierAutoDetect receiver = new NetworkChangeNotifierAuto
Detect( | 302 NetworkChangeNotifierAutoDetect receiver = new NetworkChangeNotifierAuto
Detect( |
303 observer, context, false /* always watch for changes */) { | 303 observer, context, new RegistrationPolicyApplicationStatus() { |
304 @Override | 304 @Override |
305 int getApplicationState() { | 305 int getApplicationState() { |
306 return ApplicationState.HAS_RUNNING_ACTIVITIES; | 306 return ApplicationState.HAS_RUNNING_ACTIVITIES; |
307 } | 307 } |
308 }; | 308 }); |
309 | 309 |
310 assertTrue(receiver.isReceiverRegisteredForTesting()); | 310 assertTrue(receiver.isReceiverRegisteredForTesting()); |
311 } | 311 } |
312 | 312 |
313 /** | 313 /** |
314 * Tests that the receiver toggles registration for connectivity intents bas
ed on activity | 314 * Tests that the receiver toggles registration for connectivity intents bas
ed on activity |
315 * state. | 315 * state. |
316 */ | 316 */ |
317 @UiThreadTest | 317 @UiThreadTest |
318 @MediumTest | 318 @MediumTest |
319 @Feature({"Android-AppBase"}) | 319 @Feature({"Android-AppBase"}) |
320 public void testNetworkChangeNotifierRegistersForIntents() throws Interrupte
dException { | 320 public void testNetworkChangeNotifierRegistersForIntents() throws Interrupte
dException { |
321 mReceiver.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITI
ES); | 321 RegistrationPolicyApplicationStatus policy = |
| 322 (RegistrationPolicyApplicationStatus) mReceiver.getRegistrationP
olicy(); |
| 323 policy.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITIES)
; |
322 assertTrue(mReceiver.isReceiverRegisteredForTesting()); | 324 assertTrue(mReceiver.isReceiverRegisteredForTesting()); |
323 | 325 |
324 mReceiver.onApplicationStateChange(ApplicationState.HAS_PAUSED_ACTIVITIE
S); | 326 policy.onApplicationStateChange(ApplicationState.HAS_PAUSED_ACTIVITIES); |
325 assertFalse(mReceiver.isReceiverRegisteredForTesting()); | 327 assertFalse(mReceiver.isReceiverRegisteredForTesting()); |
326 | 328 |
327 mReceiver.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITI
ES); | 329 policy.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITIES)
; |
328 assertTrue(mReceiver.isReceiverRegisteredForTesting()); | 330 assertTrue(mReceiver.isReceiverRegisteredForTesting()); |
329 } | 331 } |
330 | 332 |
331 /** | 333 /** |
332 * Tests that changing the RSSI_CHANGED_ACTION intent updates MaxBandwidth. | 334 * Tests that changing the RSSI_CHANGED_ACTION intent updates MaxBandwidth. |
333 */ | 335 */ |
334 @UiThreadTest | 336 @UiThreadTest |
335 @MediumTest | 337 @MediumTest |
336 @Feature({"Android-AppBase"}) | 338 @Feature({"Android-AppBase"}) |
337 public void testNetworkChangeNotifierRSSIEventUpdatesMaxBandwidthForWiFi() | 339 public void testNetworkChangeNotifierRSSIEventUpdatesMaxBandwidthForWiFi() |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 assertFalse(observer.hasReceivedNotification()); | 455 assertFalse(observer.hasReceivedNotification()); |
454 | 456 |
455 // Mimic that connectivity has been lost and ensure that Chrome notifies
our observer. | 457 // Mimic that connectivity has been lost and ensure that Chrome notifies
our observer. |
456 mConnectivityDelegate.setActiveNetworkExists(false); | 458 mConnectivityDelegate.setActiveNetworkExists(false); |
457 Intent noConnectivityIntent = new Intent(ConnectivityManager.CONNECTIVIT
Y_ACTION); | 459 Intent noConnectivityIntent = new Intent(ConnectivityManager.CONNECTIVIT
Y_ACTION); |
458 mReceiver.onReceive(getInstrumentation().getTargetContext(), noConnectiv
ityIntent); | 460 mReceiver.onReceive(getInstrumentation().getTargetContext(), noConnectiv
ityIntent); |
459 assertTrue(observer.hasReceivedNotification()); | 461 assertTrue(observer.hasReceivedNotification()); |
460 | 462 |
461 observer.resetHasReceivedNotification(); | 463 observer.resetHasReceivedNotification(); |
462 // Pretend we got moved to the background. | 464 // Pretend we got moved to the background. |
463 mReceiver.onApplicationStateChange(ApplicationState.HAS_PAUSED_ACTIVITIE
S); | 465 final RegistrationPolicyApplicationStatus policy = |
| 466 (RegistrationPolicyApplicationStatus) mReceiver.getRegistrationP
olicy(); |
| 467 policy.onApplicationStateChange(ApplicationState.HAS_PAUSED_ACTIVITIES); |
464 // Change the state. | 468 // Change the state. |
465 mConnectivityDelegate.setActiveNetworkExists(true); | 469 mConnectivityDelegate.setActiveNetworkExists(true); |
466 mConnectivityDelegate.setNetworkType(ConnectivityManager.TYPE_WIFI); | 470 mConnectivityDelegate.setNetworkType(ConnectivityManager.TYPE_WIFI); |
467 // The NetworkChangeNotifierAutoDetect doesn't receive any notification
while we are in the | 471 // The NetworkChangeNotifierAutoDetect doesn't receive any notification
while we are in the |
468 // background, but when we get back to the foreground the state changed
should be detected | 472 // background, but when we get back to the foreground the state changed
should be detected |
469 // and a notification sent. | 473 // and a notification sent. |
470 mReceiver.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITI
ES); | 474 policy.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITIES)
; |
471 assertTrue(observer.hasReceivedNotification()); | 475 assertTrue(observer.hasReceivedNotification()); |
472 } | 476 } |
473 | 477 |
474 /** | 478 /** |
475 * Tests that when setting {@code registerToReceiveNotificationsAlways()}, | 479 * Tests that when setting {@code registerToReceiveNotificationsAlways()}, |
476 * a NetworkChangeNotifierAutoDetect object is successfully created. | 480 * a NetworkChangeNotifierAutoDetect object is successfully created. |
477 */ | 481 */ |
478 @UiThreadTest | 482 @UiThreadTest |
479 @MediumTest | 483 @MediumTest |
480 @Feature({"Android-AppBase"}) | 484 @Feature({"Android-AppBase"}) |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 * Tests that NetworkChangeNotifierAutoDetect queryable APIs don't crash. Th
is test cannot rely | 524 * Tests that NetworkChangeNotifierAutoDetect queryable APIs don't crash. Th
is test cannot rely |
521 * on having any active network connections so it cannot usefully check resu
lts, but it can at | 525 * on having any active network connections so it cannot usefully check resu
lts, but it can at |
522 * least check that the functions don't crash. | 526 * least check that the functions don't crash. |
523 */ | 527 */ |
524 @UiThreadTest | 528 @UiThreadTest |
525 @MediumTest | 529 @MediumTest |
526 @Feature({"Android-AppBase"}) | 530 @Feature({"Android-AppBase"}) |
527 public void testQueryableAPIsDoNotCrash() { | 531 public void testQueryableAPIsDoNotCrash() { |
528 NetworkChangeNotifierAutoDetect.Observer observer = | 532 NetworkChangeNotifierAutoDetect.Observer observer = |
529 new TestNetworkChangeNotifierAutoDetectObserver(); | 533 new TestNetworkChangeNotifierAutoDetectObserver(); |
530 NetworkChangeNotifierAutoDetect ncn = new NetworkChangeNotifierAutoDetec
t( | 534 NetworkChangeNotifierAutoDetect ncn = new NetworkChangeNotifierAutoDetec
t(observer, |
531 observer, getInstrumentation().getTargetContext(), true); | 535 getInstrumentation().getTargetContext(), new RegistrationPolicyA
lwaysRegister()); |
532 ncn.getNetworksAndTypes(); | 536 ncn.getNetworksAndTypes(); |
533 ncn.getDefaultNetId(); | 537 ncn.getDefaultNetId(); |
534 } | 538 } |
535 | 539 |
536 /** | 540 /** |
537 * Tests that callbacks are issued to Observers when NetworkChangeNotifierAu
toDetect receives | 541 * Tests that callbacks are issued to Observers when NetworkChangeNotifierAu
toDetect receives |
538 * the right signals (via its NetworkCallback). | 542 * the right signals (via its NetworkCallback). |
539 */ | 543 */ |
540 @MediumTest | 544 @MediumTest |
541 @Feature({"Android-AppBase"}) | 545 @Feature({"Android-AppBase"}) |
542 public void testNetworkCallbacks() throws Exception { | 546 public void testNetworkCallbacks() throws Exception { |
543 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { | 547 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { |
544 return; | 548 return; |
545 } | 549 } |
546 // Setup NetworkChangeNotifierAutoDetect | 550 // Setup NetworkChangeNotifierAutoDetect |
547 final Context context = getInstrumentation().getTargetContext(); | 551 final Context context = getInstrumentation().getTargetContext(); |
548 final TestNetworkChangeNotifierAutoDetectObserver observer = | 552 final TestNetworkChangeNotifierAutoDetectObserver observer = |
549 new TestNetworkChangeNotifierAutoDetectObserver(); | 553 new TestNetworkChangeNotifierAutoDetectObserver(); |
550 Callable<NetworkChangeNotifierAutoDetect> callable = | 554 Callable<NetworkChangeNotifierAutoDetect> callable = |
551 new Callable<NetworkChangeNotifierAutoDetect>() { | 555 new Callable<NetworkChangeNotifierAutoDetect>() { |
552 public NetworkChangeNotifierAutoDetect call() { | 556 public NetworkChangeNotifierAutoDetect call() { |
553 return new NetworkChangeNotifierAutoDetect( | 557 return new NetworkChangeNotifierAutoDetect( |
554 observer, context, false /* always watch for cha
nges */) { | 558 observer, context, new RegistrationPolicyApplica
tionStatus() { |
555 // This override prevents NetworkChangeNotifierAutoD
etect from | 559 // This override prevents NetworkChangeNotif
ierAutoDetect from |
556 // registering for events right off the bat. We'll d
elay this | 560 // registering for events right off the bat.
We'll delay this |
557 // until our MockConnectivityManagerDelegate is firs
t installed | 561 // until our MockConnectivityManagerDelegate
is first installed |
558 // to prevent inadvertent communication with the rea
l | 562 // to prevent inadvertent communication with
the real |
559 // ConnectivityManager. | 563 // ConnectivityManager. |
560 @Override | 564 @Override |
561 int getApplicationState() { | 565 int getApplicationState() { |
562 return ApplicationState.HAS_PAUSED_ACTIVITIES; | 566 return ApplicationState.HAS_PAUSED_ACTIV
ITIES; |
563 } | 567 } |
564 }; | 568 }); |
565 } | 569 } |
566 }; | 570 }; |
567 FutureTask<NetworkChangeNotifierAutoDetect> task = | 571 FutureTask<NetworkChangeNotifierAutoDetect> task = |
568 new FutureTask<NetworkChangeNotifierAutoDetect>(callable); | 572 new FutureTask<NetworkChangeNotifierAutoDetect>(callable); |
569 ThreadUtils.postOnUiThread(task); | 573 ThreadUtils.postOnUiThread(task); |
570 NetworkChangeNotifierAutoDetect ncn = task.get(); | 574 NetworkChangeNotifierAutoDetect ncn = task.get(); |
571 | 575 |
572 // Insert mock ConnectivityDelegate | 576 // Insert mock ConnectivityDelegate |
573 mConnectivityDelegate = new MockConnectivityManagerDelegate(); | 577 mConnectivityDelegate = new MockConnectivityManagerDelegate(); |
574 ncn.setConnectivityManagerDelegateForTests(mConnectivityDelegate); | 578 ncn.setConnectivityManagerDelegateForTests(mConnectivityDelegate); |
575 // Now that mock ConnectivityDelegate is inserted, pretend app is foregr
ounded | 579 // Now that mock ConnectivityDelegate is inserted, pretend app is foregr
ounded |
576 // so NetworkChangeNotifierAutoDetect will register its NetworkCallback. | 580 // so NetworkChangeNotifierAutoDetect will register its NetworkCallback. |
577 assertFalse(ncn.isReceiverRegisteredForTesting()); | 581 assertFalse(ncn.isReceiverRegisteredForTesting()); |
578 ncn.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITIES); | 582 |
| 583 RegistrationPolicyApplicationStatus policy = |
| 584 (RegistrationPolicyApplicationStatus) mReceiver.getRegistrationP
olicy(); |
| 585 policy.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITIES)
; |
579 assertTrue(ncn.isReceiverRegisteredForTesting()); | 586 assertTrue(ncn.isReceiverRegisteredForTesting()); |
580 | 587 |
581 // Find NetworkChangeNotifierAutoDetect's NetworkCallback, which should
have been registered | 588 // Find NetworkChangeNotifierAutoDetect's NetworkCallback, which should
have been registered |
582 // with mConnectivityDelegate. | 589 // with mConnectivityDelegate. |
583 NetworkCallback networkCallback = mConnectivityDelegate.getLastRegistere
dNetworkCallback(); | 590 NetworkCallback networkCallback = mConnectivityDelegate.getLastRegistere
dNetworkCallback(); |
584 assertNotNull(networkCallback); | 591 assertNotNull(networkCallback); |
585 | 592 |
586 // First thing we'll receive is a purge to initialize any network lists. | 593 // First thing we'll receive is a purge to initialize any network lists. |
587 observer.assertLastChange(ChangeType.PURGE_LIST, NetId.INVALID); | 594 observer.assertLastChange(ChangeType.PURGE_LIST, NetId.INVALID); |
588 | 595 |
589 // Test connected signal is passed along. | 596 // Test connected signal is passed along. |
590 networkCallback.onAvailable(netIdToNetwork(100)); | 597 networkCallback.onAvailable(netIdToNetwork(100)); |
591 observer.assertLastChange(ChangeType.CONNECT, 100); | 598 observer.assertLastChange(ChangeType.CONNECT, 100); |
592 | 599 |
593 // Test soon-to-be-disconnected signal is passed along. | 600 // Test soon-to-be-disconnected signal is passed along. |
594 networkCallback.onLosing(netIdToNetwork(101), 30); | 601 networkCallback.onLosing(netIdToNetwork(101), 30); |
595 observer.assertLastChange(ChangeType.SOON_TO_DISCONNECT, 101); | 602 observer.assertLastChange(ChangeType.SOON_TO_DISCONNECT, 101); |
596 | 603 |
597 // Test connected signal is passed along. | 604 // Test connected signal is passed along. |
598 networkCallback.onLost(netIdToNetwork(102)); | 605 networkCallback.onLost(netIdToNetwork(102)); |
599 observer.assertLastChange(ChangeType.DISCONNECT, 102); | 606 observer.assertLastChange(ChangeType.DISCONNECT, 102); |
600 | 607 |
601 // Simulate app backgrounding then foregrounding. | 608 // Simulate app backgrounding then foregrounding. |
602 assertTrue(ncn.isReceiverRegisteredForTesting()); | 609 assertTrue(ncn.isReceiverRegisteredForTesting()); |
603 ncn.onApplicationStateChange(ApplicationState.HAS_PAUSED_ACTIVITIES); | 610 policy.onApplicationStateChange(ApplicationState.HAS_PAUSED_ACTIVITIES); |
604 assertFalse(ncn.isReceiverRegisteredForTesting()); | 611 assertFalse(ncn.isReceiverRegisteredForTesting()); |
605 ncn.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITIES); | 612 policy.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITIES)
; |
606 assertTrue(ncn.isReceiverRegisteredForTesting()); | 613 assertTrue(ncn.isReceiverRegisteredForTesting()); |
607 // Verify network list purged. | 614 // Verify network list purged. |
608 observer.assertLastChange(ChangeType.PURGE_LIST, NetId.INVALID); | 615 observer.assertLastChange(ChangeType.PURGE_LIST, NetId.INVALID); |
609 } | 616 } |
610 } | 617 } |
OLD | NEW |