|
| 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 | + |
| 5 | +import 'package:flutter/services.dart'; |
| 6 | +import 'package:flutter_test/flutter_test.dart'; |
| 7 | +import 'package:url_launcher_android/url_launcher_android.dart'; |
| 8 | +import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart'; |
| 9 | + |
| 10 | +void main() { |
| 11 | + TestWidgetsFlutterBinding.ensureInitialized(); |
| 12 | + |
| 13 | + group('$UrlLauncherAndroid', () { |
| 14 | + const MethodChannel channel = |
| 15 | + MethodChannel('plugins.flutter.io/url_launcher_android'); |
| 16 | + final List<MethodCall> log = <MethodCall>[]; |
| 17 | + channel.setMockMethodCallHandler((MethodCall methodCall) async { |
| 18 | + log.add(methodCall); |
| 19 | + |
| 20 | + // Return null explicitly instead of relying on the implicit null |
| 21 | + // returned by the method channel if no return statement is specified. |
| 22 | + return null; |
| 23 | + }); |
| 24 | + |
| 25 | + tearDown(() { |
| 26 | + log.clear(); |
| 27 | + }); |
| 28 | + |
| 29 | + test('registers instance', () { |
| 30 | + UrlLauncherAndroid.registerWith(); |
| 31 | + expect(UrlLauncherPlatform.instance, isA<UrlLauncherAndroid>()); |
| 32 | + }); |
| 33 | + |
| 34 | + test('canLaunch', () async { |
| 35 | + final UrlLauncherAndroid launcher = UrlLauncherAndroid(); |
| 36 | + await launcher.canLaunch('http://example.com/'); |
| 37 | + expect( |
| 38 | + log, |
| 39 | + <Matcher>[ |
| 40 | + isMethodCall('canLaunch', arguments: <String, Object>{ |
| 41 | + 'url': 'http://example.com/', |
| 42 | + }) |
| 43 | + ], |
| 44 | + ); |
| 45 | + }); |
| 46 | + |
| 47 | + test('canLaunch should return false if platform returns null', () async { |
| 48 | + final UrlLauncherAndroid launcher = UrlLauncherAndroid(); |
| 49 | + final bool canLaunch = await launcher.canLaunch('http://example.com/'); |
| 50 | + |
| 51 | + expect(canLaunch, false); |
| 52 | + }); |
| 53 | + |
| 54 | + test('launch', () async { |
| 55 | + final UrlLauncherAndroid launcher = UrlLauncherAndroid(); |
| 56 | + await launcher.launch( |
| 57 | + 'http://example.com/', |
| 58 | + useSafariVC: true, |
| 59 | + useWebView: false, |
| 60 | + enableJavaScript: false, |
| 61 | + enableDomStorage: false, |
| 62 | + universalLinksOnly: false, |
| 63 | + headers: const <String, String>{}, |
| 64 | + ); |
| 65 | + expect( |
| 66 | + log, |
| 67 | + <Matcher>[ |
| 68 | + isMethodCall('launch', arguments: <String, Object>{ |
| 69 | + 'url': 'http://example.com/', |
| 70 | + 'useWebView': false, |
| 71 | + 'enableJavaScript': false, |
| 72 | + 'enableDomStorage': false, |
| 73 | + 'universalLinksOnly': false, |
| 74 | + 'headers': <String, String>{}, |
| 75 | + }) |
| 76 | + ], |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | + test('launch with headers', () async { |
| 81 | + final UrlLauncherAndroid launcher = UrlLauncherAndroid(); |
| 82 | + await launcher.launch( |
| 83 | + 'http://example.com/', |
| 84 | + useSafariVC: true, |
| 85 | + useWebView: false, |
| 86 | + enableJavaScript: false, |
| 87 | + enableDomStorage: false, |
| 88 | + universalLinksOnly: false, |
| 89 | + headers: const <String, String>{'key': 'value'}, |
| 90 | + ); |
| 91 | + expect( |
| 92 | + log, |
| 93 | + <Matcher>[ |
| 94 | + isMethodCall('launch', arguments: <String, Object>{ |
| 95 | + 'url': 'http://example.com/', |
| 96 | + 'useWebView': false, |
| 97 | + 'enableJavaScript': false, |
| 98 | + 'enableDomStorage': false, |
| 99 | + 'universalLinksOnly': false, |
| 100 | + 'headers': <String, String>{'key': 'value'}, |
| 101 | + }) |
| 102 | + ], |
| 103 | + ); |
| 104 | + }); |
| 105 | + |
| 106 | + test('launch universal links only', () async { |
| 107 | + final UrlLauncherAndroid launcher = UrlLauncherAndroid(); |
| 108 | + await launcher.launch( |
| 109 | + 'http://example.com/', |
| 110 | + useSafariVC: false, |
| 111 | + useWebView: false, |
| 112 | + enableJavaScript: false, |
| 113 | + enableDomStorage: false, |
| 114 | + universalLinksOnly: true, |
| 115 | + headers: const <String, String>{}, |
| 116 | + ); |
| 117 | + expect( |
| 118 | + log, |
| 119 | + <Matcher>[ |
| 120 | + isMethodCall('launch', arguments: <String, Object>{ |
| 121 | + 'url': 'http://example.com/', |
| 122 | + 'useWebView': false, |
| 123 | + 'enableJavaScript': false, |
| 124 | + 'enableDomStorage': false, |
| 125 | + 'universalLinksOnly': true, |
| 126 | + 'headers': <String, String>{}, |
| 127 | + }) |
| 128 | + ], |
| 129 | + ); |
| 130 | + }); |
| 131 | + |
| 132 | + test('launch force WebView', () async { |
| 133 | + final UrlLauncherAndroid launcher = UrlLauncherAndroid(); |
| 134 | + await launcher.launch( |
| 135 | + 'http://example.com/', |
| 136 | + useSafariVC: true, |
| 137 | + useWebView: true, |
| 138 | + enableJavaScript: false, |
| 139 | + enableDomStorage: false, |
| 140 | + universalLinksOnly: false, |
| 141 | + headers: const <String, String>{}, |
| 142 | + ); |
| 143 | + expect( |
| 144 | + log, |
| 145 | + <Matcher>[ |
| 146 | + isMethodCall('launch', arguments: <String, Object>{ |
| 147 | + 'url': 'http://example.com/', |
| 148 | + 'useWebView': true, |
| 149 | + 'enableJavaScript': false, |
| 150 | + 'enableDomStorage': false, |
| 151 | + 'universalLinksOnly': false, |
| 152 | + 'headers': <String, String>{}, |
| 153 | + }) |
| 154 | + ], |
| 155 | + ); |
| 156 | + }); |
| 157 | + |
| 158 | + test('launch force WebView enable javascript', () async { |
| 159 | + final UrlLauncherAndroid launcher = UrlLauncherAndroid(); |
| 160 | + await launcher.launch( |
| 161 | + 'http://example.com/', |
| 162 | + useSafariVC: true, |
| 163 | + useWebView: true, |
| 164 | + enableJavaScript: true, |
| 165 | + enableDomStorage: false, |
| 166 | + universalLinksOnly: false, |
| 167 | + headers: const <String, String>{}, |
| 168 | + ); |
| 169 | + expect( |
| 170 | + log, |
| 171 | + <Matcher>[ |
| 172 | + isMethodCall('launch', arguments: <String, Object>{ |
| 173 | + 'url': 'http://example.com/', |
| 174 | + 'useWebView': true, |
| 175 | + 'enableJavaScript': true, |
| 176 | + 'enableDomStorage': false, |
| 177 | + 'universalLinksOnly': false, |
| 178 | + 'headers': <String, String>{}, |
| 179 | + }) |
| 180 | + ], |
| 181 | + ); |
| 182 | + }); |
| 183 | + |
| 184 | + test('launch force WebView enable DOM storage', () async { |
| 185 | + final UrlLauncherAndroid launcher = UrlLauncherAndroid(); |
| 186 | + await launcher.launch( |
| 187 | + 'http://example.com/', |
| 188 | + useSafariVC: true, |
| 189 | + useWebView: true, |
| 190 | + enableJavaScript: false, |
| 191 | + enableDomStorage: true, |
| 192 | + universalLinksOnly: false, |
| 193 | + headers: const <String, String>{}, |
| 194 | + ); |
| 195 | + expect( |
| 196 | + log, |
| 197 | + <Matcher>[ |
| 198 | + isMethodCall('launch', arguments: <String, Object>{ |
| 199 | + 'url': 'http://example.com/', |
| 200 | + 'useWebView': true, |
| 201 | + 'enableJavaScript': false, |
| 202 | + 'enableDomStorage': true, |
| 203 | + 'universalLinksOnly': false, |
| 204 | + 'headers': <String, String>{}, |
| 205 | + }) |
| 206 | + ], |
| 207 | + ); |
| 208 | + }); |
| 209 | + |
| 210 | + test('launch should return false if platform returns null', () async { |
| 211 | + final UrlLauncherAndroid launcher = UrlLauncherAndroid(); |
| 212 | + final bool launched = await launcher.launch( |
| 213 | + 'http://example.com/', |
| 214 | + useSafariVC: true, |
| 215 | + useWebView: false, |
| 216 | + enableJavaScript: false, |
| 217 | + enableDomStorage: false, |
| 218 | + universalLinksOnly: false, |
| 219 | + headers: const <String, String>{}, |
| 220 | + ); |
| 221 | + |
| 222 | + expect(launched, false); |
| 223 | + }); |
| 224 | + |
| 225 | + test('closeWebView default behavior', () async { |
| 226 | + final UrlLauncherAndroid launcher = UrlLauncherAndroid(); |
| 227 | + await launcher.closeWebView(); |
| 228 | + expect( |
| 229 | + log, |
| 230 | + <Matcher>[isMethodCall('closeWebView', arguments: null)], |
| 231 | + ); |
| 232 | + }); |
| 233 | + }); |
| 234 | +} |
0 commit comments