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

Commit f676b46

Browse files
Update tests and examples
1 parent bf84b56 commit f676b46

File tree

4 files changed

+119
-114
lines changed

4 files changed

+119
-114
lines changed

packages/url_launcher/url_launcher_android/example/integration_test/url_launcher_test.dart

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,20 @@ import 'dart:io' show Platform;
77
import 'package:flutter/foundation.dart' show kIsWeb;
88
import 'package:flutter_test/flutter_test.dart';
99
import 'package:integration_test/integration_test.dart';
10-
import 'package:url_launcher/url_launcher.dart';
10+
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
1111

1212
void main() {
1313
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
1414

1515
testWidgets('canLaunch', (WidgetTester _) async {
16-
expect(await canLaunch('randomstring'), false);
16+
UrlLauncherPlatform launcher = UrlLauncherPlatform.instance;
1717

18-
// Generally all devices should have some default browser.
19-
expect(await canLaunch('http://flutter.dev'), true);
18+
expect(await launcher.canLaunch('randomstring'), false);
2019

21-
// SMS handling is available by default on most platforms.
22-
if (kIsWeb || !(Platform.isLinux || Platform.isWindows)) {
23-
expect(await canLaunch('sms:5555555555'), true);
24-
}
20+
// Generally all devices should have some default browser.
21+
expect(await launcher.canLaunch('http://flutter.dev'), true);
2522

26-
// tel: and mailto: links may not be openable on every device. iOS
27-
// simulators notably can't open these link types.
23+
// sms:, tel:, and mailto: links may not be openable on every device, so
24+
// aren't tested here.
2825
});
2926
}

packages/url_launcher/url_launcher_android/example/lib/main.dart

Lines changed: 45 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
import 'dart:async';
88

99
import 'package:flutter/material.dart';
10-
import 'package:url_launcher/link.dart';
11-
import 'package:url_launcher/url_launcher.dart';
10+
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
1211

