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
Next Next commit
[url_launcher] Add overridable platform
  • Loading branch information
harryterkelsen committed Oct 18, 2019
commit a440899880829bafddbfa61dacaad66c33e288d4
5 changes: 5 additions & 0 deletions packages/url_launcher/url_launcher/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 5.2.3

* Adds `urlLauncherPlatform` which can be overridden to implement `url_launcher`
on other platforms (which may not want to use MethodChannels).

## 5.2.2

* Re-land embedder v2 support with correct Flutter SDK constraints.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'dart:async';

import 'package:flutter/services.dart';

import 'url_launcher_platform_interface.dart';

const MethodChannel _channel = MethodChannel('plugins.flutter.io/url_launcher');

/// An implementation of [UrlLauncherPlatform] that uses method channels.
class MethodChannelUrlLauncher extends UrlLauncherPlatform {
@override
Future<bool> canLaunch(String url) {
return _channel.invokeMethod<bool>(
'canLaunch',
<String, Object>{'url': url},
);
}

@override
Future<void> closeWebView() {
return _channel.invokeMethod<void>('closeWebView');
}

@override
Future<bool> launch(
String url,
bool useSafariVC,
bool useWebView,
bool enableJavaScript,
bool enableDomStorage,
bool universalLinksOnly,
Map<String, String> headers,
) {
_channel.invokeMethod<bool>(
'launch',
<String, Object>{
'url': url,
'useSafariVC': useSafariVC,
'useWebView': useWebView,
'enableJavaScript': enableJavaScript,
'enableDomStorage': enableDomStorage,
'universalLinksOnly': universalLinksOnly,
'headers': headers,
},
);
}
}
49 changes: 32 additions & 17 deletions packages/url_launcher/url_launcher/lib/url_launcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';

const MethodChannel _channel = MethodChannel('plugins.flutter.io/url_launcher');
import 'url_launcher_platform_interface.dart';
import 'method_channel_url_launcher.dart';

/// Parses the specified URL string and delegates handling of it to the
/// underlying platform.
Expand Down Expand Up @@ -84,17 +85,14 @@ Future<bool> launch(
? SystemUiOverlayStyle.dark
: SystemUiOverlayStyle.light);
}
final bool result = await _channel.invokeMethod<bool>(
'launch',
<String, Object>{
'url': urlString,
'useSafariVC': forceSafariVC ?? isWebURL,
'useWebView': forceWebView ?? false,
'enableJavaScript': enableJavaScript ?? false,
'enableDomStorage': enableDomStorage ?? false,
'universalLinksOnly': universalLinksOnly ?? false,
'headers': headers ?? <String, String>{},
},
final bool result = await urlLauncherPlatform.launch(
urlString,
forceSafariVC ?? isWebURL,
forceWebView ?? false,
enableJavaScript ?? false,
enableDomStorage ?? false,
universalLinksOnly ?? false,
headers ?? <String, String>{},
);
if (statusBarBrightness != null) {
WidgetsBinding.instance.renderView.automaticSystemUiAdjustment =
Expand All @@ -109,10 +107,7 @@ Future<bool> canLaunch(String urlString) async {
if (urlString == null) {
return false;
}
return await _channel.invokeMethod<bool>(
'canLaunch',
<String, Object>{'url': urlString},
);
return await urlLauncherPlatform.canLaunch(urlString);
}

/// Closes the current WebView, if one was previously opened via a call to [launch].
Expand All @@ -127,5 +122,25 @@ Future<bool> canLaunch(String urlString) async {
/// SafariViewController is only available on IOS version >= 9.0, this method does not do anything
/// on IOS version below 9.0
Future<void> closeWebView() async {
return await _channel.invokeMethod<void>('closeWebView');
return await urlLauncherPlatform.closeWebView();
}

UrlLauncherPlatform _platform;

/// The [UrlLauncherPlatform] that will do the platform-specific work.
///
/// Defaults to [MethodChannelUrlLauncher].
UrlLauncherPlatform get urlLauncherPlatform {
if (_platform == null) {
_platform = MethodChannelUrlLauncher();
}
return _platform;
}

/// Sets the [UrlLauncherPlatform] to use.
///
/// Set the platform if you have a platform-specific implementation of
/// [UrlLauncherPlatform] that you would like to use.
set urlLauncherPlatform(UrlLauncherPlatform newPlatform) {
_platform = newPlatform;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

/// The interface that implementations of url_launcher must implement.
abstract class UrlLauncherPlatform {
/// Returns `true` if this platform is able to launch [url].
Future<bool> canLaunch(String url);

/// Returns `true` if the given [url] was successfully launched.
///
/// For documentation on the other arguments, see the `launch` documentation
/// in `package:url_launcher/url_launcher.dart`.
Future<bool> launch(
String url,
bool useSafariVC,
bool useWebView,
bool enableJavaScript,
bool enableDomStorage,
bool universalLinksOnly,
Map<String, String> headers,
);


/// Closes the WebView, if one was opened earlier by `launch`.
Future<void> closeWebView();
}
2 changes: 1 addition & 1 deletion packages/url_launcher/url_launcher/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for launching a URL on Android and iOS. Supports
web, phone, SMS, and email schemes.
author: Flutter Team <[email protected]>
homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher/url_launcher
version: 5.2.2
version: 5.2.3

flutter:
plugin:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_test/flutter_test.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:url_launcher/url_launcher_platform_interface.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();

group('UrlLauncher with mock platform', () {
final MockUrlLauncherPlatform platform = MockUrlLauncherPlatform();
urlLauncherPlatform = platform;

test('can mock "canLaunch"', () async {
expect(canLaunch('http://www.google.com'), completion(isFalse));

platform.launchableUrls.add('http://www.google.com');

expect(canLaunch('http://www.google.com'), completion(isTrue));

platform.launchableUrls.clear();
});

test('can mock "launch"', () async {
expect(launch('http://www.google.com'), completion(isFalse));

platform.launchableUrls.add('http://www.google.com');

expect(launch('http://www.google.com'), completion(isTrue));

platform.launchableUrls.clear();
});

test('can mock "closeWebView"', () async {
expect(closeWebView(), completes);
});
});
}

class MockUrlLauncherPlatform extends UrlLauncherPlatform {
Set<String> launchableUrls = Set<String>();

@override
Future<bool> canLaunch(String url) {
return Future.value(launchableUrls.contains(url));
}

@override
Future<void> closeWebView() {
return Future<void>.value();
}

@override
Future<bool> launch(
String url,
bool useSafariVC,
bool useWebView,
bool enableJavaScript,
bool enableDomStorage,
bool universalLinksOnly,
Map<String, String> headers,
) {
return Future.value(launchableUrls.contains(url));
}
}