forked from facebookarchive/AsyncDisplayKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASUICollectionViewTests.m
More file actions
164 lines (134 loc) · 7.41 KB
/
Copy pathASUICollectionViewTests.m
File metadata and controls
164 lines (134 loc) · 7.41 KB
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
//
// ASUICollectionViewTests.m
// AsyncDisplayKit
//
// Created by Adlai Holler on 8/18/16.
// Copyright © 2016 Facebook. All rights reserved.
//
#import <XCTest/XCTest.h>
#import <OCMock/OCMock.h>
#import <OCMock/NSInvocation+OCMAdditions.h>
@interface ASUICollectionViewTests : XCTestCase
@end
@implementation ASUICollectionViewTests
/// Test normal item-affiliated supplementary node
- (void)testNormalTwoIndexSupplementaryElement
{
[self _testSupplementaryNodeAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1] sectionCount:2 expectException:NO];
}
/// If your supp is indexPathForItem:inSection:, the section index must be in bounds
- (void)testThatSupplementariesWithItemIndexesMustBeWithinNormalSections
{
[self _testSupplementaryNodeAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:3] sectionCount:2 expectException:YES];
}
/// If your supp is indexPathWithIndex:, that's OK even if that section is out of bounds!
- (void)testThatSupplementariesWithOneIndexAreOKOutOfSectionBounds
{
[self _testSupplementaryNodeAtIndexPath:[NSIndexPath indexPathWithIndex:3] sectionCount:2 expectException:NO];
}
- (void)testThatNestedBatchCompletionsAreCalledInOrder
{
UICollectionViewLayout *layout = [[UICollectionViewLayout alloc] init];
id layoutMock = [OCMockObject partialMockForObject:layout];
UICollectionView *cv = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) collectionViewLayout:layoutMock];
id dataSource = [OCMockObject niceMockForProtocol:@protocol(UICollectionViewDataSource)];
cv.dataSource = dataSource;
XCTestExpectation *inner0 = [self expectationWithDescription:@"Inner completion 0 is called"];
XCTestExpectation *inner1 = [self expectationWithDescription:@"Inner completion 1 is called"];
XCTestExpectation *outer = [self expectationWithDescription:@"Outer completion is called"];
NSMutableArray<XCTestExpectation *> *completions = [NSMutableArray array];
[cv performBatchUpdates:^{
[cv performBatchUpdates:^{
} completion:^(BOOL finished) {
[completions addObject:inner0];
[inner0 fulfill];
}];
[cv performBatchUpdates:^{
} completion:^(BOOL finished) {
[completions addObject:inner1];
[inner1 fulfill];
}];
} completion:^(BOOL finished) {
[completions addObject:outer];
[outer fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
XCTAssertEqualObjects(completions, (@[ outer, inner0, inner1 ]), @"Expected completion order to be correct");
}
- (void)_testSupplementaryNodeAtIndexPath:(NSIndexPath *)indexPath sectionCount:(NSInteger)sectionCount expectException:(BOOL)shouldFail
{
UICollectionViewLayoutAttributes *attr = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:@"SuppKind" withIndexPath:indexPath];
attr.frame = CGRectMake(0, 0, 20, 20);
UICollectionViewLayout *layout = [[UICollectionViewLayout alloc] init];
id layoutMock = [OCMockObject partialMockForObject:layout];
[[[[layoutMock expect] ignoringNonObjectArgs] andReturn:@[ attr ]] layoutAttributesForElementsInRect:CGRectZero];
UICollectionView *cv = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) collectionViewLayout:layoutMock];
[cv registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:@"SuppKind" withReuseIdentifier:@"ReuseID"];
id dataSource = [OCMockObject niceMockForProtocol:@protocol(UICollectionViewDataSource)];
__block id view = nil;
[[[dataSource expect] andDo:^(NSInvocation *invocation) {
NSIndexPath *indexPath = [invocation getArgumentAtIndexAsObject:4];
view = [cv dequeueReusableSupplementaryViewOfKind:@"SuppKind" withReuseIdentifier:@"ReuseID" forIndexPath:indexPath];
[invocation setReturnValue:&view];
}] collectionView:cv viewForSupplementaryElementOfKind:@"SuppKind" atIndexPath:indexPath];
[[[dataSource expect] andReturnValue:[NSNumber numberWithInteger:sectionCount]] numberOfSectionsInCollectionView:cv];
cv.dataSource = dataSource;
if (shouldFail) {
XCTAssertThrowsSpecificNamed([cv layoutIfNeeded], NSException, NSInternalInconsistencyException);
} else {
[cv layoutIfNeeded];
XCTAssertEqualObjects(attr, [cv layoutAttributesForSupplementaryElementOfKind:@"SuppKind" atIndexPath:indexPath]);
XCTAssertEqual(view, [cv supplementaryViewForElementKind:@"SuppKind" atIndexPath:indexPath]);
}
[dataSource verify];
[layoutMock verify];
}
- (void)testThatIssuingAnUpdateBeforeInitialReloadIsUnacceptable
{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
UICollectionView *cv = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) collectionViewLayout:layout];
id dataSource = [OCMockObject niceMockForProtocol:@protocol(UICollectionViewDataSource)];
// Setup empty data source – 0 sections, 0 items
[[[dataSource stub] andDo:^(NSInvocation *invocation) {
NSIndexPath *indexPath = [invocation getArgumentAtIndexAsObject:3];
__autoreleasing UICollectionViewCell *view = [cv dequeueReusableCellWithReuseIdentifier:@"CellID" forIndexPath:indexPath];
[invocation setReturnValue:&view];
}] collectionView:cv cellForItemAtIndexPath:OCMOCK_ANY];
[[[dataSource stub] andReturnValue:[NSNumber numberWithInteger:0]] numberOfSectionsInCollectionView:cv];
[[[dataSource stub] andReturnValue:[NSNumber numberWithInteger:0]] collectionView:cv numberOfItemsInSection:0];
[cv registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CellID"];
cv.dataSource = dataSource;
// Update data source – 1 section, 0 items
[[[dataSource stub] andReturnValue:[NSNumber numberWithInteger:1]] numberOfSectionsInCollectionView:cv];
/**
* Inform collection view – insert section 0
* Throws exception because collection view never saw the data source have 0 sections.
* so it's going to read "oldSectionCount" now and get 1. It will also read
* "newSectionCount" and get 1. Then it'll throw because "oldSectionCount(1) + insertedCount(1) != newSectionCount(1)".
* To workaround this, you could add `[cv numberOfSections]` before the data source is updated to
* trigger the collection view to read oldSectionCount=0.
*/
XCTAssertThrowsSpecificNamed([cv insertSections:[NSIndexSet indexSetWithIndex:0]], NSException, NSInternalInconsistencyException);
}
// If you put reloadData in a batch update, collection view will ignore it and perform the normal
// update validation i.e. throw an exception if your data source counts changed.
- (void)testThatPuttingReloadDataInABatchUpdateDoesntWork
{
UICollectionViewLayout *layout = [[UICollectionViewLayout alloc] init];
UICollectionView *cv = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) collectionViewLayout:layout];
id dataSource = [OCMockObject niceMockForProtocol:@protocol(UICollectionViewDataSource)];
// Start data source at 1 section, 1 item
[[[dataSource stub] andReturnValue:[NSNumber numberWithInteger:1]] numberOfSectionsInCollectionView:cv];
[[[dataSource expect] andReturnValue:[NSNumber numberWithInteger:1]] collectionView:cv numberOfItemsInSection:0];
cv.dataSource = dataSource;
// Verify initial data.
XCTAssertEqual([cv numberOfSections], 1);
XCTAssertEqual([cv numberOfItemsInSection:0], 1);
[dataSource verify];
XCTAssertThrows([cv performBatchUpdates:^{
// Change data source to 1 section, 2 items
[[[dataSource stub] andReturnValue:[NSNumber numberWithInteger:2]] collectionView:cv numberOfItemsInSection:0];
[cv reloadData];
} completion:nil]);
}
@end