Fix LocationPermissionCheckerTest
The mocking of services was incorrect for services queried by class name
instead of service name.
Fix getSystemService mocking to cover both methods.
Bug: 181837977
Test: atest NetworkStaticLibTests
Change-Id: Ifc9bc26e85b5152754941bbe33bccdefff62195a
diff --git a/staticlibs/tests/unit/src/com/android/net/module/util/LocationPermissionCheckerTest.java b/staticlibs/tests/unit/src/com/android/net/module/util/LocationPermissionCheckerTest.java
index 78a21a6..3fb1e92 100644
--- a/staticlibs/tests/unit/src/com/android/net/module/util/LocationPermissionCheckerTest.java
+++ b/staticlibs/tests/unit/src/com/android/net/module/util/LocationPermissionCheckerTest.java
@@ -119,11 +119,16 @@
+ " to application bound to user " + mUid))
.when(mMockAppOps).checkPackage(mUid, TEST_PKG_NAME);
}
- when(mMockContext.getSystemService(Context.APP_OPS_SERVICE))
- .thenReturn(mMockAppOps);
- when(mMockContext.getSystemService(Context.USER_SERVICE))
- .thenReturn(mMockUserManager);
- when(mMockContext.getSystemService(Context.LOCATION_SERVICE)).thenReturn(mLocationManager);
+ mockSystemService(Context.APP_OPS_SERVICE, AppOpsManager.class, mMockAppOps);
+ mockSystemService(Context.USER_SERVICE, UserManager.class, mMockUserManager);
+ mockSystemService(Context.LOCATION_SERVICE, LocationManager.class, mLocationManager);
+ }
+
+ private <T> void mockSystemService(String name, Class<T> clazz, T service) {
+ when(mMockContext.getSystemService(name)).thenReturn(service);
+ when(mMockContext.getSystemServiceName(clazz)).thenReturn(name);
+ // Do not use mockito extended final method mocking
+ when(mMockContext.getSystemService(clazz)).thenCallRealMethod();
}
private void setupTestCase() throws Exception {