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

146 lines
3.8 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.
*/
#import "FDevice.h"
2019-08-05 13:27:47 -07:00
#import <XCTest/XCTest.h>
2017-11-22 09:19:10 -08:00
#import <FirebaseDatabase/FIRDatabaseReference.h>
2017-05-15 12:27:07 -07:00
#import "FIRDatabaseConfig_Private.h"
2019-08-05 13:27:47 -07:00
#import "FIRDatabaseReference_Private.h"
#import "FRepoManager.h"
2017-05-15 12:27:07 -07:00
#import "FTestHelpers.h"
2019-08-05 13:27:47 -07:00
#import "SenTest+FWaiter.h"
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
@interface FDevice () {
FIRDatabaseConfig *config;
NSString *url;
BOOL isOnline;
BOOL disposed;
2017-05-15 12:27:07 -07:00
}
@end
@implementation FDevice
- (id)initOnline {
2019-08-05 13:27:47 -07:00
FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
return [self initOnlineWithUrl:[ref description]];
2017-05-15 12:27:07 -07:00
}
- (id)initOffline {
2019-08-05 13:27:47 -07:00
FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
return [self initOfflineWithUrl:[ref description]];
2017-05-15 12:27:07 -07:00
}
- (id)initOnlineWithUrl:(NSString *)firebaseUrl {
2019-08-05 13:27:47 -07:00
return [self initWithUrl:firebaseUrl andOnline:YES];
2017-05-15 12:27:07 -07:00
}
- (id)initOfflineWithUrl:(NSString *)firebaseUrl {
2019-08-05 13:27:47 -07:00
return [self initWithUrl:firebaseUrl andOnline:NO];
2017-05-15 12:27:07 -07:00
}
static NSUInteger deviceId = 0;
- (id)initWithUrl:(NSString *)firebaseUrl andOnline:(BOOL)online {
2019-08-05 13:27:47 -07:00
self = [super init];
if (self) {
config = [FTestHelpers
configForName:[NSString stringWithFormat:@"device-%lu", (unsigned long)deviceId++]];
config.persistenceEnabled = YES;
url = firebaseUrl;
isOnline = online;
}
return self;
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
- (void)dealloc {
if (!self->disposed) {
[NSException raise:NSInternalInconsistencyException format:@"Forgot to dispose device"];
}
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
- (void)dispose {
// TODO: clear persistence
[FRepoManager disposeRepos:self->config];
self->disposed = YES;
2017-05-15 12:27:07 -07:00
}
- (void)goOffline {
2019-08-05 13:27:47 -07:00
isOnline = NO;
[FRepoManager interrupt:config];
2017-05-15 12:27:07 -07:00
}
- (void)goOnline {
2019-08-05 13:27:47 -07:00
isOnline = YES;
[FRepoManager resume:config];
2017-05-15 12:27:07 -07:00
}
- (void)restartOnline {
2019-08-05 13:27:47 -07:00
@autoreleasepool {
[FRepoManager disposeRepos:config];
isOnline = YES;
}
2017-05-15 12:27:07 -07:00
}
- (void)restartOffline {
2019-08-05 13:27:47 -07:00
@autoreleasepool {
[FRepoManager disposeRepos:config];
isOnline = NO;
}
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
// Waits for us to connect and then does an extra round-trip to make sure all initial state
// restoration is completely done.
- (void)waitForIdleUsingWaiter:(XCTest *)waiter {
[self do:^(FIRDatabaseReference *ref) {
__block BOOL connected = NO;
FIRDatabaseHandle handle =
[[ref.root child:@".info/connected"] observeEventType:FIRDataEventTypeValue
withBlock:^(FIRDataSnapshot *snapshot) {
connected = [snapshot.value boolValue];
}];
[waiter waitUntil:^BOOL {
return connected;
2017-05-15 12:27:07 -07:00
}];
2019-08-05 13:27:47 -07:00
[ref.root removeObserverWithHandle:handle];
// 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) {
done = YES;
}];
[waiter waitUntil:^BOOL {
return done;
}];
}];
2017-05-15 12:27:07 -07:00
}
- (void)do:(void (^)(FIRDatabaseReference *))action {
2019-08-05 13:27:47 -07:00
@autoreleasepool {
FIRDatabaseReference *ref = [[[[FIRDatabaseReference alloc] initWithConfig:self->config]
database] referenceFromURL:self->url];
if (!isOnline) {
[FRepoManager interrupt:config];
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
action(ref);
}
2017-05-15 12:27:07 -07:00
}
@end