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

[url_launcher_web] Add tel URL support #2847

Merged
merged 4 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions packages/url_launcher/url_launcher_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.1.2

- Adds "tel" and "sms" support

# 0.1.1+6

- Open "mailto" urls with target set as "\_top" on Safari browsers.
Expand Down
19 changes: 14 additions & 5 deletions packages/url_launcher/url_launcher_web/lib/url_launcher_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import 'package:url_launcher_platform_interface/url_launcher_platform_interface.

import 'package:platform_detect/platform_detect.dart' show browser;

const _mailtoScheme = 'mailto';
const _safariTargetTopSchemes = {
'mailto',
'tel',
'sms',
};

/// The web implementation of [UrlLauncherPlatform].
///
Expand All @@ -16,7 +20,10 @@ class UrlLauncherPlugin extends UrlLauncherPlatform {
html.Window _window;

// The set of schemes that can be handled by the plugin
static final _supportedSchemes = {'http', 'https', _mailtoScheme};
static final _supportedSchemes = {
'http',
'https',
}.union(_safariTargetTopSchemes);

/// A constructor that allows tests to override the window object used by the plugin.
UrlLauncherPlugin({@visibleForTesting html.Window window})
Expand All @@ -29,16 +36,18 @@ class UrlLauncherPlugin extends UrlLauncherPlatform {

String _getUrlScheme(String url) => Uri.tryParse(url)?.scheme;

bool _isMailtoScheme(String url) => _getUrlScheme(url) == _mailtoScheme;
bool _isSafariTargetTopScheme(String url) =>
_safariTargetTopSchemes.contains(_getUrlScheme(url));

/// Opens the given [url] in a new window.
///
/// Returns the newly created window.
@visibleForTesting
html.WindowBase openNewWindow(String url) {
// We need to open mailto urls on the _top window context on safari browsers.
// We need to open mailto, tel and sms urls on the _top window context on safari browsers.
// See https://github.com/flutter/flutter/issues/51461 for reference.
final target = browser.isSafari && _isMailtoScheme(url) ? '_top' : '';
final target =
browser.isSafari && _isSafariTargetTopScheme(url) ? '_top' : '';
return _window.open(url, target);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/url_launcher/url_launcher_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher/u
# 0.1.y+z is compatible with 1.0.0, if you land a breaking change bump
# the version to 2.0.0.
# See more details: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0
version: 0.1.1+6
version: 0.1.2

flutter:
plugin:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ void main() {
plugin.canLaunch('mailto:name@mydomain.com'), completion(isTrue));
});

test('"tel" URLs -> false', () {
expect(plugin.canLaunch('tel:5551234567'), completion(isFalse));
test('"tel" URLs -> true', () {
expect(plugin.canLaunch('tel:5551234567'), completion(isTrue));
});

test('"sms" URLs -> true', () {
expect(plugin.canLaunch('sms:+19725551212?body=hello%20there'),
completion(isTrue));
});
});

Expand All @@ -48,6 +53,9 @@ void main() {
.thenReturn(MockWindow());
when(mockWindow.open('mailto:name@mydomain.com', ''))
.thenReturn(MockWindow());
when(mockWindow.open('tel:5551234567', '')).thenReturn(MockWindow());
when(mockWindow.open('sms:+19725551212?body=hello%20there', ''))
.thenReturn(MockWindow());
});

test('launching a URL returns true', () {
Expand Down Expand Up @@ -77,6 +85,34 @@ void main() {
),
completion(isTrue));
});

test('launching a "tel" returns true', () {
expect(
plugin.launch(
'tel:5551234567',
useSafariVC: null,
useWebView: null,
universalLinksOnly: null,
enableDomStorage: null,
enableJavaScript: null,
headers: null,
),
completion(isTrue));
});

test('launching a "sms" returns true', () {
expect(
plugin.launch(
'sms:+19725551212?body=hello%20there',
useSafariVC: null,
useWebView: null,
universalLinksOnly: null,
enableDomStorage: null,
enableJavaScript: null,
headers: null,
),
completion(isTrue));
});
});

group('openNewWindow', () {
Expand All @@ -98,6 +134,18 @@ void main() {
verify(mockWindow.open('mailto:name@mydomain.com', ''));
});

test('tel urls should be launched on a new window', () {
plugin.openNewWindow('tel:5551234567');

verify(mockWindow.open('tel:5551234567', ''));
});

test('sms urls should be launched on a new window', () {
plugin.openNewWindow('sms:+19725551212?body=hello%20there');

verify(mockWindow.open('sms:+19725551212?body=hello%20there', ''));
});

group('Safari', () {
setUp(() {
platform.configurePlatformForTesting(browser: platform.safari);
Expand All @@ -120,6 +168,19 @@ void main() {

verify(mockWindow.open('mailto:name@mydomain.com', '_top'));
});

test('tel urls should be launched on the same window', () {
plugin.openNewWindow('tel:5551234567');

verify(mockWindow.open('tel:5551234567', '_top'));
});

test('sms urls should be launched on the same window', () {
plugin.openNewWindow('sms:+19725551212?body=hello%20there');

verify(
mockWindow.open('sms:+19725551212?body=hello%20there', '_top'));
});
});
});
});
Expand Down