summaryrefslogtreecommitdiffstats
path: root/net/android/javatests/src/org
diff options
context:
space:
mode:
authortimvolodine <timvolodine@chromium.org>2015-10-08 09:25:35 -0700
committerCommit bot <commit-bot@chromium.org>2015-10-08 16:26:30 +0000
commit97997f341e5e7205b5311a5f1ed66f8cd0d66454 (patch)
tree7c906998a11d1b4327966a5a6d5056ef0546099e /net/android/javatests/src/org
parent496d6f3c798e91da129379025de6f7e034ecdc4a (diff)
downloadchromium_src-97997f341e5e7205b5311a5f1ed66f8cd0d66454.zip
chromium_src-97997f341e5e7205b5311a5f1ed66f8cd0d66454.tar.gz
chromium_src-97997f341e5e7205b5311a5f1ed66f8cd0d66454.tar.bz2
[Android] Introduce RegistrationPolicy for NetworkChangeNotifier.
Currently the NetworkChangeNotifier.setAutoDetectConnectivityState(true) uses ApplicationState to determine when to listen to network changes. However the ApplicationStatus class does not work as such in the context of WebView. To make the NetworkChangeNotifier more flexible this patch introduces a RegistrationPolicy class which can be passed to the overloaded setAutoDetectConnectivityState method. We make sure to keep the overall API compatible, such that existing users of the NetworkChangeNotifier do not have to provide a policy, which is set behind the screens instead. To this end this patch includes two concrete implementations of the RegistrationPolicy. BUG=520088 Review URL: https://codereview.chromium.org/1358163004 Cr-Commit-Position: refs/heads/master@{#353071}
Diffstat (limited to 'net/android/javatests/src/org')
-rw-r--r--net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java61
1 files changed, 34 insertions, 27 deletions
diff --git a/net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java b/net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java
index b28f5ef..cfd748b 100644
--- a/net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java
+++ b/net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java
@@ -300,12 +300,12 @@ public class NetworkChangeNotifierTest extends InstrumentationTestCase {
new TestNetworkChangeNotifierAutoDetectObserver();
NetworkChangeNotifierAutoDetect receiver = new NetworkChangeNotifierAutoDetect(
- observer, context, false /* always watch for changes */) {
- @Override
- int getApplicationState() {
- return ApplicationState.HAS_RUNNING_ACTIVITIES;
- }
- };
+ observer, context, new RegistrationPolicyApplicationStatus() {
+ @Override
+ int getApplicationState() {
+ return ApplicationState.HAS_RUNNING_ACTIVITIES;
+ }
+ });
assertTrue(receiver.isReceiverRegisteredForTesting());
}
@@ -318,13 +318,15 @@ public class NetworkChangeNotifierTest extends InstrumentationTestCase {
@MediumTest
@Feature({"Android-AppBase"})
public void testNetworkChangeNotifierRegistersForIntents() throws InterruptedException {
- mReceiver.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITIES);
+ RegistrationPolicyApplicationStatus policy =
+ (RegistrationPolicyApplicationStatus) mReceiver.getRegistrationPolicy();
+ policy.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITIES);
assertTrue(mReceiver.isReceiverRegisteredForTesting());
- mReceiver.onApplicationStateChange(ApplicationState.HAS_PAUSED_ACTIVITIES);
+ policy.onApplicationStateChange(ApplicationState.HAS_PAUSED_ACTIVITIES);
assertFalse(mReceiver.isReceiverRegisteredForTesting());
- mReceiver.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITIES);
+ policy.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITIES);
assertTrue(mReceiver.isReceiverRegisteredForTesting());
}
@@ -460,14 +462,16 @@ public class NetworkChangeNotifierTest extends InstrumentationTestCase {
observer.resetHasReceivedNotification();
// Pretend we got moved to the background.
- mReceiver.onApplicationStateChange(ApplicationState.HAS_PAUSED_ACTIVITIES);
+ final RegistrationPolicyApplicationStatus policy =
+ (RegistrationPolicyApplicationStatus) mReceiver.getRegistrationPolicy();
+ policy.onApplicationStateChange(ApplicationState.HAS_PAUSED_ACTIVITIES);
// Change the state.
mConnectivityDelegate.setActiveNetworkExists(true);
mConnectivityDelegate.setNetworkType(ConnectivityManager.TYPE_WIFI);
// The NetworkChangeNotifierAutoDetect doesn't receive any notification while we are in the
// background, but when we get back to the foreground the state changed should be detected
// and a notification sent.
- mReceiver.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITIES);
+ policy.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITIES);
assertTrue(observer.hasReceivedNotification());
}
@@ -527,8 +531,8 @@ public class NetworkChangeNotifierTest extends InstrumentationTestCase {
public void testQueryableAPIsDoNotCrash() {
NetworkChangeNotifierAutoDetect.Observer observer =
new TestNetworkChangeNotifierAutoDetectObserver();
- NetworkChangeNotifierAutoDetect ncn = new NetworkChangeNotifierAutoDetect(
- observer, getInstrumentation().getTargetContext(), true);
+ NetworkChangeNotifierAutoDetect ncn = new NetworkChangeNotifierAutoDetect(observer,
+ getInstrumentation().getTargetContext(), new RegistrationPolicyAlwaysRegister());
ncn.getNetworksAndTypes();
ncn.getDefaultNetId();
}
@@ -551,17 +555,17 @@ public class NetworkChangeNotifierTest extends InstrumentationTestCase {
new Callable<NetworkChangeNotifierAutoDetect>() {
public NetworkChangeNotifierAutoDetect call() {
return new NetworkChangeNotifierAutoDetect(
- observer, context, false /* always watch for changes */) {
- // This override prevents NetworkChangeNotifierAutoDetect from
- // registering for events right off the bat. We'll delay this
- // until our MockConnectivityManagerDelegate is first installed
- // to prevent inadvertent communication with the real
- // ConnectivityManager.
- @Override
- int getApplicationState() {
- return ApplicationState.HAS_PAUSED_ACTIVITIES;
- }
- };
+ observer, context, new RegistrationPolicyApplicationStatus() {
+ // This override prevents NetworkChangeNotifierAutoDetect from
+ // registering for events right off the bat. We'll delay this
+ // until our MockConnectivityManagerDelegate is first installed
+ // to prevent inadvertent communication with the real
+ // ConnectivityManager.
+ @Override
+ int getApplicationState() {
+ return ApplicationState.HAS_PAUSED_ACTIVITIES;
+ }
+ });
}
};
FutureTask<NetworkChangeNotifierAutoDetect> task =
@@ -575,7 +579,10 @@ public class NetworkChangeNotifierTest extends InstrumentationTestCase {
// Now that mock ConnectivityDelegate is inserted, pretend app is foregrounded
// so NetworkChangeNotifierAutoDetect will register its NetworkCallback.
assertFalse(ncn.isReceiverRegisteredForTesting());
- ncn.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITIES);
+
+ RegistrationPolicyApplicationStatus policy =
+ (RegistrationPolicyApplicationStatus) mReceiver.getRegistrationPolicy();
+ policy.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITIES);
assertTrue(ncn.isReceiverRegisteredForTesting());
// Find NetworkChangeNotifierAutoDetect's NetworkCallback, which should have been registered
@@ -600,9 +607,9 @@ public class NetworkChangeNotifierTest extends InstrumentationTestCase {
// Simulate app backgrounding then foregrounding.
assertTrue(ncn.isReceiverRegisteredForTesting());
- ncn.onApplicationStateChange(ApplicationState.HAS_PAUSED_ACTIVITIES);
+ policy.onApplicationStateChange(ApplicationState.HAS_PAUSED_ACTIVITIES);
assertFalse(ncn.isReceiverRegisteredForTesting());
- ncn.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITIES);
+ policy.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITIES);
assertTrue(ncn.isReceiverRegisteredForTesting());
// Verify network list purged.
observer.assertLastChange(ChangeType.PURGE_LIST, NetId.INVALID);