Skip to content

Commit 03049f7

Browse files
Jesse Seidmanfacebook-github-bot
Jesse Seidman
authored andcommitted
Add IGListAdapterDelegate Methods 3/n
Summary: ## CONTEXT `IGListAdapterDelegate` currently has methods that are fired when an object is *first* displayed and when an object *ended* display on screen. For many use cases this works but there are use cases in which it would be helpful to know whenever a cell will be displayed or ends display. This is especially relevant in the `IGListAdapterDelegateAnnouncer` which will enable you to add code for tracking cells globally. This change also balances the `IGListAdapterDelegate` with `IGListDisplayDelegate` which has methods which include the cell as well ## PLAN This diff will be go out in multiple parts - delegate method addition (many file change due to our pattern of not preferring default implementation of protocol methods) - Update to `IGListDisplayHandler` and `IGListAdapterDelegateAnnouncer` for invocation of delegate methods - Unit test additions ## THIS DIFF This diff updates the unit tests of `IGListAdapterDelegateAnnouncerTests`, `IGListAdapterE2ETests` and `IGListDisplayHandlerTests`. We added new expectations and to existing unit tests because the functionality to confirm is already tested it was just only checking the existing delegate methods. The new tests check the new methods as well Reviewed By: maxolls Differential Revision: D70406065 fbshipit-source-id: ee054a21f0709b14081fdff38e22ab9710222794
1 parent d1d6f9d commit 03049f7

7 files changed

+52
-13
lines changed

Tests/IGListAdapterDelegateAnnouncerTests.m

+28-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
7-
87
#import <XCTest/XCTest.h>
98

109
#import <OCMock/OCMock.h>
@@ -17,6 +16,7 @@
1716
#import "IGTestDelegateDataSource.h"
1817
#import "IGTestObject.h"
1918
#import "IGListAdapterDelegateAnnouncer.h"
19+
#import "IGTestCell.h"
2020

2121
@interface IGListAdapterDelegateAnnouncerTests : XCTestCase
2222

@@ -27,6 +27,7 @@ @interface IGListAdapterDelegateAnnouncerTests : XCTestCase
2727

