|
| 1 | +// clang-format off |
| 2 | + |
| 3 | +// |
| 4 | +// Copyright (c) 2016 Google Inc. |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | + |
| 19 | +// clang-format on |
| 20 | + |
| 21 | +#import "FirebaseDatabaseTestUtils.h" |
| 22 | + |
| 23 | +@implementation FUIDataEventHandler |
| 24 | +@end |
| 25 | + |
| 26 | +@implementation FUIFakeSnapshot |
| 27 | +- (instancetype)initWithKey:(NSString *)key value:(NSString *)value { |
| 28 | + self = [super init]; |
| 29 | + if (self != nil) { |
| 30 | + _key = [key copy]; |
| 31 | + _value = [value copy]; |
| 32 | + } |
| 33 | + return self; |
| 34 | +} |
| 35 | +@end |
| 36 | + |
| 37 | +@implementation FUITestObservable |
| 38 | + |
| 39 | +- (instancetype)init { |
| 40 | + self = [super init]; |
| 41 | + if (self != nil) { |
| 42 | + _observers = [NSMutableDictionary dictionary]; |
| 43 | + } |
| 44 | + return self; |
| 45 | +} |
| 46 | + |
| 47 | +- (void)removeObserverWithHandle:(FIRDatabaseHandle)handle { |
| 48 | + [self.observers removeObjectForKey:@(handle)]; |
| 49 | +} |
| 50 | + |
| 51 | +- (void)removeAllObservers { |
| 52 | + _observers = [NSMutableDictionary dictionary]; |
| 53 | +} |
| 54 | + |
| 55 | +- (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType |
| 56 | + andPreviousSiblingKeyWithBlock:(void (^)(FIRDataSnapshot * _Nonnull, NSString * _Nullable))block |
| 57 | + withCancelBlock:(void (^)(NSError * _Nonnull))cancelBlock { |
| 58 | + FUIDataEventHandler *handler = [[FUIDataEventHandler alloc] init]; |
| 59 | + handler.event = eventType; |
| 60 | + handler.success = block; |
| 61 | + handler.cancelled = cancelBlock; |
| 62 | + |
| 63 | + NSNumber *key = @(self.current); |
| 64 | + _current++; |
| 65 | + self.observers[key] = handler; |
| 66 | + return key.unsignedIntegerValue; |
| 67 | +} |
| 68 | + |
| 69 | +- (void)sendEvent:(FIRDataEventType)event |
| 70 | + withObject:(FUIFakeSnapshot *)object |
| 71 | + previousKey:(NSString *)string |
| 72 | + error:(NSError *)error { |
| 73 | + NSArray *allKeys = self.observers.allKeys; |
| 74 | + for (NSNumber *key in allKeys) { |
| 75 | + FUIDataEventHandler *handler = self.observers[key]; |
| 76 | + if (handler.event == event) { |
| 77 | + if (error != nil) { handler.cancelled(error); } |
| 78 | + else { handler.success((FIRDataSnapshot *)object, string); } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +- (void)populateWithCount:(NSUInteger)count generator:(NSString *(^)(NSUInteger))generator { |
| 84 | + NSString *previous = nil; |
| 85 | + for (NSUInteger i = 0; i < count; i++) { |
| 86 | + FUIFakeSnapshot *snap = [[FUIFakeSnapshot alloc] init]; |
| 87 | + snap.value = generator(i); |
| 88 | + snap.key = @(i).stringValue; |
| 89 | + [self sendEvent:FIRDataEventTypeChildAdded withObject:snap previousKey:previous error:nil]; |
| 90 | + previous = snap.key; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +- (void)populateWithCount:(NSUInteger)count { |
| 95 | + [self populateWithCount:count generator:^NSString *(NSUInteger index) { |
| 96 | + return @(index).stringValue; |
| 97 | + }]; |
| 98 | +} |
| 99 | + |
| 100 | +@end |
| 101 | + |
| 102 | +@implementation FUIFirebaseArrayTestDelegate |
| 103 | + |
| 104 | +- (void)array:(FUIArray *)array didAddObject:(id)object atIndex:(NSUInteger)index { |
| 105 | + if (self.didAddObject != NULL) { |
| 106 | + self.didAddObject(array, object, index); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +- (void)array:(FUIArray *)array didChangeObject:(id)object atIndex:(NSUInteger)index { |
| 111 | + if (self.didChangeObject != NULL) { |
| 112 | + self.didChangeObject(array, object, index); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +- (void)array:(FUIArray *)array didRemoveObject:(id)object atIndex:(NSUInteger)index { |
| 117 | + if (self.didRemoveObject != NULL) { |
| 118 | + self.didRemoveObject(array, object, index); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +- (void)array:(FUIArray *)array didMoveObject:(id)object |
| 123 | + fromIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex { |
| 124 | + if (self.didMoveObject != NULL) { |
| 125 | + self.didMoveObject(array, object, fromIndex, toIndex); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +- (void)array:(FUIArray *)array queryCancelledWithError:(NSError *)error { |
| 130 | + if (self.queryCancelled != NULL) { |
| 131 | + self.queryCancelled(array, error); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +@end |
0 commit comments