diff options
author | Joel Fischer <joeljfischer@gmail.com> | 2017-01-25 15:13:25 -0500 |
---|---|---|
committer | Joel Fischer <joeljfischer@gmail.com> | 2017-01-25 15:13:25 -0500 |
commit | 567b48bd02333a61b953f9ccb5fb76595d27c3e0 (patch) | |
tree | 2ee041524e4ce4dd4dfd88f44a47ae766913cd1d | |
parent | 1d1ddd5cc5e4a44b241ff61d2ff8b5c8a5a2ce1a (diff) | |
parent | 21cc10ffeed809384bb6aa8f88f3e7d20b7dcc4e (diff) | |
download | sdl_ios-567b48bd02333a61b953f9ccb5fb76595d27c3e0.tar.gz |
Merge branch 'master' into develop
# Conflicts:
# SmartDeviceLink/SDLLifecycleManager.h
# SmartDeviceLink/SDLLifecycleManager.m
-rw-r--r-- | SmartDeviceLink/SDLLifecycleManager.h | 10 | ||||
-rw-r--r-- | SmartDeviceLink/SDLLifecycleManager.m | 35 | ||||
-rw-r--r-- | SmartDeviceLink/SDLStateMachine.h | 2 | ||||
-rw-r--r-- | SmartDeviceLink/SDLStateMachine.m | 19 | ||||
-rw-r--r-- | SmartDeviceLinkTests/DevAPISpecs/SDLLifecycleManagerSpec.m | 47 |
5 files changed, 73 insertions, 40 deletions
diff --git a/SmartDeviceLink/SDLLifecycleManager.h b/SmartDeviceLink/SDLLifecycleManager.h index 23002df19..86508f992 100644 --- a/SmartDeviceLink/SDLLifecycleManager.h +++ b/SmartDeviceLink/SDLLifecycleManager.h @@ -14,6 +14,8 @@ @class SDLConfiguration; @class SDLFileManager; +@class SDLHMILevel; +@class SDLLanguage; @class SDLLifecycleConfiguration; @class SDLLockScreenConfiguration; @class SDLLockScreenManager; @@ -65,13 +67,13 @@ typedef void (^SDLManagerReadyBlock)(BOOL success, NSError *_Nullable error); // Deprecated internal proxy object #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" -@property (strong, nonatomic, readonly, nullable) SDLProxy *proxy; +@property (strong, nonatomic, nullable) SDLProxy *proxy; #pragma clang diagnostic pop -@property (assign, nonatomic, readonly) UInt16 lastCorrelationId; +@property (assign, nonatomic) UInt16 lastCorrelationId; @property (copy, nonatomic, readonly) SDLLifecycleState *lifecycleState; -@property (copy, nonatomic, readonly, nullable) SDLHMILevel hmiLevel; -@property (strong, nonatomic, readonly, nullable) SDLRegisterAppInterfaceResponse *registerResponse; +@property (copy, nonatomic, nullable) SDLHMILevel hmiLevel; +@property (strong, nonatomic, nullable) SDLRegisterAppInterfaceResponse *registerResponse; #pragma mark Lifecycle diff --git a/SmartDeviceLink/SDLLifecycleManager.m b/SmartDeviceLink/SDLLifecycleManager.m index 4675a30a2..302ab6b95 100644 --- a/SmartDeviceLink/SDLLifecycleManager.m +++ b/SmartDeviceLink/SDLLifecycleManager.m @@ -58,18 +58,10 @@ SDLLifecycleState *const SDLLifecycleStateReady = @"Ready"; // Readonly public properties @property (copy, nonatomic, readwrite, nullable) SDLHMILevel hmiLevel; @property (copy, nonatomic, readwrite) SDLConfiguration *configuration; -@property (assign, nonatomic, readwrite) UInt16 lastCorrelationId; -@property (strong, nonatomic, readwrite, nullable) SDLRegisterAppInterfaceResponse *registerResponse; @property (strong, nonatomic, readwrite) SDLNotificationDispatcher *notificationDispatcher; @property (strong, nonatomic, readwrite) SDLResponseDispatcher *responseDispatcher; @property (strong, nonatomic, readwrite) SDLStateMachine *lifecycleStateMachine; -// Deprecated internal proxy object -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" -@property (strong, nonatomic, readwrite, nullable) SDLProxy *proxy; -#pragma clang diagnostic pop - // Private properties @property (copy, nonatomic) SDLManagerReadyBlock readyHandler; @@ -195,8 +187,10 @@ SDLLifecycleState *const SDLLifecycleStateReady = @"Ready"; __weak typeof(self) weakSelf = self; [self sdl_sendRequest:regRequest withResponseHandler:^(__kindof SDLRPCRequest *_Nullable request, __kindof SDLRPCResponse *_Nullable response, NSError *_Nullable error) { + // If the success BOOL is NO or we received an error at this point, we failed. Call the ready handler and transition to the DISCONNECTED state. if (error != nil || ![response.success boolValue]) { [SDLDebugTool logFormat:@"Failed to register the app. Error: %@, Response: %@", error, response]; + weakSelf.readyHandler(NO, error); [weakSelf.lifecycleStateMachine transitionToState:SDLLifecycleStateDisconnected]; return; } @@ -256,32 +250,15 @@ SDLLifecycleState *const SDLLifecycleStateReady = @"Ready"; - (void)didEnterStateReady { SDLResult registerResult = self.registerResponse.resultCode; NSString *registerInfo = self.registerResponse.info; - - BOOL success = NO; NSError *startError = nil; - - if ([registerResult isEqualToString:SDLResultWarnings] || [registerResult isEqualToString:SDLResultResumeFailed]) { - // We succeeded, but with warnings + // If the resultCode isn't success, we got a warning. Errors were handled in `didEnterStateConnected`. + if (![registerResult isEqualToString:SDLResultSuccess]) { startError = [NSError sdl_lifecycle_startedWithBadResult:registerResult info:registerInfo]; - success = YES; - } else if (![registerResult isEqualToString:SDLResultSuccess]) { - // We did not succeed in registering - startError = [NSError sdl_lifecycle_failedWithBadResult:registerResult info:registerInfo]; - success = NO; - } else { - // We succeeded - success = YES; - } - - // Notify the block, send the notification if we succeeded. - self.readyHandler(success, startError); - - if (!success) { - [self.lifecycleStateMachine transitionToState:SDLLifecycleStateReady]; - return; } + // If we got to this point, we succeeded, send the error if there was a warning. + self.readyHandler(YES, startError); [self.notificationDispatcher postNotificationName:SDLDidBecomeReady infoObject:nil]; // Send the hmi level going from NONE to whatever we're at now (could still be NONE) diff --git a/SmartDeviceLink/SDLStateMachine.h b/SmartDeviceLink/SDLStateMachine.h index 3690261f9..fa3b3093b 100644 --- a/SmartDeviceLink/SDLStateMachine.h +++ b/SmartDeviceLink/SDLStateMachine.h @@ -88,7 +88,7 @@ extern SDLStateMachineTransitionFormat const SDLStateMachineTransitionFormatDidE * * @param state The state to set to */ -- (void)setToState:(SDLState *)state; +- (void)setToState:(SDLState *)state fromOldState:(nullable SDLState *)oldState callEnterTransition:(BOOL)shouldCall; /** * Return whether or not the current state is the passed state diff --git a/SmartDeviceLink/SDLStateMachine.m b/SmartDeviceLink/SDLStateMachine.m index 126f0e55b..ab5c54689 100644 --- a/SmartDeviceLink/SDLStateMachine.m +++ b/SmartDeviceLink/SDLStateMachine.m @@ -109,12 +109,25 @@ SDLStateMachineTransitionFormat const SDLStateMachineTransitionFormatDidEnter = #pragma mark - Helpers -- (void)setToState:(SDLState *)state { +- (void)setToState:(SDLState *)state fromOldState:(nullable SDLState *)oldState callEnterTransition:(BOOL)shouldCall { if (![self.states.allKeys containsObject:state]) { return; } - self.currentState = state; + if (oldState != nil && shouldCall) { + self.currentState = oldState; + [self transitionToState:state]; + } else if (shouldCall) { + SEL didEnter = NSSelectorFromString([NSString stringWithFormat:SDLStateMachineTransitionFormatDidEnter, state]); +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Warc-performSelector-leaks" + if ([self.target respondsToSelector:didEnter]) { + [self.target performSelector:didEnter]; +#pragma clang diagnostic pop + } + } else { + self.currentState = state; + } } /** @@ -139,4 +152,4 @@ SDLStateMachineTransitionFormat const SDLStateMachineTransitionFormatDidEnter = @end -NS_ASSUME_NONNULL_END
\ No newline at end of file +NS_ASSUME_NONNULL_END diff --git a/SmartDeviceLinkTests/DevAPISpecs/SDLLifecycleManagerSpec.m b/SmartDeviceLinkTests/DevAPISpecs/SDLLifecycleManagerSpec.m index e0ed18b7b..7eac1902c 100644 --- a/SmartDeviceLinkTests/DevAPISpecs/SDLLifecycleManagerSpec.m +++ b/SmartDeviceLinkTests/DevAPISpecs/SDLLifecycleManagerSpec.m @@ -21,6 +21,7 @@ #import "SDLProxyFactory.h" #import "SDLRegisterAppInterface.h" #import "SDLRegisterAppInterfaceResponse.h" +#import "SDLResult.h" #import "SDLShow.h" #import "SDLStateMachine.h" #import "SDLTextAlignment.h" @@ -156,8 +157,17 @@ describe(@"a lifecycle manager", ^{ }); describe(@"when started", ^{ + __block BOOL readyHandlerSuccess = NO; + __block NSError *readyHandlerError = nil; + beforeEach(^{ - [testManager startWithReadyHandler:^(BOOL success, NSError * _Nullable error) {}]; + readyHandlerSuccess = NO; + readyHandlerError = nil; + + [testManager startWithReadyHandler:^(BOOL success, NSError * _Nullable error) { + readyHandlerSuccess = success; + readyHandlerError = error; + }]; }); it(@"should initialize the proxy property", ^{ @@ -205,7 +215,7 @@ describe(@"a lifecycle manager", ^{ describe(@"in the connected state", ^{ beforeEach(^{ - [testManager.lifecycleStateMachine setToState:SDLLifecycleStateTransportConnected]; + [testManager.lifecycleStateMachine setToState:SDLLifecycleStateTransportConnected fromOldState:nil callEnterTransition:NO]; }); describe(@"after receiving a register app interface response", ^{ @@ -252,10 +262,41 @@ describe(@"a lifecycle manager", ^{ }); }); }); + + describe(@"transitioning to the ready state", ^{ + context(@"when the register response is a success", ^{ + it(@"should call the ready handler with success", ^{ + SDLRegisterAppInterfaceResponse *response = [[SDLRegisterAppInterfaceResponse alloc] init]; + response.resultCode = [SDLResult SUCCESS]; + testManager.registerResponse = response; + + [testManager.lifecycleStateMachine setToState:SDLLifecycleStateReady fromOldState:nil callEnterTransition:YES]; + + expect(@(readyHandlerSuccess)).to(equal(@YES)); + expect(readyHandlerError).to(beNil()); + }); + }); + + context(@"when the register response is a warning", ^{ + it(@"should call the ready handler with success but error", ^{ + SDLRegisterAppInterfaceResponse *response = [[SDLRegisterAppInterfaceResponse alloc] init]; + response.resultCode = [SDLResult WARNINGS]; + response.info = @"some info"; + testManager.registerResponse = response; + + [testManager.lifecycleStateMachine setToState:SDLLifecycleStateReady fromOldState:nil callEnterTransition:YES]; + + expect(@(readyHandlerSuccess)).to(equal(@YES)); + expect(readyHandlerError).toNot(beNil()); + expect(@(readyHandlerError.code)).to(equal(@(SDLManagerErrorRegistrationFailed))); + expect(readyHandlerError.userInfo[NSLocalizedFailureReasonErrorKey]).to(match(response.info)); + }); + }); + }); describe(@"in the ready state", ^{ beforeEach(^{ - [testManager.lifecycleStateMachine setToState:SDLLifecycleStateReady]; + [testManager.lifecycleStateMachine setToState:SDLLifecycleStateReady fromOldState:nil callEnterTransition:NO]; }); it(@"can send an RPC", ^{ |