summaryrefslogtreecommitdiff
path: root/SmartDeviceLinkTests/UtilitiesSpecs/Touches/SDLPinchGestureSpec.m
blob: 61c82061e4934881efea8b38529880ee8467e7da (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
//
//  SDLPinchGestureSpec.m
//  SmartDeviceLink-iOS
//
//  Created by Muller, Alexander (A.) on 7/1/16.
//  Copyright © 2016 smartdevicelink. All rights reserved.
//

#import <Foundation/Foundation.h>

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

#import "SDLPinchGesture.h"
#import "SDLTouchCoord.h"
#import "SDLTouchEvent.h"
#import "SDLTouch.h"

QuickSpecBegin(SDLPinchGestureSpec)

describe(@"SDLPinchGesture Tests", ^{
    context(@"SDLPinchGestureZero", ^{
        __block SDLPinchGesture* pinchGesture = [[SDLPinchGesture alloc] init];;
        
        it(@"should correctly initialize", ^{
            expect(pinchGesture.firstTouch).to(beNil());
            expect(pinchGesture.secondTouch).to(beNil());
            expect(@(pinchGesture.distance)).to(equal(@0));
            expect(@(CGPointEqualToPoint(pinchGesture.center, CGPointZero))).to(beTruthy());
        });
        
        it(@"should not be a valid SDLPinchGesture", ^{
            expect(@(pinchGesture.isValid)).to(beFalsy());
        });
    });
    
    context(@"SDLPinchGestureMake", ^{
        __block SDLPinchGesture* pinchGesture;
        __block unsigned long timeStamp = [[NSDate date] timeIntervalSince1970] * 1000;
        __block unsigned long secondTimeStamp = timeStamp + 1000;
        
        beforeEach(^{
            SDLTouchCoord* firstCoord = [[SDLTouchCoord alloc] init];
            firstCoord.x = @100;
            firstCoord.y = @200;
            
            SDLTouchEvent* firstTouchEvent = [[SDLTouchEvent alloc] init];
            firstTouchEvent.touchEventId = @0;
            firstTouchEvent.coord = [NSArray arrayWithObject:firstCoord];
            firstTouchEvent.timeStamp = [NSArray arrayWithObject:@(timeStamp)];
            
            SDLTouch* firstTouch = [[SDLTouch alloc] initWithTouchEvent:firstTouchEvent];
            
            SDLTouchCoord* secondCoord = [[SDLTouchCoord alloc] init];
            secondCoord.x = @200;
            secondCoord.y = @300;
            
            SDLTouchEvent* secondTouchEvent = [[SDLTouchEvent alloc] init];
            secondTouchEvent.touchEventId = @1;
            secondTouchEvent.coord = [NSArray arrayWithObject:secondCoord];
            secondTouchEvent.timeStamp = [NSArray arrayWithObject:@(secondTimeStamp)];
            
            SDLTouch* secondTouch = [[SDLTouch alloc] initWithTouchEvent:secondTouchEvent];

            pinchGesture = [[SDLPinchGesture alloc] initWithFirstTouch:firstTouch
                                                           secondTouch:secondTouch];
        });
        
        it(@"should correctly initialize", ^{
            expect(@(pinchGesture.firstTouch.identifier)).to(equal(@(SDLTouchIdentifierFirstFinger)));
            expect(@(pinchGesture.firstTouch.location.x)).to(equal(@100));
            expect(@(pinchGesture.firstTouch.location.y)).to(equal(@200));
            expect(@(pinchGesture.firstTouch.timeStamp)).to(equal(@(timeStamp)));
            
            expect(@(pinchGesture.secondTouch.identifier)).to(equal(@(SDLTouchIdentifierSecondFinger)));
            expect(@(pinchGesture.secondTouch.location.x)).to(equal(@200));
            expect(@(pinchGesture.secondTouch.location.y)).to(equal(@300));
            expect(@(pinchGesture.secondTouch.timeStamp)).to(equal(@(secondTimeStamp)));
            
            expect(@(pinchGesture.distance)).to(beCloseTo(@141.4213).within(0.0001));
            expect(@(pinchGesture.center.x)).to(equal(@150));
            expect(@(pinchGesture.center.y)).to(equal(@250));
        });
        
        it(@"should be a valid SDLPinchGesture", ^{
            expect(@(pinchGesture.isValid)).to(beTruthy());
        });
    });
    
    context(@"updating SDLPinchGesture", ^{
        __block SDLPinchGesture* pinchGesture;
        __block unsigned long timeStamp = [[NSDate date] timeIntervalSince1970] * 1000;
        __block unsigned long secondTimeStamp = timeStamp + 1000;
        __block unsigned long newTimeStamp = timeStamp + 1000;
        
        __block SDLTouch* newFirstTouch;
        __block SDLTouch* newSecondTouch;
        
        beforeEach(^{
            SDLTouchCoord* firstCoord = [[SDLTouchCoord alloc] init];
            firstCoord.x = @100;
            firstCoord.y = @200;
            
            SDLTouchEvent* firstTouchEvent = [[SDLTouchEvent alloc] init];
            firstTouchEvent.touchEventId = @0;
            firstTouchEvent.coord = [NSArray arrayWithObject:firstCoord];
            firstTouchEvent.timeStamp = [NSArray arrayWithObject:@(timeStamp)];
            
            SDLTouch* firstTouch = [[SDLTouch alloc] initWithTouchEvent:firstTouchEvent];
            
            SDLTouchCoord* secondCoord = [[SDLTouchCoord alloc] init];
            secondCoord.x = @200;
            secondCoord.y = @300;
            
            SDLTouchEvent* secondTouchEvent = [[SDLTouchEvent alloc] init];
            secondTouchEvent.touchEventId = @1;
            secondTouchEvent.coord = [NSArray arrayWithObject:secondCoord];
            secondTouchEvent.timeStamp = [NSArray arrayWithObject:@(secondTimeStamp)];
            
            SDLTouch* secondTouch = [[SDLTouch alloc] initWithTouchEvent:secondTouchEvent];
            
            pinchGesture = [[SDLPinchGesture alloc] initWithFirstTouch:firstTouch
                                                           secondTouch:secondTouch];
            
            SDLTouchCoord* newCoord = [[SDLTouchCoord alloc] init];
            newCoord.x = @150;
            newCoord.y = @250;
            
            SDLTouchEvent* newFirstTouchEvent = [[SDLTouchEvent alloc] init];
            newFirstTouchEvent.touchEventId = @0;
            newFirstTouchEvent.coord = [NSArray arrayWithObject:newCoord];
            newFirstTouchEvent.timeStamp = [NSArray arrayWithObject:@(newTimeStamp)];

            SDLTouchEvent* newSecondTouchEvent = [[SDLTouchEvent alloc] init];
            newSecondTouchEvent.touchEventId = @1;
            newSecondTouchEvent.coord = [NSArray arrayWithObject:newCoord];
            newSecondTouchEvent.timeStamp = [NSArray arrayWithObject:@(newTimeStamp)];

            newFirstTouch = [[SDLTouch alloc] initWithTouchEvent:newFirstTouchEvent];
            newSecondTouch = [[SDLTouch alloc] initWithTouchEvent:newSecondTouchEvent];
        });
        
        it(@"should update first point correctly", ^{
            pinchGesture.firstTouch = newFirstTouch;
            
            expect(@(pinchGesture.firstTouch.identifier)).to(equal(@(SDLTouchIdentifierFirstFinger)));
            expect(@(pinchGesture.firstTouch.location.x)).to(equal(@150));
            expect(@(pinchGesture.firstTouch.location.y)).to(equal(@250));
            expect(@(pinchGesture.firstTouch.timeStamp)).to(equal(@(newTimeStamp)));
            
            expect(@(pinchGesture.secondTouch.identifier)).to(equal(@(SDLTouchIdentifierSecondFinger)));
            expect(@(pinchGesture.secondTouch.location.x)).to(equal(@200));
            expect(@(pinchGesture.secondTouch.location.y)).to(equal(@300));
            expect(@(pinchGesture.secondTouch.timeStamp)).to(equal(@(secondTimeStamp)));
            
            expect(@(pinchGesture.distance)).to(beCloseTo(@(70.7107)).within(0.0001));
            expect(@(pinchGesture.center.x)).to(equal(@175));
            expect(@(pinchGesture.center.y)).to(equal(@275));
            
        });
        
        it(@"should update second point correctly", ^{
            pinchGesture.secondTouch = newSecondTouch;
            
            expect(@(pinchGesture.firstTouch.identifier)).to(equal(@(SDLTouchIdentifierFirstFinger)));
            expect(@(pinchGesture.firstTouch.location.x)).to(equal(@100));
            expect(@(pinchGesture.firstTouch.location.y)).to(equal(@200));
            expect(@(pinchGesture.firstTouch.timeStamp)).to(equal(@(timeStamp)));
            
            expect(@(pinchGesture.secondTouch.identifier)).to(equal(@(SDLTouchIdentifierSecondFinger)));
            expect(@(pinchGesture.secondTouch.location.x)).to(equal(@150));
            expect(@(pinchGesture.secondTouch.location.y)).to(equal(@250));
            expect(@(pinchGesture.secondTouch.timeStamp)).to(equal(@(newTimeStamp)));
            
            expect(@(pinchGesture.distance)).to(beCloseTo(@70.7107).within(0.0001));
            expect(@(pinchGesture.center.x)).to(equal(@125));
            expect(@(pinchGesture.center.y)).to(equal(@225));
        });
    });
});

QuickSpecEnd