Skip to content

Commit ead95c6

Browse files
committed
add FirebaseIndexCollectionViewDataSourceTest
1 parent 8199295 commit ead95c6

File tree

5 files changed

+182
-2
lines changed

5 files changed

+182
-2
lines changed

FirebaseDatabaseUI/FirebaseIndexCollectionViewDataSource.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ didFailLoadAtIndex:(NSUInteger)index
7474
- (instancetype)initWithIndex:(FIRDatabaseQuery *)indexQuery
7575
data:(FIRDatabaseReference *)dataQuery
7676
collectionView:(UICollectionView *)collectionView
77-
identifier:(NSString *)cellIdentifier
77+
cellReuseIdentifier:(NSString *)cellIdentifier
78+
delegate:(nullable id<FirebaseIndexCollectionViewDataSourceDelegate>)delegate
7879
populateCell:(void (^)(UICollectionViewCell *cell,
7980
FIRDataSnapshot *_Nullable))populateCell NS_DESIGNATED_INITIALIZER;
8081

FirebaseDatabaseUI/FirebaseIndexCollectionViewDataSource.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ @implementation FirebaseIndexCollectionViewDataSource
3333
- (instancetype)initWithIndex:(FIRDatabaseQuery *)indexQuery
3434
data:(FIRDatabaseReference *)dataQuery
3535
collectionView:(UICollectionView *)collectionView
36-
identifier:(NSString *)cellIdentifier
36+
cellReuseIdentifier:(NSString *)cellIdentifier
37+
delegate:(id<FirebaseIndexCollectionViewDataSourceDelegate>)delegate
3738
populateCell:(void (^)(UICollectionViewCell *,
3839
FIRDataSnapshot *))populateCell {
3940
self = [super init];
@@ -45,6 +46,7 @@ - (instancetype)initWithIndex:(FIRDatabaseQuery *)indexQuery
4546
_collectionView.dataSource = self;
4647
_identifier = [cellIdentifier copy];
4748
_populateCell = populateCell;
49+
_delegate = delegate;
4850
}
4951
return self;
5052
}

