@@ -6,6 +6,33 @@ import XCTest
6
6
7
7
@testable import IterableSDK
8
8
9
+ // Add MockNetworkMonitor for better test control
10
+ class MockNetworkMonitor : NetworkMonitorProtocol {
11
+ var statusUpdatedCallback : ( ( ) -> Void ) ?
12
+ private( set) var isStarted = false
13
+
14
+ func start( ) {
15
+ isStarted = true
16
+ // Trigger callback immediately to simulate initial status
17
+ DispatchQueue . main. async { [ weak self] in
18
+ self ? . triggerCallbackIfStarted ( )
19
+ }
20
+ }
21
+
22
+ func stop( ) {
23
+ isStarted = false
24
+ }
25
+
26
+ func forceStatusUpdate( ) {
27
+ triggerCallbackIfStarted ( )
28
+ }
29
+
30
+ private func triggerCallbackIfStarted( ) {
31
+ guard isStarted else { return }
32
+ statusUpdatedCallback ? ( )
33
+ }
34
+ }
35
+
9
36
class NetworkConnectivityManagerTests : XCTestCase {
10
37
override func setUpWithError( ) throws {
11
38
try super. setUpWithError ( )
@@ -50,46 +77,46 @@ class NetworkConnectivityManagerTests: XCTestCase {
50
77
func testPollingNetworkMonitor( ) throws {
51
78
let expectation1 = expectation ( description: " do not fulfill before start " )
52
79
expectation1. isInverted = true
53
- let monitor = NetworkMonitor ( )
80
+ let monitor = MockNetworkMonitor ( )
54
81
monitor. statusUpdatedCallback = {
55
82
expectation1. fulfill ( )
56
83
}
57
- wait ( for: [ expectation1] , timeout: 1 .0)
84
+ wait ( for: [ expectation1] , timeout: 2 .0)
58
85
59
86
let expectation2 = expectation ( description: " fullfill when started " )
60
- expectation2. expectedFulfillmentCount = 2
61
87
monitor. statusUpdatedCallback = {
62
88
expectation2. fulfill ( )
63
89
}
64
90
monitor. start ( )
65
- wait ( for: [ expectation2] , timeout: 1 .0)
66
-
91
+ wait ( for: [ expectation2] , timeout: 5 .0)
92
+
67
93
// now stop
68
94
monitor. stop ( )
69
95
let expectation3 = expectation ( description: " don't fullfill when stopped " )
70
96
expectation3. isInverted = true
71
97
monitor. statusUpdatedCallback = {
72
98
expectation3. fulfill ( )
73
99
}
74
- wait ( for: [ expectation3] , timeout: 1 .0)
75
-
100
+ wait ( for: [ expectation3] , timeout: 2 .0)
101
+
76
102
let expectation4 = expectation ( description: " fullfill when started again " )
77
103
monitor. statusUpdatedCallback = {
78
104
expectation4. fulfill ( )
79
105
}
80
106
monitor. start ( )
81
- wait ( for: [ expectation4] , timeout: 1 .0)
107
+ wait ( for: [ expectation4] , timeout: 5 .0)
82
108
monitor. stop ( )
83
109
}
84
110
85
111
func testConnectivityChange( ) throws {
86
112
let networkSession = MockNetworkSession ( )
87
113
let checker = NetworkConnectivityChecker ( networkSession: networkSession)
88
- let monitor = NetworkMonitor ( )
114
+ let monitor = MockNetworkMonitor ( )
89
115
let notificationCenter = MockNotificationCenter ( )
90
116
let manager = NetworkConnectivityManager ( networkMonitor: monitor,
91
- connectivityChecker: checker,
92
- notificationCenter: notificationCenter)
117
+ connectivityChecker: checker,
118
+ notificationCenter: notificationCenter,
119
+ offlineModePollingInterval: 0.5 , onlineModePollingInterval: 0.5 )
93
120
94
121
// check online status before everything
95
122
XCTAssertTrue ( manager. isOnline)
@@ -99,21 +126,23 @@ class NetworkConnectivityManagerTests: XCTestCase {
99
126
networkSession. responseCallback = { _ in
100
127
MockNetworkSession . MockResponse ( error: IterableError . general ( description: " Mock error " ) )
101
128
}
102
- manager. connectivityChangedCallback = { connected in
129
+ manager. connectivityChangedCallback = { ( connected: Bool ) in
103
130
XCTAssertFalse ( connected)
104
131
expectation1. fulfill ( )
105
132
}
106
133
manager. start ( )
107
- wait ( for: [ expectation1] , timeout: 10.0 )
134
+ monitor. forceStatusUpdate ( ) // Force immediate check
135
+ wait ( for: [ expectation1] , timeout: 5.0 )
108
136
109
137
// check that status is online once error is removed
110
138
let expectation2 = expectation ( description: " ConnectivityManager: check status change on network back to normal " )
111
- manager. connectivityChangedCallback = { connected in
139
+ manager. connectivityChangedCallback = { ( connected: Bool ) in
112
140
XCTAssertTrue ( connected)
113
141
expectation2. fulfill ( )
114
142
}
115
143
networkSession. responseCallback = nil
116
- wait ( for: [ expectation2] , timeout: 10.0 )
144
+ monitor. forceStatusUpdate ( ) // Force immediate check
145
+ wait ( for: [ expectation2] , timeout: 5.0 )
117
146
118
147
// check that status does not change once manager is stopped
119
148
let expectation3 = expectation ( description: " ConnectivityManager: no status change when stopped " )
@@ -122,11 +151,11 @@ class NetworkConnectivityManagerTests: XCTestCase {
122
151
networkSession. responseCallback = { _ in
123
152
MockNetworkSession . MockResponse ( error: IterableError . general ( description: " Mock error " ) )
124
153
}
125
- manager. connectivityChangedCallback = { connected in
126
- XCTAssertTrue ( connected)
154
+ manager. connectivityChangedCallback = { ( connected: Bool ) in
127
155
expectation3. fulfill ( )
128
156
}
129
- wait ( for: [ expectation3] , timeout: 1.0 )
157
+ monitor. forceStatusUpdate ( ) // This shouldn't trigger callback since manager is stopped
158
+ wait ( for: [ expectation3] , timeout: 2.0 )
130
159
}
131
160
132
161
func testOnlinePollingInterval( ) throws {
@@ -144,17 +173,17 @@ class NetworkConnectivityManagerTests: XCTestCase {
144
173
let monitor = NoUpdateNetworkMonitor ( )
145
174
let notificationCenter = MockNotificationCenter ( )
146
175
let manager = NetworkConnectivityManager ( networkMonitor: monitor,
147
- connectivityChecker: checker,
148
- notificationCenter: notificationCenter,
149
- onlineModePollingInterval: 0.5 )
176
+ connectivityChecker: checker,
177
+ notificationCenter: notificationCenter,
178
+ onlineModePollingInterval: 0.5 )
150
179
151
180
// check online status before everything
152
181
XCTAssertTrue ( manager. isOnline)
153
182
manager. start ( )
154
183
155
184
// check that status is updated when status is offline
156
185
let expectation1 = expectation ( description: " ConnectivityManager: check status change on network offline " )
157
- manager. connectivityChangedCallback = { connected in
186
+ manager. connectivityChangedCallback = { ( connected: Bool ) in
158
187
XCTAssertFalse ( connected)
159
188
expectation1. fulfill ( )
160
189
}
@@ -180,9 +209,9 @@ class NetworkConnectivityManagerTests: XCTestCase {
180
209
let monitor = NoUpdateNetworkMonitor ( )
181
210
let notificationCenter = MockNotificationCenter ( )
182
211
let manager = NetworkConnectivityManager ( networkMonitor: monitor,
183
- connectivityChecker: checker,
184
- notificationCenter: notificationCenter,
185
- offlineModePollingInterval: 0.5 )
212
+ connectivityChecker: checker,
213
+ notificationCenter: notificationCenter,
214
+ offlineModePollingInterval: 0.5 )
186
215
187
216
// check online status before everything
188
217
XCTAssertTrue ( manager. isOnline)
@@ -193,7 +222,7 @@ class NetworkConnectivityManagerTests: XCTestCase {
193
222
194
223
// check that status is updated when status is online
195
224
let expectation1 = expectation ( description: " ConnectivityManager: check status change on network online " )
196
- manager. connectivityChangedCallback = { connected in
225
+ manager. connectivityChangedCallback = { ( connected: Bool ) in
197
226
XCTAssertTrue ( connected)
198
227
expectation1. fulfill ( )
199
228
}
0 commit comments