Allow tests to set the NetworkAgent's callbacks when creating it.
ConnectivityServiceTest#testNetworkAgentCallbacks tests the behaviour of the NetworkAgent callbacks onNetworkCreated, onNetworkUnwanted, and onNetworkDestroyed. This uses a NetworkAgentWrapper method that sets the callbacks after the test agent is constructed. This infrastructure not sufficient to test an upcoming change which will make onNetworkCreated be fired as soon as the registration onNetworkCreated is fired as soon as the agent is registered. Fix the code so that the callbacks can be specified at agent registration time. This is also a bit more realistic since in real usage, the callbacks are methods on the NetworkAgent subclass and are already set when the agent is constructed. Bug: 143158421 Test: test-only change Change-Id: I53c58e7b6c6ae4abf08e0df5051694cc4568a510
This commit is contained in:
@@ -61,6 +61,7 @@ import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class NetworkAgentWrapper implements TestableNetworkCallback.HasNetwork {
|
||||
private final NetworkCapabilities mNetworkCapabilities;
|
||||
@@ -83,14 +84,35 @@ public class NetworkAgentWrapper implements TestableNetworkCallback.HasNetwork {
|
||||
private final ArrayTrackRecord<CallbackType>.ReadHead mCallbackHistory =
|
||||
new ArrayTrackRecord<CallbackType>().newReadHead();
|
||||
|
||||
public static class Callbacks {
|
||||
public final Consumer<NetworkAgent> onNetworkCreated;
|
||||
public final Consumer<NetworkAgent> onNetworkUnwanted;
|
||||
public final Consumer<NetworkAgent> onNetworkDestroyed;
|
||||
|
||||
public Callbacks() {
|
||||
this(null, null, null);
|
||||
}
|
||||
|
||||
public Callbacks(Consumer<NetworkAgent> onNetworkCreated,
|
||||
Consumer<NetworkAgent> onNetworkUnwanted,
|
||||
Consumer<NetworkAgent> onNetworkDestroyed) {
|
||||
this.onNetworkCreated = onNetworkCreated;
|
||||
this.onNetworkUnwanted = onNetworkUnwanted;
|
||||
this.onNetworkDestroyed = onNetworkDestroyed;
|
||||
}
|
||||
}
|
||||
|
||||
private final Callbacks mCallbacks;
|
||||
|
||||
public NetworkAgentWrapper(int transport, LinkProperties linkProperties,
|
||||
NetworkCapabilities ncTemplate, Context context) throws Exception {
|
||||
this(transport, linkProperties, ncTemplate, null /* provider */, context);
|
||||
this(transport, linkProperties, ncTemplate, null /* provider */,
|
||||
null /* callbacks */, context);
|
||||
}
|
||||
|
||||
public NetworkAgentWrapper(int transport, LinkProperties linkProperties,
|
||||
NetworkCapabilities ncTemplate, NetworkProvider provider,
|
||||
Context context) throws Exception {
|
||||
Callbacks callbacks, Context context) throws Exception {
|
||||
final int type = transportToLegacyType(transport);
|
||||
final String typeName = ConnectivityManager.getNetworkTypeName(type);
|
||||
mNetworkCapabilities = (ncTemplate != null) ? ncTemplate : new NetworkCapabilities();
|
||||
@@ -135,6 +157,7 @@ public class NetworkAgentWrapper implements TestableNetworkCallback.HasNetwork {
|
||||
.setLegacyTypeName(typeName)
|
||||
.setLegacyExtraInfo(extraInfo)
|
||||
.build();
|
||||
mCallbacks = (callbacks != null) ? callbacks : new Callbacks();
|
||||
mNetworkAgent = makeNetworkAgent(linkProperties, mNetworkAgentConfig, provider);
|
||||
}
|
||||
|
||||
@@ -214,6 +237,31 @@ public class NetworkAgentWrapper implements TestableNetworkCallback.HasNetwork {
|
||||
protected void removeKeepalivePacketFilter(Message msg) {
|
||||
Log.i(mWrapper.mLogTag, "Remove keepalive packet filter.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNetworkCreated() {
|
||||
super.onNetworkCreated();
|
||||
if (mWrapper.mCallbacks.onNetworkCreated != null) {
|
||||
mWrapper.mCallbacks.onNetworkCreated.accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNetworkUnwanted() {
|
||||
super.onNetworkUnwanted();
|
||||
if (mWrapper.mCallbacks.onNetworkUnwanted != null) {
|
||||
mWrapper.mCallbacks.onNetworkUnwanted.accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNetworkDestroyed() {
|
||||
super.onNetworkDestroyed();
|
||||
if (mWrapper.mCallbacks.onNetworkDestroyed != null) {
|
||||
mWrapper.mCallbacks.onNetworkDestroyed.accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void setScore(@NonNull final NetworkScore score) {
|
||||
|
||||
Reference in New Issue
Block a user