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

[url_launcher] Launches only mailto urls in same window on iOS devices #2740

Merged
merged 10 commits into from
May 16, 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
11 changes: 8 additions & 3 deletions packages/url_launcher/url_launcher_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
# 0.1.1+6

- Open "mailto" urls with target set as "\_top" on Safari browsers.
- Update lower bound of dart dependency to 2.2.0.

# 0.1.1+5

* Update lower bound of dart dependency to 2.1.0.
- Update lower bound of dart dependency to 2.1.0.

# 0.1.1+4

* Declare API stability and compatibility with `1.0.0` (more details at: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0).
- Declare API stability and compatibility with `1.0.0` (more details at: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0).

# 0.1.1+3

- Refactor tests to not rely on the underlying browser behavior.

# 0.1.1+2

- Open urls with target "_top" on iOS PWAs.
- Open urls with target "\_top" on iOS PWAs.

# 0.1.1+1

Expand Down
15 changes: 0 additions & 15 deletions packages/url_launcher/url_launcher_web/lib/src/navigator.dart

This file was deleted.

23 changes: 14 additions & 9 deletions packages/url_launcher/url_launcher_web/lib/url_launcher_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ import 'dart:html' as html;
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 'src/navigator.dart' as navigator;

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

const _mailtoScheme = 'mailto';

/// The web implementation of [UrlLauncherPlatform].
///
/// This class implements the `package:url_launcher` functionality for the web.
class UrlLauncherPlugin extends UrlLauncherPlatform {
html.Window _window;

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

/// A constructor that allows tests to override the window object used by the plugin.
UrlLauncherPlugin({@visibleForTesting html.Window window})
: _window = window ?? html.window;
Expand All @@ -21,25 +27,24 @@ class UrlLauncherPlugin extends UrlLauncherPlatform {
UrlLauncherPlatform.instance = UrlLauncherPlugin();
}

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

bool _isMailtoScheme(String url) => _getUrlScheme(url) == _mailtoScheme;

/// Opens the given [url] in a new window.
///
/// Returns the newly created window.
@visibleForTesting
html.WindowBase openNewWindow(String url) {
// We need to open on _top in ios browsers in standalone mode.
// We need to open mailto urls on the _top window context on safari browsers.
// See https://github.com/flutter/flutter/issues/51461 for reference.
final target = navigator.standalone ? '_top' : '';
final target = browser.isSafari && _isMailtoScheme(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(_supportedSchemes.contains(_getUrlScheme(url)));
}

@override
Expand Down
6 changes: 3 additions & 3 deletions 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+5
version: 0.1.1+6

flutter:
plugin:
Expand All @@ -15,12 +15,12 @@ flutter:

dependencies:
url_launcher_platform_interface: ^1.0.1
platform_detect: ^1.4.0
flutter:
sdk: flutter
flutter_web_plugins:
sdk: flutter
meta: ^1.1.7
js: ^0.6.0

dev_dependencies:
flutter_test:
Expand All @@ -30,5 +30,5 @@ dev_dependencies:
mockito: ^4.1.1

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: ">=2.2.0 <3.0.0"
flutter: ">=1.10.0 <2.0.0"
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@
import 'dart:html' as html;
import 'package:flutter_test/flutter_test.dart';
import 'package:url_launcher_web/url_launcher_web.dart';
import 'package:url_launcher_web/src/navigator.dart' as navigator;
import 'package:mockito/mockito.dart';

import 'package:platform_detect/test_utils.dart' as 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 @@ -75,28 +80,46 @@ void main() {
});

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

setUp(() {
_standalone = navigator.standalone;
verify(mockWindow.open('http://www.google.com', ''));
});

tearDown(() {
navigator.standalone = _standalone;
});

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

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

test('the window that is launched is in the same window', () {
navigator.standalone = true;
test('mailto urls should be launched on a new window', () {
plugin.openNewWindow('mailto:[email protected]');

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

group('Safari', () {
setUp(() {
platform.configurePlatformForTesting(browser: platform.safari);
});

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

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

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

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

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

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