Skip to content

Commit be2f252

Browse files
authored
Merge pull request #24 from Baseflow/implementing_tests
Implementing tests
2 parents cdb634e + d08d9e1 commit be2f252

9 files changed

+137
-94
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 3.0.1
2+
3+
* Added unit-tests to guard API against breaking changes.
4+
15
## 3.0.0
26

37
* Stable null safety release.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Flutter Google Api Availability Plugin
22

3-
[![pub package](https://img.shields.io/pub/v/google_api_availability.svg)](https://pub.dartlang.org/packages/google_api_availability) ![Build status](https://github.com/Baseflow/flutter-google-api-availability/workflows/google_api_availability/badge.svg?branch=master)
3+
[![pub package](https://img.shields.io/pub/v/google_api_availability.svg)](https://pub.dartlang.org/packages/google_api_availability) ![Build status](https://github.com/Baseflow/flutter-google-api-availability/workflows/google_api_availability/badge.svg?branch=master) [![codecov](https://codecov.io/gh/Baseflow/flutter-google-api-availability/branch/master/graph/badge.svg)](https://codecov.io/gh/Baseflow/flutter-google-api-availability)
44

55
A Flutter plugin to check the availability of Google Play services on the current device.
66

@@ -14,7 +14,7 @@ To use this plugin, add `google_api_availability` as a [dependency in your pubsp
1414

1515
```yaml
1616
dependencies:
17-
google_api_availability: ^3.0.0
17+
google_api_availability: ^3.0.1
1818
```
1919
2020
> **NOTE:** There's a known issue with integrating plugins that use Swift into a Flutter project created with the Objective-C template. See issue [Flutter#16049](https://github.com/flutter/flutter/issues/16049) for help on integration.

lib/src/google_api_availability.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:async';
2-
import 'dart:io';
32

3+
import 'package:flutter/foundation.dart';
4+
import 'package:meta/meta.dart';
45
import 'package:flutter/services.dart';
56
import 'package:google_api_availability/src/models/google_play_services_availability.dart';
67

@@ -11,6 +12,9 @@ import 'models/google_play_services_availability.dart';
1112
class GoogleApiAvailability {
1213
const GoogleApiAvailability._();
1314

15+
@visibleForTesting
16+
const GoogleApiAvailability.private();
17+
1418
/// Acquires an instance of the [GoogleApiAvailability] class.
1519
static const GoogleApiAvailability instance = GoogleApiAvailability._();
1620

@@ -22,7 +26,7 @@ class GoogleApiAvailability {
2226
/// [GooglePlayServicesAvailability.notAvailableOnPlatform].
2327
Future<GooglePlayServicesAvailability> checkGooglePlayServicesAvailability(
2428
[bool showDialogIfNecessary = false]) async {
25-
if (!Platform.isAndroid) {
29+
if (defaultTargetPlatform != TargetPlatform.android) {
2630
return GooglePlayServicesAvailability.notAvailableOnPlatform;
2731
}
2832

lib/src/models/google_play_services_availability.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import 'package:meta/meta.dart';
2+
13
/// Indicates possible states of the Google Api Services availability.
24
class GooglePlayServicesAvailability {
35
const GooglePlayServicesAvailability._(this.value);
6+
factory GooglePlayServicesAvailability.byValue(int value) => values[value];
7+
8+
@visibleForTesting
9+
const GooglePlayServicesAvailability.private(this.value);
410

511
/// Represents the integer value of the Google Api Services availability
612
/// state.

pubspec.lock

Lines changed: 1 addition & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
11
# Generated by pub
22
# See https://dart.dev/tools/pub/glossary#lockfile
33
packages:
4-
archive:
5-
dependency: transitive
6-
description:
7-
name: archive
8-
url: "https://pub.dartlang.org"
9-
source: hosted
10-
version: "2.0.13"
11-
args:
12-
dependency: transitive
13-
description:
14-
name: args
15-
url: "https://pub.dartlang.org"
16-
source: hosted
17-
version: "1.6.0"
184
async:
195
dependency: transitive
206
description:
@@ -57,58 +43,20 @@ packages:
5743
url: "https://pub.dartlang.org"
5844
source: hosted
5945
version: "1.15.0"
60-
convert:
61-
dependency: transitive
62-
description:
63-
name: convert
64-
url: "https://pub.dartlang.org"
65-
source: hosted
66-
version: "2.1.1"
67-
crypto:
68-
dependency: transitive
69-
description:
70-
name: crypto
71-
url: "https://pub.dartlang.org"
72-
source: hosted
73-
version: "2.1.5"
74-
e2e:
75-
dependency: "direct dev"
76-
description:
77-
name: e2e
78-
url: "https://pub.dartlang.org"
79-
source: hosted
80-
version: "0.4.3+1"
8146
fake_async:
8247
dependency: transitive
8348
description:
8449
name: fake_async
8550
url: "https://pub.dartlang.org"
8651
source: hosted
8752
version: "1.2.0"
88-
file:
89-
dependency: transitive
90-
description:
91-
name: file
92-
url: "https://pub.dartlang.org"
93-
source: hosted
94-
version: "6.0.0"
9553
flutter:
9654
dependency: "direct main"
9755
description: flutter
9856
source: sdk
9957
version: "0.0.0"
100-
flutter_driver:
101-
dependency: transitive
102-
description: flutter
103-
source: sdk
104-
version: "0.0.0"
10558
flutter_test:
106-
dependency: transitive
107-
description: flutter
108-
source: sdk
109-
version: "0.0.0"
110-
fuchsia_remote_debug_protocol:
111-
dependency: transitive
59+
dependency: "direct dev"
11260
description: flutter
11361
source: sdk
11462
version: "0.0.0"
@@ -140,20 +88,6 @@ packages:
14088
url: "https://pub.dartlang.org"
14189
source: hosted
14290
version: "1.10.0"
143-
platform:
144-
dependency: transitive
145-
description:
146-
name: platform
147-
url: "https://pub.dartlang.org"
148-
source: hosted
149-
version: "3.0.0"
150-
process:
151-
dependency: transitive
152-
description:
153-
name: process
154-
url: "https://pub.dartlang.org"
155-
source: hosted
156-
version: "4.0.0"
15791
sky_engine:
15892
dependency: transitive
15993
description: flutter
@@ -187,13 +121,6 @@ packages:
187121
url: "https://pub.dartlang.org"
188122
source: hosted
189123
version: "1.1.0"
190-
sync_http:
191-
dependency: transitive
192-
description:
193-
name: sync_http
194-
url: "https://pub.dartlang.org"
195-
source: hosted
196-
version: "0.2.0"
197124
term_glyph:
198125
dependency: transitive
199126
description:
@@ -222,20 +149,6 @@ packages:
222149
url: "https://pub.dartlang.org"
223150
source: hosted
224151
version: "2.1.0"
225-
vm_service:
226-
dependency: transitive
227-
description:
228-
name: vm_service
229-
url: "https://pub.dartlang.org"
230-
source: hosted
231-
version: "5.5.0"
232-
webdriver:
233-
dependency: transitive
234-
description:
235-
name: webdriver
236-
url: "https://pub.dartlang.org"
237-
source: hosted
238-
version: "2.1.2"
239152
sdks:
240153
dart: ">=2.12.0-259.9.beta <3.0.0"
241154
flutter: ">=1.12.13+hotfix.6"

pubspec.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: google_api_availability
22
description: A Flutter plugin to check the availability of Google Play Services on an Android device.
3-
version: 3.0.0
3+
version: 3.0.1
44
homepage: https://github.com/baseflowit/flutter-google-api-availability
55

66
environment:
@@ -12,8 +12,9 @@ dependencies:
1212
sdk: flutter
1313

1414
dev_dependencies:
15+
flutter_test:
16+
sdk: flutter
1517
pedantic: ^1.10.0
16-
e2e: ^0.4.0
1718

1819
flutter:
1920
plugin:
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import 'package:flutter/foundation.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:google_api_availability/google_api_availability.dart';
4+
5+
import 'method_channel_mock.dart';
6+
7+
void main() {
8+
TestWidgetsFlutterBinding.ensureInitialized();
9+
10+
test('Should receive notAvailableOnPlatform is not Android', () async {
11+
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
12+
13+
final googlePlayServiceAvailability = await GoogleApiAvailability.private()
14+
.checkGooglePlayServicesAvailability();
15+
16+
expect(googlePlayServiceAvailability,
17+
GooglePlayServicesAvailability.notAvailableOnPlatform);
18+
19+
debugDefaultTargetPlatformOverride = null;
20+
});
21+
22+
test('Should receive the corresponding GooglePlayServiceAvailability',
23+
() async {
24+
final availability = GooglePlayServicesAvailability.serviceDisabled;
25+
26+
MethodChannelMock(
27+
channelName: 'flutter.baseflow.com/google_api_availability/methods',
28+
method: 'checkPlayServicesAvailability',
29+
result: availability.value,
30+
);
31+
32+
final googlePlayServiceAvailability = await GoogleApiAvailability.private()
33+
.checkGooglePlayServicesAvailability();
34+
35+
expect(googlePlayServiceAvailability, availability);
36+
});
37+
38+
test(
39+
'Should receive GooglePlayServiceAvailability.unknown when availability is null',
40+
() async {
41+
final availability = null;
42+
43+
MethodChannelMock(
44+
channelName: 'flutter.baseflow.com/google_api_availability/methods',
45+
method: 'checkPlayServicesAvailability',
46+
result: availability,
47+
);
48+
49+
final googlePlayServiceAvailability = await GoogleApiAvailability.private()
50+
.checkGooglePlayServicesAvailability();
51+
52+
expect(
53+
googlePlayServiceAvailability, GooglePlayServicesAvailability.unknown);
54+
});
55+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:google_api_availability/google_api_availability.dart';
3+
4+
void main() {
5+
test(
6+
'GooglePlayServicesAvailability has the right amount of availability states',
7+
() {
8+
final values = GooglePlayServicesAvailability.values;
9+
10+
expect(values.length, 8);
11+
});
12+
13+
test('check if corresponding status gets received when calling constructor',
14+
() {
15+
final values = GooglePlayServicesAvailability.values;
16+
17+
for (var i = 0; i < values.length; i++) {
18+
expect(values[i], GooglePlayServicesAvailability.byValue(i));
19+
}
20+
});
21+
22+
test('check if toString method returns the corresponding name', () {
23+
var playServicesAvailability = GooglePlayServicesAvailability.private(0);
24+
25+
expect(playServicesAvailability.toString(),
26+
'GooglePlayServicesAvailability.success');
27+
});
28+
}

test/method_channel_mock.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import 'package:flutter/services.dart';
2+
3+
class MethodChannelMock {
4+
final MethodChannel methodChannel;
5+
final String method;
6+
final dynamic result;
7+
final Duration delay;
8+
9+
MethodChannelMock({
10+
required String channelName,
11+
required this.method,
12+
this.result,
13+
this.delay = Duration.zero,
14+
}) : methodChannel = MethodChannel(channelName) {
15+
methodChannel.setMockMethodCallHandler(_handler);
16+
}
17+
18+
Future _handler(MethodCall methodCall) async {
19+
if (methodCall.method != method) {
20+
throw MissingPluginException('No implementation found for method '
21+
'$method on channel ${methodChannel.name}');
22+
}
23+
24+
return Future.delayed(delay, () {
25+
if (result is Exception) {
26+
throw result;
27+
}
28+
29+
return Future.value(result);
30+
});
31+
}
32+
}

0 commit comments

Comments
 (0)