forked from Instagram/IGListKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIGListAdapterTests.m
1136 lines (924 loc) · 58.8 KB
/
IGListAdapterTests.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import <objc/runtime.h>
#import <XCTest/XCTest.h>
#import <OCMock/OCMock.h>
#import <IGListKit/IGListKit.h>
#import "IGListAdapterInternal.h"
#import "IGListTestAdapterDataSource.h"
#import "IGListTestAdapterHorizontalDataSource.h"
#import "IGListTestSection.h"
#import "IGTestSupplementarySource.h"
#import "IGTestNibSupplementaryView.h"
#define IGAssertEqualPoint(point, x, y, ...) \
do { \
CGPoint p = CGPointMake(x, y); \
XCTAssertEqual(CGPointEqualToPoint(point, p), YES); \
} while(0)
#define IGAssertEqualSize(size, w, h, ...) \
do { \
CGSize s = CGSizeMake(w, h); \
XCTAssertEqual(CGSizeEqualToSize(size, s), YES); \
} while(0)
@interface IGListAdapterTests : XCTestCase
// infra does not hold a strong ref to collection view
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) IGListAdapter *adapter;
@property (nonatomic, strong) IGListTestAdapterDataSource *dataSource;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) UIWindow *window;
@end
@implementation IGListAdapterTests
- (void)setUp {
[super setUp];
// minimum line spacing, item size, and minimum interim spacing are all set in IGListTestSection
self.window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
self.layout = [[UICollectionViewFlowLayout alloc] init];
self.collectionView = [[UICollectionView alloc] initWithFrame:self.window.bounds collectionViewLayout:self.layout];
[self.window addSubview:self.collectionView];
// syncronous reloads so we dont have to do expectations or other nonsense
IGListReloadDataUpdater *updater = [[IGListReloadDataUpdater alloc] init];
self.dataSource = [[IGListTestAdapterDataSource alloc] init];
self.adapter = [[IGListAdapter alloc] initWithUpdater:updater
viewController:nil
workingRangeSize:0];
self.adapter.collectionView = self.collectionView;
self.adapter.dataSource = self.dataSource;
}
- (void)tearDown {
[super tearDown];
self.window = nil;
self.collectionView = nil;
self.adapter = nil;
self.dataSource = nil;
self.layout = nil;
}
- (void)test_whenAdapterNotUpdated_withDataSourceUpdated_thatAdapterHasNoSectionControllers {
self.dataSource.objects = @[@0, @1, @2];
XCTAssertNil([self.adapter sectionControllerForObject:@0]);
XCTAssertNil([self.adapter sectionControllerForObject:@1]);
XCTAssertNil([self.adapter sectionControllerForObject:@2]);
}
- (void)test_whenAdapterUpdated_thatAdapterHasSectionControllers {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter performUpdatesAnimated:YES completion:nil];
XCTAssertNotNil([self.adapter sectionControllerForObject:@0]);
XCTAssertNotNil([self.adapter sectionControllerForObject:@1]);
XCTAssertNotNil([self.adapter sectionControllerForObject:@2]);
}
- (void)test_whenAdapterReloaded_thatAdapterHasSectionControllers {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter reloadDataWithCompletion:nil];
XCTAssertNotNil([self.adapter sectionControllerForObject:@0]);
XCTAssertNotNil([self.adapter sectionControllerForObject:@1]);
XCTAssertNotNil([self.adapter sectionControllerForObject:@2]);
}
- (void)test_whenAdapterUpdated_thatSectionControllerHasSection {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter performUpdatesAnimated:YES completion:nil];
IGListSectionController <IGListSectionType> * list = [self.adapter sectionControllerForObject:@1];
XCTAssertEqual([self.adapter sectionForSectionController:list], 1);
}
- (void)test_whenAdapterUpdated_withUnknownItem_thatSectionControllerHasNoSection {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter performUpdatesAnimated:YES completion:nil];
IGListSectionController <IGListSectionType> * randomList = [[IGListTestSection alloc] init];
XCTAssertEqual([self.adapter sectionForSectionController:randomList], NSNotFound);
}
- (void)test_whenQueryingAdapter_withUnknownItem_thatSectionControllerIsNil {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter performUpdatesAnimated:YES completion:nil];
XCTAssertNil([self.adapter sectionControllerForObject:@3]);
}
- (void)test_whenAdapterUpdated_thatSectionControllerHasCorrectObject {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter performUpdatesAnimated:YES completion:nil];
IGListSectionController <IGListSectionType> * list = [self.adapter sectionControllerForObject:@1];
XCTAssertEqual([self.adapter objectForSectionController:list], @1);
}
- (void)test_whenQueryingAdapter_withUnknownItem_thatObjectForSectionControllerIsNil {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter performUpdatesAnimated:YES completion:nil];
IGListSectionController <IGListSectionType> * randomList = [[IGListTestSection alloc] init];
XCTAssertNil([self.adapter objectForSectionController:randomList]);
}
- (void)test_whenQueryingIndexPaths_withSectionController_thatPathsAreEqual {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter performUpdatesAnimated:YES completion:nil];
IGListSectionController <IGListSectionType> * second = [self.adapter sectionControllerForObject:@1];
NSArray *paths0 = [self.adapter indexPathsFromSectionController:second
indexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2, 4)]
usePreviousIfInUpdateBlock:NO];
NSArray *expected = @[
[NSIndexPath indexPathForItem:2 inSection:1],
[NSIndexPath indexPathForItem:3 inSection:1],
[NSIndexPath indexPathForItem:4 inSection:1],
[NSIndexPath indexPathForItem:5 inSection:1],
];
XCTAssertEqualObjects(paths0, expected);
}
- (void)test_whenQueryingIndexPaths_insideBatchUpdateBlock_thatPathsAreEqual {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter performUpdatesAnimated:YES completion:nil];
IGListSectionController <IGListSectionType> * second = [self.adapter sectionControllerForObject:@1];
__block BOOL executed = NO;
[self.adapter performBatchAnimated:YES updates:^(id<IGListBatchContext> batchContext) {
NSArray *paths = [self.adapter indexPathsFromSectionController:second
indexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2, 2)]
usePreviousIfInUpdateBlock:YES];
NSArray *expected = @[
[NSIndexPath indexPathForItem:2 inSection:1],
[NSIndexPath indexPathForItem:3 inSection:1],
];
XCTAssertEqualObjects(paths, expected);
executed = YES;
} completion:nil];
XCTAssertTrue(executed);
}
- (void)test_whenQueryingReusableIdentifier_thatIdentifierEqualsClassName {
NSString *identifier = IGListReusableViewIdentifier(UICollectionViewCell.class, nil, nil);
XCTAssertEqualObjects(identifier, @"UICollectionViewCell");
}
- (void)test_whenQueryingReusableIdentifier_thatIdentifierEqualsClassNameAndSupplimentaryKind {
NSString *identifier = IGListReusableViewIdentifier(UICollectionViewCell.class, nil, UICollectionElementKindSectionFooter);
XCTAssertEqualObjects(identifier, @"UICollectionElementKindSectionFooterUICollectionViewCell");
}
- (void)test_whenQueryingReusableIdentifier_thatIdentifierEqualsClassNameAndNibName {
NSString *nibName = @"IGNibName";
NSString *identifier = IGListReusableViewIdentifier(UICollectionViewCell.class, nibName, nil);
XCTAssertEqualObjects(identifier, @"IGNibNameUICollectionViewCell");
}
- (void)test_whenDataSourceChanges_thatBackgroundViewVisibilityChanges {
self.dataSource.objects = @[@1];
UIView *background = [[UIView alloc] init];
self.dataSource.backgroundView = background;
__block BOOL executed = NO;
[self.adapter reloadDataWithCompletion:^(BOOL finished) {
XCTAssertTrue(self.adapter.collectionView.backgroundView.hidden, @"Background view should be hidden");
XCTAssertEqualObjects(background, self.adapter.collectionView.backgroundView, @"Background view not correctly assigned");
self.dataSource.objects = @[];
[self.adapter reloadDataWithCompletion:^(BOOL finished2) {
XCTAssertFalse(self.adapter.collectionView.backgroundView.hidden, @"Background view should be visible");
XCTAssertEqualObjects(background, self.adapter.collectionView.backgroundView, @"Background view not correctly assigned");
executed = YES;
}];
}];
XCTAssertTrue(executed);
}
- (void)test_whenReloadingData_thatNewSectionControllersAreCreated {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter reloadDataWithCompletion:nil];
IGListSectionController <IGListSectionType> *oldSectionController = [self.adapter sectionControllerForObject:@1];
[self.adapter reloadDataWithCompletion:nil];
IGListSectionController <IGListSectionType> *newSectionController = [self.adapter sectionControllerForObject:@1];
XCTAssertNotEqual(oldSectionController, newSectionController);
}
- (void)test_whenSettingCollectionView_thenSettingDataSource_thatViewControllerIsSet {
self.dataSource.objects = @[@0, @1, @2];
UIViewController *controller = [UIViewController new];
IGListAdapter *adapter = [[IGListAdapter alloc] initWithUpdater:[IGListReloadDataUpdater new]
viewController:controller
workingRangeSize:0];
adapter.collectionView = self.collectionView;
adapter.dataSource = self.dataSource;
IGListSectionController <IGListSectionType> *sectionController = [adapter sectionControllerForObject:@1];
XCTAssertEqual(controller, sectionController.viewController);
}
- (void)test_whenSettingCollectionView_thenSettingDataSource_thatCellExists {
self.dataSource.objects = @[@1];
IGListAdapter *adapter = [[IGListAdapter alloc] initWithUpdater:[IGListReloadDataUpdater new]
viewController:nil
workingRangeSize:0];
adapter.collectionView = self.collectionView;
adapter.dataSource = self.dataSource;
[self.collectionView layoutIfNeeded];
XCTAssertNotNil([self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]);
}
- (void)test_whenSettingDataSource_thenSettingCollectionView_thatCellExists {
self.dataSource.objects = @[@1];
IGListAdapter *adapter = [[IGListAdapter alloc] initWithUpdater:[IGListReloadDataUpdater new]
viewController:nil
workingRangeSize:0];
adapter.dataSource = self.dataSource;
adapter.collectionView = self.collectionView;
[self.collectionView layoutIfNeeded];
XCTAssertNotNil([self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]);
}
- (void)test_whenChangingCollectionViews_thatCellsExist {
self.dataSource.objects = @[@1];
IGListAdapterUpdater *updater = [[IGListAdapterUpdater alloc] init];
IGListAdapter *adapter = [[IGListAdapter alloc] initWithUpdater:updater viewController:nil workingRangeSize:0];
adapter.dataSource = self.dataSource;
adapter.collectionView = self.collectionView;
[self.collectionView layoutIfNeeded];
XCTAssertNotNil([self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]);
UICollectionView *otherCollectionView = [[UICollectionView alloc] initWithFrame:self.collectionView.frame collectionViewLayout:self.collectionView.collectionViewLayout];
adapter.collectionView = otherCollectionView;
[otherCollectionView layoutIfNeeded];
XCTAssertNotNil([otherCollectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]);
}
- (void)test_whenChangingCollectionViewsToACollectionViewInUseByAnotherAdapter_thatCollectionViewDelegateIsUpdated {
IGListTestAdapterDataSource *dataSource1 = [[IGListTestAdapterDataSource alloc] init];
dataSource1.objects = @[@1];
IGListAdapterUpdater *updater1 = [[IGListAdapterUpdater alloc] init];
IGListAdapter *adapter1 = [[IGListAdapter alloc] initWithUpdater:updater1 viewController:nil workingRangeSize:0];
adapter1.dataSource = dataSource1;
IGListTestAdapterDataSource *dataSource2 = [[IGListTestAdapterDataSource alloc] init];
dataSource1.objects = @[@1];
IGListAdapterUpdater *updater2 = [[IGListAdapterUpdater alloc] init];
IGListAdapter *adapter2 = [[IGListAdapter alloc] initWithUpdater:updater2 viewController:nil workingRangeSize:0];
adapter1.dataSource = dataSource2;
// associate collection view with adapter1
adapter1.collectionView = self.collectionView;
XCTAssertEqual(self.collectionView.dataSource, adapter1);
// associate collection view with adapter2
adapter2.collectionView = self.collectionView;
XCTAssertEqual(self.collectionView.dataSource, adapter2);
// associate collection view with adapter1
adapter1.collectionView = self.collectionView;
XCTAssertEqual(self.collectionView.dataSource, adapter1);
}
- (void)test_whenCellsExtendBeyondBounds_thatVisibleSectionControllersAreLimited {
// # of items for each object == [item integerValue], so @2 has 2 items (cells)
self.dataSource.objects = @[@1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12];
[self.adapter reloadDataWithCompletion:nil];
XCTAssertEqual([self.collectionView numberOfSections], 12);
NSArray *visibleSectionControllers = [self.adapter visibleSectionControllers];
// UIWindow is 100x100, each cell is 100x10 so should have the following section/cell count: 1 + 2 + 3 + 4 = 10 (100 tall)
XCTAssertEqual(visibleSectionControllers.count, 4);
XCTAssertTrue([visibleSectionControllers containsObject:[self.adapter sectionControllerForObject:@1]]);
XCTAssertTrue([visibleSectionControllers containsObject:[self.adapter sectionControllerForObject:@2]]);
XCTAssertTrue([visibleSectionControllers containsObject:[self.adapter sectionControllerForObject:@3]]);
XCTAssertTrue([visibleSectionControllers containsObject:[self.adapter sectionControllerForObject:@4]]);
}
- (void)test_whenCellsExtendBeyondBounds_thatVisibleCellsExistForSectionControllers {
self.dataSource.objects = @[@2, @3, @4, @5, @6];
[self.adapter reloadDataWithCompletion:nil];
id sectionController2 = [self.adapter sectionControllerForObject:@2];
id sectionController3 = [self.adapter sectionControllerForObject:@3];
id sectionController4 = [self.adapter sectionControllerForObject:@4];
id sectionController5 = [self.adapter sectionControllerForObject:@5];
id sectionController6 = [self.adapter sectionControllerForObject:@6];
XCTAssertEqual([self.adapter visibleCellsForSectionController:sectionController2].count, 2);
XCTAssertEqual([self.adapter visibleCellsForSectionController:sectionController3].count, 3);
XCTAssertEqual([self.adapter visibleCellsForSectionController:sectionController4].count, 4);
XCTAssertEqual([self.adapter visibleCellsForSectionController:sectionController5].count, 1);
XCTAssertEqual([self.adapter visibleCellsForSectionController:sectionController6].count, 0);
}
- (void)test_whenCellsExtendBeyondBounds_thatVisibleIndexPathsExistForSectionControllers {
self.dataSource.objects = @[@2, @3, @4, @5, @6];
[self.adapter reloadDataWithCompletion:nil];
id sectionController2 = [self.adapter sectionControllerForObject:@2];
id sectionController3 = [self.adapter sectionControllerForObject:@3];
id sectionController4 = [self.adapter sectionControllerForObject:@4];
id sectionController5 = [self.adapter sectionControllerForObject:@5];
id sectionController6 = [self.adapter sectionControllerForObject:@6];
XCTAssertEqual([self.adapter visibleIndexPathsForSectionController:sectionController2].count, 2);
XCTAssertEqual([self.adapter visibleIndexPathsForSectionController:sectionController3].count, 3);
XCTAssertEqual([self.adapter visibleIndexPathsForSectionController:sectionController4].count, 4);
XCTAssertEqual([self.adapter visibleIndexPathsForSectionController:sectionController5].count, 1);
XCTAssertEqual([self.adapter visibleIndexPathsForSectionController:sectionController6].count, 0);
}
- (void)test_whenDataSourceAddsItems_thatEmptyViewBecomesVisible {
self.dataSource.objects = @[];
UIView *background = [UIView new];
self.dataSource.backgroundView = background;
[self.adapter reloadDataWithCompletion:nil];
XCTAssertEqual(self.collectionView.backgroundView, background);
XCTAssertFalse(self.collectionView.backgroundView.hidden);
self.dataSource.objects = @[@2];
[self.adapter reloadDataWithCompletion:nil];
XCTAssertTrue(self.collectionView.backgroundView.hidden);
}
- (void)test_whenInsertingIntoEmptySection_thatEmptyViewBecomesHidden {
self.dataSource.objects = @[@0];
self.dataSource.backgroundView = [UIView new];
[self.adapter reloadDataWithCompletion:nil];
XCTAssertFalse(self.collectionView.backgroundView.hidden);
IGListTestSection *sectionController = [self.adapter sectionControllerForObject:@(0)];
sectionController.items = 1;
[self.adapter insertInSectionController:sectionController atIndexes:[NSIndexSet indexSetWithIndex:0]];
XCTAssertTrue(self.collectionView.backgroundView.hidden);
}
- (void)test_whenDeletingAllItemsFromSection_thatEmptyViewBecomesVisible {
self.dataSource.objects = @[@1];
self.dataSource.backgroundView = [UIView new];
[self.adapter reloadDataWithCompletion:nil];
XCTAssertTrue(self.collectionView.backgroundView.hidden);
IGListTestSection *sectionController = [self.adapter sectionControllerForObject:@(1)];
sectionController.items = 0;
[self.adapter deleteInSectionController:sectionController atIndexes:[NSIndexSet indexSetWithIndex:0]];
XCTAssertFalse(self.collectionView.backgroundView.hidden);
}
- (void)test_whenEmptySectionAddsItems_thatEmptyViewBecomesHidden {
self.dataSource.objects = @[@0];
self.dataSource.backgroundView = [UIView new];
[self.adapter reloadDataWithCompletion:nil];
XCTAssertFalse(self.collectionView.backgroundView.hidden);
IGListTestSection *sectionController = [self.adapter sectionControllerForObject:@(0)];
sectionController.items = 2;
[self.adapter reloadSectionController:sectionController];
XCTAssertTrue(self.collectionView.backgroundView.hidden);
}
- (void)test_whenSectionItemsAreDeletedAsBatch_thatEmptyViewBecomesVisible {
self.dataSource.objects = @[@1, @2];
self.dataSource.backgroundView = [UIView new];
[self.adapter reloadDataWithCompletion:nil];
XCTAssertTrue(self.collectionView.backgroundView.hidden);
IGListTestSection *firstSectionController = [self.adapter sectionControllerForObject:@(1)];
IGListTestSection *secondSectionController = [self.adapter sectionControllerForObject:@(2)];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.adapter performBatchAnimated:YES updates:^(id<IGListBatchContext> batchContext) {
firstSectionController.items = 0;
[self.adapter deleteInSectionController:firstSectionController atIndexes:[NSIndexSet indexSetWithIndex:0]];
secondSectionController.items = 0;
NSIndexSet *indexesToDelete = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 2)];
[self.adapter deleteInSectionController:secondSectionController atIndexes:indexesToDelete];
} completion:^(BOOL finished) {
XCTAssertFalse(self.collectionView.backgroundView.hidden);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:15 handler:nil];
}
- (void)test_whenScrollViewDelegateSet_thatDelegateReceivesEvents {
id mockDelegate = [OCMockObject mockForProtocol:@protocol(UIScrollViewDelegate)];
self.adapter.collectionViewDelegate = nil;
self.adapter.scrollViewDelegate = mockDelegate;
[[mockDelegate expect] scrollViewDidScroll:self.collectionView];
[self.adapter scrollViewDidScroll:self.collectionView];
[mockDelegate verify];
}
- (void)test_whenCollectionViewDelegateSet_thatDelegateReceivesEvents {
// silence display handler asserts
self.dataSource.objects = @[@1, @2];
[self.adapter reloadDataWithCompletion:nil];
id mockDelegate = [OCMockObject mockForProtocol:@protocol(UICollectionViewDelegate)];
self.adapter.collectionViewDelegate = mockDelegate;
self.adapter.scrollViewDelegate = nil;
NSIndexPath *path = [NSIndexPath indexPathForItem:0 inSection:0];
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:path];
[[mockDelegate expect] collectionView:self.collectionView didEndDisplayingCell:cell forItemAtIndexPath:path];
[self.adapter collectionView:self.collectionView didEndDisplayingCell:cell forItemAtIndexPath:path];
[mockDelegate verify];
}
- (void)test_whenCollectionViewDelegateSet_withScrollViewDelegateSet_thatDelegatesReceiveUniqueEvents {
// silence display handler asserts
self.dataSource.objects = @[@1, @2];
[self.adapter reloadDataWithCompletion:nil];
id mockCollectionViewDelegate = [OCMockObject mockForProtocol:@protocol(UICollectionViewDelegate)];
id mockScrollViewDelegate = [OCMockObject mockForProtocol:@protocol(UIScrollViewDelegate)];
self.adapter.collectionViewDelegate = mockCollectionViewDelegate;
self.adapter.scrollViewDelegate = mockScrollViewDelegate;
NSIndexPath *path = [NSIndexPath indexPathForItem:0 inSection:0];
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:path];
[[mockScrollViewDelegate expect] scrollViewDidScroll:self.collectionView];
[[mockCollectionViewDelegate reject] scrollViewDidScroll:self.collectionView];
[[mockCollectionViewDelegate expect] collectionView:self.collectionView didEndDisplayingCell:cell forItemAtIndexPath:path];
[self.adapter scrollViewDidScroll:self.collectionView];
[self.adapter collectionView:self.collectionView didEndDisplayingCell:cell forItemAtIndexPath:path];
[mockScrollViewDelegate verify];
[mockCollectionViewDelegate verify];
}
- (void)test_whenSupplementarySourceSupportsFooter_thatHeaderViewsAreNil {
self.dataSource.objects = @[@1, @2];
[self.adapter reloadDataWithCompletion:nil];
IGTestSupplementarySource *supplementarySource = [IGTestSupplementarySource new];
supplementarySource.collectionContext = self.adapter;
supplementarySource.supportedElementKinds = @[UICollectionElementKindSectionFooter];
IGListSectionController<IGListSectionType> *controller = [self.adapter sectionControllerForObject:@1];
controller.supplementaryViewSource = supplementarySource;
supplementarySource.sectionController = controller;
[self.adapter performUpdatesAnimated:NO completion:nil];
XCTAssertNotNil([self.collectionView supplementaryViewForElementKind:UICollectionElementKindSectionFooter atIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]);
XCTAssertNil([self.collectionView supplementaryViewForElementKind:UICollectionElementKindSectionHeader atIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]);
XCTAssertNil([self.collectionView supplementaryViewForElementKind:UICollectionElementKindSectionHeader atIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]]);
XCTAssertNil([self.collectionView supplementaryViewForElementKind:UICollectionElementKindSectionFooter atIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]]);
}
- (void)test_whenSupplementarySourceSupportsFooter_withNibs_thatHeaderViewsAreNil {
self.dataSource.objects = @[@1, @2];
[self.adapter reloadDataWithCompletion:nil];
IGTestSupplementarySource *supplementarySource = [IGTestSupplementarySource new];
supplementarySource.dequeueFromNib = YES;
supplementarySource.collectionContext = self.adapter;
supplementarySource.supportedElementKinds = @[UICollectionElementKindSectionFooter];
IGListSectionController<IGListSectionType> *controller = [self.adapter sectionControllerForObject:@1];
controller.supplementaryViewSource = supplementarySource;
supplementarySource.sectionController = controller;
[self.adapter performUpdatesAnimated:NO completion:nil];
id view = [self.collectionView supplementaryViewForElementKind:UICollectionElementKindSectionFooter atIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
XCTAssertTrue([view isKindOfClass:IGTestNibSupplementaryView.class]);
XCTAssertEqualObjects([[(IGTestNibSupplementaryView *)view label] text], @"Foo bar baz");
XCTAssertNil([self.collectionView supplementaryViewForElementKind:UICollectionElementKindSectionHeader atIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]);
XCTAssertNil([self.collectionView supplementaryViewForElementKind:UICollectionElementKindSectionHeader atIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]]);
XCTAssertNil([self.collectionView supplementaryViewForElementKind:UICollectionElementKindSectionFooter atIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]]);
}
- (void)test_whenAdapterReleased_withSectionControllerStrongRefToCell_thatSectionControllersRelease {
__weak id weakCollectionView = nil, weakAdapter = nil, weakSectionController = nil;
@autoreleasepool {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)
collectionViewLayout:layout];
weakCollectionView = collectionView;
IGListTestAdapterDataSource *dataSource = [[IGListTestAdapterDataSource alloc] init];
dataSource.objects = @[@0, @1, @2];
IGListReloadDataUpdater *updater = [[IGListReloadDataUpdater alloc] init];
IGListAdapter *adapter = [[IGListAdapter alloc] initWithUpdater:updater viewController:nil workingRangeSize:0];
adapter.collectionView = collectionView;
adapter.dataSource = dataSource;
weakAdapter = adapter;
IGListSectionController *sectionController = [adapter sectionControllerForObject:@1];
weakSectionController = sectionController;
// force the collection view to layout and generate cells
[collectionView layoutIfNeeded];
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]];
XCTAssertNotNil(cell);
// strongly attach the cell to an section controller
objc_setAssociatedObject(sectionController, @"some_random_key", cell, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
// weak refs should exist at this point
XCTAssertNotNil(weakCollectionView);
XCTAssertNotNil(weakAdapter);
XCTAssertNotNil(weakSectionController);
}
XCTAssertNil(weakCollectionView);
XCTAssertNil(weakAdapter);
XCTAssertNil(weakSectionController);
}
- (void)test_whenAdapterReleased_withSectionControllerStrongRefToCollectionView_thatSectionControllersRelease {
__weak id weakCollectionView = nil, weakAdapter = nil, weakSectionController = nil;
@autoreleasepool {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)
collectionViewLayout:layout];
weakCollectionView = collectionView;
IGListTestAdapterDataSource *dataSource = [[IGListTestAdapterDataSource alloc] init];
dataSource.objects = @[@0, @1, @2];
IGListReloadDataUpdater *updater = [[IGListReloadDataUpdater alloc] init];
IGListAdapter *adapter = [[IGListAdapter alloc] initWithUpdater:updater viewController:nil workingRangeSize:0];
adapter.collectionView = collectionView;
adapter.dataSource = dataSource;
weakAdapter = adapter;
IGListSectionController *sectionController = [adapter sectionControllerForObject:@1];
weakSectionController = sectionController;
// force the collection view to layout and generate cells
[collectionView layoutIfNeeded];
// strongly attach the cell to an section controller
objc_setAssociatedObject(sectionController, @"some_random_key", collectionView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
// weak refs should exist at this point
XCTAssertNotNil(weakCollectionView);
XCTAssertNotNil(weakAdapter);
XCTAssertNotNil(weakSectionController);
}
XCTAssertNil(weakCollectionView);
XCTAssertNil(weakAdapter);
XCTAssertNil(weakSectionController);
}
- (void)test_whenAdapterUpdatedTwice_withThreeSections_thatSectionsUpdatedFirstLast {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter reloadDataWithCompletion:nil];
XCTAssertTrue([[self.adapter sectionControllerForObject:@0] isFirstSection]);
XCTAssertFalse([[self.adapter sectionControllerForObject:@1] isFirstSection]);
XCTAssertFalse([[self.adapter sectionControllerForObject:@2] isFirstSection]);
XCTAssertFalse([[self.adapter sectionControllerForObject:@0] isLastSection]);
XCTAssertFalse([[self.adapter sectionControllerForObject:@1] isLastSection]);
XCTAssertTrue([[self.adapter sectionControllerForObject:@2] isLastSection]);
// update and shift objects to test that first/last flags are also updated
self.dataSource.objects = @[@2, @0, @1];
[self.adapter performUpdatesAnimated:NO completion:nil];
XCTAssertFalse([[self.adapter sectionControllerForObject:@0] isFirstSection]);
XCTAssertFalse([[self.adapter sectionControllerForObject:@1] isFirstSection]);
XCTAssertTrue([[self.adapter sectionControllerForObject:@2] isFirstSection]);
XCTAssertFalse([[self.adapter sectionControllerForObject:@0] isLastSection]);
XCTAssertTrue([[self.adapter sectionControllerForObject:@1] isLastSection]);
XCTAssertFalse([[self.adapter sectionControllerForObject:@2] isLastSection]);
}
- (void)test_whenAdapterUpdated_withObjectsOverflow_thatVisibleObjectsIsSubsetOfAllObjects {
// each section controller returns n items sized 100x10
self.dataSource.objects = @[@1, @2, @3, @4, @5, @6];
[self.adapter reloadDataWithCompletion:nil];
self.collectionView.contentOffset = CGPointMake(0, 30);
[self.collectionView layoutIfNeeded];
NSArray *visibleObjects = [[self.adapter visibleObjects] sortedArrayUsingSelector:@selector(compare:)];
NSArray *expectedObjects = @[@3, @4, @5];
XCTAssertEqualObjects(visibleObjects, expectedObjects);
}
- (void)test_whenAdapterUpdated_thatVisibleCellsForObjectAreFound {
// each section controller returns n items sized 100x10
self.dataSource.objects = @[@2, @10, @5];
[self.adapter reloadDataWithCompletion:nil];
self.collectionView.contentOffset = CGPointMake(0, 80);
[self.collectionView layoutIfNeeded];
UICollectionView *collectionView = self.collectionView;
NSArray *visibleCellsForObject = [[self.adapter visibleCellsForObject:@10] sortedArrayUsingComparator:^NSComparisonResult(UICollectionViewCell* lhs, UICollectionViewCell* rhs) {
NSIndexPath *lhsIndexPath = [collectionView indexPathForCell:lhs];
NSIndexPath *rhsIndexPath = [collectionView indexPathForCell:rhs];
if (lhsIndexPath.section == rhsIndexPath.section) {
return lhsIndexPath.item > rhsIndexPath.item;
}
return lhsIndexPath.section > rhsIndexPath.section;
}];
XCTAssertEqual(visibleCellsForObject.count, 4);
XCTAssertEqual([self.collectionView indexPathForCell:visibleCellsForObject[0]].item, 6);
XCTAssertEqual([self.collectionView indexPathForCell:visibleCellsForObject[1]].item, 7);
XCTAssertEqual([self.collectionView indexPathForCell:visibleCellsForObject[2]].item, 8);
XCTAssertEqual([self.collectionView indexPathForCell:visibleCellsForObject[3]].item, 9);
NSArray *visibleCellsForObjectTwo = [self.adapter visibleCellsForObject:@5];
XCTAssertEqual(visibleCellsForObjectTwo.count, 5);
}
- (void)test_whenAdapterUpdated_thatVisibleCellsForNilObjectIsEmpty {
// each section controller returns n items sized 100x10
self.dataSource.objects = @[@2, @10, @5];
[self.adapter reloadDataWithCompletion:nil];
self.collectionView.contentOffset = CGPointMake(0, 80);
[self.collectionView layoutIfNeeded];
NSArray *visibleCellsForObject = [self.adapter visibleCellsForObject:@3];
XCTAssertEqual(visibleCellsForObject.count, 0);
}
- (void)test_whenScrollVerticallyToItem {
// # of items for each object == [item integerValue], so @2 has 2 items (cells)
self.dataSource.objects = @[@1, @2, @3, @4, @5, @6];
[self.adapter reloadDataWithCompletion:nil];
XCTAssertEqual([self.collectionView numberOfSections], 6);
[self.adapter scrollToObject:@1 supplementaryKinds:nil scrollDirection:UICollectionViewScrollDirectionVertical scrollPosition:UICollectionViewScrollPositionNone animated:NO];
IGAssertEqualPoint([self.collectionView contentOffset], 0, 0);
[self.adapter scrollToObject:@2 supplementaryKinds:nil scrollDirection:UICollectionViewScrollDirectionVertical scrollPosition:UICollectionViewScrollPositionNone animated:NO];
IGAssertEqualPoint([self.collectionView contentOffset], 0, 10);
[self.adapter scrollToObject:@3 supplementaryKinds:nil scrollDirection:UICollectionViewScrollDirectionVertical scrollPosition:UICollectionViewScrollPositionNone animated:NO];
IGAssertEqualPoint([self.collectionView contentOffset], 0, 30);
[self.adapter scrollToObject:@6 supplementaryKinds:nil scrollDirection:UICollectionViewScrollDirectionVertical scrollPosition:UICollectionViewScrollPositionNone animated:NO];
IGAssertEqualPoint([self.collectionView contentOffset], 0, 150);
[self.adapter scrollToObject:@6 supplementaryKinds:nil scrollDirection:UICollectionViewScrollDirectionVertical scrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];
IGAssertEqualPoint([self.collectionView contentOffset], 0, 105);
[self.adapter scrollToObject:@6 supplementaryKinds:nil scrollDirection:UICollectionViewScrollDirectionVertical scrollPosition:UICollectionViewScrollPositionBottom animated:NO];
IGAssertEqualPoint([self.collectionView contentOffset], 0, 60);
}
- (void)test_whenScrollHorizontallyToItem {
// # of items for each object == [item integerValue], so @2 has 2 items (cells)
IGListTestAdapterHorizontalDataSource *dataSource = [[IGListTestAdapterHorizontalDataSource alloc] init];
self.adapter.dataSource = dataSource;
dataSource.objects = @[@1, @2, @3, @4, @5, @6];
self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
[self.adapter reloadDataWithCompletion:nil];
XCTAssertEqual([self.collectionView numberOfSections], 6);
[self.adapter scrollToObject:@1 supplementaryKinds:nil scrollDirection:UICollectionViewScrollDirectionHorizontal scrollPosition:UICollectionViewScrollPositionNone animated:NO];
IGAssertEqualPoint([self.collectionView contentOffset], 0, 0);
[self.adapter scrollToObject:@2 supplementaryKinds:nil scrollDirection:UICollectionViewScrollDirectionHorizontal scrollPosition:UICollectionViewScrollPositionNone animated:NO];
IGAssertEqualPoint([self.collectionView contentOffset], 10, 0);
[self.adapter scrollToObject:@3 supplementaryKinds:nil scrollDirection:UICollectionViewScrollDirectionHorizontal scrollPosition:UICollectionViewScrollPositionNone animated:NO];
IGAssertEqualPoint([self.collectionView contentOffset], 30, 0);
[self.adapter scrollToObject:@6 supplementaryKinds:nil scrollDirection:UICollectionViewScrollDirectionHorizontal scrollPosition:UICollectionViewScrollPositionNone animated:NO];
IGAssertEqualPoint([self.collectionView contentOffset], 150, 0);
[self.adapter scrollToObject:@6 supplementaryKinds:nil scrollDirection:UICollectionViewScrollDirectionHorizontal scrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
IGAssertEqualPoint([self.collectionView contentOffset], 105, 0);
[self.adapter scrollToObject:@6 supplementaryKinds:nil scrollDirection:UICollectionViewScrollDirectionHorizontal scrollPosition:UICollectionViewScrollPositionRight animated:NO];
IGAssertEqualPoint([self.collectionView contentOffset], 60, 0);
self.layout.scrollDirection = UICollectionViewScrollDirectionVertical;
self.adapter.dataSource = self.dataSource;
}
- (void)test_whenScrollToItem_thatSupplementarySourceSupportsSingleHeader {
self.dataSource.objects = @[@1, @2];
[self.adapter reloadDataWithCompletion:nil];
IGTestSupplementarySource *supplementarySource = [IGTestSupplementarySource new];
supplementarySource.collectionContext = self.adapter;
supplementarySource.supportedElementKinds = @[UICollectionElementKindSectionHeader];
IGListSectionController<IGListSectionType> *controller = [self.adapter sectionControllerForObject:@1];
controller.supplementaryViewSource = supplementarySource;
supplementarySource.sectionController = controller;
[self.adapter performUpdatesAnimated:NO completion:nil];
XCTAssertNotNil([self.collectionView supplementaryViewForElementKind:UICollectionElementKindSectionHeader atIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]);
[self.adapter scrollToObject:@1 supplementaryKinds:@[UICollectionElementKindSectionHeader] scrollDirection:UICollectionViewScrollDirectionVertical scrollPosition:UICollectionViewScrollPositionNone animated:NO];
IGAssertEqualPoint([self.collectionView contentOffset], 0, 0);
[self.adapter scrollToObject:@2 supplementaryKinds:@[UICollectionElementKindSectionHeader] scrollDirection:UICollectionViewScrollDirectionVertical scrollPosition:UICollectionViewScrollPositionNone animated:NO];
IGAssertEqualPoint([self.collectionView contentOffset], 0, 20);
}
- (void)test_whenScrollToItem_thatSupplementarySourceSupportsHeaderAndFooter {
self.dataSource.objects = @[@1, @2];
[self.adapter reloadDataWithCompletion:nil];
IGTestSupplementarySource *supplementarySource = [IGTestSupplementarySource new];
supplementarySource.collectionContext = self.adapter;
supplementarySource.supportedElementKinds = @[UICollectionElementKindSectionHeader, UICollectionElementKindSectionFooter];
IGListSectionController<IGListSectionType> *controller = [self.adapter sectionControllerForObject:@1];
controller.supplementaryViewSource = supplementarySource;
supplementarySource.sectionController = controller;
[self.adapter performUpdatesAnimated:NO completion:nil];
XCTAssertNotNil([self.collectionView supplementaryViewForElementKind:UICollectionElementKindSectionHeader atIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]);
XCTAssertNotNil([self.collectionView supplementaryViewForElementKind:UICollectionElementKindSectionFooter atIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]);
[self.adapter scrollToObject:@1 supplementaryKinds:@[UICollectionElementKindSectionHeader, UICollectionElementKindSectionFooter] scrollDirection:UICollectionViewScrollDirectionVertical scrollPosition:UICollectionViewScrollPositionNone animated:NO];
IGAssertEqualPoint([self.collectionView contentOffset], 0, 0);
[self.adapter scrollToObject:@2 supplementaryKinds:@[UICollectionElementKindSectionHeader, UICollectionElementKindSectionFooter] scrollDirection:UICollectionViewScrollDirectionVertical scrollPosition:UICollectionViewScrollPositionNone animated:NO];
IGAssertEqualPoint([self.collectionView contentOffset], 0, 30);
}
- (void)test_whenScrollVerticallyToItem_thatFeedIsEmpty {
self.dataSource.objects = @[];
[self.adapter reloadDataWithCompletion:nil];
XCTAssertEqual([self.collectionView numberOfSections], 0);
[self.adapter scrollToObject:@1 supplementaryKinds:nil scrollDirection:UICollectionViewScrollDirectionVertical scrollPosition:UICollectionViewScrollPositionNone animated:NO];
IGAssertEqualPoint([self.collectionView contentOffset], 0, 0);
}
- (void)test_whenScrollVerticallyToItem_thatItemNotInFeed {
// # of items for each object == [item integerValue], so @2 has 2 items (cells)
self.dataSource.objects = @[@1, @2, @3, @4];
[self.adapter reloadDataWithCompletion:nil];
XCTAssertEqual([self.collectionView numberOfSections], 4);
[self.adapter scrollToObject:@1 supplementaryKinds:nil scrollDirection:UICollectionViewScrollDirectionVertical scrollPosition:UICollectionViewScrollPositionNone animated:NO];
IGAssertEqualPoint([self.collectionView contentOffset], 0, 0);
[self.adapter scrollToObject:@5 supplementaryKinds:nil scrollDirection:UICollectionViewScrollDirectionVertical scrollPosition:UICollectionViewScrollPositionNone animated:NO];
IGAssertEqualPoint([self.collectionView contentOffset], 0, 0);
[self.adapter scrollToObject:@2 supplementaryKinds:nil scrollDirection:UICollectionViewScrollDirectionVertical scrollPosition:UICollectionViewScrollPositionNone animated:NO];
IGAssertEqualPoint([self.collectionView contentOffset], 0, 10);
[self.adapter scrollToObject:@5 supplementaryKinds:nil scrollDirection:UICollectionViewScrollDirectionVertical scrollPosition:UICollectionViewScrollPositionNone animated:NO];
IGAssertEqualPoint([self.collectionView contentOffset], 0, 10);
}
- (void)test_whenQueryingIndexPath_withOOBSectionController_thatNilReturned {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter reloadDataWithCompletion:nil];
id randomSectionController = [IGListSectionController new];
XCTAssertNil([self.adapter indexPathForSectionController:randomSectionController index:0 usePreviousIfInUpdateBlock:NO]);
}
- (void)test_whenQueryingSectionForObject_thatSectionReturned {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter reloadDataWithCompletion:nil];
XCTAssertEqual([self.adapter sectionForObject:@0], 0);
XCTAssertEqual([self.adapter sectionForObject:@1], 1);
XCTAssertEqual([self.adapter sectionForObject:@2], 2);
XCTAssertEqual([self.adapter sectionForObject:@3], NSNotFound);
}
- (void)test_whenQueryingSectionControllerForSection_thatControllerReturned {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter reloadDataWithCompletion:nil];
XCTAssertEqual([self.adapter sectionControllerForSection:0], [self.adapter sectionControllerForObject:@0]);
XCTAssertEqual([self.adapter sectionControllerForSection:1], [self.adapter sectionControllerForObject:@1]);
XCTAssertEqual([self.adapter sectionControllerForSection:2], [self.adapter sectionControllerForObject:@2]);
}
- (void)test_whenReloadingData_withNoDataSource_thatCompletionCalledWithNO {
self.dataSource.objects = @[@1];
IGListAdapter *adapter = [[IGListAdapter alloc] initWithUpdater:[IGListReloadDataUpdater new]
viewController:nil
workingRangeSize:0];
adapter.collectionView = self.collectionView;
__block BOOL executed = NO;
[adapter reloadDataWithCompletion:^(BOOL finished) {
executed = YES;
XCTAssertFalse(finished);
}];
XCTAssertTrue(executed);
}
- (void)test_whenReloadingData_withNoCollectionView_thatCompletionCalledWithNO {
self.dataSource.objects = @[@1];
IGListAdapter *adapter = [[IGListAdapter alloc] initWithUpdater:[IGListReloadDataUpdater new]
viewController:nil
workingRangeSize:0];
adapter.dataSource = self.dataSource;
__block BOOL executed = NO;
[adapter reloadDataWithCompletion:^(BOOL finished) {
executed = YES;
XCTAssertFalse(finished);
}];
XCTAssertTrue(executed);
}
- (void)test_whenSectionControllerDeleting_withEmptyIndexes_thatNoUpdatesHappen {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter reloadDataWithCompletion:nil];
id mockDelegate = [OCMockObject mockForProtocol:@protocol(IGListUpdatingDelegate)];
[[mockDelegate reject] deleteItemsFromCollectionView:[OCMArg any] indexPaths:[OCMArg any]];
self.adapter.updater = mockDelegate;
id sectionController = [self.adapter sectionControllerForObject:@1];
[self.adapter deleteInSectionController:sectionController atIndexes:[NSIndexSet new]];
[mockDelegate verify];
}
- (void)test_whenSectionControllerInserting_withEmptyIndexes_thatNoUpdatesHappen {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter reloadDataWithCompletion:nil];
id mockDelegate = [OCMockObject mockForProtocol:@protocol(IGListUpdatingDelegate)];
[[mockDelegate reject] insertItemsIntoCollectionView:[OCMArg any] indexPaths:[OCMArg any]];
self.adapter.updater = mockDelegate;
id sectionController = [self.adapter sectionControllerForObject:@1];
[self.adapter insertInSectionController:sectionController atIndexes:[NSIndexSet new]];
[mockDelegate verify];
}
- (void)test_whenReloading_withSectionControllerNotFound_thatNoUpdatesHappen {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter reloadDataWithCompletion:nil];
id mockDelegate = [OCMockObject mockForProtocol:@protocol(IGListUpdatingDelegate)];
[[mockDelegate reject] reloadCollectionView:[OCMArg any] sections:[OCMArg any]];
self.adapter.updater = mockDelegate;
id sectionController = [IGListSectionController new];
[self.adapter reloadSectionController:sectionController];
[mockDelegate verify];
}
- (void)test_whenSelectingCell_thatCollectionViewDelegateReceivesMethod {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter reloadDataWithCompletion:nil];
id mockDelegate = [OCMockObject mockForProtocol:@protocol(UICollectionViewDelegate)];
self.adapter.collectionViewDelegate = mockDelegate;
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
[[mockDelegate expect] collectionView:self.collectionView didSelectItemAtIndexPath:indexPath];
// simulates the collectionview telling its delegate that it was tapped
[self.adapter collectionView:self.collectionView didSelectItemAtIndexPath:indexPath];
[mockDelegate verify];
}
- (void)test_whenSelectingCell_thatSectionControllerReceivesMethod {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter reloadDataWithCompletion:nil];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
// simulates the collectionview telling its delegate that it was tapped
[self.adapter collectionView:self.collectionView didSelectItemAtIndexPath:indexPath];
IGListTestSection *s0 = [self.adapter sectionControllerForObject:@0];
IGListTestSection *s1 = [self.adapter sectionControllerForObject:@1];
IGListTestSection *s2 = [self.adapter sectionControllerForObject:@2];
XCTAssertTrue(s0.wasSelected);
XCTAssertFalse(s1.wasSelected);
XCTAssertFalse(s2.wasSelected);
}
- (void)test_whenDisplayingCell_thatCollectionViewDelegateReceivesMethod {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter reloadDataWithCompletion:nil];
id mockDelegate = [OCMockObject mockForProtocol:@protocol(UICollectionViewDelegate)];
self.adapter.collectionViewDelegate = mockDelegate;
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
UICollectionViewCell *cell = [UICollectionViewCell new];
[[mockDelegate expect] collectionView:self.collectionView willDisplayCell:cell forItemAtIndexPath:indexPath];
// simulates the collectionview telling its delegate that a cell will be displayed
[self.adapter collectionView:self.collectionView willDisplayCell:cell forItemAtIndexPath:indexPath];
[mockDelegate verify];
}
- (void)test_whenWillBeginDragging_thatScrollViewDelegateReceivesMethod {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter reloadDataWithCompletion:nil];
id mockCollectionDelegate = [OCMockObject mockForProtocol:@protocol(UICollectionViewDelegate)];
id mockScrollDelegate = [OCMockObject mockForProtocol:@protocol(UIScrollViewDelegate)];
self.adapter.collectionViewDelegate = mockCollectionDelegate;
self.adapter.scrollViewDelegate = mockScrollDelegate;
[[mockCollectionDelegate reject] scrollViewWillBeginDragging:self.collectionView];
[[mockScrollDelegate expect] scrollViewWillBeginDragging:self.collectionView];
// simulates the scrollview delegate telling the adapter that it will begin dragging
[self.adapter scrollViewWillBeginDragging:self.collectionView];
[mockCollectionDelegate verify];
[mockScrollDelegate verify];
}
- (void)test_whenDidEndDragging_thatScrollViewDelegateReceivesMethod {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter reloadDataWithCompletion:nil];
id mockCollectionDelegate = [OCMockObject mockForProtocol:@protocol(UICollectionViewDelegate)];
id mockScrollDelegate = [OCMockObject mockForProtocol:@protocol(UIScrollViewDelegate)];
self.adapter.collectionViewDelegate = mockCollectionDelegate;
self.adapter.scrollViewDelegate = mockScrollDelegate;
[[mockCollectionDelegate reject] scrollViewDidEndDragging:self.collectionView willDecelerate:NO];
[[mockScrollDelegate expect] scrollViewDidEndDragging:self.collectionView willDecelerate:NO];
// simulates the scrollview delegate telling the adapter that it will end dragging
[self.adapter scrollViewDidEndDragging:self.collectionView willDecelerate:NO];
[mockCollectionDelegate verify];
[mockScrollDelegate verify];
}
- (void)test_whenReloadingObjectsThatDontExist_thatAdapterContinues {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter reloadDataWithCompletion:nil];
[self.adapter reloadObjects:@[@1, @3]];
XCTAssertEqual(self.collectionView.numberOfSections, 3);
}
- (void)test_whenDeselectingThroughContext_thatCellDeselected {
self.dataSource.objects = @[@1, @2, @3];
[self.adapter reloadDataWithCompletion:nil];
NSIndexPath *path = [NSIndexPath indexPathForItem:0 inSection:0];
[self.collectionView selectItemAtIndexPath:path animated:NO scrollPosition:UICollectionViewScrollPositionTop];
XCTAssertTrue([[self.collectionView cellForItemAtIndexPath:path] isSelected]);
id section = [self.adapter sectionControllerForObject:@1];
[self.adapter deselectItemAtIndex:0 sectionController:section animated:NO];
XCTAssertFalse([[self.collectionView cellForItemAtIndexPath:path] isSelected]);
}
- (void)test_whenScrollingToIndex_withSectionController_thatPositionCorrect {
self.dataSource.objects = @[@1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13, @14, @15, @16, @17, @18, @19];
[self.adapter reloadDataWithCompletion:nil];
IGListSectionController<IGListSectionType> *section = [self.adapter sectionControllerForObject:@8];
[section.collectionContext scrollToSectionController:section atIndex:0 scrollPosition:UICollectionViewScrollPositionTop animated:NO];
XCTAssertEqual(self.collectionView.contentOffset.x, 0);
XCTAssertEqual(self.collectionView.contentOffset.y, 280);
}
- (void)test_whenDisplayingSectionController_withOnlySupplementaryView_thatDisplayEventStillSent {
self.dataSource.objects = @[@0];
[self.adapter reloadDataWithCompletion:nil];
XCTAssertNil([self.collectionView supplementaryViewForElementKind:UICollectionElementKindSectionHeader atIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]);
IGTestSupplementarySource *supplementarySource = [IGTestSupplementarySource new];
supplementarySource.collectionContext = self.adapter;
supplementarySource.supportedElementKinds = @[UICollectionElementKindSectionHeader];
IGListSectionController<IGListSectionType> *controller = [self.adapter sectionControllerForObject:@0];
controller.supplementaryViewSource = supplementarySource;
supplementarySource.sectionController = controller;
id mockDisplayDelegate = [OCMockObject mockForProtocol:@protocol(IGListDisplayDelegate)];
[[mockDisplayDelegate expect] listAdapter:self.adapter willDisplaySectionController:controller];