Skip to content

Commit

Permalink
Create MissingAndroidActivityException
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenWeener committed Oct 19, 2023
1 parent 75ac27c commit 8c7b31a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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';
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -55,7 +56,7 @@ class PermissionHandlerAndroid extends PermissionHandlerPlatform {
@override
Future<bool> shouldShowRequestPermissionRationale(Permission permission) {
if (_activity == null) {
throw Exception('There is no attached activity');
throw const MissingAndroidActivityException();
}

return ActivityCompat.shouldShowRequestPermissionRationale(
Expand Down
10 changes: 8 additions & 2 deletions permission_handler_android/test/permission_handler_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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: (_) {},
Expand All @@ -106,7 +109,10 @@ void main() {
.shouldShowRequestPermissionRationale(Permission.contacts);

// > Assert
expect(shouldShowRequestPermissionRationale(), throwsException);
expect(
shouldShowRequestPermissionRationale(),
throwsA(isA<MissingAndroidActivityException>()),
);
});

test('returns properly', () async {
Expand Down

0 comments on commit 8c7b31a

Please sign in to comment.