Update CrossProfileHostTest to work for auto

- Skip user 0 check in setup if it is automotive :
  auto device will have non-user 0 as current user.

- Check support of managed_users for all tests using profile
  user.

Bug: 152081437
Test: atest android.content.cts.ContextCrossProfileHostTest

Change-Id: Ia54c03aef24706ae8face5344c10aae079b73fa8
diff --git a/hostsidetests/content/src/android/content/cts/ContextCrossProfileHostTest.java b/hostsidetests/content/src/android/content/cts/ContextCrossProfileHostTest.java
index f02e901..0e473f2 100644
--- a/hostsidetests/content/src/android/content/cts/ContextCrossProfileHostTest.java
+++ b/hostsidetests/content/src/android/content/cts/ContextCrossProfileHostTest.java
@@ -30,6 +30,7 @@
 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
 import com.android.cts.devicepolicy.metrics.DevicePolicyEventWrapper;
 import com.android.tradefed.build.IBuildInfo;
+import com.android.tradefed.device.DeviceNotAvailableException;
 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
 import com.android.tradefed.testtype.IBuildReceiver;
 
@@ -73,7 +74,10 @@
         assumeTrue(mSupportsMultiUser);
 
         mParentUserId = getDevice().getCurrentUser();
-        assertEquals(USER_SYSTEM, mParentUserId);
+        // Automotive uses non-system user as current user always
+        if (!getDevice().hasFeature("feature:android.hardware.type.automotive")) {
+            assertEquals(USER_SYSTEM, mParentUserId);
+        }
 
         CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(mCtsBuild);
         mApkFile = buildHelper.getTestFile(TEST_WITH_PERMISSION_APK);
