Testing Permission Handling in Flutter: A Comprehensive Guide

Testing Permission Handling in Flutter: A Comprehensive Guide

A Comprehensive Guide to testing in Flutter using custom mocks

When developing Flutter applications that require device permissions, it's crucial to thoroughly test your permission-handling logic.

This article will guide you through the process of testing a permission service class, using a PermissionHandlerAppPermissionsService as an example.

Understanding the Permission Service

First, let's look at a simplified version of our PermissionHandlerAppPermissionsService:

import 'package:permission_handler/permission_handler.dart';
import '../domain/app_permissions_service.dart';
import '../domain/model/permission_type.dart';
import '../domain/model/permission_status.dart' as domain;

class PermissionHandlerAppPermissionsService extends AppPermissionsService {
  @override
  Future<Result<AppException<PermissionErrorCode>, domain.PermissionStatus>>
      requestPermission(PermissionType type) async {
    try {
      final permission = type.toPermission();
      final status = await permission.request();
      return Success(status.toDomain());
    } catch (e) {
      return Failure(AppException(PermissionErrorCode.unknown));
    }
  }
}

This service uses the permission_handler package to request permissions and handles the results.

Setting Up the Test Environment

To test this service effectively, we'll use a custom MethodChannelMock class.

This class allows us to simulate method channel calls without relying on the actual platform implementation. Here's the implementation:

import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

class MethodChannelMock {
  MethodChannelMock({
    required String channelName,
    required this.method,
    this.result,
    this.delay = Duration.zero,
  }) : methodChannel = MethodChannel(channelName) {
    TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
        .setMockMethodCallHandler(methodChannel, _handler);
  }
  final MethodChannel methodChannel;
  final String method;
  final dynamic result;
  final Duration delay;

  Future _handler(MethodCall methodCall) async {
    if (methodCall.method != method) {
      throw MissingPluginException('No implementation found for method '
          '$method on channel ${methodChannel.name}');
    }

    return Future.delayed(delay, () {
      if (result is Exception) {
        throw result;
      }

      return Future.value(result);
    });
  }
}

This custom mock class allows us to easily set up method channel mocks with specific results and delays.

This custom mock class is designed to simulate method channel calls in Flutter tests. Here's a breakdown of its key components:

  1. Constructor:

    1. Takes channelName, method, result, and an optional delay.

    2. Creates a MethodChannel with the given channelName.

    3. Sets up a mock method call handler using TestDefaultBinaryMessengerBinding.

  2. Properties:

    1. methodChannel: The MethodChannel being mocked.

    2. method: The specific method name to mock.

    3. result: The result to return when the method is called.

    4. delay: An optional delay before returning the result.

  3. _handler method:

    1. This is the core of the mock. It's called when the mocked method is invoked.

    2. It checks if the called method matches the expected method name.

    3. If there's a mismatch, it throws a MissingPluginException.

    4. If the method matches, it returns the result after the specified delay.

    5. If the result is an Exception, it throws the exception instead.

The class provides a flexible way to mock method channel calls by:

  • Allowing specification of the exact method to mock.

  • Providing custom results or exceptions.

  • Simulating delays in responses.

This approach allows for precise control over the behavior of platform channel interactions in tests, enabling thorough testing of various scenarios without relying on actual platform implementations.

Writing the Tests

Now, let's write some tests for our PermissionHandlerAppPermissionsService using the MethodChannelMock:

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/services.dart';
import 'package:your_app/modules/permissions/data/permission_handler_app_permissions_service.dart';
import 'package:your_app/modules/permissions/domain/model/permission_type.dart';
import 'package:your_app/modules/permissions/domain/model/permission_status.dart' as domain;
import '../../../common/method_channel_mock.dart';

void main() {
  late PermissionHandlerAppPermissionsService service;
  late MethodChannelMock methodChannelMock;

  setUp(() {
    service = PermissionHandlerAppPermissionsService();
    methodChannelMock = MethodChannelMock(
      channelName: 'flutter.baseflow.com/permissions/methods',
      method: 'requestPermissions',
      result: {0: 1}, // This represents PermissionStatus.granted
    );
  });

  test('requestPermission returns Success with granted status', () async {
    final result = await service.requestPermission(PermissionType.camera);

    expect(result, isA<Success>());
    expect((result as Success).value, equals(domain.PermissionStatus.granted));
  });

  test('requestPermission returns Success with denied status', () async {
    methodChannelMock = MethodChannelMock(
      channelName: 'flutter.baseflow.com/permissions/methods',
      method: 'requestPermissions',
      result: {0: 0}, // This represents PermissionStatus.denied
    );

    final result = await service.requestPermission(PermissionType.camera);

    expect(result, isA<Success>());
    expect((result as Success).value, equals(domain.PermissionStatus.denied));
  });

  test('requestPermission returns Failure on exception', () async {
    methodChannelMock = MethodChannelMock(
      channelName: 'flutter.baseflow.com/permissions/methods',
      method: 'requestPermissions',
      result: PlatformException(code: 'TEST_ERROR'),
    );

    final result = await service.requestPermission(PermissionType.camera);

    expect(result, isA<Failure>());
    expect((result as Failure).error.code, equals(PermissionErrorCode.unknown));
  });
}

In these tests, we're using the MethodChannelMock to simulate different responses from the platform channel. Here's what's happening:

  1. We create a new MethodChannelMock in the setUp function with a default "granted" response.

  2. In each test, we can create a new MethodChannelMock with different results to test various scenarios.

  3. We test for granted permissions, denied permissions, and exceptions.

Advantages of Using MethodChannelMock

The MethodChannelMock class provides several benefits:

  • Simplicity: It's easy to set up and use in tests, requiring minimal boilerplate code.

  • Flexibility: You can easily change the result for different test scenarios.

  • Realism: It closely mimics the actual method channel behavior, including the ability to simulate delays and exceptions.

Testing Different Permission Statuses

To thoroughly test your permission service, create tests for all possible permission statuses.

The MethodChannelMock makes it easy to simulate these different outcomes:

test('requestPermission returns Success with restricted status', () async {
  methodChannelMock = MethodChannelMock(
    channelName: 'flutter.baseflow.com/permissions/methods',
    method: 'requestPermissions',
    result: {0: 1}, // This represents PermissionStatus.restricted
  );

  final result = await service.requestPermission(PermissionType.camera);

  expect(result, isA<Success>());
  expect((result as Success).value, equals(domain.PermissionStatus.granted));
});

Conclusion

Testing permission-handling classes in Flutter becomes more straightforward and reliable with a custom MethodChannelMock class. This approach allows you to:

  • Simulate various permission statuses and error conditions.

  • Test your code's behavior without relying on actual device permissions.

  • Ensure your permission handling logic works correctly across different scenarios.

By following this testing strategy, you can create comprehensive tests that cover various permission-related scenarios, leading to a more robust and reliable application.

Remember to test for all possible permission statuses, error conditions, and edge cases. This thorough testing will help you catch and fix issues early in the development process, ensuring your app handles permissions correctly in all situations.

Did you find this article valuable?

Support Temitope Ajiboye by becoming a sponsor. Any amount is appreciated!