From bce30b98b8e7addce075b488c186e4c088ab79ed Mon Sep 17 00:00:00 2001 From: Jean-Richard Lai Date: Fri, 15 Apr 2016 08:53:48 -0700 Subject: [PATCH] Add RCTLocationObserverTests --- .../UIExplorer.xcodeproj/project.pbxproj | 4 + .../RCTLocationObserverTests.m | 110 ++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 Examples/UIExplorer/UIExplorerUnitTests/RCTLocationObserverTests.m diff --git a/Examples/UIExplorer/UIExplorer.xcodeproj/project.pbxproj b/Examples/UIExplorer/UIExplorer.xcodeproj/project.pbxproj index 6396f98462747a..57c80423f2ba3b 100644 --- a/Examples/UIExplorer/UIExplorer.xcodeproj/project.pbxproj +++ b/Examples/UIExplorer/UIExplorer.xcodeproj/project.pbxproj @@ -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 */ @@ -252,6 +253,7 @@ 8385CEF41B873B5C00C6273E /* RCTImageLoaderTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTImageLoaderTests.m; sourceTree = ""; }; 8385CF031B87479200C6273E /* RCTImageLoaderHelpers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTImageLoaderHelpers.m; sourceTree = ""; }; 8385CF051B8747A000C6273E /* RCTImageLoaderHelpers.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RCTImageLoaderHelpers.h; sourceTree = ""; }; + B69E592E1CC0C4E20034545C /* RCTLocationObserverTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTLocationObserverTests.m; sourceTree = ""; }; D85B82911AB6D5CE003F4FE2 /* RCTVibration.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTVibration.xcodeproj; path = ../../Libraries/Vibration/RCTVibration.xcodeproj; sourceTree = ""; }; /* End PBXFileReference section */ @@ -434,6 +436,7 @@ 138D6A161B53CD440074A87E /* RCTShadowViewTests.m */, 1497CFAB1B21F5E400C1F8F2 /* RCTUIManagerTests.m */, 13BCE84E1C9C209600DD7AAD /* RCTComponentPropsTests.m */, + B69E592E1CC0C4E20034545C /* RCTLocationObserverTests.m */, 143BC57E1B21E18100462512 /* Info.plist */, 14D6D7101B220EB3001FB087 /* libOCMock.a */, 14D6D7011B220AE3001FB087 /* OCMock */, @@ -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 */, diff --git a/Examples/UIExplorer/UIExplorerUnitTests/RCTLocationObserverTests.m b/Examples/UIExplorer/UIExplorerUnitTests/RCTLocationObserverTests.m new file mode 100644 index 00000000000000..aafb1852d86b11 --- /dev/null +++ b/Examples/UIExplorer/UIExplorerUnitTests/RCTLocationObserverTests.m @@ -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 + +#import +#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