|
| 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 |
0 commit comments