Skip to content

Commit

Permalink
Add RCTLocationObserverTests
Browse files Browse the repository at this point in the history
  • Loading branch information
jrichardlai committed Apr 15, 2016
1 parent 26893cf commit bce30b9
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Examples/UIExplorer/UIExplorer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
83636F8F1B53F22C009F943E /* RCTUIManagerScenarioTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 83636F8E1B53F22C009F943E /* RCTUIManagerScenarioTests.m */; };
8385CEF51B873B5C00C6273E /* RCTImageLoaderTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8385CEF41B873B5C00C6273E /* RCTImageLoaderTests.m */; };
8385CF041B87479200C6273E /* RCTImageLoaderHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 8385CF031B87479200C6273E /* RCTImageLoaderHelpers.m */; };
B69E592F1CC0C4E20034545C /* RCTLocationObserverTests.m in Sources */ = {isa = PBXBuildFile; fileRef = B69E592E1CC0C4E20034545C /* RCTLocationObserverTests.m */; };
D85B829E1AB6D5D7003F4FE2 /* libRCTVibration.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D85B829C1AB6D5CE003F4FE2 /* libRCTVibration.a */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -252,6 +253,7 @@
8385CEF41B873B5C00C6273E /* RCTImageLoaderTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTImageLoaderTests.m; sourceTree = "<group>"; };
8385CF031B87479200C6273E /* RCTImageLoaderHelpers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTImageLoaderHelpers.m; sourceTree = "<group>"; };
8385CF051B8747A000C6273E /* RCTImageLoaderHelpers.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RCTImageLoaderHelpers.h; sourceTree = "<group>"; };
B69E592E1CC0C4E20034545C /* RCTLocationObserverTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTLocationObserverTests.m; sourceTree = "<group>"; };
D85B82911AB6D5CE003F4FE2 /* RCTVibration.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTVibration.xcodeproj; path = ../../Libraries/Vibration/RCTVibration.xcodeproj; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -434,6 +436,7 @@
138D6A161B53CD440074A87E /* RCTShadowViewTests.m */,
1497CFAB1B21F5E400C1F8F2 /* RCTUIManagerTests.m */,
13BCE84E1C9C209600DD7AAD /* RCTComponentPropsTests.m */,
B69E592E1CC0C4E20034545C /* RCTLocationObserverTests.m */,
143BC57E1B21E18100462512 /* Info.plist */,
14D6D7101B220EB3001FB087 /* libOCMock.a */,
14D6D7011B220AE3001FB087 /* OCMock */,
Expand Down Expand Up @@ -895,6 +898,7 @@
13129DD41C85F87C007D611C /* RCTModuleInitNotificationRaceTests.m in Sources */,
1497CFAD1B21F5E400C1F8F2 /* RCTBridgeTests.m in Sources */,
134CB92A1C85A38800265FA6 /* RCTModuleInitTests.m in Sources */,
B69E592F1CC0C4E20034545C /* RCTLocationObserverTests.m in Sources */,
1497CFB11B21F5E400C1F8F2 /* RCTEventDispatcherTests.m in Sources */,
1497CFB31B21F5E400C1F8F2 /* RCTUIManagerTests.m in Sources */,
13DB03481B5D2ED500C27245 /* RCTJSONTests.m in Sources */,
Expand Down
110 changes: 110 additions & 0 deletions Examples/UIExplorer/UIExplorerUnitTests/RCTLocationObserverTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* The examples provided by Facebook are for non-commercial testing and
* evaluation purposes only.
*
* Facebook reserves all rights not expressly granted.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#import <XCTest/XCTest.h>

#import <CoreLocation/CLLocationManager.h>
#import "RCTLocationObserver.h"

@interface RCTLocationObserverTests : XCTestCase

@property (nonatomic, readwrite, strong) RCTLocationObserver *locationObserver;

@end

typedef struct {
double timeout;
double maximumAge;
double accuracy;
double distanceFilter;
} RCTLocationOptions;

@implementation RCTLocationObserverTests

- (void)setUp
{
[super setUp];

self.locationObserver = [RCTLocationObserver new];
}

#pragma GCC diagnostic ignored "-Wundeclared-selector"

- (void)callStartObservingWithOptions:(RCTLocationOptions)options {
SEL startObservingSel = @selector(startObserving:);
NSMethodSignature *methodSignature = [self.locationObserver methodSignatureForSelector:startObservingSel];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
[invocation setTarget:self.locationObserver];
[invocation setSelector:startObservingSel];
[invocation setArgument:&options atIndex:2];
[invocation invoke];
}

- (void)callGetCurrentPositionWithOptions:(RCTLocationOptions)options {
SEL getCurrentPositionSel = @selector(getCurrentPosition:withSuccessCallback:errorCallback:);
NSMethodSignature *methodSignature = [self.locationObserver methodSignatureForSelector:getCurrentPositionSel];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];

void (^noOpBlock)() = ^void() {};

[invocation setTarget:self.locationObserver];
[invocation setSelector:getCurrentPositionSel];
[invocation setArgument:&options atIndex:2];
[invocation setArgument:(__bridge void * _Nonnull)(noOpBlock) atIndex:3];
[invocation setArgument:(__bridge void * _Nonnull)(noOpBlock) atIndex:4];
[invocation invoke];
}

- (void)testLocationMangerSettingsCanBeChanged
{
CLLocationManager *locationManager;
locationManager = [self.locationObserver valueForKey:@"locationManager"];
// make sure it is not initialized when nothing happened
XCTAssertNil(locationManager);

[self callStartObservingWithOptions:(RCTLocationOptions){
.accuracy = 10,
.distanceFilter = 20
}];
locationManager = [self.locationObserver valueForKey:@"locationManager"];
XCTAssertEqual(locationManager.desiredAccuracy, 10);
XCTAssertEqual(locationManager.distanceFilter, 20);

[self callStartObservingWithOptions:(RCTLocationOptions){
.accuracy = 30,
.distanceFilter = 40
}];
locationManager = [self.locationObserver valueForKey:@"locationManager"];
XCTAssertEqual(locationManager.desiredAccuracy, 30);
XCTAssertEqual(locationManager.distanceFilter, 40);
}

- (void)testGetCurrentPositionRunsProperly
{
CLLocationManager *locationManager;
locationManager = [self.locationObserver valueForKey:@"locationManager"];
// make sure it is not initialized when nothing happened
XCTAssertNil(locationManager);

[self callGetCurrentPositionWithOptions:(RCTLocationOptions){
.accuracy = 10,
.distanceFilter = 20
}];

locationManager = [self.locationObserver valueForKey:@"locationManager"];
XCTAssertEqual(locationManager.desiredAccuracy, 10);
XCTAssertEqual(locationManager.distanceFilter, 20);
}

@end

0 comments on commit bce30b9

Please sign in to comment.