This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[url_launcher] Move away from shared method channel implementation in native packages. #4719
Merged
BeMacized
merged 4 commits into
flutter:main
from
Baseflow:url_launcher/no_shared_method_channel
Feb 8, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
packages/url_launcher/url_launcher_android/lib/url_launcher_android.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2013 The Flutter 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'; | ||
|
||
import 'package:flutter/services.dart'; | ||
import 'package:url_launcher_platform_interface/link.dart'; | ||
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart'; | ||
|
||
const MethodChannel _channel = | ||
MethodChannel('plugins.flutter.io/url_launcher_android'); | ||
|
||
/// An implementation of [UrlLauncherPlatform] for Android. | ||
class UrlLauncherAndroid extends UrlLauncherPlatform { | ||
/// Registers this class as the default instance of [UrlLauncherPlatform]. | ||
static void registerWith() { | ||
UrlLauncherPlatform.instance = UrlLauncherAndroid(); | ||
} | ||
|
||
@override | ||
final LinkDelegate? linkDelegate = null; | ||
|
||
@override | ||
Future<bool> canLaunch(String url) { | ||
return _channel.invokeMethod<bool>( | ||
'canLaunch', | ||
<String, Object>{'url': url}, | ||
).then((bool? value) => value ?? false); | ||
} | ||
|
||
@override | ||
Future<void> closeWebView() { | ||
return _channel.invokeMethod<void>('closeWebView'); | ||
} | ||
|
||
@override | ||
Future<bool> launch( | ||
String url, { | ||
required bool useSafariVC, | ||
required bool useWebView, | ||
required bool enableJavaScript, | ||
required bool enableDomStorage, | ||
required bool universalLinksOnly, | ||
required Map<String, String> headers, | ||
String? webOnlyWindowName, | ||
}) { | ||
return _channel.invokeMethod<bool>( | ||
'launch', | ||
<String, Object>{ | ||
'url': url, | ||
'useWebView': useWebView, | ||
'enableJavaScript': enableJavaScript, | ||
'enableDomStorage': enableDomStorage, | ||
'universalLinksOnly': universalLinksOnly, | ||
'headers': headers, | ||
}, | ||
).then((bool? value) => value ?? false); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
234 changes: 234 additions & 0 deletions
234
packages/url_launcher/url_launcher_android/test/url_launcher_android_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
// Copyright 2013 The Flutter 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/services.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:url_launcher_android/url_launcher_android.dart'; | ||
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart'; | ||
|
||
void main() { | ||
TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
||
group('$UrlLauncherAndroid', () { | ||
const MethodChannel channel = | ||
MethodChannel('plugins.flutter.io/url_launcher_android'); | ||
final List<MethodCall> log = <MethodCall>[]; | ||
channel.setMockMethodCallHandler((MethodCall methodCall) async { | ||
log.add(methodCall); | ||
|
||
// Return null explicitly instead of relying on the implicit null | ||
// returned by the method channel if no return statement is specified. | ||
return null; | ||
}); | ||
|
||
tearDown(() { | ||
log.clear(); | ||
}); | ||
|
||
test('registers instance', () { | ||
UrlLauncherAndroid.registerWith(); | ||
expect(UrlLauncherPlatform.instance, isA<UrlLauncherAndroid>()); | ||
}); | ||
|
||
test('canLaunch', () async { | ||
final UrlLauncherAndroid launcher = UrlLauncherAndroid(); | ||
await launcher.canLaunch('http://example.com/'); | ||
expect( | ||
log, | ||
<Matcher>[ | ||
isMethodCall('canLaunch', arguments: <String, Object>{ | ||
'url': 'http://example.com/', | ||
}) | ||
], | ||
); | ||
}); | ||
|
||
test('canLaunch should return false if platform returns null', () async { | ||
final UrlLauncherAndroid launcher = UrlLauncherAndroid(); | ||
final bool canLaunch = await launcher.canLaunch('http://example.com/'); | ||
|
||
expect(canLaunch, false); | ||
}); | ||
|
||
test('launch', () async { | ||
final UrlLauncherAndroid launcher = UrlLauncherAndroid(); | ||
await launcher.launch( | ||
'http://example.com/', | ||
useSafariVC: true, | ||
useWebView: false, | ||
enableJavaScript: false, | ||
enableDomStorage: false, | ||
universalLinksOnly: false, | ||
headers: const <String, String>{}, | ||
); | ||
expect( | ||
log, | ||
<Matcher>[ | ||
isMethodCall('launch', arguments: <String, Object>{ | ||
'url': 'http://example.com/', | ||
'useWebView': false, | ||
'enableJavaScript': false, | ||
'enableDomStorage': false, | ||
'universalLinksOnly': false, | ||
'headers': <String, String>{}, | ||
}) | ||
], | ||
); | ||
}); | ||
|
||
test('launch with headers', () async { | ||
final UrlLauncherAndroid launcher = UrlLauncherAndroid(); | ||
await launcher.launch( | ||
'http://example.com/', | ||
useSafariVC: true, | ||
useWebView: false, | ||
enableJavaScript: false, | ||
enableDomStorage: false, | ||
universalLinksOnly: false, | ||
headers: const <String, String>{'key': 'value'}, | ||
); | ||
expect( | ||
log, | ||
<Matcher>[ | ||
isMethodCall('launch', arguments: <String, Object>{ | ||
'url': 'http://example.com/', | ||
'useWebView': false, | ||
'enableJavaScript': false, | ||
'enableDomStorage': false, | ||
'universalLinksOnly': false, | ||
'headers': <String, String>{'key': 'value'}, | ||
}) | ||
], | ||
); | ||
}); | ||
|
||
test('launch universal links only', () async { | ||
final UrlLauncherAndroid launcher = UrlLauncherAndroid(); | ||
await launcher.launch( | ||
'http://example.com/', | ||
useSafariVC: false, | ||
useWebView: false, | ||
enableJavaScript: false, | ||
enableDomStorage: false, | ||
universalLinksOnly: true, | ||
headers: const <String, String>{}, | ||
); | ||
expect( | ||
log, | ||
<Matcher>[ | ||
isMethodCall('launch', arguments: <String, Object>{ | ||
'url': 'http://example.com/', | ||
'useWebView': false, | ||
'enableJavaScript': false, | ||
'enableDomStorage': false, | ||
'universalLinksOnly': true, | ||
'headers': <String, String>{}, | ||
}) | ||
], | ||
); | ||
}); | ||
|
||
test('launch force WebView', () async { | ||
final UrlLauncherAndroid launcher = UrlLauncherAndroid(); | ||
await launcher.launch( | ||
'http://example.com/', | ||
useSafariVC: true, | ||
useWebView: true, | ||
enableJavaScript: false, | ||
enableDomStorage: false, | ||
universalLinksOnly: false, | ||
headers: const <String, String>{}, | ||
); | ||
expect( | ||
log, | ||
<Matcher>[ | ||
isMethodCall('launch', arguments: <String, Object>{ | ||
'url': 'http://example.com/', | ||
'useWebView': true, | ||
'enableJavaScript': false, | ||
'enableDomStorage': false, | ||
'universalLinksOnly': false, | ||
'headers': <String, String>{}, | ||
}) | ||
], | ||
); | ||
}); | ||
|
||
test('launch force WebView enable javascript', () async { | ||
final UrlLauncherAndroid launcher = UrlLauncherAndroid(); | ||
await launcher.launch( | ||
'http://example.com/', | ||
useSafariVC: true, | ||
useWebView: true, | ||
enableJavaScript: true, | ||
enableDomStorage: false, | ||
universalLinksOnly: false, | ||
headers: const <String, String>{}, | ||
); | ||
expect( | ||
log, | ||
<Matcher>[ | ||
isMethodCall('launch', arguments: <String, Object>{ | ||
'url': 'http://example.com/', | ||
'useWebView': true, | ||
'enableJavaScript': true, | ||
'enableDomStorage': false, | ||
'universalLinksOnly': false, | ||
'headers': <String, String>{}, | ||
}) | ||
], | ||
); | ||
}); | ||
|
||
test('launch force WebView enable DOM storage', () async { | ||
final UrlLauncherAndroid launcher = UrlLauncherAndroid(); | ||
await launcher.launch( | ||
'http://example.com/', | ||
useSafariVC: true, | ||
useWebView: true, | ||
enableJavaScript: false, | ||
enableDomStorage: true, | ||
universalLinksOnly: false, | ||
headers: const <String, String>{}, | ||
); | ||
expect( | ||
log, | ||
<Matcher>[ | ||
isMethodCall('launch', arguments: <String, Object>{ | ||
'url': 'http://example.com/', | ||
'useWebView': true, | ||
'enableJavaScript': false, | ||
'enableDomStorage': true, | ||
'universalLinksOnly': false, | ||
'headers': <String, String>{}, | ||
}) | ||
], | ||
); | ||
}); | ||
|
||
test('launch should return false if platform returns null', () async { | ||
final UrlLauncherAndroid launcher = UrlLauncherAndroid(); | ||
final bool launched = await launcher.launch( | ||
'http://example.com/', | ||
useSafariVC: true, | ||
useWebView: false, | ||
enableJavaScript: false, | ||
enableDomStorage: false, | ||
universalLinksOnly: false, | ||
headers: const <String, String>{}, | ||
); | ||
|
||
expect(launched, false); | ||
}); | ||
|
||
test('closeWebView default behavior', () async { | ||
final UrlLauncherAndroid launcher = UrlLauncherAndroid(); | ||
await launcher.closeWebView(); | ||
expect( | ||
log, | ||
<Matcher>[isMethodCall('closeWebView', arguments: null)], | ||
); | ||
}); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.