Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

371 righe
13 KiB

  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/services.dart';
  5. import 'package:flutter_test/flutter_test.dart';
  6. import 'package:mockito/annotations.dart';
  7. import 'package:mockito/mockito.dart';
  8. import 'package:url_launcher_ios/src/messages.g.dart';
  9. import 'package:url_launcher_ios/url_launcher_ios.dart';
  10. import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
  11. import 'url_launcher_ios_test.mocks.dart';
  12. // A web URL to use in tests where the specifics of the URL don't matter.
  13. const String _webUrl = 'https://example.com/';
  14. @GenerateMocks(<Type>[UrlLauncherApi])
  15. void main() {
  16. late MockUrlLauncherApi api;
  17. setUp(() {
  18. api = MockUrlLauncherApi();
  19. });
  20. test('registers instance', () {
  21. UrlLauncherIOS.registerWith();
  22. expect(UrlLauncherPlatform.instance, isA<UrlLauncherIOS>());
  23. });
  24. group('canLaunch', () {
  25. test('handles success', () async {
  26. when(api.canLaunchUrl(_webUrl))
  27. .thenAnswer((_) async => LaunchResult.success);
  28. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  29. expect(await launcher.canLaunch(_webUrl), true);
  30. });
  31. test('handles failure', () async {
  32. when(api.canLaunchUrl(_webUrl))
  33. .thenAnswer((_) async => LaunchResult.failure);
  34. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  35. expect(await launcher.canLaunch(_webUrl), false);
  36. });
  37. test('throws PlatformException for invalid URL', () async {
  38. when(api.canLaunchUrl(_webUrl))
  39. .thenAnswer((_) async => LaunchResult.invalidUrl);
  40. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  41. await expectLater(
  42. launcher.canLaunch(_webUrl),
  43. throwsA(isA<PlatformException>().having(
  44. (PlatformException e) => e.code, 'code', 'argument_error')));
  45. });
  46. });
  47. group('legacy launch', () {
  48. test('handles success', () async {
  49. when(api.launchUrl(_webUrl, any))
  50. .thenAnswer((_) async => LaunchResult.success);
  51. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  52. expect(
  53. await launcher.launch(
  54. _webUrl,
  55. useSafariVC: false,
  56. useWebView: false,
  57. enableJavaScript: false,
  58. enableDomStorage: false,
  59. universalLinksOnly: false,
  60. headers: const <String, String>{},
  61. ),
  62. true);
  63. verifyNever(api.openUrlInSafariViewController(any));
  64. });
  65. test('handles failure', () async {
  66. when(api.launchUrl(_webUrl, any))
  67. .thenAnswer((_) async => LaunchResult.failure);
  68. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  69. expect(
  70. await launcher.launch(
  71. _webUrl,
  72. useSafariVC: false,
  73. useWebView: false,
  74. enableJavaScript: false,
  75. enableDomStorage: false,
  76. universalLinksOnly: false,
  77. headers: const <String, String>{},
  78. ),
  79. false);
  80. verifyNever(api.openUrlInSafariViewController(any));
  81. });
  82. test('throws PlatformException for invalid URL', () async {
  83. when(api.launchUrl(_webUrl, any))
  84. .thenAnswer((_) async => LaunchResult.invalidUrl);
  85. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  86. await expectLater(
  87. launcher.launch(
  88. _webUrl,
  89. useSafariVC: false,
  90. useWebView: false,
  91. enableJavaScript: false,
  92. enableDomStorage: false,
  93. universalLinksOnly: false,
  94. headers: const <String, String>{},
  95. ),
  96. throwsA(isA<PlatformException>().having(
  97. (PlatformException e) => e.code, 'code', 'argument_error')));
  98. });
  99. test('force SafariVC is handled', () async {
  100. when(api.openUrlInSafariViewController(_webUrl))
  101. .thenAnswer((_) async => InAppLoadResult.success);
  102. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  103. expect(
  104. await launcher.launch(
  105. _webUrl,
  106. useSafariVC: true,
  107. useWebView: false,
  108. enableJavaScript: false,
  109. enableDomStorage: false,
  110. universalLinksOnly: false,
  111. headers: const <String, String>{},
  112. ),
  113. true);
  114. verifyNever(api.launchUrl(any, any));
  115. });
  116. test('universal links only is handled', () async {
  117. when(api.launchUrl(_webUrl, any))
  118. .thenAnswer((_) async => LaunchResult.success);
  119. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  120. expect(
  121. await launcher.launch(
  122. _webUrl,
  123. useSafariVC: false,
  124. useWebView: false,
  125. enableJavaScript: false,
  126. enableDomStorage: false,
  127. universalLinksOnly: true,
  128. headers: const <String, String>{},
  129. ),
  130. true);
  131. verifyNever(api.openUrlInSafariViewController(any));
  132. });
  133. test('disallowing SafariVC is handled', () async {
  134. when(api.launchUrl(_webUrl, any))
  135. .thenAnswer((_) async => LaunchResult.success);
  136. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  137. expect(
  138. await launcher.launch(
  139. _webUrl,
  140. useSafariVC: false,
  141. useWebView: false,
  142. enableJavaScript: false,
  143. enableDomStorage: false,
  144. universalLinksOnly: false,
  145. headers: const <String, String>{},
  146. ),
  147. true);
  148. verifyNever(api.openUrlInSafariViewController(any));
  149. });
  150. });
  151. test('closeWebView calls through', () async {
  152. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  153. await launcher.closeWebView();
  154. verify(api.closeSafariViewController()).called(1);
  155. });
  156. group('launch without webview', () {
  157. test('calls through', () async {
  158. when(api.launchUrl(_webUrl, any))
  159. .thenAnswer((_) async => LaunchResult.success);
  160. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  161. final bool launched = await launcher.launchUrl(
  162. _webUrl,
  163. const LaunchOptions(mode: PreferredLaunchMode.externalApplication),
  164. );
  165. expect(launched, true);
  166. verifyNever(api.openUrlInSafariViewController(any));
  167. });
  168. test('throws PlatformException for invalid URL', () async {
  169. when(api.launchUrl(_webUrl, any))
  170. .thenAnswer((_) async => LaunchResult.invalidUrl);
  171. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  172. await expectLater(
  173. launcher.launchUrl(
  174. _webUrl,
  175. const LaunchOptions(mode: PreferredLaunchMode.externalApplication),
  176. ),
  177. throwsA(isA<PlatformException>().having(
  178. (PlatformException e) => e.code, 'code', 'argument_error')));
  179. });
  180. });
  181. group('launch with Safari view controller', () {
  182. test('calls through with inAppWebView', () async {
  183. when(api.openUrlInSafariViewController(_webUrl))
  184. .thenAnswer((_) async => InAppLoadResult.success);
  185. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  186. final bool launched = await launcher.launchUrl(
  187. _webUrl, const LaunchOptions(mode: PreferredLaunchMode.inAppWebView));
  188. expect(launched, true);
  189. verifyNever(api.launchUrl(any, any));
  190. });
  191. test('calls through with inAppBrowserView', () async {
  192. when(api.openUrlInSafariViewController(_webUrl))
  193. .thenAnswer((_) async => InAppLoadResult.success);
  194. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  195. final bool launched = await launcher.launchUrl(_webUrl,
  196. const LaunchOptions(mode: PreferredLaunchMode.inAppBrowserView));
  197. expect(launched, true);
  198. verifyNever(api.launchUrl(any, any));
  199. });
  200. test('throws PlatformException for invalid URL', () async {
  201. when(api.openUrlInSafariViewController(_webUrl))
  202. .thenAnswer((_) async => InAppLoadResult.invalidUrl);
  203. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  204. await expectLater(
  205. launcher.launchUrl(_webUrl,
  206. const LaunchOptions(mode: PreferredLaunchMode.inAppWebView)),
  207. throwsA(isA<PlatformException>().having(
  208. (PlatformException e) => e.code, 'code', 'argument_error')));
  209. });
  210. test('throws PlatformException for load failure', () async {
  211. when(api.openUrlInSafariViewController(_webUrl))
  212. .thenAnswer((_) async => InAppLoadResult.failedToLoad);
  213. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  214. await expectLater(
  215. launcher.launchUrl(_webUrl,
  216. const LaunchOptions(mode: PreferredLaunchMode.inAppWebView)),
  217. throwsA(isA<PlatformException>()
  218. .having((PlatformException e) => e.code, 'code', 'Error')));
  219. });
  220. });
  221. group('launch with universal links', () {
  222. test('calls through', () async {
  223. when(api.launchUrl(_webUrl, any))
  224. .thenAnswer((_) async => LaunchResult.success);
  225. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  226. final bool launched = await launcher.launchUrl(
  227. _webUrl,
  228. const LaunchOptions(
  229. mode: PreferredLaunchMode.externalNonBrowserApplication),
  230. );
  231. expect(launched, true);
  232. verifyNever(api.openUrlInSafariViewController(any));
  233. });
  234. test('throws PlatformException for invalid URL', () async {
  235. when(api.launchUrl(_webUrl, any))
  236. .thenAnswer((_) async => LaunchResult.invalidUrl);
  237. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  238. await expectLater(
  239. launcher.launchUrl(
  240. _webUrl,
  241. const LaunchOptions(
  242. mode: PreferredLaunchMode.externalNonBrowserApplication)),
  243. throwsA(isA<PlatformException>().having(
  244. (PlatformException e) => e.code, 'code', 'argument_error')));
  245. });
  246. });
  247. group('launch with platform default', () {
  248. test('uses Safari view controller for http', () async {
  249. const String httpUrl = 'http://example.com/';
  250. when(api.openUrlInSafariViewController(httpUrl))
  251. .thenAnswer((_) async => InAppLoadResult.success);
  252. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  253. final bool launched =
  254. await launcher.launchUrl(httpUrl, const LaunchOptions());
  255. expect(launched, true);
  256. verifyNever(api.launchUrl(any, any));
  257. });
  258. test('uses Safari view controller for https', () async {
  259. const String httpsUrl = 'https://example.com/';
  260. when(api.openUrlInSafariViewController(httpsUrl))
  261. .thenAnswer((_) async => InAppLoadResult.success);
  262. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  263. final bool launched =
  264. await launcher.launchUrl(httpsUrl, const LaunchOptions());
  265. expect(launched, true);
  266. verifyNever(api.launchUrl(any, any));
  267. });
  268. test('uses standard external for other schemes', () async {
  269. const String nonWebUrl = 'supportedcustomscheme://example.com/';
  270. when(api.launchUrl(nonWebUrl, any))
  271. .thenAnswer((_) async => LaunchResult.success);
  272. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  273. final bool launched =
  274. await launcher.launchUrl(nonWebUrl, const LaunchOptions());
  275. expect(launched, true);
  276. verifyNever(api.openUrlInSafariViewController(any));
  277. });
  278. });
  279. group('supportsMode', () {
  280. test('returns true for platformDefault', () async {
  281. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  282. expect(await launcher.supportsMode(PreferredLaunchMode.platformDefault),
  283. true);
  284. });
  285. test('returns true for external application', () async {
  286. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  287. expect(
  288. await launcher.supportsMode(PreferredLaunchMode.externalApplication),
  289. true);
  290. });
  291. test('returns true for external non-browser application', () async {
  292. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  293. expect(
  294. await launcher
  295. .supportsMode(PreferredLaunchMode.externalNonBrowserApplication),
  296. true);
  297. });
  298. test('returns true for in app web view', () async {
  299. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  300. expect(
  301. await launcher.supportsMode(PreferredLaunchMode.inAppWebView), true);
  302. });
  303. test('returns true for in app browser view', () async {
  304. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  305. expect(await launcher.supportsMode(PreferredLaunchMode.inAppBrowserView),
  306. true);
  307. });
  308. });
  309. group('supportsCloseForMode', () {
  310. test('returns true for in app web view', () async {
  311. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  312. expect(
  313. await launcher.supportsCloseForMode(PreferredLaunchMode.inAppWebView),
  314. true);
  315. });
  316. test('returns true for in app browser view', () async {
  317. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  318. expect(
  319. await launcher
  320. .supportsCloseForMode(PreferredLaunchMode.inAppBrowserView),
  321. true);
  322. });
  323. test('returns false for other modes', () async {
  324. final UrlLauncherIOS launcher = UrlLauncherIOS(api: api);
  325. expect(
  326. await launcher
  327. .supportsCloseForMode(PreferredLaunchMode.externalApplication),
  328. false);
  329. expect(
  330. await launcher.supportsCloseForMode(
  331. PreferredLaunchMode.externalNonBrowserApplication),
  332. false);
  333. });
  334. });
  335. }