Skip to content
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

[local_auth_darwin] MacOS Support #6267

Open
wants to merge 96 commits into
base: main
Choose a base branch
from

Conversation

alexrabin-sentracam
Copy link

@alexrabin-sentracam alexrabin-sentracam commented Mar 5, 2024

Adds macOS support for local_auth_darwin

Screenshot 2024-03-05 at 8 30 35 AM

Screenshot 2024-03-05 at 8 30 56 AM

Cancelled Example:

Screenshot 2024-03-05 at 8 31 12 AM

Success Example

Screenshot 2024-03-05 at 8 31 32 AM

Error Example

Screenshot 2024-03-05 at 4 01 58 PM

List which issues are fixed by this PR. You must list at least one issue.

flutter/flutter#140685

If you had to change anything in the flutter/tests repo, include a link to the migration guide as per the breaking change policy.

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Copy link

google-cla bot commented Mar 5, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@stuartmorgan stuartmorgan self-requested a review March 5, 2024 18:21
@stuartmorgan
Copy link
Contributor

Thanks for the contribution!

This appears to be very similar to #5766; does this need to be a new PR rather than leaving comments on that one about any suggested changes?

@alexrabin-sentracam
Copy link
Author

@stuartmorgan That pr doesn't seem to have any momentum and is outdated so I figured I would take this on.

@stuartmorgan
Copy link
Contributor

Is this ready for re-review? There are still unaddressed comments and failing tests, so I assumed it was a work-in-progress.

@alexrabin-sentracam
Copy link
Author

I could use some assistance on the comments @stuartmorgan left me. I am not seeing a clean way to use the Platform.isMacos and still have the dart test cases pass.

I am also not well versed in testing the alert construction as you requested.

@stuartmorgan
Copy link
Contributor

I am not seeing a clean way to use the Platform.isMacos and still have the dart test cases pass.

Per my earlier comment, you can wrap those calls in something that can have an alternate implementation injected for unit tests, using standard dependency injection patterns.

@alexrabin-sentracam
Copy link
Author

I am not seeing a clean way to use the Platform.isMacos and still have the dart test cases pass.

Per my earlier comment, you can wrap those calls in something that can have an alternate implementation injected for unit tests, using standard dependency injection patterns.

All fixed. I addressed all your comments @stuartmorgan . This pr is ready for re-review.

@stuartmorgan
Copy link
Contributor

@alexrabin-sentracam I'll re-review when I have time (I currently have some very high priority work that will delay it); it's not necessary to constantly merge the branch in the meantime.

@stuartmorgan stuartmorgan self-requested a review April 30, 2024 13:55
@alexrabin-sentracam
Copy link
Author

@stuartmorgan Okay sounds good. Thank you.

@implementation FLADefaultAlertFactory

#if TARGET_OS_OSX
- (NSAlert *)createNSAlert {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I would just call this alert, for consistency with the iOS version (see next comment).

return [[NSAlert alloc] init];
}
#elif TARGET_OS_IOS
- (UIAlertController *)createUIAlert:(NSString *)message {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Injections wrappers should be as thin as possible, since by definition anything in this method won't be tested. So this should be alertControllerWithTitle:message:preferredStyle:, and be a direct passthrough.

@@ -18,6 +18,24 @@ - (LAContext *)createAuthContext {
}
@end

@interface FLADefaultAlertFactory : NSObject <FLADAlertFactory>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a declaration comment, per the style guide.

@@ -49,24 +67,37 @@ - (instancetype)initWithOptions:(nonnull FLADAuthOptions *)options
@interface FLALocalAuthPlugin ()
@property(nonatomic, strong, nullable) FLAStickyAuthState *lastCallState;
@property(nonatomic, strong) NSObject<FLADAuthContextFactory> *authContextFactory;
@property(nonatomic, strong) NSObject<FLADAlertFactory> *alertFactory;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These need declaration comments, per the style guide. (It's an oversight that the others don't have them.)

- (instancetype)init {
return [self initWithContextFactory:[[FLADefaultAuthContextFactory alloc] init]];
- (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
self = [super init];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be calling the init with the most parameters, not re-implementing initialization.

- (instancetype)initWithNSAlert:(NSAlert *)alert {
self = [super init];
if (self) {
_alert = alert;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These aren't doing copies, which violates the current API contract. (This will be resolved if you don't declare them as copy though.)

// Tests that the alert window is the same as the registrar window
XCTAssertEqual(alertFactory.alert.window, reg.view.window);
// TODO(stuartmorgan): Fix this; this was the pre-Pigeon-migration
// behavior, so is preserved as part of the migration, but a failed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can an entirely new platform implementation have previous behavior that needs to be preserved?

StubAlertFactory *alertFactory;
#if TARGET_OS_OSX

id mockNSAlert = OCMClassMock([NSAlert class]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a mock? The only thing that this appears to be used for in the test is to read a public property back out of it, which can be done with a real object, avoiding the need for a mock.

FlutterError *_Nullable error) {
XCTAssertTrue([NSThread isMainThread]);
// Tests that the alert window is the same as the registrar window
XCTAssertEqual(alertFactory.alert.window, reg.view.window);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect there to also be a test that if the user provides a custom message, the alert has that message.

[self waitForExpectationsWithTimeout:kTimeout handler:nil];
}

#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've changed the iOS codepaths to make the alert testable; there needs to be at least one new test covering those changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants