Add KeepaliveTracker.Dependencies class for testing purpose
This is a part of preliminary work for adding tests in the
follow up commit.
Bug: 196453719
Test: atest FrameworksNetTests
Change-Id: Ibf1bd267c4c144f533380eeafc6a6144f64da483
diff --git a/service/src/com/android/server/connectivity/KeepaliveTracker.java b/service/src/com/android/server/connectivity/KeepaliveTracker.java
index b4f74d5..0e30b7a 100644
--- a/service/src/com/android/server/connectivity/KeepaliveTracker.java
+++ b/service/src/com/android/server/connectivity/KeepaliveTracker.java
@@ -104,19 +104,21 @@
// Allowed unprivileged keepalive slots per uid. Caller's permission will be enforced if
// the number of remaining keepalive slots is less than or equal to the threshold.
private final int mAllowedUnprivilegedSlotsForUid;
-
+ private final Dependencies mDependencies;
public KeepaliveTracker(Context context, Handler handler) {
- this(context, handler, new TcpKeepaliveController(handler));
+ this(context, handler, new TcpKeepaliveController(handler), new Dependencies());
}
@VisibleForTesting
- KeepaliveTracker(Context context, Handler handler, TcpKeepaliveController tcpController) {
+ KeepaliveTracker(Context context, Handler handler, TcpKeepaliveController tcpController,
+ Dependencies deps) {
mTcpController = tcpController;
mContext = context;
+ mDependencies = deps;
- mSupportedKeepalives = KeepaliveResourceUtil.getSupportedKeepalives(context);
+ mSupportedKeepalives = mDependencies.getSupportedKeepalives(mContext);
- final ConnectivityResources res = new ConnectivityResources(mContext);
+ final ConnectivityResources res = mDependencies.createConnectivityResources(mContext);
mReservedPrivilegedSlots = res.get().getInteger(
R.integer.config_reservedPrivilegedKeepaliveSlots);
mAllowedUnprivilegedSlotsForUid = res.get().getInteger(
@@ -866,4 +868,31 @@
}
pw.decreaseIndent();
}
+
+ /**
+ * Dependencies class for testing.
+ */
+ @VisibleForTesting
+ public static class Dependencies {
+ /**
+ * Read supported keepalive count for each transport type from overlay resource. This should
+ * be used to create a local variable store of resource customization, and set as the
+ * input for {@link getSupportedKeepalivesForNetworkCapabilities}.
+ *
+ * @param context The context to read resource from.
+ * @return An array of supported keepalive count for each transport type.
+ */
+ @NonNull
+ public int[] getSupportedKeepalives(@NonNull Context context) {
+ return KeepaliveResourceUtil.getSupportedKeepalives(context);
+ }
+
+ /**
+ * Create a new {@link ConnectivityResources}.
+ */
+ @NonNull
+ public ConnectivityResources createConnectivityResources(@NonNull Context context) {
+ return new ConnectivityResources(context);
+ }
+ }
}
diff --git a/tests/unit/java/com/android/server/connectivity/AutomaticOnOffKeepaliveTrackerTest.java b/tests/unit/java/com/android/server/connectivity/AutomaticOnOffKeepaliveTrackerTest.java
index 8232658..67adcc0 100644
--- a/tests/unit/java/com/android/server/connectivity/AutomaticOnOffKeepaliveTrackerTest.java
+++ b/tests/unit/java/com/android/server/connectivity/AutomaticOnOffKeepaliveTrackerTest.java
@@ -265,7 +265,7 @@
TestKeepaliveTracker(@NonNull final Context context, @NonNull final Handler handler,
@NonNull final TcpKeepaliveController tcpController) {
- super(context, handler, tcpController);
+ super(context, handler, tcpController, new Dependencies());
}
public void setReturnedKeepaliveInfo(@NonNull final KeepaliveInfo ki) {