Add more tests for corner cases in role.

Also fixes the wrong assumption that the browser role must be
visible. Currently there isn't a role that must be visible.

Bug: 11417844
Test: presubmit
Change-Id: Ibfdb7d86b2e4d32b0ecf138a889e8e50a9601f19
diff --git a/tests/tests/role/src/android/app/role/cts/RoleControllerManagerTest.kt b/tests/tests/role/src/android/app/role/cts/RoleControllerManagerTest.kt
index 37d2efe..d3ebfc5 100644
--- a/tests/tests/role/src/android/app/role/cts/RoleControllerManagerTest.kt
+++ b/tests/tests/role/src/android/app/role/cts/RoleControllerManagerTest.kt
@@ -29,9 +29,11 @@
 import androidx.test.runner.AndroidJUnit4
 import com.android.compatibility.common.util.SystemUtil.runShellCommand
 import com.android.compatibility.common.util.SystemUtil.runWithShellPermissionIdentity
+import com.android.compatibility.common.util.ThrowingSupplier
 import com.google.common.truth.Truth.assertThat
 import org.junit.After
 import org.junit.Assert.assertEquals
+import org.junit.Assume.assumeTrue
 import org.junit.Before
 import org.junit.Test
 import org.junit.runner.RunWith
@@ -62,6 +64,7 @@
 
     @Test
     fun appIsVisibleForRole() {
+        assumeRoleIsVisible()
         assertAppIsVisibleForRole(APP_PACKAGE_NAME, ROLE_NAME, true)
     }
 
@@ -100,32 +103,29 @@
         }
     }
 
