summaryrefslogtreecommitdiff
path: root/SmartDeviceLinkTests/UtilitiesSpecs/Touches/SDLTouchManagerSpec.m
blob: 21639b6b77b156f87c29d6802db7bd740edb8fde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
//
//  SDLTouchManagerSpec.m
//  SmartDeviceLink-iOS
//
//  Created by Muller, Alexander (A.) on 6/17/16.
//  Copyright © 2016 smartdevicelink. All rights reserved.
//

#import <Foundation/Foundation.h>

#import <Quick/Quick.h>
#import <Nimble/Nimble.h>
#import <OCMock/OCMock.h>

#import "SDLOnTouchEvent.h"
#import "SDLTouchCoord.h"
#import "SDLTouchEvent.h"
#import "SDLTouchManager.h"
#import "SDLTouchType.h"

QuickSpecBegin(SDLTouchManagerSpec)

describe(@"SDLTouchManager Tests", ^{
    
    __block SDLTouchManager* touchManager;
    
    context(@"initializing", ^{
        it(@"should correctly have default properties", ^{
            SDLTouchManager* touchManager = [[SDLTouchManager alloc] init];
            expect(touchManager.touchEventDelegate).to(beNil());
            expect(@(touchManager.tapDistanceThreshold)).to(equal(@50));
            expect(@(touchManager.tapTimeThreshold)).to(beCloseTo(@0.4).within(0.0001));
            expect(@(touchManager.movementTimeThreshold)).to(beCloseTo(@0.05).within(0.0001));
            expect(@(touchManager.isTouchEnabled)).to(beTruthy());
        });
        
    });
    
    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(SDLTouchManagerDelegate));
            touchManager.touchEventDelegate = delegateMock;
            controlPoint = CGPointMake(100, 200);
            
            didCallSingleTap = NO;
            [[[[delegateMock stub] andDo:^(NSInvocation* invocation) {
                didCallSingleTap = YES;
                
                singleTapTests(invocation);
            }] ignoringNonObjectArgs] touchManager:[OCMArg any] didReceiveSingleTapAtPoint:CGPointZero];

            singleTapTests = ^(NSInvocation* invocation) {
                fail();
            };
            
            didCallDoubleTap = NO;
            [[[[delegateMock stub] andDo:^(NSInvocation* invocation) {
                didCallDoubleTap = YES;
                
                doubleTapTests(invocation);
            }] ignoringNonObjectArgs] touchManager:[OCMArg any] didReceiveDoubleTapAtPoint:CGPointZero];
            
            doubleTapTests = ^(NSInvocation* invocation) {
                fail();
            };
            
            didCallBeginPan = NO;
            [[[[delegateMock stub] andDo:^(NSInvocation* invocation) {
                didCallBeginPan = YES;
                
                panStartTests(invocation);
            }] ignoringNonObjectArgs] touchManager:[OCMArg any] panningDidStartAtPoint:CGPointZero];
            
            panStartTests = ^(NSInvocation* invocation) {
                fail();
            };
            
            didCallMovePan = NO;
            [[[[delegateMock stub] andDo:^(NSInvocation* invocation) {
                didCallMovePan = YES;
                
                panMoveTests(invocation);
            }] ignoringNonObjectArgs] touchManager:[OCMArg any] didReceivePanningFromPoint:CGPointZero toPoint:CGPointZero];
            
            panMoveTests = ^(NSInvocation* invocation) {
                fail();
            };
            
            didCallEndPan = NO;
            [[[[delegateMock stub] andDo:^(NSInvocation* invocation) {
                didCallEndPan = YES;
                
                panEndTests(invocation);
            }] ignoringNonObjectArgs] touchManager:[OCMArg any] panningDidEndAtPoint:CGPointZero];
        
            panEndTests = ^(NSInvocation* invocation) {
                fail();
            };
            
            didCallBeginPinch = NO;
            [[[[delegateMock stub] andDo:^(NSInvocation* invocation) {
                didCallBeginPinch = YES;
                
                pinchStartTests(invocation);
            }] ignoringNonObjectArgs] touchManager:[OCMArg any] pinchDidStartAtCenterPoint:CGPointZero];

            pinchStartTests = ^(NSInvocation* invocation) {
                fail();
            };
            
            didCallMovePinch = NO;
            [[[[delegateMock stub] andDo:^(NSInvocation* invocation) {
                didCallMovePinch = YES;
                
                pinchMoveTests(invocation);
            }] ignoringNonObjectArgs] touchManager:[OCMArg any] didReceivePinchAtCenterPoint:CGPointZero withScale:0];

            pinchMoveTests = ^(NSInvocation* invocation) {
                fail();
            };
            
            didCallEndPinch = NO;
            [[[[delegateMock stub] andDo:^(NSInvocation* invocation) {
                didCallEndPinch = YES;
                
                pinchEndTests(invocation);
            }] ignoringNonObjectArgs] touchManager:[OCMArg any] pinchDidEndAtCenterPoint:CGPointZero];
            
            pinchEndTests = ^(NSInvocation* invocation) {
                fail();
            };
        });
        
        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);
                
                firstTouchTimeStamp = [[NSDate date] timeIntervalSince1970] * 1000.0;
                
                SDLTouchEvent* touchEvent = [[SDLTouchEvent alloc] init];
                touchEvent.touchEventId = @0;
                touchEvent.coord = [NSMutableArray arrayWithObject:firstTouchCoord];
                touchEvent.timeStamp = [NSMutableArray arrayWithObject:@(firstTouchTimeStamp)];
                
                firstOnTouchEventStart = [[SDLOnTouchEvent alloc] init];
                firstOnTouchEventStart.type = SDLTouchType.BEGIN;
                firstOnTouchEventStart.event = [NSMutableArray arrayWithObject:touchEvent];
                
                firstOnTouchEventEnd = [[SDLOnTouchEvent alloc] init];
                firstOnTouchEventEnd.type = SDLTouchType.END;
                firstOnTouchEventEnd.event = [NSMutableArray arrayWithObject:touchEvent];
            });
            
            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;
                
                __block SDLTouchEvent* secondTouchEvent;
                
                __block SDLOnTouchEvent* secondOnTouchEventStart;
                __block SDLOnTouchEvent* secondOnTouchEventEnd;
                
                beforeEach(^{
                    secondOnTouchEventStart = [[SDLOnTouchEvent alloc] init];
                    secondOnTouchEventStart.type = SDLTouchType.BEGIN;
                    
                    secondOnTouchEventEnd = [[SDLOnTouchEvent alloc] init];
                    secondOnTouchEventEnd.type = SDLTouchType.END;
                    
                    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);
                    });
                    
                    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);
                
                CGFloat movementTimeThresholdOffset = (touchManager.movementTimeThreshold + .01) * 1000;
                
                NSUInteger panStartTimeStamp = ([[NSDate date] timeIntervalSince1970] * 1000) + movementTimeThresholdOffset;
                
                SDLTouchEvent* panStartTouchEvent = [[SDLTouchEvent alloc] init];
                panStartTouchEvent.coord = [NSMutableArray arrayWithObject:panStartTouchCoord];
                panStartTouchEvent.timeStamp = [NSMutableArray arrayWithObject:@(panStartTimeStamp)];
                
                panStartOnTouchEvent = [[SDLOnTouchEvent alloc] init];
                panStartOnTouchEvent.event = [NSMutableArray arrayWithObject:panStartTouchEvent];
                panStartOnTouchEvent.type = SDLTouchType.BEGIN;
                
                // Finger Move
                panMovePoint = CGPointMake(panStartPoint.x + distanceMoveX, panStartPoint.y + distanceMoveY);

                SDLTouchCoord* panMoveTouchCoord = [[SDLTouchCoord alloc] init];
                panMoveTouchCoord.x = @(panMovePoint.x);
                panMoveTouchCoord.y = @(panMovePoint.y);
                
                NSUInteger panMoveTimeStamp = panStartTimeStamp + movementTimeThresholdOffset;
                
                SDLTouchEvent* panMoveTouchEvent = [[SDLTouchEvent alloc] init];
                panMoveTouchEvent.coord = [NSMutableArray arrayWithObject:panMoveTouchCoord];
                panMoveTouchEvent.timeStamp = [NSMutableArray arrayWithObject:@(panMoveTimeStamp)];
                
                panMoveOnTouchEvent = [[SDLOnTouchEvent alloc] init];
                panMoveOnTouchEvent.event = [NSMutableArray arrayWithObject:panMoveTouchEvent];
                panMoveOnTouchEvent.type = SDLTouchType.MOVE;
                
                // Finger Move
                panSecondMovePoint = CGPointMake(panMovePoint.x + distanceMoveX, panMovePoint.y + distanceMoveY);
                
                SDLTouchCoord* panSecondMoveTouchCoord = [[SDLTouchCoord alloc] init];
                panSecondMoveTouchCoord.x = @(panSecondMovePoint.x);
                panSecondMoveTouchCoord.y = @(panSecondMovePoint.y);
                
                NSUInteger panSecondMoveTimeStamp = panMoveTimeStamp + movementTimeThresholdOffset;
                
                SDLTouchEvent* panSecondMoveTouchEvent = [[SDLTouchEvent alloc] init];
                panSecondMoveTouchEvent.coord = [NSMutableArray arrayWithObject:panSecondMoveTouchCoord];
                panSecondMoveTouchEvent.timeStamp = [NSMutableArray arrayWithObject:@(panSecondMoveTimeStamp)];
                
                panSecondMoveOnTouchEvent = [[SDLOnTouchEvent alloc] init];
                panSecondMoveOnTouchEvent.event = [NSMutableArray arrayWithObject:panSecondMoveTouchEvent];
                panSecondMoveOnTouchEvent.type = SDLTouchType.MOVE;
                
                // Finger End
                panEndPoint = CGPointMake(panSecondMovePoint.x, panSecondMovePoint.y);

                SDLTouchCoord* panEndTouchCoord = [[SDLTouchCoord alloc] init];
                panEndTouchCoord.x = @(panEndPoint.x);
                panEndTouchCoord.y = @(panEndPoint.y);
                
                NSUInteger panEndTimeStamp = panSecondMoveTimeStamp + movementTimeThresholdOffset;
                
                SDLTouchEvent* panEndTouchEvent = [[SDLTouchEvent alloc] init];
                panEndTouchEvent.coord = [NSMutableArray arrayWithObject:panEndTouchCoord];
                panEndTouchEvent.timeStamp = [NSMutableArray arrayWithObject:@(panEndTimeStamp)];
                
                panEndOnTouchEvent = [[SDLOnTouchEvent alloc] init];
                panEndOnTouchEvent.event = [NSMutableArray arrayWithObject:panEndTouchEvent];
                panEndOnTouchEvent.type = SDLTouchType.END;
            });
            
            it(@"should correctly give all pan callbacks", ^{
                panStartTests = ^(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, panMovePoint))).to(beTruthy());
                };
                
                panMoveTests = ^(NSInvocation* invocation) {
                    __unsafe_unretained SDLTouchManager* touchManagerCallback;
                    
                    CGPoint startPoint;
                    CGPoint endPoint;
                    
                    [invocation getArgument:&touchManagerCallback atIndex:2];
                    [invocation getArgument:&startPoint atIndex:3];
                    [invocation getArgument:&endPoint atIndex:4];
                    
                    expect(touchManagerCallback).to(equal(touchManager));
                    expect(@(CGPointEqualToPoint(startPoint, panMovePoint))).to(beTruthy());
                    expect(@(CGPointEqualToPoint(endPoint, panSecondMovePoint))).to(beTruthy());
                };
                
                panEndTests = ^(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, panEndPoint))).to(beTruthy());
                };

                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());
                expect(@(didCallBeginPan)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beTruthy());
                expect(@(didCallMovePan)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beTruthy());
                expect(@(didCallEndPan)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beTruthy());
                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", ^{
            
            __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.movementTimeThreshold + 0.1) * 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.movementTimeThreshold + 0.1) * 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;
                    
                    CGPoint point;
                    
                    [invocation getArgument:&touchManagerCallback atIndex:2];
                    [invocation getArgument:&point atIndex:3];
                    
                    expect(touchManagerCallback).to(equal(touchManager));
                    expect(@(CGPointEqualToPoint(point, pinchStartCenter))).to(beTruthy());
                };
                
                pinchMoveTests = ^(NSInvocation* invocation) {
                    __unsafe_unretained SDLTouchManager* touchManagerCallback;
                    
                    CGPoint point;
                    CGFloat scale;
                    
                    [invocation getArgument:&touchManagerCallback atIndex:2];
                    [invocation getArgument:&point atIndex:3];
                    [invocation getArgument:&scale atIndex:4];
                    
                    expect(touchManagerCallback).to(equal(touchManager));
                    expect(@(CGPointEqualToPoint(point, pinchMoveCenter))).to(beTruthy());
                    expect(@(scale)).to(beCloseTo(@(pinchMoveScale)).within(0.0001));
                };
                
                pinchEndTests = ^(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, pinchEndCenter))).to(beTruthy());
                };

                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());
                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(beTruthy());
                expect(@(didCallMovePinch)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beTruthy());
                expect(@(didCallEndPinch)).withTimeout(touchManager.tapTimeThreshold + 0.1).toEventually(beTruthy());
            });
        });
    });
});

QuickSpecEnd