Skip to content
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
34 changes: 23 additions & 11 deletions packages/url_launcher/url_launcher_web/lib/url_launcher_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,25 @@ import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:meta/meta.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';

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

final _httpSchemes = {
'http',
'https',
};

// See https://github.com/flutter/flutter/issues/51461
final _safariTargetTopSchemes = {
'mailto',
};

/// The set of schemes that can be handled by the plugin
final _validSchemes = _httpSchemes.union(_safariTargetTopSchemes);

/// The web implementation of [UrlLauncherPlatform].
///
/// This class implements the `package:url_launcher` functionality for the web.
class UrlLauncherPlugin extends UrlLauncherPlatform {
static final _iosPlatforms = RegExp(r'iPad|iPhone|iPod');
html.Window _window;

/// A constructor that allows tests to override the window object used by the plugin.
Expand All @@ -21,9 +35,7 @@ class UrlLauncherPlugin extends UrlLauncherPlatform {
UrlLauncherPlatform.instance = UrlLauncherPlugin();
}

bool get _isIos => _iosPlatforms.hasMatch(_window.navigator.platform);

bool _isMailTo(String url) => Uri.tryParse(url)?.isScheme('mailto') ?? false;
String _getUrlScheme(String url) => Uri.tryParse(url)?.scheme;

/// Opens the given [url] in a new window.
///
Expand All @@ -32,18 +44,18 @@ class UrlLauncherPlugin extends UrlLauncherPlatform {
html.WindowBase openNewWindow(String url) {
// We need to open mailto urls on the _top window context on iOS devices.
// See https://github.com/flutter/flutter/issues/51461 for reference.
final target = _isIos && _isMailTo(url) ? '_top' : '';

final target =
browser.isSafari && _safariTargetTopSchemes.contains(_getUrlScheme(url))
? '_top'
: '';

return _window.open(url, target);
}

@override
Future<bool> canLaunch(String url) {
final Uri parsedUrl = Uri.tryParse(url);
if (parsedUrl == null) return Future<bool>.value(false);

return Future<bool>.value(parsedUrl.isScheme('http') ||
parsedUrl.isScheme('https') ||
parsedUrl.isScheme('mailto'));
return Future<bool>.value(_validSchemes.contains(_getUrlScheme(url)));
}

@override
Expand Down
3 changes: 2 additions & 1 deletion packages/url_launcher/url_launcher_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ flutter:

dependencies:
url_launcher_platform_interface: ^1.0.1
platform_detect: ^1.4.0
flutter:
sdk: flutter
flutter_web_plugins:
Expand All @@ -29,5 +30,5 @@ dev_dependencies:
mockito: ^4.1.1

environment:
sdk: ">=2.0.0-dev.28.0 <3.0.0"
sdk: ">=2.2.0 <3.0.0"
flutter: ">=1.10.0 <2.0.0"
100 changes: 22 additions & 78 deletions packages/url_launcher/url_launcher_web/test/url_launcher_web_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,19 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:url_launcher_web/url_launcher_web.dart';
import 'package:mockito/mockito.dart';

class MockNavigator extends Mock implements html.Navigator {
final String platform;
import 'package:platform_detect/test_utils.dart' as platform;

MockNavigator(this.platform);
}

class MockWindow extends Mock implements html.Window {
final MockNavigator navigator;

MockWindow({String platform = ''}) : navigator = MockNavigator(platform);
}
class MockWindow extends Mock implements html.Window {}

void main() {
group('$UrlLauncherPlugin', () {
MockWindow mockWindow = MockWindow();
UrlLauncherPlugin plugin = UrlLauncherPlugin(window: mockWindow);

setUp(() {
platform.configurePlatformForTesting(browser: platform.chrome);
});

group('canLaunch', () {
test('"http" URLs -> true', () {
expect(plugin.canLaunch('http://google.com'), completion(isTrue));
Expand Down Expand Up @@ -84,89 +80,37 @@ void main() {
});

group('openNewWindow', () {
test('the window that is launched is a new window', () {
test('http(s) urls should be launched in a new window', () {
plugin.openNewWindow('http://www.google.com');
plugin.openNewWindow('https://www.google.com');

verify(mockWindow.open('http://www.google.com', ''));
verify(mockWindow.open('https://www.google.com', ''));
});

group('iosDevices', () {
test('http urls should be launched in a new window', () {
final mockIosWindow = MockWindow(platform: 'iPhone');
UrlLauncherPlugin plugin = UrlLauncherPlugin(window: mockIosWindow);
test('mailto urls should be launched on a new window', () {
plugin.openNewWindow('mailto:[email protected]');

plugin.openNewWindow('http://www.google.com');
verify(mockWindow.open('mailto:[email protected]', ''));
});

verify(mockIosWindow.open('http://www.google.com', ''));
group('Safari', () {
setUp(() {
platform.configurePlatformForTesting(browser: platform.safari);
});

test('https urls should be launched in a new window', () {
final mockIosWindow = MockWindow(platform: 'iPhone');
UrlLauncherPlugin plugin = UrlLauncherPlugin(window: mockIosWindow);

test('http(s) urls should be launched in a new window', () {
plugin.openNewWindow('http://www.google.com');
plugin.openNewWindow('https://www.google.com');

verify(mockIosWindow.open('https://www.google.com', ''));
});

test('mailto urls should be launched on the same window on Iphone', () {
final mockIosWindow = MockWindow(platform: 'iPhone');
UrlLauncherPlugin plugin = UrlLauncherPlugin(window: mockIosWindow);

plugin.openNewWindow('mailto:[email protected]');

verify(mockIosWindow.open('mailto:[email protected]', '_top'));
});

test('mailto urls should be launched on the same window on Ipad', () {
final mockIosWindow = MockWindow(platform: 'iPad');
UrlLauncherPlugin plugin = UrlLauncherPlugin(window: mockIosWindow);

plugin.openNewWindow('mailto:[email protected]');

verify(mockIosWindow.open('mailto:[email protected]', '_top'));
});

test('mailto urls should be launched on the same window on Iphone', () {
final mockIosWindow = MockWindow(platform: 'iPod');
UrlLauncherPlugin plugin = UrlLauncherPlugin(window: mockIosWindow);

plugin.openNewWindow('mailto:[email protected]');

verify(mockIosWindow.open('mailto:[email protected]', '_top'));
});

test(
'mailto urls should be launched on the same window on an simulated Iphone',
() {
final mockIosWindow = MockWindow(platform: 'iPhone Simulator');
UrlLauncherPlugin plugin = UrlLauncherPlugin(window: mockIosWindow);

plugin.openNewWindow('mailto:[email protected]');

verify(mockIosWindow.open('mailto:[email protected]', '_top'));
verify(mockWindow.open('http://www.google.com', ''));
verify(mockWindow.open('https://www.google.com', ''));
});

test(
'mailto urls should be launched on the same window on an simulated Ipad',
() {
final mockIosWindow = MockWindow(platform: 'iPad Simulator');
UrlLauncherPlugin plugin = UrlLauncherPlugin(window: mockIosWindow);

plugin.openNewWindow('mailto:[email protected]');

verify(mockIosWindow.open('mailto:[email protected]', '_top'));
});

test(
'mailto urls should be launched on the same window on an simulated Iphone',
() {
final mockIosWindow = MockWindow(platform: 'iPod Simulator');
UrlLauncherPlugin plugin = UrlLauncherPlugin(window: mockIosWindow);

test('mailto urls should be launched on the same window', () {
plugin.openNewWindow('mailto:[email protected]');

verify(mockIosWindow.open('mailto:[email protected]', '_top'));
verify(mockWindow.open('mailto:[email protected]', '_top'));
});
});
});
Expand Down