@@ -7,59 +7,52 @@ import XCTest
7
7
8
8
@testable import url_launcher_ios
9
9
10
+ // Tests whether NSURL parsing is strict. When linking against the iOS 17 SDK or later,
11
+ // NSURL uses a more lenient parser which will not return nil.
12
+ private func urlParsingIsStrict( ) -> Bool {
13
+ return URL ( string: " b a d U R L " ) == nil
14
+ }
15
+
10
16
final class URLLauncherTests : XCTestCase {
11
17
12
- private func createPlugin( ) -> FLTURLLauncherPlugin {
18
+ private func createPlugin( ) -> URLLauncherPlugin {
13
19
let launcher = FakeLauncher ( )
14
- return FLTURLLauncherPlugin ( launcher: launcher)
20
+ return URLLauncherPlugin ( launcher: launcher)
15
21
}
16
22
17
- private func createPlugin( launcher: FakeLauncher ) -> FLTURLLauncherPlugin {
18
- FLTURLLauncherPlugin ( launcher: launcher)
23
+ private func createPlugin( launcher: FakeLauncher ) -> URLLauncherPlugin {
24
+ return URLLauncherPlugin ( launcher: launcher)
19
25
}
20
26
21
27
func testCanLaunchSuccess( ) {
22
- var error : FlutterError ?
23
- let result = createPlugin ( ) . canLaunchURL ( " good://url " , error: & error)
24
-
25
- XCTAssertNotNil ( result)
26
- XCTAssertTrue ( result? . boolValue ?? false )
27
- XCTAssertNil ( error)
28
+ let result = createPlugin ( ) . canLaunchUrl ( url: " good://url " )
29
+ XCTAssertEqual ( result, . success)
28
30
}
29
31
30
32
func testCanLaunchFailure( ) {
31
- var error : FlutterError ?
32
- let result = createPlugin ( ) . canLaunchURL ( " bad://url " , error: & error)
33
-
34
- XCTAssertNotNil ( result)
35
- XCTAssertFalse ( result? . boolValue ?? true )
33
+ let result = createPlugin ( ) . canLaunchUrl ( url: " bad://url " )
34
+ XCTAssertEqual ( result, . failure)
36
35
}
37
36
38
37
func testCanLaunchFailureWithInvalidURL( ) {
39
- var error : FlutterError ?
40
- let result = createPlugin ( ) . canLaunchURL ( " urls can't have spaces " , error: & error)
41
-
42
- if ( error == nil ) {
43
- // When linking against the iOS 17 SDK or later, NSURL uses a lenient parser, and won't
44
- // fail to parse URLs, so the test must allow for either outcome.
45
- XCTAssertNotNil ( result)
46
- XCTAssertFalse ( result? . boolValue ?? true )
47
- XCTAssertNil ( error)
38
+ let result = createPlugin ( ) . canLaunchUrl ( url: " urls can't have spaces " )
39
+
40
+ if urlParsingIsStrict ( ) {
41
+ XCTAssertEqual ( result, . invalidUrl)
48
42
} else {
49
- XCTAssertNil ( result)
50
- XCTAssertNotNil ( error)
51
- XCTAssertEqual ( error? . code, " argument_error " )
52
- XCTAssertEqual ( error? . message, " Unable to parse URL " )
53
- XCTAssertEqual ( error? . details as? String , " Provided URL: urls can't have spaces " )
43
+ XCTAssertEqual ( result, . failure)
54
44
}
55
45
}
56
46
57
47
func testLaunchSuccess( ) {
58
48
let expectation = XCTestExpectation ( description: " completion called " )
59
- createPlugin ( ) . launchURL ( " good://url " , universalLinksOnly: false ) { result, error in
60
- XCTAssertNotNil ( result)
61
- XCTAssertTrue ( result? . boolValue ?? false )
62
- XCTAssertNil ( error)
49
+ createPlugin ( ) . launchUrl ( url: " good://url " , universalLinksOnly: false ) { result in
50
+ switch result {
51
+ case . success( let details) :
52
+ XCTAssertEqual ( details, . success)
53
+ case . failure( let error) :
54
+ XCTFail ( " Unexpected error: \( error) " )
55
+ }
63
56
expectation. fulfill ( )
64
57
}
65
58
@@ -68,11 +61,13 @@ final class URLLauncherTests: XCTestCase {
68
61
69
62
func testLaunchFailure( ) {
70
63
let expectation = XCTestExpectation ( description: " completion called " )
71
-
72
- createPlugin ( ) . launchURL ( " bad://url " , universalLinksOnly: false ) { result, error in
73
- XCTAssertNotNil ( result)
74
- XCTAssertFalse ( result? . boolValue ?? true )
75
- XCTAssertNil ( error)
64
+ createPlugin ( ) . launchUrl ( url: " bad://url " , universalLinksOnly: false ) { result in
65
+ switch result {
66
+ case . success( let details) :
67
+ XCTAssertEqual ( details, . failure)
68
+ case . failure( let error) :
69
+ XCTFail ( " Unexpected error: \( error) " )
70
+ }
76
71
expectation. fulfill ( )
77
72
}
78
73
@@ -81,22 +76,17 @@ final class URLLauncherTests: XCTestCase {
81
76
82
77
func testLaunchFailureWithInvalidURL( ) {
83
78
let expectation = XCTestExpectation ( description: " completion called " )
84
-
85
- createPlugin ( ) . launchURL ( " urls can't have spaces " , universalLinksOnly: false ) { result, error in
86
- if ( error == nil ) {
87
- // When linking against the iOS 17 SDK or later, NSURL uses a lenient parser, and won't
88
- // fail to parse URLs, so the test must allow for either outcome.
89
- XCTAssertNotNil ( result)
90
- XCTAssertFalse ( result? . boolValue ?? true )
91
- XCTAssertNil ( error)
92
- } else {
93
- XCTAssertNil ( result)
94
- XCTAssertNotNil ( error)
95
- XCTAssertEqual ( error? . code, " argument_error " )
96
- XCTAssertEqual ( error? . message, " Unable to parse URL " )
97
- XCTAssertEqual ( error? . details as? String , " Provided URL: urls can't have spaces " )
79
+ createPlugin ( ) . launchUrl ( url: " urls can't have spaces " , universalLinksOnly: false ) { result in
80
+ switch result {
81
+ case . success( let details) :
82
+ if urlParsingIsStrict ( ) {
83
+ XCTAssertEqual ( details, . invalidUrl)
84
+ } else {
85
+ XCTAssertEqual ( details, . failure)
86
+ }
87
+ case . failure( let error) :
88
+ XCTFail ( " Unexpected error: \( error) " )
98
89
}
99
-
100
90
expectation. fulfill ( )
101
91
}
102
92
@@ -108,13 +98,17 @@ final class URLLauncherTests: XCTestCase {
108
98
let plugin = createPlugin ( launcher: launcher)
109
99
110
100
let expectation = XCTestExpectation ( description: " completion called " )
111
- plugin. launchURL ( " good://url " , universalLinksOnly: false ) { result, error in
112
- XCTAssertNil ( error)
101
+ plugin. launchUrl ( url: " good://url " , universalLinksOnly: false ) { result in
102
+ switch result {
103
+ case . success( let details) :
104
+ XCTAssertEqual ( details, . success)
105
+ case . failure( let error) :
106
+ XCTFail ( " Unexpected error: \( error) " )
107
+ }
113
108
expectation. fulfill ( )
114
109
}
115
110
116
111
wait ( for: [ expectation] , timeout: 1 )
117
-
118
112
XCTAssertEqual ( launcher. passedOptions ? [ . universalLinksOnly] as? Bool , false )
119
113
}
120
114
@@ -123,31 +117,35 @@ final class URLLauncherTests: XCTestCase {
123
117
let plugin = createPlugin ( launcher: launcher)
124
118
125
119
let expectation = XCTestExpectation ( description: " completion called " )
126
-
127
- plugin. launchURL ( " good://url " , universalLinksOnly: true ) { result, error in
128
- XCTAssertNil ( error)
120
+ plugin. launchUrl ( url: " good://url " , universalLinksOnly: true ) { result in
121
+ switch result {
122
+ case . success( let details) :
123
+ XCTAssertEqual ( details, . success)
124
+ case . failure( let error) :
125
+ XCTFail ( " Unexpected error: \( error) " )
126
+ }
129
127
expectation. fulfill ( )
130
128
}
131
129
132
130
wait ( for: [ expectation] , timeout: 1 )
133
-
134
131
XCTAssertEqual ( launcher. passedOptions ? [ . universalLinksOnly] as? Bool , true )
135
132
}
136
133
137
134
}
138
135
139
- final private class FakeLauncher : NSObject , FULLauncher {
136
+ final private class FakeLauncher : NSObject , Launcher {
140
137
var passedOptions : [ UIApplication . OpenExternalURLOptionsKey : Any ] ?
141
138
142
- func canOpen ( _ url: URL ) -> Bool {
143
- return url. scheme == " good "
139
+ func canOpenURL ( _ url: URL ) -> Bool {
140
+ url. scheme == " good "
144
141
}
145
142
146
143
func open(
147
- _ url: URL , options: [ UIApplication . OpenExternalURLOptionsKey : Any ] = [ : ] ,
148
- completionHandler: ( ( Bool ) -> Void ) ? = nil
144
+ _ url: URL ,
145
+ options: [ UIApplication . OpenExternalURLOptionsKey : Any ] ,
146
+ completionHandler completion: ( ( Bool ) -> Void ) ?
149
147
) {
150
148
self . passedOptions = options
151
- completionHandler ? ( url. scheme == " good " )
149
+ completion ? ( url. scheme == " good " )
152
150
}
153
151
}
0 commit comments