@@ -19,14 +19,18 @@ @interface GeolocationHandlerTests : XCTestCase
19
19
20
20
@implementation GeolocationHandlerTests {
21
21
CLLocationManager *_mockLocationManager;
22
+ CLLocationManager *_mockOneTimeLocationManager;
23
+
22
24
GeolocationHandler *_geolocationHandler;
23
25
}
24
26
25
27
- (void )setUp {
26
28
_mockLocationManager = OCMClassMock (CLLocationManager.class );
29
+ _mockOneTimeLocationManager = OCMClassMock (CLLocationManager.class );
27
30
28
31
_geolocationHandler = [[GeolocationHandler alloc ] init ];
29
32
[_geolocationHandler setLocationManagerOverride: _mockLocationManager];
33
+ [_geolocationHandler setOneTimeLocationManagerOverride: _mockOneTimeLocationManager];
30
34
}
31
35
32
36
#pragma mark - Test requesting current location
@@ -36,25 +40,25 @@ - (void)testGetCurrentPositionShouldCallStartUpdatingLocation {
36
40
resultHandler: ^(CLLocation * _Nullable location) {}
37
41
errorHandler: ^(NSString * _Nonnull errorCode, NSString * _Nonnull errorDescription) {}];
38
42
39
- OCMVerify (times (1 ), [self ->_mockLocationManager setDesiredAccuracy: kCLLocationAccuracyBest ]);
40
- OCMVerify (times (1 ), [self ->_mockLocationManager startUpdatingLocation ]);
43
+ OCMVerify (times (1 ), [self ->_mockOneTimeLocationManager setDesiredAccuracy: kCLLocationAccuracyBest ]);
44
+ OCMVerify (times (1 ), [self ->_mockOneTimeLocationManager startUpdatingLocation ]);
41
45
}
42
46
43
47
- (void )testRequestPositionShouldReturnLocationWithinTimeConstraints {
44
48
NSDate *now = [NSDate date ];
45
49
NSCalendar *calendar = [NSCalendar currentCalendar ];
46
-
50
+
47
51
CLLocation *firstLocation = [[CLLocation alloc ] initWithCoordinate: CLLocationCoordinate2DMake (54.0 , 6.4 )
48
52
altitude: 0.0
49
53
horizontalAccuracy: 0
50
54
verticalAccuracy: 0
51
55
timestamp: [calendar dateByAddingUnit: NSCalendarUnitSecond value: -6 toDate: now options: 0 ]];
52
56
CLLocation *secondLocation = [[CLLocation alloc ] initWithCoordinate: CLLocationCoordinate2DMake (54.1 , 6.4 )
53
- altitude: 0.0
54
- horizontalAccuracy: 0
55
- verticalAccuracy: 0
56
- timestamp: now];
57
-
57
+ altitude: 0.0
58
+ horizontalAccuracy: 0
59
+ verticalAccuracy: 0
60
+ timestamp: now];
61
+
58
62
59
63
XCTestExpectation *expectation = [self expectationWithDescription: @" expect result return third location" ];
60
64
[_geolocationHandler requestPositionWithDesiredAccuracy: kCLLocationAccuracyBest
@@ -64,20 +68,20 @@ - (void)testRequestPositionShouldReturnLocationWithinTimeConstraints {
64
68
}
65
69
errorHandler: ^(NSString * _Nonnull errorCode, NSString * _Nonnull errorDescription) {}];
66
70
67
- [_geolocationHandler locationManager: _mockLocationManager didUpdateLocations: @[firstLocation]];
68
- [_geolocationHandler locationManager: _mockLocationManager didUpdateLocations: @[secondLocation]];
69
-
71
+ [_geolocationHandler locationManager: _mockOneTimeLocationManager didUpdateLocations: @[firstLocation]];
72
+ [_geolocationHandler locationManager: _mockOneTimeLocationManager didUpdateLocations: @[secondLocation]];
73
+
70
74
[self waitForExpectationsWithTimeout: 5.0 handler: nil ];
71
75
72
- OCMVerify (times (1 ), [self ->_mockLocationManager stopUpdatingLocation ]);
76
+ OCMVerify (times (1 ), [self ->_mockOneTimeLocationManager stopUpdatingLocation ]);
73
77
}
74
78
75
79
- (void )testRequestPositionShouldStopListeningOnResult {
76
80
CLLocation *location = [[CLLocation alloc ] initWithCoordinate: CLLocationCoordinate2DMake (54.1 , 6.4 )
77
- altitude: 0.0
78
- horizontalAccuracy: 0
79
- verticalAccuracy: 0
80
- timestamp: [NSDate date ]];
81
+ altitude: 0.0
82
+ horizontalAccuracy: 0
83
+ verticalAccuracy: 0
84
+ timestamp: [NSDate date ]];
81
85
82
86
XCTestExpectation *expectation = [self expectationWithDescription: @" expect first result return third location" ];
83
87
[_geolocationHandler requestPositionWithDesiredAccuracy: kCLLocationAccuracyBest
@@ -88,11 +92,11 @@ - (void)testRequestPositionShouldStopListeningOnResult {
88
92
89
93
}];
90
94
91
- [_geolocationHandler locationManager: _mockLocationManager didUpdateLocations: @[location]];
95
+ [_geolocationHandler locationManager: _mockOneTimeLocationManager didUpdateLocations: @[location]];
92
96
93
97
[self waitForExpectationsWithTimeout: 5.0 handler: nil ];
94
98
95
- OCMVerify (times (1 ), [self ->_mockLocationManager stopUpdatingLocation ]);
99
+ OCMVerify (times (1 ), [self ->_mockOneTimeLocationManager stopUpdatingLocation ]);
96
100
}
97
101
98
102
- (void )testRequestPositionShouldStopListeningOnError {
@@ -107,11 +111,11 @@ - (void)testRequestPositionShouldStopListeningOnError {
107
111
108
112
}];
109
113
110
- [_geolocationHandler locationManager: _mockLocationManager didFailWithError: error];
114
+ [_geolocationHandler locationManager: _mockOneTimeLocationManager didFailWithError: error];
111
115
112
116
[self waitForExpectationsWithTimeout: 5.0 handler: nil ];
113
117
114
- OCMVerify (times (1 ), [self ->_mockLocationManager stopUpdatingLocation ]);
118
+ OCMVerify (times (1 ), [self ->_mockOneTimeLocationManager stopUpdatingLocation ]);
115
119
}
116
120
117
121
- (void )testRequestPositionShouldNotStopListeningOnErrorDomainAndErrorLocationUnknown {
@@ -158,7 +162,7 @@ - (void)testStartListeningShouldNotStopListeningWhenListeningToStream {
158
162
- (void )testRequestingPositionWhileListeningDoesntStopStream {
159
163
CLLocation *mockLocation = [[CLLocation alloc ] initWithLatitude: 54.0 longitude: 6.4 ];
160
164
XCTestExpectation *expectationStream = [self expectationWithDescription: @" expect result return third location" ];
161
- XCTestExpectation *expectationForeground = [self expectationWithDescription: @" expect result return third location" ];
165
+ XCTestExpectation *expectationForeground = [self expectationWithDescription: @" expect result return third location" ];
162
166
[_geolocationHandler startListeningWithDesiredAccuracy: kCLLocationAccuracyBest
163
167
distanceFilter: 0
164
168
pauseLocationUpdatesAutomatically: NO
@@ -172,14 +176,14 @@ - (void)testRequestingPositionWhileListeningDoesntStopStream {
172
176
errorHandler: ^(NSString * _Nonnull errorCode, NSString * _Nonnull errorDescription) {
173
177
174
178
}];
175
-
179
+
176
180
[_geolocationHandler requestPositionWithDesiredAccuracy: kCLLocationAccuracyHundredMeters
177
181
resultHandler: ^(CLLocation * _Nullable location) {
178
- XCTAssertEqual (location, mockLocation);
179
- [expectationForeground fulfill ];
180
- } errorHandler: ^(NSString * _Nonnull errorCode, NSString * _Nonnull errorDescription) {
181
-
182
- }];
182
+ XCTAssertEqual (location, mockLocation);
183
+ [expectationForeground fulfill ];
184
+ } errorHandler: ^(NSString * _Nonnull errorCode, NSString * _Nonnull errorDescription) {
185
+
186
+ }];
183
187
[_geolocationHandler locationManager: _mockLocationManager didUpdateLocations: @[mockLocation]];
184
188
185
189
[self waitForExpectationsWithTimeout: 5.0 handler: nil ];
@@ -236,39 +240,39 @@ - (void)testStartListeningShouldNotReportErrorOnErrorDomainAndErrorLocationUnkno
236
240
}
237
241
238
242
- (void )testListeningBackgroundGeolocationOnlyWhenAllowedAndEnabled {
239
- id geolocationHandlerMock = OCMPartialMock (_geolocationHandler);
240
- [geolocationHandlerMock setLocationManagerOverride: _mockLocationManager];
241
- OCMStub (ClassMethod ([geolocationHandlerMock shouldEnableBackgroundLocationUpdates ]))._andReturn ([NSNumber numberWithBool: YES ]);
242
- [geolocationHandlerMock startListeningWithDesiredAccuracy: kCLLocationAccuracyBest
243
- distanceFilter: 0
244
- pauseLocationUpdatesAutomatically: NO
245
- showBackgroundLocationIndicator: NO
246
- activityType: CLActivityTypeOther
247
- allowBackgroundLocationUpdates: YES
248
- resultHandler: ^(CLLocation * _Nullable location) {
249
- }
250
- errorHandler: ^(NSString * _Nonnull errorCode, NSString * _Nonnull errorDescription) {
251
-
252
- }];
253
- OCMVerify ([_mockLocationManager setAllowsBackgroundLocationUpdates: YES ]);
243
+ id geolocationHandlerMock = OCMPartialMock (_geolocationHandler);
244
+ [geolocationHandlerMock setLocationManagerOverride: _mockLocationManager];
245
+ OCMStub (ClassMethod ([geolocationHandlerMock shouldEnableBackgroundLocationUpdates ]))._andReturn ([NSNumber numberWithBool: YES ]);
246
+ [geolocationHandlerMock startListeningWithDesiredAccuracy: kCLLocationAccuracyBest
247
+ distanceFilter: 0
248
+ pauseLocationUpdatesAutomatically: NO
249
+ showBackgroundLocationIndicator: NO
250
+ activityType: CLActivityTypeOther
251
+ allowBackgroundLocationUpdates: YES
252
+ resultHandler: ^(CLLocation * _Nullable location) {
253
+ }
254
+ errorHandler: ^(NSString * _Nonnull errorCode, NSString * _Nonnull errorDescription) {
255
+
256
+ }];
257
+ OCMVerify ([_mockLocationManager setAllowsBackgroundLocationUpdates: YES ]);
254
258
}
255
259
256
260
- (void )testNotListeningBackgroundGeolocationWhenNotEnabled {
257
- id geolocationHandlerMock = OCMPartialMock (_geolocationHandler);
258
- [geolocationHandlerMock setLocationManagerOverride: _mockLocationManager];
259
- OCMStub (ClassMethod ([geolocationHandlerMock shouldEnableBackgroundLocationUpdates ]))._andReturn ([NSNumber numberWithBool: YES ]);
260
- [geolocationHandlerMock startListeningWithDesiredAccuracy: kCLLocationAccuracyBest
261
- distanceFilter: 0
262
- pauseLocationUpdatesAutomatically: NO
263
- showBackgroundLocationIndicator: NO
264
- activityType: CLActivityTypeOther
265
- allowBackgroundLocationUpdates: NO
266
- resultHandler: ^(CLLocation * _Nullable location) {
267
- }
268
- errorHandler: ^(NSString * _Nonnull errorCode, NSString * _Nonnull errorDescription) {
269
-
270
- }];
271
- OCMVerify (never (), [_mockLocationManager setAllowsBackgroundLocationUpdates: YES ]);
261
+ id geolocationHandlerMock = OCMPartialMock (_geolocationHandler);
262
+ [geolocationHandlerMock setLocationManagerOverride: _mockLocationManager];
263
+ OCMStub (ClassMethod ([geolocationHandlerMock shouldEnableBackgroundLocationUpdates ]))._andReturn ([NSNumber numberWithBool: YES ]);
264
+ [geolocationHandlerMock startListeningWithDesiredAccuracy: kCLLocationAccuracyBest
265
+ distanceFilter: 0
266
+ pauseLocationUpdatesAutomatically: NO
267
+ showBackgroundLocationIndicator: NO
268
+ activityType: CLActivityTypeOther
269
+ allowBackgroundLocationUpdates: NO
270
+ resultHandler: ^(CLLocation * _Nullable location) {
271
+ }
272
+ errorHandler: ^(NSString * _Nonnull errorCode, NSString * _Nonnull errorDescription) {
273
+
274
+ }];
275
+ OCMVerify (never (), [_mockLocationManager setAllowsBackgroundLocationUpdates: YES ]);
272
276
}
273
277
274
278
- (void )testKeepLocationManagerSettingsWhenRequestingCurrentPosition {
@@ -283,14 +287,14 @@ - (void)testKeepLocationManagerSettingsWhenRequestingCurrentPosition {
283
287
._andReturn ([NSNumber numberWithBool: YES ]);
284
288
}
285
289
[geolocationHandlerMock startListeningWithDesiredAccuracy: kCLLocationAccuracyBest
286
- distanceFilter: 0
287
- pauseLocationUpdatesAutomatically: NO
288
- showBackgroundLocationIndicator: YES
289
- activityType: CLActivityTypeOther
290
- allowBackgroundLocationUpdates: YES
291
- resultHandler: ^(CLLocation * _Nullable location) {
290
+ distanceFilter: 0
291
+ pauseLocationUpdatesAutomatically: NO
292
+ showBackgroundLocationIndicator: YES
293
+ activityType: CLActivityTypeOther
294
+ allowBackgroundLocationUpdates: YES
295
+ resultHandler: ^(CLLocation * _Nullable location) {
292
296
}
293
- errorHandler: ^(NSString * _Nonnull errorCode, NSString * _Nonnull errorDescription) {
297
+ errorHandler: ^(NSString * _Nonnull errorCode, NSString * _Nonnull errorDescription) {
294
298
295
299
}];
296
300
OCMVerify ([_mockLocationManager setAllowsBackgroundLocationUpdates: YES ]);
@@ -306,4 +310,29 @@ - (void)testKeepLocationManagerSettingsWhenRequestingCurrentPosition {
306
310
}
307
311
}
308
312
313
+ - (void )testKeepLocationManagerSettingsWhenRequestingCurrentPosition1 {
314
+ [_geolocationHandler startListeningWithDesiredAccuracy: kCLLocationAccuracyThreeKilometers
315
+ distanceFilter: kCLLocationAccuracyThreeKilometers
316
+ pauseLocationUpdatesAutomatically: NO
317
+ showBackgroundLocationIndicator: YES
318
+ activityType: CLActivityTypeOther
319
+ allowBackgroundLocationUpdates: YES
320
+ resultHandler: ^(CLLocation * _Nullable location) {
321
+ }
322
+ errorHandler: ^(NSString * _Nonnull errorCode, NSString * _Nonnull errorDescription) {
323
+
324
+ }];
325
+ OCMVerify ([_mockLocationManager setDesiredAccuracy: kCLLocationAccuracyThreeKilometers ]);
326
+ OCMVerify ([_mockLocationManager setDistanceFilter: kCLLocationAccuracyThreeKilometers ]);
327
+
328
+ [_geolocationHandler requestPositionWithDesiredAccuracy: kCLLocationAccuracyBest
329
+ resultHandler: ^(CLLocation * _Nullable location) {}
330
+ errorHandler: ^(NSString * _Nonnull errorCode, NSString * _Nonnull errorDescription) {}];
331
+ OCMVerify ([_mockOneTimeLocationManager setDesiredAccuracy: kCLLocationAccuracyBest ]);
332
+ OCMVerify ([_mockOneTimeLocationManager setDistanceFilter: kCLDistanceFilterNone ]);
333
+
334
+ OCMVerify (never (), [_mockLocationManager setDesiredAccuracy: kCLLocationAccuracyBest ]);
335
+ OCMVerify (never (), [_mockLocationManager setDistanceFilter: kCLDistanceFilterNone ]);
336
+ }
337
+
309
338
@end
0 commit comments