Skip to content

Commit 7cba7f3

Browse files
committed
Added tests, added possible null values checks
1 parent 9ac9195 commit 7cba7f3

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

lib/src/google_api_availability.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ class GoogleApiAvailability {
2525
/// This feature is only available on Android devices. On any other platforms
2626
/// the [checkPlayServicesAvailability] method will always return
2727
/// [GooglePlayServicesAvailability.notAvailableOnPlatform].
28+
///
29+
/// Returns the connection status of Google Play Service.
30+
/// Optionally, you can also show an error dialog if the connection status is not [SUCCESS].
2831
Future<GooglePlayServicesAvailability> checkGooglePlayServicesAvailability(
2932
[bool showDialogIfNecessary = false]) async {
3033
if (defaultTargetPlatform != TargetPlatform.android) {
@@ -55,6 +58,10 @@ class GoogleApiAvailability {
5558
final availability =
5659
await _methodChannel.invokeMethod('makeGooglePlayServicesAvailable');
5760

61+
if (availability == null) {
62+
return false;
63+
}
64+
5865
return availability;
5966
}
6067

@@ -67,6 +74,10 @@ class GoogleApiAvailability {
6774

6875
final errorString = await _methodChannel.invokeMethod('getErrorString');
6976

77+
if (errorString == null) {
78+
return "ErrorString is null";
79+
}
80+
7081
return errorString;
7182
}
7283

@@ -81,6 +92,10 @@ class GoogleApiAvailability {
8192
final isUserResolvable =
8293
await _methodChannel.invokeMethod('isUserResolvable');
8394

95+
if (isUserResolvable == null) {
96+
return false;
97+
}
98+
8499
return isUserResolvable;
85100
}
86101

@@ -99,6 +114,10 @@ class GoogleApiAvailability {
99114
final showErrorNotification =
100115
await _methodChannel.invokeMethod('showErrorNotification');
101116

117+
if (showErrorNotification == null) {
118+
return false;
119+
}
120+
102121
return showErrorNotification;
103122
}
104123
}

test/google_api_availability_test.dart

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,25 @@ void main() {
7171
debugDefaultTargetPlatformOverride = null;
7272
});
7373

74+
test(
75+
'Should receive false when availability is null',
76+
() async {
77+
const availability = null;
78+
79+
MethodChannelMock(
80+
channelName: 'flutter.baseflow.com/google_api_availability/methods',
81+
method: 'makeGooglePlayServicesAvailable',
82+
result: availability,
83+
);
84+
85+
final googlePlayServiceAvailability =
86+
await const GoogleApiAvailability.private()
87+
.makeGooglePlayServicesAvailable();
88+
89+
expect(googlePlayServiceAvailability,
90+
false);
91+
});
92+
7493
test(
7594
'Should receive true when device is able to set Google Play Services to available',
7695
() async {
@@ -104,6 +123,25 @@ void main() {
104123
debugDefaultTargetPlatformOverride = null;
105124
});
106125

126+
test(
127+
'Should receive "ErrorString is null" when availability is null',
128+
() async {
129+
const errorString = null;
130+
131+
MethodChannelMock(
132+
channelName: 'flutter.baseflow.com/google_api_availability/methods',
133+
method: 'getErrorString',
134+
result: errorString,
135+
);
136+
137+
final errorStringResult =
138+
await const GoogleApiAvailability.private()
139+
.getErrorString();
140+
141+
expect(errorStringResult,
142+
"ErrorString is null");
143+
});
144+
107145
test('Should receive SUCCESS when connection status is success', () async {
108146
MethodChannelMock(
109147
channelName: 'flutter.baseflow.com/google_api_availability/methods',
@@ -130,6 +168,25 @@ void main() {
130168
debugDefaultTargetPlatformOverride = null;
131169
});
132170

171+
test(
172+
'Should receive false when isUserResolvable is null',
173+
() async {
174+
const isUserResolvable = null;
175+
176+
MethodChannelMock(
177+
channelName: 'flutter.baseflow.com/google_api_availability/methods',
178+
method: 'isUserResolvable',
179+
result: isUserResolvable,
180+
);
181+
182+
final isUserResolvableResult =
183+
await const GoogleApiAvailability.private()
184+
.isUserResolvable();
185+
186+
expect(isUserResolvableResult,
187+
false);
188+
});
189+
133190
test('Should receive true when error is user resolvable', () async {
134191
MethodChannelMock(
135192
channelName: 'flutter.baseflow.com/google_api_availability/methods',
@@ -156,6 +213,25 @@ void main() {
156213
debugDefaultTargetPlatformOverride = null;
157214
});
158215

216+
test(
217+
'Should receive false when showErrorNotification is null',
218+
() async {
219+
const showErrorNotification = null;
220+
221+
MethodChannelMock(
222+
channelName: 'flutter.baseflow.com/google_api_availability/methods',
223+
method: 'showErrorNotification',
224+
result: showErrorNotification,
225+
);
226+
227+
final showErrorNotificationResult =
228+
await const GoogleApiAvailability.private()
229+
.showErrorNotification();
230+
231+
expect(showErrorNotificationResult,
232+
false);
233+
});
234+
159235
test('Should receive true when notification is shown', () async {
160236
MethodChannelMock(
161237
channelName: 'flutter.baseflow.com/google_api_availability/methods',

0 commit comments

Comments
 (0)