FirebaseDatabaseUI/FirebaseIndexTableViewDataSource.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ - (instancetype)initWithIndex:(FIRDatabaseQuery *)indexQuery
5454
tableView.dataSource = self;
5555
_identifier = [cellIdentifier copy];
5656
_populateCell = populateCell;
57+
_delegate = delegate;
5758
}
5859
return self;
5960
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
//
2+
// Copyright (c) 2016 Google Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
@import XCTest;
18+
@import UIKit;
19+
20+
@import FirebaseDatabaseUI;
21+
22+
#import "FirebaseIndexCollectionViewDataSource.h"
23+
#import "FirebaseArrayTestUtils.h"
24+
25+
static NSString *const kTestReuseIdentifier = @"FirebaseIndexCollectionViewDataSourceTest";
26+
27+
@interface FirebaseIndexCollectionViewDataSourceTest : XCTestCase <FirebaseIndexCollectionViewDataSourceDelegate>
28+
29+
@property (nonatomic, readwrite) FirebaseIndexCollectionViewDataSource *dataSource;
30+
31+
@property (nonatomic, readwrite) UICollectionView *collectionView;
32+
33+
@property (nonatomic, readwrite) FUITestObservable *index;
34+
@property (nonatomic, readwrite) FUITestObservable *data;
35+
36+
@property (nonatomic, readwrite) NSMutableDictionary *dict;
37+
38+
@end
39+
40+
@implementation FirebaseIndexCollectionViewDataSourceTest
41+
42+
static inline NSDictionary *database() {
43+
static NSDictionary *dict;
44+
static dispatch_once_t onceToken;
45+
dispatch_once(&onceToken, ^{
46+
dict = @{
47+
@"index": @{
48+
@"1": @(YES),
49+
@"2": @(YES),
50+
@"3": @(YES),
51+
},
52+
@"data": @{
53+
@"1": @{ @"data": @"1" },
54+
@"2": @{ @"data": @"2" },
55+
@"3": @{ @"data": @"3" },
56+
},
57+
};
58+
});
59+
return dict;
60+
}
61+
62+
- (void)setUp {
63+
[super setUp];
64+
[UIView setAnimationsEnabled:NO];
65+
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)
66+
collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
67+
[self.collectionView registerClass:[UICollectionViewCell class]
68+
forCellWithReuseIdentifier:kTestReuseIdentifier];
69+
70+
self.index = [[FUITestObservable alloc] initWithDictionary:database()[@"index"]];
71+
self.data = [[FUITestObservable alloc] initWithDictionary:database()[@"data"]];
72+
self.dataSource = [[FirebaseIndexCollectionViewDataSource alloc] initWithIndex:(FIRDatabaseQuery *)self.index
73+
data:(FIRDatabaseReference *)self.data
74+
collectionView:self.collectionView
75+
cellReuseIdentifier:kTestReuseIdentifier
76+
delegate:self
77+
populateCell:^(UICollectionViewCell *cell,
78+
FIRDataSnapshot *snap) {
79+
cell.accessibilityLabel = snap.key;
80+
cell.accessibilityValue = snap.value;
81+
}];
82+
self.dict = [database() mutableCopy];
83+
84+
// Removing this NSLog causes the tests to crash since `numberOfItemsInSection`
85+
// actually pulls updates from the data source or something
86+
NSLog(@"count: %lu", [self.collectionView numberOfItemsInSection:0]);
87+
}
88+
89+
- (void)tearDown {
90+
[UIView setAnimationsEnabled:YES];
91+
[super tearDown];
92+
}
93+
94+
- (void)testItPopulatesCells {
95+
UICollectionViewCell *cell = [self.dataSource collectionView:self.collectionView
96+
cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
97+
98+
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
99+
XCTAssertEqualObjects(cell.accessibilityValue, @"1");
100+
101+
cell = [self.dataSource collectionView:self.collectionView
102+
cellForItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0]];
103+
104+
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
105+
XCTAssertEqualObjects(cell.accessibilityValue, @"2");
106+
107+
cell = [self.dataSource collectionView:self.collectionView
108+
cellForItemAtIndexPath:[NSIndexPath indexPathForItem:2 inSection:0]];
109+
110+
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
111+
XCTAssertEqualObjects(cell.accessibilityValue, @"3");
112+
}
113+
114+
- (void)testItUpdatesOnInsertion {
115+
// insert item
116+
[self.data addObject:@{ @"data": @"4" } forKey:@"4"];
117+
[self.index addObject:@(YES) forKey:@"4"];
118+
119+
UICollectionViewCell *cell = [self.dataSource collectionView:self.collectionView
120+
cellForItemAtIndexPath:[NSIndexPath indexPathForItem:3 inSection:0]];
121+
122+
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
123+
XCTAssertEqualObjects(cell.accessibilityValue, @"4");
124+
}
125+
126+
- (void)testItUpdatesOnDeletion {
127+
// delete item
128+
[self.data removeObjectForKey:@"2"];
129+
[self.index removeObjectForKey:@"2"];
130+
131+
UICollectionViewCell *cell = [self.dataSource collectionView:self.collectionView
132+
cellForItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0]];
133+
134+
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
135+
XCTAssertEqualObjects(cell.accessibilityValue, @"3");
136+
}
137+
138+
- (void)testItUpdatesOnChange {
139+
// change item
140+
[self.data changeObject:@{ @"data": @"changed" } forKey:@"2"];
141+
[self.index changeObject:@(YES) forKey:@"2"];
142+
143+
UICollectionViewCell *cell = [self.dataSource collectionView:self.collectionView
144+
cellForItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0]];
145+
146+
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
147+
XCTAssertEqualObjects(cell.accessibilityValue, @"changed");
148+
}
149+
150+
- (void)testItUpdatesOnMove {
151+
// move item to back
152+
[self.data moveObjectFromIndex:0 toIndex:2];
153+
[self.index moveObjectFromIndex:0 toIndex:2];
154+
155+
UICollectionViewCell *cell = [self.dataSource collectionView:self.collectionView
156+
cellForItemAtIndexPath:[NSIndexPath indexPathForItem:2 inSection:0]];
157+
158+
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
159+
XCTAssertEqualObjects(cell.accessibilityValue, @"1");
160+
161+
// move item to front
162+
[self.data moveObjectFromIndex:2 toIndex:0];
163+
[self.index moveObjectFromIndex:2 toIndex:0];
164+
165+
cell = [self.dataSource collectionView:self.collectionView
166+
cellForItemAtIndexPath:[NSIndexPath indexPathForItem:2 inSection:0]];
167+
168+
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
169+
XCTAssertEqualObjects(cell.accessibilityValue, @"3");
170+
}
171+
172+
@end

