diff options
author | Joel Fischer <joeljfischer@gmail.com> | 2020-11-10 16:00:31 -0500 |
---|---|---|
committer | Joel Fischer <joeljfischer@gmail.com> | 2020-11-10 16:00:31 -0500 |
commit | 11a6f4fde3a006326774c9f9801afa8a3bb030f5 (patch) | |
tree | a22383e020449578f92e1bb8dc53efc868d941e5 | |
parent | 0ff8887499cf489f09c3b3feda9255d8cf587306 (diff) | |
download | sdl_ios-11a6f4fde3a006326774c9f9801afa8a3bb030f5.tar.gz |
Fix cancelling operation before start not calling back properly
* Start on unit tests
-rw-r--r-- | SmartDeviceLink/SDLVoiceCommandUpdateOperation.h | 2 | ||||
-rw-r--r-- | SmartDeviceLink/SDLVoiceCommandUpdateOperation.m | 7 | ||||
-rw-r--r-- | SmartDeviceLinkTests/DevAPISpecs/SDLVoiceCommandOperationSpec.m | 93 |
3 files changed, 95 insertions, 7 deletions
diff --git a/SmartDeviceLink/SDLVoiceCommandUpdateOperation.h b/SmartDeviceLink/SDLVoiceCommandUpdateOperation.h index dae970988..1175da1b5 100644 --- a/SmartDeviceLink/SDLVoiceCommandUpdateOperation.h +++ b/SmartDeviceLink/SDLVoiceCommandUpdateOperation.h @@ -20,7 +20,7 @@ typedef void(^SDLVoiceCommandUpdateCompletionHandler)(NSArray<SDLVoiceCommand *> @property (strong, nonatomic, nullable) NSArray<SDLVoiceCommand *> *currentVoiceCommands; -- (instancetype)initWithConnectionManager:(id<SDLConnectionManagerType>)connectionManager newVoiceCommands:(nullable NSArray<SDLVoiceCommand *> *)newVoiceCommands oldVoiceCommands:(nullable NSArray<SDLVoiceCommand *> *)oldVoiceCommands updateCompletionHandler:(SDLVoiceCommandUpdateCompletionHandler)completionHandler; +- (instancetype)initWithConnectionManager:(id<SDLConnectionManagerType>)connectionManager newVoiceCommands:(NSArray<SDLVoiceCommand *> *)newVoiceCommands oldVoiceCommands:(NSArray<SDLVoiceCommand *> *)oldVoiceCommands updateCompletionHandler:(SDLVoiceCommandUpdateCompletionHandler)completionHandler; @end diff --git a/SmartDeviceLink/SDLVoiceCommandUpdateOperation.m b/SmartDeviceLink/SDLVoiceCommandUpdateOperation.m index a3c8b49e6..00af39062 100644 --- a/SmartDeviceLink/SDLVoiceCommandUpdateOperation.m +++ b/SmartDeviceLink/SDLVoiceCommandUpdateOperation.m @@ -35,7 +35,7 @@ NS_ASSUME_NONNULL_BEGIN @implementation SDLVoiceCommandUpdateOperation -- (instancetype)initWithConnectionManager:(id<SDLConnectionManagerType>)connectionManager newVoiceCommands:(nullable NSArray<SDLVoiceCommand *> *)newVoiceCommands oldVoiceCommands:(nullable NSArray<SDLVoiceCommand *> *)oldVoiceCommands updateCompletionHandler:(SDLVoiceCommandUpdateCompletionHandler)completionHandler { +- (instancetype)initWithConnectionManager:(id<SDLConnectionManagerType>)connectionManager newVoiceCommands:(NSArray<SDLVoiceCommand *> *)newVoiceCommands oldVoiceCommands:(NSArray<SDLVoiceCommand *> *)oldVoiceCommands updateCompletionHandler:(SDLVoiceCommandUpdateCompletionHandler)completionHandler { self = [self init]; if (!self) { return nil; } @@ -49,7 +49,10 @@ NS_ASSUME_NONNULL_BEGIN - (void)start { [super start]; - if (self.isCancelled) { return; } + if (self.isCancelled) { + [self finishOperation]; + return; + } __weak typeof(self) weakSelf = self; [self sdl_sendDeleteCurrentVoiceCommands:^(NSArray<SDLVoiceCommand *> *currentVoiceCommands, NSError * _Nullable error) { diff --git a/SmartDeviceLinkTests/DevAPISpecs/SDLVoiceCommandOperationSpec.m b/SmartDeviceLinkTests/DevAPISpecs/SDLVoiceCommandOperationSpec.m index e8fe9bd4a..6def3969e 100644 --- a/SmartDeviceLinkTests/DevAPISpecs/SDLVoiceCommandOperationSpec.m +++ b/SmartDeviceLinkTests/DevAPISpecs/SDLVoiceCommandOperationSpec.m @@ -5,10 +5,12 @@ #import "SDLAddCommand.h" #import "SDLAddCommandResponse.h" #import "SDLDeleteCommand.h" +#import "SDLDeleteCommandResponse.h" #import "SDLFileManager.h" #import "SDLHMILevel.h" #import "SDLVoiceCommand.h" #import "SDLVoiceCommandManager.h" +#import "SDLVoiceCommandUpdateOperation.h" #import "TestConnectionManager.h" @interface SDLVoiceCommand() @@ -20,33 +22,116 @@ QuickSpecBegin(SDLVoiceCommandOperationSpec) describe(@"a voice command operation", ^{ + __block TestConnectionManager *testConnectionManager = nil; + __block SDLVoiceCommandUpdateOperation *testOp = nil; + + __block SDLVoiceCommand *newVoiceCommand1 = [[SDLVoiceCommand alloc] initWithVoiceCommands:@[@"NewVC1"] handler:^{}]; + __block SDLVoiceCommand *newVoiceCommand2 = [[SDLVoiceCommand alloc] initWithVoiceCommands:@[@"NewVC2"] handler:^{}]; + __block SDLVoiceCommand *oldVoiceCommand1 = [[SDLVoiceCommand alloc] initWithVoiceCommands:@[@"OldVC1"] handler:^{}]; + __block SDLVoiceCommand *oldVoiceCommand2 = [[SDLVoiceCommand alloc] initWithVoiceCommands:@[@"OldVC2"] handler:^{}]; + + __block NSError *callbackError = nil; + __block NSArray<SDLVoiceCommand *> *callbackCurrentVoiceCommands = nil; + + beforeEach(^{ + newVoiceCommand1.commandId = 1; + newVoiceCommand2.commandId = 2; + oldVoiceCommand1.commandId = 3; + oldVoiceCommand2.commandId = 4; + + testConnectionManager = [[TestConnectionManager alloc] init]; + testOp = nil; + + callbackError = nil; + callbackCurrentVoiceCommands = nil; + }); + + // should have a priority of 'normal' it(@"should have a priority of 'normal'", ^{ + testOp = [[SDLVoiceCommandUpdateOperation alloc] initWithConnectionManager:testConnectionManager newVoiceCommands:@[] oldVoiceCommands:@[] updateCompletionHandler:^(NSArray<SDLVoiceCommand *> * _Nonnull newCurrentVoiceCommands, NSError * _Nullable error) { + }]; + expect(@(testOp.queuePriority)).to(equal(@(NSOperationQueuePriorityNormal))); }); + // initializing the operation describe(@"initializing the operation", ^{ + testOp = [[SDLVoiceCommandUpdateOperation alloc] initWithConnectionManager:testConnectionManager newVoiceCommands:@[newVoiceCommand1, newVoiceCommand2] oldVoiceCommands:@[oldVoiceCommand1, oldVoiceCommand2] updateCompletionHandler:^(NSArray<SDLVoiceCommand *> * _Nonnull newCurrentVoiceCommands, NSError * _Nullable error) {}]; + expect(testOp.currentVoiceCommands).to(equal(@[oldVoiceCommand1, oldVoiceCommand2])); }); + // starting the operation describe(@"starting the operation", ^{ + + // if it starts cancelled context(@"if it starts cancelled", ^{ - it(@"should return immediately with an error", ^{ + beforeEach(^{ + testOp = [[SDLVoiceCommandUpdateOperation alloc] initWithConnectionManager:testConnectionManager newVoiceCommands:@[newVoiceCommand1, newVoiceCommand2] oldVoiceCommands:@[oldVoiceCommand1, oldVoiceCommand2] updateCompletionHandler:^(NSArray<SDLVoiceCommand *> * _Nonnull newCurrentVoiceCommands, NSError * _Nullable error) { + callbackCurrentVoiceCommands = newCurrentVoiceCommands; + callbackError = error; + }]; + [testOp cancel]; + [testOp start]; + }); + it(@"should return immediately with an error", ^{ + expect(callbackError).toEventuallyNot(beNil()); }); }); context(@"if it has voice commands to delete", ^{ + beforeEach(^{ + testOp = [[SDLVoiceCommandUpdateOperation alloc] initWithConnectionManager:testConnectionManager newVoiceCommands:@[newVoiceCommand1, newVoiceCommand2] oldVoiceCommands:@[oldVoiceCommand1, oldVoiceCommand2] updateCompletionHandler:^(NSArray<SDLVoiceCommand *> * _Nonnull newCurrentVoiceCommands, NSError * _Nullable error) { + callbackCurrentVoiceCommands = newCurrentVoiceCommands; + callbackError = error; + }]; + [testOp start]; + }); + context(@"and the delete succeeds", ^{ - <#code#> + beforeEach(^{ + SDLDeleteCommandResponse *deleteOld1 = [[SDLDeleteCommandResponse alloc] init]; + deleteOld1.success = @YES; + deleteOld1.resultCode = SDLResultSuccess; + + SDLDeleteCommandResponse *deleteOld2 = [[SDLDeleteCommandResponse alloc] init]; + deleteOld2.success = @YES; + deleteOld2.resultCode = SDLResultSuccess; + + [testConnectionManager respondToRequestWithResponse:deleteOld1 requestNumber:0 error:nil]; + [testConnectionManager respondToRequestWithResponse:deleteOld2 requestNumber:1 error:nil]; + }); + + fit(@"should update the current voice commands and attempt to send the adds", ^{ + expect(testOp.currentVoiceCommands).to(haveCount(0)); + expect(testConnectionManager.receivedRequests).to(haveCount(4)); + }); }); context(@"and the delete fails", ^{ - <#code#> + + }); + + context(@"and the delete partially fails", ^{ + }); }); context(@"if it doesn't have any voice commands to delete", ^{ - <#code#> + context(@"adding voice commands", ^{ + context(@"and the add succeeds", ^{ + + }); + + context(@"and the add fails", ^{ + + }); + + context(@"and the add partially fails", ^{ + + }); + }); }); }); }); |