-    @Test
-    fun roleIsVisible() {
-        assertRoleIsVisible(ROLE_NAME, true)
+    private fun assumeRoleIsVisible() {
+        assumeTrue(isRoleVisible(ROLE_NAME))
     }
 
     @Test
     fun systemGalleryRoleIsNotVisible() {
         // The system gallery role should always be hidden.
-        assertRoleIsVisible(SYSTEM_GALLERY_ROLE_NAME, false)
+        assertThat(isRoleVisible(SYSTEM_GALLERY_ROLE_NAME)).isEqualTo(false)
     }
 
     @Test
     fun invalidRoleIsNotVisible() {
-        assertRoleIsVisible("invalid", false)
+        assertThat(isRoleVisible("invalid")).isEqualTo(false)
     }
 
-    private fun assertRoleIsVisible(roleName: String, expectedIsVisible: Boolean) {
-        runWithShellPermissionIdentity {
+    private fun isRoleVisible(roleName: String): Boolean =
+        runWithShellPermissionIdentity(ThrowingSupplier {
             val future = CompletableFuture<Boolean>()
             roleControllerManager.isRoleVisible(
                 roleName, context.mainExecutor, Consumer { future.complete(it) }
             )
-            val isVisible = future.get(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)
-            assertThat(isVisible).isEqualTo(expectedIsVisible)
-        }
-    }
+            future.get(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)
+        })
 
     private fun installPackage(apkPath: String) {
         assertEquals(
diff --git a/tests/tests/role/src/android/app/role/cts/RoleManagerTest.java b/tests/tests/role/src/android/app/role/cts/RoleManagerTest.java
index a67a50e..60cac6a 100644
--- a/tests/tests/role/src/android/app/role/cts/RoleManagerTest.java
+++ b/tests/tests/role/src/android/app/role/cts/RoleManagerTest.java
@@ -77,8 +77,6 @@
 @RunWith(AndroidJUnit4.class)
 public class RoleManagerTest {
 
-    private static final String LOG_TAG = "RoleManagerTest";
-
     private static final long TIMEOUT_MILLIS = 15 * 1000;
 
     private static final long UNEXPECTED_TIMEOUT_MILLIS = 1000;
@@ -744,6 +742,26 @@
     }
 
     @Test
+    public void addInvalidRoleHolderThenFails() throws Exception {
+        addRoleHolder("invalid", APP_PACKAGE_NAME, false);
+    }
+
+    @Test
+    public void addUnqualifiedRoleHolderThenFails() throws Exception {
+        addRoleHolder(RoleManager.ROLE_HOME, APP_PACKAGE_NAME, false);
+    }
+
+    @Test
+    public void removeInvalidRoleHolderThenFails() throws Exception {
+        removeRoleHolder("invalid", APP_PACKAGE_NAME, false);
+    }
+
+    @Test
+    public void clearInvalidRoleHoldersThenFails() throws Exception {
+        clearRoleHolders("invalid", false);
+    }
+
+    @Test
     public void addOnRoleHoldersChangedListenerAndAddRoleHolderThenIsNotified() throws Exception {
         assertOnRoleHoldersChangedListenerIsNotified(() -> addRoleHolder(ROLE_NAME,
                 APP_PACKAGE_NAME));
@@ -919,27 +937,42 @@
         }
      }
 
-    private void addRoleHolder(@NonNull String roleName, @NonNull String packageName)
-            throws Exception {
+    private void addRoleHolder(@NonNull String roleName, @NonNull String packageName,
+            boolean expectSuccess) throws Exception {
         CallbackFuture future = new CallbackFuture();
         runWithShellPermissionIdentity(() -> sRoleManager.addRoleHolderAsUser(roleName,
                 packageName, 0, Process.myUserHandle(), sContext.getMainExecutor(), future));
-        future.get(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
+        assertThat(future.get(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)).isEqualTo(expectSuccess);
+    }
+
+    private void addRoleHolder(@NonNull String roleName, @NonNull String packageName)
+            throws Exception {
+        addRoleHolder(roleName, packageName, true);
+    }
+
+    private void removeRoleHolder(@NonNull String roleName, @NonNull String packageName,
+            boolean expectSuccess) throws Exception {
+        CallbackFuture future = new CallbackFuture();
+        runWithShellPermissionIdentity(() -> sRoleManager.removeRoleHolderAsUser(roleName,
+                packageName, 0, Process.myUserHandle(), sContext.getMainExecutor(), future));
+        assertThat(future.get(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)).isEqualTo(expectSuccess);
     }
 
     private void removeRoleHolder(@NonNull String roleName, @NonNull String packageName)
             throws Exception {
-        CallbackFuture future = new CallbackFuture();
-        runWithShellPermissionIdentity(() -> sRoleManager.removeRoleHolderAsUser(roleName,
-                packageName, 0, Process.myUserHandle(), sContext.getMainExecutor(), future));
-        future.get(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
+        removeRoleHolder(roleName, packageName, true);
     }
 
-    private void clearRoleHolders(@NonNull String roleName) throws Exception {
+    private void clearRoleHolders(@NonNull String roleName, boolean expectSuccess)
+            throws Exception {
         CallbackFuture future = new CallbackFuture();
         runWithShellPermissionIdentity(() -> sRoleManager.clearRoleHoldersAsUser(roleName, 0,
                 Process.myUserHandle(), sContext.getMainExecutor(), future));
-        future.get(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
+        assertThat(future.get(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)).isEqualTo(expectSuccess);
+    }
+
+    private void clearRoleHolders(@NonNull String roleName) throws Exception {
+        clearRoleHolders(roleName, true);
     }
 
     private static class ListenerFuture extends CompletableFuture<Pair<String, UserHandle>>
@@ -951,16 +984,12 @@
         }
     }
 
-    private static class CallbackFuture extends CompletableFuture<Void>
+    private static class CallbackFuture extends CompletableFuture<Boolean>
             implements Consumer<Boolean> {
 
         @Override
         public void accept(Boolean successful) {
-            if (successful) {
-                complete(null);
-            } else {
-                completeExceptionally(new RuntimeException());
-            }
+            complete(successful);
         }
     }
 }