您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

152 行
4.4 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 Flutter
  5. import XCTest
  6. @testable import url_launcher_ios
  7. // Tests whether NSURL parsing is strict. When linking against the iOS 17 SDK or later,
  8. // NSURL uses a more lenient parser which will not return nil.
  9. private func urlParsingIsStrict() -> Bool {
  10. return URL(string: "b a d U R L") == nil
  11. }
  12. final class URLLauncherTests: XCTestCase {
  13. private func createPlugin() -> URLLauncherPlugin {
  14. let launcher = FakeLauncher()
  15. return URLLauncherPlugin(launcher: launcher)
  16. }
  17. private func createPlugin(launcher: FakeLauncher) -> URLLauncherPlugin {
  18. return URLLauncherPlugin(launcher: launcher)
  19. }
  20. func testCanLaunchSuccess() {
  21. let result = createPlugin().canLaunchUrl(url: "good://url")
  22. XCTAssertEqual(result, .success)
  23. }
  24. func testCanLaunchFailure() {
  25. let result = createPlugin().canLaunchUrl(url: "bad://url")
  26. XCTAssertEqual(result, .failure)
  27. }
  28. func testCanLaunchFailureWithInvalidURL() {
  29. let result = createPlugin().canLaunchUrl(url: "urls can't have spaces")
  30. if urlParsingIsStrict() {
  31. XCTAssertEqual(result, .invalidUrl)
  32. } else {
  33. XCTAssertEqual(result, .failure)
  34. }
  35. }
  36. func testLaunchSuccess() {
  37. let expectation = XCTestExpectation(description: "completion called")
  38. createPlugin().launchUrl(url: "good://url", universalLinksOnly: false) { result in
  39. switch result {
  40. case .success(let details):
  41. XCTAssertEqual(details, .success)
  42. case .failure(let error):
  43. XCTFail("Unexpected error: \(error)")
  44. }
  45. expectation.fulfill()
  46. }
  47. wait(for: [expectation], timeout: 1)
  48. }
  49. func testLaunchFailure() {
  50. let expectation = XCTestExpectation(description: "completion called")
  51. createPlugin().launchUrl(url: "bad://url", universalLinksOnly: false) { result in
  52. switch result {
  53. case .success(let details):
  54. XCTAssertEqual(details, .failure)
  55. case .failure(let error):
  56. XCTFail("Unexpected error: \(error)")
  57. }
  58. expectation.fulfill()
  59. }
  60. wait(for: [expectation], timeout: 1)
  61. }
  62. func testLaunchFailureWithInvalidURL() {
  63. let expectation = XCTestExpectation(description: "completion called")
  64. createPlugin().launchUrl(url: "urls can't have spaces", universalLinksOnly: false) { result in
  65. switch result {
  66. case .success(let details):
  67. if urlParsingIsStrict() {
  68. XCTAssertEqual(details, .invalidUrl)
  69. } else {
  70. XCTAssertEqual(details, .failure)
  71. }
  72. case .failure(let error):
  73. XCTFail("Unexpected error: \(error)")
  74. }
  75. expectation.fulfill()
  76. }
  77. wait(for: [expectation], timeout: 1)
  78. }
  79. func testLaunchWithoutUniversalLinks() {
  80. let launcher = FakeLauncher()
  81. let plugin = createPlugin(launcher: launcher)
  82. let expectation = XCTestExpectation(description: "completion called")
  83. plugin.launchUrl(url: "good://url", universalLinksOnly: false) { result in
  84. switch result {
  85. case .success(let details):
  86. XCTAssertEqual(details, .success)
  87. case .failure(let error):
  88. XCTFail("Unexpected error: \(error)")
  89. }
  90. expectation.fulfill()
  91. }
  92. wait(for: [expectation], timeout: 1)
  93. XCTAssertEqual(launcher.passedOptions?[.universalLinksOnly] as? Bool, false)
  94. }
  95. func testLaunchWithUniversalLinks() {
  96. let launcher = FakeLauncher()
  97. let plugin = createPlugin(launcher: launcher)
  98. let expectation = XCTestExpectation(description: "completion called")
  99. plugin.launchUrl(url: "good://url", universalLinksOnly: true) { result in
  100. switch result {
  101. case .success(let details):
  102. XCTAssertEqual(details, .success)
  103. case .failure(let error):
  104. XCTFail("Unexpected error: \(error)")
  105. }
  106. expectation.fulfill()
  107. }
  108. wait(for: [expectation], timeout: 1)
  109. XCTAssertEqual(launcher.passedOptions?[.universalLinksOnly] as? Bool, true)
  110. }
  111. }
  112. final private class FakeLauncher: NSObject, Launcher {
  113. var passedOptions: [UIApplication.OpenExternalURLOptionsKey: Any]?
  114. func canOpenURL(_ url: URL) -> Bool {
  115. url.scheme == "good"
  116. }
  117. func open(
  118. _ url: URL,
  119. options: [UIApplication.OpenExternalURLOptionsKey: Any],
  120. completionHandler completion: ((Bool) -> Void)?
  121. ) {
  122. self.passedOptions = options
  123. completion?(url.scheme == "good")
  124. }
  125. }