summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Remi NGUYEN VAN <reminv@google.com> 2020-03-19 11:25:50 +0900
committer Remi NGUYEN VAN <reminv@google.com> 2020-03-23 03:25:51 +0000
commita24a9f710e371e276664a74e882a510394427664 (patch)
tree641d056cc805aaffba2df11d05172c64bd10063f
parentd49a77f6e61d226bf799a59647dfc4b7f4016bf1 (diff)
Add a test API for testing NetworkStack.getService
NetworkStack.getService was introduced to avoid relying on Context#getSystemService to obtain the NetworkStack binder token. To allow it to be mocked in tests, a method is introduced to allow tests to specify a mock NetworkStack token for their own process. Test: atest NetworkStackTests, using the change Bug: 151243982 (clean cherry-pick from internal branch) Merged-In: I04058a007f2dfe1044cabeb3ac508404873665ad Change-Id: I0e626d871176112a8575d629a05cb7a935b577a6
-rw-r--r--api/test-current.txt1
-rw-r--r--core/java/android/net/NetworkStack.java17
2 files changed, 18 insertions, 0 deletions
diff --git a/api/test-current.txt b/api/test-current.txt
index c776e6f3f31b..723f08b49102 100644
--- a/api/test-current.txt
+++ b/api/test-current.txt
@@ -1518,6 +1518,7 @@ package android.net {
public class NetworkStack {
method @Nullable public static android.os.IBinder getService();
+ method public static void setServiceForTest(@Nullable android.os.IBinder);
field public static final String PERMISSION_MAINLINE_NETWORK_STACK = "android.permission.MAINLINE_NETWORK_STACK";
}
diff --git a/core/java/android/net/NetworkStack.java b/core/java/android/net/NetworkStack.java
index a6d52d133ce9..86f3dfd412f6 100644
--- a/core/java/android/net/NetworkStack.java
+++ b/core/java/android/net/NetworkStack.java
@@ -45,6 +45,9 @@ public class NetworkStack {
public static final String PERMISSION_MAINLINE_NETWORK_STACK =
"android.permission.MAINLINE_NETWORK_STACK";
+ @Nullable
+ private static volatile IBinder sMockService;
+
/**
* Get an {@link IBinder} representing the NetworkStack stable AIDL Interface, if registered.
* @hide
@@ -53,9 +56,23 @@ public class NetworkStack {
@SystemApi
@TestApi
public static IBinder getService() {
+ final IBinder mockService = sMockService;
+ if (mockService != null) return mockService;
return ServiceManager.getService(Context.NETWORK_STACK_SERVICE);
}
+ /**
+ * Set a mock service for testing, to be returned by future calls to {@link #getService()}.
+ *
+ * <p>Passing a {@code null} {@code mockService} resets {@link #getService()} to normal
+ * behavior.
+ * @hide
+ */
+ @TestApi
+ public static void setServiceForTest(@Nullable IBinder mockService) {
+ sMockService = mockService;
+ }
+
private NetworkStack() {}
/**