Click allow button by text in UndefinedGroupPermissionTest

Automotive uses a standard system dialog for it's permission dialog,
and thus doesn't have the ids that are being referenced in this test.

Fixes: 162000537
Test: m cts, cts-tradefed, run cts -m CtsPermissionTestCases -t android.permission.cts.UndefinedGroupPermissionTest
Change-Id: I1a667508e6fff8985b669019e33324c3b75acee9
diff --git a/tests/tests/permission/src/android/permission/cts/UndefinedGroupPermissionTest.kt b/tests/tests/permission/src/android/permission/cts/UndefinedGroupPermissionTest.kt
index ba9212b..d293c70 100644
--- a/tests/tests/permission/src/android/permission/cts/UndefinedGroupPermissionTest.kt
+++ b/tests/tests/permission/src/android/permission/cts/UndefinedGroupPermissionTest.kt
@@ -25,6 +25,7 @@
 import android.os.Process
 import android.support.test.uiautomator.By
 import android.support.test.uiautomator.UiDevice
+import android.support.test.uiautomator.UiObject2
 import android.support.test.uiautomator.UiObjectNotFoundException
 import androidx.test.platform.app.InstrumentationRegistry
 import com.android.compatibility.common.util.SystemUtil
@@ -34,6 +35,7 @@
 import org.junit.Assert
 import org.junit.Before
 import org.junit.Test
+import java.util.regex.Pattern
 
 /**
  * Tests that the permissioncontroller behaves normally when an app defines a permission in the
@@ -44,11 +46,12 @@
     private var mUiDevice: UiDevice? = null
     private var mContext: Context? = null
     private var mPm: PackageManager? = null
+    private var mAllowButtonText: Pattern? = null
 
     @Before
     fun install() {
         SystemUtil.runShellCommand("pm install -r " +
-                "$TEST_APP_DEFINES_UNDEFINED_PERMISSION_GROUP_ELEMENT_APK")
+                TEST_APP_DEFINES_UNDEFINED_PERMISSION_GROUP_ELEMENT_APK)
     }
 
     @Before
@@ -57,6 +60,14 @@
         mUiDevice = UiDevice.getInstance(mInstrumentation)
         mContext = mInstrumentation?.targetContext
         mPm = mContext?.packageManager
+        val permissionControllerResources = mContext?.createPackageContext(
+                mContext?.packageManager?.permissionControllerPackageName, 0)?.resources
+        mAllowButtonText = Pattern.compile(
+                Pattern.quote(requireNotNull(permissionControllerResources?.getString(
+                        permissionControllerResources.getIdentifier(
+                                "grant_dialog_button_allow", "string",
+                                "com.android.permissioncontroller")))),
+                Pattern.CASE_INSENSITIVE or Pattern.UNICODE_CASE)
     }
 
     @Before
@@ -94,9 +105,7 @@
         eventually {
             startRequestActivity(arrayOf(TEST))
             mUiDevice!!.waitForIdle()
-            waitFindObject(By.res(
-                    "com.android.permissioncontroller:id/permission_allow_button"),
-                    100).click()
+            findAllowButton().click()
         }
         eventually {
             Assert.assertEquals(mPm!!.checkPermission(CAMERA, APP_PKG_NAME),
@@ -113,6 +122,17 @@
         SystemUtil.runShellCommand("pm uninstall $APP_PKG_NAME")
     }
 
+    fun findAllowButton(): UiObject2 {
+        return if (mContext?.packageManager
+                        ?.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE) == true) {
+            waitFindObject(By.text(mAllowButtonText), 100)
+        } else {
+            waitFindObject(By.res(
+                    "com.android.permissioncontroller:id/permission_allow_button"),
+                    100)
+        }
+    }
+
     /**
      * If app has one permission granted, then it can't grant itself another permission for free.
      */
@@ -131,7 +151,12 @@
             startRequestActivity(arrayOf(targetPermission))
             mUiDevice!!.waitForIdle()
             try {
-                waitFindObject(By.res("com.android.permissioncontroller:id/grant_dialog"), 100)
+                if (mContext?.packageManager
+                                ?.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE) == true) {
+                    findAllowButton()
+                } else {
+                    waitFindObject(By.res("com.android.permissioncontroller:id/grant_dialog"), 100)
+                }
             } catch (e: UiObjectNotFoundException) {
                 Assert.assertEquals("grant dialog never showed.",
                         mPm!!.checkPermission(targetPermission,