Skip to content

Commit 6a226c8

Browse files
ksenia-lyagushaOksana Liahusha
andauthored
Fixes a bug where iOS location settings, e.g. accuracy and distanceFilter are overridden by different calls (Baseflow#1600)
* Fixes a bug where iOS location settings, e.g. `accuracy` and `distanceFilter` are overridden by different calls * Reverted changes to the geolocator package to create a separate PR for those. Revert the changes for the requestLocation API. Now use separate oneTimeLocationManager with startUpdatingLocation logic. Reverted and updated tests. --------- Co-authored-by: Oksana Liahusha <oksana.liahusha@ssa.group>
1 parent b53e4a5 commit 6a226c8

File tree

7 files changed

+176
-128
lines changed

7 files changed

+176
-128
lines changed

geolocator_apple/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 2.3.8
2+
3+
* Uses different `CLLocationManager` instances, for one time request location and persistent request location.
4+
* Fixes a bug where iOS location settings, e.g. `accuracy` and `distanceFilter` are overridden by different calls.
5+
* Updates minimum deployment target to `iOS 11` as lower is not supported anymore by Xcode.
6+
17
## 2.3.7
28

39
* Adds privacy manifest.

geolocator_apple/example/ios/RunnerTests/GeolocationHandlerTests.m

Lines changed: 93 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@ @interface GeolocationHandlerTests : XCTestCase
1919

2020
@implementation GeolocationHandlerTests {
2121
CLLocationManager *_mockLocationManager;
22+
CLLocationManager *_mockOneTimeLocationManager;
23+
2224
GeolocationHandler *_geolocationHandler;
2325
}
2426

2527
- (void)setUp {
2628
_mockLocationManager = OCMClassMock(CLLocationManager.class);
29+
_mockOneTimeLocationManager = OCMClassMock(CLLocationManager.class);
2730

2831
_geolocationHandler = [[GeolocationHandler alloc] init];
2932
[_geolocationHandler setLocationManagerOverride:_mockLocationManager];
33+
[_geolocationHandler setOneTimeLocationManagerOverride:_mockOneTimeLocationManager];
3034
}
3135

3236
#pragma mark - Test requesting current location
@@ -36,25 +40,25 @@ - (void)testGetCurrentPositionShouldCallStartUpdatingLocation {
3640
resultHandler:^(CLLocation * _Nullable location) {}
3741
errorHandler:^(NSString * _Nonnull errorCode, NSString * _Nonnull errorDescription) {}];
3842

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]);
4145
}
4246

4347
- (void)testRequestPositionShouldReturnLocationWithinTimeConstraints {
4448
NSDate *now = [NSDate date];
4549
NSCalendar *calendar = [NSCalendar currentCalendar];
46-
50+
4751
CLLocation *firstLocation = [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(54.0, 6.4)
4852
altitude:0.0
4953
horizontalAccuracy:0
5054
verticalAccuracy:0
5155
timestamp:[calendar dateByAddingUnit:NSCalendarUnitSecond value:-6 toDate:now options:0]];
5256
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+
5862

5963
XCTestExpectation *expectation = [self expectationWithDescription:@"expect result return third location"];
6064
[_geolocationHandler requestPositionWithDesiredAccuracy:kCLLocationAccuracyBest
@@ -64,20 +68,20 @@ - (void)testRequestPositionShouldReturnLocationWithinTimeConstraints {
6468
}
6569
errorHandler:^(NSString * _Nonnull errorCode, NSString * _Nonnull errorDescription) {}];
6670

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+
7074
[self waitForExpectationsWithTimeout:5.0 handler:nil];
7175

72-
OCMVerify(times(1), [self->_mockLocationManager stopUpdatingLocation]);
76+
OCMVerify(times(1), [self->_mockOneTimeLocationManager stopUpdatingLocation]);
7377
}
7478

7579
- (void)testRequestPositionShouldStopListeningOnResult {
7680
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]];
8185

8286
XCTestExpectation *expectation = [self expectationWithDescription:@"expect first result return third location"];
8387
[_geolocationHandler requestPositionWithDesiredAccuracy:kCLLocationAccuracyBest
@@ -88,11 +92,11 @@ - (void)testRequestPositionShouldStopListeningOnResult {
8892

8993
}];
9094

91-
[_geolocationHandler locationManager:_mockLocationManager didUpdateLocations: @[location]];
95+
[_geolocationHandler locationManager:_mockOneTimeLocationManager didUpdateLocations: @[location]];
9296

