Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update tests and examples
  • Loading branch information
stuartmorgan-g committed Nov 16, 2021
commit f676b46ba26bef8bedfee660cd3f5831462b87e1
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,20 @@ import 'dart:io' show Platform;
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

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

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

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

// tel: and mailto: links may not be openable on every device. iOS
// simulators notably can't open these link types.
// sms:, tel:, and mailto: links may not be openable on every device, so
// aren't tested here.
});
}
103 changes: 45 additions & 58 deletions packages/url_launcher/url_launcher_android/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:url_launcher/link.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';

void main() {
runApp(MyApp());
Expand Down Expand Up @@ -40,11 +39,15 @@ class _MyHomePageState extends State<MyHomePage> {
String _phone = '';

Future<void> _launchInBrowser(String url) async {
if (await canLaunch(url)) {
await launch(
UrlLauncherPlatform launcher = UrlLauncherPlatform.instance;
if (await launcher.canLaunch(url)) {
await launcher.launch(
url,
forceSafariVC: false,
forceWebView: false,
useSafariVC: false,
useWebView: false,
enableJavaScript: false,
enableDomStorage: false,
universalLinksOnly: false,
headers: <String, String>{'my_header_key': 'my_header_value'},
);
} else {
Expand All @@ -53,11 +56,15 @@ class _MyHomePageState extends State<MyHomePage> {
}

Future<void> _launchInWebViewOrVC(String url) async {
if (await canLaunch(url)) {
await launch(
UrlLauncherPlatform launcher = UrlLauncherPlatform.instance;
if (await launcher.canLaunch(url)) {
await launcher.launch(
url,
forceSafariVC: true,
forceWebView: true,
useSafariVC: true,
useWebView: true,
enableJavaScript: false,
enableDomStorage: false,
universalLinksOnly: false,
headers: <String, String>{'my_header_key': 'my_header_value'},
);
} else {
Expand All @@ -66,47 +73,39 @@ class _MyHomePageState extends State<MyHomePage> {
}

Future<void> _launchInWebViewWithJavaScript(String url) async {
if (await canLaunch(url)) {
await launch(
UrlLauncherPlatform launcher = UrlLauncherPlatform.instance;
if (await launcher.canLaunch(url)) {
await launcher.launch(
url,
forceSafariVC: true,
forceWebView: true,
useSafariVC: true,
useWebView: true,
enableJavaScript: true,
enableDomStorage: false,
universalLinksOnly: false,
headers: <String, String>{},
);
} else {
throw 'Could not launch $url';
}
}

Future<void> _launchInWebViewWithDomStorage(String url) async {
if (await canLaunch(url)) {
await launch(
UrlLauncherPlatform launcher = UrlLauncherPlatform.instance;
if (await launcher.canLaunch(url)) {
await launcher.launch(
url,
forceSafariVC: true,
forceWebView: true,
useSafariVC: true,
useWebView: true,
enableJavaScript: false,
enableDomStorage: true,
universalLinksOnly: false,
headers: <String, String>{},
);
} else {
throw 'Could not launch $url';
}
}

Future<void> _launchUniversalLinkIos(String url) async {
if (await canLaunch(url)) {
final bool nativeAppLaunchSucceeded = await launch(
url,
forceSafariVC: false,
universalLinksOnly: true,
);
if (!nativeAppLaunchSucceeded) {
await launch(
url,
forceSafariVC: true,
);
}
}
}

Widget _launchStatus(BuildContext context, AsyncSnapshot<void> snapshot) {
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
Expand All @@ -116,8 +115,17 @@ class _MyHomePageState extends State<MyHomePage> {
}

Future<void> _makePhoneCall(String url) async {
if (await canLaunch(url)) {
await launch(url);
UrlLauncherPlatform launcher = UrlLauncherPlatform.instance;
if (await launcher.canLaunch(url)) {
await launcher.launch(
url,
useSafariVC: false,
useWebView: false,
enableJavaScript: false,
enableDomStorage: false,
universalLinksOnly: true,
headers: <String, String>{},
);
} else {
throw 'Could not launch $url';
}
Expand Down Expand Up @@ -178,38 +186,17 @@ class _MyHomePageState extends State<MyHomePage> {
child: const Text('Launch in app(DOM storage ON)'),
),
const Padding(padding: EdgeInsets.all(16.0)),
ElevatedButton(
onPressed: () => setState(() {
_launched = _launchUniversalLinkIos(toLaunch);
}),
child: const Text(
'Launch a universal link in a native app, fallback to Safari.(Youtube)'),
),
const Padding(padding: EdgeInsets.all(16.0)),
ElevatedButton(
onPressed: () => setState(() {
_launched = _launchInWebViewOrVC(toLaunch);
Timer(const Duration(seconds: 5), () {
print('Closing WebView after 5 seconds...');
closeWebView();
UrlLauncherPlatform.instance.closeWebView();
});
}),
child: const Text('Launch in app + close after 5 seconds'),
),
const Padding(padding: EdgeInsets.all(16.0)),
Link(
uri: Uri.parse(
'https://pub.dev/documentation/url_launcher/latest/link/link-library.html'),
target: LinkTarget.blank,
builder: (ctx, openLink) {
return TextButton.icon(
onPressed: openLink,
label: Text('Link Widget documentation'),
icon: Icon(Icons.read_more),
);
},
),
const Padding(padding: EdgeInsets.all(16.0)),
FutureBuilder<void>(future: _launched, builder: _launchStatus),
],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ import 'dart:io' show Platform;
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

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

expect(await launcher.canLaunch('randomstring'), false);

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

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

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