@@ -6,19 +6,149 @@ import FlutterMacOS
6
6
import XCTest
7
7
import url_launcher_macos
8
8
9
+ /// A stub to simulate the system Url handler.
10
+ class StubWorkspace : SystemURLHandler {
11
+
12
+ var isSuccessful = true
13
+
14
+ func open( _ url: URL ) -> Bool {
15
+ return isSuccessful
16
+ }
17
+
18
+ func urlForApplication( toOpen: URL ) -> URL ? {
19
+ return toOpen
20
+ }
21
+ }
22
+
9
23
class RunnerTests : XCTestCase {
10
- func testCanLaunch( ) throws {
24
+
25
+ func testCanLaunchSuccessReturnsTrue( ) throws {
26
+ let expectation = XCTestExpectation ( description: " Check if the URL can be launched " )
11
27
let plugin = UrlLauncherPlugin ( )
28
+
12
29
let call = FlutterMethodCall (
13
30
methodName: " canLaunch " ,
14
31
arguments: [ " url " : " https://flutter.dev " ] )
15
- var canLaunch : Bool ?
32
+
33
+ plugin. handle (
34
+ call,
35
+ result: { ( result: Any ? ) -> Void in
36
+ XCTAssertEqual ( result as? Bool , true )
37
+ expectation. fulfill ( )
38
+ } )
39
+
40
+ wait ( for: [ expectation] , timeout: 10.0 )
41
+ }
42
+
43
+ func testCanLaunchNoAppIsAbleToOpenUrlReturnsFalse( ) throws {
44
+ let expectation = XCTestExpectation ( description: " Check if the URL can be launched " )
45
+ let plugin = UrlLauncherPlugin ( )
46
+
47
+ let call = FlutterMethodCall (
48
+ methodName: " canLaunch " ,
49
+ arguments: [ " url " : " example://flutter.dev " ] )
50
+
51
+ plugin. handle (
52
+ call,
53
+ result: { ( result: Any ? ) -> Void in
54
+ XCTAssertEqual ( result as? Bool , false )
55
+ expectation. fulfill ( )
56
+ } )
57
+
58
+ wait ( for: [ expectation] , timeout: 10.0 )
59
+ }
60
+
61
+ func testCanLaunchInvalidUrlReturnsFalse( ) throws {
62
+ let expectation = XCTestExpectation ( description: " Check if the URL can be launched " )
63
+ let plugin = UrlLauncherPlugin ( )
64
+
65
+ let call = FlutterMethodCall (
66
+ methodName: " canLaunch " ,
67
+ arguments: [ " url " : " brokenUrl " ] )
68
+
69
+ plugin. handle (
70
+ call,
71
+ result: { ( result: Any ? ) -> Void in
72
+ XCTAssertEqual ( result as? Bool , false )
73
+ expectation. fulfill ( )
74
+ } )
75
+
76
+ wait ( for: [ expectation] , timeout: 10.0 )
77
+ }
78
+
79
+ func testCanLaunchMissingArgumentReturnsFlutterError( ) throws {
80
+ let expectation = XCTestExpectation ( description: " Check if the URL can be launched " )
81
+ let plugin = UrlLauncherPlugin ( )
82
+
83
+ let call = FlutterMethodCall (
84
+ methodName: " canLaunch " ,
85
+ arguments: [ ] )
86
+
16
87
plugin. handle (
17
88
call,
18
89
result: { ( result: Any ? ) -> Void in
19
- canLaunch = result as? Bool
90
+ XCTAssertTrue ( result is FlutterError )
91
+ expectation. fulfill ( )
92
+ } )
93
+
94
+ wait ( for: [ expectation] , timeout: 10.0 )
95
+ }
96
+
97
+ func testLaunchSuccessReturnsTrue( ) throws {
98
+ let expectation = XCTestExpectation ( description: " Try to open the URL " )
99
+ let workspace = StubWorkspace ( )
100
+ let pluginWithStubWorkspace = UrlLauncherPlugin ( workspace)
101
+
102
+ let call = FlutterMethodCall (
103
+ methodName: " launch " ,
104
+ arguments: [ " url " : " https://flutter.dev " ] )
105
+
106
+ pluginWithStubWorkspace. handle (
107
+ call,
108
+ result: { ( result: Any ? ) -> Void in
109
+ XCTAssertEqual ( result as? Bool , true )
110
+ expectation. fulfill ( )
111
+ } )
112
+
113
+ wait ( for: [ expectation] , timeout: 10.0 )
114
+ }
115
+
116
+ func testLaunchNoAppIsAbleToOpenUrlReturnsFalse( ) throws {
117
+ let expectation = XCTestExpectation ( description: " Try to open the URL " )
118
+ let workspace = StubWorkspace ( )
119
+ workspace. isSuccessful = false
120
+ let pluginWithStubWorkspace = UrlLauncherPlugin ( workspace)
121
+
122
+ let call = FlutterMethodCall (
123
+ methodName: " launch " ,
124
+ arguments: [ " url " : " schemethatdoesnotexist://flutter.dev " ] )
125
+
126
+ pluginWithStubWorkspace. handle (
127
+ call,
128
+ result: { ( result: Any ? ) -> Void in
129
+ XCTAssertEqual ( result as? Bool , false )
130
+ expectation. fulfill ( )
131
+ } )
132
+
133
+ wait ( for: [ expectation] , timeout: 10.0 )
134
+ }
135
+
136
+ func testLaunchMissingArgumentReturnsFlutterError( ) throws {
137
+ let expectation = XCTestExpectation ( description: " Try to open the URL " )
138
+ let workspace = StubWorkspace ( )
139
+ let pluginWithStubWorkspace = UrlLauncherPlugin ( workspace)
140
+
141
+ let call = FlutterMethodCall (
142
+ methodName: " launch " ,
143
+ arguments: [ ] )
144
+
145
+ pluginWithStubWorkspace. handle (
146
+ call,
147
+ result: { ( result: Any ? ) -> Void in
148
+ XCTAssertTrue ( result is FlutterError )
149
+ expectation. fulfill ( )
20
150
} )
21
151
22
- XCTAssertTrue ( canLaunch == true )
152
+ wait ( for : [ expectation ] , timeout : 10.0 )
23
153
}
24
154
}
0 commit comments