Skip to content

Commit f78e852

Browse files
committed
Updated tests
1 parent 038660f commit f78e852

File tree

4 files changed

+46
-136
lines changed

4 files changed

+46
-136
lines changed

android/src/main/java/com/baseflow/googleapiavailability/GoogleApiAvailabilityManager.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ interface SuccessCallback {
2323

2424
@FunctionalInterface
2525
interface MakeGooglePlayServicesAvailableCallback {
26-
void onSuccess();
26+
void onSuccess(Void v);
2727
}
2828

2929
@FunctionalInterface
@@ -61,16 +61,17 @@ void checkPlayServicesAvailability(Boolean showDialog, Activity activity, Contex
6161
final int connectionResult = googleApiAvailability
6262
.isGooglePlayServicesAvailable(applicationContext);
6363

64-
if (activity != null) {
65-
if (showDialog != null && showDialog) {
66-
googleApiAvailability
67-
.showErrorDialogFragment(activity, connectionResult, GoogleApiAvailabilityConstants.REQUEST_GOOGLE_PLAY_SERVICES);
68-
}
69-
} else {
64+
if (activity == null) {
7065
if (showDialog != null && showDialog) {
7166
// Only log warning when `showDialog` property was `true`.
7267
Log.w(GoogleApiAvailabilityConstants.LOG_TAG, "Unable to show dialog as `Activity` is not available.");
7368
}
69+
showDialog = false;
70+
}
71+
72+
if (showDialog != null && showDialog) {
73+
googleApiAvailability
74+
.showErrorDialogFragment(activity, connectionResult, GoogleApiAvailabilityConstants.REQUEST_GOOGLE_PLAY_SERVICES);
7475
}
7576

7677
successCallback.onSuccess(GoogleApiAvailabilityConstants.toPlayServiceAvailability(connectionResult));
@@ -85,7 +86,7 @@ void makeGooglePlayServicesAvailable(Activity activity, MakeGooglePlayServicesAv
8586

8687
googleApiAvailability.makeGooglePlayServicesAvailable(activity)
8788
.addOnFailureListener((Exception e) -> errorCallback.onError("GoogleApiAvailability.makeGooglePlayServicesAvailable", e.getMessage()))
88-
.addOnSuccessListener((Void t) -> successCallback.onSuccess());
89+
.addOnSuccessListener((Void t) -> successCallback.onSuccess(null));
8990
}
9091

9192
void getErrorString(Context applicationContext, getErrorStringCallback successCallback, ErrorCallback errorCallback) {

example/lib/main.dart

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class _MyAppState extends State<MyApp> {
2020
GooglePlayServicesAvailability.unknown;
2121
String _errorString = "unknown";
2222
bool _isUserResolvable = false;
23-
bool _errorNotificationShown = false;
2423
bool _errorDialogFragmentShown = false;
2524

2625
// Platform messages are asynchronous, so we initialize in an async method.
@@ -96,22 +95,15 @@ class _MyAppState extends State<MyApp> {
9695
}
9796

9897
Future<void> showErrorNotification() async {
99-
bool errorNotificationShown;
100-
10198
try {
102-
errorNotificationShown =
103-
await GoogleApiAvailability.instance.showErrorNotification();
99+
await GoogleApiAvailability.instance.showErrorNotification();
104100
} on PlatformException {
105-
errorNotificationShown = false;
101+
return;
106102
}
107103

108104
if (!mounted) {
109105
return;
110106
}
111-
112-
setState(() {
113-
_errorNotificationShown = errorNotificationShown;
114-
});
115107
}
116108

117109
Future<void> showErrorDialogFragment() async {
@@ -184,9 +176,7 @@ class _MyAppState extends State<MyApp> {
184176
child: const Text('Show error notification'),
185177
color: Colors.red,
186178
),
187-
Center(
188-
child: Text(
189-
'Error notification shown: $_errorNotificationShown\n')),
179+
const SizedBox(height: 30),
190180
MaterialButton(
191181
onPressed: () => showErrorDialogFragment(),
192182
child: const Text('Show error dialog fragment'),

lib/src/google_api_availability.dart

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class GoogleApiAvailability {
3131
Future<GooglePlayServicesAvailability> checkGooglePlayServicesAvailability(
3232
[bool showDialogIfNecessary = false]) async {
3333
if (defaultTargetPlatform != TargetPlatform.android) {
34-
return GooglePlayServicesAvailability.notAvailableOnPlatform;
34+
throw UnsupportedError('Not available on non Android devices.');
3535
}
3636

3737
final availability = await _methodChannel.invokeMethod(
@@ -55,14 +55,7 @@ class GoogleApiAvailability {
5555
throw UnsupportedError('Not available on non Android devices.');
5656
}
5757

58-
final availability =
59-
await _methodChannel.invokeMethod('makeGooglePlayServicesAvailable');
60-
61-
if (availability == null) {
62-
return;
63-
}
64-
65-
return availability;
58+
await _methodChannel.invokeMethod('makeGooglePlayServicesAvailable');
6659
}
6760

6861
/// Returns a human-readable string of the error code.
@@ -104,19 +97,12 @@ class GoogleApiAvailability {
10497
/// Returns true if the connection status did not equal [SUCCESS] or
10598
/// any other non-[ConnectionResult] value.
10699
/// Returns false otherwise.
107-
Future<bool> showErrorNotification() async {
100+
Future<void> showErrorNotification() async {
108101
if (defaultTargetPlatform != TargetPlatform.android) {
109102
throw UnsupportedError('Not available on non Android devices.');
110103
}
111104

112-
final showErrorNotification =
113-
await _methodChannel.invokeMethod('showErrorNotification');
114-
115-
if (showErrorNotification == null) {
116-
return false;
117-
}
118-
119-
return showErrorNotification;
105+
await _methodChannel.invokeMethod('showErrorNotification');
120106
}
121107

122108
/// Display an error dialog according to the [ErrorCode] if the connection status is not [SUCCESS].

test/google_api_availability_test.dart

Lines changed: 30 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ void main() {
88
TestWidgetsFlutterBinding.ensureInitialized();
99

1010
group('checkGooglePlayServiceAvailability', () {
11-
test('Should receive notAvailableOnPlatform if not Android', () async {
11+
test('Should throw UnsuppertedError if not Android', () async {
1212
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
1313

14-
final googlePlayServiceAvailability =
15-
await const GoogleApiAvailability.private()
16-
.checkGooglePlayServicesAvailability();
17-
18-
expect(googlePlayServiceAvailability,
19-
GooglePlayServicesAvailability.notAvailableOnPlatform);
14+
expect(
15+
() async => await const GoogleApiAvailability.private()
16+
.checkGooglePlayServicesAvailability(),
17+
throwsA(isA<UnsupportedError>()));
2018

2119
debugDefaultTargetPlatformOverride = null;
2220
});
@@ -59,63 +57,26 @@ void main() {
5957
});
6058

6159
group('makeGooglePlayServicesAvailable', () {
62-
test('Should receive false if not Android', () async {
60+
test('Should throw UnsuppertedError if not Android', () async {
6361
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
6462

65-
final googlePlayServiceAvailability =
66-
await const GoogleApiAvailability.private()
67-
.makeGooglePlayServicesAvailable();
68-
69-
expect(googlePlayServiceAvailability, false);
63+
expect(
64+
() async => await const GoogleApiAvailability.private()
65+
.makeGooglePlayServicesAvailable(),
66+
throwsA(isA<UnsupportedError>()));
7067

7168
debugDefaultTargetPlatformOverride = null;
7269
});
73-
74-
test('Should receive false when availability is null', () async {
75-
const availability = null;
76-
77-
MethodChannelMock(
78-
channelName: 'flutter.baseflow.com/google_api_availability/methods',
79-
method: 'makeGooglePlayServicesAvailable',
80-
result: availability,
81-
);
82-
83-
final googlePlayServiceAvailability =
84-
await const GoogleApiAvailability.private()
85-
.makeGooglePlayServicesAvailable();
86-
87-
expect(googlePlayServiceAvailability, false);
88-
});
89-
90-
test(
91-
'Should receive true when device is able to set Google Play Services to available',
92-
() async {
93-
const availability = true;
94-
95-
MethodChannelMock(
96-
channelName: 'flutter.baseflow.com/google_api_availability/methods',
97-
method: 'makeGooglePlayServicesAvailable',
98-
result: availability,
99-
);
100-
101-
final makeGooglePlayServiceAvailability =
102-
await const GoogleApiAvailability.private()
103-
.makeGooglePlayServicesAvailable();
104-
105-
expect(makeGooglePlayServiceAvailability, true);
106-
});
10770
});
10871

10972
group('getErrorString', () {
110-
test(
111-
'Should receive "Not available on non Android devices" if not on Android',
112-
() async {
73+
test('Should throw UnsuppertedError if not on Android', () async {
11374
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
11475

115-
final errorString =
116-
await const GoogleApiAvailability.private().getErrorString();
117-
118-
expect(errorString, "Not available on non Android devices");
76+
expect(
77+
() async =>
78+
await const GoogleApiAvailability.private().getErrorString(),
79+
throwsA(isA<UnsupportedError>()));
11980

12081
debugDefaultTargetPlatformOverride = null;
12182
});
@@ -151,13 +112,13 @@ void main() {
151112
});
152113

153114
group('isUserResolvable', () {
154-
test('Should receive false if not Android', () async {
115+
test('Should throw UnsuppertedError if not Android', () async {
155116
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
156117

157-
final isUserResolvable =
158-
await const GoogleApiAvailability.private().isUserResolvable();
159-
160-
expect(isUserResolvable, false);
118+
expect(
119+
() async =>
120+
await const GoogleApiAvailability.private().isUserResolvable(),
121+
throwsA(isA<UnsupportedError>()));
161122

162123
debugDefaultTargetPlatformOverride = null;
163124
});
@@ -192,54 +153,26 @@ void main() {
192153
});
193154

194155
group('showErrorNotification', () {
195-
test('Should receive false if not Android', () async {
156+
test('Should throw UnsuppertedError if not Android', () async {
196157
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
197158

198-
final showErrorNotification =
199-
await const GoogleApiAvailability.private().showErrorNotification();
200-
201-
expect(showErrorNotification, false);
159+
expect(
160+
() async => await const GoogleApiAvailability.private()
161+
.showErrorNotification(),
162+
throwsA(isA<UnsupportedError>()));
202163

203164
debugDefaultTargetPlatformOverride = null;
204165
});
205-
206-
test('Should receive false when showErrorNotification is null', () async {
207-
const showErrorNotification = null;
208-
209-
MethodChannelMock(
210-
channelName: 'flutter.baseflow.com/google_api_availability/methods',
211-
method: 'showErrorNotification',
212-
result: showErrorNotification,
213-
);
214-
215-
final showErrorNotificationResult =
216-
await const GoogleApiAvailability.private().showErrorNotification();
217-
218-
expect(showErrorNotificationResult, false);
219-
});
220-
221-
test('Should receive true when notification is shown', () async {
222-
MethodChannelMock(
223-
channelName: 'flutter.baseflow.com/google_api_availability/methods',
224-
method: 'showErrorNotification',
225-
result: true,
226-
);
227-
228-
final showErrorNotification =
229-
await const GoogleApiAvailability.private().showErrorNotification();
230-
231-
expect(showErrorNotification, true);
232-
});
233166
});
234167

235168
group('showErrorDialogFragment', () {
236-
test('Should receive false if not Android', () async {
169+
test('Should throw UnsuppertedError if not Android', () async {
237170
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
238171

239-
final showErrorDialogFragment =
240-
await const GoogleApiAvailability.private().showErrorDialogFragment();
241-
242-
expect(showErrorDialogFragment, false);
172+
expect(
173+
() async => await const GoogleApiAvailability.private()
174+
.showErrorDialogFragment(),
175+
throwsA(isA<UnsupportedError>()));
243176

244177
debugDefaultTargetPlatformOverride = null;
245178
});

0 commit comments

Comments
 (0)