FTestBase.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright 2017 Google
  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. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  17. #import "FirebaseDatabase/Sources/Api/Private/FIRDatabaseQuery_Private.h"
  18. #import "FirebaseDatabase/Tests/Helpers/FTestBase.h"
  19. #import "SharedTestUtilities/FIROptionsMock.h"
  20. @interface FIROptions (Testing)
  21. + (NSString *)plistFilePathWithName:(NSString *)fileName;
  22. @end
  23. @implementation FTestBase
  24. + (void)setUp {
  25. static dispatch_once_t once;
  26. dispatch_once(&once, ^{
  27. if ([FIROptions plistFilePathWithName:@"GoogleService-Info"] == nil) {
  28. // Mock options for unit tests and emulator runs.
  29. // To run the integration tests with a real GoogleService-Info.plist, add the
  30. // GoogleService-Info.plist file to the AppHost-FirebaseDatabase-Unit-Tests generated target
  31. // in the Pods project created by `pod gen`.
  32. [FIROptionsMock mockFIROptions];
  33. }
  34. [FIRApp configure];
  35. });
  36. }
  37. - (void)setUp {
  38. [super setUp];
  39. [FIRDatabase setLoggingEnabled:YES];
  40. _databaseURL = [FTestHelpers databaseURL];
  41. // Disabled normally since they slow down the tests and don't actually assert anything (they just
  42. // NSLog timings).
  43. runPerfTests = NO;
  44. }
  45. - (void)snapWaiter:(FIRDatabaseReference *)path withBlock:(fbt_void_datasnapshot)fn {
  46. __block BOOL done = NO;
  47. [path observeSingleEventOfType:FIRDataEventTypeValue
  48. withBlock:^(FIRDataSnapshot *snap) {
  49. fn(snap);
  50. done = YES;
  51. }];
  52. NSTimeInterval timeTaken = [self
  53. waitUntil:^BOOL {
  54. return done;
  55. }
  56. timeout:kFirebaseTestWaitUntilTimeout];
  57. NSLog(@"snapWaiter:withBlock: timeTaken:%f", timeTaken);
  58. XCTAssertTrue(done, @"Properly finished.");
  59. }
  60. - (void)waitUntilConnected:(FIRDatabaseReference *)ref {
  61. __block BOOL connected = NO;
  62. FIRDatabaseHandle handle =
  63. [[ref.root child:@".info/connected"] observeEventType:FIRDataEventTypeValue
  64. withBlock:^(FIRDataSnapshot *snapshot) {
  65. connected = [snapshot.value boolValue];
  66. }];
  67. WAIT_FOR(connected);
  68. [ref.root removeObserverWithHandle:handle];
  69. }
  70. - (void)waitForRoundTrip:(FIRDatabaseReference *)ref {
  71. // HACK: Do a deep setPriority (which we expect to fail because there's no data there) to do a
  72. // no-op roundtrip.
  73. __block BOOL done = NO;
  74. [[ref.root child:@"ENTOHTNUHOE/ONTEHNUHTOE"]
  75. setPriority:@"blah"
  76. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  77. done = YES;
  78. }];
  79. WAIT_FOR(done);
  80. }
  81. - (void)waitForQueue:(FIRDatabaseReference *)ref {
  82. dispatch_sync([FIRDatabaseQuery sharedQueue], ^{
  83. });
  84. }
  85. - (void)waitForEvents:(FIRDatabaseReference *)ref {
  86. [self waitForQueue:ref];
  87. __block BOOL done = NO;
  88. dispatch_async(dispatch_get_main_queue(), ^{
  89. done = YES;
  90. });
  91. WAIT_FOR(done);
  92. }
  93. - (void)waitForValueOf:(FIRDatabaseQuery *)ref toBe:(id)expected {
  94. __block id value;
  95. FIRDatabaseHandle handle = [ref observeEventType:FIRDataEventTypeValue
  96. withBlock:^(FIRDataSnapshot *snapshot) {
  97. value = snapshot.value;
  98. }];
  99. @try {
  100. [self waitUntil:^BOOL {
  101. return [value isEqual:expected];
  102. }];
  103. } @catch (NSException *exception) {
  104. @throw [NSException exceptionWithName:@"DidNotGetValue"
  105. reason:@"Did not get expected value"
  106. userInfo:@{
  107. @"expected" : (!expected ? @"nil" : expected),
  108. @"actual" : (!value ? @"nil" : value)
  109. }];
  110. } @finally {
  111. [ref removeObserverWithHandle:handle];
  112. }
  113. }
  114. - (void)waitForExportValueOf:(FIRDatabaseQuery *)ref toBe:(id)expected {
  115. __block id value;
  116. FIRDatabaseHandle handle = [ref observeEventType:FIRDataEventTypeValue
  117. withBlock:^(FIRDataSnapshot *snapshot) {
  118. value = snapshot.valueInExportFormat;
  119. }];
  120. @try {
  121. [self waitUntil:^BOOL {
  122. return [value isEqual:expected];
  123. }];
  124. } @catch (NSException *exception) {
  125. if ([exception.name isEqualToString:@"Timed out"]) {
  126. @throw [NSException exceptionWithName:@"DidNotGetValue"
  127. reason:@"Did not get expected value"
  128. userInfo:@{
  129. @"expected" : (!expected ? @"nil" : expected),
  130. @"actual" : (!value ? @"nil" : value)
  131. }];
  132. } else {
  133. @throw exception;
  134. }
  135. } @finally {
  136. [ref removeObserverWithHandle:handle];
  137. }
  138. }
  139. - (void)waitForCompletionOf:(FIRDatabaseReference *)ref setValue:(id)value {
  140. [self waitForCompletionOf:ref setValue:value andPriority:nil];
  141. }
  142. - (void)waitForCompletionOf:(FIRDatabaseReference *)ref
  143. setValue:(id)value
  144. andPriority:(id)priority {
  145. __block BOOL done = NO;
  146. [ref setValue:value
  147. andPriority:priority
  148. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  149. done = YES;
  150. }];
  151. @try {
  152. WAIT_FOR(done);
  153. } @catch (NSException *exception) {
  154. @throw [NSException exceptionWithName:@"DidNotSetValue"
  155. reason:@"Did not complete setting value"
  156. userInfo:@{
  157. @"ref" : [ref description],
  158. @"done" : done ? @"true" : @"false",
  159. @"value" : (!value ? @"nil" : value),
  160. @"priority" : (!priority ? @"nil" : priority)
  161. }];
  162. }
  163. }
  164. - (void)waitForCompletionOf:(FIRDatabaseReference *)ref updateChildValues:(NSDictionary *)values {
  165. __block BOOL done = NO;
  166. [ref updateChildValues:values
  167. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  168. done = YES;
  169. }];
  170. @try {
  171. WAIT_FOR(done);
  172. } @catch (NSException *exception) {
  173. @throw [NSException
  174. exceptionWithName:@"DidNotUpdateChildValues"
  175. reason:@"Could not finish updating child values"
  176. userInfo:@{@"ref" : [ref description], @"values" : (!values ? @"nil" : values)}];
  177. }
  178. }
  179. @end