/
TrackingServiceTests.swift
183 lines (155 loc) · 5.47 KB
/
TrackingServiceTests.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
//
// TrackingServiceTests.swift
// RxLocationTests
//
// Created by Vadym Brusko on 11/6/17.
// Copyright © 2017 Vadim Inc. All rights reserved.
//
@testable import RxLocation
import XCTest
import RxTest
import RxSwift
import CoreLocation
class TrackingServiceTests: XCTestCase {
let someLocation = CLLocation(latitude: 1.00000001, longitude: 2.00000002)
var service: TrackingAlgorithm!
var locationService: MockLocationService!
var scheduler: TestScheduler!
override func setUp() {
locationService = MockLocationService()
scheduler = TestScheduler(initialClock: 0)
service = TrackingService(locationService: locationService, timerScheduler: scheduler)
}
func test_track_valueNotUpdate_emitPreviousValueEveryOneMinute() {
// arrange
let observable = scheduler.createHotObservable([
next(0, someLocation)
]).asObservable()
locationService.locationReturnValue = observable
let results = scheduler.createObserver(CLLocation.self)
var subscription: Disposable! = nil
// act
subscription = self.service.track().subscribe(results)
scheduler.scheduleAt(finishTime) { subscription.dispose() }
scheduler.start()
// assert
XCTAssertEqual(results.events, [
next(0, someLocation),
next(60, someLocation),
next(120, someLocation),
next(180, someLocation),
next(240, someLocation),
next(300, someLocation),
next(360, someLocation),
next(420, someLocation),
next(480, someLocation),
next(540, someLocation),
next(600, someLocation)
])
}
var finishTime: Int {
let repeatingTimePeriod = 60
let countOfRepeating = 10
return repeatingTimePeriod*countOfRepeating + repeatingTimePeriod/2
}
func test_track_positionChangedLessThan10Meters_doesNotEmitNewValue() {
// arrange
let location2 = CLLocation(latitude: 10.00000003, longitude: 20.00000005)
let location3 = CLLocation(latitude: 10.00000004, longitude: 20.00000006)
let observable = scheduler.createHotObservable([
next(0, someLocation),
next(10, location2),
next(20, location3)
]).asObservable()
locationService.locationReturnValue = observable
let results = scheduler.createObserver(CLLocation.self)
var subscription: Disposable! = nil
// act
subscription = self.service.track().subscribe(results)
scheduler.scheduleAt(50) { subscription.dispose() }
scheduler.start()
// assert
XCTAssertEqual(results.events, [
next(0, someLocation),
next(10, location2)
])
}
func test_track_positionChangedMoreThan10Meters_emitNewValue() {
// arrange
let location2 = CLLocation(latitude: 10.00000003, longitude: 20.00000005)
let location3 = CLLocation(latitude: 10.00010000, longitude: 20.00010000)
let observable = scheduler.createHotObservable([
next(0, someLocation),
next(10, location2),
next(20, location3)
]).asObservable()
locationService.locationReturnValue = observable
let results = scheduler.createObserver(CLLocation.self)
var subscription: Disposable! = nil
// act
subscription = self.service.track().subscribe(results)
scheduler.scheduleAt(50) { subscription.dispose() }
scheduler.start()
// assert
XCTAssertEqual(results.events, [
next(0, someLocation),
next(10, location2),
next(20, location3)
])
}
func test_track_positionChangedLessThan10Meters_emitPreviousValueAfterOneMinute() {
// arrange
let location2 = CLLocation(latitude: 1.00000003, longitude: 2.00000005)
let location3 = CLLocation(latitude: 1.00000004, longitude: 2.00000006)
let location4 = CLLocation(latitude: 1.00010000, longitude: 2.00010000)
let location5 = CLLocation(latitude: 1.00010001, longitude: 2.00010002)
let observable = scheduler.createHotObservable([
next(0, someLocation),
next(40, location2),
next(80, location3),
next(150, location4),
next(170, location5),
]).asObservable()
locationService.locationReturnValue = observable
let results = scheduler.createObserver(CLLocation.self)
var subscription: Disposable! = nil
// act
subscription = self.service.track().subscribe(results)
scheduler.scheduleAt(220) { subscription.dispose() }
scheduler.start()
// assert
XCTAssertEqual(results.events, [
next(0, someLocation),
next(60, location2),
next(120, location3),
next(150, location4),
next(210, location5),
])
}
func test_track_positionChangedFast_doesNotEmitMoreOftenThanEvery10Seconds() {
let location2 = CLLocation(latitude: 1.00010000, longitude: 2.00010000)
let location3 = CLLocation(latitude: 1.00020000, longitude: 2.00020000)
let location4 = CLLocation(latitude: 1.00030000, longitude: 2.00030000)
let location5 = CLLocation(latitude: 1.00040000, longitude: 2.00040000)
let observable = scheduler.createHotObservable([
next(0, someLocation),
next(3, location2),
next(5, location3),
next(12, location4),
next(17, location5),
]).asObservable()
locationService.locationReturnValue = observable
let results = scheduler.createObserver(CLLocation.self)
var subscription: Disposable! = nil
// act
subscription = self.service.track().subscribe(results)
scheduler.scheduleAt(50) { subscription.dispose() }
scheduler.start()
// assert
XCTAssertEqual(results.events, [
next(0, someLocation),
next(10, location3),
next(20, location5),
])
}
}