This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathUIViewSpec+Spec.mm
341 lines (270 loc) · 12.7 KB
/
UIViewSpec+Spec.mm
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
#import "Cedar.h"
#import "UIView+Spec.h"
#import "UIView+StubbedGestureRecognizers.h"
#import "Target.h"
#import "UIGestureRecognizer+Spec.h"
using namespace Cedar::Matchers;
using namespace Cedar::Doubles;
SPEC_BEGIN(UIViewSpec_Spec)
describe(@"UIView+Spec", ^{
__block UIView *view;
__block Target *target, *otherTarget;
beforeEach(^{
view = [[UIView alloc] init];
target = [[Target alloc] init];
spy_on(target);
otherTarget = [[Target alloc] init];
spy_on(otherTarget);
});
describe(@"tapping on the view", ^{
beforeEach(^{
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] init];
[tapRecognizer addTarget:target action:@selector(hello)];
UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] init];
[swipeRecognizer addTarget:otherTarget action:@selector(hello)];
[view addGestureRecognizer:tapRecognizer];
[view addGestureRecognizer:swipeRecognizer];
});
it(@"should dispatch tap events when you call -tap", ^{
[view tap];
target should have_received(@selector(hello));
otherTarget should_not have_received(@selector(hello));
});
});
describe(@"long pressing on the view", ^{
beforeEach(^{
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] init];
[longPressRecognizer addTarget:target action:@selector(hello)];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] init];
[tapRecognizer addTarget:otherTarget action:@selector(hello)];
[view addGestureRecognizer:longPressRecognizer];
[view addGestureRecognizer:tapRecognizer];
});
it(@"should dispatch longPress events when you call -longPress", ^{
[view longPress];
target should have_received(@selector(hello));
otherTarget should_not have_received(@selector(hello));
});
});
describe(@"swiping the view", ^{
beforeEach(^{
UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] init];
[swipeRecognizer addTarget:target action:@selector(hello)];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] init];
[tapRecognizer addTarget:otherTarget action:@selector(hello)];
[view addGestureRecognizer:tapRecognizer];
[view addGestureRecognizer:swipeRecognizer];
});
it(@"should dispatch swipe events when you call -swipe", ^{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated"
[view swipe];
#pragma clang diagnostic pop
target should have_received(@selector(hello));
otherTarget should_not have_received(@selector(hello));
});
});
describe(@"swiping the view with a direction", ^{
__block Target *upTarget, *leftTarget, *rightTarget, *downTarget;
beforeEach(^{
upTarget = nice_fake_for([Target class]);
leftTarget = nice_fake_for([Target class]);
rightTarget = nice_fake_for([Target class]);
downTarget = nice_fake_for([Target class]);
UISwipeGestureRecognizer *upSwipeRecognizer = [[UISwipeGestureRecognizer alloc] init];
upSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
[upSwipeRecognizer addTarget:upTarget action:@selector(hello)];
UISwipeGestureRecognizer *leftSwipeRecognizer = [[UISwipeGestureRecognizer alloc] init];
leftSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[leftSwipeRecognizer addTarget:leftTarget action:@selector(hello)];
UISwipeGestureRecognizer *rightSwipeRecognizer = [[UISwipeGestureRecognizer alloc] init];
rightSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[rightSwipeRecognizer addTarget:rightTarget action:@selector(hello)];
UISwipeGestureRecognizer *downSwipeRecognizer = [[UISwipeGestureRecognizer alloc] init];
downSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
[downSwipeRecognizer addTarget:downTarget action:@selector(hello)];
[view addGestureRecognizer:upSwipeRecognizer];
[view addGestureRecognizer:downSwipeRecognizer];
[view addGestureRecognizer:leftSwipeRecognizer];
[view addGestureRecognizer:rightSwipeRecognizer];
});
it(@"should swipe events for the up direction when you call -swipeWithDirection: with UISwipeGestureRecognizerDirectionUp", ^{
[view swipeInDirection:UISwipeGestureRecognizerDirectionUp];
upTarget should have_received(@selector(hello));
downTarget should_not have_received(@selector(hello));
leftTarget should_not have_received(@selector(hello));
rightTarget should_not have_received(@selector(hello));
});
it(@"should swipe events for the up direction when you call -swipeWithDirection: with UISwipeGestureRecognizerDirectionDown", ^{
[view swipeInDirection:UISwipeGestureRecognizerDirectionDown];
downTarget should have_received(@selector(hello));
upTarget should_not have_received(@selector(hello));
leftTarget should_not have_received(@selector(hello));
rightTarget should_not have_received(@selector(hello));
});
it(@"should swipe events for the up direction when you call -swipeWithDirection: with UISwipeGestureRecognizerDirectionLeft", ^{
[view swipeInDirection:UISwipeGestureRecognizerDirectionLeft];
leftTarget should have_received(@selector(hello));
upTarget should_not have_received(@selector(hello));
downTarget should_not have_received(@selector(hello));
rightTarget should_not have_received(@selector(hello));
});
it(@"should swipe events for the up direction when you call -swipeWithDirection: with UISwipeGestureRecognizerDirectionRight", ^{
[view swipeInDirection:UISwipeGestureRecognizerDirectionRight];
rightTarget should have_received(@selector(hello));
upTarget should_not have_received(@selector(hello));
downTarget should_not have_received(@selector(hello));
leftTarget should_not have_received(@selector(hello));
});
});
#if !TARGET_OS_TV
describe(@"pinching the view", ^{
beforeEach(^{
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] init];
[pinchRecognizer addTarget:target action:@selector(hello)];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] init];
[tapRecognizer addTarget:otherTarget action:@selector(hello)];
[view addGestureRecognizer:tapRecognizer];
[view addGestureRecognizer:pinchRecognizer];
});
it(@"should dispatch pinch events when you call -pinch", ^{
[view pinch];
target should have_received(@selector(hello));
otherTarget should_not have_received(@selector(hello));
});
});
#endif
describe(@"finding subviews by accessibility identifier", ^{
__block UIView *view;
__block UIView *subview1;
__block UIView *subview2;
beforeEach(^{
view = [[UIView alloc] init];
subview1 = [[UIView alloc] init];
subview2 = [[UIView alloc] init];
[subview1 setAccessibilityIdentifier:@"I, Robot"];
[subview2 setAccessibilityIdentifier:@"Foundation"];
[view addSubview:subview1];
[view addSubview:subview2];
});
it(@"should return a subview with matching accessibility identifier", ^{
[view subviewWithAccessibilityIdentifier:@"I, Robot"] should equal(subview1);
[view subviewWithAccessibilityIdentifier:@"Foundation"] should equal(subview2);
[view subviewWithAccessibilityIdentifier:@"Kryten"] should be_nil;
});
});
describe(@"finding the first subview of a class", ^{
__block UIView *view;
__block UIView *subview1;
__block UILabel *subview2;
__block UILabel *subsubview;
beforeEach(^{
view = [[UIView alloc] init];
subview1 = [[UIView alloc] init];
subview2 = [[UILabel alloc] init];
subsubview = [[UILabel alloc] init];
[view addSubview:subview1];
[view addSubview:subview2];
[subview1 addSubview:subsubview];
});
it(@"should return the closest subview in the tree that is of the given class", ^{
[view firstSubviewOfClass:[UILabel class]] should equal(subview2);
});
});
describe(@"determining if view is completely on screen", ^{
__block UIView *rootView;
beforeEach(^{
rootView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
});
context(@"if the view is visible", ^{
it(@"should be visible", ^{
[rootView isTrulyVisible] should be_truthy;
});
});
context(@"if a view is hidden", ^{
beforeEach(^{
rootView.hidden = YES;
});
it(@"should not be visible", ^{
[rootView isTrulyVisible] should be_falsy;
});
});
context(@"if a view has 0 alpha", ^{
beforeEach(^{
rootView.alpha = 0;
});
it(@"should not be visible", ^{
[rootView isTrulyVisible] should be_falsy;
});
});
context(@"if a view has clipsToBounds turned on", ^{
beforeEach(^{
rootView.clipsToBounds = YES;
});
context(@"and it has no width", ^{
beforeEach(^{
rootView.frame = CGRectMake(0, 0, 0, 50);
});
it(@"should not be visible", ^{
[rootView isTrulyVisible] should be_falsy;
});
});
context(@"and it has no height", ^{
beforeEach(^{
rootView.frame = CGRectMake(0, 0, 50, 0);
});
it(@"should not be visible", ^{
[rootView isTrulyVisible] should be_falsy;
});
});
});
context(@"if a view has clipsToBounds turned off", ^{
beforeEach(^{
rootView.clipsToBounds = NO;
});
context(@"when the view has subviews", ^{
context(@"and it has no width or no height", ^{
beforeEach(^{
[rootView addSubview:[UIView new]];
rootView.frame = CGRectMake(0, 0, 0, 50);
});
it(@"should still be considered visible", ^{
[rootView isTrulyVisible] should be_truthy;
});
});
});
context(@"when the view does not have any subviews", ^{
context(@"and it has no width or no height", ^{
beforeEach(^{
rootView.frame = CGRectMake(0, 0, 0, 50);
});
it(@"should not be visible", ^{
[rootView isTrulyVisible] should be_falsy;
});
});
});
});
context(@"if there is a view inside of another view", ^{
__block UIView *childView;
beforeEach(^{
childView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[rootView addSubview:childView];
});
context(@"and the superview and the child view are both visible", ^{
it(@"should be visible", ^{
[childView isTrulyVisible] should be_truthy;
});
});
context(@"and the superview is not visible", ^{
beforeEach(^{
rootView.alpha = 0;
[rootView isTrulyVisible] should be_falsy;
});
it(@"should not be visible", ^{
[childView isTrulyVisible] should be_falsy;
});
});
});
});
});
SPEC_END