FirebaseUI.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
8D01FC7B1DAEC2FF00BD7848 /* FirebaseIndexCollectionViewDataSourceTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D01FC7A1DAEC2FF00BD7848 /* FirebaseIndexCollectionViewDataSourceTest.m */; };
1011
8D1E107C1DAEB97300AEFCA0 /* FirebaseIndexTableViewDataSourceTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D1E107B1DAEB97300AEFCA0 /* FirebaseIndexTableViewDataSourceTest.m */; };
1112
8D2A84AA1D678B2B0058DF04 /* FirebaseUI.h in Headers */ = {isa = PBXBuildFile; fileRef = 8D2A84A91D678B2B0058DF04 /* FirebaseUI.h */; settings = {ATTRIBUTES = (Public, ); }; };
1213
8D78AF061D9D8CB000CFA9C5 /* UIImageView+FirebaseStorage.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D7AD9B61D9317FB006866B9 /* UIImageView+FirebaseStorage.m */; };
@@ -348,6 +349,7 @@
348349
/* End PBXCopyFilesBuildPhase section */
349350

350351
/* Begin PBXFileReference section */
352+
8D01FC7A1DAEC2FF00BD7848 /* FirebaseIndexCollectionViewDataSourceTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FirebaseIndexCollectionViewDataSourceTest.m; sourceTree = "<group>"; };
351353
8D1E107B1DAEB97300AEFCA0 /* FirebaseIndexTableViewDataSourceTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FirebaseIndexTableViewDataSourceTest.m; sourceTree = "<group>"; };
352354
8D2A84A61D678B2B0058DF04 /* FirebaseUI.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = FirebaseUI.framework; sourceTree = BUILT_PRODUCTS_DIR; };
353355
8D2A84A91D678B2B0058DF04 /* FirebaseUI.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FirebaseUI.h; sourceTree = "<group>"; };
@@ -864,6 +866,7 @@
864866
8DA941EC1D67951B00CD3685 /* FirebaseCollectionViewDataSourceTest.m */,
865867
8DA941ED1D67951B00CD3685 /* FirebaseTableViewDataSourceTest.m */,
866868
8D1E107B1DAEB97300AEFCA0 /* FirebaseIndexTableViewDataSourceTest.m */,
869+
8D01FC7A1DAEC2FF00BD7848 /* FirebaseIndexCollectionViewDataSourceTest.m */,
867870
);
868871
path = FirebaseDatabaseUITests;
869872
sourceTree = "<group>";
@@ -1900,6 +1903,7 @@
19001903
files = (
19011904
8DA941F01D67951B00CD3685 /* FirebaseCollectionViewDataSourceTest.m in Sources */,
19021905
8DA941F11D67951B00CD3685 /* FirebaseTableViewDataSourceTest.m in Sources */,
1906+
8D01FC7B1DAEC2FF00BD7848 /* FirebaseIndexCollectionViewDataSourceTest.m in Sources */,
19031907
8D1E107C1DAEB97300AEFCA0 /* FirebaseIndexTableViewDataSourceTest.m in Sources */,
19041908
8DA941EF1D67951B00CD3685 /* FirebaseArrayTestUtils.m in Sources */,
19051909
8D924C611DA6F69100C4DA48 /* FirebaseIndexArrayTest.m in Sources */,

0 commit comments

Comments
 (0)