Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit a0d99ee

Browse files
authored
[google_sign_in_web] Migrate to null-safety (#3628)
1 parent cb64042 commit a0d99ee

26 files changed

+322
-653
lines changed

packages/google_sign_in/google_sign_in_web/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.10.0
2+
3+
* Migrate to null-safety.
4+
15
## 0.9.2+1
26

37
* Update Flutter SDK constraint.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Testing
2+
3+
This package utilizes the `integration_test` package to run its tests in a web browser.
4+
5+
See [flutter.dev > Integration testing](https://flutter.dev/docs/testing/integration-tests) for more info.
6+
7+
## Running the tests
8+
9+
Make sure you have updated to the latest Flutter master.
10+
11+
1. Check what version of Chrome is running on the machine you're running tests on.
12+
13+
2. Download and install driver for that version from here:
14+
* <https://chromedriver.chromium.org/downloads>
15+
16+
3. Start the driver using `chromedriver --port=4444`
17+
18+
4. Run tests: `flutter drive -d web-server --browser-name=chrome --driver=test_driver/integration_driver.dart --target=integration_test/TEST_NAME.dart`, or (in Linux):
19+
20+
* Single: `./run_test.sh integration_test/TEST_NAME.dart`
21+
* All: `./run_test.sh`

packages/google_sign_in/google_sign_in_web/test/test_driver/auth2_integration.dart renamed to packages/google_sign_in/google_sign_in_web/example/integration_test/auth2_test.dart

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
// found in the LICENSE file.
44

55
import 'package:flutter/services.dart';
6-
import 'package:integration_test/integration_test.dart';
7-
86
import 'package:flutter_test/flutter_test.dart';
97
import 'package:google_sign_in_platform_interface/google_sign_in_platform_interface.dart';
108
import 'package:google_sign_in_web/google_sign_in_web.dart';
9+
import 'package:integration_test/integration_test.dart';
10+
import 'package:js/js_util.dart' as js_util;
11+
1112
import 'gapi_mocks/gapi_mocks.dart' as gapi_mocks;
1213
import 'src/test_utils.dart';
1314

@@ -25,7 +26,7 @@ void main() {
2526
idToken: expectedTokenData.idToken,
2627
);
2728

28-
GoogleSignInPlugin plugin;
29+
late GoogleSignInPlugin plugin;
2930

3031
group('plugin.init() throws a catchable exception', () {
3132
setUp(() {
@@ -54,15 +55,16 @@ void main() {
5455
);
5556
fail('plugin.init should have thrown an exception!');
5657
} catch (e) {
57-
expect(e.code, 'idpiframe_initialization_failed');
58+
final String code = js_util.getProperty(e, 'code') as String;
59+
expect(code, 'idpiframe_initialization_failed');
5860
}
5961
});
6062
});
6163

6264
group('other methods also throw catchable exceptions on init fail', () {
6365
// This function ensures that init gets called, but for some reason, we
6466
// ignored that it has thrown stuff...
65-
void _discardInit() async {
67+
Future<void> _discardInit() async {
6668
try {
6769
await plugin.init(
6870
hostedDomain: 'foo',
@@ -135,13 +137,13 @@ void main() {
135137
});
136138

137139
testWidgets('signInSilently', (WidgetTester tester) async {
138-
GoogleSignInUserData actualUser = await plugin.signInSilently();
140+
GoogleSignInUserData actualUser = (await plugin.signInSilently())!;
139141

140142
expect(actualUser, expectedUserData);
141143
});
142144

143145
testWidgets('signIn', (WidgetTester tester) async {
144-
GoogleSignInUserData actualUser = await plugin.signIn();
146+
GoogleSignInUserData actualUser = (await plugin.signIn())!;
145147

146148
expect(actualUser, expectedUserData);
147149
});
@@ -185,7 +187,8 @@ void main() {
185187
await plugin.signIn();
186188
fail('plugin.signIn() should have thrown an exception!');
187189
} catch (e) {
188-
expect(e.code, 'popup_closed_by_user');
190+
final String code = js_util.getProperty(e, 'code') as String;
191+
expect(code, 'popup_closed_by_user');
189192
}
190193
});
191194
});

packages/google_sign_in/google_sign_in_web/test/test_driver/gapi_load_integration.dart renamed to packages/google_sign_in/google_sign_in_web/example/integration_test/gapi_load_test.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44

55
import 'dart:html' as html;
66

7-
import 'package:integration_test/integration_test.dart';
8-
97
import 'package:flutter_test/flutter_test.dart';
108
import 'package:google_sign_in_platform_interface/google_sign_in_platform_interface.dart';
119
import 'package:google_sign_in_web/google_sign_in_web.dart';
10+
import 'package:integration_test/integration_test.dart';
11+
1212
import 'gapi_mocks/gapi_mocks.dart' as gapi_mocks;
1313
import 'src/test_utils.dart';
1414

1515
void main() {
1616
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
1717

18-
gapiUrl = toBase64Url(gapi_mocks.auth2InitSuccess(GoogleSignInUserData()));
18+
gapiUrl = toBase64Url(gapi_mocks.auth2InitSuccess(
19+
GoogleSignInUserData(email: '[email protected]', id: '1234')));
1920

2021
testWidgets('Plugin is initialized after GAPI fully loads and init is called',
2122
(WidgetTester tester) async {
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
// Copyright 2019 The Chromium Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4-
import 'package:flutter_test/flutter_test.dart';
5-
6-
import 'package:integration_test/integration_test.dart';
74

5+
import 'package:flutter_test/flutter_test.dart';
86
import 'package:google_sign_in_web/src/generated/gapiauth2.dart' as gapi;
97
import 'package:google_sign_in_web/src/utils.dart';
10-
import 'package:mockito/mockito.dart';
11-
12-
class MockGoogleUser extends Mock implements gapi.GoogleUser {}
13-
14-
class MockBasicProfile extends Mock implements gapi.BasicProfile {}
8+
import 'package:integration_test/integration_test.dart';
159

1610
void main() {
1711
// The non-null use cases are covered by the auth2_test.dart file.
1812
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
1913

2014
group('gapiUserToPluginUserData', () {
21-
var mockUser;
15+
late FakeGoogleUser fakeUser;
2216

2317
setUp(() {
24-
mockUser = MockGoogleUser();
18+
fakeUser = FakeGoogleUser();
2519
});
2620

2721
testWidgets('null user -> null response', (WidgetTester tester) async {
@@ -30,21 +24,45 @@ void main() {
3024

3125
testWidgets('not signed-in user -> null response',
3226
(WidgetTester tester) async {
33-
when(mockUser.isSignedIn()).thenReturn(false);
34-
expect(gapiUserToPluginUserData(mockUser), isNull);
27+
expect(gapiUserToPluginUserData(fakeUser), isNull);
3528
});
3629

3730
testWidgets('signed-in, but null profile user -> null response',
3831
(WidgetTester tester) async {
39-
when(mockUser.isSignedIn()).thenReturn(true);
40-
expect(gapiUserToPluginUserData(mockUser), isNull);
32+
fakeUser.setIsSignedIn(true);
33+
expect(gapiUserToPluginUserData(fakeUser), isNull);
4134
});
4235

4336
testWidgets('signed-in, null userId in profile user -> null response',
4437
(WidgetTester tester) async {
45-
when(mockUser.isSignedIn()).thenReturn(true);
46-
when(mockUser.getBasicProfile()).thenReturn(MockBasicProfile());
47-
expect(gapiUserToPluginUserData(mockUser), isNull);
38+
fakeUser.setIsSignedIn(true);
39+
fakeUser.setBasicProfile(FakeBasicProfile());
40+
expect(gapiUserToPluginUserData(fakeUser), isNull);
4841
});
4942
});
5043
}
44+
45+
class FakeGoogleUser extends Fake implements gapi.GoogleUser {
46+
bool _isSignedIn = false;
47+
gapi.BasicProfile? _basicProfile;
48+
49+
@override
50+
bool isSignedIn() => _isSignedIn;
51+
@override
52+
gapi.BasicProfile? getBasicProfile() => _basicProfile;
53+
54+
void setIsSignedIn(bool isSignedIn) {
55+
_isSignedIn = isSignedIn;
56+
}
57+
58+
void setBasicProfile(gapi.BasicProfile basicProfile) {
59+
_basicProfile = basicProfile;
60+
}
61+
}
62+
63+
class FakeBasicProfile extends Fake implements gapi.BasicProfile {
64+
String? _id;
65+
66+
@override
67+
String? getId() => _id;
68+
}

packages/google_sign_in/google_sign_in_web/test/pubspec.yaml renamed to packages/google_sign_in/google_sign_in_web/example/pubspec.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
name: regular_integration_tests
1+
name: google_sign_in_web_integration_tests
22
publish_to: none
33

44
environment:
5-
sdk: ">=2.2.2 <3.0.0"
5+
sdk: ">=2.12.0-259.9.beta <3.0.0"
6+
flutter: ">=1.27.0-0" # For integration_test from sdk
67

78
dependencies:
89
flutter:
910
sdk: flutter
1011

1112
dev_dependencies:
12-
google_sign_in: ^4.5.3
13+
http: ^0.13.0
14+
js: ^0.6.3
1315
flutter_driver:
1416
sdk: flutter
1517
flutter_test:
1618
sdk: flutter
17-
http: ^0.12.2
18-
mockito: ^4.1.1
1919
integration_test:
20-
path: ../../../integration_test
20+
sdk: flutter
2121

2222
dependency_overrides:
2323
google_sign_in_web:
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
#!/usr/bin/bash
2+
23
if pgrep -lf chromedriver > /dev/null; then
34
echo "chromedriver is running."
45

56
if [ $# -eq 0 ]; then
67
echo "No target specified, running all tests..."
7-
find test_driver/ -iname *_integration.dart | xargs -n1 -i -t flutter drive -d web-server --web-port=7357 --browser-name=chrome --target='{}'
8+
find integration_test/ -iname *_test.dart | xargs -n1 -i -t flutter drive -d web-server --web-port=7357 --browser-name=chrome --driver=test_driver/integration_driver.dart --target='{}'
89
else
910
echo "Running test target: $1..."
1011
set -x
11-
flutter drive -d web-server --web-port=7357 --browser-name=chrome --target=$1
12+
flutter drive -d web-server --web-port=7357 --browser-name=chrome --driver=test_driver/integration_driver.dart --target=$1
1213
fi
1314

1415
else
1516
echo "chromedriver is not running."
1617
fi
1718

19+
20+

0 commit comments

Comments
 (0)