diff options
author | Justin Gluck <justin.gluck@livio.io> | 2020-02-07 09:55:17 -0500 |
---|---|---|
committer | Justin Gluck <justin.gluck@livio.io> | 2020-02-07 09:55:17 -0500 |
commit | 73ad4212bf62d2afdaca13a6b4bbdab5b3242cbf (patch) | |
tree | deae12a1d44703847768e617d0a497446b38b501 | |
parent | d04c9c8ff88eb4bff609766908ec70f882e8d891 (diff) | |
download | sdl_ios-73ad4212bf62d2afdaca13a6b4bbdab5b3242cbf.tar.gz |
adding unit tests, removing appIsReady since we now wait for HMI full before we connect
-rw-r--r-- | SmartDeviceLink/SDLSecondaryTransportManager.m | 29 | ||||
-rw-r--r-- | SmartDeviceLinkTests/ProxySpecs/SDLSecondaryTransportManagerSpec.m | 52 |
2 files changed, 40 insertions, 41 deletions
diff --git a/SmartDeviceLink/SDLSecondaryTransportManager.m b/SmartDeviceLink/SDLSecondaryTransportManager.m index 3bb74c25d..3f0fb4a4c 100644 --- a/SmartDeviceLink/SDLSecondaryTransportManager.m +++ b/SmartDeviceLink/SDLSecondaryTransportManager.m @@ -97,8 +97,6 @@ static const int TCPPortUnspecified = -1; @property (strong, nonatomic, nullable) NSString *ipAddress; // TCP port number of SDL Core. If the information isn't available then TCPPortUnspecified is stored. @property (assign, nonatomic) int tcpPort; -// App is ready to set security manager to secondary protocol -@property (assign, nonatomic, getter=isAppReady) BOOL appReady; @property (assign, nonatomic) BOOL shouldOpenConnection; @@ -126,7 +124,6 @@ static const int TCPPortUnspecified = -1; @(SDLServiceTypeVideo):@(SDLTransportClassInvalid)} mutableCopy]; _tcpPort = TCPPortUnspecified; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBecomeReady) name:SDLDidBecomeReady object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sdl_hmiStatusDidChange:) name:SDLDidChangeHMIStatusNotification object:nil]; return self; @@ -176,6 +173,7 @@ static const int TCPPortUnspecified = -1; [secondaryTransports addObject:@(transport)]; } } + NSArray<SDLTransportClassBox *> *transportsForAudio = [self sdl_convertServiceTransports:payload.audioServiceTransports]; NSArray<SDLTransportClassBox *> *transportsForVideo = [self sdl_convertServiceTransports:payload.videoServiceTransports]; @@ -234,8 +232,8 @@ static const int TCPPortUnspecified = -1; } - (void)didEnterStateConfigured { - if ((self.secondaryTransportType == SDLSecondaryTransportTypeTCP && [self sdl_isTCPReady] && self.isAppReady && self.shouldOpenConnection) - || (self.secondaryTransportType == SDLSecondaryTransportTypeIAP && self.isAppReady && self.shouldOpenConnection)) { + if ((self.secondaryTransportType == SDLSecondaryTransportTypeTCP && [self sdl_isTCPReady] && self.shouldOpenConnection) + || (self.secondaryTransportType == SDLSecondaryTransportTypeIAP && self.shouldOpenConnection)) { [self.stateMachine transitionToState:SDLSecondaryTransportStateConnecting]; } } @@ -287,7 +285,6 @@ static const int TCPPortUnspecified = -1; } - (void)didEnterStateReconnecting { - self.appReady = NO; __weak typeof(self) weakSelf = self; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(RetryConnectionDelay * NSEC_PER_SEC)), _stateMachineQueue, ^{ if ([weakSelf.stateMachine isCurrentState:SDLSecondaryTransportStateReconnecting]) { @@ -320,6 +317,7 @@ static const int TCPPortUnspecified = -1; self.ipAddress = nil; self.tcpPort = TCPPortUnspecified; + self.shouldOpenConnection = nil; } - (void)sdl_configureManager:(nullable NSArray<SDLSecondaryTransportTypeBox *> *)availableSecondaryTransports @@ -377,7 +375,7 @@ static const int TCPPortUnspecified = -1; return; } - if ([self.stateMachine isCurrentState:SDLSecondaryTransportStateConfigured] && [self sdl_isTCPReady] && self.isAppReady && self.shouldOpenConnection) { + if ([self.stateMachine isCurrentState:SDLSecondaryTransportStateConfigured] && [self sdl_isTCPReady] && self.shouldOpenConnection) { [self.stateMachine transitionToState:SDLSecondaryTransportStateConnecting]; } else if ([self sdl_isTransportOpened]) { // Disconnect current transport. If the IP address is available then we will reconnect immediately. @@ -661,7 +659,7 @@ static const int TCPPortUnspecified = -1; } } else if (notification.name == UIApplicationDidBecomeActiveNotification) { if (([self.stateMachine isCurrentState:SDLSecondaryTransportStateConfigured]) - && self.secondaryTransportType == SDLSecondaryTransportTypeTCP && [self sdl_isTCPReady] && self.isAppReady && self.shouldOpenConnection) { + && self.secondaryTransportType == SDLSecondaryTransportTypeTCP && [self sdl_isTCPReady] && self.shouldOpenConnection) { SDLLogD(@"Resuming TCP transport since the app becomes foreground"); [self.stateMachine transitionToState:SDLSecondaryTransportStateConnecting]; } @@ -718,23 +716,18 @@ static const int TCPPortUnspecified = -1; } } -- (void)appDidBecomeReady { - self.appReady = YES; - if (([self.stateMachine.currentState isEqualToString:SDLSecondaryTransportStateConfigured] && self.tcpPort != SDLControlFrameInt32NotFound && self.ipAddress != nil && self.shouldOpenConnection) - || self.secondaryTransportType == SDLSecondaryTransportTypeIAP) { - [self.stateMachine transitionToState:SDLSecondaryTransportStateConnecting]; - } -} - - (void)sdl_hmiStatusDidChange:(SDLRPCNotificationNotification *)notification { if (![notification isNotificationMemberOfClass:[SDLOnHMIStatus class]]) { return; } SDLOnHMIStatus *hmiStatusNotification = notification.notification; - if(![hmiStatusNotification.hmiLevel isEqualToEnum:SDLHMILevelNone]) { + if(![hmiStatusNotification.hmiLevel isEqualToEnum:SDLHMILevelNone] && !self.shouldOpenConnection && [self.stateMachine isCurrentState:SDLSecondaryTransportStateConfigured]) { self.shouldOpenConnection = true; - [self.stateMachine transitionToState:SDLSecondaryTransportStateConfigured]; + if ((self.secondaryTransportType == SDLSecondaryTransportTypeTCP && [self sdl_isTCPReady]) + || (self.secondaryTransportType == SDLSecondaryTransportTypeIAP)) { + [self.stateMachine transitionToState:SDLSecondaryTransportStateConnecting]; + } } } diff --git a/SmartDeviceLinkTests/ProxySpecs/SDLSecondaryTransportManagerSpec.m b/SmartDeviceLinkTests/ProxySpecs/SDLSecondaryTransportManagerSpec.m index d520a367e..ac5c95f33 100644 --- a/SmartDeviceLinkTests/ProxySpecs/SDLSecondaryTransportManagerSpec.m +++ b/SmartDeviceLinkTests/ProxySpecs/SDLSecondaryTransportManagerSpec.m @@ -15,12 +15,15 @@ #import "SDLControlFramePayloadTransportEventUpdate.h" #import "SDLIAPTransport.h" #import "SDLNotificationConstants.h" +#import "SDLRPCNotificationNotification.h" #import "SDLProtocol.h" #import "SDLSecondaryTransportManager.h" #import "SDLStateMachine.h" #import "SDLTCPTransport.h" #import "SDLV2ProtocolMessage.h" #import "SDLFakeSecurityManager.h" +#import "SDLHMILevel.h" +#import "SDLOnHMIStatus.h" /* copied from SDLSecondaryTransportManager.m */ typedef NSNumber SDLServiceTypeBox; @@ -55,7 +58,7 @@ static const int TCPPortUnspecified = -1; @property (strong, nonatomic) NSMutableDictionary<SDLServiceTypeBox *, SDLTransportClassBox *> *streamingServiceTransportMap; @property (strong, nonatomic, nullable) NSString *ipAddress; @property (assign, nonatomic) int tcpPort; -@property (assign, nonatomic, getter=isAppReady) BOOL appReady; +@property (assign, nonatomic) BOOL shouldOpenConnection; @end @@ -137,6 +140,15 @@ describe(@"the secondary transport manager ", ^{ __block id<SDLTransportType> testPrimaryTransport = nil; __block id testStreamingProtocolDelegate = nil; + __block void (^sendNotificationForHMILevel)(SDLHMILevel hmiLevel) = ^(SDLHMILevel hmiLevel) { + SDLOnHMIStatus *hmiStatus = [[SDLOnHMIStatus alloc] init]; + hmiStatus.hmiLevel = hmiLevel; + SDLRPCNotificationNotification *notification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidChangeHMIStatusNotification object:self rpcNotification:hmiStatus]; + [[NSNotificationCenter defaultCenter] postNotification:notification]; + + [NSThread sleepForTimeInterval:0.3]; + }; + beforeEach(^{ [SDLSecondaryTransportManager swapGetAppStateMethod]; [SDLTCPTransport swapConnectionMethods]; @@ -413,7 +425,7 @@ describe(@"the secondary transport manager ", ^{ testStartServiceACKPayload = [[SDLControlFramePayloadRPCStartServiceAck alloc] initWithHashId:testHashId mtu:testMtu authToken:nil protocolVersion:testProtocolVersion secondaryTransports:testSecondaryTransports audioServiceTransports:testAudioServiceTransports videoServiceTransports:testVideoServiceTransports]; testStartServiceACKMessage = [[SDLV2ProtocolMessage alloc] initWithHeader:testStartServiceACKHeader andPayload:testStartServiceACKPayload.data]; - manager.appReady = YES; + manager.shouldOpenConnection = YES; }); it(@"should configure its properties and immediately transition to Connecting state", ^{ @@ -507,27 +519,23 @@ describe(@"the secondary transport manager ", ^{ }); }); - context(@"before the security manager is set by register app interface response", ^{ + context(@"before the app context is HMI FULL", ^{ it(@"should stay in state Configured", ^{ expect(manager.stateMachine.currentState).to(equal(SDLSecondaryTransportStateConfigured)); - expect(manager.secondaryProtocol.securityManager).to(beNil()); - expect(manager.isAppReady).to(equal(NO)); + expect(manager.shouldOpenConnection).to(equal(NO)); OCMVerifyAll(testStreamingProtocolDelegate); }); }); - context(@"after the security manager is set by register app interface response", ^{ + context(@"app becomes HMI FULL", ^{ beforeEach(^{ - testPrimaryProtocol.securityManager = OCMClassMock([SDLFakeSecurityManager class]); - // By the time this notification is recieved the RAIR should have been sent and the security manager should exist if available - [[NSNotificationCenter defaultCenter] postNotificationName:SDLDidBecomeReady object:nil]; + sendNotificationForHMILevel(SDLHMILevelFull); }); it(@"should transition to Connecting state", ^{ expect(manager.stateMachine.currentState).to(equal(SDLSecondaryTransportStateConnecting)); - expect(manager.secondaryProtocol.securityManager).to(equal(testPrimaryProtocol.securityManager)); - expect(manager.isAppReady).to(equal(YES)); + expect(manager.shouldOpenConnection).to(equal(YES)); OCMVerifyAll(testStreamingProtocolDelegate); }); @@ -561,7 +569,7 @@ describe(@"the secondary transport manager ", ^{ }); }); - describe(@"and Transport Event Update is received", ^{ + describe(@"and a Transport Event Update has been received", ^{ __block SDLProtocolHeader *testTransportEventUpdateHeader = nil; __block SDLProtocolMessage *testTransportEventUpdateMessage = nil; __block SDLControlFramePayloadTransportEventUpdate *testTransportEventUpdatePayload = nil; @@ -584,24 +592,23 @@ describe(@"the secondary transport manager ", ^{ }); - context(@"before the security manager is set by register app interface response", ^{ + context(@"before the app context is HMI FULL", ^{ it(@"should stay in Configured state", ^{ expect(manager.stateMachine.currentState).to(equal(SDLSecondaryTransportStateConfigured)); - expect(manager.secondaryProtocol.securityManager).to(beNil()); + expect(manager.shouldOpenConnection).to(equal(NO)); + OCMVerifyAll(testStreamingProtocolDelegate); }); }); - context(@"after the security manager is set by register app interface response", ^{ + context(@"app becomes HMI FULL", ^{ beforeEach(^{ - testPrimaryProtocol.securityManager = OCMClassMock([SDLFakeSecurityManager class]); - // By the time this notification is recieved the RAIR should have been sent and the security manager should exist if available - [[NSNotificationCenter defaultCenter] postNotificationName:SDLDidBecomeReady object:nil]; + sendNotificationForHMILevel(SDLHMILevelFull); }); it(@"should transition to Connecting", ^{ expect(manager.stateMachine.currentState).to(equal(SDLSecondaryTransportStateConnecting)); - expect(manager.secondaryProtocol.securityManager).to(equal(testPrimaryProtocol.securityManager)); + expect(manager.shouldOpenConnection).to(equal(YES)); OCMVerifyAll(testStreamingProtocolDelegate); }); @@ -731,7 +738,6 @@ describe(@"the secondary transport manager ", ^{ [NSThread sleepForTimeInterval:0.1]; expect(manager.stateMachine.currentState).to(equal(SDLSecondaryTransportStateReconnecting)); - expect(manager.isAppReady).to(equal(NO)); OCMVerifyAll(testStreamingProtocolDelegate); }); }); @@ -776,7 +782,7 @@ describe(@"the secondary transport manager ", ^{ beforeEach(^{ testTcpIpAddress = @"172.16.12.34"; testTcpPort = 12345; - manager.appReady = YES; + manager.shouldOpenConnection = YES; testTransportEventUpdatePayload = [[SDLControlFramePayloadTransportEventUpdate alloc] initWithTcpIpAddress:testTcpIpAddress tcpPort:testTcpPort]; testTransportEventUpdateMessage = [[SDLV2ProtocolMessage alloc] initWithHeader:testTransportEventUpdateHeader andPayload:testTransportEventUpdatePayload.data]; @@ -864,7 +870,7 @@ describe(@"the secondary transport manager ", ^{ manager.secondaryTransportType = SDLTransportSelectionTCP; manager.ipAddress = @"192.168.1.1"; manager.tcpPort = 12345; - manager.appReady = YES; + manager.shouldOpenConnection = YES; testTransportEventUpdateHeader = [SDLProtocolHeader headerForVersion:5]; testTransportEventUpdateHeader.frameType = SDLFrameTypeControl; @@ -1023,7 +1029,6 @@ describe(@"the secondary transport manager ", ^{ manager.secondaryTransportType = SDLTransportSelectionTCP; manager.ipAddress = @"192.168.1.1"; manager.tcpPort = 12345; - manager.appReady = YES; testTransportEventUpdateHeader = [SDLProtocolHeader headerForVersion:5]; testTransportEventUpdateHeader.frameType = SDLFrameTypeControl; @@ -1053,6 +1058,7 @@ describe(@"the secondary transport manager ", ^{ beforeEach(^{ testTcpIpAddress = @"172.16.12.34"; testTcpPort = 12345; + manager.shouldOpenConnection = YES; testTransportEventUpdatePayload = [[SDLControlFramePayloadTransportEventUpdate alloc] initWithTcpIpAddress:testTcpIpAddress tcpPort:testTcpPort]; testTransportEventUpdateMessage = [[SDLV2ProtocolMessage alloc] initWithHeader:testTransportEventUpdateHeader andPayload:testTransportEventUpdatePayload.data]; |