forked from parse-community/Parse-SDK-iOS-OSX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeoPointLocationTests.m
126 lines (107 loc) · 4.39 KB
/
GeoPointLocationTests.m
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
/**
* Copyright (c) 2015-present, Parse, LLC.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "CLLocationManager+TestAdditions.h"
#import "PFCoreManager.h"
#import "PFGeoPoint.h"
#import "PFUnitTestCase.h"
#import "Parse_Private.h"
@interface GeoPointLocationTests : PFUnitTestCase
@end
@implementation GeoPointLocationTests
///--------------------------------------
#pragma mark - XCTestCase
///--------------------------------------
- (void)setUp {
[super setUp];
[CLLocationManager reset];
}
///--------------------------------------
#pragma mark - Tests
///--------------------------------------
- (void)testGeoPointForCurrentLocation {
[CLLocationManager setMockingEnabled:YES];
__block NSInteger returnedGeoPoints = 0;
// Simulate a delayed response from locationManager, make sure PFGeoPoints are
// returned for all requests.
[CLLocationManager setReturnLocation:NO];
[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
XCTAssertEqualWithAccuracy(geoPoint.latitude, CL_DEFAULT_LATITUDE, 0.00001,
@"Current location should have been set to fakeLocation");
XCTAssertEqualWithAccuracy(geoPoint.longitude, CL_DEFAULT_LONGITUDE, 0.00001,
@"Current location should have been set to fakeLocation");
XCTAssertNil(error, @"No error should have been found");
if (geoPoint) {
returnedGeoPoints++;
}
}];
[CLLocationManager setReturnLocation:YES];
[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
if (geoPoint) {
returnedGeoPoints++;
}
}];
XCTAssertEqual(2, returnedGeoPoints, @"Both blocks should have been called");
returnedGeoPoints = 0;
[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
if (geoPoint) {
returnedGeoPoints++;
}
}];
XCTAssertEqual(1, returnedGeoPoints, @"Only the final block should have been called");
}
- (void)testGeoLocationManager {
[CLLocationManager setMockingEnabled:YES];
// Short-circuits the locationManager so it calls the delegate's -locationManager:didFailWithError:
[CLLocationManager setWillFail:YES];
__block NSInteger errorCount = 0;
[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
if (error) {
errorCount++;
}
}];
XCTAssertEqual(1, errorCount, @"Failure is passed back as an error");
[CLLocationManager setWillFail:NO];
__block NSInteger geoPointCount = 0;
[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
if (geoPoint) {
geoPointCount++;
}
}];
XCTAssertEqual(1, geoPointCount, @"CLLocationManager should be passing back locations");
}
- (void)testGeoPointForCurrentLocationNested {
// Short-circuits the locationManager so it calls the delegate's -locationManager:didFailWithError:
[CLLocationManager setWillFail:YES];
__block NSInteger errorCount = 0;
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
if (error) {
errorCount++;
[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
errorCount++;
[expectation fulfill];
}];
}
}];
[self waitForTestExpectations];
XCTAssertEqual(2, errorCount, @"Failure is passed back as an error");
}
- (void)testGeoPointForCurrentLocationFromBackgroundThread {
[CLLocationManager setMockingEnabled:YES];
[CLLocationManager setReturnLocation:YES];
[CLLocationManager setWillFail:NO];
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
[expectation fulfill];
}];
});
[self waitForTestExpectations];
}
@end