Files
firebase-ios-sdk/Example/Database/Tests/Helpers/FTestBase.m
T

197 lines
6.4 KiB
Objective-C
Raw Normal View History

2017-05-15 12:27:07 -07:00
/*
* Copyright 2017 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2017-11-22 09:19:10 -08:00
#import <FirebaseCore/FIRApp.h>
#import <FirebaseCore/FIROptions.h>
2017-05-15 12:27:07 -07:00
#import "FIRDatabaseQuery_Private.h"
#import "FIRTestAuthTokenProvider.h"
2019-08-05 13:27:47 -07:00
#import "FTestAuthTokenGenerator.h"
#import "FTestBase.h"
2017-05-15 12:27:07 -07:00
@implementation FTestBase
2019-08-05 13:27:47 -07:00
+ (void)setUp {
static dispatch_once_t once;
dispatch_once(&once, ^{
[FIRApp configure];
});
2017-05-15 12:27:07 -07:00
}
2017-06-30 10:47:37 -07:00
2019-08-05 13:27:47 -07:00
- (void)setUp {
[super setUp];
2017-06-30 10:47:37 -07:00
2019-08-05 13:27:47 -07:00
[FIRDatabase setLoggingEnabled:YES];
_databaseURL = [[FIRApp defaultApp] options].databaseURL;
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
// Disabled normally since they slow down the tests and don't actually assert anything (they just
// NSLog timings).
runPerfTests = NO;
2017-05-15 12:27:07 -07:00
}
- (void)snapWaiter:(FIRDatabaseReference *)path withBlock:(fbt_void_datasnapshot)fn {
2019-08-05 13:27:47 -07:00
__block BOOL done = NO;
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[path observeSingleEventOfType:FIRDataEventTypeValue
withBlock:^(FIRDataSnapshot *snap) {
fn(snap);
done = YES;
}];
2017-06-30 10:47:37 -07:00
2019-08-05 13:27:47 -07:00
NSTimeInterval timeTaken = [self
waitUntil:^BOOL {
2017-05-15 12:27:07 -07:00
return done;
2019-08-05 13:27:47 -07:00
}
timeout:kFirebaseTestWaitUntilTimeout];
2017-06-30 10:47:37 -07:00
2019-08-05 13:27:47 -07:00
NSLog(@"snapWaiter:withBlock: timeTaken:%f", timeTaken);
2017-06-30 10:47:37 -07:00
2019-08-05 13:27:47 -07:00
XCTAssertTrue(done, @"Properly finished.");
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
- (void)waitUntilConnected:(FIRDatabaseReference *)ref {
__block BOOL connected = NO;
FIRDatabaseHandle handle =
[[ref.root child:@".info/connected"] observeEventType:FIRDataEventTypeValue
withBlock:^(FIRDataSnapshot *snapshot) {
connected = [snapshot.value boolValue];
}];
WAIT_FOR(connected);
[ref.root removeObserverWithHandle:handle];
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
- (void)waitForRoundTrip:(FIRDatabaseReference *)ref {
// HACK: Do a deep setPriority (which we expect to fail because there's no data there) to do a
// no-op roundtrip.
__block BOOL done = NO;
[[ref.root child:@"ENTOHTNUHOE/ONTEHNUHTOE"]
setPriority:@"blah"
withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
2017-05-15 12:27:07 -07:00
done = YES;
2019-08-05 13:27:47 -07:00
}];
WAIT_FOR(done);
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
- (void)waitForQueue:(FIRDatabaseReference *)ref {
dispatch_sync([FIRDatabaseQuery sharedQueue], ^{
});
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
- (void)waitForEvents:(FIRDatabaseReference *)ref {
[self waitForQueue:ref];
__block BOOL done = NO;
dispatch_async(dispatch_get_main_queue(), ^{
done = YES;
});
WAIT_FOR(done);
2017-05-15 12:27:07 -07:00
}
- (void)waitForValueOf:(FIRDatabaseQuery *)ref toBe:(id)expected {
2019-08-05 13:27:47 -07:00
__block id value;
FIRDatabaseHandle handle = [ref observeEventType:FIRDataEventTypeValue
withBlock:^(FIRDataSnapshot *snapshot) {
value = snapshot.value;
}];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
@try {
[self waitUntil:^BOOL {
return [value isEqual:expected];
}];
} @catch (NSException *exception) {
@throw [NSException exceptionWithName:@"DidNotGetValue"
reason:@"Did not get expected value"
userInfo:@{
@"expected" : (!expected ? @"nil" : expected),
@"actual" : (!value ? @"nil" : value)
}];
} @finally {
[ref removeObserverWithHandle:handle];
}
2017-05-15 12:27:07 -07:00
}
- (void)waitForExportValueOf:(FIRDatabaseQuery *)ref toBe:(id)expected {
2019-08-05 13:27:47 -07:00
__block id value;
FIRDatabaseHandle handle = [ref observeEventType:FIRDataEventTypeValue
withBlock:^(FIRDataSnapshot *snapshot) {
value = snapshot.valueInExportFormat;
}];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
@try {
[self waitUntil:^BOOL {
return [value isEqual:expected];
}];
} @catch (NSException *exception) {
if ([exception.name isEqualToString:@"Timed out"]) {
@throw [NSException exceptionWithName:@"DidNotGetValue"
reason:@"Did not get expected value"
userInfo:@{
@"expected" : (!expected ? @"nil" : expected),
@"actual" : (!value ? @"nil" : value)
}];
} else {
@throw exception;
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
} @finally {
[ref removeObserverWithHandle:handle];
}
2017-05-15 12:27:07 -07:00
}
- (void)waitForCompletionOf:(FIRDatabaseReference *)ref setValue:(id)value {
2019-08-05 13:27:47 -07:00
[self waitForCompletionOf:ref setValue:value andPriority:nil];
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
- (void)waitForCompletionOf:(FIRDatabaseReference *)ref
setValue:(id)value
andPriority:(id)priority {
__block BOOL done = NO;
[ref setValue:value
andPriority:priority
withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
2017-05-15 12:27:07 -07:00
done = YES;
2019-08-05 13:27:47 -07:00
}];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
@try {
WAIT_FOR(done);
} @catch (NSException *exception) {
@throw [NSException exceptionWithName:@"DidNotSetValue"
reason:@"Did not complete setting value"
userInfo:@{
@"ref" : [ref description],
@"done" : done ? @"true" : @"false",
@"value" : (!value ? @"nil" : value),
@"priority" : (!priority ? @"nil" : priority)
}];
}
2017-05-15 12:27:07 -07:00
}
- (void)waitForCompletionOf:(FIRDatabaseReference *)ref updateChildValues:(NSDictionary *)values {
2019-08-05 13:27:47 -07:00
__block BOOL done = NO;
[ref updateChildValues:values
withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
2017-05-15 12:27:07 -07:00
done = YES;
2019-08-05 13:27:47 -07:00
}];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
@try {
WAIT_FOR(done);
} @catch (NSException *exception) {
@throw [NSException
exceptionWithName:@"DidNotUpdateChildValues"
reason:@"Could not finish updating child values"
userInfo:@{@"ref" : [ref description], @"values" : (!values ? @"nil" : values)}];
}
2017-05-15 12:27:07 -07:00
}
@end