diff --git a/Foundation/CPOperation.j b/Foundation/CPOperation.j index 4fbdd5612..b0b8a6154 100644 --- a/Foundation/CPOperation.j +++ b/Foundation/CPOperation.j @@ -117,10 +117,11 @@ CPOperationQueuePriorityVeryHigh = 8; [self willChangeValueForKey:@"isExecuting"]; _executing = NO; [self didChangeValueForKey:@"isExecuting"]; - [self willChangeValueForKey:@"isFinished"]; - _finished = YES; - [self didChangeValueForKey:@"isFinished"]; } + + [self willChangeValueForKey:@"isFinished"]; + _finished = YES; + [self didChangeValueForKey:@"isFinished"]; } /*! diff --git a/Foundation/CPOperationQueue.j b/Foundation/CPOperationQueue.j index 1e018df15..de9322ce6 100644 --- a/Foundation/CPOperationQueue.j +++ b/Foundation/CPOperationQueue.j @@ -70,7 +70,7 @@ var cpOperationMainQueue = nil; for (; i < count; i++) { var op = [_operations objectAtIndex:i]; - if ([op isReady] && ![op isCancelled] && ![op isFinished] && ![op isExecuting]) + if ([op isReady] && ![op isFinished] && ![op isExecuting]) { [op start]; } @@ -260,7 +260,7 @@ var cpOperationMainQueue = nil; for (; i < count; i++) { var op = [ops objectAtIndex:i]; - if ([op isReady] && ![op isCancelled] && ![op isFinished] && ![op isExecuting]) + if ([op isReady] && ![op isFinished] && ![op isExecuting]) { [op start]; } diff --git a/Tests/Foundation/CPOperationQueueTest.j b/Tests/Foundation/CPOperationQueueTest.j index 0cc5de666..5005a1ae7 100644 --- a/Tests/Foundation/CPOperationQueueTest.j +++ b/Tests/Foundation/CPOperationQueueTest.j @@ -16,6 +16,33 @@ globalResults = []; @end +@implementation TestCancelOperation : CPOperation +{ + BOOL _started @accessors(getter=didStart); + BOOL _mained @accessors(getter=didMain); +} + +- (id)init +{ + self = [super init]; + _started = NO; + _mained = NO; + return self; +} + +- (void)main +{ + _mained = YES; +} + +- (void)start +{ + [super start]; + _started = YES; +} + +@end + @implementation TestObserver : CPObject { CPArray changedKeyPaths @accessors; @@ -167,4 +194,27 @@ globalResults = []; [self assert:@"name" equals:[[obs changedKeyPaths] objectAtIndex:4]]; } +- (void)testCancelledOperationDoesStart +{ + var op = [[TestCancelOperation alloc] init], + queue = [[CPOperationQueue alloc] init]; + + [self assertFalse:[op isCancelled]]; + [self assertFalse:[op isFinished]]; + [self assertFalse:[op didMain]]; + [self assertFalse:[op didStart]]; + + [op cancel]; + + [self assertTrue:[op isCancelled]]; + [self assertFalse:[op isFinished]]; + + [queue addOperations:[op] waitUntilFinished:YES]; + + [self assertFalse:[op didMain]]; + [self assertTrue:[op didStart]]; + [self assertTrue:[op isCancelled]]; + [self assertTrue:[op isFinished]]; +} + @end \ No newline at end of file diff --git a/Tests/Foundation/CPOperationTest.j b/Tests/Foundation/CPOperationTest.j index 1c1beef56..ea424ef56 100644 --- a/Tests/Foundation/CPOperationTest.j +++ b/Tests/Foundation/CPOperationTest.j @@ -180,4 +180,21 @@ [self assert:@"isCancelled" equals:[[obs changedKeyPaths] objectAtIndex:9]]; } +- (void)testCancelledOperationIsFinished +{ + var results = @[], + funcOp = [CPFunctionOperation functionOperationWithFunction:function() {[results addObject:"funcOp"];}]; + + [funcOp cancel]; + [self assertTrue:[funcOp isCancelled]]; + [self assertFalse:[funcOp isFinished]]; + + [funcOp start]; + + [self assertTrue:[funcOp isCancelled]]; + [self assertTrue:[funcOp isFinished]]; + + [self assertTrue:([results count] == 0)]; +} + @end