mock_url_launcher.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2013 The Flutter Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. import 'package:flutter_test/flutter_test.dart';
  5. import 'package:plugin_platform_interface/plugin_platform_interface.dart';
  6. import 'package:url_launcher_platform_interface/link.dart';
  7. import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
  8. class MockUrlLauncher extends Fake
  9. with MockPlatformInterfaceMixin
  10. implements UrlLauncherPlatform {
  11. String? url;
  12. PreferredLaunchMode? launchMode;
  13. bool? useSafariVC;
  14. bool? useWebView;
  15. bool? enableJavaScript;
  16. bool? enableDomStorage;
  17. bool? universalLinksOnly;
  18. Map<String, String>? headers;
  19. String? webOnlyWindowName;
  20. bool? response;
  21. bool closeWebViewCalled = false;
  22. bool canLaunchCalled = false;
  23. bool launchCalled = false;
  24. // ignore: use_setters_to_change_properties
  25. void setCanLaunchExpectations(String url) {
  26. this.url = url;
  27. }
  28. void setLaunchExpectations({
  29. required String url,
  30. PreferredLaunchMode? launchMode,
  31. bool? useSafariVC,
  32. bool? useWebView,
  33. required bool enableJavaScript,
  34. required bool enableDomStorage,
  35. required bool universalLinksOnly,
  36. required Map<String, String> headers,
  37. required String? webOnlyWindowName,
  38. }) {
  39. this.url = url;
  40. this.launchMode = launchMode;
  41. this.useSafariVC = useSafariVC;
  42. this.useWebView = useWebView;
  43. this.enableJavaScript = enableJavaScript;
  44. this.enableDomStorage = enableDomStorage;
  45. this.universalLinksOnly = universalLinksOnly;
  46. this.headers = headers;
  47. this.webOnlyWindowName = webOnlyWindowName;
  48. }
  49. // ignore: use_setters_to_change_properties
  50. void setResponse(bool response) {
  51. this.response = response;
  52. }
  53. @override
  54. LinkDelegate? get linkDelegate => null;
  55. @override
  56. Future<bool> canLaunch(String url) async {
  57. expect(url, this.url);
  58. canLaunchCalled = true;
  59. return response!;
  60. }
  61. @override
  62. Future<bool> launch(
  63. String url, {
  64. required bool useSafariVC,
  65. required bool useWebView,
  66. required bool enableJavaScript,
  67. required bool enableDomStorage,
  68. required bool universalLinksOnly,
  69. required Map<String, String> headers,
  70. String? webOnlyWindowName,
  71. }) async {
  72. expect(url, this.url);
  73. expect(useSafariVC, this.useSafariVC);
  74. expect(useWebView, this.useWebView);
  75. expect(enableJavaScript, this.enableJavaScript);
  76. expect(enableDomStorage, this.enableDomStorage);
  77. expect(universalLinksOnly, this.universalLinksOnly);
  78. expect(headers, this.headers);
  79. expect(webOnlyWindowName, this.webOnlyWindowName);
  80. launchCalled = true;
  81. return response!;
  82. }
  83. @override
  84. Future<bool> launchUrl(String url, LaunchOptions options) async {
  85. expect(url, this.url);
  86. expect(options.mode, launchMode);
  87. expect(options.webViewConfiguration.enableJavaScript, enableJavaScript);
  88. expect(options.webViewConfiguration.enableDomStorage, enableDomStorage);
  89. expect(options.webViewConfiguration.headers, headers);
  90. expect(options.webOnlyWindowName, webOnlyWindowName);
  91. launchCalled = true;
  92. return response!;
  93. }
  94. @override
  95. Future<void> closeWebView() async {
  96. closeWebViewCalled = true;
  97. }
  98. }