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

232 lines
6.6 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 <XCTest/XCTest.h>
#import "FTestBase.h"
2019-08-05 13:27:47 -07:00
#import "FTestHelpers.h"
2017-05-15 12:27:07 -07:00
@interface FKeepSyncedTest : FTestBase
@end
@implementation FKeepSyncedTest
static NSUInteger fGlobalKeepSyncedTestCounter = 0;
- (void)assertIsKeptSynced:(FIRDatabaseQuery *)query {
2019-08-05 13:27:47 -07:00
FIRDatabaseReference *ref = query.ref;
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
// First set a unique value to the value of child
fGlobalKeepSyncedTestCounter++;
NSNumber *currentValue = @(fGlobalKeepSyncedTestCounter);
__block BOOL done = NO;
[ref setValue:@{@"child" : currentValue}
withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
2017-05-15 12:27:07 -07:00
XCTAssertNil(error);
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
WAIT_FOR(done);
done = NO;
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
// Next go offline, if it's kept synced we should have kept the value, after going offline no way
// to get the value except from cache
[FIRDatabaseReference goOffline];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[query observeSingleEventOfType:FIRDataEventTypeValue
withBlock:^(FIRDataSnapshot *snapshot) {
// We should receive an event
XCTAssertEqualObjects(snapshot.value, @{@"child" : currentValue});
done = YES;
}];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
WAIT_FOR(done);
// All good, go back online
[FIRDatabaseReference goOnline];
2017-05-15 12:27:07 -07:00
}
- (void)assertNotKeptSynced:(FIRDatabaseQuery *)query {
2019-08-05 13:27:47 -07:00
FIRDatabaseReference *ref = query.ref;
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
// First set a unique value to the value of child
fGlobalKeepSyncedTestCounter++;
NSNumber *currentValue = @(fGlobalKeepSyncedTestCounter);
fGlobalKeepSyncedTestCounter++;
NSNumber *newValue = @(fGlobalKeepSyncedTestCounter);
__block BOOL done = NO;
[ref setValue:@{@"child" : currentValue}
withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
2017-05-15 12:27:07 -07:00
XCTAssertNil(error);
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
WAIT_FOR(done);
done = NO;
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
// Next go offline, if it's kept synced we should have kept the value, after going offline no way
// to get the value except from cache
[FIRDatabaseReference goOffline];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[query observeSingleEventOfType:FIRDataEventTypeValue
withBlock:^(FIRDataSnapshot *snapshot) {
// We should receive an event
XCTAssertEqualObjects(snapshot.value, @{@"child" : newValue});
done = YES;
}];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
// By now, if we had it synced we should have gotten an event with the wrong value
// Write a new value so the value event listener will be triggered
[ref setValue:@{@"child" : newValue}];
WAIT_FOR(done);
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
// All good, go back online
[FIRDatabaseReference goOnline];
2017-05-15 12:27:07 -07:00
}
- (void)testKeepSynced {
2019-08-05 13:27:47 -07:00
FIRDatabaseReference *ref = [FTestHelpers getRandomNodeWithoutPersistence];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[ref keepSynced:YES];
[self assertIsKeptSynced:ref];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[ref keepSynced:NO];
[self assertNotKeptSynced:ref];
2017-05-15 12:27:07 -07:00
}
- (void)testManyKeepSyncedCallsDontAccumulate {
2019-08-05 13:27:47 -07:00
FIRDatabaseReference *ref = [FTestHelpers getRandomNodeWithoutPersistence];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[ref keepSynced:YES];
[ref keepSynced:YES];
[ref keepSynced:YES];
[self assertIsKeptSynced:ref];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
// If it were balanced, this would not be enough
[ref keepSynced:NO];
[ref keepSynced:NO];
[self assertNotKeptSynced:ref];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
// If it were balanced, this would not be enough
[ref keepSynced:YES];
[self assertIsKeptSynced:ref];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
// cleanup
[ref keepSynced:NO];
2017-05-15 12:27:07 -07:00
}
- (void)testRemoveAllObserversDoesNotAffectKeepSynced {
2019-08-05 13:27:47 -07:00
FIRDatabaseReference *ref = [FTestHelpers getRandomNodeWithoutPersistence];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[ref keepSynced:YES];
[self assertIsKeptSynced:ref];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[ref removeAllObservers];
[self assertIsKeptSynced:ref];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
// cleanup
[ref keepSynced:NO];
2017-05-15 12:27:07 -07:00
}
- (void)testRemoveSingleObserverDoesNotAffectKeepSynced {
2019-08-05 13:27:47 -07:00
FIRDatabaseReference *ref = [FTestHelpers getRandomNodeWithoutPersistence];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[ref keepSynced:YES];
[self assertIsKeptSynced:ref];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
__block BOOL done = NO;
FIRDatabaseHandle handle = [ref observeEventType:FIRDataEventTypeValue
withBlock:^(FIRDataSnapshot *snapshot) {
done = YES;
}];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
WAIT_FOR(done);
[ref removeObserverWithHandle:handle];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[self assertIsKeptSynced:ref];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
// cleanup
[ref keepSynced:NO];
2017-05-15 12:27:07 -07:00
}
- (void)testKeepSyncedNoDoesNotAffectExistingObserver {
2019-08-05 13:27:47 -07:00
FIRDatabaseReference *ref = [FTestHelpers getRandomNodeWithoutPersistence];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[ref keepSynced:YES];
[self assertIsKeptSynced:ref];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
__block BOOL done = NO;
FIRDatabaseHandle handle = [ref observeEventType:FIRDataEventTypeValue
withBlock:^(FIRDataSnapshot *snapshot) {
done = [snapshot.value isEqual:@"done"];
}];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
// cleanup
[ref keepSynced:NO];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[ref setValue:@"done"];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
WAIT_FOR(done);
[ref removeObserverWithHandle:handle];
2017-05-15 12:27:07 -07:00
}
- (void)testDifferentQueriesAreIndependent {
2019-08-05 13:27:47 -07:00
FIRDatabaseReference *ref = [FTestHelpers getRandomNodeWithoutPersistence];
FIRDatabaseQuery *query1 = [ref queryLimitedToFirst:1];
FIRDatabaseQuery *query2 = [ref queryLimitedToFirst:2];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[query1 keepSynced:YES];
[self assertIsKeptSynced:query1];
[self assertNotKeptSynced:query2];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[query2 keepSynced:YES];
[self assertIsKeptSynced:query1];
[self assertIsKeptSynced:query2];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[query1 keepSynced:NO];
[self assertIsKeptSynced:query2];
[self assertNotKeptSynced:query1];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[query2 keepSynced:NO];
[self assertNotKeptSynced:query1];
[self assertNotKeptSynced:query2];
2017-05-15 12:27:07 -07:00
}
- (void)testChildIsKeptSynced {
2019-08-05 13:27:47 -07:00
FIRDatabaseReference *ref = [FTestHelpers getRandomNodeWithoutPersistence];
FIRDatabaseReference *child = [ref child:@"random-child"];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[ref keepSynced:YES];
[self assertIsKeptSynced:child];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
// cleanup
[ref keepSynced:NO];
2017-05-15 12:27:07 -07:00
}
- (void)testRootIsKeptSynced {
2019-08-05 13:27:47 -07:00
FIRDatabaseReference *ref = [[FTestHelpers getRandomNodeWithoutPersistence] root];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
[ref keepSynced:YES];
// Run on random child to make sure writes from this test doesn't interfere with any other tests.
[self assertIsKeptSynced:[ref childByAutoId]];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
// cleanup
[ref keepSynced:NO];
2017-05-15 12:27:07 -07:00
}
// TODO[offline]: Cancel listens for keep synced....
@end