From c60efbd18b4f5d9ee9e0809d25c5de0b70669f92 Mon Sep 17 00:00:00 2001 From: hotchemi Date: Mon, 3 Dec 2018 01:24:21 +0900 Subject: [PATCH 1/3] Add validator method to check special permissions with @OnNeverAskAgain annotation. --- .../processor/RuntimePermissionsElement.kt | 15 ++---------- ...alPermissionsWithNeverAskAgainException.kt | 3 +++ .../dispatcher/processor/util/Validators.kt | 23 ++++++++++--------- 3 files changed, 17 insertions(+), 24 deletions(-) create mode 100644 processor/src/main/kotlin/permissions/dispatcher/processor/exception/SpecialPermissionsWithNeverAskAgainException.kt diff --git a/processor/src/main/kotlin/permissions/dispatcher/processor/RuntimePermissionsElement.kt b/processor/src/main/kotlin/permissions/dispatcher/processor/RuntimePermissionsElement.kt index 8e2fb904..007ea158 100644 --- a/processor/src/main/kotlin/permissions/dispatcher/processor/RuntimePermissionsElement.kt +++ b/processor/src/main/kotlin/permissions/dispatcher/processor/RuntimePermissionsElement.kt @@ -8,19 +8,7 @@ import permissions.dispatcher.NeedsPermission import permissions.dispatcher.OnNeverAskAgain import permissions.dispatcher.OnPermissionDenied import permissions.dispatcher.OnShowRationale -import permissions.dispatcher.processor.util.GEN_CLASS_SUFFIX -import permissions.dispatcher.processor.util.checkDuplicatedMethodName -import permissions.dispatcher.processor.util.checkDuplicatedValue -import permissions.dispatcher.processor.util.checkMethodParameters -import permissions.dispatcher.processor.util.checkMethodSignature -import permissions.dispatcher.processor.util.checkMixPermissionType -import permissions.dispatcher.processor.util.checkNotEmpty -import permissions.dispatcher.processor.util.checkPrivateMethods -import permissions.dispatcher.processor.util.childElementsAnnotatedWith -import permissions.dispatcher.processor.util.findMatchingMethodForNeeds -import permissions.dispatcher.processor.util.packageName -import permissions.dispatcher.processor.util.simpleString -import permissions.dispatcher.processor.util.typeMirrorOf +import permissions.dispatcher.processor.util.* import javax.lang.model.element.ExecutableElement import javax.lang.model.element.TypeElement @@ -73,6 +61,7 @@ class RuntimePermissionsElement(e: TypeElement) { checkPrivateMethods(onNeverAskElements, OnNeverAskAgain::class.java) checkMethodSignature(onNeverAskElements) checkMethodParameters(onNeverAskElements, 0) + checkSpecialPermissionsWithNeverAskAgain(onNeverAskElements) } /* Begin public */ diff --git a/processor/src/main/kotlin/permissions/dispatcher/processor/exception/SpecialPermissionsWithNeverAskAgainException.kt b/processor/src/main/kotlin/permissions/dispatcher/processor/exception/SpecialPermissionsWithNeverAskAgainException.kt new file mode 100644 index 00000000..a8141d17 --- /dev/null +++ b/processor/src/main/kotlin/permissions/dispatcher/processor/exception/SpecialPermissionsWithNeverAskAgainException.kt @@ -0,0 +1,3 @@ +package permissions.dispatcher.processor.exception + +class SpecialPermissionsWithNeverAskAgainException : RuntimeException("@NeverAskAgain annotated method never being called with WRITE_SETTINGS or SYSTEM_ALERT_WINDOW permission.") \ No newline at end of file diff --git a/processor/src/main/kotlin/permissions/dispatcher/processor/util/Validators.kt b/processor/src/main/kotlin/permissions/dispatcher/processor/util/Validators.kt index 17ae19a8..d725d68a 100644 --- a/processor/src/main/kotlin/permissions/dispatcher/processor/util/Validators.kt +++ b/processor/src/main/kotlin/permissions/dispatcher/processor/util/Validators.kt @@ -1,18 +1,10 @@ package permissions.dispatcher.processor.util +import permissions.dispatcher.OnNeverAskAgain import permissions.dispatcher.processor.ProcessorUnit import permissions.dispatcher.processor.RuntimePermissionsElement import permissions.dispatcher.processor.TYPE_UTILS -import permissions.dispatcher.processor.exception.DuplicatedMethodNameException -import permissions.dispatcher.processor.exception.DuplicatedValueException -import permissions.dispatcher.processor.exception.MixPermissionTypeException -import permissions.dispatcher.processor.exception.NoAnnotatedMethodsException -import permissions.dispatcher.processor.exception.NoParametersAllowedException -import permissions.dispatcher.processor.exception.NoThrowsAllowedException -import permissions.dispatcher.processor.exception.PrivateMethodException -import permissions.dispatcher.processor.exception.WrongClassException -import permissions.dispatcher.processor.exception.WrongParametersException -import permissions.dispatcher.processor.exception.WrongReturnTypeException +import permissions.dispatcher.processor.exception.* import java.util.* import javax.lang.model.element.Element import javax.lang.model.element.ExecutableElement @@ -27,7 +19,7 @@ private const val SYSTEM_ALERT_WINDOW = "android.permission.SYSTEM_ALERT_WINDOW" * Obtains the [ProcessorUnit] implementation for the provided element. * Raises an exception if no suitable implementation exists */ -fun findAndValidateProcessorUnit(units: List>, element: Element) : ProcessorUnit { +fun findAndValidateProcessorUnit(units: List>, element: Element): ProcessorUnit { val type = element.asType() try { return units.first { type.isSubtypeOf(it.getTargetType()) } @@ -131,6 +123,15 @@ fun checkMixPermissionType(items: List, anno } } +fun checkSpecialPermissionsWithNeverAskAgain(items: List, annotationClass: Class = OnNeverAskAgain::class.java) { + items.forEach { + val permissionValue = it.getAnnotation(annotationClass).permissionValue() + if (permissionValue.contains(WRITE_SETTINGS) || permissionValue.contains(SYSTEM_ALERT_WINDOW)) { + throw SpecialPermissionsWithNeverAskAgainException() + } + } +} + fun checkDuplicatedMethodName(items: List) { items.forEach { item -> items.firstOrNull { it != item && it.simpleName == item.simpleName }?.let { From 2c011d8e2b8a030a1fb976b70bbcb14b43a8d0cc Mon Sep 17 00:00:00 2001 From: hotchemi Date: Mon, 3 Dec 2018 03:42:20 +0900 Subject: [PATCH 2/3] Fix failed tests. --- .../dispatcher/processor/data/Source.java | 54 +++---------------- ...tyWithSystemAlertWindowAllAnnotations.java | 5 -- ...tyWithSystemAlertWindowKtAllAnnotations.kt | 4 -- ...ctivityWithWriteSettingAllAnnotations.java | 5 -- ...ctivityWithWriteSettingKtAllAnnotations.kt | 4 -- ...AllAnnotationsPermissionsDispatcherTest.kt | 12 ----- ...thSystemAlertWindowKtAllAnnotationsTest.kt | 12 ----- ...AllAnnotationsPermissionsDispatcherTest.kt | 11 ---- ...ityWithWriteSettingKtAllAnnotationsTest.kt | 12 ----- 9 files changed, 6 insertions(+), 113 deletions(-) diff --git a/processor/src/test/java/permissions/dispatcher/processor/data/Source.java b/processor/src/test/java/permissions/dispatcher/processor/data/Source.java index 5d4e8320..11a66dde 100644 --- a/processor/src/test/java/permissions/dispatcher/processor/data/Source.java +++ b/processor/src/test/java/permissions/dispatcher/processor/data/Source.java @@ -4963,9 +4963,6 @@ protected String[] getActualSource() { " @OnPermissionDenied(Manifest.permission.WRITE_SETTINGS)", " void writeSettingOnPermissionDenied() {", " }", - " @OnNeverAskAgain(Manifest.permission.WRITE_SETTINGS)", - " void writeSettingOnNeverAskAgain() {", - " }", "}" }; } @@ -5006,11 +5003,7 @@ protected String[] getExpectSource() { " if (PermissionUtils.hasSelfPermissions(target.requireActivity(), PERMISSION_WRITESETTINGS) || Settings.System.canWrite(target.requireActivity())) {", " target.writeSettings();", " } else {", - " if (!PermissionUtils.shouldShowRequestPermissionRationale(target, PERMISSION_WRITESETTINGS)) {", - " target.writeSettingOnNeverAskAgain();", - " } else {", - " target.writeSettingOnPermissionDenied();", - " }", + " target.writeSettingOnPermissionDenied();", " }", " break;", " default:", @@ -5070,9 +5063,6 @@ protected String[] getActualSource() { " @OnPermissionDenied(Manifest.permission.SYSTEM_ALERT_WINDOW)", " void systemAlertWindowOnPermissionDenied() {", " }", - " @OnNeverAskAgain(Manifest.permission.SYSTEM_ALERT_WINDOW)", - " void systemAlertWindowOnNeverAskAgain() {", - " }", "}" }; } @@ -5113,11 +5103,7 @@ protected String[] getExpectSource() { " if (PermissionUtils.hasSelfPermissions(target.requireActivity(), PERMISSION_SYSTEMALERTWINDOW) || Settings.canDrawOverlays(target.requireActivity())) {", " target.systemAlertWindow();", " } else {", - " if (!PermissionUtils.shouldShowRequestPermissionRationale(target, PERMISSION_SYSTEMALERTWINDOW)) {", - " target.systemAlertWindowOnNeverAskAgain();", - " } else {", - " target.systemAlertWindowOnPermissionDenied();", - " }", + " target.systemAlertWindowOnPermissionDenied();", " }", " break;", " default:", @@ -5177,9 +5163,6 @@ protected String[] getActualSource() { " @OnPermissionDenied(Manifest.permission.WRITE_SETTINGS)", " void writeSettingsOnPermissionDenied() {", " }", - " @OnNeverAskAgain(Manifest.permission.WRITE_SETTINGS)", - " void writeSettingsOnNeverAskAgain() {", - " }", "}" }; } @@ -5220,11 +5203,7 @@ protected String[] getExpectSource() { " if (PermissionUtils.hasSelfPermissions(target, PERMISSION_WRITESETTINGS) || Settings.System.canWrite(target)) {", " target.writeSettings();", " } else {", - " if (!PermissionUtils.shouldShowRequestPermissionRationale(target, PERMISSION_WRITESETTINGS)) {", - " target.writeSettingsOnNeverAskAgain();", - " } else {", - " target.writeSettingsOnPermissionDenied();", - " }", + " target.writeSettingsOnPermissionDenied();", " }", " break;", " default:", @@ -5284,9 +5263,6 @@ protected String[] getActualSource() { " @OnPermissionDenied(Manifest.permission.SYSTEM_ALERT_WINDOW)", " void systemAlertWindowOnPermissionDenied() {", " }", - " @OnNeverAskAgain(Manifest.permission.SYSTEM_ALERT_WINDOW)", - " void systemAlertWindowOnNeverAskAgain() {", - " }", "}" }; } @@ -5327,11 +5303,7 @@ protected String[] getExpectSource() { " if (PermissionUtils.hasSelfPermissions(target, PERMISSION_SYSTEMALERTWINDOW) || Settings.canDrawOverlays(target)) {", " target.systemAlertWindow();", " } else {", - " if (!PermissionUtils.shouldShowRequestPermissionRationale(target, PERMISSION_SYSTEMALERTWINDOW)) {", - " target.systemAlertWindowOnNeverAskAgain();", - " } else {", - " target.systemAlertWindowOnPermissionDenied();", - " }", + " target.systemAlertWindowOnPermissionDenied();", " }", " break;", " default:", @@ -5393,9 +5365,6 @@ protected String[] getActualSource() { " @OnPermissionDenied(Manifest.permission.SYSTEM_ALERT_WINDOW)", " void systemAlertWindowOnPermissionDenied() {", " }", - " @OnNeverAskAgain(Manifest.permission.SYSTEM_ALERT_WINDOW)", - " void systemAlertWindowOnNeverAskAgain() {", - " }", "}" }; } @@ -5439,11 +5408,7 @@ protected String[] getExpectSource() { " if (PermissionUtils.hasSelfPermissions(target.requireActivity(), PERMISSION_SYSTEMALERTWINDOW) || Settings.canDrawOverlays(target.requireActivity())) {", " target.systemAlertWindow();", " } else {", - " if (!PermissionUtils.shouldShowRequestPermissionRationale(target, PERMISSION_SYSTEMALERTWINDOW)) {", - " target.systemAlertWindowOnNeverAskAgain();", - " } else {", - " target.systemAlertWindowOnPermissionDenied();", - " }", + " target.systemAlertWindowOnPermissionDenied();", " }", " break;", " default:", @@ -5505,9 +5470,6 @@ protected String[] getActualSource() { " @OnPermissionDenied(Manifest.permission.SYSTEM_ALERT_WINDOW)", " void systemAlertWindowOnPermissionDenied() {", " }", - " @OnNeverAskAgain(Manifest.permission.SYSTEM_ALERT_WINDOW)", - " void systemAlertWindowOnNeverAskAgain() {", - " }", "}" }; } @@ -5551,11 +5513,7 @@ protected String[] getExpectSource() { " if (PermissionUtils.hasSelfPermissions(target, PERMISSION_SYSTEMALERTWINDOW) || Settings.canDrawOverlays(target)) {", " target.systemAlertWindow();", " } else {", - " if (!PermissionUtils.shouldShowRequestPermissionRationale(target, PERMISSION_SYSTEMALERTWINDOW)) {", - " target.systemAlertWindowOnNeverAskAgain();", - " } else {", - " target.systemAlertWindowOnPermissionDenied();", - " }", + " target.systemAlertWindowOnPermissionDenied();", " }", " break;", " default:", diff --git a/test/src/main/java/permissions/dispatcher/test/ActivityWithSystemAlertWindowAllAnnotations.java b/test/src/main/java/permissions/dispatcher/test/ActivityWithSystemAlertWindowAllAnnotations.java index a6f7e55f..647de4e3 100644 --- a/test/src/main/java/permissions/dispatcher/test/ActivityWithSystemAlertWindowAllAnnotations.java +++ b/test/src/main/java/permissions/dispatcher/test/ActivityWithSystemAlertWindowAllAnnotations.java @@ -5,7 +5,6 @@ import androidx.appcompat.app.AppCompatActivity; import permissions.dispatcher.NeedsPermission; -import permissions.dispatcher.OnNeverAskAgain; import permissions.dispatcher.OnPermissionDenied; import permissions.dispatcher.OnShowRationale; import permissions.dispatcher.PermissionRequest; @@ -26,10 +25,6 @@ void showRationaleForSystemAlertWindow(final PermissionRequest request) { void showDeniedForSystemAlertWindow() { } - @OnNeverAskAgain(Manifest.permission.SYSTEM_ALERT_WINDOW) - void showNeverAskForSystemAlertWindow() { - } - @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); diff --git a/test/src/main/java/permissions/dispatcher/test/ActivityWithSystemAlertWindowKtAllAnnotations.kt b/test/src/main/java/permissions/dispatcher/test/ActivityWithSystemAlertWindowKtAllAnnotations.kt index 9a6b93f4..f18f748b 100644 --- a/test/src/main/java/permissions/dispatcher/test/ActivityWithSystemAlertWindowKtAllAnnotations.kt +++ b/test/src/main/java/permissions/dispatcher/test/ActivityWithSystemAlertWindowKtAllAnnotations.kt @@ -20,10 +20,6 @@ open class ActivityWithSystemAlertWindowKtAllAnnotations : AppCompatActivity() { internal fun showDeniedForSystemAlertWindow() { } - @OnNeverAskAgain(Manifest.permission.SYSTEM_ALERT_WINDOW) - internal fun showNeverAskForSystemAlertWindow() { - } - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) onActivityResult(requestCode) diff --git a/test/src/main/java/permissions/dispatcher/test/ActivityWithWriteSettingAllAnnotations.java b/test/src/main/java/permissions/dispatcher/test/ActivityWithWriteSettingAllAnnotations.java index 5ced1267..b198fb91 100644 --- a/test/src/main/java/permissions/dispatcher/test/ActivityWithWriteSettingAllAnnotations.java +++ b/test/src/main/java/permissions/dispatcher/test/ActivityWithWriteSettingAllAnnotations.java @@ -5,7 +5,6 @@ import androidx.appcompat.app.AppCompatActivity; import permissions.dispatcher.NeedsPermission; -import permissions.dispatcher.OnNeverAskAgain; import permissions.dispatcher.OnPermissionDenied; import permissions.dispatcher.OnShowRationale; import permissions.dispatcher.PermissionRequest; @@ -26,10 +25,6 @@ void showRationaleForWriteSettings(final PermissionRequest request) { void showDeniedForWriteSettings() { } - @OnNeverAskAgain(Manifest.permission.WRITE_SETTINGS) - void showNeverAskForWriteSettings() { - } - @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); diff --git a/test/src/main/java/permissions/dispatcher/test/ActivityWithWriteSettingKtAllAnnotations.kt b/test/src/main/java/permissions/dispatcher/test/ActivityWithWriteSettingKtAllAnnotations.kt index a0621942..df66faa2 100644 --- a/test/src/main/java/permissions/dispatcher/test/ActivityWithWriteSettingKtAllAnnotations.kt +++ b/test/src/main/java/permissions/dispatcher/test/ActivityWithWriteSettingKtAllAnnotations.kt @@ -20,10 +20,6 @@ open class ActivityWithWriteSettingKtAllAnnotations : AppCompatActivity() { internal fun showDeniedForWriteSettings() { } - @OnNeverAskAgain(Manifest.permission.WRITE_SETTINGS) - internal fun showNeverAskForWriteSettings() { - } - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) onActivityResult(requestCode) diff --git a/test/src/test/java/permissions/dispatcher/test/ActivityWithSystemAlertWindowAllAnnotationsPermissionsDispatcherTest.kt b/test/src/test/java/permissions/dispatcher/test/ActivityWithSystemAlertWindowAllAnnotationsPermissionsDispatcherTest.kt index c89a8cc6..a772742e 100644 --- a/test/src/test/java/permissions/dispatcher/test/ActivityWithSystemAlertWindowAllAnnotationsPermissionsDispatcherTest.kt +++ b/test/src/test/java/permissions/dispatcher/test/ActivityWithSystemAlertWindowAllAnnotationsPermissionsDispatcherTest.kt @@ -139,17 +139,6 @@ class ActivityWithSystemAlertWindowAllAnnotationsPermissionsDispatcherTest { Mockito.verify(activity, Mockito.times(0)).systemAlertWindow() } - @Test - fun `call showNeverAsk method if permission not granted and shouldShowRequestPermissionRationale false`() { - mockCheckSelfPermission(false) - mockCanDrawOverlays(false) - mockShouldShowRequestPermissionRationaleActivity(false) - - onActivityResult(activity, requestCode) - - Mockito.verify(activity, Mockito.times(1)).showNeverAskForSystemAlertWindow() - } - @Test fun `call showDenied method if permission not granted and shouldShowRequestPermissionRationale true`() { mockCheckSelfPermission(false) @@ -160,5 +149,4 @@ class ActivityWithSystemAlertWindowAllAnnotationsPermissionsDispatcherTest { Mockito.verify(activity, Mockito.times(1)).showDeniedForSystemAlertWindow() } - } \ No newline at end of file diff --git a/test/src/test/java/permissions/dispatcher/test/ActivityWithSystemAlertWindowKtAllAnnotationsTest.kt b/test/src/test/java/permissions/dispatcher/test/ActivityWithSystemAlertWindowKtAllAnnotationsTest.kt index 32f8539a..deab0723 100644 --- a/test/src/test/java/permissions/dispatcher/test/ActivityWithSystemAlertWindowKtAllAnnotationsTest.kt +++ b/test/src/test/java/permissions/dispatcher/test/ActivityWithSystemAlertWindowKtAllAnnotationsTest.kt @@ -138,17 +138,6 @@ class ActivityWithSystemAlertWindowKtAllAnnotationsTest { Mockito.verify(activity, Mockito.times(0)).systemAlertWindow() } - @Test - fun `call showNeverAsk method if permission not granted and shouldShowRequestPermissionRationale false`() { - mockCheckSelfPermission(false) - mockCanDrawOverlays(false) - mockShouldShowRequestPermissionRationaleActivity(false) - - activity.onActivityResult(requestCode) - - Mockito.verify(activity, Mockito.times(1)).showNeverAskForSystemAlertWindow() - } - @Test fun `call showDenied method if permission not granted and shouldShowRequestPermissionRationale true`() { mockCheckSelfPermission(false) @@ -159,5 +148,4 @@ class ActivityWithSystemAlertWindowKtAllAnnotationsTest { Mockito.verify(activity, Mockito.times(1)).showDeniedForSystemAlertWindow() } - } \ No newline at end of file diff --git a/test/src/test/java/permissions/dispatcher/test/ActivityWithWriteSettingAllAnnotationsPermissionsDispatcherTest.kt b/test/src/test/java/permissions/dispatcher/test/ActivityWithWriteSettingAllAnnotationsPermissionsDispatcherTest.kt index cf3b78a0..fe3e0cb2 100644 --- a/test/src/test/java/permissions/dispatcher/test/ActivityWithWriteSettingAllAnnotationsPermissionsDispatcherTest.kt +++ b/test/src/test/java/permissions/dispatcher/test/ActivityWithWriteSettingAllAnnotationsPermissionsDispatcherTest.kt @@ -124,17 +124,6 @@ class ActivityWithWriteSettingAllAnnotationsPermissionsDispatcherTest { Mockito.verify(activity, Mockito.times(0)).writeSetting() } - @Test - fun `call showNeverAsk method if permission not granted and shouldShowRequestPermissionRationale false`() { - mockCheckSelfPermission(false) - mockCanWrite(false) - mockShouldShowRequestPermissionRationaleActivity(false) - - onActivityResult(activity, requestCode) - - Mockito.verify(activity, Mockito.times(1)).showNeverAskForWriteSettings() - } - @Test fun `call showDenied method if permission not granted and shouldShowRequestPermissionRationale true`() { mockCheckSelfPermission(false) diff --git a/test/src/test/java/permissions/dispatcher/test/ActivityWithWriteSettingKtAllAnnotationsTest.kt b/test/src/test/java/permissions/dispatcher/test/ActivityWithWriteSettingKtAllAnnotationsTest.kt index 07698955..2a738a5a 100644 --- a/test/src/test/java/permissions/dispatcher/test/ActivityWithWriteSettingKtAllAnnotationsTest.kt +++ b/test/src/test/java/permissions/dispatcher/test/ActivityWithWriteSettingKtAllAnnotationsTest.kt @@ -138,17 +138,6 @@ class ActivityWithWriteSettingKtAllAnnotationsTest { Mockito.verify(activity, Mockito.times(0)).writeSetting() } - @Test - fun `call showNeverAsk method if permission not granted and shouldShowRequestPermissionRationale false`() { - mockCheckSelfPermission(false) - mockCanWrite(false) - mockShouldShowRequestPermissionRationaleActivity(false) - - activity.onActivityResult(requestCode) - - Mockito.verify(activity, Mockito.times(1)).showNeverAskForWriteSettings() - } - @Test fun `call showDenied method if permission not granted and shouldShowRequestPermissionRationale true`() { mockCheckSelfPermission(false) @@ -159,5 +148,4 @@ class ActivityWithWriteSettingKtAllAnnotationsTest { Mockito.verify(activity, Mockito.times(1)).showDeniedForWriteSettings() } - } \ No newline at end of file From 8a0fa29b7663fc97128b2ea6a71d38dc63d48af1 Mon Sep 17 00:00:00 2001 From: hotchemi Date: Mon, 3 Dec 2018 03:53:25 +0900 Subject: [PATCH 3/3] Add test cases for the change. --- ...alPermissionsWithNeverAskAgainException.kt | 2 +- .../processor/ProcessorTestSuite.java | 10 +++ .../dispatcher/processor/data/Source.java | 84 +++++++++++++++++++ 3 files changed, 95 insertions(+), 1 deletion(-) diff --git a/processor/src/main/kotlin/permissions/dispatcher/processor/exception/SpecialPermissionsWithNeverAskAgainException.kt b/processor/src/main/kotlin/permissions/dispatcher/processor/exception/SpecialPermissionsWithNeverAskAgainException.kt index a8141d17..fde6cecb 100644 --- a/processor/src/main/kotlin/permissions/dispatcher/processor/exception/SpecialPermissionsWithNeverAskAgainException.kt +++ b/processor/src/main/kotlin/permissions/dispatcher/processor/exception/SpecialPermissionsWithNeverAskAgainException.kt @@ -1,3 +1,3 @@ package permissions.dispatcher.processor.exception -class SpecialPermissionsWithNeverAskAgainException : RuntimeException("@NeverAskAgain annotated method never being called with WRITE_SETTINGS or SYSTEM_ALERT_WINDOW permission.") \ No newline at end of file +class SpecialPermissionsWithNeverAskAgainException : RuntimeException("'@NeverAskAgain' annotated method never being called with 'WRITE_SETTINGS' or 'SYSTEM_ALERT_WINDOW' permission.") \ No newline at end of file diff --git a/processor/src/test/java/permissions/dispatcher/processor/ProcessorTestSuite.java b/processor/src/test/java/permissions/dispatcher/processor/ProcessorTestSuite.java index 26ca7c3f..1a7ff46e 100644 --- a/processor/src/test/java/permissions/dispatcher/processor/ProcessorTestSuite.java +++ b/processor/src/test/java/permissions/dispatcher/processor/ProcessorTestSuite.java @@ -131,6 +131,16 @@ public class ProcessorTestSuite extends TestSuite { assertJavaSource(Source.needsPermissionMethodOverloadFragment); } + @Test public void systemAlertWindowWithOnNeverAskAgain() { + expectRuntimeException("'@NeverAskAgain' annotated method never being called with 'WRITE_SETTINGS' or 'SYSTEM_ALERT_WINDOW' permission."); + assertJavaSource(Source.systemAlertWindowWithOnNeverAskAgain); + } + + @Test public void writeSettingsWithOnNeverAskAgain() { + expectRuntimeException("'@NeverAskAgain' annotated method never being called with 'WRITE_SETTINGS' or 'SYSTEM_ALERT_WINDOW' permission."); + assertJavaSource(Source.writeSettingsWithOnNeverAskAgain); + } + @Test public void methodOverloadWithoutNeedsPermission() { assertJavaSource(Source.methodOverloadWithoutNeedsPermission); } diff --git a/processor/src/test/java/permissions/dispatcher/processor/data/Source.java b/processor/src/test/java/permissions/dispatcher/processor/data/Source.java index 11a66dde..66ef4ba8 100644 --- a/processor/src/test/java/permissions/dispatcher/processor/data/Source.java +++ b/processor/src/test/java/permissions/dispatcher/processor/data/Source.java @@ -6001,6 +6001,90 @@ protected String[] getExpectSource() { } }; + public static final BaseTest systemAlertWindowWithOnNeverAskAgain = new BaseTest() { + @Override + protected String getName() { + return "MyFragment"; + } + + @Override + protected String[] getActualSource() { + return new String[]{ + "package test;", + "import android.Manifest;", + "import androidx.fragment.app.Fragment;", + "import permissions.dispatcher.NeedsPermission;", + "import permissions.dispatcher.OnNeverAskAgain;", + "import permissions.dispatcher.OnPermissionDenied;", + "import permissions.dispatcher.OnShowRationale;", + "import permissions.dispatcher.PermissionRequest;", + "import permissions.dispatcher.RuntimePermissions;", + "@RuntimePermissions", + "public class MyFragment extends Fragment {", + " @NeedsPermission(Manifest.permission.SYSTEM_ALERT_WINDOW)", + " void systemAlertWindow() {", + " }", + " @OnShowRationale(Manifest.permission.SYSTEM_ALERT_WINDOW)", + " void systemAlertWindowOnShowRationale(PermissionRequest request) {", + " }", + " @OnNeverAskAgain(Manifest.permission.SYSTEM_ALERT_WINDOW)", + " void systemAlertWindowOnNeverAskAgain() {", + " }", + " @OnPermissionDenied(Manifest.permission.SYSTEM_ALERT_WINDOW)", + " void systemAlertWindowOnPermissionDenied() {", + " }", + "}" + }; + } + + @Override + protected String[] getExpectSource() { + return EMPTY_SOURCE; + } + }; + + public static final BaseTest writeSettingsWithOnNeverAskAgain = new BaseTest() { + @Override + protected String getName() { + return "MyFragment"; + } + + @Override + protected String[] getActualSource() { + return new String[]{ + "package test;", + "import android.Manifest;", + "import androidx.fragment.app.Fragment;", + "import permissions.dispatcher.NeedsPermission;", + "import permissions.dispatcher.OnNeverAskAgain;", + "import permissions.dispatcher.OnPermissionDenied;", + "import permissions.dispatcher.OnShowRationale;", + "import permissions.dispatcher.PermissionRequest;", + "import permissions.dispatcher.RuntimePermissions;", + "@RuntimePermissions", + "public class MyFragment extends Fragment {", + " @NeedsPermission(Manifest.permission.WRITE_SETTINGS)", + " void writeSettings() {", + " }", + " @OnShowRationale(Manifest.permission.WRITE_SETTINGS)", + " void writeSettingOnShowRationale(PermissionRequest request) {", + " }", + " @OnNeverAskAgain(Manifest.permission.WRITE_SETTINGS)", + " void writeSettingOnNeverAskAgain() {", + " }", + " @OnPermissionDenied(Manifest.permission.WRITE_SETTINGS)", + " void writeSettingOnPermissionDenied() {", + " }", + "}" + }; + } + + @Override + protected String[] getExpectSource() { + return EMPTY_SOURCE; + } + }; + public static final BaseTest methodOverloadWithoutNeedsPermission = new BaseTest() { @Override protected String getName() {