-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[local_auth] Migrate iOS to Pigeon #3974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
auto-submit
merged 15 commits into
flutter:main
from
stuartmorgan-g:local-auth-ios-pigeon
May 19, 2023
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
086e8ad
Remove useless check
stuartmorgan-g 093b0a2
Add todo for incorrect implementation to mark it as staying native
stuartmorgan-g 5b7888b
Initial Pigeon definition, modified from Android
stuartmorgan-g 10a3ab4
Adjust native implementation to conform to Pigeon API, adjusting Pige…
stuartmorgan-g e6a6a61
Update native tests, add missing test
stuartmorgan-g a97c41a
Update Dart implmeentation, modeled on Android
stuartmorgan-g ecd6e93
Replace Dart unit tests, based on Android
stuartmorgan-g 99c2963
Version bump
stuartmorgan-g 6bd50db
Autoformat
stuartmorgan-g 830a8df
Typo fix
stuartmorgan-g 167c018
Merge branch 'main' into local-auth-ios-pigeon
stuartmorgan-g 9794851
Typo fix
stuartmorgan-g 99f60e9
Rename result enum field
stuartmorgan-g 933c0d4
Other review fixes
stuartmorgan-g 3e707a8
Fix call sites
stuartmorgan-g File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Replace Dart unit tests, based on Android
- Loading branch information
commit ecd6e936b00779d835f9a01aca3ebcc3536d9a5a
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,4 +26,5 @@ dev_dependencies: | |
| build_runner: ^2.3.3 | ||
| flutter_test: | ||
| sdk: flutter | ||
| mockito: 5.4.0 | ||
| pigeon: ^9.2.4 | ||
330 changes: 330 additions & 0 deletions
330
packages/local_auth/local_auth_ios/test/local_auth_ios_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,330 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:flutter/services.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:local_auth_ios/local_auth_ios.dart'; | ||
| import 'package:local_auth_ios/src/messages.g.dart'; | ||
| import 'package:local_auth_platform_interface/local_auth_platform_interface.dart'; | ||
| import 'package:mockito/annotations.dart'; | ||
| import 'package:mockito/mockito.dart'; | ||
|
|
||
| import 'local_auth_ios_test.mocks.dart'; | ||
|
|
||
| @GenerateMocks(<Type>[LocalAuthApi]) | ||
| void main() { | ||
| late MockLocalAuthApi api; | ||
| late LocalAuthIOS plugin; | ||
|
|
||
| setUp(() { | ||
| api = MockLocalAuthApi(); | ||
| plugin = LocalAuthIOS(api: api); | ||
| }); | ||
|
|
||
| test('registers instance', () { | ||
| LocalAuthIOS.registerWith(); | ||
| expect(LocalAuthPlatform.instance, isA<LocalAuthIOS>()); | ||
| }); | ||
|
|
||
| group('deviceSupportsBiometrics', () { | ||
| test('handles true', () async { | ||
| when(api.deviceCanSupportBiometrics()).thenAnswer((_) async => true); | ||
| expect(await plugin.deviceSupportsBiometrics(), true); | ||
| }); | ||
|
|
||
| test('handles false', () async { | ||
| when(api.deviceCanSupportBiometrics()).thenAnswer((_) async => false); | ||
| expect(await plugin.deviceSupportsBiometrics(), false); | ||
| }); | ||
| }); | ||
|
|
||
| group('isDeviceSupported', () { | ||
| test('handles true', () async { | ||
| when(api.isDeviceSupported()).thenAnswer((_) async => true); | ||
| expect(await plugin.isDeviceSupported(), true); | ||
| }); | ||
|
|
||
| test('handles false', () async { | ||
| when(api.isDeviceSupported()).thenAnswer((_) async => false); | ||
| expect(await plugin.isDeviceSupported(), false); | ||
| }); | ||
| }); | ||
|
|
||
| group('stopAuthentication', () { | ||
| test('always returns false', () async { | ||
| expect(await plugin.stopAuthentication(), false); | ||
| }); | ||
| }); | ||
|
|
||
| group('getEnrolledBiometrics', () { | ||
| test('translates values', () async { | ||
| when(api.getEnrolledBiometrics()) | ||
| .thenAnswer((_) async => <AuthBiometricWrapper>[ | ||
| AuthBiometricWrapper(value: AuthBiometric.face), | ||
| AuthBiometricWrapper(value: AuthBiometric.fingerprint), | ||
| ]); | ||
|
|
||
| final List<BiometricType> result = await plugin.getEnrolledBiometrics(); | ||
|
|
||
| expect(result, <BiometricType>[ | ||
| BiometricType.face, | ||
| BiometricType.fingerprint, | ||
| ]); | ||
| }); | ||
|
|
||
| test('handles emtpy', () async { | ||
| when(api.getEnrolledBiometrics()) | ||
| .thenAnswer((_) async => <AuthBiometricWrapper>[]); | ||
|
|
||
| final List<BiometricType> result = await plugin.getEnrolledBiometrics(); | ||
|
|
||
| expect(result, <BiometricType>[]); | ||
| }); | ||
| }); | ||
|
|
||
| group('authenticate', () { | ||
| group('strings', () { | ||
| test('passes default values when nothing is provided', () async { | ||
| when(api.authenticate(any, any)).thenAnswer( | ||
| (_) async => AuthResultDetails(value: AuthResult.success)); | ||
|
|
||
| const String reason = 'test reason'; | ||
| await plugin.authenticate( | ||
| localizedReason: reason, authMessages: <AuthMessages>[]); | ||
|
|
||
| final VerificationResult result = | ||
| verify(api.authenticate(any, captureAny)); | ||
| final AuthStrings strings = result.captured[0] as AuthStrings; | ||
| expect(strings.reason, reason); | ||
| // These should all be the default values from | ||
| // auth_messages_ios.dart | ||
| expect(strings.lockOut, iOSLockOut); | ||
| expect(strings.goToSettingsButton, goToSettings); | ||
| expect(strings.goToSettingsDescription, iOSGoToSettingsDescription); | ||
| expect(strings.cancelButton, iOSOkButton); | ||
| expect(strings.localizedFallbackTitle, null); | ||
| }); | ||
|
|
||
| test('passes default values when only other platform values are provided', | ||
| () async { | ||
| when(api.authenticate(any, any)).thenAnswer( | ||
| (_) async => AuthResultDetails(value: AuthResult.success)); | ||
|
|
||
| const String reason = 'test reason'; | ||
| await plugin.authenticate( | ||
| localizedReason: reason, | ||
| authMessages: <AuthMessages>[AnotherPlatformAuthMessages()]); | ||
|
|
||
| final VerificationResult result = | ||
| verify(api.authenticate(any, captureAny)); | ||
| final AuthStrings strings = result.captured[0] as AuthStrings; | ||
| expect(strings.reason, reason); | ||
| // These should all be the default values from | ||
| // auth_messages_ios.dart | ||
| expect(strings.lockOut, iOSLockOut); | ||
| expect(strings.goToSettingsButton, goToSettings); | ||
| expect(strings.goToSettingsDescription, iOSGoToSettingsDescription); | ||
| expect(strings.cancelButton, iOSOkButton); | ||
| expect(strings.localizedFallbackTitle, null); | ||
| }); | ||
|
|
||
| test('passes all non-default values correctly', () async { | ||
| when(api.authenticate(any, any)).thenAnswer( | ||
| (_) async => AuthResultDetails(value: AuthResult.success)); | ||
|
|
||
| // These are arbitrary values; all that matters is that: | ||
| // - they are different from the defaults, and | ||
| // - they are different from each other. | ||
| const String reason = 'A'; | ||
| const String lockOut = 'B'; | ||
| const String goToSettingsButton = 'C'; | ||
| const String gotToSettingsDescription = 'D'; | ||
| const String cancel = 'E'; | ||
| const String localizedFallbackTitle = 'F'; | ||
| await plugin | ||
| .authenticate(localizedReason: reason, authMessages: <AuthMessages>[ | ||
| const IOSAuthMessages( | ||
| lockOut: lockOut, | ||
| goToSettingsButton: goToSettingsButton, | ||
| goToSettingsDescription: gotToSettingsDescription, | ||
| cancelButton: cancel, | ||
| localizedFallbackTitle: localizedFallbackTitle, | ||
| ), | ||
| AnotherPlatformAuthMessages(), | ||
| ]); | ||
|
|
||
| final VerificationResult result = | ||
| verify(api.authenticate(any, captureAny)); | ||
| final AuthStrings strings = result.captured[0] as AuthStrings; | ||
| expect(strings.reason, reason); | ||
| expect(strings.lockOut, lockOut); | ||
| expect(strings.goToSettingsButton, goToSettingsButton); | ||
| expect(strings.goToSettingsDescription, gotToSettingsDescription); | ||
| expect(strings.cancelButton, cancel); | ||
| expect(strings.localizedFallbackTitle, localizedFallbackTitle); | ||
| }); | ||
|
|
||
| test('passes provided messages with default fallbacks', () async { | ||
| when(api.authenticate(any, any)).thenAnswer( | ||
| (_) async => AuthResultDetails(value: AuthResult.success)); | ||
|
|
||
| // These are arbitrary values; all that matters is that: | ||
| // - they are different from the defaults, and | ||
| // - they are different from each other. | ||
| const String reason = 'A'; | ||
| const String lockOut = 'B'; | ||
| const String localizedFallbackTitle = 'C'; | ||
| const String cancel = 'D'; | ||
| await plugin | ||
| .authenticate(localizedReason: reason, authMessages: <AuthMessages>[ | ||
| const IOSAuthMessages( | ||
| lockOut: lockOut, | ||
| localizedFallbackTitle: localizedFallbackTitle, | ||
| cancelButton: cancel, | ||
| ), | ||
| ]); | ||
|
|
||
| final VerificationResult result = | ||
| verify(api.authenticate(any, captureAny)); | ||
| final AuthStrings strings = result.captured[0] as AuthStrings; | ||
| expect(strings.reason, reason); | ||
| // These should all be the provided values. | ||
| expect(strings.lockOut, lockOut); | ||
| expect(strings.localizedFallbackTitle, localizedFallbackTitle); | ||
| expect(strings.cancelButton, cancel); | ||
| // These were not set, so should all be the default values from | ||
| // auth_messages_ios.dart | ||
| expect(strings.goToSettingsButton, goToSettings); | ||
| expect(strings.goToSettingsDescription, iOSGoToSettingsDescription); | ||
| }); | ||
| }); | ||
|
|
||
| group('options', () { | ||
| test('passes default values', () async { | ||
| when(api.authenticate(any, any)).thenAnswer( | ||
| (_) async => AuthResultDetails(value: AuthResult.success)); | ||
|
|
||
| await plugin.authenticate( | ||
| localizedReason: 'reason', authMessages: <AuthMessages>[]); | ||
|
|
||
| final VerificationResult result = | ||
| verify(api.authenticate(captureAny, any)); | ||
| final AuthOptions options = result.captured[0] as AuthOptions; | ||
| expect(options.biometricOnly, false); | ||
| expect(options.sticky, false); | ||
| expect(options.useErrorDialgs, true); | ||
| }); | ||
|
|
||
| test('passes provided non-default values', () async { | ||
| when(api.authenticate(any, any)).thenAnswer( | ||
| (_) async => AuthResultDetails(value: AuthResult.success)); | ||
|
|
||
| await plugin.authenticate( | ||
| localizedReason: 'reason', | ||
| authMessages: <AuthMessages>[], | ||
| options: const AuthenticationOptions( | ||
| biometricOnly: true, | ||
| stickyAuth: true, | ||
| useErrorDialogs: false, | ||
| )); | ||
|
|
||
| final VerificationResult result = | ||
| verify(api.authenticate(captureAny, any)); | ||
| final AuthOptions options = result.captured[0] as AuthOptions; | ||
| expect(options.biometricOnly, true); | ||
| expect(options.sticky, true); | ||
| expect(options.useErrorDialgs, false); | ||
| }); | ||
| }); | ||
|
|
||
| group('return values', () { | ||
| test('handles success', () async { | ||
| when(api.authenticate(any, any)).thenAnswer( | ||
| (_) async => AuthResultDetails(value: AuthResult.success)); | ||
|
|
||
| final bool result = await plugin.authenticate( | ||
| localizedReason: 'reason', authMessages: <AuthMessages>[]); | ||
|
|
||
| expect(result, true); | ||
| }); | ||
|
|
||
| test('handles failure', () async { | ||
| when(api.authenticate(any, any)).thenAnswer( | ||
| (_) async => AuthResultDetails(value: AuthResult.failure)); | ||
|
|
||
| final bool result = await plugin.authenticate( | ||
| localizedReason: 'reason', authMessages: <AuthMessages>[]); | ||
|
|
||
| expect(result, false); | ||
| }); | ||
|
|
||
| test('converts errorNotAvailable to legacy PlatformException', () async { | ||
| const String errorMessage = 'a message'; | ||
| const String errorDetails = 'some details'; | ||
| when(api.authenticate(any, any)).thenAnswer((_) async => | ||
| AuthResultDetails( | ||
| value: AuthResult.errorNotAvailable, | ||
| errorMessage: errorMessage, | ||
| errorDetails: errorDetails)); | ||
|
|
||
| expect( | ||
| () async => plugin.authenticate( | ||
| localizedReason: 'reason', authMessages: <AuthMessages>[]), | ||
| throwsA(isA<PlatformException>() | ||
| .having((PlatformException e) => e.code, 'code', 'NotAvailable') | ||
| .having( | ||
| (PlatformException e) => e.message, 'message', errorMessage) | ||
| .having((PlatformException e) => e.details, 'details', | ||
| errorDetails))); | ||
| }); | ||
|
|
||
| test('converts errorNotEnrolled to legacy PlatformException', () async { | ||
| const String errorMessage = 'a message'; | ||
| const String errorDetails = 'some details'; | ||
| when(api.authenticate(any, any)).thenAnswer((_) async => | ||
| AuthResultDetails( | ||
| value: AuthResult.errorNotEnrolled, | ||
| errorMessage: errorMessage, | ||
| errorDetails: errorDetails)); | ||
|
|
||
| expect( | ||
| () async => plugin.authenticate( | ||
| localizedReason: 'reason', authMessages: <AuthMessages>[]), | ||
| throwsA(isA<PlatformException>() | ||
| .having((PlatformException e) => e.code, 'code', 'NotEnrolled') | ||
| .having( | ||
| (PlatformException e) => e.message, 'message', errorMessage) | ||
| .having((PlatformException e) => e.details, 'details', | ||
| errorDetails))); | ||
| }); | ||
|
|
||
| test('converts errorPasscodeNotSet to legacy PlatformException', | ||
| () async { | ||
| const String errorMessage = 'a message'; | ||
| const String errorDetails = 'some details'; | ||
| when(api.authenticate(any, any)).thenAnswer((_) async => | ||
| AuthResultDetails( | ||
| value: AuthResult.errorPasscodeNotSet, | ||
| errorMessage: errorMessage, | ||
| errorDetails: errorDetails)); | ||
|
|
||
| expect( | ||
| () async => plugin.authenticate( | ||
| localizedReason: 'reason', authMessages: <AuthMessages>[]), | ||
| throwsA(isA<PlatformException>() | ||
| .having( | ||
| (PlatformException e) => e.code, 'code', 'PasscodeNotSet') | ||
| .having( | ||
| (PlatformException e) => e.message, 'message', errorMessage) | ||
| .having((PlatformException e) => e.details, 'details', | ||
| errorDetails))); | ||
| }); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| class AnotherPlatformAuthMessages extends AuthMessages { | ||
| @override | ||
| Map<String, String> get args => throw UnimplementedError(); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.