diff options
author | Muller, Alexander (A.) <amulle19@ford.com> | 2016-07-01 22:06:54 -0400 |
---|---|---|
committer | Muller, Alexander (A.) <amulle19@ford.com> | 2016-07-01 22:06:54 -0400 |
commit | 1ff49f62bec2e2f122cfd4a509141ecba95047cb (patch) | |
tree | 6373be820059d0b107079a199aabf7e3180b4589 | |
parent | ac2dcf5dac12a4610ccddb83fb95360f4dac557f (diff) | |
download | sdl_ios-1ff49f62bec2e2f122cfd4a509141ecba95047cb.tar.gz |
Rewrote SDLTouchManager tests to extract variables out of the tests and into beforeEaches.
-rw-r--r-- | SmartDeviceLinkTests/UtilitiesSpecs/Touches/SDLTouchManagerSpec.m | 607 |
1 files changed, 345 insertions, 262 deletions
diff --git a/SmartDeviceLinkTests/UtilitiesSpecs/Touches/SDLTouchManagerSpec.m b/SmartDeviceLinkTests/UtilitiesSpecs/Touches/SDLTouchManagerSpec.m index 71ac916ab..e7125fca1 100644 --- a/SmartDeviceLinkTests/UtilitiesSpecs/Touches/SDLTouchManagerSpec.m +++ b/SmartDeviceLinkTests/UtilitiesSpecs/Touches/SDLTouchManagerSpec.m @@ -17,47 +17,15 @@ #import "SDLTouchEvent.h" #import "SDLTouchManager.h" #import "SDLTouchType.h" -#import "CGPoint_Util.h" +//#import "CGPoint_Util.h" QuickSpecBegin(SDLTouchManagerSpec) -typedef void (^DelegateCallbackBlock)(NSInvocation* invocation); - describe(@"SDLTouchManager Tests", ^{ __block SDLTouchManager* touchManager; - __block id delegateMock; - __block NSUInteger timeStamp; - __block CGPoint controlPoint; - __block SDLTouchCoord* touchCoord; - __block SDLTouchEvent* touchEvent; - __block SDLOnTouchEvent* beginOnTouchEvent; - __block SDLOnTouchEvent* moveOnTouchEvent; - __block SDLOnTouchEvent* endOnTouchEvent; - __block BOOL didCallSingleTap; - __block BOOL didCallDoubleTap; - __block BOOL didCallBeginPan; - __block BOOL didCallMovePan; - __block BOOL didCallEndPan; - __block BOOL didCallBeginPinch; - __block BOOL didCallMovePinch; - __block BOOL didCallEndPinch; - - __block DelegateCallbackBlock singleTapTests; - __block DelegateCallbackBlock doubleTapTests; - __block DelegateCallbackBlock panStartTests; - __block DelegateCallbackBlock panMoveTests; - __block DelegateCallbackBlock panEndTests; - __block DelegateCallbackBlock pinchStartTests; - __block DelegateCallbackBlock pinchMoveTests; - __block DelegateCallbackBlock pinchEndTests; - - __block void (^performTouchEvent)(SDLTouchManager* touchManager, SDLOnTouchEvent* onTouchEvent) = ^(SDLTouchManager* touchManager, SDLOnTouchEvent* onTouchEvent) { - SEL onOnTouchEvent = NSSelectorFromString(@"onOnTouchEvent:"); - ((void (*)(id, SEL, id))[touchManager methodForSelector:onOnTouchEvent])(touchManager, onOnTouchEvent, onTouchEvent); - }; - context(@"Initializing", ^{ + context(@"initializing", ^{ it(@"should correctly have default properties", ^{ SDLTouchManager* touchManager = [[SDLTouchManager alloc] init]; expect(touchManager.touchEventListener).to(beNil()); @@ -68,33 +36,41 @@ describe(@"SDLTouchManager Tests", ^{ }); }); + describe(@"touch events", ^{ + typedef void (^DelegateCallbackBlock)(NSInvocation* invocation); + + __block id delegateMock; + __block CGPoint controlPoint; + __block BOOL didCallSingleTap; + __block BOOL didCallDoubleTap; + __block BOOL didCallBeginPan; + __block BOOL didCallMovePan; + __block BOOL didCallEndPan; + __block BOOL didCallBeginPinch; + __block BOOL didCallMovePinch; + __block BOOL didCallEndPinch; + + __block DelegateCallbackBlock singleTapTests; + __block DelegateCallbackBlock doubleTapTests; + __block DelegateCallbackBlock panStartTests; + __block DelegateCallbackBlock panMoveTests; + __block DelegateCallbackBlock panEndTests; + __block DelegateCallbackBlock pinchStartTests; + __block DelegateCallbackBlock pinchMoveTests; + __block DelegateCallbackBlock pinchEndTests; + + __block void (^performTouchEvent)(SDLTouchManager* touchManager, SDLOnTouchEvent* onTouchEvent) = ^(SDLTouchManager* touchManager, SDLOnTouchEvent* onTouchEvent) { + SEL onOnTouchEvent = NSSelectorFromString(@"onOnTouchEvent:"); + ((void (*)(id, SEL, id))[touchManager methodForSelector:onOnTouchEvent])(touchManager, onOnTouchEvent, onTouchEvent); + }; + beforeEach(^{ touchManager = [[SDLTouchManager alloc] init]; delegateMock = OCMProtocolMock(@protocol(SDLTouchManagerListener)); touchManager.touchEventListener = delegateMock; - timeStamp = [[NSDate date] timeIntervalSince1970] * 1000.0; controlPoint = CGPointMake(100, 200); - touchCoord = [[SDLTouchCoord alloc] init]; - touchCoord.x = @(controlPoint.x); - touchCoord.y = @(controlPoint.y); - - touchEvent = [[SDLTouchEvent alloc] init]; - touchEvent.touchEventId = @0; - touchEvent.coord = [NSMutableArray arrayWithObject:touchCoord]; - touchEvent.timeStamp = [NSMutableArray arrayWithObject:@(timeStamp)]; - - beginOnTouchEvent = [[SDLOnTouchEvent alloc] init]; - beginOnTouchEvent.type = SDLTouchType.BEGIN; - beginOnTouchEvent.event = [NSMutableArray arrayWithObject:touchEvent]; - - moveOnTouchEvent = [[SDLOnTouchEvent alloc] init]; - moveOnTouchEvent.type = SDLTouchType.MOVE; - - endOnTouchEvent = [[SDLOnTouchEvent alloc] init]; - endOnTouchEvent.type = SDLTouchType.END; - didCallSingleTap = NO; [[[[delegateMock stub] andDo:^(NSInvocation* invocation) { didCallSingleTap = YES; @@ -184,142 +160,246 @@ describe(@"SDLTouchManager Tests", ^{ }; }); - context(@"when receiving a single tap", ^{ - it(@"should correctly handle a single tap", ^{ - singleTapTests = ^(NSInvocation* invocation) { - __unsafe_unretained SDLTouchManager* touchManagerCallback; - - CGPoint point; - - [invocation getArgument:&touchManagerCallback atIndex:2]; - [invocation getArgument:&point atIndex:3]; - - expect(touchManagerCallback).to(equal(touchManager)); - expect(@(CGPointEqualToPoint(point, controlPoint))).to(beTruthy()); - }; + describe(@"single finger", ^{ + __block SDLTouchCoord* firstTouchCoord; + __block NSUInteger firstTouchTimeStamp; + + __block SDLOnTouchEvent* firstOnTouchEventStart; + __block SDLOnTouchEvent* firstOnTouchEventEnd; + + beforeEach(^{ + firstTouchCoord = [[SDLTouchCoord alloc] init]; + firstTouchCoord.x = @(controlPoint.x); + firstTouchCoord.y = @(controlPoint.y); - // Begin Touch - performTouchEvent(touchManager, beginOnTouchEvent); + firstTouchTimeStamp = [[NSDate date] timeIntervalSince1970] * 1000.0; - // End Touch - touchEvent.timeStamp = [NSMutableArray arrayWithObject:@(timeStamp)]; + SDLTouchEvent* touchEvent = [[SDLTouchEvent alloc] init]; + touchEvent.touchEventId = @0; + touchEvent.coord = [NSMutableArray arrayWithObject:firstTouchCoord]; + touchEvent.timeStamp = [NSMutableArray arrayWithObject:@(firstTouchTimeStamp)]; - endOnTouchEvent.event = [NSMutableArray arrayWithObject:touchEvent]; - performTouchEvent(touchManager, endOnTouchEvent); + firstOnTouchEventStart = [[SDLOnTouchEvent alloc] init]; + firstOnTouchEventStart.type = SDLTouchType.BEGIN; + firstOnTouchEventStart.event = [NSMutableArray arrayWithObject:touchEvent]; - expect(@(didCallSingleTap)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beTruthy()); - expect(@(didCallDoubleTap)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); - expect(@(didCallBeginPan)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); - expect(@(didCallMovePan)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); - expect(@(didCallEndPan)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); - expect(@(didCallBeginPinch)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); - expect(@(didCallMovePinch)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); - expect(@(didCallEndPinch)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + firstOnTouchEventEnd = [[SDLOnTouchEvent alloc] init]; + firstOnTouchEventEnd.type = SDLTouchType.END; + firstOnTouchEventEnd.event = [NSMutableArray arrayWithObject:touchEvent]; }); - }); - - context(@"when receiving a double tap", ^{ - it(@"should correctly handle a double tap near same point", ^{ + + describe(@"when receiving a single tap", ^{ + it(@"should correctly handle a single tap", ^{ + + singleTapTests = ^(NSInvocation* invocation) { + __unsafe_unretained SDLTouchManager* touchManagerCallback; + + CGPoint point; + + [invocation getArgument:&touchManagerCallback atIndex:2]; + [invocation getArgument:&point atIndex:3]; + + expect(touchManagerCallback).to(equal(touchManager)); + expect(@(CGPointEqualToPoint(point, controlPoint))).to(beTruthy()); + }; + + performTouchEvent(touchManager, firstOnTouchEventStart); + + performTouchEvent(touchManager, firstOnTouchEventEnd); + + expect(@(didCallSingleTap)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beTruthy()); + expect(@(didCallDoubleTap)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + expect(@(didCallBeginPan)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + expect(@(didCallMovePan)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + expect(@(didCallEndPan)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + expect(@(didCallBeginPinch)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + expect(@(didCallMovePinch)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + expect(@(didCallEndPinch)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + }); + }); + + describe(@"when receiving a double tap", ^{ __block CGPoint averagePoint; - doubleTapTests = ^(NSInvocation* invocation) { - __unsafe_unretained SDLTouchManager* touchManagerCallback; + __block SDLTouchEvent* secondTouchEvent; + + __block SDLOnTouchEvent* secondOnTouchEventStart; + __block SDLOnTouchEvent* secondOnTouchEventEnd; + + beforeEach(^{ + secondOnTouchEventStart = [[SDLOnTouchEvent alloc] init]; + secondOnTouchEventStart.type = SDLTouchType.BEGIN; - CGPoint point; + secondOnTouchEventEnd = [[SDLOnTouchEvent alloc] init]; + secondOnTouchEventEnd.type = SDLTouchType.END; - [invocation getArgument:&touchManagerCallback atIndex:2]; - [invocation getArgument:&point atIndex:3]; + secondTouchEvent = [[SDLTouchEvent alloc] init]; + secondTouchEvent.touchEventId = @0; + NSUInteger secondTouchTimeStamp = firstTouchTimeStamp + (touchManager.tapTimeThreshold - 0.1) * 1000; + secondTouchEvent.timeStamp = [NSMutableArray arrayWithObject:@(secondTouchTimeStamp)]; + }); + + context(@"near the same point", ^{ + beforeEach(^{ + SDLTouchCoord* touchCoord = [[SDLTouchCoord alloc] init]; + touchCoord.x = @(firstTouchCoord.x.floatValue + touchManager.tapDistanceThreshold); + touchCoord.y = @(firstTouchCoord.y.floatValue + touchManager.tapDistanceThreshold); + + secondTouchEvent.coord = [NSMutableArray arrayWithObject:touchCoord]; + + secondOnTouchEventStart.event = [NSMutableArray arrayWithObject:secondTouchEvent]; + + secondOnTouchEventEnd.event = [NSMutableArray arrayWithObject:secondTouchEvent]; + + averagePoint = CGPointMake((firstTouchCoord.x.floatValue + touchCoord.x.floatValue) / 2.0f, + (firstTouchCoord.y.floatValue + touchCoord.y.floatValue) / 2.0f); + }); - expect(touchManagerCallback).to(equal(touchManager)); - expect(@(CGPointEqualToPoint(point, averagePoint))).to(beTruthy()); - }; + it(@"should issue delegate callbacks", ^{ + doubleTapTests = ^(NSInvocation* invocation) { + __unsafe_unretained SDLTouchManager* touchManagerCallback; + + CGPoint point; + + [invocation getArgument:&touchManagerCallback atIndex:2]; + [invocation getArgument:&point atIndex:3]; + + expect(touchManagerCallback).to(equal(touchManager)); + expect(@(CGPointEqualToPoint(point, averagePoint))).to(beTruthy()); + }; + + performTouchEvent(touchManager, firstOnTouchEventStart); + performTouchEvent(touchManager, firstOnTouchEventEnd); + performTouchEvent(touchManager, secondOnTouchEventStart); + performTouchEvent(touchManager, secondOnTouchEventEnd); + + expect(@(didCallSingleTap)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + expect(@(didCallDoubleTap)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beTruthy()); + expect(@(didCallBeginPan)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + expect(@(didCallMovePan)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + expect(@(didCallEndPan)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + expect(@(didCallBeginPinch)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + expect(@(didCallMovePinch)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + expect(@(didCallEndPinch)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + }); + }); + + context(@"not near the same point", ^{ + beforeEach(^{ + SDLTouchCoord* touchCoord = [[SDLTouchCoord alloc] init]; + touchCoord.x = @(firstTouchCoord.x.floatValue + touchManager.tapDistanceThreshold + 1); + touchCoord.y = @(firstTouchCoord.y.floatValue + touchManager.tapDistanceThreshold + 1); + + secondTouchEvent.coord = [NSMutableArray arrayWithObject:touchCoord]; + + secondOnTouchEventStart.event = [NSMutableArray arrayWithObject:secondTouchEvent]; + + secondOnTouchEventEnd.event = [NSMutableArray arrayWithObject:secondTouchEvent]; + }); + + it(@"should should not issue delegate callbacks", ^{ + performTouchEvent(touchManager, firstOnTouchEventStart); + performTouchEvent(touchManager, firstOnTouchEventEnd); + performTouchEvent(touchManager, secondOnTouchEventStart); + performTouchEvent(touchManager, secondOnTouchEventEnd); + + expect(@(didCallSingleTap)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + expect(@(didCallDoubleTap)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + expect(@(didCallBeginPan)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + expect(@(didCallMovePan)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + expect(@(didCallEndPan)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + expect(@(didCallBeginPinch)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + expect(@(didCallMovePinch)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + expect(@(didCallEndPinch)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + }); + }); + }); + }); + context(@"when receiving a pan", ^{ + __block CGPoint panStartPoint; + __block CGPoint panMovePoint; + __block CGPoint panSecondMovePoint; + __block CGPoint panEndPoint; + + __block CGFloat distanceMoveX = 10; + __block CGFloat distanceMoveY = 20; + + __block SDLOnTouchEvent* panStartOnTouchEvent; + __block SDLOnTouchEvent* panMoveOnTouchEvent; + __block SDLOnTouchEvent* panSecondMoveOnTouchEvent; + __block SDLOnTouchEvent* panEndOnTouchEvent; + + beforeEach(^{ + // Finger touch down + panStartPoint = controlPoint; + + SDLTouchCoord* panStartTouchCoord = [[SDLTouchCoord alloc] init]; + panStartTouchCoord.x = @(panStartPoint.x); + panStartTouchCoord.y = @(panStartPoint.y); - // Begin First Touch - performTouchEvent(touchManager, beginOnTouchEvent); + NSUInteger panStartTimeStamp = ([[NSDate date] timeIntervalSince1970] + touchManager.panTimeThreshold) * 1000; - // End First Touch - touchEvent.timeStamp = [NSMutableArray arrayWithObject:@(timeStamp)]; + SDLTouchEvent* panStartTouchEvent = [[SDLTouchEvent alloc] init]; + panStartTouchEvent.coord = [NSMutableArray arrayWithObject:panStartTouchCoord]; + panStartTouchEvent.timeStamp = [NSMutableArray arrayWithObject:@(panStartTimeStamp)]; - endOnTouchEvent.event = [NSMutableArray arrayWithObject:touchEvent]; - performTouchEvent(touchManager, endOnTouchEvent); + panStartOnTouchEvent = [[SDLOnTouchEvent alloc] init]; + panStartOnTouchEvent.event = [NSMutableArray arrayWithObject:panStartTouchEvent]; + panStartOnTouchEvent.type = SDLTouchType.BEGIN; - // Start Second Touch - touchCoord.x = @(touchCoord.x.floatValue + touchManager.tapDistanceThreshold); - touchCoord.y = @(touchCoord.y.floatValue + touchManager.tapDistanceThreshold); + // Finger Move + panMovePoint = CGPointMake(panStartPoint.x + distanceMoveX, panStartPoint.y + distanceMoveY); - averagePoint = CGPointAverageOfPoints(controlPoint, - CGPointMake(touchCoord.x.floatValue, - touchCoord.y.floatValue)); - - timeStamp += (touchManager.tapTimeThreshold - 0.1) * 1000; - - touchEvent.coord = [NSMutableArray arrayWithObject:touchCoord]; - touchEvent.timeStamp = [NSMutableArray arrayWithObject:@(timeStamp)]; + SDLTouchCoord* panMoveTouchCoord = [[SDLTouchCoord alloc] init]; + panMoveTouchCoord.x = @(panMovePoint.x); + panMoveTouchCoord.y = @(panMovePoint.y); - beginOnTouchEvent.event = [NSMutableArray arrayWithObject:touchEvent]; - performTouchEvent(touchManager, beginOnTouchEvent); + NSUInteger panMoveTimeStamp = panStartTimeStamp + (touchManager.panTimeThreshold * 1000); - // End Second Touch - touchEvent.timeStamp = [NSMutableArray arrayWithObject:@(timeStamp)]; + SDLTouchEvent* panMoveTouchEvent = [[SDLTouchEvent alloc] init]; + panMoveTouchEvent.coord = [NSMutableArray arrayWithObject:panMoveTouchCoord]; + panMoveTouchEvent.timeStamp = [NSMutableArray arrayWithObject:@(panMoveTimeStamp)]; - endOnTouchEvent.event = [NSMutableArray arrayWithObject:touchEvent]; - performTouchEvent(touchManager, endOnTouchEvent); + panMoveOnTouchEvent = [[SDLOnTouchEvent alloc] init]; + panMoveOnTouchEvent.event = [NSMutableArray arrayWithObject:panMoveTouchEvent]; + panMoveOnTouchEvent.type = SDLTouchType.MOVE; - expect(@(didCallSingleTap)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); - expect(@(didCallDoubleTap)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beTruthy()); - expect(@(didCallBeginPan)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); - expect(@(didCallMovePan)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); - expect(@(didCallEndPan)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); - expect(@(didCallBeginPinch)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); - expect(@(didCallMovePinch)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); - expect(@(didCallEndPinch)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); - }); - - it(@"should correctly not handle a double tap", ^{ - // Begin First Touch - performTouchEvent(touchManager, beginOnTouchEvent); + // Finger Move + panSecondMovePoint = CGPointMake(panMovePoint.x + distanceMoveX, panMovePoint.y + distanceMoveY); - // End First Touch - endOnTouchEvent.event = [NSMutableArray arrayWithObject:touchEvent]; - performTouchEvent(touchManager, endOnTouchEvent); + SDLTouchCoord* panSecondMoveTouchCoord = [[SDLTouchCoord alloc] init]; + panSecondMoveTouchCoord.x = @(panSecondMovePoint.x); + panSecondMoveTouchCoord.y = @(panSecondMovePoint.y); - // Start Second Touch - timeStamp += (touchManager.tapTimeThreshold - 0.1) * 1000; + NSUInteger panSecondMoveTimeStamp = panMoveTimeStamp + (touchManager.panTimeThreshold * 1000); - touchCoord.x = @(touchCoord.x.floatValue + touchManager.tapDistanceThreshold + 1); - touchCoord.y = @(touchCoord.y.floatValue + touchManager.tapDistanceThreshold + 1); + SDLTouchEvent* panSecondMoveTouchEvent = [[SDLTouchEvent alloc] init]; + panSecondMoveTouchEvent.coord = [NSMutableArray arrayWithObject:panSecondMoveTouchCoord]; + panSecondMoveTouchEvent.timeStamp = [NSMutableArray arrayWithObject:@(panSecondMoveTimeStamp)]; - touchEvent.coord = [NSMutableArray arrayWithObject:touchCoord]; - touchEvent.timeStamp = [NSMutableArray arrayWithObject:@(timeStamp)]; + panSecondMoveOnTouchEvent = [[SDLOnTouchEvent alloc] init]; + panSecondMoveOnTouchEvent.event = [NSMutableArray arrayWithObject:panSecondMoveTouchEvent]; + panSecondMoveOnTouchEvent.type = SDLTouchType.MOVE; - beginOnTouchEvent.event = [NSMutableArray arrayWithObject:touchEvent]; - performTouchEvent(touchManager, beginOnTouchEvent); + // Finger End + panEndPoint = CGPointMake(panSecondMovePoint.x, panSecondMovePoint.y); + + SDLTouchCoord* panEndTouchCoord = [[SDLTouchCoord alloc] init]; + panEndTouchCoord.x = @(panEndPoint.x); + panEndTouchCoord.y = @(panEndPoint.y); - // End Second Touch - touchEvent.timeStamp = [NSMutableArray arrayWithObject:@(timeStamp)]; + NSUInteger panEndTimeStamp = panSecondMoveTimeStamp + (touchManager.panTimeThreshold * 1000); - endOnTouchEvent.event = [NSMutableArray arrayWithObject:touchEvent]; - performTouchEvent(touchManager, endOnTouchEvent); + SDLTouchEvent* panEndTouchEvent = [[SDLTouchEvent alloc] init]; + panEndTouchEvent.coord = [NSMutableArray arrayWithObject:panEndTouchCoord]; + panEndTouchEvent.timeStamp = [NSMutableArray arrayWithObject:@(panEndTimeStamp)]; - expect(@(didCallSingleTap)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); - expect(@(didCallDoubleTap)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); - expect(@(didCallBeginPan)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); - expect(@(didCallMovePan)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); - expect(@(didCallEndPan)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); - expect(@(didCallBeginPinch)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); - expect(@(didCallMovePinch)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); - expect(@(didCallEndPinch)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + panEndOnTouchEvent = [[SDLOnTouchEvent alloc] init]; + panEndOnTouchEvent.event = [NSMutableArray arrayWithObject:panEndTouchEvent]; + panEndOnTouchEvent.type = SDLTouchType.END; }); - }); - context(@"when receiving a pan", ^{ - __block CGPoint panStartPoint; - __block CGPoint panMovePoint; - __block CGPoint panEndPoint; it(@"should correctly give all pan callbacks", ^{ - __block CGFloat distanceMoveX = 10; - __block CGFloat distanceMoveY = 20; - panStartTests = ^(NSInvocation* invocation) { __unsafe_unretained SDLTouchManager* touchManagerCallback; @@ -329,9 +409,9 @@ describe(@"SDLTouchManager Tests", ^{ [invocation getArgument:&point atIndex:3]; expect(touchManagerCallback).to(equal(touchManager)); - expect(@(CGPointEqualToPoint(point, panStartPoint))).to(beTruthy()); + expect(@(CGPointEqualToPoint(point, panMovePoint))).to(beTruthy()); }; - + panMoveTests = ^(NSInvocation* invocation) { __unsafe_unretained SDLTouchManager* touchManagerCallback; @@ -343,8 +423,8 @@ describe(@"SDLTouchManager Tests", ^{ [invocation getArgument:&endPoint atIndex:4]; expect(touchManagerCallback).to(equal(touchManager)); - expect(@(CGPointEqualToPoint(startPoint, panStartPoint))).to(beTruthy()); - expect(@(CGPointEqualToPoint(endPoint, panMovePoint))).to(beTruthy()); + expect(@(CGPointEqualToPoint(startPoint, panMovePoint))).to(beTruthy()); + expect(@(CGPointEqualToPoint(endPoint, panSecondMovePoint))).to(beTruthy()); }; panEndTests = ^(NSInvocation* invocation) { @@ -358,48 +438,11 @@ describe(@"SDLTouchManager Tests", ^{ expect(touchManagerCallback).to(equal(touchManager)); expect(@(CGPointEqualToPoint(point, panEndPoint))).to(beTruthy()); }; - - // Begin Touch - performTouchEvent(touchManager, beginOnTouchEvent); - - // Move Touch - touchCoord.x = @(touchCoord.x.floatValue + distanceMoveX); - touchCoord.y = @(touchCoord.y.floatValue + distanceMoveY); - touchEvent.coord = [NSMutableArray arrayWithObject:touchCoord]; - - panStartPoint = CGPointMake(touchCoord.x.floatValue, touchCoord.y.floatValue); - - timeStamp += (touchManager.panTimeThreshold * 1000); - touchEvent.timeStamp = [NSMutableArray arrayWithObject:@(timeStamp)]; - - moveOnTouchEvent.event = [NSMutableArray arrayWithObject:touchEvent]; - performTouchEvent(touchManager, moveOnTouchEvent); - - // Move Touch 2 - touchCoord.x = @(touchCoord.x.floatValue + distanceMoveX); - touchCoord.y = @(touchCoord.y.floatValue + distanceMoveY); - touchEvent.coord = [NSMutableArray arrayWithObject:touchCoord]; - - panMovePoint = CGPointMake(touchCoord.x.floatValue, touchCoord.y.floatValue); - - timeStamp += (touchManager.panTimeThreshold * 1000); - touchEvent.timeStamp = [NSMutableArray arrayWithObject:@(timeStamp)]; - - moveOnTouchEvent.event = [NSMutableArray arrayWithObject:touchEvent]; - performTouchEvent(touchManager, moveOnTouchEvent); - - // End Touch - touchCoord.x = @(touchCoord.x.floatValue + distanceMoveX); - touchCoord.y = @(touchCoord.y.floatValue + distanceMoveY); - touchEvent.coord = [NSMutableArray arrayWithObject:touchCoord]; - - panEndPoint = CGPointMake(touchCoord.x.floatValue, touchCoord.y.floatValue); - - timeStamp += (touchManager.panTimeThreshold * 1000); - touchEvent.timeStamp = [NSMutableArray arrayWithObject:@(timeStamp)]; - endOnTouchEvent.event = [NSMutableArray arrayWithObject:touchEvent]; - performTouchEvent(touchManager, endOnTouchEvent); + performTouchEvent(touchManager, panStartOnTouchEvent); + performTouchEvent(touchManager, panMoveOnTouchEvent); + performTouchEvent(touchManager, panSecondMoveOnTouchEvent); + performTouchEvent(touchManager, panEndOnTouchEvent); expect(@(didCallSingleTap)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); expect(@(didCallDoubleTap)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); @@ -409,15 +452,102 @@ describe(@"SDLTouchManager Tests", ^{ expect(@(didCallBeginPinch)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); expect(@(didCallMovePinch)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); expect(@(didCallEndPinch)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); + }); }); context(@"when receiving a pinch", ^{ - it(@"should correctly give all pinch callbacks", ^{ - __block CGPoint pinchStartCenter; - __block CGPoint pinchMoveCenter; - __block CGFloat pinchMoveScale; - __block CGPoint pinchEndCenter; + + __block CGPoint pinchStartCenter; + __block CGPoint pinchMoveCenter; + __block CGFloat pinchMoveScale; + __block CGPoint pinchEndCenter; + + __block SDLOnTouchEvent* pinchStartFirstFingerOnTouchEvent; + __block SDLOnTouchEvent* pinchStartSecondFingerOnTouchEvent; + __block SDLOnTouchEvent* pinchMoveSecondFingerOnTouchEvent; + __block SDLOnTouchEvent* pinchEndSecondFingerOnTouchEvent; + + beforeEach(^{ + // First finger touch down + SDLTouchCoord* firstFingerTouchCoord = [[SDLTouchCoord alloc] init]; + firstFingerTouchCoord.x = @(controlPoint.x); + firstFingerTouchCoord.y = @(controlPoint.y); + + NSUInteger firstFingerTimeStamp = [[NSDate date] timeIntervalSince1970] * 1000; + + SDLTouchEvent* firstFingerTouchEvent = [[SDLTouchEvent alloc] init]; + firstFingerTouchEvent.coord = [NSMutableArray arrayWithObject:firstFingerTouchCoord]; + firstFingerTouchEvent.timeStamp = [NSMutableArray arrayWithObject:@(firstFingerTimeStamp)]; + + pinchStartFirstFingerOnTouchEvent = [[SDLOnTouchEvent alloc] init]; + pinchStartFirstFingerOnTouchEvent.event = [NSMutableArray arrayWithObject:firstFingerTouchEvent]; + pinchStartFirstFingerOnTouchEvent.type = SDLTouchType.BEGIN; + + // Second finger touch down + SDLTouchCoord* secondFingerTouchCoord = [[SDLTouchCoord alloc] init]; + secondFingerTouchCoord.x = @(firstFingerTouchCoord.x.floatValue + 100); + secondFingerTouchCoord.y = @(firstFingerTouchCoord.y.floatValue + 100); + + NSUInteger secondFingerTimeStamp = firstFingerTimeStamp; + SDLTouchEvent* secondFingerTouchEvent = [[SDLTouchEvent alloc] init]; + secondFingerTouchEvent.touchEventId = @1; + secondFingerTouchEvent.coord = [NSMutableArray arrayWithObject:secondFingerTouchCoord]; + secondFingerTouchEvent.timeStamp = [NSMutableArray arrayWithObject:@(secondFingerTimeStamp)]; + + pinchStartSecondFingerOnTouchEvent = [[SDLOnTouchEvent alloc] init]; + pinchStartSecondFingerOnTouchEvent.event = [NSMutableArray arrayWithObject:secondFingerTouchEvent]; + pinchStartSecondFingerOnTouchEvent.type = SDLTouchType.BEGIN; + + pinchStartCenter = CGPointMake((firstFingerTouchCoord.x.floatValue + secondFingerTouchCoord.x.floatValue) / 2.0f, + (firstFingerTouchCoord.y.floatValue + secondFingerTouchCoord.y.floatValue) / 2.0f); + + CGFloat pinchStartDistance = hypotf(firstFingerTouchCoord.x.floatValue - secondFingerTouchCoord.x.floatValue, + firstFingerTouchCoord.y.floatValue - secondFingerTouchCoord.y.floatValue); + + // Second finger move + SDLTouchCoord* secondFingerMoveTouchCoord = [[SDLTouchCoord alloc] init]; + secondFingerMoveTouchCoord.x = @(secondFingerTouchCoord.x.floatValue - 50); + secondFingerMoveTouchCoord.y = @(secondFingerTouchCoord.y.floatValue - 40); + + NSUInteger secondFingerMoveTimeStamp = secondFingerTimeStamp + (touchManager.panTimeThreshold * 1000); + + SDLTouchEvent* secondFingerMoveTouchEvent = [[SDLTouchEvent alloc] init]; + secondFingerMoveTouchEvent.touchEventId = @1; + secondFingerMoveTouchEvent.coord = [NSMutableArray arrayWithObject:secondFingerMoveTouchCoord]; + secondFingerMoveTouchEvent.timeStamp = [NSMutableArray arrayWithObject:@(secondFingerMoveTimeStamp)]; + + pinchMoveSecondFingerOnTouchEvent = [[SDLOnTouchEvent alloc] init]; + pinchMoveSecondFingerOnTouchEvent.event = [NSMutableArray arrayWithObject:secondFingerMoveTouchEvent]; + pinchMoveSecondFingerOnTouchEvent.type = SDLTouchType.MOVE; + + pinchMoveCenter = CGPointMake((firstFingerTouchCoord.x.floatValue + secondFingerMoveTouchCoord.x.floatValue) / 2.0f, + (firstFingerTouchCoord.y.floatValue + secondFingerMoveTouchCoord.y.floatValue) / 2.0f); + + CGFloat pinchMoveDistance = hypotf(firstFingerTouchCoord.x.floatValue - secondFingerMoveTouchCoord.x.floatValue, + firstFingerTouchCoord.y.floatValue - secondFingerMoveTouchCoord.y.floatValue); + + pinchMoveScale = pinchMoveDistance / pinchStartDistance; + + // Second finger end + SDLTouchCoord* secondFingerEndTouchCoord = secondFingerMoveTouchCoord; + + NSUInteger secondFingerEndTimeStamp = secondFingerMoveTimeStamp + (touchManager.panTimeThreshold * 1000); + + SDLTouchEvent* secondFingerEndTouchEvent = [[SDLTouchEvent alloc] init]; + secondFingerEndTouchEvent.touchEventId = @1; + secondFingerEndTouchEvent.coord = [NSMutableArray arrayWithObject:secondFingerEndTouchCoord]; + secondFingerEndTouchEvent.timeStamp = [NSMutableArray arrayWithObject:@(secondFingerEndTimeStamp)]; + + pinchEndSecondFingerOnTouchEvent = [[SDLOnTouchEvent alloc] init]; + pinchEndSecondFingerOnTouchEvent.event = [NSMutableArray arrayWithObject:secondFingerEndTouchEvent]; + pinchEndSecondFingerOnTouchEvent.type = SDLTouchType.END; + + pinchEndCenter = CGPointMake((firstFingerTouchCoord.x.floatValue + secondFingerEndTouchCoord.x.floatValue) / 2.0f, + (firstFingerTouchCoord.y.floatValue + secondFingerEndTouchCoord.y.floatValue) / 2.0f); + }); + + it(@"should correctly give all pinch callback", ^{ pinchStartTests = ^(NSInvocation* invocation) { __unsafe_unretained SDLTouchManager* touchManagerCallback; @@ -444,7 +574,7 @@ describe(@"SDLTouchManager Tests", ^{ expect(@(CGPointEqualToPoint(point, pinchMoveCenter))).to(beTruthy()); expect(@(scale)).to(beCloseTo(@(pinchMoveScale)).within(0.0001)); }; - + pinchEndTests = ^(NSInvocation* invocation) { __unsafe_unretained SDLTouchManager* touchManagerCallback; @@ -456,58 +586,11 @@ describe(@"SDLTouchManager Tests", ^{ expect(touchManagerCallback).to(equal(touchManager)); expect(@(CGPointEqualToPoint(point, pinchEndCenter))).to(beTruthy()); }; - - // Begin First Touch - performTouchEvent(touchManager, beginOnTouchEvent); - - // Begin Second Touch - SDLTouchCoord* secondTouchCoord = [[SDLTouchCoord alloc] init]; - secondTouchCoord.x = @(controlPoint.x + 100); - secondTouchCoord.y = @(controlPoint.y + 100); - - CGPoint firstPoint = CGPointMake(touchCoord.x.floatValue, touchCoord.y.floatValue); - CGPoint secondPoint = CGPointMake(secondTouchCoord.x.floatValue, secondTouchCoord.y.floatValue); - - pinchStartCenter = CGPointCenterOfPoints(firstPoint, secondPoint); - CGFloat pinchStartDistance = CGPointDistanceBetweenPoints(firstPoint, secondPoint); - - touchEvent.touchEventId = @1; - touchEvent.coord = [NSMutableArray arrayWithObject:secondTouchCoord]; - touchEvent.timeStamp = [NSMutableArray arrayWithObject:@(timeStamp)]; - - beginOnTouchEvent.event = [NSMutableArray arrayWithObject:touchEvent]; - performTouchEvent(touchManager, beginOnTouchEvent); - - - // Move Second Finger - secondTouchCoord.x = @(secondTouchCoord.x.floatValue - 30); - secondTouchCoord.y = @(secondTouchCoord.y.floatValue - 40); - - secondPoint = CGPointMake(secondTouchCoord.x.floatValue, secondTouchCoord.y.floatValue); - - pinchMoveCenter = CGPointCenterOfPoints(firstPoint, secondPoint); - CGFloat pinchMoveDistance = CGPointDistanceBetweenPoints(firstPoint, secondPoint); - pinchMoveScale = pinchMoveDistance / pinchStartDistance; - - timeStamp += touchManager.panTimeThreshold * 1000; - - touchEvent.touchEventId = @1; - touchEvent.coord = [NSMutableArray arrayWithObject:secondTouchCoord]; - touchEvent.timeStamp = [NSMutableArray arrayWithObject:@(timeStamp)]; - - moveOnTouchEvent.event = [NSMutableArray arrayWithObject:touchEvent]; - performTouchEvent(touchManager, moveOnTouchEvent); - - // End First Finger - timeStamp += touchManager.panTimeThreshold * 1000; - touchEvent.timeStamp = [NSMutableArray arrayWithObject:@(timeStamp)]; - secondPoint = CGPointMake(secondTouchCoord.x.floatValue, secondTouchCoord.y.floatValue); - - pinchEndCenter = CGPointCenterOfPoints(firstPoint, secondPoint); - - endOnTouchEvent.event = [NSMutableArray arrayWithObject:touchEvent]; - performTouchEvent(touchManager, endOnTouchEvent); + performTouchEvent(touchManager, pinchStartFirstFingerOnTouchEvent); + performTouchEvent(touchManager, pinchStartSecondFingerOnTouchEvent); + performTouchEvent(touchManager, pinchMoveSecondFingerOnTouchEvent); + performTouchEvent(touchManager, pinchEndSecondFingerOnTouchEvent); expect(@(didCallSingleTap)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); expect(@(didCallDoubleTap)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beFalsy()); |