|
5 | 5 | import 'package:flutter/services.dart';
|
6 | 6 | import 'package:flutter_test/flutter_test.dart';
|
7 | 7 | import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
|
| 8 | +import 'package:url_launcher_windows/src/messages.g.dart'; |
8 | 9 | import 'package:url_launcher_windows/url_launcher_windows.dart';
|
9 | 10 |
|
10 | 11 | void main() {
|
11 |
| - TestWidgetsFlutterBinding.ensureInitialized(); |
12 |
| - |
13 |
| - group('$UrlLauncherWindows', () { |
14 |
| - const MethodChannel channel = |
15 |
| - MethodChannel('plugins.flutter.io/url_launcher_windows'); |
16 |
| - final List<MethodCall> log = <MethodCall>[]; |
17 |
| - channel.setMockMethodCallHandler((MethodCall methodCall) async { |
18 |
| - log.add(methodCall); |
19 |
| - |
20 |
| - // Return null explicitly instead of relying on the implicit null |
21 |
| - // returned by the method channel if no return statement is specified. |
22 |
| - return null; |
23 |
| - }); |
| 12 | + late _FakeUrlLauncherApi api; |
| 13 | + late UrlLauncherWindows plugin; |
24 | 14 |
|
25 |
| - test('registers instance', () { |
26 |
| - UrlLauncherWindows.registerWith(); |
27 |
| - expect(UrlLauncherPlatform.instance, isA<UrlLauncherWindows>()); |
28 |
| - }); |
| 15 | + setUp(() { |
| 16 | + api = _FakeUrlLauncherApi(); |
| 17 | + plugin = UrlLauncherWindows(api: api); |
| 18 | + }); |
29 | 19 |
|
30 |
| - tearDown(() { |
31 |
| - log.clear(); |
32 |
| - }); |
| 20 | + test('registers instance', () { |
| 21 | + UrlLauncherWindows.registerWith(); |
| 22 | + expect(UrlLauncherPlatform.instance, isA<UrlLauncherWindows>()); |
| 23 | + }); |
33 | 24 |
|
34 |
| - test('canLaunch', () async { |
35 |
| - final UrlLauncherWindows launcher = UrlLauncherWindows(); |
36 |
| - await launcher.canLaunch('http://example.com/'); |
37 |
| - expect( |
38 |
| - log, |
39 |
| - <Matcher>[ |
40 |
| - isMethodCall('canLaunch', arguments: <String, Object>{ |
41 |
| - 'url': 'http://example.com/', |
42 |
| - }) |
43 |
| - ], |
44 |
| - ); |
45 |
| - }); |
| 25 | + group('canLaunch', () { |
| 26 | + test('handles true', () async { |
| 27 | + api.canLaunch = true; |
46 | 28 |
|
47 |
| - test('canLaunch should return false if platform returns null', () async { |
48 |
| - final UrlLauncherWindows launcher = UrlLauncherWindows(); |
49 |
| - final bool canLaunch = await launcher.canLaunch('http://example.com/'); |
| 29 | + final bool result = await plugin.canLaunch('http://example.com/'); |
50 | 30 |
|
51 |
| - expect(canLaunch, false); |
| 31 | + expect(result, isTrue); |
| 32 | + expect(api.argument, 'http://example.com/'); |
52 | 33 | });
|
53 | 34 |
|
54 |
| - test('launch', () async { |
55 |
| - final UrlLauncherWindows launcher = UrlLauncherWindows(); |
56 |
| - await launcher.launch( |
57 |
| - 'http://example.com/', |
58 |
| - useSafariVC: true, |
59 |
| - useWebView: false, |
60 |
| - enableJavaScript: false, |
61 |
| - enableDomStorage: false, |
62 |
| - universalLinksOnly: false, |
63 |
| - headers: const <String, String>{}, |
64 |
| - ); |
65 |
| - expect( |
66 |
| - log, |
67 |
| - <Matcher>[ |
68 |
| - isMethodCall('launch', arguments: <String, Object>{ |
69 |
| - 'url': 'http://example.com/', |
70 |
| - 'enableJavaScript': false, |
71 |
| - 'enableDomStorage': false, |
72 |
| - 'universalLinksOnly': false, |
73 |
| - 'headers': <String, String>{}, |
74 |
| - }) |
75 |
| - ], |
76 |
| - ); |
77 |
| - }); |
| 35 | + test('handles false', () async { |
| 36 | + api.canLaunch = false; |
78 | 37 |
|
79 |
| - test('launch with headers', () async { |
80 |
| - final UrlLauncherWindows launcher = UrlLauncherWindows(); |
81 |
| - await launcher.launch( |
82 |
| - 'http://example.com/', |
83 |
| - useSafariVC: true, |
84 |
| - useWebView: false, |
85 |
| - enableJavaScript: false, |
86 |
| - enableDomStorage: false, |
87 |
| - universalLinksOnly: false, |
88 |
| - headers: const <String, String>{'key': 'value'}, |
89 |
| - ); |
90 |
| - expect( |
91 |
| - log, |
92 |
| - <Matcher>[ |
93 |
| - isMethodCall('launch', arguments: <String, Object>{ |
94 |
| - 'url': 'http://example.com/', |
95 |
| - 'enableJavaScript': false, |
96 |
| - 'enableDomStorage': false, |
97 |
| - 'universalLinksOnly': false, |
98 |
| - 'headers': <String, String>{'key': 'value'}, |
99 |
| - }) |
100 |
| - ], |
101 |
| - ); |
| 38 | + final bool result = await plugin.canLaunch('http://example.com/'); |
| 39 | + |
| 40 | + expect(result, isFalse); |
| 41 | + expect(api.argument, 'http://example.com/'); |
102 | 42 | });
|
| 43 | + }); |
| 44 | + |
| 45 | + group('launch', () { |
| 46 | + test('handles success', () async { |
| 47 | + api.canLaunch = true; |
103 | 48 |
|
104 |
| - test('launch universal links only', () async { |
105 |
| - final UrlLauncherWindows launcher = UrlLauncherWindows(); |
106 |
| - await launcher.launch( |
107 |
| - 'http://example.com/', |
108 |
| - useSafariVC: false, |
109 |
| - useWebView: false, |
110 |
| - enableJavaScript: false, |
111 |
| - enableDomStorage: false, |
112 |
| - universalLinksOnly: true, |
113 |
| - headers: const <String, String>{}, |
114 |
| - ); |
115 | 49 | expect(
|
116 |
| - log, |
117 |
| - <Matcher>[ |
118 |
| - isMethodCall('launch', arguments: <String, Object>{ |
119 |
| - 'url': 'http://example.com/', |
120 |
| - 'enableJavaScript': false, |
121 |
| - 'enableDomStorage': false, |
122 |
| - 'universalLinksOnly': true, |
123 |
| - 'headers': <String, String>{}, |
124 |
| - }) |
125 |
| - ], |
126 |
| - ); |
| 50 | + plugin.launch( |
| 51 | + 'http://example.com/', |
| 52 | + useSafariVC: true, |
| 53 | + useWebView: false, |
| 54 | + enableJavaScript: false, |
| 55 | + enableDomStorage: false, |
| 56 | + universalLinksOnly: false, |
| 57 | + headers: const <String, String>{}, |
| 58 | + ), |
| 59 | + completes); |
| 60 | + expect(api.argument, 'http://example.com/'); |
127 | 61 | });
|
128 | 62 |
|
129 |
| - test('launch should return false if platform returns null', () async { |
130 |
| - final UrlLauncherWindows launcher = UrlLauncherWindows(); |
131 |
| - final bool launched = await launcher.launch( |
132 |
| - 'http://example.com/', |
133 |
| - useSafariVC: true, |
134 |
| - useWebView: false, |
135 |
| - enableJavaScript: false, |
136 |
| - enableDomStorage: false, |
137 |
| - universalLinksOnly: false, |
138 |
| - headers: const <String, String>{}, |
139 |
| - ); |
140 |
| - |
141 |
| - expect(launched, false); |
| 63 | + test('handles failure', () async { |
| 64 | + api.canLaunch = false; |
| 65 | + |
| 66 | + await expectLater( |
| 67 | + plugin.launch( |
| 68 | + 'http://example.com/', |
| 69 | + useSafariVC: true, |
| 70 | + useWebView: false, |
| 71 | + enableJavaScript: false, |
| 72 | + enableDomStorage: false, |
| 73 | + universalLinksOnly: false, |
| 74 | + headers: const <String, String>{}, |
| 75 | + ), |
| 76 | + throwsA(isA<PlatformException>())); |
| 77 | + expect(api.argument, 'http://example.com/'); |
142 | 78 | });
|
143 | 79 | });
|
144 | 80 | }
|
| 81 | + |
| 82 | +class _FakeUrlLauncherApi implements UrlLauncherApi { |
| 83 | + /// The argument that was passed to an API call. |
| 84 | + String? argument; |
| 85 | + |
| 86 | + /// Controls the behavior of the fake implementations. |
| 87 | + /// |
| 88 | + /// - [canLaunchUrl] returns this value. |
| 89 | + /// - [launchUrl] throws if this is false. |
| 90 | + bool canLaunch = false; |
| 91 | + |
| 92 | + @override |
| 93 | + Future<bool> canLaunchUrl(String url) async { |
| 94 | + argument = url; |
| 95 | + return canLaunch; |
| 96 | + } |
| 97 | + |
| 98 | + @override |
| 99 | + Future<void> launchUrl(String url) async { |
| 100 | + argument = url; |
| 101 | + if (!canLaunch) { |
| 102 | + throw PlatformException(code: 'Failed'); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments