Add mockService method.
Add a mockService method to support the common usage that requires
mocking the system service from with a mock context.
Bug: 231373589
Test: atest FrameworksNetTests
Change-Id: Id17af4027a019834c1779be550f4329b1edd6809
diff --git a/staticlibs/testutils/devicetests/com/android/testutils/ContextUtils.kt b/staticlibs/testutils/devicetests/com/android/testutils/ContextUtils.kt
index 814a75b..936b568 100644
--- a/staticlibs/testutils/devicetests/com/android/testutils/ContextUtils.kt
+++ b/staticlibs/testutils/devicetests/com/android/testutils/ContextUtils.kt
@@ -53,3 +53,15 @@
asUserContext
}.`when`(context).createContextAsUser(any(UserHandle::class.java), anyInt() /* flags */)
}
+
+/**
+ * Helper function to mock the desired system service.
+ *
+ * @param context the mock context to set up the getSystemService and getSystemServiceName.
+ * @param clazz the system service class that intents to mock.
+ * @param service the system service name that intents to mock.
+ */
+fun <T> mockService(context: Context, clazz: Class<T>, name: String, service: T) {
+ doReturn(service).`when`(context).getSystemService(name)
+ doReturn(name).`when`(context).getSystemServiceName(clazz)
+}
\ No newline at end of file