@@ -99,6 +103,7 @@
     @Test
     public void testBindServiceAsUser_differentUser_bindsServiceToCorrectUser()
             throws Exception {
+        assumeTrue(supportsManagedUsers());
         int userInSameProfileGroup = createProfile(mParentUserId);
         getDevice().startUser(userInSameProfileGroup, /* waitFlag= */true);
         mTestArgs.put("testUser", Integer.toString(userInSameProfileGroup));
@@ -126,6 +131,7 @@
     @Test
     public void testBindServiceAsUser_sameProfileGroup_samePackage_withAcrossUsersPermission_bindsService()
             throws Exception {
+        assumeTrue(supportsManagedUsers());
         int userInSameProfileGroup = createProfile(mParentUserId);
         getDevice().startUser(userInSameProfileGroup, /* waitFlag= */true);
         mTestArgs.put("testUser", Integer.toString(userInSameProfileGroup));
@@ -153,6 +159,7 @@
     @Test
     public void testBindServiceAsUser_sameProfileGroup_differentPackage_withAcrossUsersPermission_bindsService()
             throws Exception {
+        assumeTrue(supportsManagedUsers());
         int userInSameProfileGroup = createProfile(mParentUserId);
         getDevice().startUser(userInSameProfileGroup, /* waitFlag= */true);
         mTestArgs.put("testUser", Integer.toString(userInSameProfileGroup));
@@ -180,6 +187,7 @@
     @Test
     public void testBindServiceAsUser_sameProfileGroup_samePackage_withAcrossProfilesPermission_bindsService()
             throws Exception {
+        assumeTrue(supportsManagedUsers());
         int userInSameProfileGroup = createProfile(mParentUserId);
         getDevice().startUser(userInSameProfileGroup, /* waitFlag= */true);
         mTestArgs.put("testUser", Integer.toString(userInSameProfileGroup));
@@ -207,6 +215,7 @@
     @Test
     public void testBindServiceAsUser_sameProfileGroup_differentPackage_withAcrossProfilesPermission_throwsException()
             throws Exception {
+        assumeTrue(supportsManagedUsers());
         int userInSameProfileGroup = createProfile(mParentUserId);
         getDevice().startUser(userInSameProfileGroup, /* waitFlag= */true);
         mTestArgs.put("testUser", Integer.toString(userInSameProfileGroup));
@@ -234,6 +243,7 @@
     @Test
     public void testBindServiceAsUser_sameProfileGroup_samePackage_withAcrossProfilesAppOp_bindsService()
             throws Exception {
+        assumeTrue(supportsManagedUsers());
         int userInSameProfileGroup = createProfile(mParentUserId);
         getDevice().startUser(userInSameProfileGroup, /* waitFlag= */true);
         mTestArgs.put("testUser", Integer.toString(userInSameProfileGroup));
@@ -261,6 +271,7 @@
     @Test
     public void testBindServiceAsUser_sameProfileGroup_differentPackage_withAcrossProfilesAppOp_throwsException()
             throws Exception {
+        assumeTrue(supportsManagedUsers());
         int userInSameProfileGroup = createProfile(mParentUserId);
         getDevice().startUser(userInSameProfileGroup, /* waitFlag= */true);
         mTestArgs.put("testUser", Integer.toString(userInSameProfileGroup));
@@ -375,6 +386,7 @@
     @Test
     public void testBindServiceAsUser_sameProfileGroup_withNoPermissions_throwsException()
             throws Exception {
+        assumeTrue(supportsManagedUsers());
         int userInSameProfileGroup = createProfile(mParentUserId);
         getDevice().startUser(userInSameProfileGroup, /* waitFlag= */true);
         mTestArgs.put("testUser", Integer.toString(userInSameProfileGroup));
@@ -402,9 +414,8 @@
     @Test
     public void testBindServiceAsUser_sameProfileGroup_reportsMetric()
             throws Exception {
-        if (!isStatsdEnabled(getDevice())) {
-            return;
-        }
+        assumeTrue(isStatsdEnabled(getDevice()));
+        assumeTrue(supportsManagedUsers());
         int userInSameProfileGroup = createProfile(mParentUserId);
         getDevice().startUser(userInSameProfileGroup, /* waitFlag= */ true);
         mTestArgs.put("testUser", Integer.toString(userInSameProfileGroup));
@@ -444,9 +455,7 @@
     @Test
     public void testBindServiceAsUser_differentProfileGroup_doesNotReportMetric()
             throws Exception {
-        if (!isStatsdEnabled(getDevice())) {
-            return;
-        }
+        assumeTrue(isStatsdEnabled(getDevice()));
         int userInDifferentProfileGroup = createUser();
         getDevice().startUser(userInDifferentProfileGroup, /* waitFlag= */ true);
         mTestArgs.put("testUser", Integer.toString(userInDifferentProfileGroup));
@@ -483,9 +492,8 @@
     @Test
     public void testBindServiceAsUser_sameUser_doesNotReportMetric()
             throws Exception {
-        if (!isStatsdEnabled(getDevice())) {
-            return;
-        }
+        assumeTrue(isStatsdEnabled(getDevice()));
+
         mTestArgs.put("testUser", Integer.toString(mParentUserId));
 
         assertMetricsNotLogged(getDevice(), () -> {
@@ -506,6 +514,7 @@
     @Test
     public void testCreateContextAsUser_sameProfileGroup_withInteractAcrossProfilesPermission_throwsException()
             throws Exception {
+        assumeTrue(supportsManagedUsers());
         int userInSameProfileGroup = createProfile(mParentUserId);
         getDevice().startUser(userInSameProfileGroup, /* waitFlag= */true);
         mTestArgs.put("testUser", Integer.toString(userInSameProfileGroup));
@@ -533,6 +542,7 @@
     @Test
     public void testCreateContextAsUser_sameProfileGroup_withInteractAcrossUsersPermission_createsContext()
             throws Exception {
+        assumeTrue(supportsManagedUsers());
         int userInSameProfileGroup = createProfile(mParentUserId);
         getDevice().startUser(userInSameProfileGroup, /* waitFlag= */true);
         mTestArgs.put("testUser", Integer.toString(userInSameProfileGroup));
@@ -556,4 +566,12 @@
                 /* timeout= */60L,
                 TimeUnit.SECONDS);
     }
+
+    boolean supportsManagedUsers() {
+        try {
+            return getDevice().hasFeature("feature:android.software.managed_users");
+        } catch (DeviceNotAvailableException e) {
+            return false;
+        }
+    }
 }