-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathIGListDisplayHandlerTests.m
247 lines (183 loc) · 13 KB
/
IGListDisplayHandlerTests.m
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <XCTest/XCTest.h>
#import <OCMock/OCMock.h>
#import <IGListKit/IGListKit.h>
#import "IGListAdapterInternal.h"
#import "IGListDisplayHandler.h"
#import "IGListTestAdapterDataSource.h"
#import "IGListTestSection.h"
@interface IGListDisplayHandlerTests : XCTestCase
@property (nonatomic, strong) IGListDisplayHandler *displayHandler;
@property (nonatomic, strong) IGListAdapter *adapter;
@property (nonatomic, strong) id mockDisplayDelegate;
@property (nonatomic, strong) id mockAdapterDelegate;
@property (nonatomic, strong) id mockAdapterDataSource;
@property (nonatomic, strong) IGListTestSection *list;
@property (nonatomic, strong) id object;
@end
@implementation IGListDisplayHandlerTests
- (void)setUp {
[super setUp];
self.list = [[IGListTestSection alloc] init];
self.object = [[NSObject alloc] init];
self.displayHandler = [[IGListDisplayHandler alloc] init];
UICollectionView *collectionView = [OCMockObject niceMockForClass:[UICollectionView class]];
self.mockAdapterDataSource = [OCMockObject niceMockForProtocol:@protocol(IGListAdapterDataSource)];
IGListAdapterUpdater *updater = [IGListAdapterUpdater new];
self.adapter = [[IGListAdapter alloc] initWithUpdater:updater viewController:nil];
self.adapter.collectionView = collectionView;
self.adapter.dataSource = self.mockAdapterDataSource;
self.mockDisplayDelegate = [OCMockObject mockForProtocol:@protocol(IGListDisplayDelegate)];
self.mockAdapterDelegate = [OCMockObject mockForProtocol:@protocol(IGListAdapterDelegate)];
}
- (void)tearDown {
[super tearDown];
self.list.displayDelegate = nil;
self.adapter.delegate = nil;
}
- (void)test_whenDisplayingFirstCell_thatDisplayHandlerReceivesEvent {
NSIndexPath *path = [NSIndexPath new];
UICollectionViewCell *cell = [UICollectionViewCell new];
[[self.mockDisplayDelegate expect] listAdapter:self.adapter willDisplaySectionController:self.list];
[[self.mockDisplayDelegate expect] listAdapter:self.adapter willDisplaySectionController:self.list cell:cell atIndex:path.item];
[[self.mockAdapterDelegate expect] listAdapter:self.adapter willDisplayObject:self.object atIndex:path.section];
self.list.displayDelegate = self.mockDisplayDelegate;
self.adapter.delegate = self.mockAdapterDelegate;
[self.displayHandler willDisplayCell:cell forListAdapter:self.adapter sectionController:self.list object:self.object indexPath:path];
[self.mockDisplayDelegate verify];
[self.mockAdapterDelegate verify];
}
- (void)test_whenDisplayingSecondCell_thatDisplayHandlerReceivesEvent {
// simulate first cell appearing in the collection view
NSIndexPath *firstPath = [NSIndexPath indexPathForItem:0 inSection:0];
[self.displayHandler willDisplayCell:[UICollectionViewCell new] forListAdapter:self.adapter sectionController:self.list object:self.object indexPath:firstPath];
NSIndexPath *nextPath = [NSIndexPath indexPathForItem:1 inSection:0];
UICollectionViewCell *cell = [UICollectionViewCell new];
[[self.mockDisplayDelegate expect] listAdapter:self.adapter willDisplaySectionController:self.list cell:cell atIndex:nextPath.item];
[[self.mockDisplayDelegate reject] listAdapter:self.adapter willDisplaySectionController:self.list];
[[self.mockAdapterDelegate reject] listAdapter:self.adapter willDisplayObject:self.object atIndex:firstPath.section];
self.list.displayDelegate = self.mockDisplayDelegate;
self.adapter.delegate = self.mockAdapterDelegate;
[self.displayHandler willDisplayCell:cell forListAdapter:self.adapter sectionController:self.list object:self.object indexPath:nextPath];
[self.mockDisplayDelegate verify];
[self.mockAdapterDelegate verify];
}
- (void)test_whenEndDisplayingSecondToLastCell_thatDisplayHandlerReceivesEvent {
// simulate first cell appearing in the collection view
NSIndexPath *firstPath = [NSIndexPath indexPathForItem:0 inSection:0];
UICollectionViewCell *cellOne = [UICollectionViewCell new];
UICollectionViewCell *cellTwo = [UICollectionViewCell new];
[self.displayHandler willDisplayCell:cellOne forListAdapter:self.adapter sectionController:self.list object:self.object indexPath:firstPath];
NSIndexPath *nextPath = [NSIndexPath indexPathForItem:1 inSection:0];
[self.displayHandler willDisplayCell:cellTwo forListAdapter:self.adapter sectionController:self.list object:self.object indexPath:nextPath];
[[self.mockDisplayDelegate reject] listAdapter:self.adapter didEndDisplayingSectionController:self.list];
[[self.mockDisplayDelegate expect] listAdapter:self.adapter didEndDisplayingSectionController:self.list cell:cellOne atIndex:firstPath.item];
[[self.mockAdapterDelegate reject] listAdapter:self.adapter didEndDisplayingObject:self.object atIndex:firstPath.section];
self.list.displayDelegate = self.mockDisplayDelegate;
self.adapter.delegate = self.mockAdapterDelegate;
[self.displayHandler didEndDisplayingCell:cellOne forListAdapter:self.adapter sectionController:self.list indexPath:firstPath];
[self.mockDisplayDelegate verify];
[self.mockAdapterDelegate verify];
}
- (void)test_whenEndDisplayingLastCell_thatDisplayHandlerReceivesEvent {
// simulate first cell appearing then disappearing in the collection view
NSIndexPath *firstPath = [NSIndexPath indexPathForItem:0 inSection:0];
UICollectionViewCell *cell = [UICollectionViewCell new];
[self.displayHandler willDisplayCell:cell forListAdapter:self.adapter sectionController:self.list object:self.object indexPath:firstPath];
[[self.mockDisplayDelegate expect] listAdapter:self.adapter didEndDisplayingSectionController:self.list];
[[self.mockDisplayDelegate expect] listAdapter:self.adapter didEndDisplayingSectionController:self.list cell:cell atIndex:firstPath.item];
[[self.mockAdapterDelegate expect] listAdapter:self.adapter didEndDisplayingObject:self.object atIndex:firstPath.section];
self.list.displayDelegate = self.mockDisplayDelegate;
self.adapter.delegate = self.mockAdapterDelegate;
[self.displayHandler didEndDisplayingCell:cell forListAdapter:self.adapter sectionController:self.list indexPath:firstPath];
[self.mockDisplayDelegate verify];
[self.mockAdapterDelegate verify];
}
- (void)test_whenEndDisplayingCell_withCellNeverDisplayed_thatDisplayHandlerReceivesNoEvent {
//simulate a cell received didEndDisplay when it didn't receive willDisplay. OS 7 issue only
NSIndexPath *firstPath = [NSIndexPath indexPathForItem:0 inSection:0];
UICollectionViewCell *cell = [UICollectionViewCell new];
// all following methods shouldn't be called.
[[self.mockDisplayDelegate reject] listAdapter:self.adapter didEndDisplayingSectionController:self.list];
[[self.mockDisplayDelegate reject] listAdapter:self.adapter didEndDisplayingSectionController:self.list cell:cell atIndex:firstPath.item];
[[self.mockAdapterDelegate reject] listAdapter:self.adapter didEndDisplayingObject:self.object atIndex:firstPath.section];
self.list.displayDelegate = self.mockDisplayDelegate;
self.adapter.delegate = self.mockAdapterDelegate;
[self.displayHandler didEndDisplayingCell:cell forListAdapter:self.adapter sectionController:self.list indexPath:firstPath];
}
- (void)test_whenEndDisplayingCell_withEndDisplayTwice_thatDisplayHandlerReceivesOneEvent {
//simulate a cell received didEndDisplay twice but willDisplay once. OS 7 issue only
NSIndexPath *firstPath = [NSIndexPath indexPathForItem:0 inSection:0];
UICollectionViewCell *cell = [UICollectionViewCell new];
[self.displayHandler willDisplayCell:cell forListAdapter:self.adapter sectionController:self.list object:self.object indexPath:firstPath];
[[self.mockDisplayDelegate expect] listAdapter:self.adapter didEndDisplayingSectionController:self.list];
[[self.mockDisplayDelegate expect] listAdapter:self.adapter didEndDisplayingSectionController:self.list cell:cell atIndex:firstPath.item];
[[self.mockAdapterDelegate expect] listAdapter:self.adapter didEndDisplayingObject:self.object atIndex:firstPath.section];
[[self.mockDisplayDelegate reject] listAdapter:self.adapter didEndDisplayingSectionController:self.list];
[[self.mockDisplayDelegate reject] listAdapter:self.adapter didEndDisplayingSectionController:self.list cell:cell atIndex:firstPath.item];
[[self.mockAdapterDelegate reject] listAdapter:self.adapter didEndDisplayingObject:self.object atIndex:firstPath.section];
self.list.displayDelegate = self.mockDisplayDelegate;
self.adapter.delegate = self.mockAdapterDelegate;
//first call
[self.displayHandler didEndDisplayingCell:cell forListAdapter:self.adapter sectionController:self.list indexPath:firstPath];
//second call
[self.displayHandler didEndDisplayingCell:cell forListAdapter:self.adapter sectionController:self.list indexPath:firstPath];
[self.mockDisplayDelegate verify];
[self.mockAdapterDelegate verify];
}
- (void)test_whenCellInserted_withDisplayedCellExistingAtPath_thatDisplayHandlerReceivesCorrectParams {
// simulate first cell appearing in the collection view
NSIndexPath *path = [NSIndexPath indexPathForItem:0 inSection:0];
UICollectionViewCell *cellOne = [UICollectionViewCell new];
// display the "old" cell/object
[self.displayHandler willDisplayCell:cellOne forListAdapter:self.adapter sectionController:self.list object:self.object indexPath:path];
// simulate a new object being inserted into the index path of the old section
IGListTestSection *anotherList = [IGListTestSection new];
id anotherObject = [NSObject new];
UICollectionViewCell *anotherCell = [UICollectionViewCell new];
[[self.mockDisplayDelegate expect] listAdapter:self.adapter willDisplaySectionController:anotherList];
[[self.mockDisplayDelegate expect] listAdapter:self.adapter willDisplaySectionController:anotherList cell:anotherCell atIndex:path.item];
anotherList.displayDelegate = self.mockDisplayDelegate;
[self.displayHandler willDisplayCell:anotherCell forListAdapter:self.adapter sectionController:anotherList object:anotherObject indexPath:path];
[self.mockDisplayDelegate verify];
}
- (void)test_whenWillDisplaySupplementaryView_withCellDisplayedAfter_thatDisplayHandlerReceivesOneEvent {
NSIndexPath *path = [NSIndexPath indexPathForItem:0 inSection:0];
UICollectionReusableView *view = [UICollectionReusableView new];
UICollectionViewCell *cell = [UICollectionViewCell new];
self.list.displayDelegate = self.mockDisplayDelegate;
self.adapter.delegate = self.mockAdapterDelegate;
[[self.mockDisplayDelegate expect] listAdapter:self.adapter willDisplaySectionController:self.list];
[[self.mockAdapterDelegate expect] listAdapter:self.adapter willDisplayObject:self.object atIndex:path.section];
[self.displayHandler willDisplaySupplementaryView:view forListAdapter:self.adapter sectionController:self.list object:self.object indexPath:path];
[self.mockDisplayDelegate verify];
[self.mockAdapterDelegate verify];
[[self.mockDisplayDelegate expect] listAdapter:self.adapter willDisplaySectionController:self.list cell:cell atIndex:path.item];
[[self.mockAdapterDelegate reject] listAdapter:self.adapter willDisplayObject:self.object atIndex:path.item];
[[self.mockDisplayDelegate reject] listAdapter:self.adapter willDisplaySectionController:self.list];
[self.displayHandler willDisplayCell:cell forListAdapter:self.adapter sectionController:self.list object:self.object indexPath:path];
}
- (void)test_whenEndDisplayingSupplementaryView_withEndDisplayingTwice_thatDisplayHandlerReceivesOneEvent {
NSIndexPath *path = [NSIndexPath indexPathForItem:0 inSection:0];
UICollectionReusableView *view = [UICollectionReusableView new];
[self.displayHandler willDisplaySupplementaryView:view forListAdapter:self.adapter sectionController:self.list object:self.object indexPath:path];
[[self.mockDisplayDelegate expect] listAdapter:self.adapter didEndDisplayingSectionController:self.list];
[[self.mockAdapterDelegate expect] listAdapter:self.adapter didEndDisplayingObject:self.object atIndex:path.section];
[[self.mockDisplayDelegate reject] listAdapter:self.adapter didEndDisplayingSectionController:self.list];
[[self.mockDisplayDelegate reject] listAdapter:self.adapter didEndDisplayingSectionController:self.list cell:[OCMArg any] atIndex:path.item];
[[self.mockAdapterDelegate reject] listAdapter:self.adapter didEndDisplayingObject:self.object atIndex:path.section];
self.list.displayDelegate = self.mockDisplayDelegate;
self.adapter.delegate = self.mockAdapterDelegate;
//first call
[self.displayHandler didEndDisplayingSupplementaryView:view forListAdapter:self.adapter sectionController:self.list indexPath:path];
//second call
[self.displayHandler didEndDisplayingSupplementaryView:view forListAdapter:self.adapter sectionController:self.list indexPath:path];
[self.mockDisplayDelegate verify];
[self.mockAdapterDelegate verify];
}
@end