Increase CarUserService class code coverage
- Excludes methods that are going to be moved to
ExperimentalCarUserManager
- Adds more unit tests
Fixes: 190839545
Test: atest CarServiceUnitTest:CarUserServiceTest \
CarSecurityPermissionTest
Change-Id: Ic4c9e4fd6badcfabf8b1e4e16ecaa37be10d5079
diff --git a/service/src/com/android/car/user/CarUserService.java b/service/src/com/android/car/user/CarUserService.java
index e61419b..1fde509 100644
--- a/service/src/com/android/car/user/CarUserService.java
+++ b/service/src/com/android/car/user/CarUserService.java
@@ -22,6 +22,7 @@
import static com.android.car.PermissionHelper.checkHasAtLeastOnePermissionGranted;
import static com.android.car.PermissionHelper.checkHasDumpPermissionGranted;
+import static com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport.DEPRECATED_CODE;
import static com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport.DUMP_INFO;
import android.annotation.NonNull;
@@ -475,6 +476,8 @@
* @see ExperimentalCarUserManager.createDriver
*/
@Override
+ @ExcludeFromCodeCoverageGeneratedReport(reason = DEPRECATED_CODE,
+ details = "TODO(b/172262561) remove annotation after refactoring")
public AndroidFuture<UserCreationResult> createDriver(@NonNull String name, boolean admin) {
checkManageUsersPermission("createDriver");
Objects.requireNonNull(name, "name cannot be null");
@@ -510,6 +513,8 @@
*/
@Override
@Nullable
+ @ExcludeFromCodeCoverageGeneratedReport(reason = DEPRECATED_CODE,
+ details = "TODO(b/172262561) remove annotation after refactoring")
public UserInfo createPassenger(@NonNull String name, @UserIdInt int driverId) {
checkManageUsersPermission("createPassenger");
Objects.requireNonNull(name, "name cannot be null");
@@ -540,6 +545,8 @@
* @see ExperimentalCarUserManager.switchDriver
*/
@Override
+ @ExcludeFromCodeCoverageGeneratedReport(reason = DEPRECATED_CODE,
+ details = "TODO(b/172262561) remove annotation after refactoring")
public void switchDriver(@UserIdInt int driverId, AndroidFuture<UserSwitchResult> receiver) {
checkManageUsersPermission("switchDriver");
if (UserHelperLite.isHeadlessSystemUser(driverId)) {
diff --git a/tests/CarSecurityPermissionTest/src/com/android/car/user/CarUserManagerPermissionTest.java b/tests/CarSecurityPermissionTest/src/com/android/car/user/CarUserManagerPermissionTest.java
index 63c5ece..b255536 100644
--- a/tests/CarSecurityPermissionTest/src/com/android/car/user/CarUserManagerPermissionTest.java
+++ b/tests/CarSecurityPermissionTest/src/com/android/car/user/CarUserManagerPermissionTest.java
@@ -125,7 +125,6 @@
Exception e = expectThrows(SecurityException.class,
() -> mCarUserManager.addListener(Runnable::run, listener));
-
assertThat(e.getMessage()).contains(INTERACT_ACROSS_USERS);
assertThat(e.getMessage()).contains(INTERACT_ACROSS_USERS_FULL);
}
@@ -138,7 +137,6 @@
Exception e = expectThrows(SecurityException.class,
() -> mCarUserManager.removeListener(listener));
-
assertThat(e.getMessage()).contains(INTERACT_ACROSS_USERS);
assertThat(e.getMessage()).contains(INTERACT_ACROSS_USERS_FULL);
}
@@ -167,8 +165,10 @@
@Test
public void testSetUserSwitchUiCallback() {
+ CarUserManager.UserSwitchUiCallback callback = (u)-> { };
+
Exception e = expectThrows(SecurityException.class,
- () -> mCarUserManager.getUserIdentificationAssociation(CUSTOM_1));
+ () -> mCarUserManager.setUserSwitchUiCallback(callback));
assertThat(e.getMessage()).contains(MANAGE_USERS);
}
}
diff --git a/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java b/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java
index bc6c7ea..1685a71 100644
--- a/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java
@@ -2208,6 +2208,15 @@
}
@Test
+ @ExpectWtf
+ public void testSetInitialUser_nullUser() throws Exception {
+ mCarUserService.setInitialUser(null);
+
+ mockInteractAcrossUsersPermission(true);
+ assertThat(mCarUserService.getInitialUser()).isNull();
+ }
+
+ @Test
public void testOnSuspend_replace() throws Exception {
mockExistingUsersAndCurrentUser(mGuestUser);
when(mInitialUserSetter.canReplaceGuestUser(any())).thenReturn(true);
@@ -2672,13 +2681,22 @@
}).when(mUserHal).setUserAssociation(eq(mAsyncCallTimeoutMs), notNull(), notNull());
}
+ private void mockInteractAcrossUsersPermission(boolean granted) {
+ int result = granted ? android.content.pm.PackageManager.PERMISSION_GRANTED
+ : android.content.pm.PackageManager.PERMISSION_DENIED;
+
+ doReturn(result).when(() -> ActivityManager.checkComponentPermission(
+ eq(android.Manifest.permission.INTERACT_ACROSS_USERS),
+ anyInt(), anyInt(), eq(true)));
+ doReturn(result).when(() -> ActivityManager.checkComponentPermission(
+ eq(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL),
+ anyInt(), anyInt(), eq(true)));
+ }
+
private void mockManageUsersPermission(String permission, boolean granted) {
- int result;
- if (granted) {
- result = android.content.pm.PackageManager.PERMISSION_GRANTED;
- } else {
- result = android.content.pm.PackageManager.PERMISSION_DENIED;
- }
+ int result = granted ? android.content.pm.PackageManager.PERMISSION_GRANTED
+ : android.content.pm.PackageManager.PERMISSION_DENIED;
+
doReturn(result).when(() -> ActivityManager.checkComponentPermission(eq(permission),
anyInt(), anyInt(), eq(true)));
}