9397
[self waitForExpectationsWithTimeout:5.0 handler:nil];
9498

95-
OCMVerify(times(1), [self->_mockLocationManager stopUpdatingLocation]);
99+
OCMVerify(times(1), [self->_mockOneTimeLocationManager stopUpdatingLocation]);
96100
}
97101

98102
- (void)testRequestPositionShouldStopListeningOnError {
@@ -107,11 +111,11 @@ - (void)testRequestPositionShouldStopListeningOnError {
107111

108112
}];
109113

110-
[_geolocationHandler locationManager:_mockLocationManager didFailWithError: error];
114+
[_geolocationHandler locationManager:_mockOneTimeLocationManager didFailWithError: error];
111115

112116
[self waitForExpectationsWithTimeout:5.0 handler:nil];
113117

114-
OCMVerify(times(1), [self->_mockLocationManager stopUpdatingLocation]);
118+
OCMVerify(times(1), [self->_mockOneTimeLocationManager stopUpdatingLocation]);
115119
}
116120

117121
- (void)testRequestPositionShouldNotStopListeningOnErrorDomainAndErrorLocationUnknown {
@@ -158,7 +162,7 @@ - (void)testStartListeningShouldNotStopListeningWhenListeningToStream {
158162
- (void)testRequestingPositionWhileListeningDoesntStopStream {
159163
CLLocation *mockLocation = [[CLLocation alloc] initWithLatitude:54.0 longitude:6.4];
160164
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"];
162166
[_geolocationHandler startListeningWithDesiredAccuracy: kCLLocationAccuracyBest
163167
distanceFilter:0
164168
pauseLocationUpdatesAutomatically:NO
@@ -172,14 +176,14 @@ - (void)testRequestingPositionWhileListeningDoesntStopStream {
172176
errorHandler:^(NSString * _Nonnull errorCode, NSString * _Nonnull errorDescription) {
173177

174178
}];
175-
179+
176180
[_geolocationHandler requestPositionWithDesiredAccuracy:kCLLocationAccuracyHundredMeters
177181
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+
}];
183187
[_geolocationHandler locationManager:_mockLocationManager didUpdateLocations: @[mockLocation]];
184188

185189
[self waitForExpectationsWithTimeout:5.0 handler:nil];
@@ -236,39 +240,39 @@ - (void)testStartListeningShouldNotReportErrorOnErrorDomainAndErrorLocationUnkno
236240
}
237241

238242
- (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]);
254258
}
255259

256260
- (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]);
272276
}
273277

274278
- (void)testKeepLocationManagerSettingsWhenRequestingCurrentPosition {
@@ -283,14 +287,14 @@ - (void)testKeepLocationManagerSettingsWhenRequestingCurrentPosition {
283287
._andReturn([NSNumber numberWithBool:YES]);
284288
}
285289
[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) {
292296
}
293-
errorHandler:^(NSString * _Nonnull errorCode, NSString * _Nonnull errorDescription) {
297+
errorHandler:^(NSString * _Nonnull errorCode, NSString * _Nonnull errorDescription) {
294298

295299
}];
296300
OCMVerify([_mockLocationManager setAllowsBackgroundLocationUpdates:YES]);
@@ -306,4 +310,29 @@ - (void)testKeepLocationManagerSettingsWhenRequestingCurrentPosition {
306310
}
307311
}
308312

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+
309338
@end

geolocator_apple/example/ios/RunnerTests/GeolocatorPluginTests.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ - (void)testOpenAppSettings {
336336

337337
if (@available(iOS 8, *)) {
338338
id mockApplication = OCMClassMock([UIApplication class]);
339-
OCMStub([mockApplication openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]).andReturn(YES);
339+
OCMStub([(UIApplication *)mockApplication openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]).andReturn(YES);
340340
OCMStub(ClassMethod([mockApplication sharedApplication])).andReturn(mockApplication);
341341

342342

@@ -394,7 +394,7 @@ - (void)testOpenLocationSettings {
394394

395395
if (@available(iOS 8, *)) {
396396
id mockApplication = OCMClassMock([UIApplication class]);
397-
OCMStub([mockApplication openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]).andReturn(YES);
397+
OCMStub([(UIApplication *)mockApplication openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]).andReturn(YES);
398398
OCMStub(ClassMethod([mockApplication sharedApplication])).andReturn(mockApplication);
399399

400400

0 commit comments

Comments
 (0)