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