diff options
author | NicoleYarroch <nicole@livio.io> | 2019-05-22 16:48:32 -0400 |
---|---|---|
committer | NicoleYarroch <nicole@livio.io> | 2019-05-22 16:48:32 -0400 |
commit | 762e2a3e0409c3a6e4b1da682c75b67499123833 (patch) | |
tree | 76d99cb2c53b4e31090dba1a0ab8005cdfb2ace2 | |
parent | e46468c8698f660150ad9f9db2296c1ec36c5527 (diff) | |
download | sdl_ios-762e2a3e0409c3a6e4b1da682c75b67499123833.tar.gz |
Fixed broken iAP classes test cases
4 files changed, 110 insertions, 464 deletions
diff --git a/SmartDeviceLinkTests/TransportSpecs/iAP/SDLIAPControlSessionSpec.m b/SmartDeviceLinkTests/TransportSpecs/iAP/SDLIAPControlSessionSpec.m index 5a389b16c..113c06f5f 100644 --- a/SmartDeviceLinkTests/TransportSpecs/iAP/SDLIAPControlSessionSpec.m +++ b/SmartDeviceLinkTests/TransportSpecs/iAP/SDLIAPControlSessionSpec.m @@ -29,155 +29,35 @@ QuickSpecBegin(SDLIAPControlSessionSpec) describe(@"SDLIAPControlSession", ^{ __block SDLIAPControlSession *controlSession = nil; __block EAAccessory *mockAccessory = nil; - __block SDLIAPSession *mockSession = nil; __block id<SDLIAPControlSessionDelegate> mockDelegate = nil; beforeEach(^{ mockDelegate = OCMProtocolMock(@protocol(SDLIAPControlSessionDelegate)); mockAccessory = [EAAccessory.class sdlCoreMock]; - mockSession = OCMClassMock([SDLIAPSession class]); - OCMStub([mockSession accessory]).andReturn(mockAccessory); }); describe(@"init", ^{ - context(@"with a valid session", ^{ - beforeEach(^{ - controlSession = [[SDLIAPControlSession alloc] initWithSession:mockSession delegate:mockDelegate]; - }); - - it(@"Should get/set correctly", ^{ - expect(controlSession.session).toNot(beNil()); - expect(controlSession.connectionID).to(equal(5)); - expect(controlSession.delegate).to(equal(mockDelegate)); - }); - - describe(@"When checking if the session is in progress", ^{ - it(@"Should not be in progress if the session is stopped", ^{ - OCMStub([mockSession isStopped]).andReturn(NO); - expect(controlSession.isSessionInProgress).to(beTrue()); - }); - - it(@"Should be in progress if the session is running", ^{ - OCMStub([mockSession isStopped]).andReturn(YES); - expect(controlSession.isSessionInProgress).to(beFalse()); - }); - }); + beforeEach(^{ + controlSession = [[SDLIAPControlSession alloc] initWithAccessory:mockAccessory delegate:mockDelegate]; }); - context(@"with a nil session", ^{ - beforeEach(^{ - controlSession = [[SDLIAPControlSession alloc] initWithSession:nil delegate:mockDelegate]; - }); - - it(@"Should get/set correctly", ^{ - expect(controlSession.session).to(beNil()); - expect(controlSession.connectionID).to(equal(0)); - expect(controlSession.delegate).to(equal(mockDelegate)); - }); - - describe(@"When checking if the session is in progress", ^{ - it(@"Should not be in progress if the session is stopped because the session is `nil`", ^{ - OCMStub([mockSession isStopped]).andReturn(NO); - expect(controlSession.isSessionInProgress).to(beFalse()); - }); - - it(@"Should be in progress if the session is running becasue the session is `nil`", ^{ - OCMStub([mockSession isStopped]).andReturn(YES); - expect(controlSession.isSessionInProgress).to(beFalse()); - }); - }); + it(@"Should get/set correctly", ^{ + expect(controlSession.accessory).to(equal(mockAccessory)); + expect(controlSession.protocolString).to(equal(ControlProtocolString)); + expect(controlSession.protocolIndexTimer).to(beNil()); + expect(controlSession.delegate).to(equal(mockDelegate)); }); }); - describe(@"Starting a session", ^{ - describe(@"When a session starts successfully", ^{ + describe(@"starting a session", ^{ + context(@"it should attempt to retry the session", ^{ beforeEach(^{ - OCMStub([mockSession start]).andReturn(YES); - controlSession = [[SDLIAPControlSession alloc] initWithSession:mockSession delegate:mockDelegate]; - [controlSession startSession]; - }); - - it(@"Should create a timer", ^{ - expect(controlSession.protocolIndexTimer).toNot(beNil()); + controlSession = [[SDLIAPControlSession alloc] initWithAccessory:nil delegate:mockDelegate]; }); - it(@"Should not try to establish a new session", ^{ - OCMReject([mockDelegate retryControlSession]); - OCMReject([mockDelegate controlSession:[OCMArg any] didGetProtocolString:[OCMArg any] forConnectedAccessory:[OCMArg any]]); - }); - }); - - describe(@"When a session does not start successfully", ^{ - beforeEach(^{ - OCMStub([mockSession start]).andReturn(NO); - controlSession = [[SDLIAPControlSession alloc] initWithSession:mockSession delegate:mockDelegate]; + it(@"Should start correctly", ^{ [controlSession startSession]; - }); - - it(@"Should not create a timer", ^{ - expect(controlSession.protocolIndexTimer).to(beNil()); - }); - - it(@"Should try to establish a new session", ^{ - OCMVerify([mockDelegate retryControlSession]); - OCMReject([mockDelegate controlSession:[OCMArg any] didGetProtocolString:[OCMArg any] forConnectedAccessory:[OCMArg any]]); - }); - - it(@"Should should stop and destroy the session", ^{ - expect(controlSession.session).to(beNil()); - expect(controlSession.connectionID).to(equal(0)); - }); - - it(@"Should should stop but not destroy the session", ^{ - OCMVerify([mockSession stop]); - }); - }); - - describe(@"When a session can not be started because the session is nil", ^{ - beforeEach(^{ - controlSession = [[SDLIAPControlSession alloc] initWithSession:nil delegate:mockDelegate]; - [controlSession startSession]; - }); - - it(@"Should not create a timer", ^{ - expect(controlSession.protocolIndexTimer).to(beNil()); - }); - - it(@"Should return a connectionID of zero", ^{ - expect(controlSession.connectionID).to(equal(0)); - }); - - it(@"Should try to establish a new session", ^{ - OCMVerify([mockDelegate retryControlSession]); - OCMReject([mockDelegate controlSession:[OCMArg any] didGetProtocolString:[OCMArg any] forConnectedAccessory:[OCMArg any]]); - }); - }); - }); - - describe(@"Stopping a session", ^{ - context(@"That is nil", ^{ - beforeEach(^{ - mockSession = nil; - controlSession = [[SDLIAPControlSession alloc] initWithSession:mockSession delegate:mockDelegate]; - [controlSession destroySession]; - }); - - it(@"Should not try to stop the session", ^{ - expect(controlSession.session).to(beNil()); - OCMReject([mockSession stop]); - }); - }); - - context(@"That is started", ^{ - beforeEach(^{ - controlSession = [[SDLIAPControlSession alloc] initWithSession:mockSession delegate:mockDelegate]; - [controlSession destroySession]; - }); - - it(@"Should try to stop the session", ^{ - expect(controlSession.session).to(beNil()); - expect(controlSession.session.streamDelegate).to(beNil()); - OCMVerify([mockSession stop]); + OCMExpect([mockDelegate retryControlSession]); }); }); }); diff --git a/SmartDeviceLinkTests/TransportSpecs/iAP/SDLIAPDataSessionSpec.m b/SmartDeviceLinkTests/TransportSpecs/iAP/SDLIAPDataSessionSpec.m index 534457160..84e741f7b 100644 --- a/SmartDeviceLinkTests/TransportSpecs/iAP/SDLIAPDataSessionSpec.m +++ b/SmartDeviceLinkTests/TransportSpecs/iAP/SDLIAPDataSessionSpec.m @@ -16,9 +16,15 @@ #import "SDLIAPConstants.h" #import "SDLIAPSession.h" #import "SDLIAPDataSessionDelegate.h" +#import "SDLMutableDataQueue.h" + @interface SDLIAPDataSession() + @property (weak, nonatomic) id<SDLIAPDataSessionDelegate> delegate; +@property (nonatomic, strong) SDLMutableDataQueue *sendDataQueue; +@property (nonatomic, strong) dispatch_semaphore_t canceledSemaphore; + @end QuickSpecBegin(SDLIAPDataSessionSpec) @@ -26,139 +32,42 @@ QuickSpecBegin(SDLIAPDataSessionSpec) describe(@"SDLIAPDataSession", ^{ __block SDLIAPDataSession *dataSession = nil; __block EAAccessory *mockAccessory = nil; - __block SDLIAPSession *mockSession = nil; __block id<SDLIAPDataSessionDelegate> mockDelegate = nil; beforeEach(^{ mockDelegate = OCMProtocolMock(@protocol(SDLIAPDataSessionDelegate)); mockAccessory = [EAAccessory.class sdlCoreMock]; - mockSession = OCMClassMock([SDLIAPSession class]); - OCMStub([mockSession accessory]).andReturn(mockAccessory); }); describe(@"init", ^{ - context(@"with a valid session", ^{ - beforeEach(^{ - dataSession = [[SDLIAPDataSession alloc] initWithSession:mockSession delegate:mockDelegate]; - }); - - it(@"Should get/set correctly", ^{ - expect(dataSession.session).toNot(beNil()); - expect(dataSession.connectionID).to(equal(5)); - expect(dataSession.delegate).to(equal(mockDelegate)); - }); - - describe(@"When checking if the session is in progress", ^{ - it(@"Should not be in progress if the session is stopped", ^{ - OCMStub([mockSession isStopped]).andReturn(NO); - expect(dataSession.isSessionInProgress).to(beTrue()); - }); - - it(@"Should be in progress if the session is running", ^{ - OCMStub([mockSession isStopped]).andReturn(YES); - expect(dataSession.isSessionInProgress).to(beFalse()); - }); - }); + beforeEach(^{ + dataSession = [[SDLIAPDataSession alloc] initWithAccessory:mockAccessory delegate:mockDelegate forProtocol:MultiSessionProtocolString]; }); - context(@"with a nil session", ^{ - beforeEach(^{ - dataSession = [[SDLIAPDataSession alloc] initWithSession:nil delegate:mockDelegate]; - }); - - it(@"Should get/set correctly", ^{ - expect(dataSession.session).to(beNil()); - expect(dataSession.connectionID).to(equal(0)); - expect(dataSession.delegate).to(equal(mockDelegate)); - }); - - describe(@"When checking if the session is in progress", ^{ - it(@"Should not be in progress if the session is stopped because the session is `nil`", ^{ - OCMStub([mockSession isStopped]).andReturn(NO); - expect(dataSession.isSessionInProgress).to(beFalse()); - }); - - it(@"Should be in progress if the session is running becasue the session is `nil`", ^{ - OCMStub([mockSession isStopped]).andReturn(YES); - expect(dataSession.isSessionInProgress).to(beFalse()); - }); - }); + it(@"Should init correctly", ^{ + expect(dataSession.delegate).to(equal(mockDelegate)); + expect(dataSession.sendDataQueue).toNot(beNil()); + expect(dataSession.canceledSemaphore).toNot(beNil()); }); - }); - - describe(@"Starting a session", ^{ - xdescribe(@"When a session starts successfully", ^{ - beforeEach(^{ - OCMStub([mockSession start]).andReturn(YES); - dataSession = [[SDLIAPDataSession alloc] initWithSession:mockSession delegate:mockDelegate]; - [dataSession startSession]; - }); - - it(@"Should not try to establish a new session", ^{ - OCMReject([mockDelegate retryDataSession]); - OCMReject([mockDelegate dataReceived:[OCMArg any]]); - }); - }); - - describe(@"When a session does not start successfully", ^{ - beforeEach(^{ - OCMStub([mockSession start]).andReturn(NO); - dataSession = [[SDLIAPDataSession alloc] initWithSession:mockSession delegate:mockDelegate]; - [dataSession startSession]; - }); - - it(@"Should try to establish a new session", ^{ - OCMVerify([mockDelegate retryDataSession]); - OCMReject([mockDelegate dataReceived:[OCMArg any]]); - }); - - it(@"Should should stop and destroy the session", ^{ - expect(dataSession.session).to(beNil()); - expect(dataSession.connectionID).to(equal(0)); - OCMVerify([mockSession stop]); - }); - }); - - describe(@"When a session can not be started because the session is nil", ^{ - beforeEach(^{ - dataSession = [[SDLIAPDataSession alloc] initWithSession:nil delegate:mockDelegate]; - [dataSession startSession]; - }); - - it(@"Should return a connectionID of zero", ^{ - expect(dataSession.connectionID).to(equal(0)); - }); - - it(@"Should try to establish a new session", ^{ - OCMVerify([mockDelegate retryDataSession]); - OCMReject([mockDelegate dataReceived:[OCMArg any]]); - }); + it(@"Should get correctly", ^{ + expect(dataSession.accessory).to(equal(mockAccessory)); + expect(dataSession.protocolString).to(equal(MultiSessionProtocolString)); + expect(dataSession.isStopped).to(beTrue()); + expect(dataSession.connectionID).to(equal(0)); + expect(dataSession.sessionInProgress).to(beFalse()); }); }); - describe(@"Stopping a session", ^{ - context(@"That is nil", ^{ - beforeEach(^{ - dataSession = [[SDLIAPDataSession alloc] initWithSession:nil delegate:mockDelegate]; - [dataSession destroySession]; - }); - - it(@"Should not try to stop the session", ^{ - expect(dataSession.session).to(beNil()); - }); - }); - - context(@"That is started", ^{ + describe(@"starting a session", ^{ + context(@"it should attempt to retry the session", ^{ beforeEach(^{ - dataSession = [[SDLIAPDataSession alloc] initWithSession:mockSession delegate:mockDelegate]; - [dataSession destroySession]; + dataSession = [[SDLIAPDataSession alloc] initWithAccessory:nil delegate:mockDelegate forProtocol:MultiSessionProtocolString]; }); - it(@"Should try to stop and detroy the session", ^{ - expect(dataSession.session).to(beNil()); - expect(dataSession.session.streamDelegate).to(beNil()); - OCMVerify([mockSession stop]); + it(@"Should start correctly", ^{ + [dataSession startSession]; + OCMExpect([mockDelegate retryDataSession]); }); }); }); diff --git a/SmartDeviceLinkTests/TransportSpecs/iAP/SDLIAPSessionSpec.m b/SmartDeviceLinkTests/TransportSpecs/iAP/SDLIAPSessionSpec.m index 1e0992379..17984383f 100644 --- a/SmartDeviceLinkTests/TransportSpecs/iAP/SDLIAPSessionSpec.m +++ b/SmartDeviceLinkTests/TransportSpecs/iAP/SDLIAPSessionSpec.m @@ -12,150 +12,40 @@ #import "EAAccessory+OCMock.m" #import "SDLIAPSession.h" -#import "SDLMutableDataQueue.h" -#import "SDLStreamDelegate.h" +#import "SDLIAPConstants.h" @interface SDLIAPSession () + @property (nonatomic, assign) BOOL isInputStreamOpen; @property (nonatomic, assign) BOOL isOutputStreamOpen; -@property (nullable, nonatomic, strong) NSThread *ioStreamThread; -@property (nonatomic, strong) SDLMutableDataQueue *sendDataQueue; -@property (nonatomic, strong) dispatch_semaphore_t canceledSemaphore; -- (BOOL)sdl_startWithSession:(EASession *)session; + @end + QuickSpecBegin(SDLIAPSessionSpec) describe(@"SDLIAPSession", ^{ - __block SDLIAPSession *iapSession = nil; __block EAAccessory *mockAccessory = nil; - __block NSString *protocol = nil; describe(@"Initialization", ^{ + __block SDLIAPSession *testSession = nil; + beforeEach(^{ mockAccessory = [EAAccessory.class sdlCoreMock]; + testSession = [[SDLIAPSession alloc] initWithAccessory:mockAccessory forProtocol:ControlProtocolString]; }); - it(@"Should init correctly with a control protocol string", ^{ - protocol = @"com.smartdevicelink.prot0"; - iapSession = [[SDLIAPSession alloc] initWithAccessory:mockAccessory forProtocol:protocol]; - - expect(iapSession.isDataSession).to(beFalse()); - }); - - it(@"Should init correctly with a multisession protocol string", ^{ - protocol = @"com.smartdevicelink.multisession"; - iapSession = [[SDLIAPSession alloc] initWithAccessory:mockAccessory forProtocol:protocol]; - - expect(iapSession.isDataSession).to(beTrue()); - }); - - it(@"Should init correctly with a legacy protocol string", ^{ - protocol = @"com.ford.sync.prot0"; - iapSession = [[SDLIAPSession alloc] initWithAccessory:mockAccessory forProtocol:protocol]; - - expect(iapSession.isDataSession).to(beTrue()); - }); - - it(@"Should init correctly with a indexed protocol string", ^{ - protocol = @"com.smartdevicelink.prot1"; - iapSession = [[SDLIAPSession alloc] initWithAccessory:mockAccessory forProtocol:protocol]; - - expect(iapSession.isDataSession).to(beTrue()); - }); - - afterEach(^{ - expect(iapSession).toNot(beNil()); - expect(iapSession.protocolString).to(match(protocol)); - expect(iapSession.accessory).to(equal(mockAccessory)); - expect(iapSession.canceledSemaphore).toNot(beNil()); - expect(iapSession.sendDataQueue).toNot(beNil()); - expect(iapSession.isInputStreamOpen).to(beFalse()); - expect(iapSession.isOutputStreamOpen).to(beFalse()); + it(@"should init correctly", ^{ + expect(testSession.accessory).to(equal(mockAccessory)); + expect(testSession.protocolString).to(equal(ControlProtocolString)); + expect(testSession.isInputStreamOpen).to(beFalse()); + expect(testSession.isOutputStreamOpen).to(beFalse()); }); - }); - - describe(@"When starting a session", ^{ - __block SDLStreamDelegate *streamDelegate = nil; - __block NSInputStream *inputStream = nil; - __block NSOutputStream *outputStream = nil; - - describe(@"unsuccessfully", ^{ - beforeEach(^{ - protocol = @"com.smartdevicelink.multisession"; - mockAccessory = [EAAccessory.class sdlCoreMock]; - iapSession = [[SDLIAPSession alloc] initWithAccessory:mockAccessory forProtocol:protocol]; - streamDelegate = [[SDLStreamDelegate alloc] init]; - iapSession.streamDelegate = streamDelegate; - }); - - it(@"the start method should return false if a session cannot be created", ^{ - BOOL success = [iapSession sdl_startWithSession:nil]; - expect(success).to(beFalse()); - expect(iapSession.ioStreamThread).to(beNil()); - expect(iapSession.isInputStreamOpen).to(beFalse()); - expect(iapSession.isOutputStreamOpen).to(beFalse()); - }); - }); - - describe(@"successfully", ^{ - beforeEach(^{ - inputStream = OCMClassMock([NSInputStream class]); - outputStream = OCMClassMock([NSOutputStream class]); - }); - - context(@"if creating a control session", ^{ - beforeEach(^{ - protocol = @"com.smartdevicelink.prot0"; - mockAccessory = [EAAccessory.class sdlCoreMock]; - iapSession = [[SDLIAPSession alloc] initWithAccessory:mockAccessory forProtocol:protocol]; - streamDelegate = [[SDLStreamDelegate alloc] init]; - iapSession.streamDelegate = streamDelegate; - - expect(iapSession.isDataSession).to(beFalse()); - }); - - it(@"should establish a control session ", ^{ - EASession *mockSession = [EASession.class mockSessionWithAccessory:mockAccessory protocolString:protocol inputStream:inputStream outputStream:outputStream]; - iapSession.easession = mockSession; - - BOOL success = [iapSession sdl_startWithSession:mockSession]; - expect(success).to(beTrue()); - expect(iapSession.ioStreamThread).to(beNil()); - expect(iapSession.easession.inputStream).toNot(beNil()); - expect(iapSession.easession.outputStream).toNot(beNil()); - }); - }); - - context(@"if creating a data session", ^{ - beforeEach(^{ - protocol = @"com.smartdevicelink.multisession"; - mockAccessory = [EAAccessory.class sdlCoreMock]; - iapSession = [[SDLIAPSession alloc] initWithAccessory:mockAccessory forProtocol:protocol]; - streamDelegate = [[SDLStreamDelegate alloc] init]; - iapSession.streamDelegate = streamDelegate; - - expect(iapSession.isDataSession).to(beTrue()); - }); - - it(@"should establish a data session ", ^{ - EASession *mockSession = [EASession.class mockSessionWithAccessory:mockAccessory protocolString:protocol inputStream:inputStream outputStream:outputStream]; - iapSession.easession = mockSession; - BOOL success = [iapSession sdl_startWithSession:mockSession]; - expect(success).to(beTrue()); - expect(iapSession.ioStreamThread).toNot(beNil()); - // The streams are opened in the `sdl_streamHasSpaceHandler` method - expect(iapSession.easession.inputStream).toNot(beNil()); - expect(iapSession.easession.outputStream).toNot(beNil()); - }); - }); - }); - - afterEach(^{ - [iapSession stop]; - expect(iapSession.easession).to(beNil()); - expect(iapSession.ioStreamThread).to(beNil()); + it(@"should get correctly", ^{ + expect(testSession.isStopped).to(beTrue()); + expect(testSession.connectionID).to(equal(0)); + expect(testSession.isSessionInProgress).to(beFalse()); }); }); }); diff --git a/SmartDeviceLinkTests/TransportSpecs/iAP/SDLIAPTransportSpec.m b/SmartDeviceLinkTests/TransportSpecs/iAP/SDLIAPTransportSpec.m index fc64cac00..e31a300e4 100644 --- a/SmartDeviceLinkTests/TransportSpecs/iAP/SDLIAPTransportSpec.m +++ b/SmartDeviceLinkTests/TransportSpecs/iAP/SDLIAPTransportSpec.m @@ -11,6 +11,7 @@ #import <OCMock/OCMock.h> #import "EAAccessory+OCMock.m" +#import "SDLIAPConstants.h" #import "SDLIAPTransport.h" #import "SDLIAPSession.h" #import "SDLTimer.h" @@ -26,6 +27,7 @@ @property (assign, nonatomic) int retryCounter; @property (assign, nonatomic) BOOL sessionSetupInProgress; +@property (assign, nonatomic) BOOL transportDisconnected; @property (nonatomic, assign) UIBackgroundTaskIdentifier backgroundTaskId; @property (assign, nonatomic) BOOL accessoryConnectDuringActiveSession; - (BOOL)sdl_isDataSessionActive:(nullable SDLIAPDataSession *)dataSession newAccessory:(EAAccessory *)newAccessory; @@ -57,62 +59,49 @@ describe(@"SDLIAPTransport", ^{ }); }); - describe(@"When an accessory connects while a data session has been created but not open", ^{ - beforeEach(^{ - transport.dataSession = OCMClassMock([SDLIAPDataSession class]); - OCMStub([transport.dataSession isSessionInProgress]).andReturn(NO); - - expect(transport.controlSession.session).to(beNil()); - }); + describe(@"When checking if the data session is active ", ^{ + it(@"should return that the data session is not active if the data session is nil", ^{ + transport.dataSession = nil; - it(@"should return that a session is not active", ^{ BOOL sessionInProgress = [transport sdl_isDataSessionActive:transport.dataSession newAccessory:mockAccessory]; expect(sessionInProgress).to(beFalse()); }); - }); - - describe(@"When an accessory connects when a data session is already open", ^{ - beforeEach(^{ - transport.dataSession = OCMClassMock([SDLIAPDataSession class]); - OCMStub([transport.dataSession isSessionInProgress]).andReturn(YES); - expect(transport.controlSession.session).to(beNil()); - }); + it(@"should return that the data session is not active if the data session is not inprogress", ^{ + SDLIAPDataSession *mockDataSession = OCMClassMock([SDLIAPDataSession class]); + OCMStub([mockDataSession isSessionInProgress]).andReturn(NO); - it(@"should return that a session is active", ^{ BOOL sessionInProgress = [transport sdl_isDataSessionActive:transport.dataSession newAccessory:mockAccessory]; - expect(sessionInProgress).to(beTrue()); + expect(sessionInProgress).to(beFalse()); }); - }); - describe(@"When an accessory connects when a data session has not been created", ^{ - beforeEach(^{ - transport.dataSession = nil; + it(@"should return that the data session is not active if the data session and the new accessory have matching connection ids", ^{ + SDLIAPDataSession *mockDataSession = OCMClassMock([SDLIAPDataSession class]); + OCMStub([mockDataSession isSessionInProgress]).andReturn(YES); + OCMStub([mockDataSession connectionID]).andReturn(5); - expect(transport.controlSession.session).to(beNil()); + BOOL sessionInProgress = [transport sdl_isDataSessionActive:mockDataSession newAccessory:mockAccessory]; + expect(sessionInProgress).to(beFalse()); }); - it(@"should return that a session is active", ^{ - BOOL sessionInProgress = [transport sdl_isDataSessionActive:transport.dataSession newAccessory:mockAccessory]; - expect(sessionInProgress).to(beFalse()); + it(@"should return that the data session is active if the data session and the new accessory have different connection ids", ^{ + SDLIAPDataSession *mockDataSession = OCMClassMock([SDLIAPDataSession class]); + OCMStub([mockDataSession isSessionInProgress]).andReturn(YES); + OCMStub([mockDataSession connectionID]).andReturn(96); + + BOOL sessionInProgress = [transport sdl_isDataSessionActive:mockDataSession newAccessory:mockAccessory]; + expect(sessionInProgress).to(beTrue()); }); }); describe(@"When an accessory disconnects", ^{ - __block SDLIAPSession *mockSession = nil; __block NSNotification *accessoryDisconnectedNotification = nil; - __block id<SDLIAPDataSessionDelegate> mockDelegate = nil; beforeEach(^{ - transport.controlSession = nil; - mockSession = OCMClassMock([SDLIAPSession class]); - OCMStub([mockSession accessory]).andReturn(mockAccessory); - transport.dataSession = [[SDLIAPDataSession alloc] initWithSession:mockSession delegate:mockDelegate]; - accessoryDisconnectedNotification = [[NSNotification alloc] initWithName:EAAccessoryDidDisconnectNotification object:nil userInfo:@{EAAccessoryKey: mockAccessory}]; }); - describe(@"If an accessory connected during an active session", ^{ + context(@"If an accessory disconnects during an active session", ^{ beforeEach(^{ transport.accessoryConnectDuringActiveSession = YES; [[NSNotificationCenter defaultCenter] postNotification:accessoryDisconnectedNotification]; @@ -123,7 +112,7 @@ describe(@"SDLIAPTransport", ^{ }); }); - describe(@"If no accessory connected during an active session", ^{ + context(@"If an accessory disconnects while no session is active", ^{ beforeEach(^{ transport.accessoryConnectDuringActiveSession = NO; [[NSNotificationCenter defaultCenter] postNotification:accessoryDisconnectedNotification]; @@ -134,94 +123,72 @@ describe(@"SDLIAPTransport", ^{ }); }); - describe(@"When a data session is open", ^{ + context(@"When a neither a data or control session is open", ^{ beforeEach(^{ + transport.dataSession = nil; + transport.controlSession = nil; + [[NSNotificationCenter defaultCenter] postNotification:accessoryDisconnectedNotification]; }); - it(@"It should close and destroy the open data session", ^{ - expect(transport.dataSession).toNot(beNil()); - expect(transport.dataSession.session).to(beNil()); - expect(transport.dataSession.session.delegate).to(beNil()); - - expect(transport.sessionSetupInProgress).to(beFalse()); + it(@"It should cleanup on disconnect", ^{ expect(transport.retryCounter).to(equal(0)); - expect(transport.controlSession).to(beNil()); - - OCMVerify([mockTransportDelegate onTransportDisconnected]); - OCMVerify([mockSession stop]); + expect(transport.sessionSetupInProgress).to(beFalse()); + expect(transport.transportDisconnected).to(beFalse()); }); }); - describe(@"When a control session is open", ^{ - __block SDLIAPSession *mockControlSession = nil; - __block NSNotification *accessoryDisconnectedNotification = nil; - __block id<SDLIAPControlSessionDelegate> mockDelegate = nil; + context(@"When a data session is open", ^{ + __block SDLIAPDataSession *mockDataSession = nil; beforeEach(^{ - mockDelegate = OCMProtocolMock(@protocol(SDLIAPControlSessionDelegate)); - mockControlSession = OCMClassMock([SDLIAPSession class]); - OCMStub([mockControlSession accessory]).andReturn(mockAccessory); - transport.controlSession = [[SDLIAPControlSession alloc] initWithSession:mockControlSession delegate:mockDelegate]; - - transport.dataSession = nil; - transport.sessionSetupInProgress = YES; - transport.retryCounter = 1; - accessoryDisconnectedNotification = [[NSNotification alloc] initWithName:EAAccessoryDidDisconnectNotification object:nil userInfo:@{EAAccessoryKey: mockAccessory}]; - }); + mockDataSession = OCMClassMock([SDLIAPDataSession class]); + OCMStub([mockDataSession isSessionInProgress]).andReturn(YES); + transport.dataSession = mockDataSession; + transport.controlSession = nil; - it(@"Should not tell the delegate that the transport closed", ^{ - OCMReject([mockTransportDelegate onTransportDisconnected]); [[NSNotificationCenter defaultCenter] postNotification:accessoryDisconnectedNotification]; }); - it(@"It should reset the setup helpers", ^{ - [[NSNotificationCenter defaultCenter] postNotification:accessoryDisconnectedNotification]; - + it(@"It should cleanup on disconnect", ^{ expect(transport.retryCounter).to(equal(0)); expect(transport.sessionSetupInProgress).to(beFalse()); + expect(transport.transportDisconnected).to(beTrue()); }); - it(@"It should close and destroy the open control session", ^{ - [[NSNotificationCenter defaultCenter] postNotification:accessoryDisconnectedNotification]; - - OCMVerify([mockControlSession stop]); + it(@"It should close and destroy data session", ^{ + OCMVerify([mockDataSession destroySession]); + }); - expect(transport.controlSession).toNot(beNil()); - expect(transport.controlSession.session).to(beNil()); - expect(transport.dataSession).to(beNil()); + it(@"It should notify the lifecycle manager that the transport disconnected ", ^{ + OCMVerify([mockTransportDelegate onTransportDisconnected]); }); }); - describe(@"When neither a control or data session has been established/opened", ^{ - __block NSNotification *accessoryDisconnectedNotification = nil; - __block SDLIAPControlSession *mockControlSession; - __block SDLIAPDataSession *mockDataSession; + describe(@"When a control session is open", ^{ + __block SDLIAPControlSession *mockControlSession = nil; beforeEach(^{ mockControlSession = OCMClassMock([SDLIAPControlSession class]); - OCMStub([mockControlSession isSessionInProgress]).andReturn(NO); + OCMStub([mockControlSession isSessionInProgress]).andReturn(YES); transport.controlSession = mockControlSession; + transport.dataSession = nil; - mockDataSession = OCMClassMock([SDLIAPDataSession class]); - OCMStub([mockDataSession isSessionInProgress]).andReturn(NO); - transport.dataSession = mockDataSession; - - transport.sessionSetupInProgress = YES; - transport.retryCounter = 1; - accessoryDisconnectedNotification = [[NSNotification alloc] initWithName:EAAccessoryDidDisconnectNotification object:nil userInfo:@{EAAccessoryKey: mockAccessory}]; - }); - - it(@"Should not tell the delegate that the transport closed", ^{ - OCMReject([mockTransportDelegate onTransportDisconnected]); [[NSNotificationCenter defaultCenter] postNotification:accessoryDisconnectedNotification]; }); - it(@"It should reset the setup helpers", ^{ - [[NSNotificationCenter defaultCenter] postNotification:accessoryDisconnectedNotification]; - + it(@"It should cleanup on disconnect", ^{ expect(transport.retryCounter).to(equal(0)); expect(transport.sessionSetupInProgress).to(beFalse()); + expect(transport.transportDisconnected).to(beFalse()); + }); + + it(@"It should close and destroy data session", ^{ + OCMVerify([mockControlSession destroySession]); + }); + + it(@"Should not tell the delegate that the transport closed", ^{ + OCMReject([mockTransportDelegate onTransportDisconnected]); }); }); }); |