This repository has been 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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[url_launcher] Move away from shared method channel implementation in…
… native packages. (#4719) * Move away from shared method channel implementation in native packages. * Implement PR Feedback Implement PR Feedback Implement PR Feedback * Fix missed test * Implement PR feedback
- Loading branch information
Showing
26 changed files
with
1,213 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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.