Commit be4ae86c by Maurits van Beusekom Committed by GitHub

Update the MethodChannelMock due to breaking changes. (#674)

* Update the MethodChannelMock due to breaking changes.

* Migrate to flutter_lints.
parent 8d0d47bd
## 3.6.2
* Updated the MethodChannelMock due to breaking changes in the platform channel test interface.
## 3.6.1 ## 3.6.1
* Updated `meta` dependency to version `^1.3.0`. * Updated `meta` dependency to version `^1.3.0`.
......
include: package:flutter_lints/flutter.yaml
analyzer:
exclude:
# Ignore generated files
- '**/*.g.dart'
- 'lib/src/generated/*.dart'
linter:
rules:
- public_member_api_docs
\ No newline at end of file
...@@ -11,11 +11,12 @@ const MethodChannel _methodChannel = ...@@ -11,11 +11,12 @@ const MethodChannel _methodChannel =
/// An implementation of [PermissionHandlerPlatform] that uses [MethodChannel]s. /// An implementation of [PermissionHandlerPlatform] that uses [MethodChannel]s.
class MethodChannelPermissionHandler extends PermissionHandlerPlatform { class MethodChannelPermissionHandler extends PermissionHandlerPlatform {
/// Checks the current status of the given [Permission]. /// Checks the current status of the given [Permission].
@override
Future<PermissionStatus> checkPermissionStatus(Permission permission) async { Future<PermissionStatus> checkPermissionStatus(Permission permission) async {
final status = await _methodChannel.invokeMethod( final status = await _methodChannel.invokeMethod(
'checkPermissionStatus', permission.value); 'checkPermissionStatus', permission.value);
return Codec.decodePermissionStatus(status); return decodePermissionStatus(status);
} }
/// Checks the current status of the service associated with the given /// Checks the current status of the service associated with the given
...@@ -39,17 +40,19 @@ class MethodChannelPermissionHandler extends PermissionHandlerPlatform { ...@@ -39,17 +40,19 @@ class MethodChannelPermissionHandler extends PermissionHandlerPlatform {
/// - **PLEASE NOTE that this is still not a perfect indication** of the /// - **PLEASE NOTE that this is still not a perfect indication** of the
/// device's capability to place & connect phone calls as it also depends /// device's capability to place & connect phone calls as it also depends
/// on the network condition. /// on the network condition.
@override
Future<ServiceStatus> checkServiceStatus(Permission permission) async { Future<ServiceStatus> checkServiceStatus(Permission permission) async {
final status = await _methodChannel.invokeMethod( final status = await _methodChannel.invokeMethod(
'checkServiceStatus', permission.value); 'checkServiceStatus', permission.value);
return Codec.decodeServiceStatus(status); return decodeServiceStatus(status);
} }
/// Opens the app settings page. /// Opens the app settings page.
/// ///
/// Returns [true] if the app settings page could be opened, otherwise /// Returns [true] if the app settings page could be opened, otherwise
/// [false]. /// [false].
@override
Future<bool> openAppSettings() async { Future<bool> openAppSettings() async {
final wasOpened = await _methodChannel.invokeMethod('openAppSettings'); final wasOpened = await _methodChannel.invokeMethod('openAppSettings');
...@@ -60,19 +63,21 @@ class MethodChannelPermissionHandler extends PermissionHandlerPlatform { ...@@ -60,19 +63,21 @@ class MethodChannelPermissionHandler extends PermissionHandlerPlatform {
/// they have not already been granted before. /// they have not already been granted before.
/// ///
/// Returns a [Map] containing the status per requested [Permission]. /// Returns a [Map] containing the status per requested [Permission].
@override
Future<Map<Permission, PermissionStatus>> requestPermissions( Future<Map<Permission, PermissionStatus>> requestPermissions(
List<Permission> permissions) async { List<Permission> permissions) async {
final data = Codec.encodePermissions(permissions); final data = encodePermissions(permissions);
final status = final status =
await _methodChannel.invokeMethod('requestPermissions', data); await _methodChannel.invokeMethod('requestPermissions', data);
return Codec.decodePermissionRequestResult(Map<int, int>.from(status)); return decodePermissionRequestResult(Map<int, int>.from(status));
} }
/// Checks if you should show a rationale for requesting permission. /// Checks if you should show a rationale for requesting permission.
/// ///
/// This method is only implemented on Android, calling this on iOS always /// This method is only implemented on Android, calling this on iOS always
/// returns [false]. /// returns [false].
@override
Future<bool> shouldShowRequestPermissionRationale( Future<bool> shouldShowRequestPermissionRationale(
Permission permission) async { Permission permission) async {
final shouldShowRationale = await _methodChannel.invokeMethod( final shouldShowRationale = await _methodChannel.invokeMethod(
......
import '../../../permission_handler_platform_interface.dart'; import '../../../permission_handler_platform_interface.dart';
/// Provides utility methods for encoding messages that are sent on the Flutter /// Converts the given [value] into a [PermissionStatus] instance.
/// message channel. PermissionStatus decodePermissionStatus(int value) {
class Codec {
/// Converts the given [value] into a [PermissionStatus] instance.
static PermissionStatus decodePermissionStatus(int value) {
return PermissionStatusValue.statusByValue(value); return PermissionStatusValue.statusByValue(value);
} }
/// Converts the given [value] into a [ServiceStatus] instance. /// Converts the given [value] into a [ServiceStatus] instance.
static ServiceStatus decodeServiceStatus(int value) { ServiceStatus decodeServiceStatus(int value) {
return ServiceStatusValue.statusByValue(value); return ServiceStatusValue.statusByValue(value);
} }
/// Converts the given [Map] of [int]s into a [Map] with [Permission]s as /// Converts the given [Map] of [int]s into a [Map] with [Permission]s as
/// keys and their respective [PermissionStatus] as value. /// keys and their respective [PermissionStatus] as value.
static Map<Permission, PermissionStatus> decodePermissionRequestResult( Map<Permission, PermissionStatus> decodePermissionRequestResult(
Map<int, int> value) { Map<int, int> value) {
return value.map((key, value) => MapEntry<Permission, PermissionStatus>( return value.map((key, value) => MapEntry<Permission, PermissionStatus>(
Permission.byValue(key), PermissionStatusValue.statusByValue(value))); Permission.byValue(key), PermissionStatusValue.statusByValue(value)));
} }
/// Converts the given [List] of [Permission]s into a [List] of [int]s which /// Converts the given [List] of [Permission]s into a [List] of [int]s which
/// can be sent on the Flutter method channel. /// can be sent on the Flutter method channel.
static List<int> encodePermissions(List<Permission> permissions) { List<int> encodePermissions(List<Permission> permissions) {
return permissions.map((it) => it.value).toList(); return permissions.map((it) => it.value).toList();
}
} }
...@@ -25,7 +25,9 @@ enum PermissionStatus { ...@@ -25,7 +25,9 @@ enum PermissionStatus {
permanentlyDenied, permanentlyDenied,
} }
/// Conversion extension methods for the [PermissionStatus] type.
extension PermissionStatusValue on PermissionStatus { extension PermissionStatusValue on PermissionStatus {
/// Converts the [PermissionStatus] value into an integer.
int get value { int get value {
switch (this) { switch (this) {
case PermissionStatus.denied: case PermissionStatus.denied:
...@@ -43,6 +45,7 @@ extension PermissionStatusValue on PermissionStatus { ...@@ -43,6 +45,7 @@ extension PermissionStatusValue on PermissionStatus {
} }
} }
/// Converts the supplied integer value into a [PermissionStatus] enum.
static PermissionStatus statusByValue(int value) { static PermissionStatus statusByValue(int value) {
return [ return [
PermissionStatus.denied, PermissionStatus.denied,
...@@ -54,6 +57,7 @@ extension PermissionStatusValue on PermissionStatus { ...@@ -54,6 +57,7 @@ extension PermissionStatusValue on PermissionStatus {
} }
} }
/// Utility getter extensions for the [PermissionStatus] type.
extension PermissionStatusGetters on PermissionStatus { extension PermissionStatusGetters on PermissionStatus {
/// If the user denied access to the requested feature. /// If the user denied access to the requested feature.
bool get isDenied => this == PermissionStatus.denied; bool get isDenied => this == PermissionStatus.denied;
...@@ -76,9 +80,11 @@ extension PermissionStatusGetters on PermissionStatus { ...@@ -76,9 +80,11 @@ extension PermissionStatusGetters on PermissionStatus {
/// Therefore make a `request` call first. /// Therefore make a `request` call first.
bool get isPermanentlyDenied => this == PermissionStatus.permanentlyDenied; bool get isPermanentlyDenied => this == PermissionStatus.permanentlyDenied;
/// Indicates that permission for limited use of the resource is granted.
bool get isLimited => this == PermissionStatus.limited; bool get isLimited => this == PermissionStatus.limited;
} }
/// Utility getter extensions for the `Future<PermissionStatus>` type.
extension FuturePermissionStatusGetters on Future<PermissionStatus> { extension FuturePermissionStatusGetters on Future<PermissionStatus> {
/// If the user granted access to the requested feature. /// If the user granted access to the requested feature.
Future<bool> get isGranted async => (await this).isGranted; Future<bool> get isGranted async => (await this).isGranted;
...@@ -99,5 +105,6 @@ extension FuturePermissionStatusGetters on Future<PermissionStatus> { ...@@ -99,5 +105,6 @@ extension FuturePermissionStatusGetters on Future<PermissionStatus> {
Future<bool> get isPermanentlyDenied async => Future<bool> get isPermanentlyDenied async =>
(await this).isPermanentlyDenied; (await this).isPermanentlyDenied;
/// Indicates that permission for limited use of the resource is granted.
Future<bool> get isLimited async => (await this).isLimited; Future<bool> get isLimited async => (await this).isLimited;
} }
...@@ -6,6 +6,9 @@ part of permission_handler_platform_interface; ...@@ -6,6 +6,9 @@ part of permission_handler_platform_interface;
class PermissionWithService extends Permission { class PermissionWithService extends Permission {
const PermissionWithService._(int value) : super._(value); const PermissionWithService._(int value) : super._(value);
/// Creates a [PermissionWithService] instance.
///
/// This constructor is marked public for testing purposes only.
@visibleForTesting @visibleForTesting
const PermissionWithService.private(int value) : super._(value); const PermissionWithService.private(int value) : super._(value);
} }
...@@ -14,6 +17,8 @@ class PermissionWithService extends Permission { ...@@ -14,6 +17,8 @@ class PermissionWithService extends Permission {
@immutable @immutable
class Permission { class Permission {
const Permission._(this.value); const Permission._(this.value);
/// Creates a [Permission] using the supplied integer value.
factory Permission.byValue(int value) => values[value]; factory Permission.byValue(int value) => values[value];
/// Integer representation of the [Permission]. /// Integer representation of the [Permission].
......
part of permission_handler_platform_interface; part of permission_handler_platform_interface;
/// Defines the different states a service can be in.
enum ServiceStatus { enum ServiceStatus {
/// The service for the permission is disabled. /// The service for the permission is disabled.
disabled, disabled,
...@@ -12,7 +13,9 @@ enum ServiceStatus { ...@@ -12,7 +13,9 @@ enum ServiceStatus {
notApplicable, notApplicable,
} }
/// Conversion extension methods for the [ServiceStatus] type.
extension ServiceStatusValue on ServiceStatus { extension ServiceStatusValue on ServiceStatus {
/// Converts the [ServiceStatus] value into an integer.
int get value { int get value {
switch (this) { switch (this) {
case ServiceStatus.disabled: case ServiceStatus.disabled:
...@@ -26,6 +29,7 @@ extension ServiceStatusValue on ServiceStatus { ...@@ -26,6 +29,7 @@ extension ServiceStatusValue on ServiceStatus {
} }
} }
/// Converts the supplied integer value into a [ServiceStatus] enum.
static ServiceStatus statusByValue(int value) { static ServiceStatus statusByValue(int value) {
return [ return [
ServiceStatus.disabled, ServiceStatus.disabled,
...@@ -35,6 +39,7 @@ extension ServiceStatusValue on ServiceStatus { ...@@ -35,6 +39,7 @@ extension ServiceStatusValue on ServiceStatus {
} }
} }
/// Utility getter extensions for the [ServiceStatus] type.
extension ServiceStatusGetters on ServiceStatus { extension ServiceStatusGetters on ServiceStatus {
/// If the service for the permission is disabled. /// If the service for the permission is disabled.
bool get isDisabled => this == ServiceStatus.disabled; bool get isDisabled => this == ServiceStatus.disabled;
...@@ -47,6 +52,7 @@ extension ServiceStatusGetters on ServiceStatus { ...@@ -47,6 +52,7 @@ extension ServiceStatusGetters on ServiceStatus {
bool get isNotApplicable => this == ServiceStatus.notApplicable; bool get isNotApplicable => this == ServiceStatus.notApplicable;
} }
/// Utility getter extensions for the `Future<ServiceStatus>` type.
extension FutureServiceStatusGetters on Future<ServiceStatus> { extension FutureServiceStatusGetters on Future<ServiceStatus> {
/// If the service for the permission is disabled. /// If the service for the permission is disabled.
Future<bool> get isDisabled async => (await this).isDisabled; Future<bool> get isDisabled async => (await this).isDisabled;
......
...@@ -3,7 +3,7 @@ description: A common platform interface for the permission_handler plugin. ...@@ -3,7 +3,7 @@ description: A common platform interface for the permission_handler plugin.
homepage: https://github.com/baseflow/flutter-permission-handler/tree/master/permission_handler_platform_interface homepage: https://github.com/baseflow/flutter-permission-handler/tree/master/permission_handler_platform_interface
# NOTE: We strongly prefer non-breaking changes, even at the expense of a # NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes # less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 3.6.1 version: 3.6.2
dependencies: dependencies:
flutter: flutter:
...@@ -15,8 +15,8 @@ dev_dependencies: ...@@ -15,8 +15,8 @@ dev_dependencies:
flutter_test: flutter_test:
sdk: flutter sdk: flutter
mockito: ^5.0.0 mockito: ^5.0.0
effective_dart: ^1.3.0 flutter_lints: ^1.0.4
environment: environment:
sdk: ">=2.12.0 <3.0.0" sdk: ">=2.14.0 <3.0.0"
flutter: ">=1.22.0" flutter: ">=2.5.0"
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
class MethodChannelMock { class MethodChannelMock {
final MethodChannel methodChannel; final MethodChannel methodChannel;
...@@ -12,7 +13,8 @@ class MethodChannelMock { ...@@ -12,7 +13,8 @@ class MethodChannelMock {
this.result, this.result,
this.delay = Duration.zero, this.delay = Duration.zero,
}) : methodChannel = MethodChannel(channelName) { }) : methodChannel = MethodChannel(channelName) {
methodChannel.setMockMethodCallHandler(_handler); TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger
.setMockMethodCallHandler(methodChannel, _handler);
} }
Future _handler(MethodCall methodCall) async { Future _handler(MethodCall methodCall) async {
......
...@@ -5,11 +5,11 @@ import 'package:permission_handler_platform_interface/src/method_channel/utils/c ...@@ -5,11 +5,11 @@ import 'package:permission_handler_platform_interface/src/method_channel/utils/c
void main() { void main() {
group('Codec', () { group('Codec', () {
test('decodePermissionStatus should return a PermissionStatus', () { test('decodePermissionStatus should return a PermissionStatus', () {
expect(Codec.decodePermissionStatus(0), PermissionStatus.denied); expect(decodePermissionStatus(0), PermissionStatus.denied);
}); });
test('decodeServiceStatus should a corresponding ServiceStatus', () { test('decodeServiceStatus should a corresponding ServiceStatus', () {
expect(Codec.decodeServiceStatus(0), ServiceStatus.disabled); expect(decodeServiceStatus(0), ServiceStatus.disabled);
}); });
test( test(
...@@ -19,7 +19,7 @@ void main() { ...@@ -19,7 +19,7 @@ void main() {
1: 1, 1: 1,
}; };
var permissionMap = Codec.decodePermissionRequestResult(value); var permissionMap = decodePermissionRequestResult(value);
expect(permissionMap.keys.first, isA<Permission>()); expect(permissionMap.keys.first, isA<Permission>());
expect(permissionMap.values.first, isA<PermissionStatus>()); expect(permissionMap.values.first, isA<PermissionStatus>());
...@@ -28,7 +28,7 @@ void main() { ...@@ -28,7 +28,7 @@ void main() {
test('encodePermissions should return a list of integers', () { test('encodePermissions should return a list of integers', () {
var permissions = [Permission.accessMediaLocation]; var permissions = [Permission.accessMediaLocation];
var integers = Codec.encodePermissions(permissions); var integers = encodePermissions(permissions);
expect(integers.first, isA<int>()); expect(integers.first, isA<int>());
}); });
......
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:permission_handler_platform_interface/permission_handler_platform_interface.dart'; import 'package:permission_handler_platform_interface/permission_handler_platform_interface.dart';
import 'package:permission_handler_platform_interface/src/method_channel/method_channel_permission_handler.dart'; import 'package:permission_handler_platform_interface/src/method_channel/method_channel_permission_handler.dart';
import 'package:mockito/mockito.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart'; import 'package:plugin_platform_interface/plugin_platform_interface.dart';
void main() { void main() {
......
...@@ -4,13 +4,13 @@ import 'package:permission_handler_platform_interface/permission_handler_platfor ...@@ -4,13 +4,13 @@ import 'package:permission_handler_platform_interface/permission_handler_platfor
void main() { void main() {
group('PermissionSatus', () { group('PermissionSatus', () {
test('PermissionStatus should contain 5 options', () { test('PermissionStatus should contain 5 options', () {
final values = PermissionStatus.values; const values = PermissionStatus.values;
expect(values.length, 5); expect(values.length, 5);
}); });
test('PermissionStatus enum should have items in correct index', () { test('PermissionStatus enum should have items in correct index', () {
final values = PermissionStatus.values; const values = PermissionStatus.values;
expect(values[0], PermissionStatus.denied); expect(values[0], PermissionStatus.denied);
expect(values[1], PermissionStatus.granted); expect(values[1], PermissionStatus.granted);
......
...@@ -4,13 +4,13 @@ import 'package:permission_handler_platform_interface/permission_handler_platfor ...@@ -4,13 +4,13 @@ import 'package:permission_handler_platform_interface/permission_handler_platfor
void main() { void main() {
test('Permission has the right amount of possible PermissionGroup values', test('Permission has the right amount of possible PermissionGroup values',
() { () {
final values = Permission.values; const values = Permission.values;
expect(values.length, 28); expect(values.length, 28);
}); });
test('check if byValue returns corresponding PermissionGroup value', () { test('check if byValue returns corresponding PermissionGroup value', () {
final values = Permission.values; const values = Permission.values;
for (var i = 0; i < values.length; i++) { for (var i = 0; i < values.length; i++) {
expect(values[i], Permission.byValue(i)); expect(values[i], Permission.byValue(i));
...@@ -18,7 +18,8 @@ void main() { ...@@ -18,7 +18,8 @@ void main() {
}); });
test('check if toString method returns the corresponding name', () { test('check if toString method returns the corresponding name', () {
var permissionWithService = PermissionWithService.private(0); const PermissionWithService permissionWithService =
PermissionWithService.private(0);
var permissionName = permissionWithService.toString(); var permissionName = permissionWithService.toString();
expect(permissionName, 'Permission.calendar'); expect(permissionName, 'Permission.calendar');
}); });
......
...@@ -4,13 +4,13 @@ import 'package:permission_handler_platform_interface/permission_handler_platfor ...@@ -4,13 +4,13 @@ import 'package:permission_handler_platform_interface/permission_handler_platfor
void main() { void main() {
group('ServiceStatus', () { group('ServiceStatus', () {
test('ServiceStatus should contain 3 options', () { test('ServiceStatus should contain 3 options', () {
final values = ServiceStatus.values; const values = ServiceStatus.values;
expect(values.length, 3); expect(values.length, 3);
}); });
test('ServiceStatus enum should have items in correct index', () { test('ServiceStatus enum should have items in correct index', () {
final values = ServiceStatus.values; const values = ServiceStatus.values;
expect(values[0], ServiceStatus.disabled); expect(values[0], ServiceStatus.disabled);
expect(values[1], ServiceStatus.enabled); expect(values[1], ServiceStatus.enabled);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment