Skip to content

[gis_web] Make maybeEnum more robust in google_identity_services_web shared file. #8999

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
merged 12 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/google_identity_services_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.3+1

* Handles potential exceptions gracefully while fetching `Moment*Reason` for invalid value.

## 0.3.3

* Moves all the JavaScript types to extend `JSObject`.
Expand Down
7 changes: 0 additions & 7 deletions packages/google_identity_services_web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,3 @@ Refer to the official documentation site for the latest browser compatibility
information of the underlying JS SDK:

* **Sign In With Google > [Supported browsers and platforms](https://developers.google.com/identity/gsi/web/guides/supported-browsers)**

## Testing

This web-only package uses `dart:test` to test its features. They can be run
with `dart test -p chrome`.

_(Look at `test/README.md` and `tool/run_tests.dart` for more info.)_
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ void main() async {
});

group('prompt', () {
testWidgets('supports a moment notification callback', (_) async {
testWidgets(
'supports a moment notification callback with correct type and reason',
(_) async {
id.initialize(IdConfiguration(client_id: 'testing_1-2-3'));
utils.setMockMomentNotification('skipped', 'user_cancel');

final StreamController<PromptMomentNotification> controller =
StreamController<PromptMomentNotification>();
Expand All @@ -93,11 +96,27 @@ void main() async {

final PromptMomentNotification moment = await controller.stream.first;

// These defaults are set in mock-gis.js
expect(moment.getMomentType(), MomentType.skipped);
expect(moment.getSkippedReason(), MomentSkippedReason.user_cancel);
});

testWidgets(
'supports a moment notification callback while handling invalid reason '
'value gracefully', (_) async {
id.initialize(IdConfiguration(client_id: 'testing_1-2-3'));
utils.setMockMomentNotification('skipped', 'random_invalid_reason');

final StreamController<PromptMomentNotification> controller =
StreamController<PromptMomentNotification>();

id.prompt(controller.add);

final PromptMomentNotification moment = await controller.stream.first;

expect(moment.getMomentType(), MomentType.skipped);
expect(moment.getSkippedReason(), isNull);
});

testWidgets('calls config callback with credential response', (_) async {
const String expected = 'should_be_a_proper_jwt_token';
utils.setMockCredentialResponse(expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,31 @@ void setMockTokenResponse(TokenClient client, [String? authToken]) {
client.setMockTokenResponse(authToken?.toJS);
}

/// Allows calling a `setMockCredentialResponse` method (set by mock-gis.js)
/// Allows calling `setMockCredentialResponse` and `setMockMomentNotification`
/// methods (set by mock-gis.js).
extension on GoogleAccountsId {
external void setMockCredentialResponse(
JSString credential,
JSString select_by, //ignore: non_constant_identifier_names
);

external void setMockMomentNotification(
JSString momentType,
JSString reason,
);
}

/// Sets a mock credential response in `google.accounts.id`.
void setMockCredentialResponse([String value = 'default_value']) {
_getGoogleAccountsId().setMockCredentialResponse(value.toJS, 'auto'.toJS);
}

/// Sets a mock moment notification in `google.accounts.id`.
void setMockMomentNotification(String momentType, String reason) {
_getGoogleAccountsId()
.setMockMomentNotification(momentType.toJS, reason.toJS);
}

GoogleAccountsId _getGoogleAccountsId() {
return _getDeepProperty<GoogleAccountsId>(
web.window as JSObject, 'google.accounts.id');
Expand Down
15 changes: 8 additions & 7 deletions packages/google_identity_services_web/example/web/mock-gis.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ class PromptMomentNotification {
isNotDisplayed() { return this.isDisplayMoment() && this.reason; }
}

const CREDENTIAL_RETURNED = new PromptMomentNotification("dismissed", "credential_returned");
const USER_CANCEL = new PromptMomentNotification("skipped", "user_cancel");

function callAsync(func, timeout = 100) {
window.setTimeout(func, timeout)
}
Expand All @@ -47,11 +44,11 @@ class Id {
if (callback) {
callback(this.mockCredentialResponse);
}
if (momentListener) {
momentListener(CREDENTIAL_RETURNED);
}
if (momentListener) {
if (this.mockMomentNotification) {
momentListener(this.mockMomentNotification);
}
} else if (momentListener) {
momentListener(USER_CANCEL);
}
});
}
Expand All @@ -61,11 +58,15 @@ class Id {
select_by: select_by,
};
}
setMockMomentNotification(momentType, reason) {
this.mockMomentNotification = new PromptMomentNotification(momentType, reason);
}
disableAutoSelect() {}
storeCredential() {}
cancel() {}
revoke(hint, callback) {
this.mockCredentialResponse = null;
this.mockMomentNotification = null;
if (!callback) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
// found in the LICENSE file.

/// Attempts to retrieve an enum value from [haystack] if [needle] is not null.
///
/// Returns `null` if no enum value in [haystack] matches [needle].
T? maybeEnum<T extends Enum>(String? needle, List<T> haystack) {
if (needle == null) {
return null;
}
return haystack.byName(needle);
for (final T value in haystack) {
if (value.name == needle) {
return value;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

A future improvement may be to log a warning or similar when we attempt to decode a value of T that will return null, so people can report bugs. We'll see if that becomes a problem later.

return null;
}

/// The type of several functions from the library, that don't receive
Expand Down
2 changes: 1 addition & 1 deletion packages/google_identity_services_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: google_identity_services_web
description: A Dart JS-interop layer for Google Identity Services. Google's new sign-in SDK for Web that supports multiple types of credentials.
repository: https://github.com/flutter/packages/tree/main/packages/google_identity_services_web
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+google_identiy_services_web%22
version: 0.3.3
version: 0.3.3+1

environment:
sdk: ^3.4.0
Expand Down
2 changes: 1 addition & 1 deletion packages/google_identity_services_web/test/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Tests

Use `dart run tool/run_tests.dart` to run tests in this package.
Use `dart test -p chrome <test_name>.dart` to run tests in this package.

## Failed to run Chrome: No such file or directory

Expand Down