|
15 | 15 | */ |
16 | 16 |
|
17 | 17 | #import "FTestHelpers.h" |
18 | | -#import "FConstants.h" |
19 | | -#import <FirebaseCore/FIRApp.h> |
| 18 | + |
| 19 | +#import <FirebaseAuthInterop/FIRAuthInterop.h> |
| 20 | +#import <FirebaseCore/FIRAppInternal.h> |
| 21 | +#import <FirebaseCore/FIRComponent.h> |
| 22 | +#import <FirebaseCore/FIRComponentContainer.h> |
20 | 23 | #import <FirebaseCore/FIROptions.h> |
| 24 | + |
| 25 | +#import "FConstants.h" |
| 26 | +#import "FIRAuthInteropFake.h" |
21 | 27 | #import "FIRDatabaseConfig_Private.h" |
22 | 28 | #import "FTestAuthTokenGenerator.h" |
23 | 29 |
|
24 | 30 | @implementation FTestHelpers |
25 | 31 |
|
26 | | -+ (NSTimeInterval) waitUntil:(BOOL (^)())predicate timeout:(NSTimeInterval)seconds { |
27 | | - NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate]; |
28 | | - NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:seconds]; |
29 | | - NSTimeInterval timeoutTime = [timeoutDate timeIntervalSinceReferenceDate]; |
30 | | - NSTimeInterval currentTime; |
| 32 | ++ (NSTimeInterval)waitUntil:(BOOL (^)())predicate timeout:(NSTimeInterval)seconds { |
| 33 | + NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate]; |
| 34 | + NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:seconds]; |
| 35 | + NSTimeInterval timeoutTime = [timeoutDate timeIntervalSinceReferenceDate]; |
| 36 | + NSTimeInterval currentTime; |
31 | 37 |
|
32 | | - for (currentTime = [NSDate timeIntervalSinceReferenceDate]; |
33 | | - !predicate() && currentTime < timeoutTime; |
34 | | - currentTime = [NSDate timeIntervalSinceReferenceDate]) { |
35 | | - [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.25]]; |
36 | | - } |
| 38 | + for (currentTime = [NSDate timeIntervalSinceReferenceDate]; |
| 39 | + !predicate() && currentTime < timeoutTime; |
| 40 | + currentTime = [NSDate timeIntervalSinceReferenceDate]) { |
| 41 | + [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode |
| 42 | + beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.25]]; |
| 43 | + } |
37 | 44 |
|
38 | | - NSTimeInterval finish = [NSDate timeIntervalSinceReferenceDate]; |
| 45 | + NSTimeInterval finish = [NSDate timeIntervalSinceReferenceDate]; |
39 | 46 |
|
40 | | - NSAssert(currentTime <= timeoutTime, @"Timed out"); |
| 47 | + NSAssert(currentTime <= timeoutTime, @"Timed out"); |
41 | 48 |
|
42 | | - return (finish - start); |
| 49 | + return (finish - start); |
43 | 50 | } |
44 | 51 |
|
45 | | -+ (NSArray*) getRandomNodes:(int)num persistence:(BOOL)persistence { |
46 | | - static dispatch_once_t pred = 0; |
47 | | - static NSMutableArray *persistenceRefs = nil; |
48 | | - static NSMutableArray *noPersistenceRefs = nil; |
49 | | - dispatch_once(&pred, ^{ |
50 | | - persistenceRefs = [[NSMutableArray alloc] init]; |
51 | | - noPersistenceRefs = [[NSMutableArray alloc] init]; |
52 | | - // Uncomment the following line to run tests against a background thread |
53 | | - //[Firebase setDispatchQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)]; |
54 | | - }); |
55 | | - |
56 | | - NSMutableArray *refs = (persistence) ? persistenceRefs : noPersistenceRefs; |
57 | | - |
58 | | - id<FAuthTokenProvider> authTokenProvider = [FAuthTokenProvider authTokenProviderForApp:[FIRApp defaultApp]]; |
59 | | - |
60 | | - while (num > refs.count) { |
61 | | - NSString *sessionIdentifier = [NSString stringWithFormat:@"test-config-%@persistence-%lu", (persistence) ? @"" : @"no-", refs.count]; |
62 | | - FIRDatabaseConfig *config = [[FIRDatabaseConfig alloc] initWithSessionIdentifier:sessionIdentifier authTokenProvider:authTokenProvider]; |
63 | | - config.persistenceEnabled = persistence; |
64 | | - FIRDatabaseReference * ref = [[FIRDatabaseReference alloc] initWithConfig:config]; |
65 | | - [refs addObject:ref]; |
66 | | - } |
| 52 | ++ (FIRDatabaseConfig *)defaultConfig { |
| 53 | + return [self configForName:@"default"]; |
| 54 | +} |
67 | 55 |
|
68 | | - NSMutableArray* results = [[NSMutableArray alloc] init]; |
69 | | - NSString* name = nil; |
70 | | - for (int i = 0; i < num; ++i) { |
71 | | - FIRDatabaseReference * ref = [refs objectAtIndex:i]; |
72 | | - if (!name) { |
73 | | - name = [ref childByAutoId].key; |
74 | | - } |
75 | | - [results addObject:[ref child:name]]; |
| 56 | ++ (FIRDatabaseConfig *)configForName:(NSString *)name { |
| 57 | + id<FIRAuthInterop> auth = [[FIRAuthInteropFake alloc] initWithToken:nil userID:nil error:nil]; |
| 58 | + id<FAuthTokenProvider> authTokenProvider = [FAuthTokenProvider authTokenProviderWithAuth:auth]; |
| 59 | + return [[FIRDatabaseConfig alloc] initWithSessionIdentifier:name |
| 60 | + authTokenProvider:authTokenProvider]; |
| 61 | +} |
| 62 | + |
| 63 | ++ (NSArray *)getRandomNodes:(int)num persistence:(BOOL)persistence { |
| 64 | + static dispatch_once_t pred = 0; |
| 65 | + static NSMutableArray *persistenceRefs = nil; |
| 66 | + static NSMutableArray *noPersistenceRefs = nil; |
| 67 | + dispatch_once(&pred, ^{ |
| 68 | + persistenceRefs = [[NSMutableArray alloc] init]; |
| 69 | + noPersistenceRefs = [[NSMutableArray alloc] init]; |
| 70 | + // Uncomment the following line to run tests against a background thread |
| 71 | + //[Firebase setDispatchQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)]; |
| 72 | + }); |
| 73 | + |
| 74 | + NSMutableArray *refs = (persistence) ? persistenceRefs : noPersistenceRefs; |
| 75 | + |
| 76 | + id<FIRAuthInterop> auth = [[FIRAuthInteropFake alloc] initWithToken:nil userID:nil error:nil]; |
| 77 | + id<FAuthTokenProvider> authTokenProvider = [FAuthTokenProvider authTokenProviderWithAuth:auth]; |
| 78 | + |
| 79 | + while (num > refs.count) { |
| 80 | + NSString *sessionIdentifier = |
| 81 | + [NSString stringWithFormat:@"test-config-%@persistence-%lu", (persistence) ? @"" : @"no-", |
| 82 | + refs.count]; |
| 83 | + FIRDatabaseConfig *config = |
| 84 | + [[FIRDatabaseConfig alloc] initWithSessionIdentifier:sessionIdentifier |
| 85 | + authTokenProvider:authTokenProvider]; |
| 86 | + config.persistenceEnabled = persistence; |
| 87 | + FIRDatabaseReference *ref = [[FIRDatabaseReference alloc] initWithConfig:config]; |
| 88 | + [refs addObject:ref]; |
| 89 | + } |
| 90 | + |
| 91 | + NSMutableArray *results = [[NSMutableArray alloc] init]; |
| 92 | + NSString *name = nil; |
| 93 | + for (int i = 0; i < num; ++i) { |
| 94 | + FIRDatabaseReference *ref = [refs objectAtIndex:i]; |
| 95 | + if (!name) { |
| 96 | + name = [ref childByAutoId].key; |
76 | 97 | } |
77 | | - return results; |
| 98 | + [results addObject:[ref child:name]]; |
| 99 | + } |
| 100 | + return results; |
78 | 101 | } |
79 | 102 |
|
80 | 103 | // Helpers |
81 | | -+ (FIRDatabaseReference *) getRandomNode { |
82 | | - NSArray* refs = [self getRandomNodes:1 persistence:YES]; |
83 | | - return [refs objectAtIndex:0]; |
| 104 | ++ (FIRDatabaseReference *)getRandomNode { |
| 105 | + NSArray *refs = [self getRandomNodes:1 persistence:YES]; |
| 106 | + return [refs objectAtIndex:0]; |
84 | 107 | } |
85 | 108 |
|
86 | | -+ (FIRDatabaseReference *) getRandomNodeWithoutPersistence { |
87 | | - NSArray* refs = [self getRandomNodes:1 persistence:NO]; |
88 | | - return refs[0]; |
| 109 | ++ (FIRDatabaseReference *)getRandomNodeWithoutPersistence { |
| 110 | + NSArray *refs = [self getRandomNodes:1 persistence:NO]; |
| 111 | + return refs[0]; |
89 | 112 | } |
90 | 113 |
|
91 | | -+ (FTupleFirebase *) getRandomNodePair { |
92 | | - NSArray* refs = [self getRandomNodes:2 persistence:YES]; |
| 114 | ++ (FTupleFirebase *)getRandomNodePair { |
| 115 | + NSArray *refs = [self getRandomNodes:2 persistence:YES]; |
93 | 116 |
|
94 | | - FTupleFirebase* tuple = [[FTupleFirebase alloc] init]; |
95 | | - tuple.one = [refs objectAtIndex:0]; |
96 | | - tuple.two = [refs objectAtIndex:1]; |
| 117 | + FTupleFirebase *tuple = [[FTupleFirebase alloc] init]; |
| 118 | + tuple.one = [refs objectAtIndex:0]; |
| 119 | + tuple.two = [refs objectAtIndex:1]; |
97 | 120 |
|
98 | | - return tuple; |
| 121 | + return tuple; |
99 | 122 | } |
100 | 123 |
|
101 | | -+ (FTupleFirebase *) getRandomNodePairWithoutPersistence { |
102 | | - NSArray* refs = [self getRandomNodes:2 persistence:NO]; |
| 124 | ++ (FTupleFirebase *)getRandomNodePairWithoutPersistence { |
| 125 | + NSArray *refs = [self getRandomNodes:2 persistence:NO]; |
103 | 126 |
|
104 | | - FTupleFirebase* tuple = [[FTupleFirebase alloc] init]; |
105 | | - tuple.one = refs[0]; |
106 | | - tuple.two = refs[1]; |
| 127 | + FTupleFirebase *tuple = [[FTupleFirebase alloc] init]; |
| 128 | + tuple.one = refs[0]; |
| 129 | + tuple.two = refs[1]; |
107 | 130 |
|
108 | | - return tuple; |
| 131 | + return tuple; |
109 | 132 | } |
110 | 133 |
|
111 | | -+ (FTupleFirebase *) getRandomNodeTriple { |
112 | | - NSArray* refs = [self getRandomNodes:3 persistence:YES]; |
113 | | - FTupleFirebase* triple = [[FTupleFirebase alloc] init]; |
114 | | - triple.one = [refs objectAtIndex:0]; |
115 | | - triple.two = [refs objectAtIndex:1]; |
116 | | - triple.three = [refs objectAtIndex:2]; |
| 134 | ++ (FTupleFirebase *)getRandomNodeTriple { |
| 135 | + NSArray *refs = [self getRandomNodes:3 persistence:YES]; |
| 136 | + FTupleFirebase *triple = [[FTupleFirebase alloc] init]; |
| 137 | + triple.one = [refs objectAtIndex:0]; |
| 138 | + triple.two = [refs objectAtIndex:1]; |
| 139 | + triple.three = [refs objectAtIndex:2]; |
117 | 140 |
|
118 | | - return triple; |
| 141 | + return triple; |
119 | 142 | } |
120 | 143 |
|
121 | 144 | + (id<FNode>)leafNodeOfSize:(NSUInteger)size { |
122 | | - NSMutableString *string = [NSMutableString string]; |
123 | | - NSString *pattern = @"abdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
124 | | - for (NSUInteger i = 0; i < size - pattern.length; i = i + pattern.length) { |
125 | | - [string appendString:pattern]; |
126 | | - } |
127 | | - NSUInteger remainingLength = size - string.length; |
128 | | - [string appendString:[pattern substringToIndex:remainingLength]]; |
129 | | - return [FSnapshotUtilities nodeFrom:string]; |
| 145 | + NSMutableString *string = [NSMutableString string]; |
| 146 | + NSString *pattern = @"abdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
| 147 | + for (NSUInteger i = 0; i < size - pattern.length; i = i + pattern.length) { |
| 148 | + [string appendString:pattern]; |
| 149 | + } |
| 150 | + NSUInteger remainingLength = size - string.length; |
| 151 | + [string appendString:[pattern substringToIndex:remainingLength]]; |
| 152 | + return [FSnapshotUtilities nodeFrom:string]; |
130 | 153 | } |
131 | 154 |
|
132 | 155 | @end |
0 commit comments