Add test for using permission name as part of a word in javadoc
If `@RequiresPermission` is used without setting `conditional = true`
then text will be automatically added to the documentation explaining
that the permission is required. Metalava attempts to detect when the
documentation already mentions the permission and reports that as an
issue to avoid duplicating the documentation. However, its heuristic
for determining whether the permission is mentioned is simplistic and
only checks to see if the permission is mentioned in the text at all.
For most permissions that works because the permission name is very
specific and unlikely to occur anywhere else but some permissions, e.g.
`DUMP` could very easily be present in the documentation for some other
reason.
This change adds a test to show the issue. A follow-up change will fix
it. The test fails on Turbine because Turbine does not yet support
manipulating the javadoc like Psi does.
Bug: 367787970
Test: ./gradlew
Change-Id: Ib8d65dea8f8c7948e4278cf8c9bc102b2427dd92
diff --git a/metalava/src/test/java/com/android/tools/metalava/AndroidApiChecksTest.kt b/metalava/src/test/java/com/android/tools/metalava/AndroidApiChecksTest.kt
index 3fab285..c7ce30f 100644
--- a/metalava/src/test/java/com/android/tools/metalava/AndroidApiChecksTest.kt
+++ b/metalava/src/test/java/com/android/tools/metalava/AndroidApiChecksTest.kt
@@ -149,6 +149,62 @@
}
@Test
+ fun `Document Permissions ignore when permission is subset of a word`() {
+ check(
+ sourceFiles =
+ arrayOf(
+ java(
+ """
+ package android;
+
+ public abstract class Manifest {
+ public static final class permission {
+ public static final String PERMISSION = "android.permission.PERMISSION";
+ }
+ }
+ """
+ ),
+ requiresPermissionSource,
+ java(
+ """
+ package android.pkg;
+
+ import android.Manifest;
+ import android.annotation.RequiresPermission;
+
+ public class PermissionTest {
+ /**
+ * While this contains the name of the permission it is not actually
+ * referring to the permission ARG_PERMISSION.
+ */
+ @RequiresPermission(Manifest.permission.PERMISSION)
+ public void test0() {
+ }
+
+ /**
+ * While this contains the name of the permission it is not actually
+ * referring to the permission PERMISSION_ARG.
+ */
+ @RequiresPermission(Manifest.permission.PERMISSION)
+ public void test0() {
+ }
+ }
+ """
+ ),
+ ),
+ extraArguments =
+ arrayOf(ARG_WARNING, Issues.CONDITIONAL_REQUIRES_PERMISSION_NOT_EXPLAINED.name),
+ expectedFail = DefaultLintErrorMessage,
+ // TODO(b/367787970): Stop reporting issues when permission is just a subset of a word.
+ expectedIssues =
+ """
+ src/android/pkg/PermissionTest.java:12: error: Method 'test0' documentation duplicates auto-generated documentation by @RequiresPermission. If the permissions are only required under certain circumstances use conditional=true to suppress the auto-documentation [RequiresPermission]
+ src/android/pkg/PermissionTest.java:20: error: Method 'test0' documentation duplicates auto-generated documentation by @RequiresPermission. If the permissions are only required under certain circumstances use conditional=true to suppress the auto-documentation [RequiresPermission]
+ """,
+ )
+ }
+
+ @Test
fun `Document Intent Actions`() {
check(
expectedFail = DefaultLintErrorMessage,
diff --git a/metalava/src/test/resources/source-model-provider-baseline.txt b/metalava/src/test/resources/source-model-provider-baseline.txt
index a6dafa8..8caf026 100644
--- a/metalava/src/test/resources/source-model-provider-baseline.txt
+++ b/metalava/src/test/resources/source-model-provider-baseline.txt
@@ -1,4 +1,5 @@
com.android.tools.metalava.AndroidApiChecksTest
+ Document Permissions ignore when permission is subset of a word[turbine]
Document Permissions[turbine]
com.android.tools.metalava.AnnotationsMergerTest