This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathGeolocationServiceTests.swift
293 lines (258 loc) · 10.5 KB
/
GeolocationServiceTests.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//
// GeolocationServiceTests.swift
//
// Copyright © 2021 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 Foundation
import XCTest
import Combine
import CoreLocation
@testable import DuckDuckGo_Privacy_Browser
final class GeolocationServiceTests: XCTestCase {
var locationManagerMock: CLLocationManagerMock!
lazy var service: GeolocationService = {
GeolocationService(locationManager: locationManagerMock)
}()
override func setUp() {
CLLocationManagerMock.authStatus = .notDetermined
CLLocationManagerMock.systemLocationServicesEnabled = false
locationManagerMock = CLLocationManagerMock()
}
func testWhenGeolocationServiceInitThenNoLocationIsSet() {
XCTAssertNil(service.currentLocation)
XCTAssertFalse(locationManagerMock.isUpdatingLocation)
XCTAssertFalse(locationManagerMock.isUpdatingHeading)
}
func testWhenAuthorizationStatusChangedThenItIsPublished() {
let e1 = expectation(description: "expect notDetermined")
var e2: XCTestExpectation!
var e3: XCTestExpectation!
let c = service.authorizationStatusPublisher.sink { status in
switch status {
case .notDetermined:
e1.fulfill()
case .authorized:
e2.fulfill()
case .restricted:
e3.fulfill()
default:
XCTFail("unexpected status")
}
}
waitForExpectations(timeout: 0)
e2 = expectation(description: "expect authorized")
CLLocationManagerMock.authStatus = .authorized
e3 = expectation(description: "expect restricted")
CLLocationManagerMock.authStatus = .restricted
withExtendedLifetime(c) {
waitForExpectations(timeout: 1)
}
}
func testWhenLocationServicesEnabledChangesThenAuthorizationStatusIsPublished() {
let e1 = expectation(description: "expect false")
var e2: XCTestExpectation!
var e3: XCTestExpectation!
let c = service.authorizationStatusPublisher.sink { [service] status in
XCTAssertEqual(status, .notDetermined)
switch service.locationServicesEnabled() {
case false:
if e3 == nil {
e1.fulfill()
} else {
e3.fulfill()
}
case true:
e2.fulfill()
}
}
waitForExpectations(timeout: 0)
e2 = expectation(description: "expect true")
CLLocationManagerMock.systemLocationServicesEnabled = true
e3 = expectation(description: "expect false")
CLLocationManagerMock.systemLocationServicesEnabled = false
withExtendedLifetime(c) {
waitForExpectations(timeout: 1)
}
}
func testWhenSubscribedThenLocationIsPublished() {
let loc1 = CLLocation(latitude: 51.1, longitude: 23.4)
let loc2 = CLLocation(latitude: 11.1, longitude: -12.2)
struct TestError: Error {}
let error = TestError()
let e1 = expectation(description: "expect nil")
var e2: XCTestExpectation!
var e3: XCTestExpectation!
var e4: XCTestExpectation!
CLLocationManagerMock.authStatus = .authorized
let c = service.locationPublisher.sink { result in
switch result {
case .none:
e1.fulfill()
case .success(loc1):
e2.fulfill()
case .success(loc2):
e3.fulfill()
case .failure(_ as TestError):
e4.fulfill()
default:
XCTFail("unexpected result")
}
}
waitForExpectations(timeout: 0)
e2 = expectation(description: "expect loc1")
locationManagerMock.currentLocation = loc1
e3 = expectation(description: "expect false")
locationManagerMock.currentLocation = loc2
e4 = expectation(description: "expect error")
locationManagerMock.error = error
withExtendedLifetime(c) {
waitForExpectations(timeout: 1)
}
}
func testWhenManySubscribedThenLocationIsPublished() {
let location = CLLocation(latitude: 51.1, longitude: 23.4)
struct TestError: Error {}
let enil1 = expectation(description: "expect nil 1")
let eloc1 = expectation(description: "expect loc 1")
let eerr1 = expectation(description: "expect err 1")
CLLocationManagerMock.authStatus = .authorized
let c1 = service.locationPublisher.sink { result in
switch result {
case .none:
enil1.fulfill()
case .success(location):
eloc1.fulfill()
case .failure(_ as TestError):
eerr1.fulfill()
default:
XCTFail("Unexpected result")
}
}
let enil2 = expectation(description: "expect nil 2")
let eloc2 = expectation(description: "expect loc 2")
let eerr2 = expectation(description: "expect err 2")
let c2 = service.locationPublisher.sink { result in
switch result {
case .none:
enil2.fulfill()
case .success(location):
eloc2.fulfill()
case .failure(_ as TestError):
eerr2.fulfill()
default:
XCTFail("Unexpected result")
}
}
let enil3 = expectation(description: "expect nil 3")
let eloc3 = expectation(description: "expect loc 3")
let eerr3 = expectation(description: "expect err 3")
let c3 = service.locationPublisher.sink { result in
switch result {
case .none:
enil3.fulfill()
case .success(location):
eloc3.fulfill()
case .failure(_ as TestError):
eerr3.fulfill()
default:
XCTFail("Unexpected result")
}
}
locationManagerMock.currentLocation = location
locationManagerMock.error = TestError()
withExtendedLifetime([c1, c2, c3]) {
waitForExpectations(timeout: 1)
}
}
func testWhenLastSubscriberUnsubscribedThenLocationManagerStopped() {
let eAuthRequested = expectation(description: "Authorization requested")
locationManagerMock.whenInUseAuthorizationRequested = { eAuthRequested.fulfill() }
let c1 = service.locationPublisher.sink { _ in }
_=service.locationPublisher.sink { _ in }
waitForExpectations(timeout: 0)
let c3 = service.locationPublisher.sink { [locationManagerMock] _ in
XCTAssertTrue(locationManagerMock!.isUpdatingLocation)
}
locationManagerMock.currentLocation = CLLocation(latitude: 51.1, longitude: 23.4)
c1.cancel()
c3.cancel()
XCTAssertFalse(locationManagerMock!.isUpdatingLocation)
}
func testWhenLastSubscriberPresentThenLocationIsUpdated() {
let loc1 = CLLocation(latitude: 51.1, longitude: 23.4)
let loc2 = CLLocation(latitude: 11.1, longitude: -12.2)
let eAuthRequested = expectation(description: "Authorization requested")
locationManagerMock.whenInUseAuthorizationRequested = { eAuthRequested.fulfill() }
let c1 = service.locationPublisher.sink { _ in }
var c2: AnyCancellable! = service.locationPublisher.sink { _ in }
let e = expectation(description: "expect still receiving location")
let c3 = service.locationPublisher.sink { [locationManagerMock] result in
XCTAssertTrue(locationManagerMock!.isUpdatingLocation)
if case .success(loc2) = result {
e.fulfill()
}
}
locationManagerMock.currentLocation = loc1
c1.cancel()
c2.cancel()
c2.cancel()
c2 = nil
locationManagerMock.currentLocation = loc2
waitForExpectations(timeout: 1)
c3.cancel()
XCTAssertFalse(locationManagerMock!.isUpdatingLocation)
}
func testWhenResubscribedThenLastLocationIsPublished() {
let location = CLLocation(latitude: 51.1, longitude: 23.4)
locationManagerMock.whenInUseAuthorizationRequested = { }
let c1 = service.locationPublisher.sink { _ in }
locationManagerMock.currentLocation = location
c1.cancel()
let e = expectation(description: "expect location")
_=service.locationPublisher.sink {
guard case .success(location) = $0 else { XCTFail("Unexpected result"); return }
e.fulfill()
}
waitForExpectations(timeout: 0)
}
func testWhenStoppedThenNoErrorIsPublished() {
let location = CLLocation(latitude: 51.1, longitude: 23.4)
locationManagerMock.whenInUseAuthorizationRequested = { }
let c1 = service.locationPublisher.sink { _ in }
locationManagerMock.currentLocation = location
c1.cancel()
struct TestError: Error {}
locationManagerMock.currentLocation = nil
locationManagerMock.error = TestError()
let e = expectation(description: "expect location")
_=service.locationPublisher.sink {
guard case .success(location) = $0 else { XCTFail("Unexpected result"); return }
e.fulfill()
}
waitForExpectations(timeout: 0)
}
func testWhenHighAccuracyRequestedThenAccuracyIsSetToBest() {
let eAuthRequested = expectation(description: "Authorization requested")
locationManagerMock.whenInUseAuthorizationRequested = { eAuthRequested.fulfill() }
let c1 = service.locationPublisher.sink { _ in }
XCTAssertEqual(locationManagerMock.desiredAccuracy, kCLLocationAccuracyHundredMeters)
let c2 = service.highAccuracyPublisher.sink { _ in }
XCTAssertEqual(locationManagerMock.desiredAccuracy, kCLLocationAccuracyBest)
c2.cancel()
XCTAssertEqual(locationManagerMock.desiredAccuracy, kCLLocationAccuracyHundredMeters)
withExtendedLifetime(c1) {}
waitForExpectations(timeout: 0)
}
}