1312
void main() {
1413
runApp(MyApp());
@@ -40,11 +39,15 @@ class _MyHomePageState extends State<MyHomePage> {
4039
String _phone = '';
4140

4241
Future<void> _launchInBrowser(String url) async {
43-
if (await canLaunch(url)) {
44-
await launch(
42+
UrlLauncherPlatform launcher = UrlLauncherPlatform.instance;
43+
if (await launcher.canLaunch(url)) {
44+
await launcher.launch(
4545
url,
46-
forceSafariVC: false,
47-
forceWebView: false,
46+
useSafariVC: false,
47+
useWebView: false,
48+
enableJavaScript: false,
49+
enableDomStorage: false,
50+
universalLinksOnly: false,
4851
headers: <String, String>{'my_header_key': 'my_header_value'},
4952
);
5053
} else {
@@ -53,11 +56,15 @@ class _MyHomePageState extends State<MyHomePage> {
5356
}
5457

5558
Future<void> _launchInWebViewOrVC(String url) async {
56-
if (await canLaunch(url)) {
57-
await launch(
59+
UrlLauncherPlatform launcher = UrlLauncherPlatform.instance;
60+
if (await launcher.canLaunch(url)) {
61+
await launcher.launch(
5862
url,
59-
forceSafariVC: true,
60-
forceWebView: true,
63+
useSafariVC: true,
64+
useWebView: true,
65+
enableJavaScript: false,
66+
enableDomStorage: false,
67+
universalLinksOnly: false,
6168
headers: <String, String>{'my_header_key': 'my_header_value'},
6269
);
6370
} else {
@@ -66,47 +73,39 @@ class _MyHomePageState extends State<MyHomePage> {
6673
}
6774

6875
Future<void> _launchInWebViewWithJavaScript(String url) async {
69-
if (await canLaunch(url)) {
70-
await launch(
76+
UrlLauncherPlatform launcher = UrlLauncherPlatform.instance;
77+
if (await launcher.canLaunch(url)) {
78+
await launcher.launch(
7179
url,
72-
forceSafariVC: true,
73-
forceWebView: true,
80+
useSafariVC: true,
81+
useWebView: true,
7482
enableJavaScript: true,
83+
enableDomStorage: false,
84+
universalLinksOnly: false,
85+
headers: <String, String>{},
7586
);
7687
} else {
7788
throw 'Could not launch $url';
7889
}
7990
}
8091

8192
Future<void> _launchInWebViewWithDomStorage(String url) async {
82-
if (await canLaunch(url)) {
83-
await launch(
93+
UrlLauncherPlatform launcher = UrlLauncherPlatform.instance;
94+
if (await launcher.canLaunch(url)) {
95+
await launcher.launch(
8496
url,
85-
forceSafariVC: true,
86-
forceWebView: true,
97+
useSafariVC: true,
98+
useWebView: true,
99+
enableJavaScript: false,
87100
enableDomStorage: true,
101+
universalLinksOnly: false,
102+
headers: <String, String>{},
88103
);
89104
} else {
90105
throw 'Could not launch $url';
91106
}
92107
}
93108

94-
Future<void> _launchUniversalLinkIos(String url) async {
95-
if (await canLaunch(url)) {
96-
final bool nativeAppLaunchSucceeded = await launch(
97-
url,
98-
forceSafariVC: false,
99-
universalLinksOnly: true,
100-
);
101-
if (!nativeAppLaunchSucceeded) {
102-
await launch(
103-
url,
104-
forceSafariVC: true,
105-
);
106-
}
107-
}
108-
}
109-
110109
Widget _launchStatus(BuildContext context, AsyncSnapshot<void> snapshot) {
111110
if (snapshot.hasError) {
112111
return Text('Error: ${snapshot.error}');
@@ -116,8 +115,17 @@ class _MyHomePageState extends State<MyHomePage> {
116115
}
117116

118117
Future<void> _makePhoneCall(String url) async {
119-
if (await canLaunch(url)) {
120-
await launch(url);
118+
UrlLauncherPlatform launcher = UrlLauncherPlatform.instance;
119+
if (await launcher.canLaunch(url)) {
120+
await launcher.launch(
121+
url,
122+
useSafariVC: false,
123+
useWebView: false,
124+
enableJavaScript: false,
125+
enableDomStorage: false,
126+
universalLinksOnly: true,
127+
headers: <String, String>{},
128+
);
121129
} else {
122130
throw 'Could not launch $url';
123131
}
@@ -178,38 +186,17 @@ class _MyHomePageState extends State<MyHomePage> {
178186
child: const Text('Launch in app(DOM storage ON)'),
179187
),
180188
const Padding(padding: EdgeInsets.all(16.0)),
181-
ElevatedButton(
182-
onPressed: () => setState(() {
183-
_launched = _launchUniversalLinkIos(toLaunch);
184-
}),
185-
child: const Text(
186-
'Launch a universal link in a native app, fallback to Safari.(Youtube)'),
187-
),
188-
const Padding(padding: EdgeInsets.all(16.0)),
189189
ElevatedButton(
190190
onPressed: () => setState(() {
191191
_launched = _launchInWebViewOrVC(toLaunch);
192192
Timer(const Duration(seconds: 5), () {
193193
print('Closing WebView after 5 seconds...');
194-
closeWebView();
194+
UrlLauncherPlatform.instance.closeWebView();
195195
});
196196
}),
197197
child: const Text('Launch in app + close after 5 seconds'),
198198
),
199199
const Padding(padding: EdgeInsets.all(16.0)),
200-
Link(
201-
uri: Uri.parse(
202-
'https://pub.dev/documentation/url_launcher/latest/link/link-library.html'),
203-
target: LinkTarget.blank,
204-
builder: (ctx, openLink) {
205-
return TextButton.icon(
206-
onPressed: openLink,
207-
label: Text('Link Widget documentation'),
208-
icon: Icon(Icons.read_more),
209-
);
210-
},
211-
),
212-
const Padding(padding: EdgeInsets.all(16.0)),
213200
FutureBuilder<void>(future: _launched, builder: _launchStatus),
214201
],
215202
),

packages/url_launcher/url_launcher_ios/example/integration_test/url_launcher_test.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ import 'dart:io' show Platform;
77
import 'package:flutter/foundation.dart' show kIsWeb;
88
import 'package:flutter_test/flutter_test.dart';
99
import 'package:integration_test/integration_test.dart';
10-
import 'package:url_launcher/url_launcher.dart';
10+
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
1111

1212
void main() {
1313
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
1414

1515
testWidgets('canLaunch', (WidgetTester _) async {
16-
expect(await canLaunch('randomstring'), false);
16+
UrlLauncherPlatform launcher = UrlLauncherPlatform.instance;
17+
18+
expect(await launcher.canLaunch('randomstring'), false);
1719

1820
// Generally all devices should have some default browser.
19-
expect(await canLaunch('http://flutter.dev'), true);
21+
expect(await launcher.canLaunch('http://flutter.dev'), true);
2022

21-
// SMS handling is available by default on most platforms.
22-
if (kIsWeb || !(Platform.isLinux || Platform.isWindows)) {
23-
expect(await canLaunch('sms:5555555555'), true);
24-
}
23+
// SMS handling is available by default on test devices.
24+
expect(await launcher.canLaunch('sms:5555555555'), true);
2525

2626
// tel: and mailto: links may not be openable on every device. iOS
2727
// simulators notably can't open these link types.

0 commit comments

Comments
 (0)