forked from duckduckgo/iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkProtectionStatusViewModelTests.swift
203 lines (176 loc) · 8.14 KB
/
NetworkProtectionStatusViewModelTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//
// NetworkProtectionStatusViewModelTests.swift
// DuckDuckGo
//
// Copyright © 2023 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import XCTest
import NetworkProtection
import NetworkExtension
import NetworkProtectionTestUtils
@testable import DuckDuckGo
final class NetworkProtectionStatusViewModelTests: XCTestCase {
private var tunnelController: MockTunnelController!
private var statusObserver: MockConnectionStatusObserver!
private var serverInfoObserver: MockConnectionServerInfoObserver!
private var viewModel: NetworkProtectionStatusViewModel!
private var testError: Error {
let nsError = NSError(domain: "", code: 0)
return NEVPNError(_nsError: nsError)
}
override func setUp() {
super.setUp()
tunnelController = MockTunnelController()
statusObserver = MockConnectionStatusObserver()
serverInfoObserver = MockConnectionServerInfoObserver()
viewModel = NetworkProtectionStatusViewModel(
tunnelController: tunnelController,
statusObserver: statusObserver,
serverInfoObserver: serverInfoObserver
)
}
override func tearDown() {
serverInfoObserver = nil
statusObserver = nil
tunnelController = nil
viewModel = nil
super.tearDown()
}
func testStatusUpdate_connected_setsIsNetPEnabledToTrue() throws {
whenStatusUpdate_connected()
}
func testStatusUpdate_notConnected_setsIsNetPEnabledToFalse() throws {
whenStatusUpdate_notConnected()
}
func testDidToggleNetPToTrue_setsTunnelControllerStateToTrue() async {
await viewModel.didToggleNetP(to: true)
XCTAssertEqual(self.tunnelController.didCallStart, true)
}
func testDidToggleNetPToFalse_setsTunnelControllerStateToFalse() async {
await viewModel.didToggleNetP(to: false)
XCTAssertEqual(self.tunnelController.didCallStart, false)
}
func testStatusUpdate_connected_setsHeaderTitleToOn() {
viewModel.headerTitle = ""
whenStatusUpdate_connected()
XCTAssertEqual(self.viewModel.headerTitle, UserText.netPStatusHeaderTitleOn)
}
func testStatusUpdate_notconnected_setsHeaderTitleToOff() {
viewModel.headerTitle = ""
whenStatusUpdate_notConnected()
XCTAssertEqual(self.viewModel.headerTitle, UserText.netPStatusHeaderTitleOff)
}
func testStatusUpdate_connected_setsStatusImageIDToVPN() {
viewModel.statusImageID = ""
whenStatusUpdate_connected()
XCTAssertEqual(self.viewModel.statusImageID, "VPN")
}
func testStatusUpdate_disconnected_setsStatusImageIDToVPNDisabled() {
viewModel.statusImageID = ""
whenStatusUpdate_notConnected()
XCTAssertEqual(self.viewModel.statusImageID, "VPNDisabled")
}
func testStatusUpdate_connected_updatesStatusMessageEverySecond_withTimeLapsed() throws {
statusObserver.subject.send(.connected(connectedDate: Date()))
try waitForPublisher(viewModel.$statusMessage, toEmit: "Connected - 00:00:00")
try waitForPublisher(viewModel.$statusMessage, toEmit: "Connected - 00:00:01")
}
func testStatusUpdate_disconnecting_updateStatusToDisconnecting() throws {
viewModel.isNetPEnabled = true
statusObserver.subject.send(.disconnecting)
try waitForPublisher(viewModel.$statusMessage, toEmit: UserText.netPStatusDisconnecting)
}
func testStatusUpdate_connectingOrReasserting_updateStatusToConnecting() throws {
let connectingStates: [ConnectionStatus] = [.connecting, .reasserting]
for current in connectingStates {
statusObserver.subject.send(current)
try waitForPublisher(viewModel.$statusMessage, toEmit: UserText.netPStatusConnecting)
}
}
func testStatusUpdate_disconnectedOrNotConfigured_updateStatusToDisconnected() throws {
let disconnectedStates: [ConnectionStatus] = [.disconnected, .notConfigured]
// Wait for the initial value first
try waitForPublisher(viewModel.$statusMessage, toEmit: UserText.netPStatusDisconnected)
for current in disconnectedStates {
viewModel.statusMessage = ""
statusObserver.subject.send(current)
try waitForPublisher(viewModel.$statusMessage, toEmit: UserText.netPStatusDisconnected)
}
}
func testStatusUpdate_notLoadingStates_enablesToggle() throws {
let notLoadingStates: [ConnectionStatus] = [.connected(connectedDate: Date()), .disconnected, .notConfigured]
for current in notLoadingStates {
viewModel.shouldDisableToggle = true
statusObserver.subject.send(current)
try waitForPublisher(viewModel.$shouldDisableToggle, toEmit: false)
}
}
func testStatusUpdate_loadingStates_disablesToggle() throws {
let toggleEnabledStates: [ConnectionStatus] = [.disconnecting, .connecting, .reasserting]
for current in toggleEnabledStates {
viewModel.shouldDisableToggle = false
statusObserver.subject.send(current)
try waitForPublisher(viewModel.$shouldDisableToggle, toEmit: true)
}
}
func testStatusUpdate_publishesLocation() throws {
let location = "SomeLocation"
let serverInfo = NetworkProtectionStatusServerInfo(serverLocation: location, serverAddress: nil)
serverInfoObserver.subject.send(serverInfo)
try waitForPublisher(viewModel.$location, toEmit: location)
}
func testStatusUpdate_publishesIPAddress() throws {
let ipAddress = "123.456.789.147"
let serverInfo = NetworkProtectionStatusServerInfo(serverLocation: nil, serverAddress: ipAddress)
serverInfoObserver.subject.send(serverInfo)
try waitForPublisher(viewModel.$ipAddress, toEmit: ipAddress)
}
func testStatusUpdate_nilServerLocationAndServerAddress_hidesConnectionDetails() throws {
let serverInfo = NetworkProtectionStatusServerInfo(serverLocation: nil, serverAddress: nil)
// Wait for initial value first
try waitForPublisher(viewModel.$shouldShowConnectionDetails, toEmit: false)
serverInfoObserver.subject.send(serverInfo)
try waitForPublisher(viewModel.$shouldShowConnectionDetails, toEmit: false)
}
func testStatusUpdate_anyServerInfoPropertiesNonNil_showsConnectionDetails() throws {
for serverInfo in [
NetworkProtectionStatusServerInfo(serverLocation: nil, serverAddress: "123.123.123.123"),
NetworkProtectionStatusServerInfo(serverLocation: "Antartica", serverAddress: nil),
NetworkProtectionStatusServerInfo(serverLocation: "Your Garden", serverAddress: "111.222.333.444")
] {
serverInfoObserver.subject.send(serverInfo)
try waitForPublisher(viewModel.$shouldShowConnectionDetails, toEmit: true)
}
}
// MARK: - Helpers
private func whenStatusUpdate_connected() {
statusObserver.subject.send(.connected(connectedDate: Date()))
waitFor(condition: self.viewModel.isNetPEnabled)
}
private func whenStatusUpdate_notConnected() {
let nonConnectedCases: [ConnectionStatus] = [.disconnected, .disconnecting, .notConfigured, .reasserting]
for current in nonConnectedCases {
statusObserver.subject.send(current)
waitFor(condition: !self.viewModel.isNetPEnabled)
}
}
private func waitFor(condition: @escaping @autoclosure () -> Bool) {
let predicate = NSPredicate { _, _ in
condition()
}
let expectation = XCTNSPredicateExpectation(predicate: predicate, object: nil)
wait(for: [expectation], timeout: 5)
}
}