diff --git a/permission_handler_android/lib/src/missing_android_activity_exception.dart b/permission_handler_android/lib/src/missing_android_activity_exception.dart new file mode 100644 index 000000000..7f7697a9f --- /dev/null +++ b/permission_handler_android/lib/src/missing_android_activity_exception.dart @@ -0,0 +1,9 @@ +/// An exception thrown when the Android activity is missing. +class MissingAndroidActivityException implements Exception { + /// Creates a new instance of [MissingAndroidActivityException]. + const MissingAndroidActivityException(); + + @override + String toString() => + 'MissingAndroidActivityException: There is no attached activity'; +} diff --git a/permission_handler_android/lib/src/permission_handler_android.dart b/permission_handler_android/lib/src/permission_handler_android.dart index 921df9c4d..54a8d8369 100644 --- a/permission_handler_android/lib/src/permission_handler_android.dart +++ b/permission_handler_android/lib/src/permission_handler_android.dart @@ -4,6 +4,7 @@ import 'package:permission_handler_platform_interface/permission_handler_platfor import 'android_object_mirrors/activity.dart'; import 'android_object_mirrors/activity_compat.dart'; import 'android.dart'; +import 'missing_android_activity_exception.dart'; /// An implementation of [PermissionHandlerPlatform] for Android. class PermissionHandlerAndroid extends PermissionHandlerPlatform { @@ -55,7 +56,7 @@ class PermissionHandlerAndroid extends PermissionHandlerPlatform { @override Future shouldShowRequestPermissionRationale(Permission permission) { if (_activity == null) { - throw Exception('There is no attached activity'); + throw const MissingAndroidActivityException(); } return ActivityCompat.shouldShowRequestPermissionRationale( diff --git a/permission_handler_android/test/permission_handler_test.dart b/permission_handler_android/test/permission_handler_test.dart index e25c23d66..d16ab17e8 100644 --- a/permission_handler_android/test/permission_handler_test.dart +++ b/permission_handler_android/test/permission_handler_test.dart @@ -6,6 +6,7 @@ import 'package:flutter_instance_manager/test/test_instance_manager.pigeon.dart' import 'package:flutter_test/flutter_test.dart'; import 'package:permission_handler_android/permission_handler_android.dart'; import 'package:permission_handler_android/src/android_permission_handler_api_impls.dart'; +import 'package:permission_handler_android/src/missing_android_activity_exception.dart'; import 'package:permission_handler_platform_interface/permission_handler_platform_interface.dart'; void main() { @@ -84,7 +85,9 @@ void main() { ); }); - test('throws Exception if no activity is attached', () async { + test( + 'throws `MissingAndroidActivityException` if no activity is attached', + () async { // > Arrange final instanceManager = InstanceManager( onWeakReferenceRemoved: (_) {}, @@ -106,7 +109,10 @@ void main() { .shouldShowRequestPermissionRationale(Permission.contacts); // > Assert - expect(shouldShowRequestPermissionRationale(), throwsException); + expect( + shouldShowRequestPermissionRationale(), + throwsA(isA()), + ); }); test('returns properly', () async {