2828
@property (nonatomic, strong) UICollectionView *collectionView1;
2929
@property (nonatomic, strong) UICollectionView *collectionView2;
30+
3031
@property (nonatomic, strong) id<IGListTestCaseDataSource> dataSource1;
3132
@property (nonatomic, strong) id<IGListTestCaseDataSource> dataSource2;
3233
@property (nonatomic, strong) IGListAdapter *adapter1;
@@ -48,13 +49,16 @@ - (void)setUp {
4849

4950
self.collectionView2 = [[UICollectionView alloc] initWithFrame:self.window.bounds collectionViewLayout:[UICollectionViewFlowLayout new]];
5051
[self.window addSubview:self.collectionView2];
51-
52-
self.dataSource1 = [IGTestDelegateDataSource new];
53-
self.dataSource2 = [IGTestDelegateDataSource new];
52+
53+
IGTestDelegateDataSource *const dataSource1 = [IGTestDelegateDataSource new];
54+
self.dataSource1 = dataSource1;
55+
56+
IGTestDelegateDataSource *const dataSource2 = [IGTestDelegateDataSource new];
57+
self.dataSource2 = dataSource2;
5458

5559
self.adapter1 = [[IGListAdapter alloc] initWithUpdater:[IGListAdapterUpdater new] viewController:self.viewController];
5660
self.adapter1.globalDelegateAnnouncer = self.announcer;
57-
61+
5862
self.adapter2 = [[IGListAdapter alloc] initWithUpdater:[IGListAdapterUpdater new] viewController:self.viewController];
5963
self.adapter2.globalDelegateAnnouncer = self.announcer;
6064
}
@@ -77,19 +81,23 @@ - (void)setupAdapter2WithObjects:(NSArray *)objects {
7781

7882
- (void)test_whenShowingOneItem_withTwoListeners_withOneAdapter_thatBothListenersReceivesWillDisplay{
7983
[self setupAdapter1WithObjects:@[]];
80-
84+
8185
IGTestObject *const object = genTestObject(@1, @1);
8286
self.dataSource1.objects = @[
8387
object
8488
];
8589

90+
NSIndexPath *const indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
91+
8692
id mockDisplayHandler1 = [OCMockObject mockForProtocol:@protocol(IGListAdapterDelegate)];
8793
[self.announcer addListener:mockDisplayHandler1];
8894
[[mockDisplayHandler1 expect] listAdapter:self.adapter1 willDisplayObject:object atIndex:0];
89-
95+
[[mockDisplayHandler1 expect] listAdapter:self.adapter1 willDisplayObject:object cell: [OCMArg any] atIndexPath:indexPath];
96+
9097
id mockDisplayHandler2 = [OCMockObject mockForProtocol:@protocol(IGListAdapterDelegate)];
9198
[self.announcer addListener:mockDisplayHandler2];
9299
[[mockDisplayHandler2 expect] listAdapter:self.adapter1 willDisplayObject:object atIndex:0];
100+
[[mockDisplayHandler2 expect] listAdapter:self.adapter1 willDisplayObject:object cell: [OCMArg any] atIndexPath:indexPath];
93101

94102
XCTestExpectation *expectation = genExpectation;
95103
[self.adapter1 performUpdatesAnimated:NO completion:^(BOOL finished2) {
@@ -103,17 +111,21 @@ - (void)test_whenShowingOneItem_withTwoListeners_withOneAdapter_thatBothListener
103111

104112
- (void)test_whenRemovignOneItem_withTwoListeners_withOneAdapter_thatBothListenersReceivesEndDisplay {
105113
IGTestObject *const object = genTestObject(@1, @1);
114+
NSIndexPath *const zeroIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
115+
106116
[self setupAdapter1WithObjects:@[object]];
107117

108118
self.dataSource1.objects = @[];
109119

110120
id mockDisplayHandler1 = [OCMockObject mockForProtocol:@protocol(IGListAdapterDelegate)];
111121
[self.announcer addListener:mockDisplayHandler1];
112122
[[mockDisplayHandler1 expect] listAdapter:self.adapter1 didEndDisplayingObject:object atIndex:0];
113-
123+
[[mockDisplayHandler1 expect] listAdapter:self.adapter1 didEndDisplayingObject:object cell:[OCMArg any] atIndexPath: zeroIndexPath];
124+
114125
id mockDisplayHandler2 = [OCMockObject mockForProtocol:@protocol(IGListAdapterDelegate)];
115126
[self.announcer addListener:mockDisplayHandler2];
116127
[[mockDisplayHandler2 expect] listAdapter:self.adapter1 didEndDisplayingObject:object atIndex:0];
128+
[[mockDisplayHandler2 expect] listAdapter:self.adapter1 didEndDisplayingObject:object cell:[OCMArg any] atIndexPath: zeroIndexPath];
117129

118130
XCTestExpectation *expectation = genExpectation;
119131
[self.adapter1 performUpdatesAnimated:NO completion:^(BOOL finished2) {
@@ -130,8 +142,10 @@ - (void)test_whenRemovignOneItem_withTwoListeners_withOneAdapter_thatBothListene
130142
- (void)test_whenShowingTwoItems_withOneListeners_withTwoAdapters_thatBothItemsSendWillDisplay {
131143
[self setupAdapter1WithObjects:@[]];
132144
[self setupAdapter2WithObjects:@[]];
133-
145+
134146
IGTestObject *const object1 = genTestObject(@1, @1);
147+
NSIndexPath *const zeroIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
148+
135149
self.dataSource1.objects = @[
136150
object1
137151
];
@@ -143,9 +157,13 @@ - (void)test_whenShowingTwoItems_withOneListeners_withTwoAdapters_thatBothItemsS
143157

144158
id mockDisplayHandler = [OCMockObject mockForProtocol:@protocol(IGListAdapterDelegate)];
145159
[self.announcer addListener:mockDisplayHandler];
160+
146161
[[mockDisplayHandler expect] listAdapter:self.adapter1 willDisplayObject:object1 atIndex:0];
162+
[[mockDisplayHandler expect] listAdapter:self.adapter1 willDisplayObject:object1 cell: [OCMArg any] atIndexPath: zeroIndexPath];
163+
147164
[[mockDisplayHandler expect] listAdapter:self.adapter2 willDisplayObject:object2 atIndex:0];
148-
165+
[[mockDisplayHandler expect] listAdapter:self.adapter2 willDisplayObject:object2 cell: [OCMArg any] atIndexPath: zeroIndexPath];
166+
149167
XCTestExpectation *expectation = genExpectation;
150168
[self.adapter1 performUpdatesAnimated:NO completion:^(BOOL finished1) {
151169
[self.adapter2 performUpdatesAnimated:NO completion:^(BOOL finished2) {

Tests/IGListAdapterE2ETests.m

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ @implementation IGListAdapterE2ETests
3232

3333
- (void)setUp {
3434
self.workingRangeSize = 2;
35+
3536
self.dataSource = [IGTestDelegateDataSource new];
3637
[super setUp];
3738
}
@@ -820,10 +821,12 @@ - (void)test_whenItemDeleted_withDisplayDelegate_thatDelegateReceivesDeletedItem
820821
genTestObject(@2, @2),
821822
];
822823

824+
IGTestCell *const cell = (IGTestCell*)[self.collectionView cellForItemAtIndexPath:genIndexPath(0, 0)];
823825
id mockDisplayHandler = [OCMockObject mockForProtocol:@protocol(IGListAdapterDelegate)];
824826
self.adapter.delegate = mockDisplayHandler;
825827

826828
[[mockDisplayHandler expect] listAdapter:self.adapter didEndDisplayingObject:object atIndex:0];
829+
[[mockDisplayHandler expect] listAdapter:self.adapter didEndDisplayingObject:object cell: cell atIndexPath: [NSIndexPath indexPathForItem:0 inSection:0]];
827830

828831
XCTestExpectation *expectation = genExpectation;
829832
[self.adapter performUpdatesAnimated:YES completion:^(BOOL finished2) {
@@ -2665,7 +2668,7 @@ - (void)test_whenPerformingUpdates_withAdaptiveDiffing_thatCollectionViewCountsU
26652668
updater.adaptiveDiffingExperimentConfig = (IGListAdaptiveDiffingExperimentConfig) {
26662669
.enabled = YES,
26672670
};
2668-
2671+
26692672
[self setupWithObjects:@[
26702673
genTestObject(@1, @1),
26712674
genTestObject(@2, @2),
@@ -2696,7 +2699,7 @@ - (void)test_whenPerformingUpdates_withAdaptiveCoalescing_thatCollectionViewCoun
26962699
updater.adaptiveCoalescingExperimentConfig = (IGListAdaptiveCoalescingExperimentConfig) {
26972700
.enabled = YES,
26982701
};
2699-
2702+
27002703
[self setupWithObjects:@[
27012704
genTestObject(@1, @1),
27022705
genTestObject(@2, @2),

Tests/IGListDisplayHandlerTests.m

+11
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ - (void)test_whenDisplayingFirstCell_thatDisplayHandlerReceivesEvent {
6060
[[self.mockDisplayDelegate expect] listAdapter:self.adapter willDisplaySectionController:self.list cell:cell atIndex:path.item];
6161

6262
[[self.mockAdapterDelegate expect] listAdapter:self.adapter willDisplayObject:self.object atIndex:path.section];
63+
[[self.mockAdapterDelegate expect] listAdapter:self.adapter willDisplayObject:self.object cell:cell atIndexPath:path];
6364

6465
self.list.displayDelegate = self.mockDisplayDelegate;
6566
self.adapter.delegate = self.mockAdapterDelegate;
@@ -79,6 +80,7 @@ - (void)test_whenDisplayingSecondCell_thatDisplayHandlerReceivesEvent {
7980
[[self.mockDisplayDelegate expect] listAdapter:self.adapter willDisplaySectionController:self.list cell:cell atIndex:nextPath.item];
8081
[[self.mockDisplayDelegate reject] listAdapter:self.adapter willDisplaySectionController:self.list];
8182

83+
[[self.mockAdapterDelegate expect] listAdapter:self.adapter willDisplayObject:self.object cell:cell atIndexPath:nextPath];
8284
[[self.mockAdapterDelegate reject] listAdapter:self.adapter willDisplayObject:self.object atIndex:firstPath.section];
8385

8486
self.list.displayDelegate = self.mockDisplayDelegate;
@@ -105,6 +107,7 @@ - (void)test_whenEndDisplayingSecondToLastCell_thatDisplayHandlerReceivesEvent {
105107
[[self.mockDisplayDelegate expect] listAdapter:self.adapter didEndDisplayingSectionController:self.list cell:cellOne atIndex:firstPath.item];
106108

107109
[[self.mockAdapterDelegate reject] listAdapter:self.adapter didEndDisplayingObject:self.object atIndex:firstPath.section];
110+
[[self.mockAdapterDelegate expect] listAdapter:self.adapter didEndDisplayingObject:self.object cell:cellOne atIndexPath: firstPath];
108111

109112
self.list.displayDelegate = self.mockDisplayDelegate;
110113
self.adapter.delegate = self.mockAdapterDelegate;
@@ -125,6 +128,7 @@ - (void)test_whenEndDisplayingLastCell_thatDisplayHandlerReceivesEvent {
125128
[[self.mockDisplayDelegate expect] listAdapter:self.adapter didEndDisplayingSectionController:self.list cell:cell atIndex:firstPath.item];
126129

127130
[[self.mockAdapterDelegate expect] listAdapter:self.adapter didEndDisplayingObject:self.object atIndex:firstPath.section];
131+
[[self.mockAdapterDelegate expect] listAdapter:self.adapter didEndDisplayingObject:self.object cell:cell atIndexPath:firstPath];
128132

129133
self.list.displayDelegate = self.mockDisplayDelegate;
130134
self.adapter.delegate = self.mockAdapterDelegate;
@@ -158,10 +162,13 @@ - (void)test_whenEndDisplayingCell_withEndDisplayTwice_thatDisplayHandlerReceive
158162

159163
[[self.mockDisplayDelegate expect] listAdapter:self.adapter didEndDisplayingSectionController:self.list];
160164
[[self.mockDisplayDelegate expect] listAdapter:self.adapter didEndDisplayingSectionController:self.list cell:cell atIndex:firstPath.item];
165+
161166
[[self.mockAdapterDelegate expect] listAdapter:self.adapter didEndDisplayingObject:self.object atIndex:firstPath.section];
167+
[[self.mockAdapterDelegate expect] listAdapter:self.adapter didEndDisplayingObject:self.object cell:cell atIndexPath:firstPath];
162168

163169
[[self.mockDisplayDelegate reject] listAdapter:self.adapter didEndDisplayingSectionController:self.list];
164170
[[self.mockDisplayDelegate reject] listAdapter:self.adapter didEndDisplayingSectionController:self.list cell:cell atIndex:firstPath.item];
171+
165172
[[self.mockAdapterDelegate reject] listAdapter:self.adapter didEndDisplayingObject:self.object atIndex:firstPath.section];
166173

167174
self.list.displayDelegate = self.mockDisplayDelegate;
@@ -216,8 +223,12 @@ - (void)test_whenWillDisplaySupplementaryView_withCellDisplayedAfter_thatDisplay
216223
[[self.mockDisplayDelegate expect] listAdapter:self.adapter willDisplaySectionController:self.list cell:cell atIndex:path.item];
217224
[[self.mockAdapterDelegate reject] listAdapter:self.adapter willDisplayObject:self.object atIndex:path.item];
218225
[[self.mockDisplayDelegate reject] listAdapter:self.adapter willDisplaySectionController:self.list];
226+
[[self.mockAdapterDelegate expect] listAdapter:self.adapter willDisplayObject:self.object cell:cell atIndexPath:path];
219227

220228
[self.displayHandler willDisplayCell:cell forListAdapter:self.adapter sectionController:self.list object:self.object indexPath:path];
229+
230+
[self.mockDisplayDelegate verify];
231+
[self.mockAdapterDelegate verify];
221232
}
222233

223234
- (void)test_whenEndDisplayingSupplementaryView_withEndDisplayingTwice_thatDisplayHandlerReceivesOneEvent {

Tests/Objects/IGTestDelegateController.h

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#import <Foundation/Foundation.h>
99

1010
#import <IGListKit/IGListKit.h>
11+
#import "IGTestCell.h"
1112

1213
@class IGTestObject;
1314

@@ -29,4 +30,6 @@
2930
@property (nonatomic, assign) CGPoint initialAttributesOffset;
3031
@property (nonatomic, assign) CGPoint finalAttributesOffset;
3132

33+
@property (nonatomic, strong) IGTestCell *overrideCell;
34+
3235
@end

Tests/Objects/IGTestDelegateController.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ - (CGSize)sizeForItemAtIndex:(NSInteger)index {
3434
}
3535

3636
- (UICollectionViewCell *)cellForItemAtIndex:(NSInteger)index {
37-
IGTestCell *cell = [self.collectionContext dequeueReusableCellOfClass:IGTestCell.class
37+
IGTestCell *cell = _overrideCell ?: [self.collectionContext dequeueReusableCellOfClass:IGTestCell.class
3838
forSectionController:self atIndex:index];
3939
[[cell label] setText:[NSString stringWithFormat:@"%@", self.item.value]];
4040
[cell setDelegate:self];

Tests/Objects/IGTestDelegateDataSource.h

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import <IGListKit/IGListAdapterDataSource.h>
1111

1212
#import "IGListTestCase.h"
13+
#import "IGTestCell.h"
1314

1415
@class IGTestDelegateController;
1516
@class IGTestObject;
@@ -23,4 +24,6 @@ extern NSObject *const kIGTestDelegateDataSourceNoSectionControllerSubclass;
2324

2425
@property (nonatomic, copy) void (^cellConfigureBlock)(IGTestDelegateController *);
2526

27+
@property (nonatomic, strong) IGTestCell *overrideCell;
28+
2629
@end

Tests/Objects/IGTestDelegateDataSource.m

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ - (IGListSectionController *)listAdapter:(IGListAdapter *)listAdapter sectionCon
2929
}
3030
IGTestDelegateController *sectionController = [[IGTestDelegateController alloc] init];
3131
sectionController.cellConfigureBlock = self.cellConfigureBlock;
32+
sectionController.overrideCell = self.overrideCell;
3233
return sectionController;
3334
}
3435

0 commit comments

Comments
 (0)