Files
firebase-ios-sdk/Example/Database/Tests/Integration/FDotInfo.m
T

206 lines
7.9 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 "FDotInfo.h"
#import "FIRDatabaseConfig_Private.h"
2019-08-05 13:27:47 -07:00
#import "FTestHelpers.h"
2017-05-15 12:27:07 -07:00
@implementation FDotInfo
2019-08-05 13:27:47 -07:00
- (void)testCanGetReferenceToInfoNodes {
FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[ref.root child:@".info"];
[ref.root child:@".info/foo"];
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
- (void)testCantWriteToInfo {
FIRDatabaseReference *ref = [[FTestHelpers getRandomNode].root child:@".info"];
XCTAssertThrows([ref setValue:@"hi"], @"Cannot write to path at /.info");
XCTAssertThrows([ref setValue:@"hi" andPriority:@5], @"Cannot write to path at /.info");
XCTAssertThrows([ref setPriority:@"hi"], @"Cannot write to path at /.info");
XCTAssertThrows([ref runTransactionBlock:^FIRTransactionResult *(FIRMutableData *currentData) {
return [FIRTransactionResult successWithValue:currentData];
}],
@"Cannot write to path at /.info");
XCTAssertThrows([ref removeValue], @"Cannot write to path at /.info");
XCTAssertThrows([[ref child:@"test"] setValue:@"hi"], @"Cannot write to path at /.info");
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
- (void)testCanWatchInfoConnected {
FIRDatabaseReference *rootRef = [FTestHelpers getRandomNode].root;
__block BOOL done = NO;
[[rootRef child:@".info/connected"] observeEventType:FIRDataEventTypeValue
withBlock:^(FIRDataSnapshot *snapshot) {
if ([[snapshot value] boolValue]) {
done = YES;
}
}];
[self waitUntil:^{
return done;
}];
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
- (void)testInfoConnectedGoesToFalseOnDisconnect {
FIRDatabaseConfig *cfg = [FTestHelpers configForName:@"test-config"];
FIRDatabaseReference *rootRef = [[FIRDatabaseReference alloc] initWithConfig:cfg];
__block BOOL everConnected = NO;
__block NSMutableString *connectedHistory = [[NSMutableString alloc] init];
[[rootRef child:@".info/connected"]
observeEventType:FIRDataEventTypeValue
withBlock:^(FIRDataSnapshot *snapshot) {
if ([[snapshot value] boolValue]) {
everConnected = YES;
}
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
if (everConnected) {
[connectedHistory appendString:([[snapshot value] boolValue] ? @"YES," : @"NO,")];
}
}];
[self waitUntil:^{
return everConnected;
}];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[FRepoManager interrupt:cfg];
[FRepoManager resume:cfg];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[self waitUntil:^BOOL {
return [connectedHistory isEqualToString:@"YES,NO,YES,"];
}];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[FRepoManager interrupt:cfg];
[FRepoManager disposeRepos:cfg];
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
- (void)testInfoServerTimeOffset {
FIRDatabaseConfig *cfg = [FTestHelpers configForName:@"test-config"];
FIRDatabaseReference *ref = [[FIRDatabaseReference alloc] initWithConfig:cfg];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
// make sure childByAutoId works
[ref childByAutoId];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
NSMutableArray *offsets = [[NSMutableArray alloc] init];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[[ref child:@".info/serverTimeOffset"] observeEventType:FIRDataEventTypeValue
withBlock:^(FIRDataSnapshot *snapshot) {
NSLog(@"got value: %@", snapshot.value);
[offsets addObject:snapshot.value];
}];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
WAIT_FOR(offsets.count == 1);
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
XCTAssertTrue([[offsets objectAtIndex:0] isKindOfClass:[NSNumber class]],
@"Second element should be a number, in milliseconds");
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
// make sure childByAutoId still works
[ref childByAutoId];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[FRepoManager interrupt:cfg];
[FRepoManager disposeRepos:cfg];
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
- (void)testManualConnectionManagement {
FIRDatabaseConfig *cfg = [FTestHelpers configForName:@"test-config"];
FIRDatabaseConfig *altCfg = [FTestHelpers configForName:@"alt-config"];
2017-06-30 10:47:37 -07:00
2019-08-05 13:27:47 -07:00
FIRDatabaseReference *ref = [[FIRDatabaseReference alloc] initWithConfig:cfg];
FIRDatabaseReference *refAlt = [[FIRDatabaseReference alloc] initWithConfig:altCfg];
2017-06-30 10:47:37 -07:00
2019-08-05 13:27:47 -07:00
// Wait until we're connected to both Firebases
__block BOOL ready = NO;
[[ref child:@".info/connected"] observeEventType:FIRDataEventTypeValue
withBlock:^(FIRDataSnapshot *snapshot) {
ready = [[snapshot value] boolValue];
}];
[self waitUntil:^{
return ready;
}];
[[ref child:@".info/connected"] removeAllObservers];
2017-06-30 10:47:37 -07:00
2019-08-05 13:27:47 -07:00
ready = NO;
[[refAlt child:@".info/connected"] observeEventType:FIRDataEventTypeValue
withBlock:^(FIRDataSnapshot *snapshot) {
ready = [[snapshot value] boolValue];
}];
[self waitUntil:^{
return ready;
}];
[[refAlt child:@".info/connected"] removeAllObservers];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[FIRDatabaseReference goOffline];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
// Ensure we're disconnected from both Firebases
ready = NO;
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[[ref child:@".info/connected"]
observeSingleEventOfType:FIRDataEventTypeValue
withBlock:^(FIRDataSnapshot *snapshot) {
XCTAssertFalse([[snapshot value] boolValue],
@".info/connected should be false");
ready = YES;
}];
[self waitUntil:^{
return ready;
}];
ready = NO;
[[refAlt child:@".info/connected"]
observeSingleEventOfType:FIRDataEventTypeValue
withBlock:^(FIRDataSnapshot *snapshot) {
XCTAssertFalse([[snapshot value] boolValue],
@".info/connected should be false");
ready = YES;
}];
[self waitUntil:^{
return ready;
}];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
// Ensure that we don't automatically reconnect upon new Firebase creation
FIRDatabaseReference *refDup = [[FIRDatabaseReference alloc] initWithConfig:altCfg];
[[refDup child:@".info/connected"] observeEventType:FIRDataEventTypeValue
withBlock:^(FIRDataSnapshot *snapshot) {
if ([[snapshot value] boolValue]) {
XCTFail(@".info/connected should remain false");
}
}];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
// Wait for 1.5 seconds to make sure connected remains false
[NSThread sleepForTimeInterval:1.5];
[[refDup child:@".info/connected"] removeAllObservers];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[FIRDatabaseReference goOnline];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
// Ensure we're reconnected to both Firebases
ready = NO;
[[ref child:@".info/connected"] observeEventType:FIRDataEventTypeValue
withBlock:^(FIRDataSnapshot *snapshot) {
ready = [[snapshot value] boolValue];
}];
[self waitUntil:^{
return ready;
}];
[[ref child:@".info/connected"] removeAllObservers];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
ready = NO;
[[refAlt child:@".info/connected"] observeEventType:FIRDataEventTypeValue
withBlock:^(FIRDataSnapshot *snapshot) {
ready = [[snapshot value] boolValue];
}];
[self waitUntil:^{
return ready;
}];
[[refAlt child:@".info/connected"] removeAllObservers];
2017-05-15 12:27:07 -07:00
}
@end