|
15 | 15 | */
|
16 | 16 |
|
17 | 17 | #import "FTestHelpers.h"
|
18 |
| - |
19 |
| -#import <FirebaseAuthInterop/FIRAuthInterop.h> |
20 |
| -#import <FirebaseCore/FIRAppInternal.h> |
21 |
| -#import <FirebaseCore/FIRComponent.h> |
22 |
| -#import <FirebaseCore/FIRComponentContainer.h> |
23 |
| -#import <FirebaseCore/FIROptions.h> |
24 |
| - |
25 | 18 | #import "FConstants.h"
|
26 |
| -#import "FIRAuthInteropFake.h" |
| 19 | +#import <FirebaseCore/FIRApp.h> |
| 20 | +#import <FirebaseCore/FIROptions.h> |
27 | 21 | #import "FIRDatabaseConfig_Private.h"
|
28 | 22 | #import "FTestAuthTokenGenerator.h"
|
29 | 23 |
|
30 | 24 | @implementation FTestHelpers
|
31 | 25 |
|
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; |
37 |
| - |
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 |
| - } |
| 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; |
44 | 31 |
|
45 |
| - NSTimeInterval finish = [NSDate timeIntervalSinceReferenceDate]; |
| 32 | + for (currentTime = [NSDate timeIntervalSinceReferenceDate]; |
| 33 | + !predicate() && currentTime < timeoutTime; |
| 34 | + currentTime = [NSDate timeIntervalSinceReferenceDate]) { |
| 35 | + [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.25]]; |
| 36 | + } |
46 | 37 |
|
47 |
| - NSAssert(currentTime <= timeoutTime, @"Timed out"); |
| 38 | + NSTimeInterval finish = [NSDate timeIntervalSinceReferenceDate]; |
48 | 39 |
|
49 |
| - return (finish - start); |
50 |
| -} |
| 40 | + NSAssert(currentTime <= timeoutTime, @"Timed out"); |
51 | 41 |
|
52 |
| -+ (FIRDatabaseConfig *)defaultConfig { |
53 |
| - return [self configForName:@"default"]; |
| 42 | + return (finish - start); |
54 | 43 | }
|
55 | 44 |
|
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 |
| -} |
| 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 | + } |
62 | 67 |
|
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; |
| 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]]; |
97 | 76 | }
|
98 |
| - [results addObject:[ref child:name]]; |
99 |
| - } |
100 |
| - return results; |
| 77 | + return results; |
101 | 78 | }
|
102 | 79 |
|
103 | 80 | // Helpers
|
104 |
| -+ (FIRDatabaseReference *)getRandomNode { |
105 |
| - NSArray *refs = [self getRandomNodes:1 persistence:YES]; |
106 |
| - return [refs objectAtIndex:0]; |
| 81 | ++ (FIRDatabaseReference *) getRandomNode { |
| 82 | + NSArray* refs = [self getRandomNodes:1 persistence:YES]; |
| 83 | + return [refs objectAtIndex:0]; |
107 | 84 | }
|
108 | 85 |
|
109 |
| -+ (FIRDatabaseReference *)getRandomNodeWithoutPersistence { |
110 |
| - NSArray *refs = [self getRandomNodes:1 persistence:NO]; |
111 |
| - return refs[0]; |
| 86 | ++ (FIRDatabaseReference *) getRandomNodeWithoutPersistence { |
| 87 | + NSArray* refs = [self getRandomNodes:1 persistence:NO]; |
| 88 | + return refs[0]; |
112 | 89 | }
|
113 | 90 |
|
114 |
| -+ (FTupleFirebase *)getRandomNodePair { |
115 |
| - NSArray *refs = [self getRandomNodes:2 persistence:YES]; |
| 91 | ++ (FTupleFirebase *) getRandomNodePair { |
| 92 | + NSArray* refs = [self getRandomNodes:2 persistence:YES]; |
116 | 93 |
|
117 |
| - FTupleFirebase *tuple = [[FTupleFirebase alloc] init]; |
118 |
| - tuple.one = [refs objectAtIndex:0]; |
119 |
| - tuple.two = [refs objectAtIndex:1]; |
| 94 | + FTupleFirebase* tuple = [[FTupleFirebase alloc] init]; |
| 95 | + tuple.one = [refs objectAtIndex:0]; |
| 96 | + tuple.two = [refs objectAtIndex:1]; |
120 | 97 |
|
121 |
| - return tuple; |
| 98 | + return tuple; |
122 | 99 | }
|
123 | 100 |
|
124 |
| -+ (FTupleFirebase *)getRandomNodePairWithoutPersistence { |
125 |
| - NSArray *refs = [self getRandomNodes:2 persistence:NO]; |
| 101 | ++ (FTupleFirebase *) getRandomNodePairWithoutPersistence { |
| 102 | + NSArray* refs = [self getRandomNodes:2 persistence:NO]; |
126 | 103 |
|
127 |
| - FTupleFirebase *tuple = [[FTupleFirebase alloc] init]; |
128 |
| - tuple.one = refs[0]; |
129 |
| - tuple.two = refs[1]; |
| 104 | + FTupleFirebase* tuple = [[FTupleFirebase alloc] init]; |
| 105 | + tuple.one = refs[0]; |
| 106 | + tuple.two = refs[1]; |
130 | 107 |
|
131 |
| - return tuple; |
| 108 | + return tuple; |
132 | 109 | }
|
133 | 110 |
|
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]; |
| 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]; |
140 | 117 |
|
141 |
| - return triple; |
| 118 | + return triple; |
142 | 119 | }
|
143 | 120 |
|
144 | 121 | + (id<FNode>)leafNodeOfSize:(NSUInteger)size {
|
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]; |
| 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]; |
153 | 130 | }
|
154 | 131 |
|
155 | 132 | @end
|
0 commit comments