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

Commit f083a21

Browse files
committed
update tests
1 parent 3ed97e7 commit f083a21

File tree

3 files changed

+56
-48
lines changed

3 files changed

+56
-48
lines changed

packages/android_intent/lib/android_intent.dart

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ class AndroidIntent {
3636
_channel = const MethodChannel(kChannelName),
3737
_platform = platform ?? const LocalPlatform();
3838

39+
@visibleForTesting
40+
AndroidIntent.private({
41+
@required this.action,
42+
@required Platform platform,
43+
@required MethodChannel channel,
44+
this.flags,
45+
this.category,
46+
this.data,
47+
this.arguments,
48+
this.package,
49+
this.componentName,
50+
}) : _channel = channel,
51+
_platform = platform;
52+
3953
final String action;
4054
final List<int> flags;
4155
final String category;
@@ -65,10 +79,11 @@ class AndroidIntent {
6579

6680
/// Launch the intent.
6781
///
68-
/// This works only on Android platforms. Please guard the call so that your
69-
/// iOS app does not crash. Checked mode will throw an assert exception.
82+
/// This works only on Android platforms.
7083
Future<void> launch() async {
71-
assert(_platform.isAndroid);
84+
if (!_platform.isAndroid) {
85+
return;
86+
}
7287
final Map<String, dynamic> args = <String, dynamic>{'action': action};
7388
if (flags != null) {
7489
args['flags'] = convertFlags(flags);

packages/android_intent/pubspec.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: android_intent
22
description: Flutter plugin for launching Android Intents. Not supported on iOS.
33
author: Flutter Team <[email protected]>
44
homepage: https://github.com/flutter/plugins/tree/master/packages/android_intent
5-
version: 0.3.2
5+
version: 0.3.3
66

77
flutter:
88
plugin:
@@ -15,6 +15,9 @@ dependencies:
1515
sdk: flutter
1616
platform: ^2.0.0
1717
meta: ^1.0.5
18+
dev_dependencies:
19+
test: ^1.3.0
20+
mockito: ^3.0.0
1821
flutter_test:
1922
sdk: flutter
2023
environment:

packages/android_intent/test/android_intent_test.dart

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,56 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'dart:io';
6-
75
import 'package:android_intent/flag.dart';
86
import 'package:flutter/services.dart';
97
import 'package:flutter_test/flutter_test.dart';
108
import 'package:android_intent/android_intent.dart';
9+
import 'package:mockito/mockito.dart';
10+
import 'package:platform/platform.dart';
1111

1212
void main() {
1313
AndroidIntent androidIntent;
14-
const MethodChannel channel =
15-
MethodChannel('plugins.flutter.io/android_intent');
16-
final List<MethodCall> log = <MethodCall>[];
14+
MockMethodChannel mockChannel;
1715
setUp(() {
18-
channel.setMockMethodCallHandler((MethodCall methodCall) async {
19-
log.add(methodCall);
20-
return '';
21-
});
22-
log.clear();
16+
mockChannel = MockMethodChannel();
2317
});
2418
group('AndroidIntent', () {
2519
test('pass right params', () async {
26-
if (Platform.isIOS) {
27-
} else if (Platform.isAndroid) {
28-
androidIntent = AndroidIntent(
20+
androidIntent = AndroidIntent.private(
2921
action: 'action_view',
3022
data: Uri.encodeFull('https://flutter.io'),
3123
flags: <int>[Flag.FLAG_ACTIVITY_NEW_TASK],
32-
);
33-
androidIntent.launch();
34-
expect(
35-
log,
36-
<Matcher>[
37-
isMethodCall('launch', arguments: <String, Object>{
38-
'action': 'action_view',
39-
'data': Uri.encodeFull('https://flutter.io'),
40-
'flags': androidIntent
41-
.convertFlags(<int>[Flag.FLAG_ACTIVITY_NEW_TASK]),
42-
})
43-
],
44-
);
45-
}
24+
channel: mockChannel,
25+
platform: FakePlatform(operatingSystem: 'android'));
26+
androidIntent.launch();
27+
verify(mockChannel.invokeMethod<void>('launch', <String, Object>{
28+
'action': 'action_view',
29+
'data': Uri.encodeFull('https://flutter.io'),
30+
'flags': androidIntent.convertFlags(<int>[Flag.FLAG_ACTIVITY_NEW_TASK]),
31+
}));
32+
});
33+
test('pass null value to action param', () async {
34+
androidIntent = AndroidIntent.private(
35+
action: null,
36+
channel: mockChannel,
37+
platform: FakePlatform(operatingSystem: 'android'));
38+
androidIntent.launch();
39+
verify(mockChannel.invokeMethod<void>('launch', <String, Object>{
40+
'action': null,
41+
}));
4642
});
47-
test('pass wrong params', () async {
48-
if (Platform.isIOS) {
49-
} else if (Platform.isAndroid) {
50-
androidIntent = AndroidIntent(
43+
44+
test('call in ios platform', () async {
45+
androidIntent = AndroidIntent.private(
5146
action: null,
52-
);
53-
androidIntent.launch();
54-
expect(
55-
log,
56-
<Matcher>[
57-
isMethodCall('launch', arguments: <String, Object>{
58-
'action': null,
59-
})
60-
],
61-
);
62-
}
47+
channel: mockChannel,
48+
platform: FakePlatform(operatingSystem: 'ios'));
49+
androidIntent.launch();
50+
verifyZeroInteractions(mockChannel);
6351
});
6452
});
65-
group('Flags: ', () {
66-
androidIntent = AndroidIntent(
53+
group('convertFlags ', () {
54+
androidIntent = const AndroidIntent(
6755
action: 'action_view',
6856
);
6957
test('add filled flag list', () async {
@@ -93,3 +81,5 @@ void main() {
9381
});
9482
});
9583
}
84+
85+
class MockMethodChannel extends Mock implements MethodChannel {}

0 commit comments

Comments
 (0)