|
| 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 FirebaseDatabaseUI; |
| 19 | + |
| 20 | +#import "FUISortedArray.h" |
| 21 | + |
| 22 | +#import "FUIDatabaseTestUtils.h" |
| 23 | + |
| 24 | +@interface FUISortedArrayTest : XCTestCase |
| 25 | + |
| 26 | +@property (nonatomic, nullable) FUIArrayTestDelegate *arrayDelegate; |
| 27 | +@property (nonatomic, nullable) FUITestObservable *observable; |
| 28 | +@property (nonatomic, nullable) FUISortedArray *array; |
| 29 | +@property (nonatomic, nullable) FUIFakeSnapshot *snap; |
| 30 | + |
| 31 | +@end |
| 32 | + |
| 33 | +@implementation FUISortedArrayTest |
| 34 | + |
| 35 | +- (void)setUp { |
| 36 | + [super setUp]; |
| 37 | + self.arrayDelegate = [[FUIArrayTestDelegate alloc] init]; |
| 38 | + self.snap = [[FUIFakeSnapshot alloc] init]; |
| 39 | + self.observable = [[FUITestObservable alloc] init]; |
| 40 | + self.array = [[FUISortedArray alloc] initWithQuery:self.observable |
| 41 | + delegate:self.arrayDelegate |
| 42 | + sortDescriptor:^NSComparisonResult(FIRDataSnapshot *left, |
| 43 | + FIRDataSnapshot *right) { |
| 44 | + return [left.key compare:right.key]; |
| 45 | + }]; |
| 46 | + [self.array observeQuery]; |
| 47 | +} |
| 48 | + |
| 49 | +- (void)tearDown { |
| 50 | + [super tearDown]; |
| 51 | +} |
| 52 | + |
| 53 | +- (void)testArrayCanBeInitialized { |
| 54 | + XCTAssertNotNil(self.array, @"expected array to not be nil when initialized"); |
| 55 | +} |
| 56 | + |
| 57 | +- (void)testItSortsItselfOnMiddleInsert { |
| 58 | + [self.observable populateWithCount:10]; |
| 59 | + |
| 60 | + // Test delegate |
| 61 | + __block BOOL delegateWasCalled = NO; |
| 62 | + __block BOOL expectedParametersWereCorrect = NO; |
| 63 | + self.arrayDelegate.didAddObject = ^(FUISortedArray *array, id object, NSUInteger index) { |
| 64 | + // Xcode complains about retain cycles if an XCTAssert is placed in here. |
| 65 | + delegateWasCalled = YES; |
| 66 | + expectedParametersWereCorrect = (array == self.array && |
| 67 | + object == self.snap && |
| 68 | + // index should be 2 since "11" comes before "2" alphabetically. |
| 69 | + index == 2); |
| 70 | + }; |
| 71 | + |
| 72 | + // Test insert |
| 73 | + self.snap.key = @"11"; |
| 74 | + [self.observable sendEvent:FIRDataEventTypeChildAdded |
| 75 | + withObject:self.snap |
| 76 | + previousKey:@"0" // insert after "0" should be ignored to maintain sort |
| 77 | + error:nil]; |
| 78 | + // Array expectations |
| 79 | + XCTAssert(self.array.count == 11, @"expected empty array to contain one item after insert"); |
| 80 | + |
| 81 | + // Delegate expectations |
| 82 | + XCTAssert(delegateWasCalled, @"expected delegate to receive callback for insertion"); |
| 83 | + XCTAssert(expectedParametersWereCorrect, @"unexpected parameter in delegate callback"); |
| 84 | +} |
| 85 | + |
| 86 | +- (void)testItSortsItselfOnBeginningInsert { |
| 87 | + [self.observable populateWithCount:10]; |
| 88 | + |
| 89 | + // Test delegate |
| 90 | + __block BOOL delegateWasCalled = NO; |
| 91 | + __block BOOL expectedParametersWereCorrect = NO; |
| 92 | + self.arrayDelegate.didAddObject = ^(FUISortedArray *array, id object, NSUInteger index) { |
| 93 | + // Xcode complains about retain cycles if an XCTAssert is placed in here. |
| 94 | + delegateWasCalled = YES; |
| 95 | + expectedParametersWereCorrect = (array == self.array && |
| 96 | + object == self.snap && |
| 97 | + // index should be 0 since "+" comes before "0" alphabetically. |
| 98 | + index == 0); |
| 99 | + }; |
| 100 | + |
| 101 | + // Test insert |
| 102 | + self.snap.key = @"+"; |
| 103 | + [self.observable sendEvent:FIRDataEventTypeChildAdded |
| 104 | + withObject:self.snap |
| 105 | + previousKey:@"0" // insert after "0" should be ignored to maintain sort |
| 106 | + error:nil]; |
| 107 | + // Array expectations |
| 108 | + XCTAssert(self.array.count == 11, @"expected empty array to contain one item after insert"); |
| 109 | + |
| 110 | + // Delegate expectations |
| 111 | + XCTAssert(delegateWasCalled, @"expected delegate to receive callback for insertion"); |
| 112 | + XCTAssert(expectedParametersWereCorrect, @"unexpected parameter in delegate callback"); |
| 113 | +} |
| 114 | + |
| 115 | +- (void)testItSortsItselfOnEndInsert { |
| 116 | + [self.observable populateWithCount:10]; |
| 117 | + |
| 118 | + // Test delegate |
| 119 | + __block BOOL delegateWasCalled = NO; |
| 120 | + __block BOOL expectedParametersWereCorrect = NO; |
| 121 | + self.arrayDelegate.didAddObject = ^(FUISortedArray *array, id object, NSUInteger index) { |
| 122 | + // Xcode complains about retain cycles if an XCTAssert is placed in here. |
| 123 | + delegateWasCalled = YES; |
| 124 | + expectedParametersWereCorrect = (array == self.array && |
| 125 | + object == self.snap && |
| 126 | + // index should be 10 since "a" comes after "9" alphabetically. |
| 127 | + index == 10); |
| 128 | + }; |
| 129 | + |
| 130 | + // Test insert |
| 131 | + self.snap.key = @"a"; |
| 132 | + [self.observable sendEvent:FIRDataEventTypeChildAdded |
| 133 | + withObject:self.snap |
| 134 | + previousKey:@"0" // insert after "0" should be ignored to maintain sort |
| 135 | + error:nil]; |
| 136 | + // Array expectations |
| 137 | + XCTAssert(self.array.count == 11, @"expected empty array to contain one item after insert"); |
| 138 | + |
| 139 | + // Delegate expectations |
| 140 | + XCTAssert(delegateWasCalled, @"expected delegate to receive callback for insertion"); |
| 141 | + XCTAssert(expectedParametersWereCorrect, @"unexpected parameter in delegate callback"); |
| 142 | +} |
| 143 | + |
| 144 | +- (void)testEmptyArrayUpdatesCountOnInsert { |
| 145 | + // Test delegate |
| 146 | + __block BOOL delegateWasCalled = NO; |
| 147 | + __block BOOL expectedParametersWereCorrect = NO; |
| 148 | + self.arrayDelegate.didAddObject = ^(FUISortedArray *array, id object, NSUInteger index) { |
| 149 | + // Xcode complains about retain cycles if an XCTAssert is placed in here. |
| 150 | + delegateWasCalled = YES; |
| 151 | + expectedParametersWereCorrect = (array == self.array && |
| 152 | + object == self.snap && |
| 153 | + index == 0); |
| 154 | + }; |
| 155 | + |
| 156 | + // Test insert |
| 157 | + self.snap.key = @"snapshot"; |
| 158 | + [self.observable sendEvent:FIRDataEventTypeChildAdded |
| 159 | + withObject:self.snap |
| 160 | + previousKey:nil |
| 161 | + error:nil]; |
| 162 | + // Array expectations |
| 163 | + XCTAssert(self.array.count == 1, @"expected empty array to contain one item after insert"); |
| 164 | + |
| 165 | + // Delegate expectations |
| 166 | + XCTAssert(delegateWasCalled, @"expected delegate to receive callback for insertion"); |
| 167 | + XCTAssert(expectedParametersWereCorrect, @"unexpected parameter in delegate callback"); |
| 168 | +} |
| 169 | + |
| 170 | +@end |
0 commit comments